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  * 2021-03-04     Carl      the first version
9  */
10 
11 #include "board.h"
12 #include "drv_usart.h"
13 #include "interrupt.h"
14 #include "serial.h"
15 #include "rtconfig.h"
16 
17 #ifdef RT_USING_SERIAL
18 
19 extern u32 FUart_GetInterruptMask(Ft_Uart *uart_ptr);
20 
21 static void Ft_Os_Uart_Callback(void *Args, u32 Event, u32 EventData);
22 
rt_hw_uart_isr(int irqno,void * param)23 static void rt_hw_uart_isr(int irqno, void *param)
24 {
25     Ft_Uart *uart_ptr = (Ft_Uart *)param;
26     FUart_InterruptHandler(uart_ptr);
27 }
28 
uart_configure(struct rt_serial_device * serial,struct serial_configure * cfg)29 static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
30 {
31     struct drv_usart *uart = RT_NULL;
32     Ft_Uart *uart_ptr = RT_NULL;
33     u32 RegTemp;
34     u32 ret;
35     RT_ASSERT(serial != RT_NULL);
36     RT_ASSERT(cfg != RT_NULL);
37     uart = rt_container_of(serial, struct drv_usart, serial);
38     uart_ptr = uart->handle;
39 
40     RT_ASSERT(FUart_CfgInitialize(uart_ptr, FUart_LookupConfig(uart_ptr->Config.InstanceId)) == FST_SUCCESS);
41     FUart_SetHandler(uart_ptr, Ft_Os_Uart_Callback, serial);
42     rt_hw_interrupt_install(uart_ptr->Config.IsrNum, rt_hw_uart_isr, uart_ptr, "uart");
43     rt_hw_interrupt_umask(uart_ptr->Config.IsrNum);
44 
45     //<! 设置波特率
46     ret = FUart_SetBaudRate(uart_ptr, cfg->baud_rate);
47     RT_ASSERT(ret == FST_SUCCESS);
48 
49     //<! 打开接收中断
50     RegTemp = FUart_GetInterruptMask(uart_ptr);
51     RegTemp |= UARTMIS_RTMIS;
52     FUart_SetInterruptMask(uart_ptr, RegTemp);
53     FUart_SetOptions(uart_ptr, FUART_OPTION_UARTEN | FUART_OPTION_RXEN | FUART_OPTION_TXEN | FUART_OPTION_FIFOEN);
54     return RT_EOK;
55 }
56 
uart_control(struct rt_serial_device * serial,int cmd,void * arg)57 static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
58 {
59     struct drv_usart *uart = RT_NULL;
60     Ft_Uart *uart_ptr = RT_NULL;
61     RT_ASSERT(serial != RT_NULL);
62 
63     uart = rt_container_of(serial, struct drv_usart, serial);
64     uart_ptr = uart->handle;
65 
66     switch (cmd)
67     {
68     case RT_DEVICE_CTRL_CLR_INT:
69         /* disable rx irq */
70         rt_hw_interrupt_mask(uart_ptr->Config.IsrNum);
71         break;
72 
73     case RT_DEVICE_CTRL_SET_INT:
74         /* enable rx irq */
75         rt_hw_interrupt_umask(uart_ptr->Config.IsrNum);
76         break;
77     }
78 
79     return RT_EOK;
80 }
81 
Ft_Os_Uart_Callback(void * Args,u32 Event,u32 EventData)82 static void Ft_Os_Uart_Callback(void *Args, u32 Event, u32 EventData)
83 {
84     struct rt_serial_device *serial = (struct rt_serial_device *)Args;
85 
86     if (FUART_EVENT_RECV_DATA == Event || FUART_EVENT_RECV_TOUT == Event)
87     {
88         if (serial->serial_rx)
89             rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
90     }
91     else if (FUART_EVENT_RECV_ERROR == Event)
92     {
93     }
94     else if (FUART_EVENT_SENT_DATA == Event)
95     {
96     }
97     else if (FUART_EVENT_PARE_FRAME_BRKE == Event)
98     {
99     }
100     else if (FUART_EVENT_RECV_ORERR == Event)
101     {
102     }
103 
104     if (FUART_EVENT_SENT_DATA == Event)
105     {
106     }
107     else
108     {
109     }
110 }
111 
uart_putc(struct rt_serial_device * serial,char c)112 static int uart_putc(struct rt_serial_device *serial, char c)
113 {
114     struct drv_usart *uart = RT_NULL;
115     Ft_Uart *uart_ptr = RT_NULL;
116     RT_ASSERT(serial != RT_NULL);
117 
118     uart = rt_container_of(serial, struct drv_usart, serial);
119     uart_ptr = uart->handle;
120 
121     FUart_SendByte(uart_ptr->Config.BaseAddress, c);
122 
123     return 1;
124 }
125 
uart_getc(struct rt_serial_device * serial)126 static int uart_getc(struct rt_serial_device *serial)
127 {
128     int ch;
129     struct drv_usart *uart = RT_NULL;
130     Ft_Uart *uart_ptr = RT_NULL;
131     RT_ASSERT(serial != RT_NULL);
132 
133     uart = rt_container_of(serial, struct drv_usart, serial);
134     uart_ptr = uart->handle;
135 
136     ch = FUart_GetChar(uart_ptr->Config.BaseAddress);
137     if (ch == 0xff)
138         ch = -1;
139 
140     return ch;
141 }
142 
143 static const struct rt_uart_ops _uart_ops =
144     {
145         uart_configure,
146         uart_control,
147         uart_putc,
148         uart_getc,
149 };
150 
151 #ifdef RT_USING_UART0
152 static Ft_Uart Ft_Uart0;
153 static struct drv_usart _RtUart0;
154 #endif
155 
156 #ifdef RT_USING_UART1
157 static Ft_Uart Ft_Uart1;
158 static struct drv_usart _RtUart1;
159 #endif
160 
rt_hw_uart_init(void)161 int rt_hw_uart_init(void)
162 {
163 
164     struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
165 
166 #ifdef RT_USING_UART0
167     config.bufsz = RT_SERIAL_RB_BUFSZ;
168     _RtUart0.serial.ops = &_uart_ops;
169     _RtUart0.serial.config = config;
170     Ft_Uart0.Config.InstanceId = FT_UART0_ID;
171     _RtUart0.Handle = &Ft_Uart0;
172 
173     rt_hw_serial_register(&_RtUart0.serial, "uart0",
174                           RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
175                           &_RtUart0);
176 #endif
177 
178 #ifdef RT_USING_UART1
179     config.bufsz = RT_SERIAL_RB_BUFSZ;
180     _RtUart1.serial.ops = &_uart_ops;
181     _RtUart1.serial.config = config;
182     Ft_Uart1.Config.InstanceId = FT_UART1_ID;
183     _RtUart1.handle = &Ft_Uart1;
184     rt_hw_serial_register(&_RtUart1.serial, "uart1",
185                           RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
186                           &_RtUart1);
187 #endif
188 
189     return 0;
190 }
191 INIT_BOARD_EXPORT(rt_hw_uart_init);
192 
193 #endif /* RT_USING_SERIAL */
194