8000 Switch to port_serial_* hooks · mimoccc/circuitpython@f5d90fc · GitHub
[go: up one dir, main page]

Skip to content

Commit f5d90fc

Browse files
committed
Switch to port_serial_* hooks
This makes it easier to integrate port specific serial alongside the common approaches.
1 parent 110857c commit f5d90fc

File tree

7 files changed

+70
-214
lines changed

7 files changed

+70
-214
lines changed

ports/espressif/supervisor/serial.c

Lines changed: 5 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -24,104 +24,20 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
// This file will only be used when CIRCUITPY_USB is 0. See
28-
// supervisor/supervisor.mk for the rule that applies.
29-
30-
#include <stdarg.h>
31-
#include <string.h>
32-
3327
#include "py/mpconfig.h"
34-
#include "supervisor/shared/cpu.h"
35-
#include "supervisor/shared/display.h"
36-
#include "shared-bindings/terminalio/Terminal.h"
3728
#include "supervisor/serial.h"
38-
#include "shared-bindings/microcontroller/Pin.h"
39-
40-
#if CIRCUITPY_SERIAL_BLE
41-
#include "supervisor/shared/bluetooth/serial.h"
42-
#endif
43-
44-
#if defined(CIRCUITPY_DEBUG_UART_TX) || defined(CIRCUITPY_DEBUG_UART_RX)
45-
#include "py/mpprint.h"
46-
#include "shared-bindings/busio/UART.h"
47-
busio_uart_obj_t debug_uart;
48-
byte buf_array[64];
49-
#endif
5029

5130
#if CIRCUITPY_ESP_USB_SERIAL_JTAG
5231
#include "supervisor/usb_serial_jtag.h"
5332
#endif
5433

