0% found this document useful (0 votes)
109 views

CAN Implementation

Uploaded by

shanku kak
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

CAN Implementation

Uploaded by

shanku kak
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Implementation of CAN

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.

For these settings we go to “Parameter Settings” and set Bit Timing


Parameters as required. For obtaining 500kbps Baudrate we set
Prescaler at 18, Bit Segment 1 at 3 Times and Bit Segment 2 at 4
Times. Slide 6
Other settings under “Basic Parameters” such as Automatic
Retransmission can be enabled too.

For enabling Receive message Interrupt we go to “NVIC settings” and


enable RX0 and RX1 interrupts. Also we enable CAN callback
registration in “Project Manager > Advanced Settings”. Slide 7, 8
- After saving the settings, boilerplate code is generated, we can see
initialized instance of CAN i.e. hcan1 in definition of function
MX_CAN1_Init(). Slide 10

- In order to receive messages, we first need to setup a CAN filter


which controls what messages are allowed through and to which
FIFO they’re stored. To let all messages through and put them in
FIFO0 code used is shown. Slide 11, 12, 13

- 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

- Now we need to create a message. Code for transmission of string


“Hello!” as Data is shown. Slide 19, 20, 22
CAN_FilterTypeDef
FilterIdHigh - Specifies the filter identification number (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.

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

FilterScale - Specifies the filter scale. This parameter can be a value of


CAN_FILTERSCALE_16BIT - Two 16-bit filters
CAN_FILTERSCALE_32BIT - One 32-bit filter

FilterActivation - Enable or disable the filter.

SlaveStartFilterBank - Select the start filter bank for the slave CAN instance.

For single CAN instances, this parameter is meaningless.

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)

4. HAL_CAN_ActivateNotification(CAN_HandleTypeDef *hcan, uint32_t


ActiveITs)
For enabling interrupts.
It takes 2 parameters - hcan pointer to a CAN_HandleTypeDef structure that
contains the configuration information for CAN module. ( i.e. hcan1)
- ActiveITs indicates which interrupts will be enabled. This
parameter can be any combination of defined CAN interrupts in Interrupt
Enable Register CAN_IER
(i.e. CAN_IT_RX_FIFO0_MSG_PENDING) Slide 18
For sending data we define required variables as follows:

uint32_t mb; // mailbox defined


CAN_TxHeaderTypeDef msg; // Tx Header defined
uint8_t data[] = "Hello!"; // Data to be transmitted

Elements of TxHeaderTypeDef structure are defined:

msg.StdId = 127; // Identifier provided to the message


msg.IDE = CAN_ID_STD; // Standard Identifier
msg.RTR = CAN_RTR_DATA; // Data frame is defined
msg.DLC = 6; // Data Length is defined
msg.TransmitGlobalTime = DISABLE;

To transmit CAN message we use HAL interface AddTxMessage():

if (HAL_CAN_AddTxMessage(&hcan1, &msg, data, &mb) != HAL_OK) {


Error_Handler();
}
HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, const
CAN_TxHeaderTypeDef *pHeader, const uint8_t aData[], uint32_t *pTxMailbox)

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:

void can_irq(CAN_HandleTypeDef *pcan) {


CAN_RxHeaderTypeDef msg; // Rx Header defined
uint8_t data[8]; // Data array declared
HAL_CAN_GetRxMessage(pcan, CAN_RX_FIFO0, &msg, data);
}

HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, uint32_t RxFifo,


CAN_RxHeaderTypeDef *pHeader, uint8_t aData[])
Get a CAN frame from the Rx FIFO zone into the message RAM.
It has 4 parameters - hcan pointer to an CAN_HandleTypeDef structure that contains the
configuration information for the specified CAN.
- RxFifo Fifo number of the received message to be read.
- pHeader pointer to a CAN_RxHeaderTypeDef structure where the
header of the Rx frame will be stored.
- aData array where the payload of the Rx frame will be stored.

You might also like