1 /*
2  * Copyright 2021 MindMotion Microelectronics Co., Ltd.
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef __HAL_UART_H__
9 #define __HAL_UART_H__
10 
11 #include "hal_common.h"
12 
13 /*!
14  * @addtogroup UART
15  * @{
16  */
17 
18 /*!
19  * @brief UART driver version number.
20  */
21 #define UART_DRIVER_VERSION 0u /*!< uart_0. */
22 
23 /*!
24  * @addtogroup UART_STATUS
25  * @{
26  */
27 #define UART_STATUS_TX_DONE  UART_CSR_TXC_MASK    /*!< Status flag when UART transmiter shifter is empty after the transfer is done. */
28 #define UART_STATUS_RX_DONE  UART_CSR_RXAVL_MASK  /*!< Status flag when UART receiving buffer is with available data. */
29 #define UART_STATUS_TX_FULL  UART_CSR_TXFULL_MASK /*!< Status flag when UART transmiter buffer is full. */
30 #define UART_STATUS_TX_EMPTY UART_CSR_TXEPT_MASK  /*!< Status flag when UART transmiter buffer is empty. */
31 /*!
32  * @}
33  */
34 
35 /*!
36  * @addtogroup UART_INT
37  * @{
38  */
39 #define UART_INT_TX_EMPTY  UART_ISR_TXINTF_MASK   /*!< Interrupt enable when UART transmiter buffer is empty. */
40 #define UART_INT_RX_DONE   UART_ISR_RXINTF_MASK   /*!< Interrupt enable when UART receiving buffer is with available data. */
41 #define UART_INT_TX_DONE   UART_ISR_TXCINTF_MASK  /*!< Interrupt enable when UART transmiter shifter is empty. */
42 /*!
43  * @}
44  */
45 
46 /*!
47  * @brief UART word length type.
48  */
49 typedef enum
50 {
51     UART_WordLength_5b = 0u, /*!< Word length 5 bits. */
52     UART_WordLength_6b = 1u, /*!< Word length 6 bits. */
53     UART_WordLength_7b = 2u, /*!< Word length 7 bits. */
54     UART_WordLength_8b = 3u, /*!< Word length 8 bits. */
55 } UART_WordLength_Type;
56 
57 /*!
58  * @brief UART stop bits type.
59  */
60 typedef enum
61 {
62     UART_StopBits_1   = 0u, /*!< 1 stop bits. */
63     UART_StopBits_2   = 1u, /*!< 2 stop bits. */
64     UART_StopBits_0_5 = 2u, /*!< 0.5 stop bits. */
65     UART_StopBits_1_5 = 3u, /*!< 1.5 stop bits. */
66 } UART_StopBits_Type;
67 
68 /*!
69  * @brief UART parity type.
70  */
71 typedef enum
72 {
73     UART_Parity_None = 0u, /*!< No parity. */
74     UART_Parity_Even = 1u, /*!< Even parity. */
75     UART_Parity_Odd  = 2u, /*!< Odd parity. */
76 } UART_Parity_Type;
77 
78 /*!
79  * @brief UART hardware flow control type.
80  */
81 typedef enum
82 {
83     UART_HwFlowControl_None    = 0u, /*!< No hardware flow control. */
84     UART_HwFlowControl_RTS_CTS = 1u, /*!< Enable RTS and CTS hardware flow control.*/
85 } UART_HwFlowControl_Type;
86 
87 /*!
88  * @brief UART xfer mode type.
89  */
90 typedef enum
91 {
92     UART_XferMode_None   = 0u, /*!< Disable both Tx and Rx. */
93     UART_XferMode_RxOnly = 1u, /*!< Enable Rx only. */
94     UART_XferMode_TxOnly = 2u, /*!< Enable Tx only. */
95     UART_XferMode_RxTx   = 3u, /*!< Enable both Rx and Tx. */
96 } UART_XferMode_Type;
97 
98 /*!
99  * @brief This type of structure instance is used to keep the settings when calling the @ref UART_Init() to initialize the UART module.
100  */
101 typedef struct
102 {
103     uint32_t                ClockFreqHz;    /*!< Bus Clock Freq. */
104     uint32_t                BaudRate;       /*!< Specify the UART communication baud rate. */
105     UART_WordLength_Type    WordLength;     /*!< Specify the number of data bits transmitted or received in a frame. */
106     UART_StopBits_Type      StopBits;       /*!< Specify the number of stop bits transmitted. */
107     UART_Parity_Type        Parity;         /*!< Specify the parity mode. */
108     UART_XferMode_Type      XferMode;       /*!< Specify whether the Receive or Transmit mode is enabled or not. */
109     UART_HwFlowControl_Type HwFlowControl;  /*!< Specify whether the hardware flow control mode is enabled or not. */
110 } UART_Init_Type;
111 
112 /*!
113  * @brief Initialize the UART module.
114  *
115  * @param UARTx UART instance.
116  * @param init  Pointer to the initialization structure. See to @ref UART_Init_Type.
117  * @return None.
118  */
119 void UART_Init(UART_Type * UARTx, UART_Init_Type * init);
120 
121 /*!
122  * @brief Enable the UART module.
123  *
124  * The UART module should be enabled before sending or receiving data.
125  *
126  * @param UARTx UART instance.
127  * @param enable 'true' to enable the module, 'false' to disable the module.
128  * @return None.
129  */
130 void UART_Enable(UART_Type * UARTx, bool enable);
131 
132 /*!
133  * @brief Get the current status flags of the UART module.
134  *
135  * @param UARTx UART instance.
136  * @return Status flags. See to @ref UART_STATUS.
137  */
138 uint32_t UART_GetStatus(UART_Type * UARTx);
139 
140 /*!
141  * @brief Enable interrupts of the UART module.
142  *
143  * @param UARTx UART instance.
144  * @param interrupts Interrupt code masks. See to @ref UART_INT.
145  * @param enable 'true' to enable the indicated interrupts, 'false' to disable the indicated interrupts.
146  * @return None.
147  */
148 void UART_EnableInterrupts(UART_Type * UARTx, uint32_t interrupts, bool enable);
149 
150 /*!
151  * @brief Get the interrupts status flags of the UART module.
152  *
153  * @param UARTx UART instance.
154  * @return Interrupt status flags. See to @ref UART_INT.
155  */
156 uint32_t UART_GetInterruptStatus(UART_Type * UARTx);
157 
158 /*!
159  * @brief Clear the interrupts status flags of the UART module.
160  *
161  * @param UARTx UART instance.
162  * @param interrupts The mask codes of the indicated interrupt flags to be cleared.
163  * @return Interrupt status flags. See to @ref UART_INT.
164  */
165 void UART_ClearInterruptStatus(UART_Type * UARTx, uint32_t interrupts);
166 
167 /*!
168  * @brief Put the data into transmiter buffer of the UART module.
169  *
170  * @param UARTx UART instance.
171  * @param value Data value to be send through the transmiter.
172  * @return None.
173  */
174 void UART_PutData(UART_Type * UARTx, uint8_t value);
175 
176 /*!
177  * @brief Get the data from receiver buffer of the UART module.
178  *
179  * @param UARTx UART instance.
180  * @return The data value received from the receiver.
181  * @return None.
182  */
183 uint8_t UART_GetData(UART_Type * UARTx);
184 
185 /*!
186  * @brief Enable the DMA trigger from the UART module.
187  *
188  * The DMA trigger events are the same as the intertupts.
189  *
190  * @param UARTx UART instance.
191  * @param enable 'true' to enable the DMA trigger, 'false' to disable the DMA trigger.
192  * @return None.
193  */
194 void UART_EnableDMA(UART_Type * UARTx, bool enable);
195 
196 /*!
197  * @brief Read the current enabled interrupts the UART module.
198  *
199  * @param UARTx UART instance.
200  * @return The mask codes enabled interrupts. See to @ref UART_INT
201  */
202 uint32_t UART_GetEnabledInterrupts(UART_Type * UARTx);
203 
204 /*!
205  * @brief Get the hardware Rx data buffer's address the UART module.
206  *
207  * The return value of the address is most used with DMA module.
208  *
209  * @param UARTx UART instance.
210  * @return The value of the address for UART module's hardware Rx data buffer.
211  */
212 uint32_t UART_GetRxDataRegAddr(UART_Type * UARTx);
213 
214 /*!
215  * @brief Get the hardware Tx data buffer's address the UART module.
216  *
217  * The return value of the address is most used with DMA module.
218  *
219  * @param UARTx UART instance.
220  * @return The value of the address for UART module's hardware Tx data buffer.
221  */
222 uint32_t UART_GetTxDataRegAddr(UART_Type * UARTx);
223 
224 /*!
225  *@}
226  */
227 
228 #endif /* __HAL_UART_H__ */
229