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
2024-11-06 06:26 PM - last edited on 2024-11-07 12:59 AM by SofLit
I'm currently using a stm32f756igt chip, but CAN communication is not working smoothly What's the problem?
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 12;
RCC_OscInitStruct.PLL.PLLN = 192;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Activate the Over-Drive mode
*/
if (HAL_PWREx_EnableOverDrive() != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_6) != HAL_OK)
{
Error_Handler();
}
}
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 */
/* 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 */
/* USER CODE END CAN2_Init 2 */
}
void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hcan->Instance==CAN1)
{
/* USER CODE BEGIN CAN1_MspInit 0 */
/* USER CODE END CAN1_MspInit 0 */
/* Peripheral clock enable */
HAL_RCC_CAN1_CLK_ENABLED++;
if(HAL_RCC_CAN1_CLK_ENABLED==1){
__HAL_RCC_CAN1_CLK_ENABLE();
}
__HAL_RCC_GPIOI_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**CAN1 GPIO Configuration
PI9 ------> CAN1_RX
PA12 ------> CAN1_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* CAN1 interrupt Init */
HAL_NVIC_SetPriority(CAN1_TX_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(CAN1_TX_IRQn);
HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
HAL_NVIC_SetPriority(CAN1_RX1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(CAN1_RX1_IRQn);
/* USER CODE BEGIN CAN1_MspInit 1 */
/* USER CODE END CAN1_MspInit 1 */
}
else if(hcan->Instance==CAN2)
{
/* USER CODE BEGIN CAN2_MspInit 0 */
/* USER CODE END CAN2_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_CAN2_CLK_ENABLE();
HAL_RCC_CAN1_CLK_ENABLED++;
if(HAL_RCC_CAN1_CLK_ENABLED==1){
__HAL_RCC_CAN1_CLK_ENABLE();
}
__HAL_RCC_GPIOB_CLK_ENABLE();
/**CAN2 GPIO Configuration
PB12 ------> CAN2_RX
PB13 ------> CAN2_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* CAN2 interrupt Init */
HAL_NVIC_SetPriority(CAN2_TX_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(CAN2_TX_IRQn);
HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
HAL_NVIC_SetPriority(CAN2_RX1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(CAN2_RX1_IRQn);
/* USER CODE BEGIN CAN2_MspInit 1 */
/* USER CODE END CAN2_MspInit 1 */
}
}
/**
* @brief CAN MSP De-Initialization
* This function freeze the hardware resources used in this example
* hcan: CAN handle pointer
* @retval None
*/
void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan)
{
if(hcan->Instance==CAN1)
{
/* USER CODE BEGIN CAN1_MspDeInit 0 */
/* USER CODE END CAN1_MspDeInit 0 */
/* Peripheral clock disable */
HAL_RCC_CAN1_CLK_ENABLED--;
if(HAL_RCC_CAN1_CLK_ENABLED==0){
__HAL_RCC_CAN1_CLK_DISABLE();
}
/**CAN1 GPIO Configuration
PI9 ------> CAN1_RX
PA12 ------> CAN1_TX
*/
HAL_GPIO_DeInit(GPIOI, GPIO_PIN_9);
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12);
/* CAN1 interrupt DeInit */
HAL_NVIC_DisableIRQ(CAN1_TX_IRQn);
HAL_NVIC_DisableIRQ(CAN1_RX0_IRQn);
HAL_NVIC_DisableIRQ(CAN1_RX1_IRQn);
/* USER CODE BEGIN CAN1_MspDeInit 1 */
/* USER CODE END CAN1_MspDeInit 1 */
}
else if(hcan->Instance==CAN2)
{
/* USER CODE BEGIN CAN2_MspDeInit 0 */
/* USER CODE END CAN2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_CAN2_CLK_DISABLE();
HAL_RCC_CAN1_CLK_ENABLED--;
if(HAL_RCC_CAN1_CLK_ENABLED==0){
__HAL_RCC_CAN1_CLK_DISABLE();
}
/**CAN2 GPIO Configuration
PB12 ------> CAN2_RX
PB13 ------> CAN2_TX
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12|GPIO_PIN_13);
/* CAN2 interrupt DeInit */
HAL_NVIC_DisableIRQ(CAN2_TX_IRQn);
HAL_NVIC_DisableIRQ(CAN2_RX0_IRQn);
HAL_NVIC_DisableIRQ(CAN2_RX1_IRQn);
/* USER CODE BEGIN CAN2_MspDeInit 1 */
/* USER CODE END CAN2_MspDeInit 1 */
}
}
void CanCommunication(void const * argument)
{
uint32_t PreviousWakeTimeCAN = osKernelSysTick();
uint8_t TxCanData[8] = {11,22,33,44,55,66,77,88};
uint32_t TxMailbox;
for(;;)
{
osDelayUntil(&PreviousWakeTimeCAN, 1000UL);
#if 1
TxHeader.StdId = 0x321;
TxHeader.ExtId = 0x01;
TxHeader.RTR = CAN_RTR_DATA;
TxHeader.IDE = CAN_ID_STD;
TxHeader.DLC = 8;
TxHeader.TransmitGlobalTime = DISABLE;
// TxMailbox = HAL_CAN_GetTxMailboxesFreeLevel(&hcan2);
// HAL_CAN_AddTxMessage(&hcan2, &TxHeader, TxCanData, &TxMailbox);
if (HAL_CAN_IsTxMessagePending(&hcan2, TxMailbox) == 0) {
if( HAL_CAN_AddTxMessage(&hcan2, &TxHeader, TxCanData, &TxMailbox) != HAL_OK)
{
printf( " CAN send Fail... \r\n ");
}
else
{
printf( " CAN Send Success !! \r\n ");
}
}
else
{
printf( " mailbox Full \r\n");
}
// while(HAL_CAN_IsTxMessagePending(&hcan2, TxMailbox)){}
// osDelay(200);
#else /* CAN Rx Communication .. */
#endif
}
}
After setting it repeatedly every 1000 ms using FreeRTOS, the waveform was measured using the oscilloscope, but CAN1 has a full mailbox after a few waveform outputs and CAN2 has only one small waveform repeatedly after a few 2 waveforms. I will not use CAN1 and I want to use CAN2 to communicate.
2024-11-07 01:11 AM
You did ask the same question here:
https://community.st.com/t5/stm32-mcus-products/stm32f756-can-communication/td-p/701720
as SofLit told You we need more information.
2024-11-07 01:41 AM
Threads merged.
2024-11-07 01:47 AM - edited 2024-11-07 01:54 AM
The first question is related to CAN communication but the second question seems to be related to FreeRTOS.
@Dlak so the question is: does CAN communication OK "repeatedly every 1000 ms" without RTOS?