CAN bus protocol implementation

I want to learn and implement the CAN BUS protocol. I implemented the UART, SPI, I2C, and One Wire Bus protocols using the MSP430 Launchpad in software. Now I want to learn about the CAN Bus protocol. I have an mBed LPC 1768 Cortex M3 Development board. MBed has a Can Bus Library, but I want to write my own library so that I can study it in detail, that is, the way I did for other communication protocols.

I can’t find the right resources to start with, and the material seems to be scattered across the network. Can anyone indicate how I write and implement the CAN Bus protocol with development boards available to me.

thanks

+5
source share
3 answers

The development of the CAN library is relatively simple compared to I2C or SPI. This is because the Cortex CAN Controller takes care of most of the tricky things.

To transfer data, you need to write ID and Data in the assigned registers and set the bit for data transfer.

This NXP application note can be very helpful to you.

I would recommend you implement the following functions:

  1. InitCAN - This should set the indicated CAN baud rate.
  2. SetFilters - most CAN controllers come with receive filters, so it's good to have this
  3. SendData - make sure you accept parameters like ID_Type, RTR, etc.
  4. RecieveData - this can be a lock or interrupt based on.

Before you begin, read the basics of CAN to understand. Microchip AN713 and AN754 application notes are a good source. Also the Vector website and Wikipedia article.

In addition, you can always leave your doubts here or at Electronics.StackExchange.com :)

+5
source

So, this post is quite old, but people can look at it again like this: firstly, the Can bus is not a convenient protocol at all, such as USART or IC2, so you have to be very accurate about your bit, there are tools for this but I suggest you calculate them manually. For the microcontroller, I would suggest the STM32 and, in my opinion, would be far from the PIC series. If it is only CAN-BUS without higher level protocols such as SAE J1939, the steps are quite simple and straightforward:

1) You can initialize

2) Put CAN in setup mode and remember that you can set the baud rate, mask and filters only in setup mode!

3) Set the bit rate registers.

4) Install the mask and filters. If you need to receive all messages, just set the mask to 0x00. Then the filter will not care.

5) Set CAN to normal mode or reverse loop mode. (Reverse looping mode is mainly used for debugging purposes.)

Some of the wonderful things that people are trying to realize may be missed at first: *** For a successful transfer, you need at least 2 working CAN nodes. (of course, with an appropriate data rate). Therefore, if you want to send some data via CAN with 1 node, this will not be successful. Because your transmitter will not receive ACK.

*** Most likely, you will need a CAN transceiver. Remember to install a 100 ohm resistor or similar between the transceiver's Tx and Rx pins.

+1
source

I used canking software to talk to the mcp25050 when I found out how to implement the protocol protocol using hcs12 dragonboard. This has helped a lot because canking will initialize everything for you when you get on the bus, and all you have to do is learn to write and receive. If you want to know how to initialize, follow these steps:

  • Enables bus capability by setting bits in CAN 1 control register

  • Enable can initialize control register 0

  • wait while the bus is in initialization mode by checking the control register 1 bit

  • Enables the bus by setting the bit in CAN Control Register 1 again and setting the clock source - Ethier or eclock bus clock

  • set predictor speed and Tq with bus synchronization register

  • set sampling time and prop_seg1, prop_seg2 and phase_seg

  • set the acceptance identifier in the reception register of the identifier 0-3 or 0-7 - set your bank to get everything to set them to 00, because when you perform a comparison, the CAN bus can complement the comparison with the identifier included in

  • set the identifier mask register 0-3 or 0-7, if you want not to care about any of the bits, they are all set to FF

  • set identifier receiver acceptance register to 32-bit extended or 11 bit - I use 32

  • set control register 0 to return to normal mode

  • wait until the bus is in normal mode by checking Control Register 1

after that you can start changing the registers or reading data to do this, you must select an empty buffer, write your identifier for writing or requesting data, and then enter the address, mask and value in 3 transmitter registers when writing and then specify dlc (3 when writing and 8-1 when reading). to transmit the identifier and the data you must set, you can pass the transfer flag so that it matches the choice of the transfer buffer. ** Depending on which identifier you use, the bit offset can be tedious, so if you had a problem, I would suggest debugging and see that your transfer buffer selection registers are stored. I had this error because I did not move correctly when I sent messages to mcp25050

0
source

All Articles