1 /*
2 * Copyright (c) 2006-2022, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2022-12-08 WangShun the first version
9 */
10
11 #include "board.h"
12 #include "rtthread.h"
13 #include <drv_usart.h>
14 #include "hal_udma_ctrl_reg_defs.h"
15 #include "hal_udma_uart_reg_defs.h"
16 #include "udma_uart_driver.h"
17 #include <string.h>
18 #include "core-v-mcu-config.h"
19
20 #ifdef RT_USING_SERIAL
21 //#define DRV_DEBUG
22 #define LOG_TAG "drv.uart"
23 #include <drv_log.h>
24
25 #if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && !defined(BSP_USING_UART3) && !defined(BSP_USING_UART4) && \
26 !defined(BSP_USING_UART5) && !defined(BSP_USING_UART6) && !defined(BSP_USING_UART7) && !defined(BSP_USING_UART8)
27 #error "Please define at least one BSP_USING_UARTx"
28 /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
29 #endif
30
31 extern char u1buffer[128], u0buffer[128];
32 extern int u1rdptr, u1wrptr, u0rdptr,u0wrptr;
33 extern UdmaUart_t *puart0 ;
34 extern UdmaUart_t *puart1 ;
35
36 enum
37 {
38 UART1_INDEX,
39 };
40
41 struct corev_uart_config uart_config[1] =
42 {
43 {
44 "uart1"
45 }
46 };
47
48 struct corev_uart uart_obj[1] = {0};
49
corev_configure(struct rt_serial_device * serial,struct serial_configure * cfg)50 static rt_err_t corev_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
51 {
52 return RT_EOK;
53 }
54
corev_control(struct rt_serial_device * serial,int cmd,void * arg)55 static rt_err_t corev_control(struct rt_serial_device *serial, int cmd, void *arg)
56 {
57 return RT_EOK;
58 }
59
rt_writeraw(uint8_t uart_id,uint16_t write_len,uint8_t * write_buffer)60 uint16_t rt_writeraw(uint8_t uart_id, uint16_t write_len, uint8_t* write_buffer) {
61 UdmaUart_t* puart = (UdmaUart_t*)(UDMA_CH_ADDR_UART + uart_id * UDMA_CH_SIZE);
62
63 while (puart->status_b.tx_busy) {
64 }
65
66 puart->tx_saddr = (uint32_t)write_buffer;
67 puart->tx_size = write_len;
68 puart->tx_cfg_b.en = 1;
69
70 return 0;
71 }
72
corev_putc(struct rt_serial_device * serial,char c)73 static int corev_putc(struct rt_serial_device *serial, char c)
74 {
75 char put_data =c;
76 struct corev_uart *uart;
77 RT_ASSERT(serial != RT_NULL);
78
79 uart = (struct corev_uart *)serial->parent.user_data;
80 rt_writeraw(0, 1,&put_data);
81
82 return 1;
83 }
84
corev_getc(struct rt_serial_device * serial)85 static int corev_getc(struct rt_serial_device *serial)
86 {
87 int ch;
88 struct corev_uart *uart;
89 RT_ASSERT(serial != RT_NULL);
90 uart = (struct corev_uart *)serial->parent.user_data;
91 ch = -1;
92
93 UdmaUart_t* puart = (UdmaUart_t*)(UDMA_CH_ADDR_UART + 0 * UDMA_CH_SIZE);
94
95 if (puart->valid_b.rx_data_valid == 1) {
96 ch = puart->data_b.rx_data & 0xff;
97 }
98
99 return ch;
100 }
101
corevdma_transmit(struct rt_serial_device * serial,rt_uint8_t * buf,rt_size_t size,int direction)102 rt_size_t corevdma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
103 {
104 return RT_EOK;
105 }
106
uart_isr(struct rt_serial_device * serial)107 void uart_isr(struct rt_serial_device *serial)
108 {
109 struct corev_uart *uart = (struct corev_uart *) serial->parent.user_data;
110 RT_ASSERT(uart != RT_NULL);
111 rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
112 }
113
114 static const struct rt_uart_ops corev_uart_ops =
115 {
116 corev_configure,
117 corev_control,
118 corev_putc,
119 corev_getc,
120 corevdma_transmit
121 };
122
123 extern int irq_cli_flag;
uart_rx_isr(void * id)124 void uart_rx_isr (void *id){
125 rt_interrupt_enter();
126 if (id == 6) {
127 while (*(int*)0x1a102130) {
128 u1buffer[u1wrptr++] = puart1->data_b.rx_data & 0xff;
129 u1wrptr &= 0x7f;
130 }
131 }
132 if (id == 2) {//use this uart
133 while (puart0->valid) {
134 if(irq_cli_flag==1)
135 {
136 uart_isr(&(uart_obj[UART1_INDEX].serial));
137 }
138 else if(irq_cli_flag==0)
139 {
140 u0buffer[u0wrptr++] = puart0->data_b.rx_data & 0xff;
141 u0wrptr &= 0x7f;
142 }
143 }
144 }
145 rt_interrupt_leave();
146 }
147
rt_hw_usart_init(void)148 int rt_hw_usart_init(void)
149 {
150 rt_size_t obj_num = 1;
151 struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
152 rt_err_t result = 0;
153
154 for (int i = 0; i < obj_num; i++)
155 {
156 /* init UART object */
157 uart_obj[i].config = &uart_config[i];
158 uart_obj[i].serial.ops = &corev_uart_ops;
159 uart_obj[i].serial.config = config;
160
161 /* register UART device */
162 result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
163 RT_DEVICE_FLAG_RDWR
164 | RT_DEVICE_FLAG_INT_RX
165 ,&uart_obj[i]);
166 RT_ASSERT(result == RT_EOK);
167 }
168 return result;
169 }
170
171 #endif /* RT_USING_SERIAL */
172