cancel
Showing results for 
Search instead for 
Did you mean: 

CAN TX not working in normal mode, only Loopback mode

garzarolli
Associate II
Posted on February 20, 2011 at 19:24

CAN TX not working in normal mode, only Loopback mode

5 REPLIES 5
garzarolli
Associate II
Posted on May 17, 2011 at 14:25

Hello,

thank you for this hint, the init procedure returns CANINITFAILED. What does this mean? Is the MCU damaged?

I bridged the RX and TX (PA11 & PA12), is this okay?

garzarolli
Associate II
Posted on May 17, 2011 at 14:25

CAN_DeInit stops the clock, so the clock enable has to be done after this command. Now the initialisations works, but when I sent a CAN_Transmit (with RX and TX bridged), the status of the message stays pending, what's wrong now?

kacem.halim
Associate II
Posted on May 17, 2011 at 14:25

Hi mgarza,

From my side i think that your FW configuration is correct.

But maybe that you have a HW problem, so what i suggest you to do is to check the return status of CAN_Init(CAN1, &CAN_InitStructure) function (return value must be CANINITOK )

With best regards,

HalimFrom: mgarza

Posted: Sunday, February 20, 2011 7:24 PM

Subject: CAN TX not working in normal mode, only Loopback mode

Hello,

I do have the probelem that CAN is not operating in nomal mode, when trying transmit a CAN message the message transmission is not executed. It's working fine in loop back mode, but not in normal mode. I'm using an STM32F103RE microcrontroller. RTS is not used and I get the error when USART1 is switched off. What am I doing wrong?

Code RCC init (72MHz):

    RCC_DeInit();

   

    /* Enable HSE */

    RCC_HSEConfig(RCC_HSE_ON);

    /* Wait till HSE is ready */

    HSEStartUpStatus = RCC_WaitForHSEStartUp();

    /* Enable Prefetch Buffer */

    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    /* Flash 2 wait state */

    FLASH_SetLatency(FLASH_Latency_2);

    

    /* HCLK = SYSCLK */

    RCC_HCLKConfig(RCC_SYSCLK_Div1);

 

    /* PCLK2 = HCLK */

    RCC_PCLK2Config(RCC_HCLK_Div1);

    /* PCLK1 = HCLK/2 */

    RCC_PCLK1Config(RCC_HCLK_Div2);

    /* PLLCLK = 4MHz * 9 = 36 MHz */

    //RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_9);

    /* PLLCLK = 8MHz * 9 = 72 MHz */

    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

    /* Enable PLL */

    RCC_PLLCmd(ENABLE);

    /* Wait till PLL is ready */

    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {}

    /* Select PLL as system clock source */

    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    /* Wait till PLL is used as system clock source */

    while(RCC_GetSYSCLKSource() != 0x08) {}

CAN init:

   CAN_InitTypeDef        CAN_InitStructure;

   CAN_FilterInitTypeDef  CAN_FilterInitStructure;

   GPIO_InitTypeDef GPIO_InitStructure;

   NVIC_InitTypeDef NVIC_InitStructure;

   /* CAN1 Periph clock enable */

   RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);

//   GPIO_PinRemapConfig(GPIO_Remap1_CAN1, DISABLE);

//   GPIO_PinRemapConfig(GPIO_Remap2_CAN1, DISABLE);

   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

   /* Configure CAN1 RX pin */

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

   GPIO_Init(GPIOA, &GPIO_InitStructure);

   /* Configure CAN1 TX pin */

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;

   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

   GPIO_Init(GPIOA, &GPIO_InitStructure);

    

   /* Enable the CAN1 Interrupt */

   NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;

   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;

   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

   NVIC_Init(&NVIC_InitStructure);

//   NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn;

//   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;

//   NVIC_Init(&NVIC_InitStructure);

  /* CAN register init */

  CAN_DeInit(CAN1);

  CAN_StructInit(&CAN_InitStructure);

  /* CAN1 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_Normal;//CAN_Mode_LoopBack CAN_Mode_Normal

  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

  CAN_InitStructure.CAN_BS1 = CAN_BS1_12tq;

  CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;

  CAN_InitStructure.CAN_Prescaler = 4;

  CAN_Init(CAN1, &CAN_InitStructure);

  /* CAN1 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);

  CAN_ITConfig(CAN1, CAN_IT_FMP0 | CAN_IT_FF0 | CAN_IT_FOV0, ENABLE);  // fifo0??

//  CAN_ITConfig(CAN1, CAN_IT_TME, DISABLE);                // ????

  CAN_ITConfig(CAN1, CAN_IT_EWG | CAN_IT_EPV | CAN_IT_BOF | CAN_IT_LEC | CAN_IT_ERR | CAN_IT_WKU | CAN_IT_SLK, ENABLE);  // ERR??

CAN Message Transmission (TX):

    CanTxMsg TxMessage;

    uint8_t TransmitMailbox = 0;

    uint8_t i;

    /* transmit */

    TxMessage.StdId=0x123;

    TxMessage.ExtId=0x01;

    TxMessage.RTR=CAN_RTR_DATA;

    TxMessage.IDE=CAN_ID_STD;

    TxMessage.DLC=1;

    TxMessage.Data[0]=0x40;

    TxMessage.Data[1]=0x02;

    TxMessage.Data[2]=0x1a;

    TxMessage.Data[3]=0x80;

    TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);
smart2
Associate II
Posted on May 17, 2011 at 14:25

Just make sure that you have another CAN device with the right baud rate on the network that can receive your packet.

 Also you can check the CAN error(CAN_ESR) status register to find out the type of error.

garzarolli
Associate II
Posted on May 17, 2011 at 14:25

Now I connected 2 MCUs and and uploaded the can_init procedure. Unfortunately the reception of the transmitted data fails, when debuging the can registers of the receiving device I see that the 1st device tries to transmitt the message, but the second reports sometimes ''form error'' and sometimes ''stuff error''.

What's wrong now? Where do I have to searcg for the error now?