forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathuart_core.c
145 lines (118 loc) · 3.87 KB
/
uart_core.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
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Linaro Limited
*
* 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 <unistd.h>
#include "py/mpconfig.h"
#include "py/runtime.h"
#include "src/zephyr_getchar.h"
// Zephyr headers
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/console/console.h>
#include <zephyr/console/tty.h>
#include <zephyr/drivers/uart.h>
#ifdef CONFIG_CONSOLE_SUBSYS
static int mp_console_putchar(char c);
static int mp_console_getchar(void);
static struct tty_serial mp_console_serial;
static uint8_t mp_console_rxbuf[CONFIG_CONSOLE_GETCHAR_BUFSIZE];
static uint8_t mp_console_txbuf[CONFIG_CONSOLE_PUTCHAR_BUFSIZE];
#endif // CONFIG_CONSOLE_SUBSYS
/*
* Core UART functions to implement for a port
*/
// Receive single character
int mp_hal_stdin_rx_chr(void) {
for (;;) {
int _chr;
#ifdef CONFIG_CONSOLE_SUBSYS
_chr = mp_console_getchar();
#else
_chr = zephyr_getchar();
#endif
if (_chr >= 0) {
return _chr;
}
MICROPY_EVENT_POLL_HOOK
}
}
// Send string of given length
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
mp_uint_t ret = len;
#ifdef CONFIG_CONSOLE_SUBSYS
while (len--) {
char c = *str++;
while (mp_console_putchar(c) == -1) {
MICROPY_EVENT_POLL_HOOK
}
}
#else
static const struct device *uart_console_dev =
DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
while (len--) {
uart_poll_out(uart_console_dev, *str++);
}
#endif
return ret;
}
#ifdef CONFIG_CONSOLE_SUBSYS
int mp_console_init(void) {
const struct device *uart_dev;
int ret;
uart_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
if (!device_is_ready(uart_dev)) {
return -ENODEV;
}
ret = tty_init(&mp_console_serial, uart_dev);
if (ret) {
return ret;
}
/* Checks device driver supports for interrupt driven data transfers. */
if (CONFIG_CONSOLE_GETCHAR_BUFSIZE + CONFIG_CONSOLE_PUTCHAR_BUFSIZE) {
const struct uart_driver_api *api =
(const struct uart_driver_api *)uart_dev->api;
if (!api->irq_callback_set) {
return -ENOTSUP;
}
}
tty_set_tx_buf(&mp_console_serial, mp_console_txbuf, sizeof(mp_console_txbuf));
tty_set_rx_buf(&mp_console_serial, mp_console_rxbuf, sizeof(mp_console_rxbuf));
tty_set_rx_timeout(&mp_console_serial, 0);
tty_set_tx_timeout(&mp_console_serial, 1);
return 0;
}
static int mp_console_putchar(char c) {
return tty_write(&mp_console_serial, &c, 1);
}
static int mp_console_getchar(void) {
uint8_t c;
int res;
res = tty_read(&mp_console_serial, &c, 1);
if (res < 0) {
return res;
}
return c;
}
#endif // CONFIG_CONSOLE_SUBSYS