AnsweredAssumed Answered

STM32 HAL_CAN_Transmit always returns TIMEOUT (HAL_CAN_STATE_TIMEOUT)

Question asked by Michel Keijzers on Oct 19, 2017
Latest reply on May 12, 2018 by gong.bruce

(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/CD00171190.pdf/files/CD00171190.pdf/jcr:content/translations/en.CD00171190.pdf

Outcomes