Skip to main content
Michel Keijzers
Associate II
October 19, 2017
Question

STM32 HAL_CAN_Transmit always returns TIMEOUT (HAL_CAN_STATE_TIMEOUT)

  • October 19, 2017
  • 6 replies
  • 4720 views
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
This topic has been closed for replies.

6 replies

Amel NASRI
Technical Moderator
October 20, 2017
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 "Best Answer" on the reply which solved your issue or answered your question.
Tesla DeLorean
Guru
October 20, 2017
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 VenmoUp vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
October 20, 2017
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 VenmoUp vote any posts that you find helpful, it shows what's working..
Michel Keijzers
Associate II
October 20, 2017
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 II
October 21, 2017
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.

T J
Senior III
May 3, 2018
Posted on May 03, 2018 at 01:37

did you get this running ?

ABOM should be enabled.

Michel Keijzers
Associate II
July 7, 2018
Posted on July 07, 2018 at 13:03

Sorry for my late reaction.

I tried yesterday to set the ABOM to enabled, but no difference.
brucegong
Associate III
May 2, 2018
Posted on May 02, 2018 at 18:57

stm32f103c8t6 + cubemx的can通信,我�到过和你�其类似的问题,已�解决。我在此记录我的解决过程,希望对你有帮助:

第一个é�?™è¯¯ï¼šç�?¨äº†é»˜è®¤çš„CAN_TXå’ŒCAN_RX。开å�‘æ�¿ä¸Šå·²ç»�将这两个引脚分é…�给了USB接å�£ã€‚而ä¸�?USB上连ç�?µé˜»ç­‰å…ƒä»¶éƒ½å·²ç»�有了,å¤�ç�?¨èµ·æ�¥ä¼šå¾ˆéº»çƒ¦ï¼Œæ‰€ä»¥åº�?该将CAN进行remap,转到PB8å’ŒPB9。(很显然,这一步你已ç»�å�šäº†ï¼‰

第二个é�?™è¯¯ï¼šæˆ‘ç�?¨çš„第一å�—å¼€å�‘æ�¿ï¼ŒPB8å’ŒPB9的两个引脚,ä¾�然没安排å�šäº†å…¶ä»–ç�?¨é€�?。这是我自己没ä»�?细看开å�‘æ�¿åŽŸç�†å›¾çš„å�Žæžœã€‚事已至此,ä¸�å¾—ä¸�æ�¢ä¸€ä¸ªæ›´ç®€æ´�的开å�‘æ�¿äº†ã€‚

第三个é�?™è¯¯ï¼šcané…�置的时候,没有开å�¯SCE中断,以å�ŠRX0中断。当然,中é€�?还出现了给CANæ�?¶å�‘器ç�?¨3.3Vä¾›ç�?µçš„低级é�?™è¯¯ã€‚

然�,我就进入了如楼主所�述的状�。

实际上这时候我é�‡åˆ°çš„æ˜¯ç¬¬0个é�?™è¯¯ï¼š

使ç�?¨cubeMX,我选择了freeos,开å�¯äº†can+master(PB8+PB9)。ç�?±äºŽæˆ‘需è¦�ç�?¨st-link进行调试,所以SYS分æ�?¯æˆ‘设置为“serial wireâ€�,并选择了TIM4作为定时器时钟æº�。然å�Žç�?±äºŽå…¶ä»–功能的需è¦�,我开å�¯äº†PB3 PB4 PB5 PB6四个GPIO模å¼�â€�?â€�?â€�?â€�?è¿™ç§�é…�置的å�Žæžœå°±æ˜¯SYS分æ�?¯ä¸Šå‡ºçŽ°äº†ä¸€ä¸ªé»„è‰²æ„Ÿå�¹å�·ã€‚

真正解决关é�?®é—®é¢˜çš„æ­¥éª¤ï¼Œå°±æ˜¯å�–消掉PB3 PB4 PB5 PB6的使ç�?¨ï¼ŒæŠŠå®ƒä»¬æ�¢å¤�æˆ�默认的reset状æ€�,SYS上的黄色感å�¹å�·å°±æ¶ˆå¤±äº†ã€‚å†�å¾€å�Žå°±ä¸€åˆ‡æ­£å¸¸äº†ã€‚

brucegong
Associate III
May 6, 2018
Posted on May 06, 2018 at 19:34

To the above replies(write in Chinese), I made the following clarification: My project was not completely successful. It seems  there is a bug in the HAL library .

Hardware configuration : stm32f103c8t6 processor, using cubeMX generated project, stm32F1 cube version number 1.6.1. Two TJA1050 work as CAN tranceiver.

The test proccedure is: Device A and Device B are interconnected. A initiates the first session and sends the numbers 0 to B. While B received the message, B sends 1 to A, and A sends 2 to B again... and so on.

The test result: A power on first, then ,B power on, and B can receive the message sent by A. B can send a message, debugtrace on B show success, but A can not receive the message

                      If B power on first and then A power on, B will not receive A message. Since the first message was initiated by A, there was no result in this round of testing.

                     

With the same hardware, a simple test program was written using the std library. The test target was exactly the same as above. The test was completely successful.
T J
Senior III
May 11, 2018
Posted on May 11, 2018 at 00:45

Make sure the two boards never transmit the same MsgID.

make sure ABOM is enabled

Michel Keijzers
Associate II
July 26, 2018

@Community member​  Dear TJ, thanks for your comment ... Sorry for my late reply; there were some maintenance actions on this forum so I missed your item. I tried however ABOM some time ago but there was no difference. Also, all examples I found so far have ABOM disabled.

Michel Keijzers
Associate II
July 26, 2018

@brucegong​  Thanks for your replies, although I cannot read chinese so cannot read anything from it. ST said they will look into this problem, so I hope for a reply.