#include "USART.h"
#define CR1_UE_Set ((uint16_t)0x2000) /*!< USART Enable Mask */
#define CR1_UE_Reset ((uint16_t)0xDFFF) /*!< USART Disable Mask */
#define CR1_WAKE_Mask ((uint16_t)0xF7FF) /*!< USART WakeUp Method Mask */
#define CR1_RWU_Set ((uint16_t)0x0002) /*!< USART mute mode Enable Mask */
#define CR1_RWU_Reset ((uint16_t)0xFFFD) /*!< USART mute mode Enable Mask */
#define CR1_SBK_Set ((uint16_t)0x0001) /*!< USART Break Character send Mask */
#define CR1_CLEAR_Mask ((uint16_t)0xE9F3) /*!< USART CR1 Mask */
#define CR2_Address_Mask ((uint16_t)0xFFF0) /*!< USART address Mask */
#define CR2_LINEN_Set ((uint16_t)0x4000) /*!< USART LIN Enable Mask */
#define CR2_LINEN_Reset ((uint16_t)0xBFFF) /*!< USART LIN Disable Mask */
#define CR2_LBDL_Mask ((uint16_t)0xFFDF) /*!< USART LIN Break detection Mask */
#define CR2_STOP_CLEAR_Mask ((uint16_t)0xCFFF) /*!< USART CR2 STOP Bits Mask */
#define CR2_CLOCK_CLEAR_Mask ((uint16_t)0xF0FF) /*!< USART CR2 Clock Mask */
#define CR3_SCEN_Set ((uint16_t)0x0020) /*!< USART SC Enable Mask */
#define CR3_SCEN_Reset ((uint16_t)0xFFDF) /*!< USART SC Disable Mask */
#define CR3_NACK_Set ((uint16_t)0x0010) /*!< USART SC NACK Enable Mask */
#define CR3_NACK_Reset ((uint16_t)0xFFEF) /*!< USART SC NACK Disable Mask */
#define CR3_HDSEL_Set ((uint16_t)0x0008) /*!< USART Half-Duplex Enable Mask */
#define CR3_HDSEL_Reset ((uint16_t)0xFFF7) /*!< USART Half-Duplex Disable Mask */
#define CR3_IRLP_Mask ((uint16_t)0xFFFB) /*!< USART IrDA LowPower mode Mask */
#define CR3_CLEAR_Mask ((uint16_t)0xFCFF) /*!< USART CR3 Mask */
#define CR3_IREN_Set ((uint16_t)0x0002) /*!< USART IrDA Enable Mask */
#define CR3_IREN_Reset ((uint16_t)0xFFFD) /*!< USART IrDA Disable Mask */
#define GTPR_LSB_Mask ((uint16_t)0x00FF) /*!< Guard Time Register LSB Mask */
#define GTPR_MSB_Mask ((uint16_t)0xFF00) /*!< Guard Time Register MSB Mask */
#define IT_Mask ((uint16_t)0x001F) /*!< USART Interrupt Mask */
/* USART OverSampling-8 Mask */
#define CR1_OVER8_Set ((u16)0x8000) /* USART OVER8 mode Enable Mask */
#define CR1_OVER8_Reset ((u16)0x7FFF) /* USART OVER8 mode Disable Mask */
/* USART One Bit Sampling Mask */
#define CR3_ONEBITE_Set ((u16)0x0800) /* USART ONEBITE mode Enable Mask */
#define CR3_ONEBITE_Reset ((u16)0xF7FF) /* USART ONEBITE mode Disable Mask */
/**
* @brief Initializes the USARTx peripheral according to the specified
* parameters in the USART_InitStruct .
* @param error: An instance of ErrorStruct which contains the error information,
* @retval None
*/
bool System::USARTConfig::U_IsInitialized()
{
//if both instances are initialized then return false
return this->U_isInitialized && this->IsInitialized() ? true : false;
}
/**
* @brief Initializes the USARTx peripheral according to the specified
* parameters in the USART_InitStruct .
* @param error: An instance of ErrorStruct which contains the error information,
* @retval None
*/
void System::USART::ErrorHandler(SysError::ErrorStruct error)
{
globalHal->ErrorHandle(&error);
}
/**
* @brief Resets the registers of USART
* @param none
* @retval none
*/
void System::USART::ModuleResetMode(HAL_StateTypeDef newState)
{
if(newState == HAL_Enable)
this->DeInit();
else
{
//checks to make sure that the USARTConfig is not null and initaliazed
if(this->initializedUsartConfig != 0 && this->initializedUsartConfig->U_IsInitialized())
{
this->Init(this->initializedUsartConfig);
}
}
}
/**
* @brief Resets the registers of USART and initalizes the USART Device
* @param none
* @retval none
*/
void System::USART::RestartModule(void)
{
if(this->IsInitialized)
{
this->DeInit();
//checks to make sure that the USARTConfig is not null and initaliazed
if(this->initializedUsartConfig != 0 && this->initializedUsartConfig->U_IsInitialized())
{
//checks to that no error was returned
if(this->Init(this->initializedUsartConfig) != HAL_ERROR)
this->IsInitialized = true;
else
this->IsInitialized = false;
}
else /* !< this->initializedUsartConfig is null or its not initialized*/
this->IsInitialized = false;
}
}
/**
* @brief Initializes the USARTx peripheral according to the specified
* parameters in the USART_InitStruct .
* @param config: an instance of USARTConfig that has been inialized
* @config->USARTx: where x can be 1, 2, 3 to select the USART peripheral.
* @param USART_InitStruct: pointer to a USART_InitTypeDef structure
* that contains the configuration information for the specified USART peripheral.
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef System::USART::Init(System::USARTConfig *config)
{
uint32_t tempreg = 0x00, apbClock = 0x00;
uint32_t integerdivider = 0x00;
uint32_t fractionaldivider = 0x00;
uint32_t usartxbase = 0;
RCC_ClocksTypeDef RCC_ClocksStatus;
System::ResetClockControl *rcc = new System::ResetClockControl;
usartxbase = (uint32_t)config->USARTx;
/*---------------------------- USART CR2 Configuration -----------------------*/
tempreg = config->USARTx->CR1;
/* Clear M, PCE, PS, TE and RE bits */
tempreg &= CR1_CLEAR_Mask;
/* Configure the USART Word Length, Parity and mode ----------------------- */
/* Set the M bits according to USART_WordLength value */
/* Set PCE and PS bits according to USART_Parity value */
/* Set TE and RE bits according to USART_Mode value */
tempreg |= (uint32_t)config->WordLength | config->Parity |
config->Mode;
/* Write to USART CR1 */
config->USARTx->CR1 = (uint16_t)tempreg;
/*---------------------------- USART CR3 Configuration -----------------------*/
tempreg = config->USARTx->CR3;
/* Clear CTSE and RTSE bits */
tempreg &= CR3_CLEAR_Mask;
/* Configure the USART HFC -------------------------------------------------*/
/* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
tempreg |= config->HardwareFlowControl;
/* Write to USART CR3 */
config->USARTx->CR3 = (uint16_t)tempreg;
/*---------------------------- USART BRR Configuration -----------------------*/
/* Configure the USART Baud Rate -------------------------------------------*/
rcc->GetClocksFreq(&RCC_ClocksStatus);
if(usartxbase == USART1_BASE)
apbClock = RCC_ClocksStatus.PCLK2_Frequency;
else
apbClock = RCC_ClocksStatus.PCLK1_Frequency;
/* Determine the integer part */
if((config->USARTx->CR1 & CR1_OVER8_Set) != 0)
/* Integer part computing in case Oversampling mode is 8 Samples */
integerdivider = ((25 * apbClock) / (2 * (config->BuadRate)));
else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
/* Integer part computing in case Oversampling mode is 16 Samples */
integerdivider = ((25 * apbClock) / (4 * (config->BuadRate)));
tempreg = (integerdivider / 100) << 4;
/* Determine the fractional part */
fractionaldivider = integerdivider - (100 * (tempreg >> 4));
/* Implement the fractional part in the register */
if((config->USARTx->CR1 & CR1_OVER8_Set) != 0)
tempreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
tempreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
/* Write to USART BRR */
config->USARTx->BRR = (uint16_t)tempreg;
this->IsInitialized = true;
//set initialize instance
this->initializedUsartConfig = config;
return HAL_OK;
}
/**
* @brief Deinitializes the USARTx peripheral registers to their default reset values.
* @param config : an instance of USARTConfig which is valid and populated
* USARTx: This parameter can be one of the following values: USART1, USART2, USART3, UART4 or UART5.
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef System::USART::DeInit(System::USARTConfig *config)
{
System::ResetClockControl *rcc = new System::ResetClockControl;
/*Enable then disable periph*/
if(config->USARTx == USART1)
{
rcc->APB2PeriphClock(System::APB2Peripheral::Usart1, HAL_Enable);
rcc->APB2PeriphClock(System::APB2Peripheral::Usart1, HAL_Disable);
}
else if(config->USARTx == USART2)
{
rcc->APB1PeriphClock(System::APB1Peripheral::Usart2, HAL_Enable);
rcc->APB1PeriphClock(System::APB1Peripheral::Usart2, HAL_Disable);
}
else if(config->USARTx == USART3)
{
rcc->APB1PeriphClock(System::APB1Peripheral::Usart3, HAL_Enable);
rcc->APB1PeriphClock(System::APB1Peripheral::Usart3, HAL_Disable);
}
else if(config->USARTx == UART4)
{
rcc->APB1PeriphClock(System::APB1Peripheral::Uart4, HAL_Enable);
rcc->APB1PeriphClock(System::APB1Peripheral::Uart4, HAL_Enable);
}
else if(config->USARTx == UART5)
{
rcc->APB1PeriphClock(System::APB1Peripheral::Uart5, HAL_Enable);
rcc->APB1PeriphClock(System::APB1Peripheral::Uart5, HAL_Enable);
}
else{
const char errorMessage[100] = "Peripharel passed want NOT valid";
/*Error Struct*/
SysError::ErrorStruct error;
error.isCalledBack = false; /*<!-- Errored is not called back */
error.errorSeverity = SysError::NoActionRequired;
error.errorType = SysError::USART_InvalidPeriph;
/*Set the error message char bits*/
for(int i = 0; i <= 100; i++)
{
error.errorMessage[i] = errorMessage[i];
}
//log error
this->ErrorHandler(error);
return HAL_ERROR;
}
//instance is no longer initalized
this->IsInitialized = false;
return HAL_OK;
}
/**
* @brief see @ref System::USART::DeInit(System::USARTConfig *config)
* @param none
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef System::USART::DeInit()
{
//checks if initializedUsartConfig is points nothing
return this->initializedUsartConfig ? this->DeInit(this->initializedUsartConfig) : HAL_ERROR;
}
/**
* @brief see @ref System::USART::ClockInit(System::USARTConfig *config)
* @param none
* @retval None
*/
HAL_StatusTypeDef System::USART::ClockInit()
{
return this->initializedUsartConfig ? this->ClockInit(this->initializedUsartConfig) : HAL_ERROR;
}
/**
* @brief Initializes the USARTx peripheral Clock according to the
* specified parameters in the USARTClockConfig.
* @param config: an instance of USARTConfig that has been inialized
* @config->USARTx: where x can be 1, 2, 3 to select the USART peripheral.
* @note The Smart Card mode is not available for UART4 and UART5.
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef System::USART::ClockInit(System::USARTConfig *config)
{
uint32_t tempreg = 0x00;
/*---------------------------- USART CR2 Configuration -----------------------*/
tempreg = config->USARTx->CR2;
/* Clear CLKEN, CPOL, CPHA and LBCL bits */
tempreg &= CR2_CLOCK_CLEAR_Mask;
/* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
/* Set CLKEN bit according to USART_Clock value */
/* Set CPOL bit according to USART_CPOL value */
/* Set CPHA bit according to USART_CPHA value */
/* Set LBCL bit according to USART_LastBit value */
tempreg |= (uint32_t)config->ClockConfig->Clock | config->ClockConfig->CPOL |
config->ClockConfig->CPHA | config->ClockConfig->LastBit;
/*Write to USART CR2*/
config->USARTx->CR2 = (uint16_t)tempreg;
return HAL_OK;
}
/**
* @brief see @ref System::USART::UsartCmd(System::USARTConfig *config)
* @param none
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef System::USART::UsartCmd(HAL_StateTypeDef newState)
{
return this->initializedUsartConfig ? this->UsartCmd(this->initializedUsartConfig, newState) : HAL_ERROR;
}
/**
* @brief Enables or disables the specified USART peripheral.
* @param config: an instance of USARTConfig that has been inialized
* @config->USARTxThis parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param NewState: new state of the USARTx peripheral.
* This parameter can be: HAL_Enable or HAL_Disable.
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef System::USART::UsartCmd(System::USARTConfig *config, HAL_StateTypeDef newState)
{
if(newState == HAL_Enable)
{
/* Enable the selected USART by setting the UE bit in the CR1 register */
config->USARTx->CR1 |= CR1_UE_Set;
}
else
{
/* Disable the selected USART by clearing the UE bit in the CR1 register */
config->USARTx->CR1 &= CR1_UE_Reset;
}
return HAL_OK;
}
/**
* @brief Enables or disables the specified USART interrupts.
* @param config: an instance of USARTConfig that has been inialized
* @config->USARTxThis parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
* This parameter can be one of the following values:
* @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
* @arg USART_IT_LBD: LIN Break detection interrupt
* @arg USART_IT_TXE: Tansmit Data Register empty interrupt
* @arg USART_IT_TC: Transmission complete interrupt
* @arg USART_IT_RXNE: Receive Data register not empty interrupt
* @arg USART_IT_IDLE: Idle line detection interrupt
* @arg USART_IT_PE: Parity Error interrupt
* @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
* @param NewState: new state of the specified USARTx interrupts.
* This parameter can be: HAL_Enable or HAL_Disable.
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef System::USART::InteruptConfig(System::USARTConfig *config, uint16_t USART_IT, HAL_StateTypeDef newState)
{
uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
uint32_t usartxbase = 0x00;
usartxbase = (uint32_t)config->USARTx;
/* Get the USART register index */
usartreg = (((uint8_t)USART_IT) >> 0x05);
/* Get the interrupt position */
itpos = USART_IT & IT_Mask;
itmask = (((uint32_t)0x01) << itpos);
if (usartreg == 0x01) /* The IT is in CR1 register */
{
usartxbase += 0x0C;
}
else if (usartreg == 0x02) /* The IT is in CR2 register */
{
usartxbase += 0x10;
}
else /* The IT is in CR3 register */
{
usartxbase += 0x14;
}
if (newState != HAL_Disable)
{
*(__IO uint32_t*)usartxbase |= itmask;
}
else
{
*(__IO uint32_t*)usartxbase &= ~itmask;
}
return HAL_OK;
}
/**
* @brief Enables or disables the USART’s DMA interface.
* @param config: an instance of USARTConfig that has been inialized
* @config->USARTxThis parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param DMAReq: specifies the DMA request.
* This parameter can be any combination of the following values:
* @arg USART_DMAReq_Tx: USART DMA transmit request
* @arg USART_DMAReq_Rx: USART DMA receive request
* @param NewState: new state of the DMA Request sources.
* This parameter can be: HAL_Enable or HAL_Disable.
* @note The DMA mode is not available for UART5.
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef System::USART::DMACmd(System::USARTConfig *config, uint16_t DMAReq, HAL_StateTypeDef newState)
{
if(newState != HAL_Disable)
/* Enable the DMA transfer for selected requests by setting the DMAT and/or
DMAR bits in the USART CR3 register */
config->USARTx->CR3 |= DMAReq;
else
/* Disable the DMA transfer for selected requests by clearing the DMAT and/or
DMAR bits in the USART CR3 register */
config->USARTx->CR3 &= (uint16_t)~DMAReq;
return HAL_OK;
}
/**
* @brief Sets the address of the USART node.
* @param config: an instance of USARTConfig that has been inialized
* @config->USARTxThis parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param Address: Indicates the address of the USART node.
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef System::USART::SetAddress(System::USARTConfig *config, uint8_t Address)
{
/* Clear the USART address */
config->USARTx->CR2 &= CR2_Address_Mask;
/* Set the USART address node */
config->USARTx->CR2 |= Address;
return HAL_OK;
}
/**
* @brief Selects the USART WakeUp method.
* @param config: an instance of USARTConfig that has been inialized
* @config->USARTxThis parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param WakeUp: specifies the USART wakeup method.
* This parameter can be one of the following values:
* @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
* @arg USART_WakeUp_AddressMark: WakeUp by an address mark
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef System::USART::WakeUpConfig(System::USARTConfig *config, uint16_t WakeUp)
{
config->USARTx->CR1 &= CR1_WAKE_Mask;
config->USARTx->CR1 |= WakeUp;
return HAL_OK;
}
/**
* @brief see @ref System::USART::ReceiverWakeUpCmd(System::USARTConfig *config)
* @param none
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef System::USART::ReceiverWakeUpCmd(HAL_StateTypeDef newState)
{
return this->initializedUsartConfig ? this->ReceiverWakeUpCmd(this->initializedUsartConfig, newState) : HAL_ERROR;
}
/**
* @brief Determines if the USART is in mute mode or not.
* @param config: an instance of USARTConfig that has been inialized
* @config->USARTxThis parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param newState: new state of the USART mute mode.
* This parameter can be: HAL_Enable or HAL_Disable.
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef System::USART::ReceiverWakeUpCmd(System::USARTConfig *config, HAL_StateTypeDef newState)
{
if(newState != HAL_Disable)
/* Enable the USART mute mode by setting the RWU bit in the CR1 register */
config->USARTx->CR1 |= CR1_RWU_Set;
else
config->USARTx->CR1 &= CR1_RWU_Reset;
return HAL_OK;
}
/**
* @brief Sets the USART LIN Break detection length.
* @param config: an instance of USARTConfig that has been inialized
* @config->USARTxThis parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param LINBreakDetectLength: specifies the LIN break detection length.
* This parameter can be one of the following values:
* @arg USART_LINBreakDetectLength_10b: 10-bit break detection
* @arg USART_LINBreakDetectLength_11b: 11-bit break detection
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef System::USART::LINBreakDetectLengthConfig(System::USARTConfig *config, uint16_t LINBreakDetectLength)
{
config->USARTx->CR2 &= CR2_LBDL_Mask;
config->USARTx->CR2 |= LINBreakDetectLength;
return HAL_OK;
}
/**
* @brief Enables or disables the USART’s LIN mode.
* @param config: an instance of USARTConfig that has been inialized
* @config->USARTxThis parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param newState: new state of the USART LIN mode.
* This parameter can be: HAL_Enable or HAl_Disable.
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef System::USART::LINCmd(System::USARTConfig *config, HAL_StateTypeDef newState)
{
if(newState != HAL_Disable)
/* Enable the LIN mode by setting the LINEN bit in the CR2 register */
config->USARTx->CR2 |= CR2_LINEN_Set;
else
/* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
config->USARTx->CR2 &= CR2_LINEN_Reset;
return HAL_OK;
}
HAL_StatusTypeDef System::USART::SendData(uint16_t Data)
{
return this->initializedUsartConfig ? this->SendData(this->initializedUsartConfig, (uint16_t )Data) : HAL_ERROR;
}
HAL_StatusTypeDef System::USART::SendData(System::USARTConfig *config, uint16_t Data)
{
config->USARTx->DR = (Data & (uint16_t)0x01FF);
return HAL_OK;
}
/**
* @brief see @ref System::USART::ReceiveData(System::USARTConfig *config)
* @param none
* @retval HAL_StatusTypeDef: Indicates the success of the operation.
* @ref HAL_StatusTypeDef
*/
uint16_t System::USART::ReceiveData()
{
return this->initializedUsartConfig ? this->ReceiveData(this->initializedUsartConfig) : 0;
}
/**
* @brief Returns the most recent received data by the USARTx peripheral.
* @param config: an instance of USARTConfig that has been inialized
* @config->USARTxThis parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @retval The received data.
*/
uint16_t System::USART::ReceiveData(System::USARTConfig *config)
{
/* Receive Data */
return (uint16_t)(config->USARTx->DR & (uint16_t)0x01FF);
}
/**
* @brief Checks whether the specified USART flag is set or not.
* @param config: an instance of USARTConfig that has been inialized
* @config->USARTxThis parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param FLAG: specifies the flag to check.
* This parameter can be one of the following values:
* @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5)
* @arg USART_FLAG_LBD: LIN Break detection flag
* @arg USART_FLAG_TXE: Transmit data register empty flag
* @arg USART_FLAG_TC: Transmission Complete flag
* @arg USART_FLAG_RXNE: Receive data register not empty flag
* @arg USART_FLAG_IDLE: Idle Line detection flag
* @arg USART_FLAG_ORE: OverRun Error flag
* @arg USART_FLAG_NE: Noise Error flag
* @arg USART_FLAG_FE: Framing Error flag
* @arg USART_FLAG_PE: Parity Error flag
* @retval The new state of USART_FLAG (SET or RESET).
*/
FlagStatus System::USART::GetFlagStatus(System::USARTConfig *config, uint16_t FLAG)
{
FlagStatus bitstatus = RESET;
if ((config->USARTx->SR & FLAG) != (uint16_t)RESET)
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
return bitstatus;
}
/**
* @brief Clears the USARTx's pending flags.
* @param config: an instance of USARTConfig that has been inialized
* @config->USARTxThis parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param FLAG: specifies the flag to clear.
* This parameter can be any combination of the following values:
* @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5).
* @arg USART_FLAG_LBD: LIN Break detection flag.
* @arg USART_FLAG_TC: Transmission Complete flag.
* @arg USART_FLAG_RXNE: Receive data register not empty flag.
*
* @note
* - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
* error) and IDLE (Idle line detected) flags are cleared by software
* sequence: a read operation to USART_SR register (USART_GetFlagStatus())
* followed by a read operation to USART_DR register (USART_ReceiveData()).
* - RXNE flag can be also cleared by a read to the USART_DR register
* (USART_ReceiveData()).
* - TC flag can be also cleared by software sequence: a read operation to
* USART_SR register (USART_GetFlagStatus()) followed by a write operation
* to USART_DR register (USART_SendData()).
* - TXE flag is cleared only by a write to the USART_DR register
* (USART_SendData()).
* @retval None
*/
void System::USART::ClearFlag(System::USARTConfig *config, uint16_t FLAG)
{
config->USARTx->SR = (uint16_t)~FLAG;
}