/*!
 * @file        bmp561_uart.h
 *
 * @brief       This file contains all the functions prototypes for the UART firmware library
 *
 * @version     V1.0.0
 *
 * @date        2025-01-01
 *
 * @attention
 *
 *  Copyright (C) 2025 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Define to prevent recursive inclusion */
#ifndef __BMP561_UART_H
#define __BMP561_UART_H

#ifdef __cplusplus
  extern "C" {
#endif

#include "bmp561.h"

/** @addtogroup Peripherals_Library Standard Peripheral Library
  @{
*/

/** @addtogroup UART_Driver UART Driver
  @{
*/

/** @addtogroup UART_Enumerations Enumerations
  @{
*/

/**
 * @brief   UART stop bit
 */
typedef enum
{
    UART_STOPBIT_1B = 0x00,    /*!< 1 bit stop bit */
    UART_STOPBIT_2B = 0x01     /*!< 2 bit stop bit */
} UART_STOPBIT_T;

/**
 * @brief   UART parity
 */
typedef enum
{
    UART_PARITY_EVEN = 0x00,    /*!< Even verification */
    UART_PARITY_ODD  = 0x01     /*!< Odd verification */
} UART_PARITY_T;

/**
 * @brief   UART transmit sequence
 */
typedef enum
{
    UART_TXSEQ_LSB = 0x00,    /*!< LSB order */
    UART_TXSEQ_MSB = 0x01     /*!< MSB order */
} UART_TXSEQ_T;

/**
 * @brief   UART interrupt source
 */
typedef enum
{
    UART_INT_TX        = 0x0001,    /*!< Transmit interrupt */
    UART_INT_RX        = 0x0002,    /*!< Receive interrupt */
    UART_INT_TXOE      = 0x0004,    /*!< Transmit overflow error interrupt */
    UART_INT_RXOE      = 0x0008,    /*!< Receive overflow error interrupt */
    UART_INT_PE        = 0x0010,    /*!< Parity error interrupt */
    UART_INT_FE        = 0x0020,    /*!< Frame error interrupt */
    UART_INT_TXBUFFU   = 0x0040,    /*!< Transmit buffer full interrupt */
    UART_INT_RXBUFFU   = 0x0080,    /*!< Receive buffer full interrupt */
    UART_INT_TXBUFEM   = 0x0100,    /*!< Transmit buffer empty interrupt */
    UART_INT_RXBUFEM   = 0x0200,    /*!< Receive buffer empty interrupt */
    UART_INT_TXBUFHAFU = 0x0400,    /*!< Transmit buffer half full interrupt */
    UART_INT_RXBUFHAFU = 0x0800,    /*!< Receive buffer half full interrupt */
    UART_INT_ABC       = 0x1000,    /*!< Auto baud rate completed interrupt */
    UART_INT_ABE       = 0x2000     /*!< Auto baud rate error interrupt */
} UART_INT_T;

/**
 * @brief   UART flag
 */
typedef enum
{
    UART_FLAG_TXC       = 0x0001,    /*!< Transmit completed flag */
    UART_FLAG_RXC       = 0x0002,    /*!< Receive completed flag */
    UART_FLAG_TXOE      = 0x0004,    /*!< Transmit overflow error flag */
    UART_FLAG_RXOE      = 0x0008,    /*!< Receive overflow error flag */
    UART_FLAG_PE        = 0x0010,    /*!< Parity error flag */
    UART_FLAG_FE        = 0x0020,    /*!< Frame error flag */
    UART_FLAG_TXBUFFU   = 0x0040,    /*!< Transmit buffer full flag */
    UART_FLAG_RXBUFFU   = 0x0080,    /*!< Receive buffer full flag */
    UART_FLAG_TXBUFEM   = 0x0100,    /*!< Transmit buffer empty flag */
    UART_FLAG_RXBUFEM   = 0x0200,    /*!< Receive buffer empty flag */
    UART_FLAG_TXBUFHAFU = 0x0400,    /*!< Transmit buffer half full flag */
    UART_FLAG_RXBUFHAFU = 0x0800,    /*!< Receive buffer half full flag */
    UART_FLAG_TXBSY     = 0x1000,    /*!< Transmit busy */
    UART_FLAG_RXBSY     = 0x2000,    /*!< Receive busy */
    UART_FLAG_ABC       = 0x4000,    /*!< Auto baud rate completed flag */
    UART_FLAG_ABE       = 0x8000     /*!< Auto baud rate error flag */
} UART_FLAG_T;

/**
 * @brief   UART clear flag
 */
typedef enum
{
    UART_CLEAR_TXC  = 0x01,    /*!< Transmit completed flag */
    UART_CLEAR_RXC  = 0x02,    /*!< Receive completed flag */
    UART_CLEAR_TXOE = 0x04,    /*!< Transmit overflow error flag */
    UART_CLEAR_RXOE = 0x08,    /*!< Receive overflow error flag */
    UART_CLEAR_PE   = 0x10,    /*!< Parity error flag */
    UART_CLEAR_FE   = 0x20,    /*!< Frame error flag */
    UART_CLEAR_ABC  = 0x40,    /*!< Auto baud rate completed flag */
    UART_CLEAR_ABE  = 0x80     /*!< Auto baud rate error flag */
} UART_CLEAR_T;

/**@} end of group UART_Enumerations */

/** @addtogroup UART_Structure Data Structure
  @{
*/

/**
 * @brief    UART Config structure definition
 */
typedef struct
{
    uint16_t       baudrate;    /**
                                 * The baud rate is computed using the following formula:
                                 *   BaudRate = ((UART_PCLK) / (UART_Config_T->baudrate))
                                 * The default value is UART_PCLK = 4MHz, UART_Config_T->baudrate = 0x23, BaudRate = 115200bps
                                 */
    UART_STOPBIT_T stopbit;     /*!< UART stop bit */
    UART_PARITY_T  parity;      /**
                                 * When parity is enabled, the computed parity is inserted
                                 * at the MSB position of the transmitted data (9th bit when
                                 * the word length is set to 9 data bits; 8th bit when the
                                 * word length is set to 8 data bits).
                                 */
    uint8_t        parityen;    /*!< Enable parity, can be ENABLE or DISABLE. */
    UART_TXSEQ_T   sequence;    /*!< Transmit sequence */
    uint8_t        rxen;        /*!< Enable receive, can be ENABLE or DISABLE. */
    uint8_t        txen;        /*!< Enable transmit, can be ENABLE or DISABLE. */
} UART_Config_T;

/**@} end of group UART_Structure*/

/** @addtogroup UART_Functions Functions
  @{
*/

/* UART common configuration */
void UART_Reset(void);
void UART_Config(UART_Config_T* uartConfig);
void UART_ConfigStructInit(UART_Config_T* uartConfig);
void UART_ConfigBaudRateDiv(uint16_t bauddiv);
void UART_EnableTransmit(void);
void UART_DisableTransmit(void);
void UART_EnableReceive(void);
void UART_DisableReceive(void);
void UART_ConfigStopBit(UART_STOPBIT_T bit);
void UART_EnableParity(void);
void UART_DisableParity(void);
void UART_ConfigParity(UART_PARITY_T parity);
void UART_ConfigTransmitSequence(UART_TXSEQ_T sequence);
void UART_EnableAutoBaudRate(void);
void UART_DisableAutoBaudRate(void);
void UART_SendData(uint8_t data);
uint8_t UART_RecieveData(void);

/* UART interrupt configuration */
void UART_EnableInterrupt(UART_INT_T interrupt);
void UART_DisableInterrupt(UART_INT_T interrupt);
uint8_t UART_ReadFlag(UART_FLAG_T flag);
void UART_ClearFlag(UART_CLEAR_T flag);

/**@} end of group UART_Functions*/
/**@} end of group UART_Driver*/
/**@} end of group Peripherals_Library*/

#ifdef __cplusplus
}
#endif

#endif /* __BMP561_UART_H */
