cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 HAL_CAN_Transmit always returns TIMEOUT (HAL_CAN_STATE_TIMEOUT)

Michel Keijzers
Associate III
Posted on October 19, 2017 at 11:11

(note this is my first question on ST community If I can do anything to improve the question, please let me know).

Setup

I am using an STM32F103C8T6 (aka Blue Pill).

With the STM32Cube I set CAN_RX to PB8 and CAN_TX9 to PB9 (these are defaults/nonchangeable).

The generated CAN settings are:

 hcan.Instance = CAN1;
 hcan.Init.Prescaler = 16;
 hcan.Init.Mode = CAN_MODE_NORMAL;
 hcan.Init.SJW = CAN_SJW_1TQ;
 hcan.Init.BS1 = CAN_BS1_4TQ;
 hcan.Init.BS2 = CAN_BS2_5TQ;
 hcan.Init.TTCM = DISABLE;
 hcan.Init.ABOM = DISABLE;
 hcan.Init.AWUM = DISABLE;
 hcan.Init.NART = DISABLE;
 hcan.Init.RFLM = DISABLE;
 hcan.Init.TXFP = DISABLE;�?�?�?�?�?�?�?�?�?�?�?�?

Program

(removed STM HAL generated comments blocks)

 int main(void)
 {
 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();
 
 /* Configure the system clock */
 SystemClock_Config();
 
 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_CAN_Init();
 
 /* USER CODE BEGIN 2 */
 hcan.pRxMsg = &_rxMessage;
 hcan.pTxMsg = &_txMessage;
 
 _sFilterConfig.FilterActivation = ENABLE;
 _sFilterConfig.FilterNumber = 0;
 _sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
 _sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
 _sFilterConfig.FilterIdHigh = 0x0000;
 _sFilterConfig.FilterIdLow = 0x0000;
 _sFilterConfig.FilterMaskIdHigh = 0x0000;
 _sFilterConfig.FilterMaskIdLow = 0x0000;
 _sFilterConfig.FilterFIFOAssignment = 0;
 _sFilterConfig.BankNumber = 14;
 if (HAL_CAN_ConfigFilter(&hcan, &_sFilterConfig) != HAL_OK)
 {
 Error_Handler();
 }
 
 hcan.pTxMsg->StdId = 0x321;
 hcan.pTxMsg->ExtId = 0x01;
 hcan.pTxMsg->RTR = CAN_RTR_DATA;
 hcan.pTxMsg->IDE = CAN_ID_STD;
 hcan.pTxMsg->DLC = 2;
 hcan.pTxMsg->Data[0] = 0;
 while (1)
 {
 hcan.pTxMsg->Data[0]++;
 
 if (HAL_CAN_Transmit(&hcan, 10) != HAL_OK)
 {
 Error_Handler();
 }
 HAL_Delay(1000);
 
 if (HAL_CAN_Receive_IT(&hcan, CAN_FIFO0) != HAL_OK)
 {
 Error_Handler();
 }
 
 }
 }�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Loopback mode

When I use loopback mode:

hcan.Init.Mode = CAN_MODE_LOOPBACK

instead of Normal mode, I can transmit and receive messages (and the hcan shows the correct data in the received message).

Problem

However, in Normal mode (as shown in the code fragment above) I always get a timeout in the next command:

if (HAL_CAN_Transmit(&hcan, 10) != HAL_OK)

The function returns: HAL_CAN_STATE_TIMEOUT

All initialization seems to be ok (all functions return HAL_OK).

Analysis

What I tried was:

- Using another STM32: no difference

