cancel
Showing results for 
Search instead for 
Did you mean: 

CAN Communication - STM32F098RCT

DivyaJayan
Associate III

Hi,

I am currently working on a Battery Management System (BMS) and an instrument cluster. I need your assistance with some issues I’m encountering while communicating between the BMS and a custom cluster board using CAN.

We have two custom cluster boards equipped with STM32F098RCT6 MCUs and TJA1042 transceivers. I successfully established communication between the two boards and received the RxData in the debug terminal.

When I attempt to establish communication between the cluster board and the BMS, no data is received in the debug terminal. Despite numerous attempts, I have been unable to achieve successful communication.

Please provide some insight about this.

Here is the code:  

 

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

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

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan;

/* USER CODE BEGIN PV */

CAN_TxHeaderTypeDef   TxHeader;
CAN_RxHeaderTypeDef   RxHeader;

uint8_t     TxData[1];
uint8_t     RxData[34];
uint32_t    TxMailbox;


/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN_Init(void);
/* USER CODE BEGIN PFP */

/* 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_CAN_Init();
  /* USER CODE BEGIN 2 */

    TxHeader.StdId = 0x100;
    TxHeader.ExtId = 0x00;
    TxHeader.RTR = CAN_RTR_DATA;
    TxHeader.IDE = CAN_ID_STD;
    TxHeader.DLC = 0x5A;
    TxHeader.TransmitGlobalTime = DISABLE;
    TxData[0] = 0x01;



  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */


	  if (HAL_CAN_AddTxMessage(&hcan, &TxHeader,(uint8_t *)TxData, &TxMailbox) != HAL_OK)
	  {
	     Error_Handler();
	   }

	  HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_11);
	  HAL_Delay(500);
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {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_NONE;
  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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief CAN Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_CAN_Init(void)
{

  /* USER CODE BEGIN CAN_Init 0 */
	CAN_FilterTypeDef   sFilterconfig;
  /* USER CODE END CAN_Init 0 */

  /* USER CODE BEGIN CAN_Init 1 */

  /* USER CODE END CAN_Init 1 */
  hcan.Instance = CAN;
  hcan.Init.Prescaler = 4;
  hcan.Init.Mode = CAN_MODE_NORMAL;
  hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
  hcan.Init.TimeSeg1 = CAN_BS1_1TQ;
  hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
  hcan.Init.TimeTriggeredMode = DISABLE;
  hcan.Init.AutoBusOff = DISABLE;
  hcan.Init.AutoWakeUp = DISABLE;
  hcan.Init.AutoRetransmission = ENABLE;
  hcan.Init.ReceiveFifoLocked = DISABLE;
  hcan.Init.TransmitFifoPriority = DISABLE;
  if (HAL_CAN_Init(&hcan) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN CAN_Init 2 */

    sFilterconfig.FilterBank   = 10;
    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_FILTER_FIFO0 ;
    sFilterconfig.FilterActivation = CAN_FILTER_ENABLE;
    sFilterconfig.SlaveStartFilterBank = 14;

    if(HAL_CAN_ConfigFilter(&hcan, &sFilterconfig) != HAL_OK)
    {
  	  Error_Handler();
    }

    if(HAL_CAN_Start(&hcan) != HAL_OK)
    {
  	  Error_Handler();
    }

    if (HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
    {
      /* Notification Error */
      Error_Handler();
    }

  /* USER CODE END CAN_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_RESET);

  /*Configure GPIO pins : PC10 PC11 PC12 */
  GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{

     if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
     {
        Error_Handler();
     }
}


/* 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 */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

 

 

Screenshot 2024-10-23 114441.png

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

You need to ensure that BMS is sending frames to STM32. Did you verify that with an oscilloscope or CAN bus analyzer ?

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

13 REPLIES 13
SofLit
ST Employee

Hello,

To me there are two issues from what provided as code:

1- You are using HSI instead of HSE with an external crystal:

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

2-  Bad BS1 and BS2 parameters. You need to increase as much as possible BS1 and BS2 where BS1 = ~(70% to 85% (BS1+BS2) and decrease the preclear as much as possible:

  hcan.Init.TimeSeg1 = CAN_BS1_1TQ;
  hcan.Init.TimeSeg2 = CAN_BS2_2TQ;

 Also what is the bitrate you are using for CAN communication? are you sure you set the same CAN bitrate?

Could you please also your schematics? especially CAN part STM32/Tranceiver.

Meanwhile, refer to this article: https://community.st.com/t5/stm32-mcus/can-reception-issues-reasons-and-general-troubleshooting/ta-p/689741

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.

What debugging and instrumentation do you have at each node?

Do you have a CAN Bus Analyzer?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

And what debugging and telemetry is available to understand the state and dynamic behaviour of the nodes ?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

@DivyaJayan wrote:

USB To CAN Adapter Model A STM32 Chip Solution Multiple Working Modes Multi-system Compatible | Sharvielectronics: Best Online Electronic Products Bangalore 


Sorry this link is not working. Maybe need to provide image/screen shot.

Also please answer my previous questions. That's important to prevent unnecessary ping-pongs.

Thank you.

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.

We require a bitrate of 500,000 bps. To achieve this, I selected the appropriate prescaler, BS1, and BS2 parameters.

 

Here is the schematics:


Screenshot 2024-10-24 170606.pngScreenshot 2024-10-24 170550.png

  In the transceiver, we grounded the STB pin. 

No, we don't have any other tools than DSO and CAN Analyzer.

Here is the screenshot of USB to CAN adapter model.

 

Screenshot 2024-10-24 171311.png

 


@DivyaJayan wrote:

We require a bitrate of 500,000 bps. To achieve this, I selected the appropriate prescaler, BS1, and BS2 parameters.


What are the new timing parameters?

Did you switch to HSE/Crystal usage?

Could you please share your ioc file?

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.