cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeMX + CAN + HAL

UHarb.665
Associate

I have a STM32F373VCT6 MCU with CAN bus connected to PD0 and PA12 pins. I also have an old project which have these pins configured by hands and CAN bus works properly for this project.

Now, I need to create new project for this MCU and I try to do it with STM32CubeMX v.5.4.0.

I choose my MCU from list, add FreeRTOS to it and set PD0 and PA12 pins as CAN_RX and CAN_TX by CubeMx GUI, and generate project with makefile.

Next, in source code, I use HAL_CAN_AddTxMessage function to send some dummy frames every 20ms, but CAN bus have nothing. I think it could be bitrate problem and I took oscilloscope for check what bitrate is present on bus, but bus have no signal at all. Old project created without MX and without HAL works fine and oscilloscope show signal on bus.

There is my CAN initialization and usage code:

static void MX_CAN_Init(void)
{
 
  /* USER CODE BEGIN CAN_Init 0 */
 
  /* 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_12TQ;
  hcan.Init.TimeSeg2 = CAN_BS2_5TQ;
  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 */
 
  /* USER CODE END CAN_Init 2 */
 
}
 
 
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses 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_DIV2;
  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();
  }
}
 
 
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN 5 */
  uint8_t data[8];
  CAN_TxHeaderTypeDef txHeader;
  txHeader.StdId = 0x1D0;
  txHeader.IDE = CAN_ID_STD;
  txHeader.RTR = CAN_RTR_DATA;
  txHeader.DLC = 8;
  txHeader.TransmitGlobalTime = DISABLE;
  for (int i = 0; i < 8; ++i) {
    data[i] = i;
  }
  uint32_t mailbox;
  /* Infinite loop */
  for(;;)
  {
    HAL_CAN_AddTxMessage(&hcan, &txHeader, data, &mailbox);
    // LED blinking
    HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_2);
    osDelay(20);
  }
  /* USER CODE END 5 */
}

If I try blinking some LEDs from my code, it works fine, so, only CAN part does not work properly.

Checking return status of HAL_CAN_AddTxMessage show that first 3 messages are sent with HAL_OK, but all next show that mailboxes are full. Why can messages stack in mailboxes and not appear on physicall CAN bus at all?

0 REPLIES 0