cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 CAN transmit error Bit dominant Error

drakula
Associate
Posted on May 09, 2016 at 09:13

Hi,

i have problem with the CAN bus on a STM32F103C8 (48 pin). I configed the CAN and after the first transmit the bus went to Error and BusOFF state. The I checked the CAN Error status register and the TEC counter is full and the Last Error Code is: Bit dominant Error. 

I tried in loopback mode the software and then it runs well without error. I tried it with an STM32F103CB and with an STM32F103RB(64 pin), which is on a different hardware and it works in the same way and the bus will be off. So i thint it is a software problem and not both hardware is wrong.

Here is my source code:

int main(void)

{

    NVIC_InitTypeDef  NVIC_InitStructure;

    GPIO_InitTypeDef  GPIO_InitStructure;

    CAN_InitTypeDef        CAN_InitStructure;

    CAN_FilterInitTypeDef  CAN_FilterInitStructure;

    RCC_ClocksTypeDef     rcc;

    CanTxMsg TxMessage;

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

    RCC_GetClocksFreq(&rcc);

     /* GPIO clock enable */

     RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

     RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

     /* Configure CAN pin: RX */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

    /* Configure CAN pin: TX */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_PinRemapConfig(GPIO_Remap1_CAN1 , ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;

     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

     NVIC_Init(&NVIC_InitStructure);

       /* CAN register init */

       CAN_DeInit(CAN1);

       CAN_StructInit(&CAN_InitStructure);

       /* CAN cell init */

       CAN_InitStructure.CAN_TTCM = DISABLE;

       CAN_InitStructure.CAN_ABOM = DISABLE;

       CAN_InitStructure.CAN_AWUM = DISABLE;

       CAN_InitStructure.CAN_NART = DISABLE;

       CAN_InitStructure.CAN_RFLM = DISABLE;

       CAN_InitStructure.CAN_TXFP = DISABLE;

       //CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack;

       CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;

       /* CAN Baudrate = 0.5MBps*/

       CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

       CAN_InitStructure.CAN_BS1 = CAN_BS1_15tq;

       CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;

       CAN_InitStructure.CAN_Prescaler = 4;

       CAN_Init(CAN1, &CAN_InitStructure);

       /* CAN filter init */

       CAN_FilterInitStructure.CAN_FilterNumber = 0;

       CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;

       CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;

       CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;

       CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;

       CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;

       CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;

       CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;

       CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;

       CAN_FilterInit(&CAN_FilterInitStructure);

     CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);

     /* Transmit */

     TxMessage.StdId = 0x321;

     TxMessage.ExtId = 0x01;

     TxMessage.RTR = CAN_RTR_DATA;

     TxMessage.IDE = CAN_ID_STD;

     TxMessage.DLC = 1;

     TxMessage.Data[0] = 1;

     uint32_t i = 0;

     /* Infinite loop */

     while (1)

     {

         CAN_Transmit(CAN1, &TxMessage);

         for(i=0;i<50000;i++){}

     }

}

#stm32f103-can-bit-dominant-error
3 REPLIES 3
rob2399
Associate II
Posted on May 10, 2016 at 12:58 I have a bit of code which does something very similar on the same uC. Could you try these CAN Bit timing settings for 1MBit/s...?

/* CAN Baudrate = 1MBps*/ 
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; 
CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;//iriginally 3tq 
CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;//originally 5tq 
CAN_InitStructure.CAN_Prescaler = 4;//was 4 for 72Mhz clk 
CAN_Init(CANx, &CAN_InitStructure);

Posted on May 10, 2016 at 14:26

The APB1 in this case would be 36 MHz

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rob2399
Associate II
Posted on May 10, 2016 at 17:50

I assume you have also got the obvious 120 Ohm resistor at either end...?  That will definitely cause issues in the real world if you haven't got them.  The Sample point should also be around 75-90%.