forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 170
/
uart.c
524 lines (469 loc) · 15.6 KB
/
uart.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2021 Renesas Electronics Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "py/runtime.h"
#include "py/stream.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#include "shared/runtime/interrupt_char.h"
#include "shared/runtime/mpirq.h"
#include "uart.h"
#include "irq.h"
#include "pendsv.h"
typedef int (*KEYEX_CB)(uint32_t d);
extern void NORETURN __fatal_error(const char *msg);
#if MICROPY_KBD_EXCEPTION
extern int mp_interrupt_char;
static KEYEX_CB keyex_cb[MICROPY_HW_MAX_UART] = {(KEYEX_CB)NULL};
static int chk_kbd_interrupt(int d) {
if (d == mp_interrupt_char) {
pendsv_kbd_intr();
return 1;
} else {
return 0;
}
}
static void set_kbd_interrupt(uint32_t ch, void *keyex) {
ra_sci_rxirq_disable(ch);
keyex_cb[ch] = (KEYEX_CB)keyex;
ra_sci_rxirq_enable(ch);
}
#endif
static void uart_rx_cb(uint32_t ch, int d) {
machine_uart_obj_t *self = MP_STATE_PORT(machine_uart_obj_all)[ch];
if (self == NULL) {
// UART object has not been set, so we can't do anything, not
// even disable the IRQ. This should never happen.
return;
}
#if MICROPY_KBD_EXCEPTION
if (keyex_cb[ch]) {
(*keyex_cb[ch])(d);
}
#endif
// Check the flags to see if the user handler should be called
if (self->mp_irq_trigger) {
mp_irq_handler(self->mp_irq_obj);
}
}
void uart_init0(void) {
}
// unregister all interrupt sources
void uart_deinit_all(void) {
for (int i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(machine_uart_obj_all)); i++) {
machine_uart_obj_t *uart_obj = MP_STATE_PORT(machine_uart_obj_all)[i];
if (uart_obj != NULL && !uart_obj->is_static) {
uart_deinit(uart_obj);
MP_STATE_PORT(machine_uart_obj_all)[i] = NULL;
}
}
}
bool uart_exists(int uart_id) {
if (uart_id > MP_ARRAY_SIZE(MP_STATE_PORT(machine_uart_obj_all))) {
// safeguard against machine_uart_obj_all array being configured too small
return false;
}
switch (uart_id) {
#if defined(MICROPY_HW_UART0_TX) && defined(MICROPY_HW_UART0_RX)
case HW_UART_0:
return true;
#endif
#if defined(MICROPY_HW_UART1_TX) && defined(MICROPY_HW_UART1_RX)
case HW_UART_1:
return true;
#endif
#if defined(MICROPY_HW_UART2_TX) && defined(MICROPY_HW_UART2_RX)
case HW_UART_2:
return true;
#endif
#if defined(MICROPY_HW_UART3_TX) && defined(MICROPY_HW_UART3_RX)
case HW_UART_3:
return true;
#endif
#if defined(MICROPY_HW_UART4_TX) && defined(MICROPY_HW_UART4_RX)
case HW_UART_4:
return true;
#endif
#if defined(MICROPY_HW_UART5_TX) && defined(MICROPY_HW_UART5_RX)
case HW_UART_5:
return true;
#endif
#if defined(MICROPY_HW_UART6_TX) && defined(MICROPY_HW_UART6_RX)
case HW_UART_6:
return true;
#endif
#if defined(MICROPY_HW_UART7_TX) && defined(MICROPY_HW_UART7_RX)
case HW_UART_7:
return true;
#endif
#if defined(MICROPY_HW_UART8_TX) && defined(MICROPY_HW_UART8_RX)
case HW_UART_8:
return true;
#endif
#if defined(MICROPY_HW_UART9_TX) && defined(MICROPY_HW_UART9_RX)
case HW_UART_9:
return true;
#endif
default:
return false;
}
}
// assumes Init parameters have been set up correctly
bool uart_init(machine_uart_obj_t *uart_obj,
uint32_t baudrate, uint32_t bits, uint32_t parity, uint32_t stop, uint32_t flow) {
uart_obj->baudrate = (uint32_t)baudrate;
uart_obj->bits = (uint8_t)bits;
uart_obj->parity = (uint8_t)parity;
uart_obj->stop = (uint8_t)stop;
uart_obj->flow = (uint8_t)flow;
const machine_pin_obj_t *pins[4] = {0};
switch (uart_obj->uart_id) {
#if defined(MICROPY_HW_UART0_TX) && defined(MICROPY_HW_UART0_RX)
case HW_UART_0:
pins[0] = MICROPY_HW_UART0_TX;
pins[1] = MICROPY_HW_UART0_RX;
#if defined(MICROPY_HW_UART0_RTS)
if (flow) {
pins[2] = MICROPY_HW_UART0_RTS;
}
#endif
#if defined(MICROPY_HW_UART0_CTS)
if (flow) {
pins[3] = MICROPY_HW_UART0_CTS;
}
#endif
break;
#endif
#if defined(MICROPY_HW_UART1_TX) && defined(MICROPY_HW_UART1_RX)
case HW_UART_1:
pins[0] = MICROPY_HW_UART1_TX;
pins[1] = MICROPY_HW_UART1_RX;
#if defined(MICROPY_HW_UART1_RTS)
if (flow) {
pins[2] = MICROPY_HW_UART1_RTS;
}
#endif
#if defined(MICROPY_HW_UART1_CTS)
if (flow) {
pins[3] = MICROPY_HW_UART1_CTS;
}
#endif
break;
#endif
#if defined(MICROPY_HW_UART2_TX) && defined(MICROPY_HW_UART2_RX)
case HW_UART_2:
pins[0] = MICROPY_HW_UART2_TX;
pins[1] = MICROPY_HW_UART2_RX;
#if defined(MICROPY_HW_UART2_RTS)
if (flow) {
pins[2] = MICROPY_HW_UART2_RTS;
}
#endif
#if defined(MICROPY_HW_UART2_CTS)
if (flow) {
pins[3] = MICROPY_HW_UART2_CTS;
}
#endif
break;
#endif
#if defined(MICROPY_HW_UART3_TX) && defined(MICROPY_HW_UART3_RX)
case HW_UART_3:
pins[0] = MICROPY_HW_UART3_TX;
pins[1] = MICROPY_HW_UART3_RX;
#if defined(MICROPY_HW_UART3_RTS)
if (flow) {
pins[2] = MICROPY_HW_UART3_RTS;
}
#endif
#if defined(MICROPY_HW_UART3_CTS)
if (flow) {
pins[3] = MICROPY_HW_UART3_CTS;
}
#endif
break;
#endif
#if defined(MICROPY_HW_UART4_TX) && defined(MICROPY_HW_UART4_RX)
case HW_UART_4:
pins[0] = MICROPY_HW_UART4_TX;
pins[1] = MICROPY_HW_UART4_RX;
#if defined(MICROPY_HW_UART4_RTS)
if (flow) {
pins[2] = MICROPY_HW_UART4_RTS;
}
#endif
#if defined(MICROPY_HW_UART4_CTS)
if (flow) {
pins[3] = MICROPY_HW_UART4_CTS;
}
#endif
break;
#endif
#if defined(MICROPY_HW_UART5_TX) && defined(MICROPY_HW_UART5_RX)
case HW_UART_5:
pins[0] = MICROPY_HW_UART5_TX;
pins[1] = MICROPY_HW_UART5_RX;
#if defined(MICROPY_HW_UART5_RTS)
if (flow) {
pins[2] = MICROPY_HW_UART5_RTS;
}
#endif
#if defined(MICROPY_HW_UART5_CTS)
if (flow) {
pins[3] = MICROPY_HW_UART5_CTS;
}
#endif
break;
#endif
#if defined(MICROPY_HW_UART6_TX) && defined(MICROPY_HW_UART6_RX)
case HW_UART_6:
pins[0] = MICROPY_HW_UART6_TX;
pins[1] = MICROPY_HW_UART6_RX;
#if defined(MICROPY_HW_UART6_RTS)
if (flow) {
pins[2] = MICROPY_HW_UART6_RTS;
}
#endif
#if defined(MICROPY_HW_UART6_CTS)
if (flow) {
pins[3] = MICROPY_HW_UART6_CTS;
}
#endif
break;
#endif
#if defined(MICROPY_HW_UART7_TX) && defined(MICROPY_HW_UART7_RX)
case HW_UART_7:
pins[0] = MICROPY_HW_UART7_TX;
pins[1] = MICROPY_HW_UART7_RX;
#if defined(MICROPY_HW_UART7_RTS)
if (flow) {
pins[2] = MICROPY_HW_UART7_RTS;
}
#endif
#if defined(MICROPY_HW_UART7_CTS)
if (flow) {
pins[3] = MICROPY_HW_UART7_CTS;
}
#endif
break;
#endif
#if defined(MICROPY_HW_UART8_TX) && defined(MICROPY_HW_UART8_RX)
case HW_UART_8:
pins[0] = MICROPY_HW_UART8_TX;
pins[1] = MICROPY_HW_UART8_RX;
#if defined(MICROPY_HW_UART8_RTS)
if (flow) {
pins[2] = MICROPY_HW_UART8_RTS;
}
#endif
#if defined(MICROPY_HW_UART8_CTS)
if (flow) {
pins[3] = MICROPY_HW_UART8_CTS;
}
#endif
break;
#endif
#if defined(MICROPY_HW_UART9_TX) && defined(MICROPY_HW_UART9_RX)
case HW_UART_9:
pins[0] = MICROPY_HW_UART9_TX;
pins[1] = MICROPY_HW_UART9_RX;
#if defined(MICROPY_HW_UART9_RTS)
if (flow) {
pins[2] = MICROPY_HW_UART9_RTS;
}
#endif
#if defined(MICROPY_HW_UART9_CTS)
if (flow) {
pins[3] = MICROPY_HW_UART9_CTS;
}
#endif
break;
#endif
default:
// UART does not exist or is not configured for this board
return false;
}
uart_obj->tx = pins[0];
uart_obj->rx = pins[1];
uart_obj->rts = pins[2];
uart_obj->cts = pins[3];
if (flow && (uart_obj->rts != 0) && (uart_obj->cts != 0)) {
ra_sci_init_with_flow(uart_obj->uart_id, (uint32_t)uart_obj->tx->pin, (uint32_t)uart_obj->rx->pin, baudrate, bits, parity, stop, flow, (uint32_t)uart_obj->rts->pin, (uint32_t)uart_obj->cts->pin);
} else {
ra_sci_init(uart_obj->uart_id, (uint32_t)uart_obj->tx->pin, (uint32_t)uart_obj->rx->pin, baudrate, bits, parity, stop, flow);
}
ra_sci_rx_set_callback((int)uart_obj->uart_id, (SCI_CB)uart_rx_cb);
uart_obj->is_enabled = true;
uart_obj->attached_to_repl = false;
if (bits == 9 && parity == UART_PARITY_NONE) {
uart_obj->char_mask = 0x1ff;
uart_obj->char_width = CHAR_WIDTH_9BIT;
} else {
if (bits == 9 || parity == UART_PARITY_NONE) {
uart_obj->char_mask = 0xff;
} else {
uart_obj->char_mask = 0x7f;
}
uart_obj->char_width = CHAR_WIDTH_8BIT;
}
uart_obj->mp_irq_trigger = 0;
uart_obj->mp_irq_obj = NULL;
return true;
}
void uart_irq_config(machine_uart_obj_t *self, bool enable) {
if (self->mp_irq_trigger) {
if (enable) {
ra_sci_rxirq_enable(self->uart_id);
} else {
ra_sci_rxirq_disable(self->uart_id);
}
}
}
void uart_set_rxbuf(machine_uart_obj_t *self, size_t len, void *buf) {
// len = 0 (no interrupt) is not supported. static buf is used.
self->read_buf_len = len;
self->read_buf = buf;
if (len) {
int ch = (int)self->uart_id;
ra_sci_rxfifo_set(ch, (uint8_t *)buf, (uint32_t)len);
}
}
void uart_deinit(machine_uart_obj_t *self) {
self->is_enabled = false;
ra_sci_deinit(self->uart_id);
}
void uart_attach_to_repl(machine_uart_obj_t *self, bool attached) {
self->attached_to_repl = attached;
#if MICROPY_KBD_EXCEPTION
if (attached) {
set_kbd_interrupt((int)self->uart_id, (SCI_CB)chk_kbd_interrupt);
} else {
set_kbd_interrupt((int)self->uart_id, (SCI_CB)NULL);
}
#endif
}
mp_uint_t uart_rx_any(machine_uart_obj_t *self) {
int ch = (int)self->uart_id;
return ra_sci_rx_any(ch);
}
mp_uint_t uart_tx_avail(machine_uart_obj_t *self) {
int ch = (int)self->uart_id;
return ra_sci_tx_wait(ch);
}
mp_uint_t uart_tx_busy(machine_uart_obj_t *self) {
int ch = (int)self->uart_id;
return ra_sci_tx_busy(ch);
}
mp_uint_t uart_tx_txbuf(machine_uart_obj_t *self) {
int ch = (int)self->uart_id;
return ra_sci_tx_bufsize(ch);
}
// Waits at most timeout milliseconds for at least 1 char to become ready for
// reading (from buf or for direct reading).
// Returns true if something available, false if not.
bool uart_rx_wait(machine_uart_obj_t *self, uint32_t timeout) {
int ch = (int)self->uart_id;
uint32_t start = HAL_GetTick();
for (;;) {
if (ra_sci_rx_any(ch)) {
return true;
}
if (HAL_GetTick() - start >= timeout) {
return false; // timeout
}
MICROPY_EVENT_POLL_HOOK
}
}
// assumes there is a character available
int uart_rx_char(machine_uart_obj_t *self) {
int ch = (int)self->uart_id;
return ra_sci_rx_ch(ch);
}
// Waits at most timeout milliseconds for TX register to become empty.
// Returns true if can write, false if can't.
bool uart_tx_wait(machine_uart_obj_t *self, uint32_t timeout) {
uint32_t start = HAL_GetTick();
for (;;) {
if (uart_tx_avail(self)) {
return true;
}
if (HAL_GetTick() - start >= timeout) {
return false; // timeout
}
MICROPY_EVENT_POLL_HOOK
}
}
// src - a pointer to the data to send (16-bit aligned for 9-bit chars)
// num_chars - number of characters to send (9-bit chars count for 2 bytes from src)
// *errcode - returns 0 for success, MP_Exxx on error
// returns the number of characters sent (valid even if there was an error)
size_t uart_tx_data(machine_uart_obj_t *self, const void *src_in, size_t num_chars, int *errcode) {
int ch = (int)self->uart_id;
uint8_t *d8 = (uint8_t *)src_in;
uint16_t *d16 = (uint16_t *)src_in;
if (num_chars == 0) {
*errcode = 0;
return 0;
}
int i;
if (self->char_width == CHAR_WIDTH_9BIT) {
for (i = 0; i < (int)num_chars; i++) {
ra_sci_tx_ch(ch, (int)*d16++);
}
} else {
for (i = 0; i < (int)num_chars; i++) {
ra_sci_tx_ch(ch, (int)*d8++);
}
}
*errcode = 0;
return (size_t)num_chars;
}
void uart_tx_strn(machine_uart_obj_t *uart_obj, const char *str, uint len) {
int errcode;
uart_tx_data(uart_obj, str, len, &errcode);
}
STATIC mp_uint_t uart_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) {
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
uart_irq_config(self, false);
self->mp_irq_trigger = new_trigger;
uart_irq_config(self, true);
return 0;
}
STATIC mp_uint_t uart_irq_info(mp_obj_t self_in, mp_uint_t info_type) {
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (info_type == MP_IRQ_INFO_FLAGS) {
return self->mp_irq_flags;
} else if (info_type == MP_IRQ_INFO_TRIGGERS) {
return self->mp_irq_trigger;
}
return 0;
}
const mp_irq_methods_t uart_irq_methods = {
.trigger = uart_irq_trigger,
.info = uart_irq_info,
};