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

Hello,

I want to apologize if my previous message came across as rude or dismissive. That was not my intention. I truly appreciate your input and recommendations, and I'm grateful for your help. I'm just trying to understand and address the issues I'm facing with my CAN setup. Your suggestions are valuable, and I'll definitely try them out.

Thank you for your patience and support as I work through this. I'm here to learn and improve, and your guidance is greatly appreciated.


Best regards,
Deblina

Check this post out https://community.st.com/t5/stm32-mcus-embedded-software/two-stm32f4-discovery-boards-unable-to-communicate-over-can/td-p/107519

I talk about using only CAN2 and i have a link to a github source code showing my settings. Maybe it'll help?

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

really!!!
I really didn't see them in your CAN callback function.
I'm sorry if I miss something but I didn't see them.