]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/T-HEAD_CB2201_CDK/csi/csi_driver/csky/common/usart/dw_usart.c
Introduce a port for T-HEAD CK802. A simple demo for T-HEAD CB2201 is also included.
[freertos] / FreeRTOS / Demo / T-HEAD_CB2201_CDK / csi / csi_driver / csky / common / usart / dw_usart.c
1 /*
2  * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /******************************************************************************
18  * @file     dw_usart.c
19  * @brief    CSI Source File for usart Driver
20  * @version  V1.0
21  * @date     02. June 2017
22  ******************************************************************************/
23 #include <stdbool.h>
24 #include "csi_core.h"
25 #include "drv_usart.h"
26 #include "dw_usart.h"
27 #include "soc.h"
28
29 #define ERR_USART(errno) (CSI_DRV_ERRNO_USART_BASE | errno)
30
31 /*
32  * setting config may be accessed when the USART is not
33  * busy(USR[0]=0) and the DLAB bit(LCR[7]) is set.
34  */
35
36 #define WAIT_USART_IDLE(addr)\
37     do {                       \
38         int32_t timecount = 0;  \
39         while ((addr->USR & USR_UART_BUSY) && (timecount < UART_BUSY_TIMEOUT)) {\
40             timecount++;\
41         }\
42         if (timecount >= UART_BUSY_TIMEOUT) {\
43             return ERR_USART(EDRV_TIMEOUT);\
44         }                                   \
45     } while(0)
46
47 #define USART_NULL_PARAM_CHK(para)                   \
48     do {                                        \
49         if (para == NULL) {                     \
50             return ERR_USART(EDRV_PARAMETER);   \
51         }                                       \
52     } while (0)
53
54 typedef struct {
55     uint32_t base;
56 } dw_usart_priv_t;
57
58 extern int32_t target_usart_init(pin_t tx, pin_t rx, uint32_t *base, uint32_t *irq);
59 extern int32_t target_usart_flowctrl_init(pin_t tx_flow, pin_t rx_flow, uint32_t flag);
60
61 static dw_usart_priv_t usart_instance[CONFIG_USART_NUM];
62
63 static const usart_capabilities_t usart_capabilities = {
64     .asynchronous = 0,          /* supports USART (Asynchronous) mode */
65     .synchronous_master = 0,    /* supports Synchronous Master mode */
66     .synchronous_slave = 0,     /* supports Synchronous Slave mode */
67     .single_wire = 0,           /* supports USART Single-wire mode */
68     .event_tx_complete = 0,     /* Transmit completed event */
69     .event_rx_timeout = 0,      /* Signal receive character timeout event */
70 };
71
72 /**
73   \brief       set the baut drate of usart.
74   \param[in]   addr  usart base to operate.
75   \param[in]   baudrate baud rate
76   \param[in]   apbfreq the frequency of the apb.
77   \return      error code
78 */
79 int32_t csi_usart_config_baudrate(usart_handle_t handle, uint32_t baudrate, uint32_t apbfreq)
80 {
81     USART_NULL_PARAM_CHK(handle);
82     dw_usart_priv_t *usart_priv = handle;
83     dw_usart_reg_t *addr = (dw_usart_reg_t *)(usart_priv->base);
84
85
86 #ifdef CONFIG_CHIP_HOBBIT1_2
87     uint8_t data[16];
88     csi_usart_receive_query(handle, data, 16);
89 #endif
90
91     WAIT_USART_IDLE(addr);
92
93     /* baudrate=(seriak clock freq)/(16*divisor); algorithm :rounding*/
94     uint32_t divisor = ((apbfreq  * 10) / baudrate) >> 4;
95
96     if ((divisor % 10) >= 5) {
97         divisor = (divisor / 10) + 1;
98     } else {
99         divisor = divisor / 10;
100     }
101
102     addr->LCR |= LCR_SET_DLAB;
103     /* DLL and DLH is lower 8-bits and higher 8-bits of divisor.*/
104     addr->DLL = divisor & 0xff;
105     addr->DLH = (divisor >> 8) & 0xff;
106     /*
107      * The DLAB must be cleared after the baudrate is setted
108      * to access other registers.
109      */
110     addr->LCR &= (~LCR_SET_DLAB);
111
112     return 0;
113 }
114
115 /**
116   \brief       config usart mode.
117   \param[in]   handle  usart handle to operate.
118   \param[in]   mode    \ref usart_mode_e
119   \return      error code
120 */
121 int32_t csi_usart_config_mode(usart_handle_t handle, usart_mode_e mode)
122 {
123     if (mode == USART_MODE_ASYNCHRONOUS) {
124         return 0;
125     }
126
127     return ERR_USART(EDRV_USART_MODE);
128 }
129
130 /**
131   \brief       config usart parity.
132   \param[in]   handle  usart handle to operate.
133   \param[in]   parity    \ref usart_parity_e
134   \return      error code
135 */
136 int32_t csi_usart_config_parity(usart_handle_t handle, usart_parity_e parity)
137 {
138     USART_NULL_PARAM_CHK(handle);
139     dw_usart_priv_t *usart_priv = handle;
140     dw_usart_reg_t *addr = (dw_usart_reg_t *)(usart_priv->base);
141
142     WAIT_USART_IDLE(addr);
143
144     switch (parity) {
145         case USART_PARITY_NONE:
146             /*CLear the PEN bit(LCR[3]) to disable parity.*/
147             addr->LCR &= (~LCR_PARITY_ENABLE);
148             break;
149
150         case USART_PARITY_ODD:
151             /* Set PEN and clear EPS(LCR[4]) to set the ODD parity. */
152             addr->LCR |= LCR_PARITY_ENABLE;
153             addr->LCR &= LCR_PARITY_ODD;
154             break;
155
156         case USART_PARITY_EVEN:
157             /* Set PEN and EPS(LCR[4]) to set the EVEN parity.*/
158             addr->LCR |= LCR_PARITY_ENABLE;
159             addr->LCR |= LCR_PARITY_EVEN;
160             break;
161
162         default:
163             return ERR_USART(EDRV_USART_PARITY);
164     }
165
166     return 0;
167 }
168
169 /**
170   \brief       config usart stop bit number.
171   \param[in]   handle  usart handle to operate.
172   \param[in]   stopbits  \ref usart_stop_bits_e
173   \return      error code
174 */
175 int32_t dw_usart_config_stopbits(usart_handle_t handle, usart_stop_bits_e stopbit)
176 {
177     USART_NULL_PARAM_CHK(handle);
178     dw_usart_priv_t *usart_priv = handle;
179     dw_usart_reg_t *addr = (dw_usart_reg_t *)(usart_priv->base);
180
181     WAIT_USART_IDLE(addr);
182
183     switch (stopbit) {
184         case USART_STOP_BITS_1:
185             /* Clear the STOP bit to set 1 stop bit*/
186             addr->LCR &= LCR_STOP_BIT1;
187             break;
188
189         case USART_STOP_BITS_2:
190             /*
191             * If the STOP bit is set "1",we'd gotten 1.5 stop
192             * bits when DLS(LCR[1:0]) is zero, else 2 stop bits.
193             */
194             addr->LCR |= LCR_STOP_BIT2;
195             break;
196
197         default:
198             return ERR_USART(EDRV_USART_STOP_BITS);
199     }
200
201     return 0;
202 }
203
204 /**
205   \brief       config usart data length.
206   \param[in]   handle  usart handle to operate.
207   \param[in]   databits      \ref usart_data_bits_e
208   \return      error code
209 */
210 int32_t csi_usart_config_databits(usart_handle_t handle, usart_data_bits_e databits)
211 {
212     USART_NULL_PARAM_CHK(handle);
213     dw_usart_priv_t *usart_priv = handle;
214     dw_usart_reg_t *addr = (dw_usart_reg_t *)(usart_priv->base);
215
216 #ifdef CONFIG_CHIP_HOBBIT1_2
217     uint8_t data[16];
218     csi_usart_receive_query(handle, data, 16);
219 #endif
220
221     WAIT_USART_IDLE(addr);
222     /* The word size decides by the DLS bits(LCR[1:0]), and the
223      * corresponding relationship between them is:
224      *   DLS   word size
225      *       00 -- 5 bits
226      *       01 -- 6 bits
227      *       10 -- 7 bits
228      *       11 -- 8 bits
229      */
230
231     switch (databits) {
232         case USART_DATA_BITS_5:
233             addr->LCR &= LCR_WORD_SIZE_5;
234             break;
235
236         case USART_DATA_BITS_6:
237             addr->LCR &= 0xfd;
238             addr->LCR |= LCR_WORD_SIZE_6;
239             break;
240
241         case USART_DATA_BITS_7:
242             addr->LCR &= 0xfe;
243             addr->LCR |= LCR_WORD_SIZE_7;
244             break;
245
246         case USART_DATA_BITS_8:
247             addr->LCR |= LCR_WORD_SIZE_8;
248             break;
249
250         default:
251             return ERR_USART(EDRV_USART_DATA_BITS);
252     }
253
254     return 0;
255 }
256
257 /**
258   \brief       get character in query mode.
259   \param[in]   handle  usart handle to operate.
260   \param[in]   the pointer to the received character if return 0.
261   \return      error code
262 */
263 int32_t csi_usart_getchar(usart_handle_t handle, uint8_t *ch)
264 {
265     dw_usart_priv_t *usart_priv = handle;
266     dw_usart_reg_t *addr = (dw_usart_reg_t *)(usart_priv->base);
267
268     while (!(addr->LSR & LSR_DATA_READY));
269
270     *ch = addr->RBR;
271
272     return 0;
273 }
274
275 /**
276   \brief       transmit character in query mode.
277   \param[in]   handle  usart handle to operate.
278   \param[in]   ch  the input character
279   \return      error code
280 */
281 int32_t csi_usart_putchar(usart_handle_t handle, uint8_t ch)
282 {
283     dw_usart_priv_t *usart_priv = handle;
284     dw_usart_reg_t *addr = (dw_usart_reg_t *)(usart_priv->base);
285
286     while ((!(addr->LSR & DW_LSR_TRANS_EMPTY)));
287
288     addr->THR = ch;
289
290     return 0;
291 }
292
293 /**
294   \brief       the interrupt service function.
295   \param[in]   index of usart instance.
296 */
297 void dw_usart_irqhandler(int32_t idx)
298 {
299     (void)idx;
300 }
301
302 /**
303   \brief       Get driver capabilities.
304   \param[in]   handle  usart handle to operate.
305   \return      \ref usart_capabilities_t
306 */
307 usart_capabilities_t csi_usart_get_capabilities(usart_handle_t handle)
308 {
309     return usart_capabilities;
310 }
311
312 /**
313   \brief       Initialize USART Interface. 1. Initializes the resources needed for the USART interface 2.registers event callback function
314   \param[in]   usart pin of tx
315   \param[in]   usart pin of rx
316   \param[in]   cb_event  Pointer to \ref usart_event_cb_t
317   \return      return usart handle if success
318 */
319 usart_handle_t csi_usart_initialize(pin_t tx, pin_t rx, usart_event_cb_t cb_event, void *cb_arg)
320 {
321     uint32_t base = 0u;
322     uint32_t irq = 0u;
323     int32_t idx = target_usart_init(tx, rx, &base, &irq);
324
325     if (idx < 0 || idx >= CONFIG_USART_NUM) {
326         return NULL;
327     }
328
329     dw_usart_priv_t *usart_priv = &usart_instance[idx];
330     usart_priv->base = base;
331
332     dw_usart_reg_t *addr = (dw_usart_reg_t *)(usart_priv->base);
333     /* FIFO enable */
334     addr->FCR = DW_FCR_FIFOE | DW_FCR_RT_FIFO_HALF;
335
336     return usart_priv;
337 }
338
339 /**
340   \brief       De-initialize UART Interface. stops operation and releases the software resources used by the interface
341   \param[in]   handle  usart handle to operate.
342   \return      error code
343 */
344 int32_t csi_usart_uninitialize(usart_handle_t handle)
345 {
346     USART_NULL_PARAM_CHK(handle);
347     return 0;
348 }
349
350 /**
351   \brief       config usart mode.
352   \param[in]   handle  usart handle to operate.
353   \param[in]   sysclk    configured system clock.
354   \param[in]   baud      baud rate
355   \param[in]   mode      \ref usart_mode_e
356   \param[in]   parity    \ref usart_parity_e
357   \param[in]   stopbits  \ref usart_stop_bits_e
358   \param[in]   bits      \ref usart_data_bits_e
359   \return      error code
360 */
361 int32_t csi_usart_config(usart_handle_t handle,
362                          uint32_t sysclk,
363                          uint32_t baud,
364                          usart_mode_e mode,
365                          usart_parity_e parity,
366                          usart_stop_bits_e stopbits,
367                          usart_data_bits_e bits)
368 {
369     int32_t ret;
370
371     /* control the data_bit of the usart*/
372     ret = csi_usart_config_baudrate(handle, baud, sysclk);
373
374     if (ret < 0) {
375         return ret;
376     }
377
378     /* control mode of the usart*/
379     ret = csi_usart_config_mode(handle, mode);
380
381     if (ret < 0) {
382         return ret;
383     }
384
385     /* control the parity of the usart*/
386     ret = csi_usart_config_parity(handle, parity);
387
388     if (ret < 0) {
389         return ret;
390     }
391
392     /* control the stopbit of the usart*/
393     ret = dw_usart_config_stopbits(handle, stopbits);
394
395     if (ret < 0) {
396         return ret;
397     }
398
399     ret = csi_usart_config_databits(handle, bits);
400
401     if (ret < 0) {
402         return ret;
403     }
404
405     return 0;
406 }
407
408 /**
409   \brief       config usart default tx value. used in syn mode
410   \param[in]   handle  usart handle to operate.
411   \param[in]   value  default tx value
412   \return      error code
413 */
414 int32_t csi_usart_set_default_tx_value(usart_handle_t handle, uint32_t value)
415 {
416     USART_NULL_PARAM_CHK(handle);
417     return ERR_USART(EDRV_UNSUPPORTED);
418 }
419
420 /**
421   \brief       Start sending data to UART transmitter,(received data is ignored).
422                The function is non-blocking,UART_EVENT_TRANSFER_COMPLETE is signaled when transfer completes.
423                csi_usart_get_status can indicates if transmission is still in progress or pending
424   \param[in]   handle  usart handle to operate.
425   \param[in]   data  Pointer to buffer with data to send to UART transmitter. data_type is : uint8_t for 1..8 data bits, uint16_t for 9..16 data bits,uint32_t for 17..32 data bits,
426   \param[in]   num   Number of data items to send
427   \return      error code
428 */
429 int32_t csi_usart_send(usart_handle_t handle, const void *data, uint32_t num)
430 {
431     USART_NULL_PARAM_CHK(handle);
432     USART_NULL_PARAM_CHK(data);
433
434     return ERR_USART(EDRV_UNSUPPORTED);
435 }
436
437 /**
438   \brief       Abort Send data to UART transmitter
439   \param[in]   handle  usart handle to operate.
440   \return      error code
441 */
442 int32_t csi_usart_abort_send(usart_handle_t handle)
443 {
444     USART_NULL_PARAM_CHK(handle);
445     return ERR_USART(EDRV_UNSUPPORTED);
446 }
447
448 /**
449   \brief       Start receiving data from UART receiver.transmits the default value as specified by csi_usart_set_default_tx_value
450   \param[in]   handle  usart handle to operate.
451   \param[out]  data  Pointer to buffer for data to receive from UART receiver
452   \param[in]   num   Number of data items to receive
453   \return      error code
454 */
455 int32_t csi_usart_receive(usart_handle_t handle, void *data, uint32_t num)
456 {
457     return ERR_USART(EDRV_UNSUPPORTED);
458
459 }
460
461 /**
462   \brief       query data from UART receiver FIFO.
463   \param[in]   handle  usart handle to operate.
464   \param[out]  data  Pointer to buffer for data to receive from UART receiver
465   \param[in]   num   Number of data items to receive
466   \return      receive fifo data num
467 */
468 int32_t csi_usart_receive_query(usart_handle_t handle, void *data, uint32_t num)
469 {
470     return ERR_USART(EDRV_UNSUPPORTED);
471
472 }
473
474 /**
475   \brief       Abort Receive data from UART receiver
476   \param[in]   handle  usart handle to operate.
477   \return      error code
478 */
479 int32_t csi_usart_abort_receive(usart_handle_t handle)
480 {
481     USART_NULL_PARAM_CHK(handle);
482     return ERR_USART(EDRV_UNSUPPORTED);
483 }
484
485 /**
486   \brief       Start sending/receiving data to/from UART transmitter/receiver.
487   \param[in]   handle  usart handle to operate.
488   \param[in]   data_out  Pointer to buffer with data to send to USART transmitter
489   \param[out]  data_in   Pointer to buffer for data to receive from USART receiver
490   \param[in]   num       Number of data items to transfer
491   \return      error code
492 */
493 int32_t csi_usart_transfer(usart_handle_t handle, const void *data_out, void *data_in, uint32_t num)
494 {
495     USART_NULL_PARAM_CHK(handle);
496     return ERR_USART(EDRV_UNSUPPORTED);
497 }
498
499 /**
500   \brief       abort sending/receiving data to/from USART transmitter/receiver.
501   \param[in]   handle  usart handle to operate.
502   \return      error code
503 */
504 int32_t csi_usart_abort_transfer(usart_handle_t handle)
505 {
506     USART_NULL_PARAM_CHK(handle);
507     return ERR_USART(EDRV_UNSUPPORTED);
508 }
509
510 /**
511   \brief       Get USART status.
512   \param[in]   handle  usart handle to operate.
513   \return      USART status \ref usart_status_t
514 */
515 usart_status_t csi_usart_get_status(usart_handle_t handle)
516 {
517     usart_status_t status = {0};
518     return status;
519 }
520
521 /**
522   \brief       control the transmit.
523   \param[in]   handle  usart handle to operate.
524   \param[in]   1 - enable the transmitter. 0 - disable the transmitter
525   \return      error code
526 */
527 int32_t csi_usart_control_tx(usart_handle_t handle, uint32_t enable)
528 {
529     USART_NULL_PARAM_CHK(handle);
530     return 0;
531 }
532
533 /**
534   \brief       control the receive.
535   \param[in]   handle  usart handle to operate.
536   \param[in]   1 - enable the receiver. 0 - disable the receiver
537   \return      error code
538 */
539 int32_t csi_usart_control_rx(usart_handle_t handle, uint32_t enable)
540 {
541     USART_NULL_PARAM_CHK(handle);
542     return 0;
543 }
544
545 /**
546   \brief       control the break.
547   \param[in]   handle  usart handle to operate.
548   \param[in]   1- Enable continuous Break transmission,0 - disable continuous Break transmission
549   \return      error code
550 */
551 int32_t csi_usart_control_break(usart_handle_t handle, uint32_t enable)
552 {
553     USART_NULL_PARAM_CHK(handle);
554     return ERR_USART(EDRV_UNSUPPORTED);
555 }
556
557 /**
558   \brief       flush receive/send data.
559   \param[in]   handle usart handle to operate.
560   \param[in]   type \ref usart_flush_type_e.
561   \return      error code
562 */
563 int32_t csi_usart_flush(usart_handle_t handle, usart_flush_type_e type)
564 {
565     USART_NULL_PARAM_CHK(handle);
566
567     dw_usart_priv_t *usart_priv = handle;
568     dw_usart_reg_t *addr = (dw_usart_reg_t *)(usart_priv->base);
569
570     if (type == USART_FLUSH_WRITE) {
571         addr->FCR |= DW_FCR_XFIFOR;
572
573         while (addr->FCR & DW_FCR_XFIFOR);
574     } else if (type == USART_FLUSH_READ) {
575         addr->FCR |= DW_FCR_RFIFOR;
576
577         while (addr->FCR & DW_FCR_RFIFOR);
578     } else {
579         return ERR_USART(EDRV_PARAMETER);
580     }
581
582     return 0;
583 }
584
585 /**
586   \brief       control interrupt on/off.
587   \param[in]   handle usart handle to operate.
588   \param[in]   type \ref usart_intr_type_e.
589   \param[in]   flag 0-OFF, 1-ON.
590   \return      error code
591 */
592 int32_t csi_usart_interrupt_on_off(usart_handle_t handle, usart_intr_type_e type, int flag)
593 {
594     return ERR_USART(EDRV_UNSUPPORTED);
595 }
596
597 /**
598   \brief       Get usart send data count.
599   \param[in]   handle  usart handle to operate.
600   \return      number of data bytes transferred
601 */
602 uint32_t csi_usart_get_tx_count(usart_handle_t handle)
603 {
604     return ERR_USART(EDRV_UNSUPPORTED);
605 }
606
607 /**
608   \brief       Get usart receive data count.
609   \param[in]   handle  usart handle to operate.
610   \return      number of data bytes transferred
611 */
612 uint32_t csi_usart_get_rx_count(usart_handle_t handle)
613 {
614     return ERR_USART(EDRV_UNSUPPORTED);
615 }
616
617 /**
618   \brief       control usart power.
619   \param[in]   handle  usart handle to operate.
620   \param[in]   state   power state.\ref csi_power_stat_e.
621   \return      error code
622 */
623 int32_t csi_usart_power_control(usart_handle_t handle, csi_power_stat_e state)
624 {
625     return ERR_USART(EDRV_UNSUPPORTED);
626 }
627
628 /**
629   \brief       config usart flow control type.
630   \param[in]   handle  usart handle to operate.
631   \param[in]   flowctrl_type   flow control type.\ref usart_flowctrl_type_e.
632   \param[in]   tx_flow  The TX flow pin name
633   \param[in]   rx_flow  The RX flow pin name
634   \return      error code
635 */
636 int32_t csi_usart_config_flowctrl(usart_handle_t handle,
637                                   usart_flowctrl_type_e flowctrl_type,
638                                   pin_t tx_flow, pin_t rx_flow)
639 {
640     USART_NULL_PARAM_CHK(handle);
641     dw_usart_priv_t *usart_priv = handle;
642     dw_usart_reg_t *addr = (dw_usart_reg_t *)(usart_priv->base);
643     int32_t ret;
644
645     switch (flowctrl_type) {
646         case USART_FLOWCTRL_CTS:
647             return ERR_USART(EDRV_UNSUPPORTED);
648
649         case USART_FLOWCTRL_RTS:
650             return ERR_USART(EDRV_UNSUPPORTED);
651
652         case USART_FLOWCTRL_CTS_RTS:
653             ret = target_usart_flowctrl_init(tx_flow, rx_flow, 1);
654
655             if (ret < 0) {
656                 return ERR_USART(EDRV_PARAMETER);
657             }
658
659             WAIT_USART_IDLE(addr);
660             addr->MCR |= DW_MCR_AFCE | DW_MCR_RTS;
661             break;
662
663         case USART_FLOWCTRL_NONE:
664             ret = target_usart_flowctrl_init(tx_flow, rx_flow, 0);
665
666             if (ret < 0) {
667                 return ERR_USART(EDRV_PARAMETER);
668             }
669
670             WAIT_USART_IDLE(addr);
671             addr->MCR = 0;
672             break;
673
674         default:
675             return ERR_USART(EDRV_UNSUPPORTED);
676     }
677
678     return 0;
679 }
680
681 /**
682   \brief       usart modem control.
683   \param[in]   handle  usart handle to operate.
684   \param[in]   modem_ctrl   modem control action.\ref usart_modem_ctrl_e.
685   \return      error code
686 */
687 int32_t csi_usart_modem_ctrl(usart_handle_t handle, usart_modem_ctrl_e modem_ctrl)
688 {
689     return ERR_USART(EDRV_UNSUPPORTED);
690 }
691
692 /**
693   \brief       get usart modem status.
694   \param[in]   handle  usart handle to operate.
695   \param[in]   modem_ctrl   modem control action.\ref usart_modem_ctrl_e.
696   \return      modem status.\ref usart_modem_stat_t.
697 */
698 usart_modem_stat_t csi_usart_get_modem_stat(usart_handle_t handle)
699 {
700     usart_modem_stat_t modem_stat = {0};
701     return modem_stat;
702 }
703
704 /**
705   \brief       config usart clock Polarity and Phase.
706   \param[in]   handle  usart handle to operate.
707   \param[in]   cpol    Clock Polarity.\ref usart_cpol_e.
708   \param[in]   cpha    Clock Phase.\ref usart_cpha_e.
709   \return      error code
710 */
711 int32_t csi_usart_config_clock(usart_handle_t handle, usart_cpol_e cpol, usart_cpha_e cpha)
712 {
713     return ERR_USART(EDRV_UNSUPPORTED);
714 }
715
716 /**
717   \brief       config usart guard time.
718   \param[in]   handle  usart handle to operate.
719   \param[in]   num_of_bits   guard time in number of bit periods.
720   \return      error code
721 */
722 int32_t csi_usart_config_guard_time(usart_handle_t handle, uint32_t num_of_bits)
723 {
724     return ERR_USART(EDRV_UNSUPPORTED);
725 }
726
727 /**
728   \brief       check if usart is readable(data received).
729   \param[in]   handle  usart handle to operate.
730   \return      1 - a character can be read, 0 if nothing to read ,negative for error code
731 */
732 int32_t csi_usart_readable(usart_handle_t handle)
733 {
734     USART_NULL_PARAM_CHK(handle);
735     dw_usart_priv_t *usart_priv = handle;
736     dw_usart_reg_t *addr = (dw_usart_reg_t *)(usart_priv->base);
737
738     if (addr->LSR & LSR_DATA_READY) {
739         return 1;
740     } else {
741         return 0;
742     }
743 }
744
745 /**
746   \brief       check if usart is writable(free for data sending).
747   \param[in]   handle  usart handle to operate.
748   \return      1 - a character can be written, 0 -  cannot be written ,negative for error code
749 */
750 int32_t csi_usart_writable(usart_handle_t handle)
751 {
752     USART_NULL_PARAM_CHK(handle);
753     dw_usart_priv_t *usart_priv = handle;
754     dw_usart_reg_t *addr = (dw_usart_reg_t *)(usart_priv->base);
755
756     if (addr->LSR & DW_LSR_TRANS_EMPTY) {
757         return 1;
758     } else {
759         return 0;
760     }
761 }