2018-12-03 09:18 AM
Hi, I have just received a custom PCB using STM32F413RG (64 pin package) for a university design competition. We are using a single CAN bus (CAN2) for this PCB. I was trying to get CAN communication working and I only managed to get some junk data from the STM32.
Details are below. My questions are:
I found that when using CAN2, the CAN_TIR register (containing CAN message ID) and CAN_TDTR register (containing CAN message DLC) have junk in them which is NOT overwritten by the AddTxMessage HAL function (except for the TX request bit in TIR register). Thus I get CAN messages, but they have a junk ID and a DLC of 11 (obviously junk). I tried writing directly to the registers in my program and reading back the values, but they did not change.
As an experiment, I tried to write to the CAN1 TIR and TDTR registers with random values and read them back. This worked perfectly fine.
Investigating the reference manual RM0430, I found that CAN2 is a "slave" to CAN1 "master," in dual CAN configuration, while CAN3 is for single CAN configuration. However, the RM0430 document is unclear regarding the exact "master-slave" relationship between CAN1 and CAN2.
2018-12-03 09:26 AM
You have basically the logic for 1.5x CAN peripherals, so CAN2 uses a lot of the same logic as CAN1
CAN1 needs to be enabled, and clocking, for the symbiotic relationship to work. I presume it can be idle or mapped to unused pins.
I'd dig through the issues, but don't have time/resources to commit to it.
2018-12-03 09:31 AM
I can understand enabling the CAN1 clock, but the main question I have regarding enabling CAN1 is that the initialization procedure requires reading 11 recessive bits on the CAN RX pin as shown in the screenshot below from RM0430 page 1109. However, the CAN1 RX pin is being used for another peripheral already, so how can the initialization be completed?
2018-12-03 11:15 AM
The CAN peripheral has a loopback mode you can utilize.
2018-12-03 05:05 PM
In the CAN MSP init function generated by STM32Cube, there is a macro called __HAL_RCC_CAN2_CLK_ENABLE() that is used. On the line above this, I simply called __HAL_RCC_CAN1_CLK_ENABLE() and that fixed the problem.
I found this solution from Google Translation of these forum threads, where it seems that the solution is to set the bit that enables the CAN1 clock:
https://www.fingers-welt.de/phpBB/viewtopic.php?f=14&t=1086
http://forum.easyelectronics.ru/viewtopic.php?f=35&t=27632
2019-06-06 12:09 AM
This was a stumbling block for myself as I found myself in the same situation as I have designed a custom PCB and am using CAN2 as well. The I spent 1.5 days one this before I realised that i have to include __HAL_RCC_CAN1_CLK_ENABLE(); to get CAN2 to work. I wish this was stated in the reference manual RM0410.
Also, to get receiving filters working on CAN2, "FilterBank" must be greater than "SlaveStartFilterBank".
CAN_FilterTypeDef sFilterConfig;
// Analog Inputs - 0x18FF20xx
sFilterConfig.FilterBank = 17;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; // CAN_FILTERMODE_IDLIST;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = ( (0x18FF2000 << 3) >> 16);
sFilterConfig.FilterIdLow = ( (0x18FF2000 << 3) & 0xFFFF);
sFilterConfig.FilterMaskIdHigh = ( (0xFFFFFF00>>3)>>16);
sFilterConfig.FilterMaskIdLow = ( (0xFFFFFF00 << 3) & 0xFFFF);
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 15;
HAL_CAN_ConfigFilter(&PowerPlexCAN.CanHandle, &sFilterConfig);
2020-10-20 02:25 AM
I just finished also spending more than a day on this until I found this post. The STM32CubeMx software now initializes the __HAL_RCC_CAN1_CLK_ENABLE() correctly if you only enable CAN2. So in this case I forgive for this info not being on the manual.
BUT, the real gotcha was the fact the FilterBank setting had to be higher than the SlaveStartFilterBank setting for CAN2. The whole filter settings could be better explained in the manual.
2023-06-05 01:06 AM
Hi Rentato,
I tried setting FilterBank higher (17) than SlaveStartFilterBank(15) but still it is not working, of course after having CAN1 clock also enabled. Is there anything I am missing ?
Regards,
Parvez Akhtar