AnsweredAssumed Answered

CAN Problem on STM32F415

Question asked by hakobyan.davit on May 15, 2015
Latest reply on May 16, 2015 by hakobyan.davit
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;
}

Attachments

Outcomes