CAN bus always listen mode while receiving data

Hi,

I am phasing issue with CAN bus.
While receiving data CAN State is always listen mode.

I am using STM32L433RC and CJMCU 2551 transceiver.

Can u please help me.

Thank you

Their could ba a million reasons for this. In order to troubleshoot this issue, you have provide more details. Please share the code. If possible share the schematics of your connection or a picture of your whole setup. Please provide all the details.

1 Like

Thank you for reply.

Sure i will share right now.

/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
CAN_RxHeaderTypeDef RxHeader;
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t buffer[8];
char message[8];
char buf1[8]="HELLO";
uint8_t status2=0;
uint8_t status4=0;
uint8_t status5=0;
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan1;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */




/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_CAN1_Init(void);
/* USER CODE BEGIN PFP */
static void CAN1_Rx(void);
static void CanConfigFilter(void);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */
SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_CAN1_Init();
/* USER CODE BEGIN 2 */

status4=HAL_CAN_Init(&hcan1);
if(status4 == HAL_OK)
{
printf("ok");
}
else if(status4 == HAL_ERROR )
{
printf("error");
}
else if(status4 == HAL_BUSY)
{
printf("busy");
}
else
{
printf("timeout");
}
HAL_UART_Init(&huart2);

CanConfigFilter();



status2=HAL_CAN_WakeUp(&hcan1);
if(status2 == HAL_OK)
{
printf("ok");
}
else if(status2 == HAL_ERROR)
{
printf("error");
}
else if(status2 == HAL_BUSY)
{
printf("busy");
}
else
{
printf("HAL_TIMEOUT");
}

uint8_t status3=HAL_CAN_ActivateNotification(&hcan1,CAN_IT_RX_FIFO0_MSG_PENDING);
if(status3 == HAL_OK)
{
printf("ok");
}
else if(status3 == HAL_ERROR)
{
printf("error");
}
else if(status3 == HAL_BUSY)
{
printf("busy");
}
else
{
printf("HAL_TIMEOUT");
}
HAL_CAN_Start(&hcan1);


// HAL_CAN_RxFifo0MsgPendingCallback(&hcan1);



/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
CAN1_Rx();
}
/* USER CODE END 3 */
}

/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 10;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != 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_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
}

/**
* @brief CAN1 Initialization Function
* @param None
* @retval None
*/
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 = 20;
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 = ENABLE;
hcan1.Init.AutoRetransmission = DISABLE;
hcan1.Init.ReceiveFifoLocked = ENABLE;
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 USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{

/* USER CODE BEGIN USART2_Init 0 */

/* USER CODE END USART2_Init 0 */

/* USER CODE BEGIN USART2_Init 1 */

/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */

/* USER CODE END USART2_Init 2 */

}

/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};

/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, SMPS_EN_Pin|SMPS_V1_Pin|SMPS_SW_Pin, GPIO_PIN_RESET);

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);

/*Configure GPIO pin : B1_Pin */
GPIO_InitStruct.Pin = B1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);

/*Configure GPIO pins : SMPS_EN_Pin SMPS_V1_Pin SMPS_SW_Pin */
GPIO_InitStruct.Pin = SMPS_EN_Pin|SMPS_V1_Pin|SMPS_SW_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/*Configure GPIO pin : SMPS_PG_Pin */
GPIO_InitStruct.Pin = SMPS_PG_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(SMPS_PG_GPIO_Port, &GPIO_InitStruct);

/*Configure GPIO pin : LD4_Pin */
GPIO_InitStruct.Pin = LD4_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LD4_GPIO_Port, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */
/*void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan1)
{

RxHeader.DLC =8;
RxHeader.ExtId =0x180026F3;
RxHeader.IDE =CAN_ID_EXT;
RxHeader.RTR =CAN_RTR_REMOTE;
RxHeader.FilterMatchIndex=0x00;

//while(!HAL_CAN_GetRxFifoFillLevel(hcan1, CAN_RX_FIFO0))

if(HAL_CAN_ActivateNotification(hcan1,CAN_IT_RX_FIFO0_MSG_PENDING )!=HAL_OK)
{
Error_Handler();
}

if(HAL_CAN_GetRxMessage(hcan1,CAN_RX_FIFO0,&RxHeader,buffer)!=HAL_OK)
{
Error_Handler();
}

if(RxHeader.ExtId == 0x180026F3 )
{


sprintf(message, "%s",buffer);

}

HAL_UART_Transmit(&huart2, (uint8_t *)buffer, 8, 50);

}*/

static void CAN1_Rx(void)
{

CAN_RxHeaderTypeDef RxHeader;
uint8_t buffer[8];
/*CAN_TypeDef var1,var2;
var1.RF0R=0;
var2.RF1R=0;*/
uint8_t status=0;
uint8_t status1=0;



status=HAL_CAN_GetState(&hcan1);
if(status==HAL_CAN_STATE_RESET)
{
printf("reset");
}
else if(status == HAL_CAN_STATE_READY)
{
printf("ready");

}
else if(status == HAL_CAN_STATE_LISTENING){
printf("list");
}
else if(status == HAL_CAN_STATE_SLEEP_PENDING)
{
printf("sleep pending");
}
else if(status == HAL_CAN_STATE_SLEEP_ACTIVE){
printf("sleep active");
}
else{
printf("HAL_CAN_STATE_ERROR");
}



/*while(!(HAL_CAN_GetRxFifoFillLevel(&hcan1, CAN_RX_FIFO1)))
{

}*/

status1=HAL_CAN_GetRxMessage(&hcan1,CAN_RX_FIFO1 ,&RxHeader,buffer);
if(status1 == HAL_OK)
{
printf("ok");
}
else if(status1 == HAL_ERROR)
{
printf("error");
}
else if(status1 == HAL_BUSY)
{
printf("busy");
}
else
{
printf("HAL_TIMEOUT");
}

HAL_UART_Transmit(&huart2, (uint8_t *)buffer, 8,100);

}

static void CanConfigFilter(void)
{

CAN_FilterTypeDef sFilterConfig;

sFilterConfig.FilterActivation= CAN_FILTER_ENABLE;
sFilterConfig.FilterBank=0;
sFilterConfig.FilterFIFOAssignment=CAN_FILTER_FIFO1;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh =0x0000;
sFilterConfig.FilterMaskIdLow =0x0000;
sFilterConfig.FilterMode=CAN_FILTERMODE_IDLIST;
sFilterConfig.FilterScale=CAN_FILTERSCALE_32BIT;

status5=HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig);
if(status5 == HAL_OK)
{
printf("ok");
}
else if(status5 == HAL_ERROR )
{
printf("error");
}
else if(status5 == HAL_BUSY)
{
printf("busy");
}
else
{
printf("timeout");
}

}



/* USER CODE END 4 */

/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */

/* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

How did you conclude that CAN state is in listening mode?

Hi,

By using HAL_CAN_GetState() function I got that CAN bus is on listening mode.

Hi,
I am phasing issue with CAN Rx side. Please help me to resolve this issue.

Thank you

Sorry for the late response. I was busy with other works. I will go through your code today evening and will respond. Since I am not that familiar with CAN on STM32, I am not sure if I am able to figure it out. But I will try.

Hii,
I am waiting for your reply. Please consider my request and help me to solve this issue.