cancel
Showing results for 
Search instead for 
Did you mean: 

STM32-L432KC CAN mailboxes are not emptied HAL_CAN_ERROR_PARAM

oguh
Associate

Trying to send a CAN message every second but after a few tries it returns the HAL_CAN_ERROR_PARAM error. Tried to check if there is an empty mailbox available before sending the next can message but the mailboxes arent emptied. Main.c is below, the can_init is below that.

 

 

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* 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 */
#define WITarraySize 10
/* USER CODE END PD */

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

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
uint8_t Senix1;
uint8_t Senix2;

float WITsensor[WITarraySize];
//--------------------------------------------------
//variables to set up the timer and allow a variable interval
uint16_t currentDelay = 0;
uint32_t timeBetweenSamples = 1000;
uint8_t sampleFlag = 0;
//----------------------------------------------------------
uint8_t WITvalues[WITarraySize * 2];
CAN_TxHeaderTypeDef TxHeader;
uint8_t TxData[8] = {0};
uint32_t TxMailbox;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(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_CAN1_Init();
  MX_TIM1_Init();
  MX_TIM16_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  CAN_TX_filter_init();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	//if the flag is high, one sample moment will occur
	if(sampleFlag == 1) {
		readWIT(WITsensor);
		//delay to allow the bus to clear
		HAL_Delay(3);

		readSenixSensors(&Senix1, &Senix2);

		if (HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) != 0) {
			
			sendMessage();
		}

		sampleFlag = 0;
	}
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

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

  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure LSE Drive Capability
  */
  HAL_PWR_EnableBkUpAccess();
  __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE|RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 16;
  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_1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Enable MSI Auto calibration
  */
  HAL_RCCEx_EnableMSIPLLMode();
}

/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
	//check if the interrupt is triggered by the correct timer
	if (htim == &htim16){
		//create a variable delay
		if(currentDelay >= timeBetweenSamples) {
			sampleFlag = 1;

			currentDelay = 0;
		}
		else {
			currentDelay++;
		}
	}
}

void convertFloatArrayToInt(float floatArray[], uint8_t intArray[], uint8_t size, uint16_t scale){
	uint8_t j = 0;
	for (uint8_t i = 0; i < size; i++) {
		uint16_t scaledValue = floatArray[i] * scale;
		intArray[j] = (scaledValue & 0xFF00) >> 8;
		intArray[j+1] = scaledValue & 0x00FF;
		j += 2;
	}
}

void sendPartionalMessage(uint8_t TxData[], uint8_t TxDataArray[], uint8_t id, uint8_t begin) {
	TxData[0] = id;
	uint8_t j = begin;

	for(uint8_t i = 0; i < 7; i++) {
		TxData[i] = TxDataArray[j];
		j += 1;
	}

	/* send TxData*/
	if (HAL_CAN_AddTxMessage(&hcan1, &TxHeader, (uint8_t*)TxData, &TxMailbox) != HAL_OK)
	{
		uint32_t error = HAL_CAN_GetError(&hcan1);
		Error_Handler ();
	}
}

void sendMessage(){

	/* convert the values to a type we can send*/
	//convertFloatArrayToInt(WITsensor, WITvalues, WITarraySize, 1000);

	/* make a array size so it is devidable by 7 and can accomadate all the vales
	 * WITarraySize * 2 accommodates for all the WIT values
	 * + 2 accommodates for the Senix sensors
	 * + 6 makes it devidable by 7*/
	uint8_t TxDataArray[WITarraySize * 2 + 2 + 6];

	TxDataArray[0] = Senix1;
	TxDataArray[1] = Senix2;

	for(uint8_t i = 2; i < WITarraySize * 2 + 2; i++) {
		TxDataArray[i] = WITvalues[i - 2];
	}

	/*send all the data in different messages*/
	sendPartionalMessage(TxData, TxDataArray, 'A', 0);
//	sendPartionalMessage(TxData, TxDataArray, 'B', 7);
//	sendPartionalMessage(TxData, TxDataArray, 'C', 14);
//	sendPartionalMessage(TxData, TxDataArray, 'D', 21);


}

void CAN_TX_filter_init(void)
{
	TxHeader.StdId = 0x222;
	TxHeader.ExtId = 0;
	TxHeader.IDE = CAN_ID_STD;
	TxHeader.RTR = CAN_RTR_DATA;
	TxHeader.DLC = 8;
	TxHeader.TransmitGlobalTime = DISABLE;
}
/* 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 */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  *   file: pointer to the source file name
  *   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,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

 

 

 

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_1TQ;
  hcan1.Init.TimeSeg1 = CAN_BS1_7TQ;
  hcan1.Init.TimeSeg2 = CAN_BS2_8TQ;
  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 */
  if (HAL_CAN_Start(&hcan1) != HAL_OK) {
  	    Error_Handler();
  	}
  /* USER CODE END CAN1_Init 2 */

}

 

2 REPLIES 2
Karl Yamashita
Lead III

Is this a custom board? What are you trying to send a CAN message to? Wiring diagram, how CAN transceiver is connected to L432?

Have you tried loop back mode?

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.
SofLit
ST Employee

Hello,

First of all, seeing your code, seems you're using internal clock source (MSI) as system clock source which is not recommended in CAN communication. You need to use HSE with an external crystal.

Second, did you already establish a CAN bus with at least another CAN node connected to the bus? What is the transceiver part number are you using?

Third, better to share the schematics. It could be the transceiver is in standby mode (a dedicated pin to put it in standby mode) tied to the wrong voltage level.

 

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.