/**
  ********************************************************************************
  * @file    stm8l15x_usart.h
  * @author  MCD Application Team
  * @version V1.4.0
  * @date    09/24/2010
  * @brief   This file contains all the functions prototypes for the USART firmware
  *          library.
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM8L15x_USART_H
#define __STM8L15x_USART_H

/* Includes ------------------------------------------------------------------*/
#include "stm8l15x.h"

/** @addtogroup STM8L15x_StdPeriph_Driver
  * @{
  */

/* Exported variables ------------------------------------------------------- */
/* Exported types ------------------------------------------------------------*/

/** @addtogroup USART_Exported_Types
  * @{
  */

/**
  * @brief USART Flag possible values
  */
typedef enum { USART_FLAG_TXE   = (uint16_t)0x0080, /*!< Transmit Data Register Empty flag */
               USART_FLAG_TC    = (uint16_t)0x0040, /*!< Transmission Complete flag */
               USART_FLAG_RXNE  = (uint16_t)0x0020, /*!< Read Data Register Not Empty flag */
               USART_FLAG_IDLE  = (uint16_t)0x0010, /*!< Idle line detected flag */
               USART_FLAG_OR    = (uint16_t)0x0008, /*!< OverRun error flag */
               USART_FLAG_NF    = (uint16_t)0x0004, /*!< Noise error flag */
               USART_FLAG_FE    = (uint16_t)0x0002, /*!< Framing Error flag */
               USART_FLAG_PE    = (uint16_t)0x0001, /*!< Parity Error flag */
               USART_FLAG_SBK   = (uint16_t)0x0101  /*!< Send Break characters Flag */
             } USART_FLAG_TypeDef;

/**
  * @brief USART Interrupt definition
  * USART_IT possible values
  * Elements values convention: 0x0ZYX
  *   X: Position of the corresponding Interrupt
  *   Y: Flag position
  *   Z: Register index
  */
typedef enum { USART_IT_TXE        = (uint16_t)0x0277, /*!< Transmit interrupt */
               USART_IT_TC         = (uint16_t)0x0266, /*!< Transmission Complete interrupt */
               USART_IT_RXNE       = (uint16_t)0x0255, /*!< Receive interrupt */
               USART_IT_IDLE       = (uint16_t)0x0244, /*!< IDLE line interrupt */
               USART_IT_OR         = (uint16_t)0x0235, /*!< Overrun Error interrupt */
               USART_IT_PE         = (uint16_t)0x0100, /*!< Parity Error interrupt */
               USART_IT_ERR        = (uint16_t)0x0500, /*!< Error interrupt */
               USART_IT_NF         = (uint16_t)0x0102, /*!< Noise Error interrupt */
               USART_IT_FE         = (uint16_t)0x0101  /*!< Frame Error interrupt */
             } USART_IT_TypeDef;

/**
  * @brief USART WakeUP Modes
  */
typedef enum
{
  USART_WakeUp_IdleLine       = (uint8_t)0x00, /*!< 0x01 Idle Line wake up */
  USART_WakeUp_AddressMark    = (uint8_t)0x08  /*!< 0x02 Address Mark wake up */
} USART_WakeUp_TypeDef;


/**
  * @brief USART stop bits possible values
  */

typedef enum
{
  USART_StopBits_1   = (uint8_t)0x00, /*!< One stop bit is transmitted at the end of frame*/
  USART_StopBits_2   = (uint8_t)0x20, /*!< Two stop bits are transmitted at the end of frame*/
  USART_StopBits_1_5 = (uint8_t)0x30  /*!< One and half stop bits*/
} USART_StopBits_TypeDef;

/**
  * @brief USART parity possible values
  */
typedef enum
{
  USART_Parity_No    = (uint8_t)0x00,      /*!< No Parity*/
  USART_Parity_Even  = (uint8_t)0x04,      /*!< Even Parity*/
  USART_Parity_Odd   = (uint8_t)0x06       /*!< Odd Parity*/
} USART_Parity_TypeDef;

