cancel
Showing results for 
Search instead for 
Did you mean: 

How to transmit and received data using CAN2 in STM32F40ZGT?.

KZULK.1
Associate II

I have successfully transmit and received data using CAN1. But having problem with CAN2.

I have go through all the configuration to enable the CAN2 however, I unable to transmit the data through CAN2.

What I have done:

1) Enable CAN2 ( PB12 [Rx], PB13 [Tx] )

2) Configure the Clock, HSE Crystal 8Mhz

3) Configure the bitrate to 500kbps

4) Enable CAN2 RX0 interrupts and CAN2 RX1 interrupt

5) For CAN1, canfilter.FilterActivation = ENABLE; canfilter.FilterBank = 0; and canfilter.SlaveStartFilterBank = 14;

6) For CAN2, canfilter2.FilterActivation = ENABLE; canfilter2.FilterBank = 14; and canfilter2.SlaveStartFilterBank = 14;

7) I have check all the hardware is OK, all are connected.

Behaviour

1) CAN1 is able to transmit and received data successfully

2) CAN2 fail to transmit or received data.

The Code

====================================================

/* USER CODE BEGIN 0 */
 
 
 
// Configure for CAN1
 
CAN_RxHeaderTypeDef RxHeader; //CAN Bus Receiver Header
 
CAN_TxHeaderTypeDef TxHeader; //CAN Bus Transmit Header
 
uint32_t TxMailbox; //CAN Bus Mail box variable
 
uint8_t RxData[8] = {0,0,0,0,0,0,0,0}; //CAN Bus Receive Buffer
 
uint8_t TxData[8] = {0,0,0,0,0,0,0,0}; //CAN Bus Transmit Buffer
 
HAL_StatusTypeDef status_can1;
 
uint8_t heartbeat = 0;
 
 
 
// Configure for CAN2
 
CAN_RxHeaderTypeDef RxHeader2; //CAN2 Bus Receiver Header
 
CAN_TxHeaderTypeDef TxHeader2; //CAN2 Bus Transmit Header
 
uint32_t TxMailbox2; //CAN2 Bus Mail box variable
 
uint8_t RxData2[8] = {0,0,0,0,0,0,0,0}; //CAN2 Bus Receive Buffer
 
uint8_t TxData2[8] = {0,0,0,0,0,0,0,0}; //CAN2 Bus Transmit Buffer
 
HAL_StatusTypeDef status_can2;
 
 
 
/* USER CODE END 0 */
 
 
 
 /* USER CODE BEGIN 2 */
 
 
 
 //Start CAN1
 
 HAL_CAN_Start(&hcan1);
 
 
 
 //Activate CAN received notification
 
 HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
 
 
 
 TxHeader.DLC = 2; //Send 2 byte of data
 
 TxHeader.ExtId = 0;
 
 TxHeader.IDE = CAN_ID_STD;
 
 TxHeader.RTR = CAN_RTR_DATA;
 
 TxHeader.StdId = 0x304; //Can ID
 
 TxHeader.TransmitGlobalTime = DISABLE;
 
 
 
 //Start CAN2
 
 if(HAL_CAN_Start(&hcan2) == HAL_OK)
 
 {
 
 uint8_t hal_ok[14] = ">2 Start_OK\r\n";
 
 HAL_UART_Transmit(&huart1, hal_ok, 13, 50);
 
 }
 
 
 
 //Activate CAN received notification
 
 if(HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING) == HAL_OK)
 
 {
 
 uint8_t hal_ok[12] = ">2 Noti_OK\r\n";
 
 HAL_UART_Transmit(&huart1, hal_ok, 12, 50);
 
 }
 
 
 
 TxHeader2.DLC = 2; //Send 2 byte of data
 
 TxHeader2.ExtId = 0;
 
 TxHeader2.IDE = CAN_ID_STD;
 
 TxHeader2.RTR = CAN_RTR_DATA;
 
 TxHeader2.StdId = 0x504; //Can ID
 
 TxHeader2.TransmitGlobalTime = DISABLE;
 
 
 
 /* USER CODE END 2 */
 
 
 
  /* USER CODE BEGIN 3 */
 
 
 
 heartbeat++;
 
 
 
 TxData2[0] = heartbeat;
 
 TxData2[1] = 0x00;
 
 status_can2 = HAL_CAN_AddTxMessage(&hcan2, &TxHeader2, TxData2, &TxMailbox);
 
 
 
 if(status_can2 == HAL_OK)
 
 {
 
 uint8_t hal_ok[11] = ">2 HAL_OK\r\n";
 
 HAL_UART_Transmit(&huart1, hal_ok, 11, 50);
 
 }
 
 else if(status_can2 == HAL_ERROR)
 
 {
 
 uint8_t hal_error[14] = ">2 HAL_ERROR\r\n";
 
 HAL_UART_Transmit(&huart1, hal_error, 14, 50);
 
 }
 
 else if(status_can2 == HAL_BUSY)
 
 {
 
 uint8_t hal_busy[13] = ">2 HAL_BUSY\r\n";
 
 HAL_UART_Transmit(&huart1, hal_busy, 13, 50);
 
 }
 
 else if(status_can2 == HAL_TIMEOUT)
 
 {
 
 uint8_t hal_timeout[16] = ">2 HAL_TIMEOUT\r\n";
 
 HAL_UART_Transmit(&huart1, hal_timeout, 16, 50);
 
 }
 
 else
 
 {
 
 uint8_t _message[12] = ">2 NOTHING\r\n";
 
 HAL_UART_Transmit(&huart1, _message, 12, 50);
 
 }
 
 
 
 //6) Toggle LED
 
 HAL_Delay(1000);
 
 }
 
 /* USER CODE END 3 */
 
 
 
static void MX_CAN1_Init(void)
 
{
 
 
 
 /* USER CODE BEGIN CAN1_Init 0 */
 
 
 
 /* USER CODE END CAN1_Init 0 */
 
 
 
 /* USER CODE BEGIN CAN1_Init 1 */
 
 
 
 /* USER CODE END CAN1_Init 1 */
 
 hcan1.Instance = CAN1;
 
 hcan1.Init.Prescaler = 18;
 
 hcan1.Init.Mode = CAN_MODE_NORMAL;
 
 hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
 
 hcan1.Init.TimeSeg1 = CAN_BS1_2TQ;
 
 hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;
 
 hcan1.Init.TimeTriggeredMode = DISABLE;
 
 hcan1.Init.AutoBusOff = DISABLE;
 
 hcan1.Init.AutoWakeUp = DISABLE;
 
 hcan1.Init.AutoRetransmission = DISABLE;
 
 hcan1.Init.ReceiveFifoLocked = DISABLE;
 
 hcan1.Init.TransmitFifoPriority = DISABLE;
 
 if (HAL_CAN_Init(&hcan1) != HAL_OK)
 
 {
 
  Error_Handler();
 
 }
 
 
 
 /* USER CODE BEGIN CAN1_Init 2 */
 
 CAN_FilterTypeDef canfilter; //CAN Filter configuration
 
 canfilter.FilterActivation = ENABLE;
 
 canfilter.FilterBank = 0;
 
 canfilter.FilterFIFOAssignment = CAN_RX_FIFO0;
 
 canfilter.FilterIdHigh = 0x203<<5;
 
 canfilter.FilterIdLow = 0;
 
 canfilter.FilterMaskIdHigh = 0x203<<5;
 
 canfilter.FilterMaskIdLow = 0;
 
 canfilter.FilterMode = CAN_FILTERMODE_IDMASK;
 
 canfilter.FilterScale = CAN_FILTERSCALE_32BIT;
 
 canfilter.SlaveStartFilterBank = 14; //Slave Filter bank start at 14 to 28
 
 
 
 HAL_CAN_ConfigFilter(&hcan1,&canfilter);
 
 /* USER CODE END CAN1_Init 2 */
 
 
 
}
 
 
 
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_1TQ;
 
 hcan2.Init.TimeSeg2 = CAN_BS2_2TQ;
 
 hcan2.Init.TimeTriggeredMode = DISABLE;
 
 hcan2.Init.AutoBusOff = DISABLE;
 
 hcan2.Init.AutoWakeUp = DISABLE;
 
 hcan2.Init.AutoRetransmission = DISABLE;
 
 hcan2.Init.ReceiveFifoLocked = DISABLE;
 
 hcan2.Init.TransmitFifoPriority = DISABLE;
 
 if (HAL_CAN_Init(&hcan2) != HAL_OK)
 
 {
 
  Error_Handler();
 
 }
 
 /* USER CODE BEGIN CAN2_Init 2 */
 
 
 
 CAN_FilterTypeDef canfilter2; //CAN Filter configuration
 
 canfilter2.FilterActivation = ENABLE;
 
 canfilter2.FilterBank = 14;
 
 canfilter2.FilterFIFOAssignment = CAN_RX_FIFO1;
 
 canfilter2.FilterIdHigh = 0x0000;
 
 canfilter2.FilterIdLow = 0x0000;
 
 canfilter2.FilterMaskIdHigh = 0x0000;
 
 canfilter2.FilterMaskIdLow = 0x0000;
 
 canfilter2.FilterMode = CAN_FILTERMODE_IDMASK;
 
 canfilter2.FilterScale = CAN_FILTERSCALE_32BIT;
 
 canfilter2.SlaveStartFilterBank = 14; //Slave Filter bank start at 14 to 28
 
 
 
 if(HAL_CAN_ConfigFilter(&hcan2,&canfilter2) == HAL_OK)
 
 {
 
 uint8_t hal_ok[14] = ">2 Filter_OK\r\n";
 
 HAL_UART_Transmit(&huart1, hal_ok, 14, 50);
 
 }
 
 
 
 /* USER CODE END CAN2_Init 2 */
 
 
 
}
 
 
 
/* USER CODE BEGIN 4 */
 
 
 
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
 
{
 
	//Received CAN data
 
	if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
 
	{
 
 Error_Handler();
 
	}
 
	//Feedback through CAN1
 
 
 
	//Send to serial
 
	uint8_t _msg_can[22] = ">> IPC CAN Receiving\r\n";
 
	HAL_UART_Transmit(&huart1, _msg_can, 22, 50);
 
}
 
 
 
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
 
{
 
	//Received CAN data
 
	if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO1, &RxHeader2, RxData2) != HAL_OK)
 
	{
 
 Error_Handler();
 
	}
 
	//Feedback through CAN1
 
 
 
	//Send to serial
 
	uint8_t _msg_can[22] = ">> DBW CAN Receiving\r\n";
 
	HAL_UART_Transmit(&huart1, _msg_can, 22, 50);
 
}
 
 
 
/* USER CODE END 4 */

5 REPLIES 5
Karl Yamashita
Lead III

It's difficult to read your code as is. You need to post your code as a code snippet.

0693W00000Y7flrQAB.png

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.
KZULK.1
Associate II

@Community member​ thanks. I have edit it. Do u have any idea for me to troubleshoot the CAN2 problem on STM32?

Karl Yamashita
Lead III

Do you see your uart message when you call HAL_CAN_Start(&hcan2) ?

//Start CAN2
 
 if(HAL_CAN_Start(&hcan2) == HAL_OK)
 
 {
 
 uint8_t hal_ok[14] = ">2 Start_OK\r\n";
 
 HAL_UART_Transmit(&huart1, hal_ok, 13, 50);
 
 }

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.

">2 Start_OK"

I have validate the following

HAL_CAN_Init for CAN1 : HAL_OK

HAL_CAN_Init for CAN2 : HAL_OK

HAL_CAN_ConfigFilter for CAN1 : HAL_OK

HAL_CAN_ConfigFilter for CAN2 : HAL_OK

HAL_CAN_Start for CAN1 : HAL_OK

HAL_CAN_Start for CAN2 : HAL_OK

HAL_CAN_ActivateNotification for CAN1 : HAL_OK

HAL_CAN_AddTxMessage for CAN1 : HAL_OK

HAL_CAN_ActivateNotification for CAN2 : HAL_OK

HAL_CAN_AddTxMessage for CAN2 : HAL_OK

but the CAN2 does not transmit any data to PEAK CAN View

After a while, the CAN2 becomes HAL_ERROR

Parvez Akhtar
Associate II

Any answer to above questions ? Or still they are working on it ?