-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.c
308 lines (260 loc) · 8.54 KB
/
main.c
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright (C) 2015 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup tests
* @{
*
* @file
* @brief Manual test application for UART peripheral drivers
*
* @author Hauke Petersen <[email protected]>
*
* @}
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "board.h"
#include "shell.h"
#include "thread.h"
#include "msg.h"
#include "ringbuffer.h"
#include "periph/uart.h"
#include "stdio_uart.h"
#include "xtimer.h"
#include "sc_args.h"
#define SHELL_BUFSIZE (128U)
#define UART_BUFSIZE (128U)
#define PRINTER_PRIO (THREAD_PRIORITY_MAIN - 1)
#define PRINTER_TYPE (0xabcd)
#define POWEROFF_DELAY (250U * US_PER_MS) /* quarter of a second */
#ifndef STDIO_UART_DEV
#define STDIO_UART_DEV (UART_UNDEF)
#endif
typedef struct {
char rx_mem[UART_BUFSIZE];
ringbuffer_t rx_buf;
} uart_ctx_t;
static uart_ctx_t ctx[UART_NUMOF];
static kernel_pid_t printer_pid;
static char printer_stack[THREAD_STACKSIZE_MAIN];
#ifdef MODULE_PERIPH_UART_MODECFG
static uart_data_bits_t data_bits_lut[] = { UART_DATA_BITS_5, UART_DATA_BITS_6,
UART_DATA_BITS_7, UART_DATA_BITS_8 };
static int data_bits_lut_len = sizeof(data_bits_lut)/sizeof(data_bits_lut[0]);
static uart_stop_bits_t stop_bits_lut[] = { UART_STOP_BITS_1, UART_STOP_BITS_2 };
static int stop_bits_lut_len = sizeof(stop_bits_lut)/sizeof(stop_bits_lut[0]);
#endif
static void rx_cb(void *arg, uint8_t data)
{
uart_t dev = (uart_t)arg;
ringbuffer_add_one(&(ctx[dev].rx_buf), data);
if (data == '\n') {
msg_t msg;
msg.content.value = (uint32_t)dev;
msg_send(&msg, printer_pid);
}
}
static void *printer(void *arg)
{
(void)arg;
msg_t msg;
msg_t msg_queue[8];
msg_init_queue(msg_queue, 8);
while (1) {
msg_receive(&msg);
uart_t dev = (uart_t)msg.content.value;
char c;
printf("Success: UART_DEV(%i) RX: [", dev);
do {
c = (int)ringbuffer_get_one(&(ctx[dev].rx_buf));
if (c == '\n') {
puts("]\\n");
}
else if (c >= ' ' && c <= '~') {
printf("%c", c);
}
else {
printf("0x%02x", (unsigned char)c);
}
} while (c != '\n');
}
/* this should never be reached */
return NULL;
}
static void sleep_test(int num, uart_t uart)
{
printf("UARD_DEV(%i): test uart_poweron() and uart_poweroff() -> ", num);
uart_poweroff(uart);
xtimer_usleep(POWEROFF_DELAY);
uart_poweron(uart);
puts("[OK]");
}
static int cmd_uart_init(int argc, char **argv)
{
int res = sc_args_check(argc, argv, 2, 2, "DEV BAUD");
if (res != ARGS_OK) {
return 1;
}
int dev = sc_arg2dev(argv[1], UART_NUMOF);
if ((dev < 0) || (UART_DEV(dev) == STDIO_UART_DEV)){
return -ENODEV;
}
uint32_t baud = 0;
if (sc_arg2u32(argv[2], &baud) != ARGS_OK) {
return 1;
}
/* initialize UART */
res = uart_init(UART_DEV(dev), baud, rx_cb, (void *)dev);
if (res == UART_NOBAUD) {
printf("Error: Given baudrate (%u) not possible\n", (unsigned int)baud);
return 1;
}
else if (res != UART_OK) {
puts("Error: Unable to initialize UART device");
return 1;
}
printf("Success: Initialized UART_DEV(%i) at BAUD %"PRIu32"\n", dev, baud);
/* also test if poweron() and poweroff() work (or at least don't break
* anything) */
sleep_test(dev, UART_DEV(dev));
return 0;
}
#ifdef MODULE_PERIPH_UART_MODECFG
static int cmd_uart_mode(int argc, char **argv)
{
uart_data_bits_t data_bits;
uart_parity_t parity;
uart_stop_bits_t stop_bits;
int res = sc_args_check(argc, argv, 4, 4, "DEV DATA_BITS PARITY STOP_BITS");
if (res != ARGS_OK) {
return 1;
}
int dev = sc_arg2dev(argv[1], UART_NUMOF);
if ((dev < 0) || (UART_DEV(dev) == STDIO_UART_DEV)){
return -ENODEV;
}
int data_bits_arg = 0;
if (sc_arg2int(argv[2], &data_bits_arg) != ARGS_OK) {
return 1;
}
data_bits_arg -= 5;
if (data_bits_arg >= 0 && data_bits_arg < data_bits_lut_len) {
data_bits = data_bits_lut[data_bits_arg];
}
else {
printf("Error: Invalid number of data_bits (%i).\n", data_bits_arg + 5);
return 1;
}
argv[3][0] &= ~0x20;
switch (argv[3][0]) {
case 'N':
parity = UART_PARITY_NONE;
break;
case 'E':
parity = UART_PARITY_EVEN;
break;
case 'O':
parity = UART_PARITY_ODD;
break;
case 'M':
parity = UART_PARITY_MARK;
break;
case 'S':
parity = UART_PARITY_SPACE;
break;
default:
printf("Error: Invalid parity (%c).\n", argv[3][0]);
return 1;
}
int stop_bits_arg = 0;
if (sc_arg2int(argv[4], &stop_bits_arg) != ARGS_OK) {
return 1;
}
stop_bits_arg -= 1;
if (stop_bits_arg >= 0 && stop_bits_arg < stop_bits_lut_len) {
stop_bits = stop_bits_lut[stop_bits_arg];
}
else {
printf("Error: Invalid number of stop bits (%i).\n", stop_bits_arg + 1);
return 1;
}
if (uart_mode(UART_DEV(dev), data_bits, parity, stop_bits) != UART_OK) {
puts("Error: Unable to apply UART settings");
return 1;
}
printf("Success: Successfully applied UART_DEV(%i) settings\n", dev);
return 0;
}
#endif /* MODULE_PERIPH_UART_MODECFG */
static int cmd_uart_write(int argc, char **argv)
{
int res = sc_args_check(argc, argv, 2, 2, "DEV DATA");
if (res != ARGS_OK) {
return 1;
}
int dev = sc_arg2dev(argv[1], UART_NUMOF);
if ((dev < 0) || (UART_DEV(dev) == STDIO_UART_DEV)){
return -ENODEV;
}
printf("UART_DEV(%i) TX: %s\n", dev, argv[2]);
uint8_t endline = (uint8_t)'\n';
uart_write(UART_DEV(dev), (uint8_t *)argv[2], strlen(argv[2]));
uart_write(UART_DEV(dev), &endline, 1);
return 0;
}
int cmd_get_metadata(int argc, char **argv)
{
(void)argv;
(void)argc;
printf("Success: [%s, %s]\n", RIOT_BOARD, RIOT_APPLICATION);
return 0;
}
static const shell_command_t shell_commands[] = {
{ "uart_init", "Initialize a UART device with a given baudrate", cmd_uart_init },
#ifdef MODULE_PERIPH_UART_MODECFG
{ "uart_mode", "Setup data bits, stop bits and parity for a given UART device", cmd_uart_mode },
#endif
{ "uart_write", "Send a buffer through given UART device", cmd_uart_write },
{ "get_metadata", "Get the metadata of the test firmware", cmd_get_metadata },
{ NULL, NULL, NULL }
};
int main(void)
{
puts("\nManual UART driver test application");
puts("===================================");
puts("This application is intended for testing additional UART\n"
"interfaces, that might be defined for a board. The 'primary' UART\n"
"interface is tested implicitly, as it is running the shell...\n\n"
"When receiving data on one of the additional UART interfaces, this\n"
"data will be outputted via STDIO. So the easiest way to test an \n"
"UART interface, is to simply connect the RX with the TX pin. Then \n"
"you can send data on that interface and you should see the data \n"
"being printed to STDOUT\n\n"
"NOTE: all strings need to be '\\n' terminated!\n");
/* do sleep test for UART used as STDIO. There is a possibility that the
* value given in STDIO_UART_DEV is not a numeral (depends on the CPU
* implementation), so we rather break the output by printing a
* non-numerical value instead of breaking the UART device descriptor */
sleep_test(STDIO_UART_DEV, STDIO_UART_DEV);
puts("\nUART INFO:");
printf("Available devices: %i\n", UART_NUMOF);
printf("UART used for STDIO (the shell): UART_DEV(%i)\n\n", STDIO_UART_DEV);
/* initialize ringbuffers */
for (unsigned i = 0; i < UART_NUMOF; i++) {
ringbuffer_init(&(ctx[i].rx_buf), ctx[i].rx_mem, UART_BUFSIZE);
}
/* start the printer thread */
printer_pid = thread_create(printer_stack, sizeof(printer_stack),
PRINTER_PRIO, 0, printer, NULL, "printer");
/* run the shell */
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
return 0;
}