Electronic – STM32 HAL CAN does not update value and crashes when setting ExtId

candebuggingstm32variable

I'm running my first CAN test program using STM32CubeMX, STM32F103C8T6 and Eclipse.

I used default CAN settings in STM32CubeMX (also tried loopback but the results are equal).

What I see (see pic below) is two things I do not understand;

  • In the picture below, you can see I exceeded the line with hcan.pTxMsg->StdId = 0x321 (left red oval), but the current value shows 536891392 (right red oval).
  • When I execute the next line (green): hcan.pTxMsg->ExtId = 0x01, I end up in a hard fault interrupt:

    /**

    • @brief This function handles Hard fault interrupt.
      /
      void HardFault_Handler(void)
      {
      /
      USER CODE BEGIN HardFault_IRQn 0 */

      /* USER CODE END HardFault_IRQn 0 /
      while (1)
      {
      }
      /
      USER CODE BEGIN HardFault_IRQn 1 */

      /* USER CODE END HardFault_IRQn 1 */
      }

Why is the value not updated and why do I get a hardware fault (the pointer hcan.pTxMsg is not NULL since it even displays the field StdId (it even can display ExtId which has value 134222077 (not shown in pic below)?

screenshot

Best Answer

By default STM32CubeMx create the initialization code for 'can' (if selected). This code can be found in MX_CAN_Init and is called by main.

However, it does not assign/initialize the pRxMsg and pTxMsg.

The pTxMsg should be defined as:

CanTxMsgTypeDef txMessage;
hcan.pTxMsg = &txMessage;

and the pRxMsg as:

CanRxMsgTypeDef rxMessage;
hcan.pRxMsg = &rxMessage;