HAL Library Development Notes - CAN Communication 🚧
This article is based on the in-house RobotCtrl development kit, with the microcontroller core being STM32F407ZET6, and TJA1050 chip used for CAN communication. For the schematic and detailed information, please refer to RobotCtrl - STM32 Universal Development Kit.
Simple Steps for Loopback Testing
Configuration within CubeMX
- Depending on the CAN hardware used, on the left sidebar, click on either
CAN1
orCAN2
page, checkActivated
. In the parameter page, configure these settings:- Set
Prescaler (for Time Quantum)
to6
, and set bothTime Quanta in Bit Segment 1
andTime Quanta in Bit Segment 2
to3 Times
. This combination sets the bitrate to 1Mbps (maximum). - Configure
ReSynchronization Jump Width
as1 Time
, which is the maximum step adjustable during resynchronization. - Set
Operating Mode
toLoopback
for loopback testing.
- Set
- On the
NVIC Settings
tab, enableCANx RX0 interrupts
.
Configuration within the Code
Create a can.c
under your project and set the filter. Here, we are configuring it in list mode, filtering the extended ID 0x2233
, and standard ID 0
:
```c title="can.c"/* * Function: CAN_Filter_Config * Description: Configure CAN filters * Input: None * Output: None * Call: Called internally */ static void CAN_Filter_Config(void) { CAN_FilterTypeDef CAN_FilterTypeDef;
/* Initialize CAN filters */
CAN_FilterTypeDef.FilterBank = 0; // Filter group 0
CAN_FilterTypeDef.FilterMode = CAN_FILTERMODE_IDLIST; // Operating in list mode
CAN_FilterTypeDef.FilterScale = CAN_FILTERSCALE_32BIT; // Filter bit width is single 32-bit.
/* Enable filters, based on the content of the flag, compare and filter out if the extended ID does not match as shown below, it will be discarded, and if it matches, it will be stored in FIFO0. */
CAN_FilterTypeDef.FilterIdHigh = ((((uint32_t) 0x2233 << 3) | CAN_ID_EXT | CAN_RTR_DATA) & 0xFFFF0000) >> 16; // High part of the ID to be filtered
CAN_FilterTypeDef.FilterIdLow = (((uint32_t) 0x2233 << 3) | CAN_ID_EXT | CAN_RTR_DATA) & 0xFFFF; // Low part of the ID to be filtered
CAN_FilterTypeDef.FilterMaskIdHigh = 0; // High part of the second ID
CAN_FilterTypeDef.FilterMaskIdLow = 0; // Low part of the second ID
CAN_FilterTypeDef.FilterFIFOAssignment = CAN_FILTER_FIFO0; // Filter is associated with FIFO0
CAN_FilterTypeDef.FilterActivation = ENABLE; // Enable the filter
HAL_CAN_ConfigFilter(&hcan1, &CAN_FilterTypeDef);
} ```
Testing
Open the Device Manager to check if the device is already displayed. If you do not find the device or see a yellow exclamation mark, please visit the official ST website to download the STM32 Virtual COM Port Driver.
If you have installed the driver and the device is still not recognized correctly, you can try adjusting the Minimum Heap Size
to 0x600
or higher in CubeMX under Project Manager
- Project
- Linker Settings
.
Open a serial communication tool (baud rate can be any), send any character, and it should return the same character.
References and Acknowledgments
Original: https://wiki-power.com/ This post is protected by CC BY-NC-SA 4.0 agreement, should be reproduced with attribution.
This post is translated using ChatGPT, please feedback if any omissions.