1 /* 2 * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @file wm_uart.h 18 * 19 * @brief uart Driver Module 20 * 21 * @author dave 22 * 23 * Copyright (c) 2015 Winner Microelectronics Co., Ltd. 24 */ 25 #ifndef WM_UART_H 26 #define WM_UART_H 27 #include "list.h" 28 #include "wm_type_def.h" 29 #include "wm_osal.h" 30 31 #define TLS_UART_RX_BUF_SIZE 4096 32 #define TLS_UART_TX_BUF_SIZE 4096 33 #define WAKEUP_CHARS 256 34 35 #define MBOX_MSG_UART_RX 1 36 #define MBOX_MSG_UART_TX 2 37 38 /** baud rate definition */ 39 #define UART_BAUDRATE_B600 600 40 #define UART_BAUDRATE_B1200 1200 41 #define UART_BAUDRATE_B1800 1800 42 #define UART_BAUDRATE_B2400 2400 43 #define UART_BAUDRATE_B4800 4800 44 #define UART_BAUDRATE_B9600 9600 45 #define UART_BAUDRATE_B19200 19200 46 #define UART_BAUDRATE_B38400 38400 47 #define UART_BAUDRATE_B57600 57600 48 #define UART_BAUDRATE_B115200 115200 49 #define UART_BAUDRATE_B230400 230400 50 #define UART_BAUDRATE_B460800 460800 51 #define UART_BAUDRATE_B921600 921600 52 #define UART_BAUDRATE_B1000000 1000000 53 #define UART_BAUDRATE_B1250000 1250000 54 #define UART_BAUDRATE_B1500000 1500000 55 #define UART_BAUDRATE_B2000000 2000000 56 57 #define UART_RX_INT_FLAG (UIS_RX_FIFO | UIS_RX_FIFO_TIMEOUT | UIS_BREAK | \ 58 UIS_OVERRUN | UIS_FRM_ERR | UIS_PARITY_ERR) 59 #define UART_RX_ERR_INT_FLAG (UIS_BREAK | UIS_FRM_ERR | \ 60 UIS_PARITY_ERR) 61 62 #define UART_TX_INT_FLAG (UIS_TX_FIFO | UIS_TX_FIFO_EMPTY) 63 64 /** return count in buffer. */ 65 #define CIRC_CNT(head, tail, size) (((head) - (tail)) & ((size)-1)) 66 67 /** Return space available, 0..size-1. We always leave one free char 68 as a completely full buffer has head == tail, which is the same as 69 empty. */ 70 #define CIRC_SPACE(head, tail, size) CIRC_CNT((tail), ((head)+1), (size)) 71 72 /** Return count up to the end of the buffer. Carefully avoid 73 accessing head and tail more than once, so they can change 74 underneath us without returning inconsistent results. */ 75 #define CIRC_CNT_TO_END(head, tail, size) \ 76 ({int end = (size) - (tail); \ 77 int n = ((head) + end) & ((size)-1); \ 78 n < end ? n : end;}) \ 79 80 /** Return space available up to the end of the buffer. */ 81 #define CIRC_SPACE_TO_END(head, tail, size) \ 82 ({int end = (size) - 1 - (head); \ 83 int n = (end + (tail)) & ((size)-1); \ 84 n <= end ? n : end+1;}) \ 85 86 #define CIRC_SPACE_TO_END_FULL(head, tail, size) \ 87 ({int end = (size) - 1 - (head); \ 88 int n = (end + (tail)) & ((size)-1); \ 89 n < end ? n : end+1;}) \ 90 91 #define uart_circ_empty(circ) ((circ)->head == (circ)->tail) 92 #define uart_circ_chars_pending(circ) \ 93 (CIRC_CNT((circ)->head, (circ)->tail, TLS_UART_TX_BUF_SIZE)) 94 95 /** 96 * @struct tls_uart_baud_rate baudrate define 97 */ 98 struct tls_uart_baud_rate { 99 u32 baud_rate; 100 u16 ubdiv; 101 u16 ubdiv_frac; 102 }; 103 104 /** 105 * @enum uart number enum 106 */ 107 enum { 108 TLS_UART_0 = 0, 109 TLS_UART_1 = 1, 110 TLS_UART_2 = 2, 111 TLS_UART_3 = 3, 112 TLS_UART_4 = 4, 113 TLS_UART_5 = 5, 114 TLS_UART_MAX = 6, 115 }; 116 117 /** 118 * @typedef enum TLS_UART_PMODE Parity Mode 119 */ 120 typedef enum TLS_UART_PMODE { 121 TLS_UART_PMODE_DISABLED = 0, /**< No Parity */ 122 TLS_UART_PMODE_ODD = 1, /**< Odd Parity */ 123 TLS_UART_PMODE_EVEN = 2, /**< Even Parity */ 124 TLS_UART_PMODE_MARK = 3, /**< The parity bit is always 1. */ 125 TLS_UART_PMODE_SPACE = 4, /**< The parity bit is always 0. */ 126 } TLS_UART_PMODE_T; 127 128 /** 129 * @typedef enum TLS_UART_CHSIZE Character Size 130 */ 131 typedef enum TLS_UART_CHSIZE { 132 TLS_UART_CHSIZE_5BIT = (0x00 << 0), /**< Character size: 5 bit */ 133 TLS_UART_CHSIZE_6BIT = (0x01 << 0), /**< Character size: 6 bit */ 134 TLS_UART_CHSIZE_7BIT = (0x02 << 0), /**< Character size: 7 bit */ 135 TLS_UART_CHSIZE_8BIT = (0x03 << 0), /**< Character size: 8 bit */ 136 } TLS_UART_CHSIZE_T; 137 138 /** 139 * @typedef enum TLS_UART_FLOW_CTRL_MODE flow control mode 140 */ 141 typedef enum TLS_UART_FLOW_CTRL_MODE { 142 TLS_UART_FLOW_CTRL_NONE, 143 TLS_UART_FLOW_CTRL_HARDWARE, 144 } TLS_UART_FLOW_CTRL_MODE_T; 145 146 /** 147 * @typedef enum TLS_UART_RX_FLOW_CTRL_FLAG flow control rx flag 148 */ 149 typedef enum TLS_UART_RX_FLOW_CTRL_FLAG { 150 TLS_UART_RX_DISABLE, 151 TLS_UART_RX_ENABLE, 152 } TLS_UART_RX_FLOW_CTRL_FLAG_T; 153 154 /** 155 * @typedef enum TLS_UART_STOPBITS 156 */ 157 typedef enum TLS_UART_STOPBITS { 158 TLS_UART_ONE_STOPBITS, 159 TLS_UART_TWO_STOPBITS, 160 } TLS_UART_STOPBITS_T; 161 162 /** 163 * @typedef enum TLS_UART_STATUS 164 */ 165 typedef enum TLS_UART_STATUS { 166 TLS_UART_STATUS_OK, 167 TLS_UART_STATUS_ERROR, 168 } TLS_UART_STATUS_T; 169 170 /** 171 * @typedef enum TLS_UART_MODE operation mode 172 */ 173 typedef enum TLS_UART_MODE { 174 TLS_UART_MODE_POLL, /**< uart operation mode: poll */ 175 TLS_UART_MODE_INT, /**< uart operation mode: interrupt mode */ 176 } TLS_UART_MODE_T; 177 178 /** 179 * @struct tls_uart_icount 180 */ 181 struct tls_uart_icount { 182 u32 cts; 183 u32 dsr; 184 u32 rng; 185 u32 dcd; 186 u32 rx; 187 u32 tx; 188 u32 frame; 189 u32 overrun; 190 u32 parity; 191 u32 brk; 192 u32 buf_overrun; 193 }; 194 195 /** 196 * @typedef struct tls_uart_options 197 */ 198 typedef struct tls_uart_options { 199 u32 baudrate; /**< Set baud rate of the UART */ 200 201 TLS_UART_CHSIZE_T charlength; /**< Number of bits to transmit as a character (5 to 8). */ 202 203 TLS_UART_PMODE_T paritytype; /**< Parity type */ 204 205 TLS_UART_FLOW_CTRL_MODE_T flow_ctrl; /**< Flow control type */ 206 207 TLS_UART_STOPBITS_T stopbits; /**< Number of stop bits */ 208 } tls_uart_options_t; 209 210 /** 211 * @typedef struct tls_uart_circ_buf 212 */ 213 typedef struct tls_uart_circ_buf { 214 volatile u8 *buf; 215 volatile u32 head; 216 volatile u32 tail; 217 } tls_uart_circ_buf_t; 218 219 #if TLS_CONFIG_CMD_NET_USE_LIST_FTR 220 /** 221 * @typedef struct tls_uart_net_buf 222 */ 223 typedef struct tls_uart_net_buf { 224 struct dl_list list; 225 char *buf; 226 void *pbuf; 227 u16 buflen; 228 u16 offset; 229 } tls_uart_net_buf_t; 230 231 typedef struct tls_uart_net_msg { 232 struct dl_list tx_msg_pending_list; 233 } tls_uart_net_msg_t; 234 #endif 235 236 /** 237 * @typedef struct TLS_UART_REGS 238 */ 239 typedef struct TLS_UART_REGS { 240 u32 UR_LC; /**< line control register */ 241 u32 UR_FC; /**< flow control register */ 242 u32 UR_DMAC; /**< dma control register */ 243 u32 UR_FIFOC; /**< fifo control register */ 244 u32 UR_BD; /**< baud rate register */ 245 u32 UR_INTM; /**< interrupt mask register */ 246 u32 UR_INTS; /**< interrupt source register */ 247 u32 UR_FIFOS; /**< fifo status register */ 248 u32 UR_TXW; /**< tx windows register */ 249 u32 UR_RES0; 250 u32 UR_RES1; 251 u32 UR_RES2; 252 u32 UR_RXW; /**< rx windows register */ 253 } TLS_UART_REGS_T; 254 255 /** 256 * @typedef struct tls_uart_port 257 */ 258 typedef struct tls_uart_port { 259 u32 uart_no; /**< uart number: 0 or 1 */ 260 261 u32 uart_irq_no; /**< uart interrupt number */ 262 263 u32 plus_char_cnt; 264 265 TLS_UART_MODE_T uart_mode; /**< uart work mode: interrupt mode or poll mode */ 266 267 struct tls_uart_options opts; /**< uart config parameters */ 268 269 int fcStatus; /**< flow ctrl status,0 closed ,1 opened */ 270 271 enum TLS_UART_RX_FLOW_CTRL_FLAG rxstatus; 272 273 u32 tx_fifofull; /**< uart tx fifo trigger level */ 274 275 TLS_UART_REGS_T volatile *regs; /**< uart registers struct pointer */ 276 277 struct tls_uart_icount icount; /**< uart statistics information */ 278 279 struct tls_uart_circ_buf recv; /**< uart ring buffer */ 280 281 struct dl_list tx_msg_pending_list; 282 283 struct dl_list tx_msg_to_be_freed_list; 284 285 u8 hw_stopped; 286 287 tls_os_sem_t *tx_sem; 288 289 char *buf_ptr; 290 291 u16 buf_len; 292 293 s16(*rx_callback) (u16 len, void* priv_data); 294 295 s16(*tx_callback) (struct tls_uart_port *port); 296 s16(*tx_sent_callback) (struct tls_uart_port *port); 297 298 bool tx_dma_on; 299 bool rx_dma_on; 300 301 void *priv_data; 302 } tls_uart_port_t; 303 304 /** 305 * @typedef struct tls_uart_tx_msg 306 */ 307 typedef struct tls_uart_tx_msg { 308 struct dl_list list; 309 char *buf; 310 u16 buflen; 311 u16 offset; 312 void (*finish_callback) (void *arg); 313 void *callback_arg; 314 } tls_uart_tx_msg_t; 315 316 /** 317 * @defgroup Driver_APIs Driver APIs 318 * @brief Driver APIs 319 */ 320 321 /** 322 * @addtogroup Driver_APIs 323 * @{ 324 */ 325 326 /** 327 * @defgroup UART_Driver_APIs UART Driver APIs 328 * @brief UART driver APIs 329 */ 330 331 /** 332 * @addtogroup UART_Driver_APIs 333 * @{ 334 */ 335 336 /** 337 * @brief This function is used to initial uart port. 338 * 339 * @param[in] uart_no: is the uart number. 340 * - \ref TLS_UART_0 TLS_UART_1 TLS_UART_2 TLS_UART_3 TLS_UART_4 TLS_UART_5 341 * @param[in] opts: is the uart setting options,if this param is NULL,this function will use the default options. 342 * @param[in] modeChoose:; 343 * choose uart2 mode or 7816 mode when uart_no is TLS_UART_2, 0 for uart2 mode and 1 for 7816 mode. 344 * 345 * @retval 346 * - \ref WM_SUCCESS 347 * - \ref WM_FAILED 348 * 349 * @note When the system is initialized, the function has been called, so users can not call the function. 350 */ 351 int tls_uart_port_init(u16 uart_no, tls_uart_options_t *opts, u8 modeChoose); 352 353 /** 354 * @brief This function is used to register uart rx interrupt. 355 * 356 * @param[in] uart_no TLS_UART_0 or TLS_UART_1 357 * @param[in] rx_callback is the uart rx interrupt call back function. 358 * 359 * @return None 360 * 361 * @note None 362 */ 363 void tls_uart_rx_callback_register(u16 uart_no, s16(*rx_callback) (u16 len, void* user_data), void* user_data); 364 365 void tls_uart_rx_byte_callback_flag(u16 uart_no, u8 flag); 366 367 /** 368 * @brief This function is used to register uart tx interrupt. 369 * 370 * @param[in] uart_no: is the uart numer. 371 * @param[in] callback: is the uart tx interrupt call back function. 372 * 373 * @retval 374 */ 375 void tls_uart_tx_callback_register(u16 uart_no, s16(*tx_callback) (struct tls_uart_port *port)); 376 377 /** 378 * @brief This function is used to copy circular buffer data to user buffer. 379 * 380 * @param[in] uart_no is the uart numer 381 * @param[in] buf is the user buffer 382 * @param[in] readsize is the user read size 383 * 384 * @retval copy data size 385 * 386 * @note None 387 */ 388 int tls_uart_read(u16 uart_no, u8 *buf, u16 readsize); 389 390 /** 391 * @brief This function is used to check the available data in the cache buffer. 392 * 393 * @param[in] uart_no is the uart numer 394 * @param[in] readsize is the user read size 395 * 396 * @retval if the cache buffer size is greater or equals to readsize , then return readsize; otherwise return 0; 397 * 398 * @note None 399 */ 400 401 int tls_uart_try_read(u16 uart_no, int32_t read_size); 402 403 /** 404 * @brief This function is used to transfer data synchronously. 405 * 406 * @param[in] uart_no is the uart number 407 * @param[in] buf is a buf for saving user data 408 * @param[in] writesize is the user data length 409 * 410 * @retval WM_SUCCESS tx success 411 * @retval WM_FAILED tx failed 412 * 413 * @note None 414 */ 415 int tls_uart_write(u16 uart_no, char *buf, u16 writesize); 416 417 /** 418 * @brief This function is used to transfer data with DMA. 419 * 420 * @param[in] buf is a buf for saving user data 421 * @param[in] writesize is the user data length 422 * @param[in] cmpl_callback function point,when the transfer is completed, the function will be called. 423 * 424 * @retval WM_SUCCESS success 425 * @retval WM_FAILED failed 426 * 427 * @note Only uart1 support DMA transfer. 428 */ 429 int tls_uart_dma_write(char *buf, u16 writesize, void (*cmpl_callback) (void *p), u16 uart_no); 430 431 /** 432 * @brief This function is used to set uart parity. 433 * 434 * @param[in] uart_no is the uart number 435 * @param[in] paritytype is a parity type defined in TLS_UART_PMODE_T 436 * 437 * @retval WM_SUCCESS if setting success 438 * @retval WM_FAILED if setting fail 439 * 440 * @note None 441 */ 442 int tls_uart_set_parity(u16 uart_no, TLS_UART_PMODE_T paritytype); 443 444 /** 445 * @brief This function is used to set uart baudrate. 446 * 447 * @param[in] uart_no is the uart number 448 * @param[in] baudrate is the baudrate user want used,the unit is HZ. 449 * 450 * @retval WM_SUCCESS if setting success 451 * @retval WM_FAILED if setting fail 452 * 453 * @note None 454 */ 455 int tls_uart_set_baud_rate(u16 uart_no, u32 baudrate); 456 457 /** 458 * @brief This function is used to set uart stop bits. 459 * 460 * @param[in] uart_no is the uart number 461 * @param[in] stopbits is a stop bit type defined in TLS_UART_STOPBITS_T 462 * 463 * @retval WM_SUCCESS if setting success 464 * @retval WM_FAILED if setting fail 465 * 466 * @note None 467 */ 468 int tls_uart_set_stop_bits(u16 uart_no, TLS_UART_STOPBITS_T stopbits); 469 470 /** 471 * @} 472 */ 473 474 /** 475 * @} 476 */ 477 void tls_uart_push(int uart_no, u8* data, int length); 478 479 /** 480 * @brief This function is used to transfer data asynchronously. 481 * 482 * @param[in] uart_no is the uart number 483 * @param[in] buf is a buf for saving user data 484 * @param[in] writesize is the user data length 485 * 486 * @retval WM_SUCCESS tx success 487 * @retval WM_FAILED tx failed 488 * 489 * @note None 490 */ 491 492 int tls_uart_write_async(u16 uart_no, char *buf, u16 writesize); 493 494 /** 495 * @brief This function is used to register uart tx sent callback function. 496 * 497 * @param[in] uart_no: is the uart numer. 498 * @param[in] callback: is the uart tx sent out call back function. 499 * 500 * @retval 501 */ 502 503 void tls_uart_tx_sent_callback_register(u16 uart_no, s16(*tx_callback) (struct tls_uart_port *port)); 504 505 #endif /* WM_UART_H */