1 /****************************************************************************** 2 * @file usci_uart.h 3 * @version V3.00 4 * @brief USCI UART (UUART) 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 __USCI_UART_H__ 10 #define __USCI_UART_H__ 11 12 13 #ifdef __cplusplus 14 extern "C" 15 { 16 #endif 17 18 19 /** @addtogroup Standard_Driver Standard Driver 20 @{ 21 */ 22 23 /** @addtogroup USCI_UART_Driver USCI_UART Driver 24 @{ 25 */ 26 27 /** @addtogroup USCI_UART_EXPORTED_CONSTANTS USCI_UART Exported Constants 28 @{ 29 */ 30 31 /*---------------------------------------------------------------------------------------------------------*/ 32 /* UUART_LINECTL constants definitions */ 33 /*---------------------------------------------------------------------------------------------------------*/ 34 #define UUART_WORD_LEN_6 (6UL << UUART_LINECTL_DWIDTH_Pos) /*!< UUART_LINECTL setting to set UART word length to 6 bits */ 35 #define UUART_WORD_LEN_7 (7UL << UUART_LINECTL_DWIDTH_Pos) /*!< UUART_LINECTL setting to set UART word length to 7 bits */ 36 #define UUART_WORD_LEN_8 (8UL << UUART_LINECTL_DWIDTH_Pos) /*!< UUART_LINECTL setting to set UART word length to 8 bits */ 37 #define UUART_WORD_LEN_9 (9UL << UUART_LINECTL_DWIDTH_Pos) /*!< UUART_LINECTL setting to set UART word length to 9 bits */ 38 39 /*---------------------------------------------------------------------------------------------------------*/ 40 /* UUART_PROTCTL constants definitions */ 41 /*---------------------------------------------------------------------------------------------------------*/ 42 #define UUART_PARITY_NONE (0x0UL << UUART_PROTCTL_PARITYEN_Pos) /*!< UUART_PROTCTL setting to set UART as no parity */ 43 #define UUART_PARITY_ODD (0x1UL << UUART_PROTCTL_PARITYEN_Pos) /*!< UUART_PROTCTL setting to set UART as odd parity */ 44 #define UUART_PARITY_EVEN (0x3UL << UUART_PROTCTL_PARITYEN_Pos) /*!< UUART_PROTCTL setting to set UART as even parity */ 45 46 #define UUART_STOP_BIT_1 (0x0UL) /*!< UUART_PROTCTL setting for one stop bit */ 47 #define UUART_STOP_BIT_2 (0x1UL) /*!< UUART_PROTCTL setting for two stop bit */ 48 49 /*---------------------------------------------------------------------------------------------------------*/ 50 /* USCI UART interrupt mask definitions */ 51 /*---------------------------------------------------------------------------------------------------------*/ 52 #define UUART_ABR_INT_MASK (0x002UL) /*!< Auto-baud rate interrupt mask */ 53 #define UUART_RLS_INT_MASK (0x004UL) /*!< Receive line status interrupt mask */ 54 #define UUART_BUF_RXOV_INT_MASK (0x008UL) /*!< Buffer RX overrun interrupt mask */ 55 #define UUART_TXST_INT_MASK (0x010UL) /*!< TX start interrupt mask */ 56 #define UUART_TXEND_INT_MASK (0x020UL) /*!< Tx end interrupt mask */ 57 #define UUART_RXST_INT_MASK (0x040UL) /*!< RX start interrupt mask */ 58 #define UUART_RXEND_INT_MASK (0x080UL) /*!< RX end interrupt mask */ 59 60 61 /**@}*/ /* end of group USCI_UART_EXPORTED_CONSTANTS */ 62 63 64 /** @addtogroup USCI_UART_EXPORTED_FUNCTIONS USCI_UART Exported Functions 65 @{ 66 */ 67 68 69 /** 70 * @brief Write USCI_UART data 71 * 72 * @param[in] uuart The pointer of the specified USCI_UART module 73 * @param[in] u8Data Data byte to transmit. 74 * 75 * @return None 76 * 77 * @details This macro write Data to Tx data register. 78 */ 79 #define UUART_WRITE(uuart, u8Data) ((uuart)->TXDAT = (u8Data)) 80 81 82 /** 83 * @brief Read USCI_UART data 84 * 85 * @param[in] uuart The pointer of the specified USCI_UART module 86 * 87 * @return The oldest data byte in RX buffer. 88 * 89 * @details This macro read Rx data register. 90 */ 91 #define UUART_READ(uuart) ((uuart)->RXDAT) 92 93 94 /** 95 * @brief Get Tx empty 96 * 97 * @param[in] uuart The pointer of the specified USCI_UART module 98 * 99 * @retval 0 Tx buffer is not empty 100 * @retval >=1 Tx buffer is empty 101 * 102 * @details This macro get Transmitter buffer empty register value. 103 */ 104 #define UUART_GET_TX_EMPTY(uuart) ((uuart)->BUFSTS & UUART_BUFSTS_TXEMPTY_Msk) 105 106 107 /** 108 * @brief Get Rx empty 109 * 110 * @param[in] uuart The pointer of the specified USCI_UART module 111 * 112 * @retval 0 Rx buffer is not empty 113 * @retval >=1 Rx buffer is empty 114 * 115 * @details This macro get Receiver buffer empty register value. 116 */ 117 #define UUART_GET_RX_EMPTY(uuart) ((uuart)->BUFSTS & UUART_BUFSTS_RXEMPTY_Msk) 118 119 120 /** 121 * @brief Check specified usci_uart port transmission is over. 122 * 123 * @param[in] uuart The pointer of the specified USCI_UART module 124 * 125 * @retval 0 Tx transmission is not over 126 * @retval 1 Tx transmission is over 127 * 128 * @details This macro return Transmitter Empty Flag register bit value. \n 129 * It indicates if specified usci_uart port transmission is over nor not. 130 */ 131 #define UUART_IS_TX_EMPTY(uuart) (((uuart)->BUFSTS & UUART_BUFSTS_TXEMPTY_Msk) >> UUART_BUFSTS_TXEMPTY_Pos) 132 133 134 /** 135 * @brief Check specified usci_uart port receiver is empty. 136 * 137 * @param[in] uuart The pointer of the specified USCI_UART module 138 * 139 * @retval 0 Rx receiver is not empty 140 * @retval 1 Rx receiver is empty 141 * 142 * @details This macro return Receive Empty Flag register bit value. \n 143 * It indicates if specified usci_uart port receiver is empty nor not. 144 */ 145 #define UUART_IS_RX_EMPTY(uuart) (((uuart)->BUFSTS & UUART_BUFSTS_RXEMPTY_Msk) >> UUART_BUFSTS_RXEMPTY_Pos) 146 147 148 /** 149 * @brief Wait specified usci_uart port transmission is over 150 * 151 * @param[in] uuart The pointer of the specified USCI_UART module 152 * 153 * @return None 154 * 155 * @details This macro wait specified usci_uart port transmission is over. 156 */ 157 #define UUART_WAIT_TX_EMPTY(uuart) while(!((((uuart)->BUFSTS) & UUART_BUFSTS_TXEMPTY_Msk) >> UUART_BUFSTS_TXEMPTY_Pos)) 158 159 160 /** 161 * @brief Check TX buffer is full or not 162 * 163 * @param[in] uuart The pointer of the specified USCI_UART module 164 * 165 * @retval 1 TX buffer is full 166 * @retval 0 TX buffer is not full 167 * 168 * @details This macro check TX buffer is full or not. 169 */ 170 #define UUART_IS_TX_FULL(uuart) (((uuart)->BUFSTS & UUART_BUFSTS_TXFULL_Msk)>>UUART_BUFSTS_TXFULL_Pos) 171 172 173 /** 174 * @brief Check RX buffer is full or not 175 * 176 * @param[in] uuart The pointer of the specified USCI_UART module 177 * 178 * @retval 1 RX buffer is full 179 * @retval 0 RX buffer is not full 180 * 181 * @details This macro check RX buffer is full or not. 182 */ 183 #define UUART_IS_RX_FULL(uuart) (((uuart)->BUFSTS & UUART_BUFSTS_RXFULL_Msk)>>UUART_BUFSTS_RXFULL_Pos) 184 185 186 /** 187 * @brief Get Tx full register value 188 * 189 * @param[in] uuart The pointer of the specified USCI_UART module 190 * 191 * @retval 0 Tx buffer is not full. 192 * @retval >=1 Tx buffer is full. 193 * 194 * @details This macro get Tx full register value. 195 */ 196 #define UUART_GET_TX_FULL(uuart) ((uuart)->BUFSTS & UUART_BUFSTS_TXFULL_Msk) 197 198 199 /** 200 * @brief Get Rx full register value 201 * 202 * @param[in] uuart The pointer of the specified USCI_UART module 203 * 204 * @retval 0 Rx buffer is not full. 205 * @retval >=1 Rx buffer is full. 206 * 207 * @details This macro get Rx full register value. 208 */ 209 #define UUART_GET_RX_FULL(uuart) ((uuart)->BUFSTS & UUART_BUFSTS_RXFULL_Msk) 210 211 212 /** 213 * @brief Enable specified USCI_UART protocol interrupt 214 * 215 * @param[in] uuart The pointer of the specified USCI_UART module 216 * @param[in] u32IntSel Interrupt type select 217 * - \ref UUART_PROTIEN_RLSIEN_Msk : Rx Line status interrupt 218 * - \ref UUART_PROTIEN_ABRIEN_Msk : Auto-baud rate interrupt 219 * 220 * @return None 221 * 222 * @details This macro enable specified USCI_UART protocol interrupt. 223 */ 224 #define UUART_ENABLE_PROT_INT(uuart, u32IntSel) ((uuart)->PROTIEN |= (u32IntSel)) 225 226 227 /** 228 * @brief Disable specified USCI_UART protocol interrupt 229 * 230 * @param[in] uuart The pointer of the specified USCI_UART module 231 * @param[in] u32IntSel Interrupt type select 232 * - \ref UUART_PROTIEN_RLSIEN_Msk : Rx Line status interrupt 233 * - \ref UUART_PROTIEN_ABRIEN_Msk : Auto-baud rate interrupt 234 * 235 * @return None 236 * 237 * @details This macro disable specified USCI_UART protocol interrupt. 238 */ 239 #define UUART_DISABLE_PROT_INT(uuart, u32IntSel) ((uuart)->PROTIEN &= ~(u32IntSel)) 240 241 242 /** 243 * @brief Enable specified USCI_UART buffer interrupt 244 * 245 * @param[in] uuart The pointer of the specified USCI_UART module 246 * @param[in] u32IntSel Interrupt type select 247 * - \ref UUART_BUFCTL_RXOVIEN_Msk : Receive buffer overrun error interrupt 248 * 249 * @return None 250 * 251 * @details This macro enable specified USCI_UART buffer interrupt. 252 */ 253 #define UUART_ENABLE_BUF_INT(uuart, u32IntSel) ((uuart)->BUFCTL |= (u32IntSel)) 254 255 256 /** 257 * @brief Disable specified USCI_UART buffer interrupt 258 * 259 * @param[in] uuart The pointer of the specified USCI_UART module 260 * @param[in] u32IntSel Interrupt type select 261 * - \ref UUART_BUFCTL_RXOVIEN_Msk : Receive buffer overrun error interrupt 262 * 263 * @return None 264 * 265 * @details This macro disable specified USCI_UART buffer interrupt. 266 */ 267 #define UUART_DISABLE_BUF_INT(uuart, u32IntSel) ((uuart)->BUFCTL &= ~ (u32IntSel)) 268 269 270 /** 271 * @brief Enable specified USCI_UART transfer interrupt 272 * 273 * @param[in] uuart The pointer of the specified USCI_UART module 274 * @param[in] u32IntSel Interrupt type select 275 * - \ref UUART_INTEN_RXENDIEN_Msk : Receive end interrupt 276 * - \ref UUART_INTEN_RXSTIEN_Msk : Receive start interrupt 277 * - \ref UUART_INTEN_TXENDIEN_Msk : Transmit end interrupt 278 * - \ref UUART_INTEN_TXSTIEN_Msk : Transmit start interrupt 279 * 280 * @return None 281 * 282 * @details This macro enable specified USCI_UART transfer interrupt. 283 */ 284 #define UUART_ENABLE_TRANS_INT(uuart, u32IntSel) ((uuart)->INTEN |= (u32IntSel)) 285 286 287 /** 288 * @brief Disable specified USCI_UART transfer interrupt 289 * 290 * @param[in] uuart The pointer of the specified USCI_UART module 291 * @param[in] u32IntSel Interrupt type select 292 * - \ref UUART_INTEN_RXENDIEN_Msk : Receive end interrupt 293 * - \ref UUART_INTEN_RXSTIEN_Msk : Receive start interrupt 294 * - \ref UUART_INTEN_TXENDIEN_Msk : Transmit end interrupt 295 * - \ref UUART_INTEN_TXSTIEN_Msk : Transmit start interrupt 296 * 297 * @return None 298 * 299 * @details This macro disable specified USCI_UART transfer interrupt. 300 */ 301 #define UUART_DISABLE_TRANS_INT(uuart, u32IntSel) ((uuart)->INTEN &= ~(u32IntSel)) 302 303 304 /** 305 * @brief Get protocol interrupt flag/status 306 * 307 * @param[in] uuart The pointer of the specified USCI_UART module 308 * 309 * @return The interrupt flag/status of protocol status register. 310 * 311 * @details This macro get protocol status register value. 312 */ 313 #define UUART_GET_PROT_STATUS(uuart) ((uuart)->PROTSTS) 314 315 316 /** 317 * @brief Clear specified protocol interrupt flag 318 * 319 * @param[in] uuart The pointer of the specified USCI_UART module 320 * @param[in] u32IntTypeFlag Interrupt Type Flag, should be 321 * - \ref UUART_PROTSTS_ABERRSTS_Msk : Auto-baud Rate Error Interrupt Indicator 322 * - \ref UUART_PROTSTS_ABRDETIF_Msk : Auto-baud Rate Detected Interrupt Flag 323 * - \ref UUART_PROTSTS_BREAK_Msk : Break Flag 324 * - \ref UUART_PROTSTS_FRMERR_Msk : Framing Error Flag 325 * - \ref UUART_PROTSTS_PARITYERR_Msk : Parity Error Flag 326 * - \ref UUART_PROTSTS_RXENDIF_Msk : Receive End Interrupt Flag 327 * - \ref UUART_PROTSTS_RXSTIF_Msk : Receive Start Interrupt Flag 328 * - \ref UUART_PROTSTS_TXENDIF_Msk : Transmit End Interrupt Flag 329 * - \ref UUART_PROTSTS_TXSTIF_Msk : Transmit Start Interrupt Flag 330 * 331 * @return None 332 * 333 * @details This macro clear specified protocol interrupt flag. 334 */ 335 #define UUART_CLR_PROT_INT_FLAG(uuart,u32IntTypeFlag) ((uuart)->PROTSTS = (u32IntTypeFlag)) 336 337 338 /** 339 * @brief Get transmit/receive buffer interrupt flag/status 340 * 341 * @param[in] uuart The pointer of the specified USCI_UART module 342 * 343 * @return The interrupt flag/status of buffer status register. 344 * 345 * @details This macro get buffer status register value. 346 */ 347 #define UUART_GET_BUF_STATUS(uuart) ((uuart)->BUFSTS) 348 349 350 /** 351 * @brief Clear specified buffer interrupt flag 352 * 353 * @param[in] uuart The pointer of the specified USCI_UART module 354 * @param[in] u32IntTypeFlag Interrupt Type Flag, should be 355 * - \ref UUART_BUFSTS_RXOVIF_Msk : Receive Buffer Over-run Error Interrupt Indicator 356 * 357 * @return None 358 * 359 * @details This macro clear specified buffer interrupt flag. 360 */ 361 #define UUART_CLR_BUF_INT_FLAG(uuart,u32IntTypeFlag) ((uuart)->BUFSTS = (u32IntTypeFlag)) 362 363 364 /** 365 * @brief Get wakeup flag 366 * 367 * @param[in] uuart The pointer of the specified USCI_UART module 368 * 369 * @retval 0 Chip did not wake up from power-down mode. 370 * @retval 1 Chip waked up from power-down mode. 371 * 372 * @details This macro get wakeup flag. 373 */ 374 #define UUART_GET_WAKEUP_FLAG(uuart) ((uuart)->WKSTS & UUART_WKSTS_WKF_Msk ? 1: 0 ) 375 376 377 /** 378 * @brief Clear wakeup flag 379 * 380 * @param[in] uuart The pointer of the specified USCI_UART module 381 * 382 * @return None 383 * 384 * @details This macro clear wakeup flag. 385 */ 386 #define UUART_CLR_WAKEUP_FLAG(uuart) ((uuart)->WKSTS = UUART_WKSTS_WKF_Msk) 387 388 389 /** 390 * @brief Enable specified USCI_UART PDMA function 391 * 392 * @param[in] uuart The pointer of the specified USCI_UART module 393 * @param[in] u32FuncSel Combination of following functions 394 * - \ref UUART_PDMACTL_TXPDMAEN_Msk 395 * - \ref UUART_PDMACTL_RXPDMAEN_Msk 396 * - \ref UUART_PDMACTL_PDMAEN_Msk 397 * 398 * @return None 399 * 400 * @details This macro enable specified USCI_UART PDMA function. 401 */ 402 #define UUART_PDMA_ENABLE(uuart, u32FuncSel) ((uuart)->PDMACTL |= (u32FuncSel)) 403 404 /** 405 * @brief Disable specified USCI_UART PDMA function 406 * 407 * @param[in] uuart The pointer of the specified USCI_UART module 408 * @param[in] u32FuncSel Combination of following functions 409 * - \ref UUART_PDMACTL_TXPDMAEN_Msk 410 * - \ref UUART_PDMACTL_RXPDMAEN_Msk 411 * - \ref UUART_PDMACTL_PDMAEN_Msk 412 * 413 * @return None 414 * 415 * @details This macro disable specified USCI_UART PDMA function. 416 */ 417 #define UUART_PDMA_DISABLE(uuart, u32FuncSel) ((uuart)->PDMACTL &= ~(u32FuncSel)) 418 419 420 void UUART_ClearIntFlag(UUART_T* uuart, uint32_t u32Mask); 421 uint32_t UUART_GetIntFlag(UUART_T* uuart, uint32_t u32Mask); 422 void UUART_Close(UUART_T* uuart); 423 void UUART_DisableInt(UUART_T* uuart, uint32_t u32Mask); 424 void UUART_EnableInt(UUART_T* uuart, uint32_t u32Mask); 425 uint32_t UUART_Open(UUART_T* uuart, uint32_t u32baudrate); 426 uint32_t UUART_Read(UUART_T* uuart, uint8_t pu8RxBuf[], uint32_t u32ReadBytes); 427 uint32_t UUART_SetLine_Config(UUART_T* uuart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits); 428 uint32_t UUART_Write(UUART_T* uuart, uint8_t pu8TxBuf[], uint32_t u32WriteBytes); 429 void UUART_EnableWakeup(UUART_T* uuart, uint32_t u32WakeupMode); 430 void UUART_DisableWakeup(UUART_T* uuart); 431 void UUART_EnableFlowCtrl(UUART_T* uuart); 432 void UUART_DisableFlowCtrl(UUART_T* uuart); 433 434 435 /**@}*/ /* end of group USCI_UART_EXPORTED_FUNCTIONS */ 436 437 /**@}*/ /* end of group USCI_UART_Driver */ 438 439 /**@}*/ /* end of group Standard_Driver */ 440 441 #ifdef __cplusplus 442 } 443 #endif 444 445 #endif /* __USCI_UART_H__ */ 446