Examples/Crusher/Linux/stm32/firmware/lib/src/peripherals/stm32f4xx_can.c
/**
  * @brief  Selects the CAN Operation mode.
  * @param  CAN_OperatingMode: CAN Operating Mode.
  *         This parameter can be one of @ref CAN_OperatingMode_TypeDef enumeration.
  * @retval status of the requested mode which can be 
  *         - CAN_ModeStatus_Failed:  CAN failed entering the specific mode 
  *         - CAN_ModeStatus_Success: CAN Succeed entering the specific mode 
  */
@brief  Selects the CAN Operation mode.
@param  CAN_OperatingMode: CAN Operating Mode.
This parameter can be one of @ref CAN_OperatingMode_TypeDef enumeration.
@retval status of the requested mode which can be
- CAN_ModeStatus_Failed:  CAN failed entering the specific mode
- CAN_ModeStatus_Success: CAN Succeed entering the specific mode
@brief Selects the CAN Operation mode . @param CAN_OperatingMode : CAN Operating Mode . This parameter can be one of @ref CAN_OperatingMode_TypeDef enumeration . @retval status of the requested mode which can be - CAN_ModeStatus_Failed : CAN failed entering the specific mode - CAN_ModeStatus_Success : CAN Succeed entering the specific mode
uint8_t CAN_OperatingModeRequest(CAN_TypeDef* CANx, uint8_t CAN_OperatingMode)
{
  uint8_t status = CAN_ModeStatus_Failed;
  uint32_t timeout = INAK_TIMEOUT; 
  assert_param(IS_CAN_ALL_PERIPH(CANx));
  assert_param(IS_CAN_OPERATING_MODE(CAN_OperatingMode));
  if (CAN_OperatingMode == CAN_OperatingMode_Initialization)
  {
    CANx->MCR = (uint32_t)((CANx->MCR & (uint32_t)(~(uint32_t)CAN_MCR_SLEEP)) | CAN_MCR_INRQ);
    while (((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_INAK) && (timeout != 0))
    {
      timeout--;
    }
    if ((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_INAK)
    {
      status = CAN_ModeStatus_Failed;
    }
    else
    {
      status = CAN_ModeStatus_Success;
    }
  }
  else  if (CAN_OperatingMode == CAN_OperatingMode_Normal)
  {
    CANx->MCR &= (uint32_t)(~(CAN_MCR_SLEEP|CAN_MCR_INRQ));
    while (((CANx->MSR & CAN_MODE_MASK) != 0) && (timeout!=0))
    {
      timeout--;
    }
    if ((CANx->MSR & CAN_MODE_MASK) != 0)
    {
      status = CAN_ModeStatus_Failed;
    }
    else
    {
      status = CAN_ModeStatus_Success;
    }
  }
  else  if (CAN_OperatingMode == CAN_OperatingMode_Sleep)
  {
    CANx->MCR = (uint32_t)((CANx->MCR & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
    while (((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK) && (timeout!=0))
    {
      timeout--;
    }
    if ((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK)
    {
      status = CAN_ModeStatus_Failed;
    }
    else
    {
      status = CAN_ModeStatus_Success;
    }
  }
  else
  {
    status = CAN_ModeStatus_Failed;
  }
  return  (uint8_t) status;
}
uint8_t CAN_OperatingModeRequest ( CAN_TypeDef * CANx , uint8_t CAN_OperatingMode ) { uint8_t status = CAN_ModeStatus_Failed ; uint32_t timeout = INAK_TIMEOUT ; assert_param ( IS_CAN_ALL_PERIPH ( CANx ) ) ; assert_param ( IS_CAN_OPERATING_MODE ( CAN_OperatingMode ) ) ; if ( CAN_OperatingMode == CAN_OperatingMode_Initialization ) { CANx -> MCR = ( uint32_t ) ( ( CANx -> MCR & ( uint32_t ) ( ~ ( uint32_t ) CAN_MCR_SLEEP ) ) | CAN_MCR_INRQ ) ; while ( ( ( CANx -> MSR & CAN_MODE_MASK ) != CAN_MSR_INAK ) && ( timeout != 0 ) ) { timeout -- ; } if ( ( CANx -> MSR & CAN_MODE_MASK ) != CAN_MSR_INAK ) { status = CAN_ModeStatus_Failed ; } else { status = CAN_ModeStatus_Success ; } } else if ( CAN_OperatingMode == CAN_OperatingMode_Normal ) { CANx -> MCR &= ( uint32_t ) ( ~ ( CAN_MCR_SLEEP | CAN_MCR_INRQ ) ) ; while ( ( ( CANx -> MSR & CAN_MODE_MASK ) != 0 ) && ( timeout != 0 ) ) { timeout -- ; } if ( ( CANx -> MSR & CAN_MODE_MASK ) != 0 ) { status = CAN_ModeStatus_Failed ; } else { status = CAN_ModeStatus_Success ; } } else if ( CAN_OperatingMode == CAN_OperatingMode_Sleep ) { CANx -> MCR = ( uint32_t ) ( ( CANx -> MCR & ( uint32_t ) ( ~ ( uint32_t ) CAN_MCR_INRQ ) ) | CAN_MCR_SLEEP ) ; while ( ( ( CANx -> MSR & CAN_MODE_MASK ) != CAN_MSR_SLAK ) && ( timeout != 0 ) ) { timeout -- ; } if ( ( CANx -> MSR & CAN_MODE_MASK ) != CAN_MSR_SLAK ) { status = CAN_ModeStatus_Failed ; } else { status = CAN_ModeStatus_Success ; } } else { status = CAN_ModeStatus_Failed ; } return ( uint8_t ) status ; }
@brief  Selects the CAN Operation mode.
@brief Selects the CAN Operation mode .
/* Timeout for INAK or also for SLAK bits*/ /* Check the parameters */ /* Request initialisation */ /* Wait the acknowledge */ /* Request leave initialisation and sleep mode  and enter Normal mode */ /* Wait the acknowledge */ /* Request Sleep mode */ /* Wait the acknowledge */