- Using another transceiver: no difference - Played a bit with the SJW/BS1/BS2 time quantities: no difference - Making sure the time quantities were equal - Playing with different data values and filters: no difference - Checked the output of PB9 (CAN transmit): it seems not to change at all (so this is a problem): no difference - Removing the wire from GPIO PB9 (CAN Tx) to the TX of my CAN transceiver : no difference. - Checking the Transmit is cancelled (which is needed and was an old bug, but has already been fixed by the HAL library I'm using).

Observations

- ESR is 111110000000000001010111, which means LEC[2:0] (see [Reference manual, page 682][1]), is 101 which is Bit dominant error

- BTR is 11101010000000001001111, which means bit 30 LKBM: Loop back Mode (debug) is 1, which is strange because in the setup I assigned CAN_MODE_NORMAL. - Directly after the command, the State is HAL_CAN_STATE_TIMEOUT, the Lock is HAL_UNLOCKED, the ErrorCode is 0.

Questions

1. Why do I get a timeout? I'm having a hard time trying to find more to check what I already did. What bothers me is that the PB9 GPIO does not change, so I'm sure it's a software problem, but since there is no error code (it's 0) and initialization is OK, I'm getting out of clues.

Related question:

2. Am I in loopback mode or not (according to the Setup I put CAN_MODE_NORMAL, but the BTR bit 30: LKBM shows otherwise.

[1]:

http://www.st.com/content/ccc/resource/technical/document/reference_manual/59/b9/ba/7f/11/af/43/d5/CD00171pdf/files/CD00171pdf/jcr:content/translations/en.CD00171pdf

#can #stm32f103 #stm32cube
23 REPLIES 23
Amel NASRI
ST Employee
Posted on October 20, 2017 at 17:15

Hi

michelkeijzers

‌,

I appreciate the way you are describing your problem. This makes it easier to understand.I recommend you also to use the ''Syntax Highlighter'' to make the code formatted:

0690X00000608j3QAA.png

Then, regarding the usage of the CAN in Normal mode, I recommend you to review the exampleSTM32Cube_FW_F1_V1.6.0\Projects\STM3210E_EVAL\Examples\CAN\CAN_Networking.

Can you test this example (after porting it to your own HW)?

Then, I would like to come back to this note in your description:

''

With the STM32Cube I set CAN_RX to PB8 and CAN_TX9 to PB9 (these are defaults/nonchangeable).''

This is not right: You can check other choices putting cursor on the selected pin by CubeMX, then <Ctrl + click>.

-Amel

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.

Posted on October 20, 2017 at 17:34

>>Why do I get a timeout?

You need something on the bus to respond that it actually received the message, sending it into the abyss won't work. If you have another responding device then you need to make sure they are both configured at the same speed.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 20, 2017 at 17:37

Using the Syntax Highlighter always puts things into Moderation, it is a conversation killer if that takes 4 or 8 hours to clear. Ideally such post shouldn't flag, but the delivery target should be under 5 minutes.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 20, 2017 at 22:09

Thank you ... I will try it (not sure if I have much time this weekend). I'm a beginner with STM32 so hope I can port it.

I don't think I'm able to edit my question (I cannot find the option). But for new code I will use it, thanks.

And you are right, it's possible to change the pins (I didn't know the Control-shortcut). I can try it, but I doubt it will make a difference.
Posted on October 20, 2017 at 22:10

I have two microcontrollers with receivers and they both run the same program (thus same speed).

Michel Keijzers
Associate III
Posted on October 21, 2017 at 12:48

(Since I followed up the advice to edit the question because of code-coloring, I lost all replies :( ) .... Someone wrote me to check the examples, which I will do.

Posted on October 22, 2017 at 14:27

To edit you need to go to the thread view not the inbox view. Click on the thread title, and Edit should be under the Action options.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 22, 2017 at 14:33

Thanks Clive ... I'm new to ST community (and STM) ... but luckily I have all the emails from you and Amel N.

Today if I have time (not sure) I try to check with an official example from STM10E (which is somewhat similar to STM32F103).
Posted on October 23, 2017 at 00:41

st.mcu

‌ I copied as much as possible from the example, but without any difference.

I found however something stupid: my CAN receiver works on 5V and my STM32 is 3.3V, so I added a voltage supply that gives 5V and use that for the CAN transceivers (and connecting all grounds together). But still no difference. I ordered specifically 3.3V CAN transceivers and will try again when they arrive (can take some time).