1 /**************************************************************************//**
2  * @file     scuart.h
3  * @version  V3.00
4  * @brief    Smartcard UART mode (SCUART) driver header file
5  *
6  * @copyright SPDX-License-Identifier: Apache-2.0
7  * @copyright Copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
8  *****************************************************************************/
9 #ifndef __SCUART_H__
10 #define __SCUART_H__
11 
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16 
17 
18 /** @addtogroup Standard_Driver Standard Driver
19   @{
20 */
21 
22 /** @addtogroup SCUART_Driver SCUART Driver
23   @{
24 */
25 
26 /** @addtogroup SCUART_EXPORTED_CONSTANTS SCUART Exported Constants
27   @{
28 */
29 #define SCUART_CHAR_LEN_5     (0x3UL << SC_UARTCTL_WLS_Pos)     /*!< Set SCUART word length to 5 bits \hideinitializer */
30 #define SCUART_CHAR_LEN_6     (0x2UL << SC_UARTCTL_WLS_Pos)     /*!< Set SCUART word length to 6 bits \hideinitializer */
31 #define SCUART_CHAR_LEN_7     (0x1UL << SC_UARTCTL_WLS_Pos)     /*!< Set SCUART word length to 7 bits \hideinitializer */
32 #define SCUART_CHAR_LEN_8     (0UL)                             /*!< Set SCUART word length to 8 bits \hideinitializer */
33 
34 #define SCUART_PARITY_NONE    (SC_UARTCTL_PBOFF_Msk)            /*!< Set SCUART transfer with no parity   \hideinitializer */
35 #define SCUART_PARITY_ODD     (SC_UARTCTL_OPE_Msk)              /*!< Set SCUART transfer with odd parity  \hideinitializer */
36 #define SCUART_PARITY_EVEN    (0UL)                             /*!< Set SCUART transfer with even parity \hideinitializer */
37 
38 #define SCUART_STOP_BIT_1     (SC_CTL_NSB_Msk)                  /*!< Set SCUART transfer with one stop bit  \hideinitializer */
39 #define SCUART_STOP_BIT_2     (0UL)                             /*!< Set SCUART transfer with two stop bits \hideinitializer */
40 
41 /**@}*/ /* end of group SCUART_EXPORTED_CONSTANTS */
42 
43 
44 /** @addtogroup SCUART_EXPORTED_FUNCTIONS SCUART Exported Functions
45   @{
46 */
47 
48 /* TX Macros */
49 /**
50   * @brief Write Data to Tx data register
51   *
52   * @param[in]  sc      The pointer of smartcard module.
53   * @param[in]  u8Data  Data byte to transmit.
54   *
55   * @return     None
56   *
57   * @details    By writing data to DAT register, the SC will send out an 8-bit data.
58   * \hideinitializer
59   */
60 #define SCUART_WRITE(sc, u8Data)    ((sc)->DAT = (u8Data))
61 
62 /**
63   * @brief      Get Tx FIFO empty flag status from register
64   *
65   * @param[in]  sc      The pointer of smartcard module.
66   *
67   * @return     Transmit FIFO empty status
68   * @retval     0                       Transmit FIFO is not empty
69   * @retval     SC_STATUS_TXEMPTY_Msk   Transmit FIFO is empty
70   *
71   * @details    When the last byte of Tx buffer has been transferred to Transmitter Shift Register, hardware sets TXEMPTY (SC_STATUS[9]) high.
72   *             It will be cleared when writing data into DAT (SC_DAT[7:0]).
73   * \hideinitializer
74   */
75 #define SCUART_GET_TX_EMPTY(sc)     ((sc)->STATUS & SC_STATUS_TXEMPTY_Msk)
76 
77 /**
78   * @brief Get Tx FIFO full flag status from register
79   *
80   * @param[in]  sc      The pointer of smartcard module.
81   *
82   * @return     Transmit FIFO full status
83   * @retval     0                       Transmit FIFO is not full
84   * @retval     SC_STATUS_TXFULL_Msk    Transmit FIFO is full
85   *
86   * @details    TXFULL (SC_STATUS[10]) is set when Tx buffer counts equals to 4, otherwise is cleared by hardware.
87   * \hideinitializer
88   */
89 #define SCUART_GET_TX_FULL(sc)      ((sc)->STATUS & SC_STATUS_TXFULL_Msk)
90 
91 /**
92   * @brief      Wait specified smartcard port transmission complete
93   *
94   * @param[in]  sc      The pointer of smartcard module.
95   *
96   * @return     None
97   *
98   * @details    TXACT (SC_STATUS[31]) is cleared automatically when Tx transfer is finished or the last byte transmission has completed.
99   *
100   * @note       This macro blocks until transmit complete.
101   * \hideinitializer
102   */
103 #define SCUART_WAIT_TX_EMPTY(sc)    while(((sc)->STATUS & SC_STATUS_TXACT_Msk) == SC_STATUS_TXACT_Msk)
104 
105 /**
106   * @brief      Check specified smartcard port transmit FIFO is full or not
107   *
108   * @param[in]  sc      The pointer of smartcard module.
109   *
110   * @return     Transmit FIFO full status
111   * @retval     0       Transmit FIFO is not full
112   * @retval     1       Transmit FIFO is full
113   *
114   * @details    TXFULL (SC_STATUS[10]) indicates Tx buffer full or not.
115   *             This bit is set when Tx buffer counts equals to 4, otherwise is cleared by hardware.
116   * \hideinitializer
117   */
118 #define SCUART_IS_TX_FULL(sc)       (((sc)->STATUS & SC_STATUS_TXFULL_Msk)? 1 : 0)
119 
120 /**
121   * @brief      Check specified smartcard port transmission is over
122   *
123   * @param[in]  sc      The pointer of smartcard module.
124   *
125   * @return     Transmit complete status
126   * @retval     0       Transmit is not complete
127   * @retval     1       Transmit complete
128   *
129   * @details    TXACT (SC_STATUS[31]) indicates Tx Transmit is complete or not.
130   * \hideinitializer
131   */
132 #define SCUART_IS_TX_EMPTY(sc)      (((sc)->STATUS & SC_STATUS_TXACT_Msk)? 0 : 1)
133 
134 /**
135   * @brief      Check specified smartcard port transmit FIFO empty status
136   *
137   * @param[in]  sc      The pointer of smartcard module.
138   *
139   * @return     Transmit FIFO empty status
140   * @retval     0       Transmit FIFO is not empty
141   * @retval     1       Transmit FIFO is empty
142   *
143   * @details    TXEMPTY (SC_STATUS[9]) is set by hardware when the last byte of Tx buffer has been transferred to Transmitter Shift Register.
144   * \hideinitializer
145   */
146 #define SCUART_IS_TX_FIFO_EMPTY(sc) (((sc)->STATUS & SC_STATUS_TXEMPTY_Msk)? 1 : 0)
147 
148 /**
149   * @brief      Check specified Smartcard port Transmission Status
150   *
151   * @param[in]  sc      The pointer of smartcard module.
152   *
153   * @retval     0       Transmit is completed
154   * @retval     1       Transmit is active
155   *
156   * @details    TXACT (SC_STATUS[31]) is set by hardware when Tx transfer is in active and the STOP bit of the last byte has been transmitted.
157   * \hideinitializer
158   */
159 #define SCUART_IS_TX_ACTIVE(sc)     (((sc)->STATUS & SC_STATUS_TXACT_Msk)? 1 : 0)
160 
161 
162 /* RX Macros */
163 /**
164   * @brief      Read Rx data register
165   *
166   * @param[in]  sc      The pointer of smartcard module.
167   *
168   * @return     The oldest data byte in RX FIFO
169   *
170   * @details    By reading DAT register, the SC will return an 8-bit received data.
171   * \hideinitializer
172   */
173 #define SCUART_READ(sc)         ((sc)->DAT)
174 
175 /**
176   * @brief      Get Rx FIFO empty flag status from register
177   *
178   * @param[in]  sc      The pointer of smartcard module.
179   *
180   * @return     Receive FIFO empty status
181   * @retval     0                       Receive FIFO is not empty
182   * @retval     SC_STATUS_RXEMPTY_Msk   Receive FIFO is empty
183   *
184   * @details    When the last byte of Rx buffer has been read by CPU, hardware sets RXEMPTY (SC_STATUS[1]) high.
185   *             It will be cleared when SC receives any new data.
186   * \hideinitializer
187   */
188 #define SCUART_GET_RX_EMPTY(sc) ((sc)->STATUS & SC_STATUS_RXEMPTY_Msk)
189 
190 /**
191   * @brief      Get Rx FIFO full flag status from register
192   *
193   * @param[in]  sc      The pointer of smartcard module.
194   *
195   * @return     Receive FIFO full status
196   * @retval     0                       Receive FIFO is not full
197   * @retval     SC_STATUS_TXFULL_Msk    Receive FIFO is full
198   *
199   * @details    RXFULL (SC_STATUS[2]) is set when Rx buffer counts equals to 4, otherwise it is cleared by hardware.
200   * \hideinitializer
201   */
202 #define SCUART_GET_RX_FULL(sc)  ((sc)->STATUS & SC_STATUS_RXFULL_Msk)
203 
204 /**
205   * @brief      Check if receive data number in FIFO reach FIFO trigger level or not
206   *
207   * @param[in]  sc      The pointer of smartcard module.
208   *
209   * @return     Receive FIFO data status
210   * @retval     0       The number of bytes in receive FIFO is less than trigger level
211   * @retval     1       The number of bytes in receive FIFO equals or larger than trigger level
212   *
213   * @details    RDAIF (SC_INTSTS[0]) is used for received data reaching trigger level RXTRGLV (SC_CTL[7:6]) interrupt status flag.
214   *
215   * @note       If receive trigger level is \b not 1 byte, this macro return 0 does not necessary indicates there is no data in FIFO.
216   * \hideinitializer
217   */
218 #define SCUART_IS_RX_READY(sc)  (((sc)->INTSTS & SC_INTSTS_RDAIF_Msk)? 1 : 0)
219 
220 /**
221   * @brief      Check specified smartcard port receive FIFO is full or not
222   *
223   * @param[in]  sc      The pointer of smartcard module.
224   *
225   * @return     Receive FIFO full status
226   * @retval     0       Receive FIFO is not full
227   * @retval     1       Receive FIFO is full
228   *
229   * @details    RXFULLF( SC_STATUS[2]) is set when Rx buffer counts equals to 4, otherwise it is cleared by hardware.
230   * \hideinitializer
231   */
232 #define SCUART_IS_RX_FULL(sc)   (((sc)->STATUS & SC_STATUS_RXFULL_Msk)? 1 : 0)
233 
234 
235 /* Interrupt Macros */
236 /**
237   * @brief      Enable specified interrupts
238   *
239   * @param[in]  sc      The pointer of smartcard module.
240   * @param[in]  u32Mask Interrupt masks to enable, a combination of following bits,
241   *                         - \ref SC_INTEN_RXTOIEN_Msk
242   *                         - \ref SC_INTEN_TERRIEN_Msk
243   *                         - \ref SC_INTEN_TBEIEN_Msk
244   *                         - \ref SC_INTEN_RDAIEN_Msk
245   *
246   * @return     None
247   *
248   * @details    The macro is used to enable receiver buffer time-out interrupt, transfer error interrupt,
249   *             transmit buffer empty interrupt or receive data reach trigger level interrupt.
250   * \hideinitializer
251   */
252 #define SCUART_ENABLE_INT(sc, u32Mask)  ((sc)->INTEN |= (u32Mask))
253 
254 /**
255   * @brief      Disable specified interrupts
256   *
257   * @param[in]  sc      The pointer of smartcard module.
258   * @param[in]  u32Mask Interrupt masks to disable, a combination of following bits,
259   *                         - \ref SC_INTEN_RXTOIEN_Msk
260   *                         - \ref SC_INTEN_TERRIEN_Msk
261   *                         - \ref SC_INTEN_TBEIEN_Msk
262   *                         - \ref SC_INTEN_RDAIEN_Msk
263   *
264   * @return     None
265   *
266   * @details    The macro is used to disable receiver buffer time-out interrupt, transfer error interrupt,
267   *             transmit buffer empty interrupt or receive data reach trigger level interrupt.
268   * \hideinitializer
269   */
270 #define SCUART_DISABLE_INT(sc, u32Mask) ((sc)->INTEN &= ~(u32Mask))
271 
272 /**
273   * @brief      Get specified interrupt flag/status
274   *
275   * @param[in]  sc      The pointer of smartcard module.
276   * @param[in] u32Type  Interrupt flag/status to check, could be one of following value
277   *                         - \ref SC_INTSTS_RXTOIF_Msk
278   *                         - \ref SC_INTSTS_TERRIF_Msk
279   *                         - \ref SC_INTSTS_TBEIF_Msk
280   *                         - \ref SC_INTSTS_RDAIF_Msk
281   *
282   * @return     The status of specified interrupt
283   * @retval     0       Specified interrupt does not happened
284   * @retval     1       Specified interrupt happened
285   *
286   * @details    The macro is used to get receiver buffer time-out interrupt status, transfer error interrupt status,
287   *             transmit buffer empty interrupt status or receive data reach interrupt status.
288   * \hideinitializer
289   */
290 #define SCUART_GET_INT_FLAG(sc, u32Type)    (((sc)->INTSTS & (u32Type))? 1 : 0)
291 
292 /**
293   * @brief      Clear specified interrupt flag/status
294   *
295   * @param[in]  sc      The pointer of smartcard module.
296   * @param[in] u32Type  Interrupt flag/status to clear, only \ref SC_INTSTS_TERRIF_Msk valid for this macro.
297   *
298   * @return     None
299   *
300   * @details    The macro is used to clear transfer error interrupt flag.
301   * \hideinitializer
302   */
303 #define SCUART_CLR_INT_FLAG(sc, u32Type)    ((sc)->INTSTS = (u32Type))
304 
305 /**
306   * @brief      Get receive error flag/status
307   *
308   * @param[in]  sc  The pointer of smartcard module.
309   *
310   * @return     Current receive error status, could one of following errors:
311   * @retval     SC_STATUS_PEF_Msk   Parity error
312   * @retval     SC_STATUS_FEF_Msk   Frame error
313   * @retval     SC_STATUS_BEF_Msk   Break error
314   *
315   * @details    The macro is used to get receiver parity error status, frame error status or break error status.
316   * \hideinitializer
317   */
318 #define SCUART_GET_ERR_FLAG(sc)             ((sc)->STATUS & (SC_STATUS_PEF_Msk | SC_STATUS_FEF_Msk | SC_STATUS_BEF_Msk))
319 
320 /**
321   * @brief      Clear specified receive error flag/status
322   *
323   * @param[in]  sc      The pointer of smartcard module.
324   * @param[in]  u32Mask Receive error flag/status to clear, combination following values
325   *                         - \ref SC_STATUS_PEF_Msk
326   *                         - \ref SC_STATUS_FEF_Msk
327   *                         - \ref SC_STATUS_BEF_Msk
328   *
329   * @return     None
330   *
331   * @details    The macro is used to clear receiver parity error flag, frame error flag or break error flag.
332   * \hideinitializer
333   */
334 #define SCUART_CLR_ERR_FLAG(sc, u32Mask)    ((sc)->STATUS = (u32Mask))
335 
336 void SCUART_Close(SC_T* sc);
337 uint32_t SCUART_Open(SC_T* sc, uint32_t u32Baudrate);
338 uint32_t SCUART_Read(SC_T* sc, uint8_t pu8RxBuf[], uint32_t u32ReadBytes);
339 uint32_t SCUART_SetLineConfig(SC_T* sc, uint32_t u32Baudrate, uint32_t u32DataWidth, uint32_t u32Parity, uint32_t u32StopBits);
340 void SCUART_SetTimeoutCnt(SC_T* sc, uint32_t u32TOC);
341 void SCUART_Write(SC_T* sc, uint8_t pu8TxBuf[], uint32_t u32WriteBytes);
342 
343 /**@}*/ /* end of group SCUART_EXPORTED_FUNCTIONS */
344 
345 /**@}*/ /* end of group SCUART_Driver */
346 
347 /**@}*/ /* end of group Standard_Driver */
348 
349 #ifdef __cplusplus
350 }
351 #endif
352 
353 #endif /* __SCUART_H__ */
354