/**
  * @brief USART LIN Break detection length possible values
  */
typedef enum
{
  USART_LINBreakDetectionLength_10BITS = (uint8_t)0x00, /*!< 10 bits Lin Break detection */
  USART_LINBreakDetectionLength_11BITS = (uint8_t)0x01  /*!< 11 bits Lin Break detection */
} USART_LINBreakDetectionLength_TypeDef;

/**
  * @brief USART Word length possible values
  */
typedef enum
{
  USART_WordLength_8b = (uint8_t)0x00,  /*!< 8 bits Data */
  USART_WordLength_9b = (uint8_t)0x10   /*!< 9 bits Data */
} USART_WordLength_TypeDef;

/**
  * @brief USART Mode possible values
  */
typedef enum
{
  USART_Mode_Rx    = (uint8_t)0x04,  /*!< Receive Enable */
  USART_Mode_Tx    = (uint8_t)0x08   /*!< Transmit Enable */
} USART_Mode_TypeDef;

/**
  * @brief USART DMA Requests
  */
typedef enum
{
  USART_DMAReq_TX    = (uint8_t)0x80,  /*!< Receive DMA request Enable */
  USART_DMAReq_RX    = (uint8_t)0x40   /*!< Transmit DMA request Enable */
} USART_DMAReq_TypeDef;

/**
  * @brief USART Irda Modes
  */

typedef enum
{
  USART_IrDAMode_Normal    = (uint8_t)0x00, /*!< IrDA Normal Mode   */
  USART_IrDAMode_LowPower  = (uint8_t)0x01  /*!< IrDA Low Power Mode */
} USART_IrDAMode_TypeDef;

/**
  * @brief USART Clock possible values
  */
typedef enum
{
  USART_Clock_Disable    = (uint8_t)0x00,  /*!< CK pin disabled */
  USART_Clock_Enable     = (uint8_t)0x08   /*!< CK pin enabled */
} USART_Clock_TypeDef;

/**
  * @brief USART Clock Polarity possible values
  */
typedef enum
{
  USART_CPOL_Low      = (uint8_t)0x00,  /*!< CK to 0 when idle */
  USART_CPOL_High     = (uint8_t)0x04   /*!< CK to 1 when idle.*/
} USART_CPOL_TypeDef;

/**
  * @brief USART Clock Phase possible values
  */
typedef enum
{
  USART_CPHA_1Edge     = (uint8_t)0x00,  /*!< The first clock transition is the first data capture edge*/
  USART_CPHA_2Edge     = (uint8_t)0x02   /*!< The second clock transition is the first data capture edge*/
} USART_CPHA_TypeDef;

/**
  * @brief USART Last Bit possible values
  */
typedef enum
{
  USART_LastBit_Disable  = (uint8_t)0x00,  /*!< The clock pulse of the last data bit is not output to the SCLK pin.*/
  USART_LastBit_Enable   = (uint8_t)0x01   /*!< The clock pulse of the last data bit is output to the SCLK pin.*/
} USART_LastBit_TypeDef;
/**
  * @}
  */

/* Exported constants --------------------------------------------------------*/
/* Exported macros -----------------------------------------------------------*/
/* Private macros ------------------------------------------------------------*/

/** @addtogroup USART_Private_Macros
  * @{
  */

/**
  * @brief Macro used by the assert function to check the different functions parameters.
  */

/**
  * @brief Macro used by the assert_param function in order to check the different
  *   sensitivity values for the FLAGs
  */
#define IS_USART_FLAG(Flag) \
  (((Flag) == USART_FLAG_TXE) || \
   ((Flag) == USART_FLAG_TC)  || \
   ((Flag) == USART_FLAG_RXNE) || \
   ((Flag) == USART_FLAG_IDLE) || \
   ((Flag) == USART_FLAG_OR) || \
   ((Flag) == USART_FLAG_NF) || \
   ((Flag) == USART_FLAG_FE) || \
   ((Flag) == USART_FLAG_PE) || \
   ((Flag) == USART_FLAG_SBK))

