cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103RBT6 CAN bus heavy issue

B C
Associate II
Posted on April 23, 2018 at 17:15

Hello everyone, I am trying to configure my CAN on my OLIMEXINO STM32 board : 

https://www.olimex.com/Products/Duino/STM32/OLIMEXINO-STM32/

  (with a STM32F103RBT6 �C), but it just won't work ...

I'm using a PCAN USB to communicate between my PC and my board, but there is no data exchange between my board and my computer. The Pcan software is sending me a 'bus heavy' flag and I don't know why.

Here is my code : 

int nb_trames = 4;

int ID[8] = {0x06, 0x11, 0x12, 0x13, 0x14,0x15,0x16,0x17};

Tab_TxMessages = malloc(nb_trames * sizeof(CanTxMsgTypeDef));

...

while (1)

{

   int cpt = 0 ;

   for (cpt = 0 ; cpt < nb_trames ; cpt++)

      {

      Tab_TxMessages[cpt].StdId = ID[cpt];

      Tab_TxMessages[cpt].IDE = 0;

      Tab_TxMessages[cpt].RTR = 0;

      Tab_TxMessages[cpt].DLC = 8;

      Tab_TxMessages[cpt].Data[0] = cpt;

      Tab_TxMessages[cpt].Data[1] = cpt+1;

      Tab_TxMessages[cpt].Data[2] = cpt+2;

      Tab_TxMessages[cpt].Data[3] = cpt+3;

      Tab_TxMessages[cpt].Data[4] = cpt+4;

      Tab_TxMessages[cpt].Data[5] = cpt+5;

      Tab_TxMessages[cpt].Data[6] = cpt+6;

      Tab_TxMessages[cpt].Data[7] = cpt+7;

      HAL_CAN_Transmit(&hcan, HAL_MAX_DELAY);

      HAL_Delay(1);

      }

}

(don't know how to properly format it though, if anyone can help...)

The issue is not coming from the 120 Ohm (I set the jumper correctly on the board). However it might come from the way I configure my CAN bus : 

void MX_CAN_Init(void)

{

hcan.Instance = CAN1;

hcan.Init.Prescaler = 4;

hcan.Init.Mode = CAN_MODE_NORMAL;

hcan.Init.SJW = CAN_SJW_1TQ;

hcan.Init.BS1 = CAN_BS1_8TQ;

hcan.Init.BS2 = CAN_BS2_8TQ;

hcan.Init.TTCM = DISABLE;

hcan.Init.ABOM = DISABLE;

hcan.Init.AWUM = DISABLE;

hcan.Init.NART = DISABLE;

hcan.Init.RFLM = DISABLE;

hcan.Init.TXFP = DISABLE;

if (HAL_CAN_Init(&hcan) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

I have no idea how to correctly configure it, so if anyone dare to explain, that might help solve the issue..

Thank you for the help !

#pcan #can-interface #olimex #stm32f103rbt6
33 REPLIES 33
Posted on April 26, 2018 at 09:01

The GPIO init is also in the code, isn't it ? HAL_CAN_MspInit(&hcan) in the beginning of the main, that's the GPIO init, no ?

void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)

{

   GPIO_InitTypeDef GPIO_InitStruct;

   if(canHandle->Instance==CAN1)

   {

      /* USER CODE BEGIN CAN1_MspInit 0 */

      /* USER CODE END CAN1_MspInit 0 */

      /* CAN1 clock enable */

      __HAL_RCC_CAN1_CLK_ENABLE();

      /**CAN GPIO Configuration

      PA11 ------> CAN_RX

      PA12 ------> CAN_TX

      */

      GPIO_InitStruct.Pin = GPIO_PIN_11;

      GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

      GPIO_InitStruct.Pull = GPIO_NOPULL;

      HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

      GPIO_InitStruct.Pin = GPIO_PIN_12;

      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

      HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

      /* USER CODE BEGIN CAN1_MspInit 1 */

      /* USER CODE END CAN1_MspInit 1 */

   }

}

For some reason here I forgot to change it to PB8 and PB9, but I checked again and on my original project it was PB8 and PB9

Posted on April 26, 2018 at 09:23

This is the time when the CAN transceiver samples the bus level during each bit, usually given in per cent of the bit time.

My PCAN Explorer uses a default of 85%, and I never needed to change it.

Posted on April 26, 2018 at 09:25

So rebuild for PB8 and PB9, double check (and do it really) that the remapping takes place correctly for CAN1 to use PB8/9. The good news, is that I can see with your binary that the CAN controller is desperately trying to drive PB12, and obviously fails because PB11 does not reflects the corresponding electrical level.

Posted on April 26, 2018 at 10:15

I feel a bit dumb though, but thank you everyone for the help anyway !

No need to.

This is kind of arcane CAN bus knowledge ...

To allow for a proper bus arbitration, and for error checking, every transceiver samples the bus state back at each bit.

One must consider the bus structure (cables, devices), to factor in all propagation delays.

The signal must have propagated to the most distant 'node' and back in this time.