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
Posted on May 12, 2018 at 15:42

‘scope’ =Oscilloscope.....OK, I have one.

As described in Chinese above, I used an incorrect boardwhich PB8 and PB9 were occupied by other function. Using the oscilloscope, I identified the problem, purchased two new boards, andmake a ‘partially successful’ ping-pong test.

The same hardware configuration, the ping-pong test with the std library was completely successful----so I believe my hardware is OK

In my work routines, the simplest and necessary step in the testing of communication entity is ping-pong test base on two entities with the same hardware configuration. Such testing is more likely to find bugs than instrument testing.

and what's more: Board A work as device A / board B work as device B and board A work as device B /board B work as device A ,the test result is total the same.

You may be surprised: On China's e-commerce website, there are a lot of very cheap development boards on sale, and using them is very convenient to complete the tests you want. The board I bought was just more than a dollar. Two boards + two transceivers for a total of about $5.

So no moreinstrument is needed

can you open this site

https://item.taobao.com/item.htm?spm=a1z2.0.0.8bf82e8dGHdbYa&id=523959965779&_u=comahc7bd84

a stm32f103c8t6 mini board costs RMB 7 ,just a little above $1. hoo hoo hoo

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.
Michel Keijzers
Associate III

@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 III

@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.