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

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)?

• Just a wild guess, but did you set hcan to a valid instance (CAN1 or whatever it is called in ST)? Similar the the message pointers. If they are pointing at garbage, something like this might happen. – Lundin Sep 27 '17 at 11:14
• @Lundin ... you pointed me in the right direction. For my future reference and others, I added it as answer. Thanks for your comment. – Michel Keijzers Sep 28 '17 at 23:13

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;