1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2020-08-20     zx.chen      first implementation
9  */
10 
11 #include <rtthread.h>
12 
13 #ifdef RT_USING_CONSOLE
14 
15 #include <rthw.h>
16 #include <rtdevice.h>
17 
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdbool.h>
22 
23 #include "board.h"
24 #include "soc.h"
25 #include "ck_usart.h"
26 #include "drv_usart.h"
27 #include "pin_name.h"
28 
29 
30 
31 __attribute__((isr)) void ck_usart0_irqhandler(void);
32 
33 struct
34 {
35     uint32_t base;
36     uint32_t irq;
37     void *handler;
38 }
39 const sg_usart_config[CONFIG_USART_NUM] =
40 {
41     {CSKY_UART_BASE, UART_IRQn, ck_usart0_irqhandler},
42 };
43 
44 
target_usart_init(int32_t idx,uint32_t * base,uint32_t * irq,void ** handler)45 int32_t target_usart_init(int32_t idx, uint32_t *base, uint32_t *irq, void **handler)
46 {
47     if (idx >= CONFIG_USART_NUM)
48     {
49         return -1;
50     }
51 
52     if (base != NULL)
53     {
54         *base = sg_usart_config[idx].base;
55     }
56 
57     if (irq != NULL)
58     {
59         *irq = sg_usart_config[idx].irq;
60     }
61 
62     if (handler != NULL)
63     {
64         *handler = sg_usart_config[idx].handler;
65     }
66 
67     return idx;
68 }
69 
70 
71 #ifdef RT_USING_UART1
72 #define UART_TXD1     PA10_TXD1_PWM1_XX_SIROUT1
73 #define UART_RXD1     PA11_RXD1_PWM2_XX_SIRIN1
74 
75 static  usart_handle_t uart1_handle;
76 static struct rt_serial_device  serial1;
77 
ck_usart0_irqhandler(void)78 __attribute__((isr)) void ck_usart0_irqhandler(void)
79 {
80     rt_hw_serial_isr(&serial1,RT_SERIAL_EVENT_RX_IND);
81 }
82 #endif
83 
84 /*
85  * UART interface
86  */
uart_configure(struct rt_serial_device * serial,struct serial_configure * cfg)87 static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
88 {
89     int ret;
90     usart_handle_t uart;
91 
92     uint32_t bauds;
93     usart_mode_e mode;
94     usart_parity_e parity;
95     usart_stop_bits_e stopbits;
96     usart_data_bits_e databits;
97 
98     RT_ASSERT(serial != RT_NULL);
99     uart = (usart_handle_t)serial->parent.user_data;
100     RT_ASSERT(uart != RT_NULL);
101 
102     /* set baudrate parity...*/
103     bauds = cfg->baud_rate;
104     mode = USART_MODE_ASYNCHRONOUS;
105 
106     if (cfg->parity == PARITY_EVEN)
107         parity = USART_PARITY_EVEN;
108     else if (cfg->parity == PARITY_ODD)
109         parity = USART_PARITY_ODD;
110     else
111         parity = USART_PARITY_NONE;
112 
113     stopbits = USART_STOP_BITS_1 ;
114     databits = USART_DATA_BITS_8;
115 
116     ret = csi_usart_config(uart, bauds, USART_MODE_ASYNCHRONOUS, parity, stopbits, databits);
117 
118     if (ret < 0)
119     {
120         return -RT_ERROR;
121     }
122 
123     return RT_EOK;
124 }
125 
uart_control(struct rt_serial_device * serial,int cmd,void * arg)126 static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
127 {
128     usart_handle_t uart;
129 
130     RT_ASSERT(serial != RT_NULL);
131     uart = (usart_handle_t)serial->parent.user_data;
132     RT_ASSERT(uart != RT_NULL);
133 
134     switch (cmd)
135     {
136     case RT_DEVICE_CTRL_CLR_INT:
137         /* Disable the UART Interrupt */
138         ck_usart_clr_int_flag(uart,IER_RDA_INT_ENABLE);
139         break;
140 
141     case RT_DEVICE_CTRL_SET_INT:
142         /* Enable the UART Interrupt */
143         ck_usart_set_int_flag(uart,IER_RDA_INT_ENABLE);
144         break;
145     }
146 
147     return (RT_EOK);
148 }
149 
uart_putc(struct rt_serial_device * serial,char c)150 static int uart_putc(struct rt_serial_device *serial, char c)
151 {
152     usart_handle_t uart;
153 
154     RT_ASSERT(serial != RT_NULL);
155     uart = (usart_handle_t)serial->parent.user_data;
156     RT_ASSERT(uart != RT_NULL);
157     csi_usart_putchar(uart,c);
158 
159     return (1);
160 }
161 
uart_getc(struct rt_serial_device * serial)162 static int uart_getc(struct rt_serial_device *serial)
163 {
164     int ch;
165     usart_handle_t uart;
166 
167     RT_ASSERT(serial != RT_NULL);
168     uart = (usart_handle_t)serial->parent.user_data;
169     RT_ASSERT(uart != RT_NULL);
170 
171 
172     ch = csi_uart_getchar(uart);
173 
174     return ch;
175 }
176 
177 const struct rt_uart_ops _uart_ops =
178 {
179     uart_configure,
180     uart_control,
181     uart_putc,
182     uart_getc,
183 };
184 
185 
rt_hw_usart_init(void)186 int rt_hw_usart_init(void)
187 {
188     struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
189 
190 #ifdef RT_USING_UART1
191     serial1.ops                 = & _uart_ops;
192     serial1.config              = config;
193     serial1.config.bufsz        = 2048;
194     serial1.config.baud_rate    = 115200;
195 
196     uart1_handle = csi_usart_initialize(0, NULL);
197 
198     rt_hw_serial_register(&serial1,
199                           "uart1",
200                           RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
201                           uart1_handle);
202 #endif
203 
204     return 0;
205 }
206 INIT_BOARD_EXPORT(rt_hw_usart_init);
207 #endif
208