AnsweredAssumed Answered

STM32F103 CAN transmit error Bit dominant Error

Question asked by stocker.bence.002 on May 9, 2016
Latest reply on May 10, 2016 by ashworth.rob.002
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++){}
     }
}

Outcomes