AnsweredAssumed Answered

Can fails to initialize on STM32F0RB Nucleo board

Question asked by mabr.farid on May 12, 2016
Latest reply on May 13, 2016 by Clive One
Hi,

I used this function below to initialize CAN bus on an STM32MF072RB Nucleo Dev board. However, it looks based on my debugging that the bit zero of MSR_INAK always stays high, it does not get cleared by the hardware and consequently the CAN never gets initialized. Can anybody tell me what might be the reason that the processor does not cleat bit of MSR register ( INAK)?

Thank you in advance.



uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct){

  uint32_t InitStatus = CAN_INITSTATUS_FAILED;        //data type changed from uint8_t to uint32_t
  uint32_t wait_ack = 0x00000000;

  /* Check the parameters */
  assert_param(IS_CAN_ALL_PERIPH(CANx));
  assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM));
  assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM));
  assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM));
  assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART));
  assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM));
  assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP));
  assert_param(IS_CAN_MODE(CAN_InitStruct->CAN_Mode));
  assert_param(IS_CAN_SJW(CAN_InitStruct->CAN_SJW));
  assert_param(IS_CAN_BS1(CAN_InitStruct->CAN_BS1));
  assert_param(IS_CAN_BS2(CAN_InitStruct->CAN_BS2));
  assert_param(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler));

  /* Exit from sleep mode */
  CANx->MCR &= (~(uint32_t)CAN_MCR_SLEEP);                     

  /* Request initialisation */
  CANx->MCR |= CAN_MCR_INRQ ;

  /* Wait the acknowledge */
  while (((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
      {
        wait_ack++;
      }

      /* Check acknowledge */
      if ((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
      {
        InitStatus = CAN_INITSTATUS_FAILED;        //data type changed from uint8_t to uint32_t
      }
      else
      {
            /* Set the time triggered communication mode */
            if (CAN_InitStruct->TTCM == ENABLE)
            {
              CANx->MCR |= CAN_MCR_TTCM;
            }
            else
            {
              CANx->MCR &= ~(uint32_t)CAN_MCR_TTCM;
            }

            /* Set the automatic bus-off management */
            if (CAN_InitStruct->ABOM == ENABLE)
            {
              CANx->MCR |= CAN_MCR_ABOM;
            }
            else
            {
              CANx->MCR &= ~(uint32_t)CAN_MCR_ABOM;
            }

            /* Set the automatic wake-up mode */
            if (CAN_InitStruct->AWUM == ENABLE)
            {
              CANx->MCR |= CAN_MCR_AWUM;
            }
            else
            {
              CANx->MCR &= ~(uint32_t)CAN_MCR_AWUM;
            }

            /* Set the no automatic retransmission */
            if (CAN_InitStruct->NART == ENABLE)
            {
              CANx->MCR |= CAN_MCR_NART;
            }
            else
            {
              CANx->MCR &= ~(uint32_t)CAN_MCR_NART;
            }

            /* Set the receive FIFO locked mode */
            if (CAN_InitStruct->RFLM == ENABLE)
            {
              CANx->MCR |= CAN_MCR_RFLM;
            }
            else
            {
              CANx->MCR &= ~(uint32_t)CAN_MCR_RFLM;
            }

            /* Set the transmit FIFO priority */
            if (CAN_InitStruct->TXFP == ENABLE)
            {
              CANx->MCR |= CAN_MCR_TXFP;
            }
            else
            {
              CANx->MCR &= ~(uint32_t)CAN_MCR_TXFP;
            }

    /* Set the bit timing register */
    CANx->BTR = (uint32_t)((uint32_t)CAN_InitStruct->Mode << 30) | \
                ((uint32_t)CAN_InitStruct->SJW << 24) | \
                ((uint32_t)CAN_InitStruct->BS1 << 16) | \
                ((uint32_t)CAN_InitStruct->BS2 << 20) | \
               ((uint32_t)CAN_InitStruct->Prescaler - 1);

    /* Request leave initialisation */
    CANx->MCR &= ~(uint32_t)CAN_MCR_INRQ;

   /* Wait the acknowledge */
   wait_ack = 0;

   while (((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
   {
     wait_ack++;
   }

        /* ...and check acknowledged */
        if ((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
        {
          InitStatus = CAN_INITSTATUS_FAILED;        //data type changed from uint8_t to uint32_t  
        }
        else
        {
          InitStatus = CAN_INITSTATUS_SUCCESS;        //data type changed from uint8_t to uint32_t
        }
 
    }
  /* At this step, return the status of initialization */
  return InitStatus;

}

Outcomes