/**
  * @brief Macro used by the assert_param function in order to check the different
  *   sensitivity values for the FLAGs that can be cleared by software writing 0
  */
#define IS_USART_CLEAR_FLAG(Flag) \
  (((Flag) == USART_FLAG_TC)  || \
   ((Flag) == USART_FLAG_RXNE))

/**
  * @brief Macro used by the assert_param function in order to check the different
  *  sensitivity values for the Interrupts
  */

#define IS_USART_CONFIG_IT(Interrupt) \
  (((Interrupt) == USART_IT_PE) || \
   ((Interrupt) == USART_IT_TXE) || \
   ((Interrupt) == USART_IT_TC) || \
   ((Interrupt) == USART_IT_RXNE) || \
   ((Interrupt) == USART_IT_OR) || \
   ((Interrupt) == USART_IT_ERR) || \
   ((Interrupt) == USART_IT_IDLE))

/**
  * @brief Macro used by the assert function in order to check the different
  *   sensitivity values for the pending bit
  */
#define IS_USART_GET_IT(ITPendingBit) \
  (((ITPendingBit) == USART_IT_TXE)  || \
   ((ITPendingBit) == USART_IT_TC)   || \
   ((ITPendingBit) == USART_IT_RXNE) || \
   ((ITPendingBit) == USART_IT_IDLE) || \
   ((ITPendingBit) == USART_IT_OR)  || \
   ((ITPendingBit) == USART_IT_PE))

/**
  * @brief Macro used by the assert function in order to check the different
  *  sensitivity values for the pending bit that can be cleared by writing 0
  */
#define IS_USART_CLEAR_IT(IT) (((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE))

/**
 * @brief Macro used by the assert function in order to check the different
 *  sensitivity values for the USART MODE
 */
#define IS_USART_MODE(MODE) ((((MODE) & (uint8_t)0xF3) == 0x00) && ((MODE) != (uint16_t)0x00))


/**
 * @brief Macro used by the assert function in order to check the different
 *  sensitivity values for the WordLength
 */
#define IS_USART_WORDLENGTH(WordLength) \
  (((WordLength) == USART_WordLength_8b) || \
   ((WordLength) == USART_WordLength_9b))

/**
 * @brief Macro used by the assert_param function in order to check the different
 *  sensitivity values for the IrDAMode
 */
#define IS_USART_IRDAMODE(IrDAMode) \
  (((IrDAMode) == USART_IrDAMode_LowPower) || \
   ((IrDAMode) == USART_IrDAMode_Normal))

/**
 * @brief Macro used by the assert function in order to check the different
 *  sensitivity values for the DMA request
 */
#define IS_USART_DMAREQ(DMAReq) ((((DMAReq) & (uint8_t)0x3F) == 0x00) && ((DMAReq) != (uint8_t)0x00))

/**
  * @brief Macro used by the assert_param function in order to check the different
  *   sensitivity values for the USART Clock
  */
#define IS_USART_CLOCK(CLOCK) (((CLOCK) == USART_Clock_Disable) ||((CLOCK) == USART_Clock_Enable))

/**
  * @brief Macro used by the assert_param function in order to check the different
  *  sensitivity values for the USART Clock Polarity
  */
#define IS_USART_CPOL(CPOL) (((CPOL) == USART_CPOL_Low) || ((CPOL) == USART_CPOL_High))

/**
  * @brief Macro used by the assert_param function in order to check the different
  *   sensitivity values for the USART Clock Phase
  */
#define IS_USART_CPHA(CPHA) (((CPHA) == USART_CPHA_1Edge) || ((CPHA) == USART_CPHA_2Edge))

/**
  * @brief Macro used by the assert_param function in order to check the different
  *   sensitivity values  for the USART Last Bit
  */
#define IS_USART_LASTBIT(LASTBIT) (((LASTBIT) == USART_LastBit_Disable) || \
                                   ((LASTBIT) == USART_LastBit_Enable))

/**
  * @brief Macro used by the assert function in order to check the different
  *   sensitivity values for the WakeUp
  */
#define IS_USART_WAKEUP(WakeUpMode)(((WakeUpMode) == USART_WakeUp_IdleLine) || \
                                    ((WakeUpMode) == USART_WakeUp_AddressMark))


/**
  * @brief Macro used by the assert function in order to check the different
  *  sensitivity values for the USART_StopBit
  */
#define IS_USART_STOPBITS(StopBit)(((StopBit) == USART_StopBits_1) || \
                                   ((StopBit) == USART_StopBits_1_5) || \
                                   ((StopBit) == USART_StopBits_2))

/**
 * @brief Macro used by the assert function in order to check the different
 *  sensitivity values for the Parity
 */
#define IS_USART_PARITY(Parity)(((Parity) == USART_Parity_No)   || \
                                ((Parity) == USART_Parity_Even) || \
                                ((Parity) == USART_Parity_Odd ))

/* BaudRate value should be < 625000 bps */
#define IS_USART_BAUDRATE(NUM) ((NUM) <= (uint32_t)625000)

/**
 * @brief Macro used by the assert function in order to check the different
 *  sensitivity values for the node address
 */
#define USART_ADDRESS_MAX ((uint8_t)16)
#define IS_USART_ADDRESS(address) ((address) < USART_ADDRESS_MAX)

/**
 * @brief Macro used by the assert function in order to check the different
 *  sensitivity values  for the data
 */
#define USART_DATA_9BITS_MAX ((uint16_t)0x1FF)
#define IS_USART_DATA_9BITS(DATA) ((DATA) < USART_DATA_9BITS_MAX)

/**
  * @}
  */

/* Exported functions ------------------------------------------------------- */
/** @addtogroup USART_Exported_functions
  * @{
  */

void USART_DeInit(USART_TypeDef* USARTx);
void USART_Init(USART_TypeDef* USARTx, uint32_t BaudRate, USART_WordLength_TypeDef
                USART_WordLength, USART_StopBits_TypeDef USART_StopBits,
                USART_Parity_TypeDef USART_Parity, USART_Mode_TypeDef USART_Mode);
void USART_ClockInit(USART_TypeDef* USARTx, USART_Clock_TypeDef USART_Clock,
                     USART_CPOL_TypeDef USART_CPOL, USART_CPHA_TypeDef USART_CPHA,
                     USART_LastBit_TypeDef USART_LastBit);
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState);

void USART_ITConfig(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT,
                    FunctionalState NewState);
void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState);
void USART_IrDAConfig(USART_TypeDef* USARTx, USART_IrDAMode_TypeDef USART_IrDAMode);
void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState);
void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState);
void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState);
void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime);
void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler);
uint8_t USART_ReceiveData8(USART_TypeDef* USARTx);
uint16_t USART_ReceiveData9(USART_TypeDef* USARTx);
void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState);
void USART_SendBreak(USART_TypeDef* USARTx);
void USART_SendData8(USART_TypeDef* USARTx, uint8_t Data);
void USART_SendData9(USART_TypeDef* USARTx, uint16_t Data);
void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address);
void USART_WakeUpConfig(USART_TypeDef* USARTx, USART_WakeUp_TypeDef USART_WakeUp);
void USART_DMACmd(USART_TypeDef* USARTx, USART_DMAReq_TypeDef USART_DMAReq,
                  FunctionalState NewState);

FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, USART_FLAG_TypeDef USART_FLAG);
void USART_ClearFlag(USART_TypeDef* USARTx, USART_FLAG_TypeDef USART_FLAG);
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT);
void USART_ClearITPendingBit(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT);
/**
  * @}
  */

#endif /* __STM8L15x_USART_H */

/**
  * @}
  */

/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
