1
\$\begingroup\$

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

\$\endgroup\$
2
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – Lundin
    Sep 27, 2017 at 11:14
  • \$\begingroup\$ @Lundin ... you pointed me in the right direction. For my future reference and others, I added it as answer. Thanks for your comment. \$\endgroup\$ Sep 28, 2017 at 23:13

1 Answer 1

2
\$\begingroup\$

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;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.