1 /*
2  * Copyright (c) 2020-2020, BLUETRUM Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef AB32VG1_HAL_UART_H__
8 #define AB32VG1_HAL_UART_H__
9 
10 #include "ab32vg1_hal_def.h"
11 
12 /* Exported types */
13 /** @defgroup UART_Exported_Types UART Exported Types
14   * @{
15   */
16 
17 /**
18   * @brief UART Init Structure definition
19   */
20 
21 struct uart_init
22 {
23     uint32_t baud;          /*!< This member configures the UART communication baud rate.
24                                 Baud Rate Register[31:16] is used to configure the rx rate.
25                                 Baud Rate Register[15: 0] is used to configure the tx rate.
26                                 The baud rate register is computed using the following formula:
27                                     UARTBAUD = Fsys_clock/BaudRate-1
28                                     for example: UARTBAUD= (26000000/2)/baud-1 */
29 
30     uint8_t  word_len;      /*!< Specifies the number of data bits transmitted or received in a frame.
31                                 This parameter can be a value of @ref UARTEx_Word_Length. */
32 
33     uint8_t  stop_bits;     /*!< Specifies the number of stop bits transmitted.
34                                 This parameter can be a value of @ref UART_Stop_Bits. */
35 
36     uint8_t  mode;          /*!< Specifies whether the receive or one line mode is enabled or disabled.
37                                 This parameter can be a value of @ref UART_Mode. */
38 };
39 
40 /**
41  * @brief UART handle struction definition
42  *
43  */
44 struct uart_handle
45 {
46     hal_sfr_t           instance;
47     struct uart_init    init;
48 };
49 
50 /**
51   * @}
52   */
53 
54 /* Exported constants */
55 
56 /**
57  * @defgroup UARTEx_Word_Length UART Word Length
58  * @{
59  */
60 #define UART_WORDLENGTH_8B                  (0x00u)         /*!< 8 bit long UART frame      */
61 #define UART_WORDLENGTH_9B                  (0x01u)         /*!< 9 bit long UART frame      */
62 /**
63  * @}
64  */
65 
66 /**
67  * @defgroup UART_Stop_Bits UART Number of Stop Bits
68  * @{
69  */
70 #define UART_STOPBITS_1                     (0x00u)         /*!< UART frame with 1 stop bit    */
71 #define UART_STOPBITS_2                     (0x01u)         /*!< UART frame with 2 stop bit    */
72 /**
73  * @}
74  */
75 
76 /** @defgroup UART_Mode UART Transfer Mode
77   * @{
78   */
79 #define UART_MODE_TX                        (0x00u)         /*!< TX mode                    */
80 #define UART_MODE_TX_RX                     (0x01u)         /*!< RX and TX mode             */
81 #define UART_MODE_1LINE                     (0x02u)         /*!< oneline mode             */
82 
83 /**
84   * @}
85   */
86 
87 #define UART_FLAG_RXPND                       (BIT(9))        /*!< RX one byte finish flag  */
88 #define UART_FLAG_TXPND                       (BIT(8))        /*!< TX one byte finish flag  */
89 
90 #define UART_MODULE_ENABLE                    (BIT(0))        /*!< UART enable bit                */
91 #define UART_BIT9_ENABLE                      (BIT(1))        /*!< BIT9 enable bit                */
92 #define UART_RXIT_ENABLE                      (BIT(2))        /*!< Receive interrupt enable bit   */
93 #define UART_TXIT_ENABLE                      (BIT(3))        /*!< Transmit interrupt enable bit  */
94 #define UART_SB2_ENABLE                       (BIT(4))        /*!< Two stop bit enable bit        */
95 #define UART_CLK_SRC1                         (BIT(5))        /*!< Clock source select bit        */
96 #define UART_1LINE_ENABLE                     (BIT(6))        /*!< One-Line mode enable bit       */
97 #define UART_RX_ENABLE                        (BIT(7))        /*!< Receive enable bit             */
98 
99 #define UART0N                                (0x00u)         /*!< Number of UART0        */
100 #define UART1N                                (0x01u)         /*!< Number of UART1        */
101 #define UART2N                                (0x02u)         /*!< Number of UART2        */
102 
103 #define UART0_BASE                            ((hal_sfr_t)(&UART0CON))
104 #define UART1_BASE                            ((hal_sfr_t)(&UART1CON))
105 #define UART2_BASE                            ((hal_sfr_t)(&UART2CON))
106 
107 /* Exported function */
108 /** @addtogroup UART_Exported_Functions UART Exported Functions
109   * @{
110   */
111 
112 /** @addtogroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
113   * @{
114   */
115 
116 /* Initialization functions */
117 hal_error_t hal_uart_init(struct uart_handle *huart);
118 void hal_uart_deinit(hal_sfr_t uartx);
119 void hal_uart_mspinit(struct uart_handle *huart);
120 
121 /**
122   * @}
123   */
124 
125 void hal_uart_control(hal_sfr_t uartx, uint32_t cntl, uint32_t param);
126 void hal_uart_write(hal_sfr_t uartx, uint8_t data);
127 uint8_t hal_uart_read(hal_sfr_t uartx);
128 uint32_t hal_uart_getflag(hal_sfr_t uartx, uint32_t flag);
129 void hal_uart_clrflag(hal_sfr_t uartx, uint32_t flag);
130 
131 /**
132   * @}
133   */
134 
135 /* Private function */
136 /** @addtogroup UART_Private_Functions UART Private Functions
137   * @{
138   */
139 void uart_config_all(struct uart_handle *huart);
140 
141 /**
142   * @}
143   */
144 
145 #endif
146