CAN Implementation
CAN Implementation
on
STM32L476
A new STM32Cube project is created in STM32CubeIDE under the name
L476Disco_CAN by selecting STM32L476G-Disco board in Target
selector.
Using STM32CubeIDE Device Configuration Tool GUI we can change
initial settings as per our requirements whose code is auto generated
when settings are saved.
Some of the useful initial settings changed are as follows:
1. Clock HSE under PinOut & Configuration section “System Core >
RCC” changed to Crystal Oscillator.
2. While under Clock Configuration selections are changed to obtain
frequency of 72Mhz by selecting the HSE from the PLL Source Mux and
PLLCLK from the System Clock Mux.
3. Next we enable CAN under “Connectivity > CAN” by checking the
“Activated” check box. The Prescaler divides the APB1 peripheral
clock before it gets input to the CAN peripheral. Every bit period in
CAN is divided up into time quanta and sample point is partway
through. Essentially we want to pick our Segment 1 and Segment 2
time quanta such that the total bit time matches our baud rate.
- The code to start the CAN peripheral and attach the receive
interrupt to call can_irq() is also shown. Slide 14, 21, 23
- Also can_irq() needs to be declared before it can be used. Slide 24
FilterIdLow - Specifies the filter identification number (LSBs for a 32-bit configuration, second one for a
16-bit configuration). This parameter must be a number between Min_Data =
0x0000 and Max_Data = 0xFFFF.
FilterMaskIdHigh - Specifies the filter mask number or identification number, according to the mode
(MSBs for a 32-bit configuration, first one for a 16-bit configuration).
This parameter must be a number between Min_Data = 0x0000 and
Max_Data = 0xFFFF.
FilterMaskIdLow - Specifies the filter mask number or identification number, according to the mode
LSBs for a 32-bit configuration, second one for a 16-bit configuration). This
parameter must be a number between Min_Data = 0x0000 and Max_Data =
0xFFFF.
FilterFIFOAssignment - Specifies the FIFO (0 or 1U) which will be assigned to the filter. This parameter
can be a value of CAN_FILTER_FIFO0 or CAN_FILTER_FIFO1
FilterBank - Specifies the filter bank which will be initialized. For single CAN instance(14 dedicated filter
banks), this parameter must be a number between Min_Data = 0 and Max_Data =
13.
CAN_FilterTypeDef ctd.
FilterMode - Specifies the filter mode to be initialized. This parameter can be a value of
CAN_FILTERMODE_IDMASK - Identifier mask mode
CAN_FILTERMODE_IDLIST - Identifier list mode
SlaveStartFilterBank - Select the start filter bank for the slave CAN instance.
For dual CAN instances, all filter banks with lower index are assigned to master
CAN instance, whereas all filter banks with greater index are assigned to slave
CAN instance. This parameter must be a number between Min_Data = 0 and
Max_Data = 27.
1. HAL_CAN_ConfigFilter(CAN_HandleTypeDef *hcan, const CAN_FilterTypeDef
*sFilterConfig)
Configures the CAN reception filter according to the specified parameters in
the CAN_FilterInitStruct.
It takes 2 parameters - hcan pointer to a CAN_HandleTypeDef structure
that contains the configuration information for the specified CAN. ( i.e.
hcan1)
- sFilterConfig pointer to a CAN_FilterTypeDef structure that
contains the filter configuration information. (i.e. sf)
2. HAL_CAN_RegisterCallback(CAN_HandleTypeDef *hcan,
HAL_CAN_CallbackIDTypeDef CallbackID, void (* pCallback)(CAN_HandleTypeDef *_hcan) )
For registering a CAN CallBack and is to be used instead of the weak predefined
callback
It takes 3 parameters - hcan pointer to a CAN_HandleTypeDef structure that
contains the configuration information for CAN module. ( i.e. hcan1)
- CallbackID which is the ID of the callback to be registered
(i.e. HAL_CAN_RX_FIFO0_MSG_PENDING_CB_ID = 0x06U) Slide 17
- pCallback which is the pointer to the Callback function
(i.e. can_irq)
3. HAL_CAN_Start(CAN_HandleTypeDef *hcan)
For starting the CAN module.
It takes 1 parameter - hcan pointer to a CAN_HandleTypeDef structure
that contains the configuration information for the specified CAN. ( i.e.
hcan1)
Adds a message to the first free Tx mailbox and activates the corresponding transmission
request.
It takes 4 parameters - hcan pointer to a CAN_HandleTypeDef structure that contains the
configuration information for the specified CAN. ( i.e. hcan1)
- pHeader pointer to a CAN_TxHeaderTypeDef structure. ( i.e. msg)
- aData array containing the payload of the Tx frame. ( i.e. data)
- pTxMailbox pointer to a variable where the function will return the
TxMailbox used to store the Tx message. ( i.e. mb)
Interrupt handler can_isr can be defined to handle message reception as follows: