cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 HAL Dual CAN: Doesn't receive messages on CAN2 (CAN slave)

0910655
Associate

I am able to send messages from CAN1 and CAN2 and able to receive messages on CAN1 but I am not able to receive messages on CAN2. Does sombody know what I am doing wrong or what I forgot to do?

 

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 = 6;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_13TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;
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 */

/*##-1- Enable Interrupt ###########################################*/

HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_TX_MAILBOX_EMPTY);

/*##-2- Configure the CAN Filter ###########################################*/

CAN_FilterTypeDef sFilterConfigCAN1;
sFilterConfigCAN1.FilterBank = 0;
sFilterConfigCAN1.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfigCAN1.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfigCAN1.FilterIdHigh = 0x0000;
sFilterConfigCAN1.FilterIdLow = 0x0000;
sFilterConfigCAN1.FilterMaskIdHigh = 0x0000;
sFilterConfigCAN1.FilterMaskIdLow = 0x0000;
sFilterConfigCAN1.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfigCAN1.FilterActivation = ENABLE;
sFilterConfigCAN1.SlaveStartFilterBank = 14;

if (HAL_CAN_ConfigFilter(&hcan1, &sFilterConfigCAN1) != HAL_OK) 
{
/* Filter configuration Error */

  Error_Handler();
}

/*##-3- Start the CAN peripheral ###########################################*/
if (HAL_CAN_Start(&hcan1) != HAL_OK) {
/* Start Error */
Error_Handler();
}

/* USER CODE END CAN1_Init 2 */
}


/**

* @brief CAN2 Initialization Function

*  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 = 6;
hcan2.Init.Mode = CAN_MODE_NORMAL;
hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan2.Init.TimeSeg1 = CAN_BS1_13TQ;
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 */



/*##-1- Enable Interrupt ###########################################*/

HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING);
HAL_CAN_ActivateNotification(&hcan2, CAN_IT_TX_MAILBOX_EMPTY);

/*##-2- Configure the CAN Filter ###########################################*/

CAN_FilterTypeDef sFilterConfigCAN2;
sFilterConfigCAN2.FilterBank = 14;
sFilterConfigCAN2.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfigCAN2.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfigCAN2.FilterIdHigh = 0x0000;
sFilterConfigCAN2.FilterIdLow = 0x0000;
sFilterConfigCAN2.FilterMaskIdHigh = 0x0000;
sFilterConfigCAN2.FilterMaskIdLow = 0x0000;
sFilterConfigCAN2.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfigCAN2.FilterActivation = ENABLE;
sFilterConfigCAN2.SlaveStartFilterBank = 14;
if (HAL_CAN_ConfigFilter(&hcan2, &sFilterConfigCAN2) != HAL_OK) {
/* Filter configuration Error */
Error_Handler();

}

/*##-3- Start the CAN peripheral ###########################################*/

if (HAL_CAN_Start(&hcan2) != HAL_OK) {
/* Start Error */
Error_Handler();
}
/* USER CODE END CAN2_Init 2 */
}

 

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

Hello,

How do you manage the reception?

You activated the interrupt notification on RX:

HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING);

But I don't see neither a RX callback managing the CAN receive frames? nor any FIFO reading!?

Also As I said by @Andy_ry170663 the FIFO assigned to the filter used by CAN2 is not the one activated by HAL_CAN_ActivateNotification().

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.

View solution in original post

2 REPLIES 2
Andy_ry170663
Associate

/* USER CODE BEGIN CAN2_Init 2 */

/*##-1- Enable Interrupt ###########################################*/

HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING);

...

sFilterConfigCAN2.FilterFIFOAssignment = CAN_RX_FIFO0;

SofLit
ST Employee

Hello,

How do you manage the reception?

You activated the interrupt notification on RX:

HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING);

But I don't see neither a RX callback managing the CAN receive frames? nor any FIFO reading!?

Also As I said by @Andy_ry170663 the FIFO assigned to the filter used by CAN2 is not the one activated by HAL_CAN_ActivateNotification().

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.