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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "board.h"
#include "drv_uart.h"
#include "hardware/uart.h"
#include "hardware/irq.h"
#define UART_ID uart0
#define BAUD_RATE 115200
#define DATA_BITS 8
#define STOP_BITS 1
#define PARITY UART_PARITY_NONE
// We are using pins 0 and 1, but see the GPIO function select table in the
// datasheet for information on which other pins can be used.
#define UART_TX_PIN 0
#define UART_RX_PIN 1
#define PICO_UART_DEVICE(uart) (struct pico_uart_dev *)(uart)
static struct pico_uart_dev uart0_dev;
struct pico_uart_dev
{
struct rt_serial_device parent;
rt_uint32_t uart_periph;
rt_uint32_t irqno;
};
void pico_uart_isr(void)
{
rt_interrupt_enter();
/* read interrupt status and clear it */
if (uart_is_readable(uart0)) /* rx ind */
{
rt_hw_serial_isr(&uart0_dev.parent, RT_SERIAL_EVENT_RX_IND);
}
rt_interrupt_leave();
}
/*
* UART interface
*/
static rt_err_t pico_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
return RT_EOK;
}
static rt_err_t pico_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
{
// Select correct interrupt for the UART we are using
int UART_IRQ = UART_ID == uart0 ? UART0_IRQ : UART1_IRQ;
switch (cmd)
{
/* enable interrupt */
case RT_DEVICE_CTRL_SET_INT:
// Set up a RX interrupt
// We need to set up the handler first
// And set up and enable the interrupt handlers
irq_set_exclusive_handler(UART_IRQ, pico_uart_isr);
irq_set_enabled(UART_IRQ, true);
// Now enable the UART to send interrupts - RX only
uart_set_irq_enables(UART_ID, true, false);
break;
}
return RT_EOK;
}
static int pico_uart_putc(struct rt_serial_device *serial, char c)
{
uart_putc_raw(uart0, c);
return 1;
}
static int pico_uart_getc(struct rt_serial_device *serial)
{
int ch;
if (uart_is_readable(uart0))
{
ch = uart_get_hw(uart0)->dr;
}
else
{
ch =-1;
}
return ch;
}
const static struct rt_uart_ops _uart_ops =
{
pico_uart_configure,
pico_uart_control,
pico_uart_putc,
pico_uart_getc,
RT_NULL,
};
/*
* UART Initiation
*/
int rt_hw_uart_init(void)
{
rt_err_t ret = RT_EOK;
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
uart_init(UART_ID, 115200);
// Set the TX and RX pins by using the function select on the GPIO
// Set datasheet for more information on function select
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
// Actually, we want a different speed
// The call will return the actual baud rate selected, which will be as close as
// possible to that requested
uart_set_baudrate(UART_ID, BAUD_RATE);
// Set UART flow control CTS/RTS, we don't want these, so turn them off
uart_set_hw_flow(UART_ID, false, false);
// Set our data format
uart_set_format(UART_ID, DATA_BITS, STOP_BITS, PARITY);
// Turn off FIFO's - we want to do this character by character
uart_set_fifo_enabled(UART_ID, false);
uart0_dev.parent.ops = &_uart_ops;
uart0_dev.parent.config = config;
ret = rt_hw_serial_register(&uart0_dev.parent,
"uart0",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
&uart0_dev);
return ret;
}
// INIT_DEVICE_EXPORT(rt_hw_uart_init);
// We are using pins 0 and 1, but see the GPIO function select table in the
// datasheet for information on which other pins can be used.
#define UART1_TX_PIN 4
#define UART1_RX_PIN 5
static struct pico_uart1_dev uart1_dev;
struct pico_uart1_dev
{
struct rt_serial_device parent;
rt_uint32_t uart_periph;
rt_uint32_t irqno;
};
void pico_uart1_isr(void)
{
rt_interrupt_enter();
/* read interrupt status and clear it */
if (uart_is_readable(uart1)) /* rx ind */
{
rt_hw_serial_isr(&uart1_dev.parent, RT_SERIAL_EVENT_RX_IND);
}
rt_interrupt_leave();
}
/*
* UART interface
*/
static rt_err_t pico_uart1_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
return RT_EOK;
}
static rt_err_t pico_uart1_control(struct rt_serial_device *serial, int cmd, void *arg)
{
switch (cmd)
{
/* enable interrupt */
case RT_DEVICE_CTRL_SET_INT:
// Set up a RX interrupt
// We need to set up the handler first
// And set up and enable the interrupt handlers
irq_set_exclusive_handler(UART1_IRQ, pico_uart1_isr);
irq_set_enabled(UART1_IRQ, true);
// Now enable the UART to send interrupts - RX only
uart_set_irq_enables(uart1, true, false);
break;
}
return RT_EOK;
}
static int pico_uart1_putc(struct rt_serial_device *serial, char c)
{
uart_putc_raw(uart1, c);
return 1;
}
static int pico_uart1_getc(struct rt_serial_device *serial)
{
int ch;
if (uart_is_readable(uart1))
{
ch = uart_get_hw(uart1)->dr;
}
else
{
ch =-1;
}
return ch;
}
const static struct rt_uart_ops _uart1_ops =
{
pico_uart1_configure,
pico_uart1_control,
pico_uart1_putc,
pico_uart1_getc,
RT_NULL,
};
/*
* UART Initiation
*/
int rt_hw_uart1_init(void)
{
rt_err_t ret = RT_EOK;
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
uart_init(uart1, 115200);
// Set the TX and RX pins by using the function select on the GPIO
// Set datasheet for more information on function select
gpio_set_function(UART1_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART1_RX_PIN, GPIO_FUNC_UART);
// Actually, we want a different speed
// The call will return the actual baud rate selected, which will be as close as
// possible to that requested
uart_set_baudrate(uart1, BAUD_RATE);
// Set UART flow control CTS/RTS, we don't want these, so turn them off
uart_set_hw_flow(uart1, false, false);
// Set our data format
uart_set_format(uart1, DATA_BITS, STOP_BITS, PARITY);
// Turn off FIFO's - we want to do this character by character
uart_set_fifo_enabled(uart1, false);
uart1_dev.parent.ops = &_uart1_ops;
uart1_dev.parent.config = config;
ret = rt_hw_serial_register(&uart1_dev.parent,
"uart1",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
&uart1_dev);
return ret;
}
INIT_DEVICE_EXPORT(rt_hw_uart1_init);
|