2024-07-25 04:18 AM - last edited on 2024-07-25 04:27 AM by Andrew Neil
I am trying to communicate with CAN, but it seems that I am not receiving Rx. I would like to know the reason for this
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 = 5;
hcan2.Init.Mode = CAN_MODE_NORMAL;
hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan2.Init.TimeSeg1 = CAN_BS1_8TQ;
hcan2.Init.TimeSeg2 = CAN_BS2_1TQ;
hcan2.Init.TimeTriggeredMode = DISABLE;
hcan2.Init.AutoBusOff = ENABLE;
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 FilterConfig;
FilterConfig.FilterBank = 14;
FilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
FilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
FilterConfig.FilterIdHigh = 0x0000;
FilterConfig.FilterIdLow = 0x0000;
FilterConfig.FilterMaskIdHigh = 0x0000;
FilterConfig.FilterMaskIdLow = 0x0000;
FilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
FilterConfig.FilterActivation = ENABLE;
FilterConfig.SlaveStartFilterBank = 14;
if (HAL_CAN_ConfigFilter(&hcan2, &FilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}
if (HAL_CAN_Start(&hcan2) != HAL_OK)
{
// Start Error
Error_Handler();
}
if (HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
{
// Notification Error
Error_Handler();
}
if(HAL_CAN_GetRxFifoFillLevel(&hcan2, CAN_RX_FIFO0) != HAL_OK)
{
/* Reception Missing */
Error_Handler();
}
// HAL_CAN_ConfigFilter(&hcan2, &FilterConfig);
// HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);
/* USER CODE END CAN2_Init 2 */
}
initiail Code and i use Can2 port
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
if ( HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &Can2RxHeader, canRxBuffer) != HAL_OK)
{
// Error_Handler();
printf( "faild read message.. \r\n");
}
else
{
printf(" Data : %d, %d, %d, %d",canRxBuffer[0],canRxBuffer[1],canRxBuffer[2],canRxBuffer[3]);
}
void Can2Task(void const * argument)
{
// CAN_TxHeaderTypeDef Can2TxHeader;
uint32_t PreviousWakeTime = osKernelSysTick();
uint32_t Tx2MailBox;
uint32_t id, rtr;
CAN_TxHeaderTypeDef Tx2Header;
// CAN_RxHeaderTypeDef Can2RxHeader;
// canTxBuffer[] =
Can2TxHeader.StdId = 0x321;
Can2TxHeader.ExtId = 0x01;
Can2TxHeader.RTR = CAN_RTR_DATA;
Can2TxHeader.IDE = CAN_ID_STD;
Can2TxHeader.DLC = 8;
Can2TxHeader.TransmitGlobalTime = DISABLE;
CanFilter2.FilterBank = 15;
CanFilter2.FilterMode = CAN_FILTERMODE_IDMASK;
CanFilter2.FilterScale = CAN_FILTERSCALE_16BIT;
CanFilter2.FilterIdHigh = 0x106 << 5;
CanFilter2.FilterIdLow = 0x106 << 5;
CanFilter2.FilterMaskIdHigh = 0x7f3 << 5;
CanFilter2.FilterMaskIdLow = 0x7f3 << 5;
CanFilter2.FilterFIFOAssignment = CAN_RX_FIFO0;
CanFilter2.FilterActivation = ENABLE;
CanFilter2.SlaveStartFilterBank = 14;
HAL_CAN_Start(&hcan2);
// if( HAL_CAN_Start(&hcan2) != HAL_OK )
// {
// Error_Handler();
// }
for(;;)
{
osDelayUntil(&PreviousWakeTime, 500UL);
#if 0 // Tx..
if ( HAL_CAN_AddTxMessage(&hcan2 , &Can2TxHeader , canTxBuffer, &Tx2MailBox) != HAL_OK)
{
printf("can send fail..\r\n");
}
else
{
printf(" can send success !! \r\n");
}
#else // Rx..
HAL_CAN_RxFifo0MsgPendingCallback(&hcan2);
#endif
}
}
2024-08-19 01:29 AM
Hello,
As you are using CAN in normal mode, you need to give more details.
You need to provide your HW config and CAN bus config. Please provide also schematics.
Are you connecting a second node on the bus?
What clock source are you using? HSE or HSI?
As you are using an RTOS, I suggest you to start with a very simple project where you try to receive a CAN frame.
Also please read this article: https://community.st.com/t5/stm32-mcus/can-reception-issues-reasons-and-general-troubleshooting/ta-p/689741