55-
#if defined(CIRCUITPY_DEBUG_UART_TX)
56-
STATIC void debug_uart_print_strn(void *env, const char *str, size_t len) {
57-
(void)env;
58-
int uart_errcode;
59-
common_hal_busio_uart_write(&debug_uart, (const uint8_t *)str, len, &uart_errcode);
60-
}
61-
62-
const mp_print_t debug_uart_print = {NULL, debug_uart_print_strn};
63-
#endif
64-
65-
int debug_uart_printf(const char *fmt, ...) {
66-
#if defined(CIRCUITPY_DEBUG_UART_TX)
67-
// Skip prints that occur before debug serial is started. It's better than
68-
// crashing.
69-
if (common_hal_busio_uart_deinited(&debug_uart)) {
70-
return 0;
71-
}
72-
va_list ap;
73-
va_start(ap, fmt);
74-
int ret = mp_vprintf(&debug_uart_print, fmt, ap);
75-
va_end(ap);
76-
return ret;
77-
#else
78-
return 0;
79-
#endif
80-
}
81-
82-
void serial_early_init(void) {
83-
#if defined(CIRCUITPY_DEBUG_UART_TX) || defined(CIRCUITPY_DEBUG_UART_RX)
84-
debug_uart.base.type = &busio_uart_type;
85-
86-
#if defined(CIRCUITPY_DEBUG_UART_RX)
87-
const mcu_pin_obj_t *rx = MP_OBJ_TO_PTR(CIRCUITPY_DEBUG_UART_RX);
88-
#else
89-
const mcu_pin_obj_t *rx = NULL;
90-
#endif
91-
92-
#if defined(CIRCUITPY_DEBUG_UART_TX)
93-
const mcu_pin_obj_t *tx = MP_OBJ_TO_PTR(CIRCUITPY_DEBUG_UART_TX);
94-
#else
95-
const mcu_pin_obj_t *tx = NULL;
96-
#endif
97-
98-
common_hal_busio_uart_construct(&debug_uart, tx, rx, NULL, NULL, NULL,
99-
false, 115200, 8, BUSIO_UART_PARITY_NONE, 1, 1.0f, 64,
100-
buf_array, true);
101-
common_hal_busio_uart_never_reset(&debug_uart);
102-
103-
// Do an initial print so that we can confirm the serial output is working.
104-
debug_uart_printf("Serial debug setup\r\n");
105-
#endif
106-
}
107-
108-
void serial_init(void) {
34+
void port_serial_init(void) {
10935
#if CIRCUITPY_ESP_USB_SERIAL_JTAG
11036
usb_serial_jtag_init();
11137
#endif
11238
}
11339

114-
bool serial_connected(void) {
115-
#if defined(CIRCUITPY_DEBUG_UART_TX) && defined(CIRCUITPY_DEBUG_UART_RX)
116-
return true;
117-
#endif
118-
119-
#if CIRCUITPY_SERIAL_BLE
120-
if (ble_serial_connected()) {
121-
return true;
122-
}
123-
#endif
124-
40+
bool port_serial_connected(void) {
12541
#if CIRCUITPY_ESP_USB_SERIAL_JTAG
12642
if (usb_serial_jtag_connected()) {
12743
return true;
@@ -131,23 +47,7 @@ bool serial_connected(void) {
13147
return false;
13248
}
13349

134-
char serial_read(void) {
135-
136-
#if defined(CIRCUITPY_DEBUG_UART_RX)
137-
if (common_hal_busio_uart_rx_characters_available(&debug_uart)) {
138-
int uart_errcode;
139-
char text;
140-
common_hal_busio_uart_read(&debug_uart, (uint8_t *)&text, 1, &uart_errcode);
141-
return text;
142-
}
143-
#endif
144-
145-
#if CIRCUITPY_SERIAL_BLE
146-
if (ble_serial_available() > 0) {
147-
return ble_serial_read_char();
148-
}
149-
#endif
150-
50+
char port_serial_read(void) {
15151
#if CIRCUITPY_ESP_USB_SERIAL_JTAG
15252
if (usb_serial_jtag_bytes_available() > 0) {
15353
return usb_serial_jtag_read_char();
@@ -156,19 +56,7 @@ char serial_read(void) {
15656
return -1;
15757
}
15858

159-
bool serial_bytes_available(void) {
160-
#if defined(CIRCUITPY_DEBUG_UART_RX)
161-
if (common_hal_busio_uart_rx_characters_available(&debug_uart)) {
162-
return true;
163-
}
164-
#endif
165-
166-
#if CIRCUITPY_SERIAL_BLE
167-
if (ble_serial_available()) {
168-
return true;
169-
}
170-
#endif
171-
59+
bool port_serial_bytes_available(void) {
17260
#if CIRCUITPY_ESP_USB_SERIAL_JTAG
17361
if (usb_serial_jtag_bytes_available()) {
17462
return true;
@@ -178,30 +66,8 @@ bool serial_bytes_available(void) {
17866
return false;
17967
}
18068

181-
void serial_write_substring(const char *text, uint32_t length) {
182-
if (length == 0) {
183-
return;
184-
}
185-
#if CIRCUITPY_TERMINALIO
186-
int errcode;
187-
common_hal_terminalio_terminal_write(&supervisor_terminal, (const uint8_t *)text, length, &errcode);
188-
#endif
189-
190-
#if defined(CIRCUITPY_DEBUG_UART_TX)
191-
int uart_errcode;
192-
193-
common_hal_busio_uart_write(&debug_uart, (const uint8_t *)text, length, &uart_errcode);
194-
#endif
195-
196-
#if CIRCUITPY_SERIAL_BLE
197-
ble_serial_write(text, length);
198-
#endif
199-
69+
void port_serial_write_substring(const char *text, uint32_t length) {
20070
#if CIRCUITPY_ESP_USB_SERIAL_JTAG
20171
usb_serial_jtag_write(text, length);
20272
#endif
20373
}
204-
205-
void serial_write(const char *text) {
206-
serial_write_substring(text, strlen(text));
207-
}

ports/espressif/supervisor/workflow.c

Lines changed: 0 additions & 43 deletions
This file was deleted.

ports/mimxrt10xx/supervisor/serial.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "fsl_clock.h"
3434
#include "fsl_lpuart.h"
3535

36+
// TODO: Switch this to using DEBUG_UART.
37+
3638
// static LPUART_Type *uart_instance = LPUART1; // evk
3739
static LPUART_Type *uart_instance = LPUART4; // feather 1011
3840
// static LPUART_Type *uart_instance = LPUART2; // feather 1062
@@ -52,7 +54,7 @@ static uint32_t UartSrcFreq(void) {
5254
return freq;
5355
}
5456

55-
void serial_init(void) {
57+
void port_serial_init(void) {
5658
lpuart_config_t config;
5759

5860
LPUART_GetDefaultConfig(&config);
@@ -63,27 +65,23 @@ void serial_init(void) {
6365
LPUART_Init(uart_instance, &config, UartSrcFreq());
6466
}
6567

66-
bool serial_connected(void) {
68+
bool port_serial_connected(void) {
6769
return true;
6870
}
6971

70-
char serial_read(void) {
72+
char port_serial_read(void) {
7173
uint8_t data;
7274

7375
LPUART_ReadBlocking(uart_instance, &data, sizeof(data));
7476

7577
return data;
7678
}
7779

78-
bool serial_bytes_available(void) {
80+
bool port_serial_bytes_available(void) {
7981
return LPUART_GetStatusFlags(uart_instance) & kLPUART_RxDataRegFullFlag;
8082
}
8183

82-
void serial_write(const char *text) {
83-
LPUART_WriteBlocking(uart_instance, (uint8_t *)text, strlen(text));
84-
}
85-
86-
void serial_write_substring(const char *text, uint32_t len) {
84+
void port_serial_write_substring(const char *text, uint32_t len) {
8785
if (len == 0) {
8886
return;
8987
}

ports/stm/supervisor/serial.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
#include "stm32f4xx_hal.h"
3232
#include "stm32f4/gpio.h"
3333

34+
// TODO: Switch this to using DEBUG_UART.
35+
3436
UART_HandleTypeDef huart2;
3537

36-
void serial_init(void) {
38+
void port_serial_init(void) {
3739
huart2.Instance = USART2;
3840
huart2.Init.BaudRate = 115200;
3941
huart2.Init.WordLength = UART_WORDLENGTH_8B;
@@ -47,25 +49,21 @@ void serial_init(void) {
4749
}
4850
}
4951

50-
bool serial_connected(void) {
52+
bool port_serial_connected(void) {
5153
return true;
5254
}
5355

54-
char serial_read(void) {
56+
char port_serial_read(void) {
5557
uint8_t data;
5658
HAL_UART_Receive(&huart2, &data, 1,500);
5759
return data;
5860
}
5961

60-
bool serial_bytes_available(void) {
62+
bool port_serial_bytes_available(void) {
6163
return __HAL_UART_GET_FLAG(&huart2, UART_FLAG_RXNE);
6264
}
6365

64-
void serial_write(const char *text) {
65-
serial_write_substring(text, strlen(text));
66-
}
67-
68-
void serial_write_substring(const char *text, uint32_t len) {
66+
void port_serial_write_substring(const char *text, uint32_t len) {
6967
if (len == 0) {
7068
return;
7169
}

supervisor/serial.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
extern vstr_t *boot_output;
4040
#endif
4141

42+
4243
void serial_early_init(void);
4344
void serial_init(void);
4445
void serial_write(const char *text);
@@ -48,6 +49,14 @@ char serial_read(void);
4849
bool serial_bytes_available(void);
4950
bool serial_connected(void);
5051

52+
// These have no-op versions that are weak and the port can override. They work
53+
// in tandem with the cross-port mechanics like USB and BLE.
54+
void port_serial_init(void);
55+
bool port_serial_connected(void);
56+
char port_serial_read(void);
57+
bool port_serial_bytes_available(void);
58+
void port_serial_write_substring(const char *text, uint32_t length);
59+
5160
int debug_uart_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
5261

5362
#endif // MICROPY_INCLUDED_SUPERVISOR_SERIAL_H

0 commit comments

Comments
 (0)
0