2019-04-29 05:03 PM
I am using STM32CubeMX and HAL libs to send and receive CAN messages on an STM32F413ZH. stm32f4xx_hal_can.c tells me to #define USE_HAL_CAN_REGISTER_CALLBACKS = 1 which I have done in both main.h and stm32f4xx_hal_conf.h (if I don't put it in the latter, the function HAL_CAN_RegisterCallback() doesn't get built)
In main.c I have:
...
HAL_Init();
SystemClock_Config();
MX_CAN1_Init();
...
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 = 8;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_2TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_8TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_3TQ;
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 */
for (filterNo = 0; filterNo < CAN1_FILTER_CNT; filterNo++){
sFilterConfig.FilterBank = filterNo;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;
if(HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
{
// Filter configuration Error
Error_Handler();
}
}
if (HAL_CAN_Start (&hcan1) != HAL_OK) {
// start error
Error_Handler();
}
// Register callback funcs
HAL_CAN_RegisterCallback(&hcan1, HAL_CAN_TX_MAILBOX0_COMPLETE_CB_ID, HAL_CAN_TxMailbox0CompleteCallback);
HAL_CAN_RegisterCallback(&hcan1, HAL_CAN_RX_FIFO0_MSG_PENDING_CB_ID, HAL_CAN_RxFIFO0MsgPendingCallback);
HAL_CAN_RegisterCallback(&hcan1, HAL_CAN_RX_FIFO0_FULL_CB_ID, HAL_CAN_RxFifo0FullCallback);
// Activate CAN Interrupts
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_TX_MAILBOX_EMPTY);
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_FULL);
/* USER CODE END CAN1_Init 2 */
}
============
My HAL_CAN_TxMailbox0CompleteCallback function doesn't get called, the default one in stm32f4xx_hal_can.c does. My function is in main.c and the prototype is in main.h.
2023-10-15 07:01 AM
These posts are from 4 years ago. Post specifics about YOUR IMPLEMENTATION and platform.
The State likely comes from initializing the peripheral properly so perhaps look at worked examples for EVAL boards on your STM32 platform.