cancel
Showing results for 
Search instead for 
Did you mean: 

CAN Problem on STM32F415

davhak
Associate II
Posted on May 15, 2015 at 13:24

Dear All,

There is a problem with CAN message reception/transmission with STM32F415 on a custom board with MCP2551 CAN tranceiver. CAN is initialized successfully.

I have checked the APB1 clock with RCC_GetClocksFreq function to confirm that it is 42 MHz.

In the attached oscilloscope snapshot (green channel is CAN_RX, yellow channel is CAN_TX), I guess, one may see that the incoming message is acknowledged by STM32,

however no CAN1_RX0_IRQHandler interrupt is triggered (the CAN_MessagePending also returns 0). CAN message transmission also returns a pending status.

In the oscilloscope snapshot one may see that CAN_RX voltage is 5V while CAN_TX is 3.3V. This, however, is, probably, not the cause of the problem as if I use a NOPULL for CAN_TX pin

and thus use the 5V external pull-up, I still don't get any CAN message.

The CAN code is attached below.

Could someone suggest anything?

Thanks very much in advance.

----------------------------------

CAN code

----------------------------------

void CAN1_RX0_IRQHandler()

{

   U8 msginbuff    = 0;

   U8 i            = 0;

   if (CAN_GetITStatus(CAN1,CAN_IT_FMP0))

   {

      CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0);

      msginbuff = CAN_MessagePending(CAN1, CAN_FIFO0);

      for (i = 0; i < msginbuff; i++)

      {

         CAN_Receive(CAN1, CAN_FIFO0, &g_RxMessages[g_rxmsg_wr_ind]);

         ++g_rxmsg_n;

         if (++g_rxmsg_wr_ind >= CAN_RX_MSG_N)

            g_rxmsg_wr_ind = 0;

         if (g_rxmsg_n > CAN_RX_MSG_N)

            g_rxmsg_overflow = 1;

         CAN_FIFORelease(CAN1, CAN_FIFO0);

      }

   }

}

void CAN_RCC_Configuration()

{

   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

   RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

}

void CAN_GPIO_Configuration(void)

{

    GPIO_InitTypeDef GPIO_InitStructureCAN_RX;

    GPIO_InitTypeDef GPIO_InitStructureCAN_TX;

    GPIO_InitStructureCAN_RX.GPIO_Pin = GPIO_Pin_8;

    GPIO_InitStructureCAN_RX.GPIO_Mode = GPIO_Mode_AF;

//    GPIO_InitStructureCAN_RX.GPIO_OType = GPIO_OType_PP;

//    GPIO_InitStructureCAN_RX.GPIO_PuPd = GPIO_PuPd_UP;

//    GPIO_InitStructureCAN_RX.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOB, &GPIO_InitStructureCAN_RX);

    GPIO_InitStructureCAN_TX.GPIO_Pin = GPIO_Pin_9;

    GPIO_InitStructureCAN_TX.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructureCAN_TX.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructureCAN_TX.GPIO_PuPd = GPIO_PuPd_UP; //GPIO_PuPd_NOPULL;

    GPIO_InitStructureCAN_TX.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOB, &GPIO_InitStructureCAN_TX);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_CAN1);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_CAN1);

}

U8 CAN_Configuration()

{

   CAN_InitTypeDef CAN_InitStructure;

   CAN_DeInit(CAN1);

   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_InitStructure.CAN_SJW = CAN_SJW_1tq;

   CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq; //CAN_BS1_14tq;

   CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq; //CAN_BS2_6tq;

   CAN_InitStructure.CAN_Prescaler = 6; //4;      // Baudrate 500 kbps

   if (CAN_Init(CAN1, &CAN_InitStructure)) return 1;

   return 0;

}

void CAN_FilterConfiguration()

{

   CAN_FilterInitTypeDef *pfilter = &g_rx_filters[0];

   pfilter->CAN_FilterNumber = 0; // Filter number = 0 (0<=x<=13)

   pfilter->CAN_FilterMode = CAN_FilterMode_IdMask;

   pfilter->CAN_FilterScale = CAN_FilterScale_16bit; //CAN_FilterScale_32bit;

   pfilter->CAN_FilterIdHigh = 0x0000;

   pfilter->CAN_FilterIdLow = 0x0000;

   pfilter->CAN_FilterMaskIdHigh = 0x0000;

   pfilter->CAN_FilterMaskIdLow = 0x0000;

   pfilter->CAN_FilterFIFOAssignment = CAN_FIFO0;

   pfilter->CAN_FilterActivation = ENABLE;

   CAN_FilterInit(pfilter);

}

void CAN_NVIC_Configuration(void)

{

  NVIC_InitTypeDef  NVIC_InitStructure;

  NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x1;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

}

U8 CANInit()

{

   U8 ret = 0;   

   

   NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );

   CAN_RCC_Configuration();

   CAN_GPIO_Configuration();

   CAN_FilterConfiguration();

   ret = CAN_Configuration();

   CAN_NVIC_Configuration();

   CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);

   CAN_ITConfig(CAN1, CAN_IT_FMP1, ENABLE);

   return ret;

}
3 REPLIES 3
davhak
Associate II
Posted on May 16, 2015 at 11:35

I also check the status flags with CAN_GetFlagStatus function. All the flags return 0.

So I can see that on the hardware layer stm32 responds with an ACK to the incoming message but it does not signal about message reception in any way. On the sending node I also see that there was no error sending the message which otherwise would happen if the sending node would not get the ACK bit set by the receiver.

Is it possible that stm32f415 is corrupt in such a way that its physical CAN layer works but on higher layers something is wrong?

davhak
Associate II
Posted on May 16, 2015 at 12:12

Ok, the problem seems to be resolved.

I was setting the all accept filter before the CAN initialization. It appeared that it must go after the Can_Init function.

davhak
Associate II
Posted on May 16, 2015 at 20:11

This seems to be a solo discussion ;)

Faced another question which has been asked also

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32%20CAN%20Filter&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=1733

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https%3a//my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32%20CAN%20Filter&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=1733

i.e. when using the CAN FIFO0 the FMI field of the received message is twice the value of the previously set filter number (regardless whether 32bit or 16 bit filter is used). And when using the FIFO1 the FMI field is 0.

Could someone elucidate the reason of this?

Thanks a lot for any help.