Examples/Crusher/Linux/stm32/firmware/lib/src/peripherals/stm32f4xx_can.c
/**
  * @brief  Initializes the CAN peripheral according to the specified
  *         parameters in the CAN_InitStruct.
  * @param  CANx: where x can be 1 or 2 to select the CAN peripheral.
  * @param  CAN_InitStruct: pointer to a CAN_InitTypeDef structure that contains
  *         the configuration information for the CAN peripheral.
  * @retval Constant indicates initialization succeed which will be 
  *         CAN_InitStatus_Failed or CAN_InitStatus_Success.
  */
@brief  Initializes the CAN peripheral according to the specified
parameters in the CAN_InitStruct.
@param  CANx: where x can be 1 or 2 to select the CAN peripheral.
@param  CAN_InitStruct: pointer to a CAN_InitTypeDef structure that contains
the configuration information for the CAN peripheral.
@retval Constant indicates initialization succeed which will be
CAN_InitStatus_Failed or CAN_InitStatus_Success.
@brief Initializes the CAN peripheral according to the specified parameters in the CAN_InitStruct . @param CANx : where x can be 1 or 2 to select the CAN peripheral . @param CAN_InitStruct : pointer to a CAN_InitTypeDef structure that contains the configuration information for the CAN peripheral . @retval Constant indicates initialization succeed which will be CAN_InitStatus_Failed or CAN_InitStatus_Success .
uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct)
{
  uint8_t InitStatus = CAN_InitStatus_Failed;
  uint32_t wait_ack = 0x00000000;
  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));
  CANx->MCR &= (~(uint32_t)CAN_MCR_SLEEP);
  CANx->MCR |= CAN_MCR_INRQ ;
  while (((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
  {
    wait_ack++;
  }
  if ((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
  {
    InitStatus = CAN_InitStatus_Failed;
  }
  else 
  {
    if (CAN_InitStruct->CAN_TTCM == ENABLE)
    {
      CANx->MCR |= CAN_MCR_TTCM;
    }
    else
    {
      CANx->MCR &= ~(uint32_t)CAN_MCR_TTCM;
    }
    if (CAN_InitStruct->CAN_ABOM == ENABLE)
    {
      CANx->MCR |= CAN_MCR_ABOM;
    }
    else
    {
      CANx->MCR &= ~(uint32_t)CAN_MCR_ABOM;
    }
    if (CAN_InitStruct->CAN_AWUM == ENABLE)
    {
      CANx->MCR |= CAN_MCR_AWUM;
    }
    else
    {
      CANx->MCR &= ~(uint32_t)CAN_MCR_AWUM;
    }
    if (CAN_InitStruct->CAN_NART == ENABLE)
    {
      CANx->MCR |= CAN_MCR_NART;
    }
    else
    {
      CANx->MCR &= ~(uint32_t)CAN_MCR_NART;
    }
    if (CAN_InitStruct->CAN_RFLM == ENABLE)
    {
      CANx->MCR |= CAN_MCR_RFLM;
    }
    else
    {
      CANx->MCR &= ~(uint32_t)CAN_MCR_RFLM;
    }
    if (CAN_InitStruct->CAN_TXFP == ENABLE)
    {
      CANx->MCR |= CAN_MCR_TXFP;
    }
    else
    {
      CANx->MCR &= ~(uint32_t)CAN_MCR_TXFP;
    }
    CANx->BTR = (uint32_t)((uint32_t)CAN_InitStruct->CAN_Mode << 30) | \
                ((uint32_t)CAN_InitStruct->CAN_SJW << 24) | \
                ((uint32_t)CAN_InitStruct->CAN_BS1 << 16) | \
                ((uint32_t)CAN_InitStruct->CAN_BS2 << 20) | \
               ((uint32_t)CAN_InitStruct->CAN_Prescaler - 1);
    CANx->MCR &= ~(uint32_t)CAN_MCR_INRQ;
   wait_ack = 0;
   while (((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
   {
     wait_ack++;
   }
    if ((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
    {
      InitStatus = CAN_InitStatus_Failed;
    }
    else
    {
      InitStatus = CAN_InitStatus_Success ;
    }
  }
  return InitStatus;
}
uint8_t CAN_Init ( CAN_TypeDef * CANx , CAN_InitTypeDef * CAN_InitStruct ) { uint8_t InitStatus = CAN_InitStatus_Failed ; uint32_t wait_ack = 0x00000000 ; 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 ) ) ; CANx -> MCR &= ( ~ ( uint32_t ) CAN_MCR_SLEEP ) ; CANx -> MCR |= CAN_MCR_INRQ ; while ( ( ( CANx -> MSR & CAN_MSR_INAK ) != CAN_MSR_INAK ) && ( wait_ack != INAK_TIMEOUT ) ) { wait_ack ++ ; } if ( ( CANx -> MSR & CAN_MSR_INAK ) != CAN_MSR_INAK ) { InitStatus = CAN_InitStatus_Failed ; } else { if ( CAN_InitStruct -> CAN_TTCM == ENABLE ) { CANx -> MCR |= CAN_MCR_TTCM ; } else { CANx -> MCR &= ~ ( uint32_t ) CAN_MCR_TTCM ; } if ( CAN_InitStruct -> CAN_ABOM == ENABLE ) { CANx -> MCR |= CAN_MCR_ABOM ; } else { CANx -> MCR &= ~ ( uint32_t ) CAN_MCR_ABOM ; } if ( CAN_InitStruct -> CAN_AWUM == ENABLE ) { CANx -> MCR |= CAN_MCR_AWUM ; } else { CANx -> MCR &= ~ ( uint32_t ) CAN_MCR_AWUM ; } if ( CAN_InitStruct -> CAN_NART == ENABLE ) { CANx -> MCR |= CAN_MCR_NART ; } else { CANx -> MCR &= ~ ( uint32_t ) CAN_MCR_NART ; } if ( CAN_InitStruct -> CAN_RFLM == ENABLE ) { CANx -> MCR |= CAN_MCR_RFLM ; } else { CANx -> MCR &= ~ ( uint32_t ) CAN_MCR_RFLM ; } if ( CAN_InitStruct -> CAN_TXFP == ENABLE ) { CANx -> MCR |= CAN_MCR_TXFP ; } else { CANx -> MCR &= ~ ( uint32_t ) CAN_MCR_TXFP ; } CANx -> BTR = ( uint32_t ) ( ( uint32_t ) CAN_InitStruct -> CAN_Mode << 30 ) | ( ( uint32_t ) CAN_InitStruct -> CAN_SJW << 24 ) | ( ( uint32_t ) CAN_InitStruct -> CAN_BS1 << 16 ) | ( ( uint32_t ) CAN_InitStruct -> CAN_BS2 << 20 ) | ( ( uint32_t ) CAN_InitStruct -> CAN_Prescaler - 1 ) ; CANx -> MCR &= ~ ( uint32_t ) CAN_MCR_INRQ ; wait_ack = 0 ; while ( ( ( CANx -> MSR & CAN_MSR_INAK ) == CAN_MSR_INAK ) && ( wait_ack != INAK_TIMEOUT ) ) { wait_ack ++ ; } if ( ( CANx -> MSR & CAN_MSR_INAK ) == CAN_MSR_INAK ) { InitStatus = CAN_InitStatus_Failed ; } else { InitStatus = CAN_InitStatus_Success ; } } return InitStatus ; }
@brief  Initializes the CAN peripheral according to the specified
         parameters in the CAN_InitStruct.
@brief Initializes the CAN peripheral according to the specified parameters in the CAN_InitStruct .
/* Check the parameters */ /* Exit from sleep mode */ /* Request initialisation */ /* Wait the acknowledge */ /* Check acknowledge */ /* Set the time triggered communication mode */ /* Set the automatic bus-off management */ /* Set the automatic wake-up mode */ /* Set the no automatic retransmission */ /* Set the receive FIFO locked mode */ /* Set the transmit FIFO priority */ /* Set the bit timing register */ /* Request leave initialisation */ /* Wait the acknowledge */ /* ...and check acknowledged */ /* At this step, return the status of initialization */