cancel
Showing results for 
Search instead for 
Did you mean: 

CAN Receive not working on STM32F412G-DISCO

Deblina
Associate II

The problem that I am facing with the CAN communication using STM32F412G-DISCO  is that the CAN transmission from the STM32F412G-DISCO devboard is working fine, but the mcu is not receiving any data on the CAN bus. I am using a CAN analyser to send the data. I can confirm the analyser is properly sending the data since another mcu is receiving the data sent from my CAN analyser properly.


I have attached the code snippets am using to run the CAN RX and TX for review if anyone could let me know whether there is an issue in the code/configuration, or is it a problem of the device itself?

 

I am using the CAN2 peripheral and the application is running RTOS. The Baud of the CAN is 500Kbps.

Deblina_0-1698480665961.png

 

 

/* Private variables ---------------------------------------------------------*/

CAN_HandleTypeDef hcan2;

 

/* USER CODE BEGIN 0 */

CAN_TxHeaderTypeDef TxHeader;

CAN_RxHeaderTypeDef   RxHeader;

uint8_t TxData[8];

uint32_t TxMailbox;

uint8_t RxData[8];

 

void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan) {

            if (HAL_CAN_GetRxMessage(&hcan2, CAN_RX_FIFO1, &RxHeader, RxData)

                                    != HAL_OK) {

                        Error_Handler();

            }

 

            datacheck = 1;

}

/* USER CODE END 0 */

 

/**

 * @brief  The application entry point.

 * @retval int

 */

int main(void) {

            /*Existing Code …*/

 

            MX_CAN2_Init();

            CAN_FilterTypeDef canfilterconfig;

 

            canfilterconfig.FilterActivation = CAN_FILTER_ENABLE;

            canfilterconfig.FilterBank = 18; // which filter bank to use from the assigned ones

            canfilterconfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;

            canfilterconfig.FilterIdHigh = 0x446 << 5;

            canfilterconfig.FilterIdLow = 0;

            canfilterconfig.FilterMaskIdHigh = 0x446 << 5;

            canfilterconfig.FilterMaskIdLow = 0x0000;

            canfilterconfig.FilterMode = CAN_FILTERMODE_IDMASK;

            canfilterconfig.FilterScale = CAN_FILTERSCALE_32BIT;

            canfilterconfig.SlaveStartFilterBank = 20; // how many filters to assign to the CAN1 (master can)

 

            HAL_CAN_ConfigFilter(&hcan2, &canfilterconfig);

 

            if (HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING)

                                    != HAL_OK) {

                        Error_Handler();

            }

 

            HAL_CAN_Start(&hcan2);

            }

 

 

/**

 * @brief CAN2 Initialization Function

 * @PAram None

 * @retval None

 */

static void MX_CAN2_Init(void) {

 

            /* USER CODE BEGIN CAN2_Init 0 */

 

            /* USER CODE END CAN2_Init 0 */

 

            /* USER CODE BEGIN CAN2_Init 1 */

 

            /* USER CODE END CAN2_Init 1 */

            hcan2.Instance = CAN2;

            hcan2.Init.Prescaler = 18;

            hcan2.Init.Mode = CAN_MODE_NORMAL;

            hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;

            hcan2.Init.TimeSeg1 = CAN_BS1_2TQ;

            hcan2.Init.TimeSeg2 = CAN_BS2_1TQ;

            hcan2.Init.TimeTriggeredMode = DISABLE;

            hcan2.Init.AutoBusOff = DISABLE;

            hcan2.Init.AutoWakeUp = DISABLE;

            hcan2.Init.AutoRetransmission = DISABLE;

            hcan2.Init.ReceiveFifoLocked = ENABLE;

            hcan2.Init.TransmitFifoPriority = DISABLE;

            if (HAL_CAN_Init(&hcan2) != HAL_OK) {

                        Error_Handler();

            }

            /* USER CODE BEGIN CAN2_Init 2 */

 

            /* USER CODE END CAN2_Init 2 */

 

}

 

I was checking the ‘datacheck’ variable but it always stays at 0. I have also tried putting a breakpoint in the callback, the control never hits the breakpoint. Which I am suspecting the callback never gets fired. I have tried without keeping the filter config, still no data received.

12 REPLIES 12
Karl Yamashita
Lead II

enable CAN1 even if you're not using it.

If you find my answers useful, click the accept button so that way others can see the solution.

Thanks for the suggestion but even after doing so, it is still not working :(

KDJEM.1
ST Employee

Hello @Deblina and welcome to the Community 🙂,

I think CAN_Networking example can help you to check the code and configuration.

This example explains how to configure the CAN peripheral to send and receive CAN frames in normal mode.

I hope this helps you solve the issue!

Kaouthar

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

SofLit
ST Employee

Hello,

hcan2.Init.Prescaler = 18;

hcan2.Init.TimeSeg1 = CAN_BS1_2TQ;

hcan2.Init.TimeSeg2 = CAN_BS2_1TQ;

Are not a good settings. BS1 and BS2 are very low. So please decrease the prescaler as much as possible and increase BS1 and BS2 as much as possible in order to get the 500kb/s.

Suggestion: first try to allow all messages to be received by setting filterIDs to 0x0000.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Tinnagit
Senior II

I think that you had use HAL_CAN_ActivateNotification() so you will get data from callback function.
which I think you should try to add

HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING)

in to your void HAL_CAN_RxFifo1MsgPendingCallback after you get CAN Data.

Hello, 
I don't think it makes much of a difference in changing the prescaler, BS1 and B2 values as the CAN transmission is working fine.
 
However, I have also tried testing the CAN receive after changing the values to the following:

hcan2.Init.Prescaler = 4;
hcan2.Init.TimeSeg1 = CAN_BS1_9TQ;
hcan2.Init.TimeSeg2 = CAN_BS2_8TQ;

as well as tried by setting filterIDs to 0x0000. 

But even after doing so, it is still not working.

As far as I could understand from your reply, if you review the code in my posted question, you will be able to see that I have already done that.

However, if that is not what you're suggesting, then could you please mention what exactly I should be doing/changing instead.

Thank you for the reply.
I tried referring and testing the CAN communication using the example code, but doing so even the CAN transmission was not working. I will continue to perform a few more tests using this, but until then I don't have a solid solution as to why the CAN receive is not working at all.

Hello,

"It does not make a difference" does not mean the recommendation I provided is not valid. I remember, in this community, people had CAN issues related to bad settings including low values of BS1 and BS2.

So now I suggest to:

- If  you are using HSI a source of clock, please use the STLink MCO instead.

- Start with Loop back mode to test your Filter config.  As I mentioned previously, start with filters config 0x0000.

- Check you connections and your HW including the transceiver/120ohm resistor

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.