Datasheet
Copyright © 2013 ARM Ltd. All rights reserved
CAN: Controller Area Network Lab using ST STM32 Cortex-M processors. www.keil.com
16
2) CAN.c (These are MCBSTM32E example line numbers but might change without notice)
Configuring the CAN Controller: (CAN.C)
There are several things that must be done to properly configure the CAN controller. These are done in CAN.c by functions
that are called by CanDemo.c. Examples are found in the function CAN_setup (lines 37 to 70) as shown in µVision:
1. Enable and set the clock for the CAN controller. TIP: The clock must be stable for CAN. No R-C oscillators here.
2. Configure GPIO ports PB8 and PB9 for the transmit and receive lines to the transceiver chip.
3. Enable the interrupts for the transmit and receive functions.
4. Set CAN_BTR: This is a 32 CAN controller register where things such as bit timing, bus frequency, sample point
and silent and loop back modes are set. In the Keil example, the baudrate is set to 500 Kbps (bits per second).
TIP: Sometimes timing settings can cause strange problems. If you experience some unusual problems you might want to
study CAN timing in greater detail. For small systems, the default settings or those suggested by the processor manufacturer
will work satisfactorily. You can experimentally adjust these settings for the most robust bus performance.
All CAN controllers have the same general settings for bit timing because of the licensing agreements with Robert Bosch
GmbH. For a detailed explanation of CAN bit timing see www.port.de/pdf/CAN_Bit_Timing.pdf and for the calculations see
the ST Reference Manual RM0008 or RM0090 (STM32F4).
TIP: All CAN controllers on a network should have consistent BTR values for stable operation.
Other Functions in CAN.c:
CAN_start: Starts the CAN controller by ending the initialization sequence.
CAN_waitReady: Waits until transmit mailbox is ready – then can add another message to be transmitted.
CAN_wrMsg: Write a message to the CAN controller and transmit it.
CAN_rdMsg: Read a message from the CAN controller and releases it to be sent to the STM32 processor.
CAN_wrFilter: Configure the acceptance filter. This is not discussed in detail in this article.
USB_HP_CAN1_TX_IRQHandler and USB_LP_CAN1_RX0_IRQHandler: The interrupt handlers.
CANx_TX_IRQHandler and CANx_RX0_IRQHandler: The interrupt handlers in Discovery CAN.c.
3) CanDemo.c (These are MCBSTM32E example line numbers but might change without notice)
This contains the main function and contains the example program that reads the voltage on the A/D converter and sends its
value as a CAN data byte with an 11 bit ID of 0x21. CanDemo.c contains functions to configure and read the A/D converter,
display the A/D values on the LCD and call the functions that initialize the CAN controller.
Transmitting a CAN Message:
Lines 102 to 106 puts the frame values into the structure CAN_TxMsg. (Except for the data byte from the variable val_Tx)
This CAN message will send one data byte. For example, if you change the value in the member CAN_TxMsg.len to “3”,
three data bytes will be sent on the bus. What data will be in them depends on the contents of the array CAN_TxMsg.data.
TIP: If you send more data bytes than you have data, it is a good idea to fill the empty data bytes with either 0 or 0xFF.
Lines 133 puts the val_Tx value into the data member CAN_TxMsg.data[0] in data byte 0 and line 134 transmits it.
118 CAN_TxMsg.id = 33; // initialize message to send
119 for (i = 0; i < 8; i++) CAN_TxMsg.data[i] = 0;
120 CAN_TxMsg.len = 1;
121 CAN_TxMsg.format = STANDARD_FORMAT;
122 CAN_TxMsg.type = DATA_FRAME;
130 if (CAN_TxRdy) { /* tx msg on CAN Ctrl */
131 CAN_TxRdy = 0;
133 CAN_TxMsg.data[0] = val_Tx; /* data[0] = ADC value */
134 CAN_wrMsg (&CAN_TxMsg); /* transmit message */ }