1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Email: [email protected]
*
* Change Logs:
* Date Author Notes
* 2022-10-26 huanghe first commit
*
*/
#include "board.h"
#include "drv_usart.h"
#include "interrupt.h"
#include "fpl011.h"
#include "rtconfig.h"
#include "fprintk.h"
#ifdef RT_USING_SERIAL
extern u32 FUart_GetInterruptMask(FPl011 *uart_ptr);
static void Ft_Os_Uart_Callback(void *Args, u32 Event, u32 EventData);
static void rt_hw_uart_isr(int irqno, void *param)
{
FPl011InterruptHandler(irqno, param);
}
static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
struct drv_usart *uart = RT_NULL;
FPl011 *uart_hw = RT_NULL;
u32 intr_mask;
FPl011Config config;
RT_ASSERT(serial != RT_NULL);
RT_ASSERT(cfg != RT_NULL);
uart = rt_container_of(serial, struct drv_usart, serial);
uart_hw = uart->handle;
config = *(const FPl011Config *)FPl011LookupConfig(uart->config.uart_instance);
RT_ASSERT(FPl011CfgInitialize(uart_hw, &config) == FT_SUCCESS);
FPl011SetHandler(uart_hw, Ft_Os_Uart_Callback, serial);
FPl011SetRxFifoThreadhold(uart_hw, FPL011IFLS_RXIFLSEL_1_4);
FPl011SetTxFifoThreadHold(uart_hw, FPL011IFLS_TXIFLSEL_1_2);
//<! 打开接收中断
intr_mask = uart->config.isr_event_mask;
FPl011SetInterruptMask(uart_hw, intr_mask);
FPl011SetOptions(uart_hw, FPL011_OPTION_UARTEN | FPL011_OPTION_RXEN | FPL011_OPTION_TXEN | FPL011_OPTION_FIFOEN);
rt_hw_interrupt_set_priority(uart_hw->config.irq_num, uart->config.isr_priority);
rt_hw_interrupt_install(uart_hw->config.irq_num, rt_hw_uart_isr, uart_hw, "uart");
rt_hw_interrupt_umask(uart_hw->config.irq_num);
return RT_EOK;
}
static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
{
struct drv_usart *uart = RT_NULL;
FPl011 *uart_ptr = RT_NULL;
RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct drv_usart, serial);
uart_ptr = uart->handle;
switch (cmd)
{
case RT_DEVICE_CTRL_CLR_INT:
/* disable rx irq */
rt_hw_interrupt_mask(uart_ptr->config.irq_num);
break;
case RT_DEVICE_CTRL_SET_INT:
/* enable rx irq */
rt_hw_interrupt_umask(uart_ptr->config.irq_num);
break;
}
return RT_EOK;
}
static void Ft_Os_Uart_Callback(void *Args, u32 Event, u32 EventData)
{
struct rt_serial_device *serial = (struct rt_serial_device *)Args;
if (FPL011_EVENT_RECV_DATA == Event || FPL011_EVENT_RECV_TOUT == Event)
{
if (serial->serial_rx)
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
}
else if (FPL011_EVENT_RECV_ERROR == Event)
{
}
else if (FPL011_EVENT_SENT_DATA == Event)
{
}
else if (FPL011_EVENT_PARE_FRAME_BRKE == Event)
{
}
else if (FPL011_EVENT_RECV_ORERR == Event)
{
}
if (FPL011_EVENT_SENT_DATA == Event)
{
}
else
{
}
}
static int uart_putc(struct rt_serial_device *serial, char c)
{
struct drv_usart *uart = RT_NULL;
FPl011 *uart_ptr = RT_NULL;
RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct drv_usart, serial);
uart_ptr = uart->handle;
FPl011SendByte(uart_ptr->config.base_address, c);
return 1;
}
u8 FPl011RecvByteNoBlocking(u32 addr)
{
u32 recieved_byte;
while (FUART_ISRECEIVEDATA(addr))
{
return 0xff;
}
recieved_byte = FUART_READREG32(addr, FPL011DR_OFFSET);
return recieved_byte;
}
static int uart_getc(struct rt_serial_device *serial)
{
int ch;
struct drv_usart *uart = RT_NULL;
FPl011 *uart_ptr = RT_NULL;
RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct drv_usart, serial);
uart_ptr = uart->handle;
ch = FPl011RecvByteNoBlocking(uart_ptr->config.base_address);
if (ch == 0xff)
{
ch = -1;
rt_kprintf("") ;
}
else
{
//
}
return ch;
}
static const struct rt_uart_ops _uart_ops =
{
uart_configure,
uart_control,
uart_putc,
uart_getc,
NULL
};
#define RT_USING_UART0
#define RT_USING_UART1
#ifdef RT_USING_UART0
static FPl011 Ft_Uart0;
static struct drv_usart _RtUart0;
#endif
#ifdef RT_USING_UART1
static FPl011 Ft_Uart1;
static struct drv_usart _RtUart1;
#endif
int rt_hw_uart_init(void)
{
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
#ifdef RT_USING_UART0
config.bufsz = RT_SERIAL_RB_BUFSZ;
_RtUart0.serial.ops = &_uart_ops;
_RtUart0.serial.config = config;
// Ft_Uart0.config.instance_id = FUART0_ID;
_RtUart0.handle = &Ft_Uart0;
_RtUart0.config.uart_instance = FUART0_ID;
_RtUart0.config.isr_priority = 0xd0;
_RtUart0.config.isr_event_mask = (RTOS_UART_ISR_OEIM_MASK | RTOS_UART_ISR_BEIM_MASK | RTOS_UART_ISR_PEIM_MASK | RTOS_UART_ISR_FEIM_MASK | RTOS_UART_ISR_RTIM_MASK | RTOS_UART_ISR_RXIM_MASK);
_RtUart0.config.uart_baudrate = 115200;
rt_hw_serial_register(&_RtUart0.serial, "uart0",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
&_RtUart0);
#endif
#ifdef RT_USING_UART1
config.bufsz = RT_SERIAL_RB_BUFSZ;
_RtUart1.serial.ops = &_uart_ops;
_RtUart1.serial.config = config;
// Ft_Uart1.config.instance_id = FUART1_ID;
_RtUart1.handle = &Ft_Uart1;
_RtUart1.config.uart_instance = FUART1_ID;
_RtUart1.config.isr_priority = 0xd0;
_RtUart1.config.isr_event_mask = (RTOS_UART_ISR_OEIM_MASK | RTOS_UART_ISR_BEIM_MASK | RTOS_UART_ISR_PEIM_MASK | RTOS_UART_ISR_FEIM_MASK | RTOS_UART_ISR_RTIM_MASK | RTOS_UART_ISR_RXIM_MASK);
_RtUart1.config.uart_baudrate = 115200;
rt_hw_serial_register(&_RtUart1.serial, "uart1",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
&_RtUart1);
#endif
return 0;
}
INIT_BOARD_EXPORT(rt_hw_uart_init);
#endif /* RT_USING_SERIAL */
|