Skip to content

Commit 007c92e

Browse files
committed
Enable showing the console on a debug uart
1 parent 6a5ab57 commit 007c92e

File tree

15 files changed

+128
-16
lines changed

15 files changed

+128
-16
lines changed

main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ int __attribute__((used)) main(void) {
430430
// displays init after filesystem, since they could share the flash SPI
431431
board_init();
432432

433+
// Start the debug serial
434+
serial_early_init();
435+
433436
// Reset everything and prep MicroPython to run boot.py.
434437
reset_port();
435438
reset_board();

ports/atmel-samd/common-hal/busio/UART.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
5656
const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts,
5757
const mcu_pin_obj_t * rs485_dir, bool rs485_invert,
5858
uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop,
59-
mp_float_t timeout, uint16_t receiver_buffer_size) {
59+
mp_float_t timeout, uint16_t receiver_buffer_size, byte* receiver_buffer,
60+
bool sigint_enabled) {
6061

6162
Sercom* sercom = NULL;
6263
uint8_t sercom_index = 255; // Unset index

ports/cxd56/common-hal/busio/UART.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
5757
const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts,
5858
const mcu_pin_obj_t * rs485_dir, bool rs485_invert,
5959
uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop,
60-
mp_float_t timeout, uint16_t receiver_buffer_size) {
60+
mp_float_t timeout, uint16_t receiver_buffer_size, byte* receiver_buffer,
61+
bool sigint_enabled) {
6162
struct termios tio;
6263

6364
if ((rts != NULL) || (cts != NULL) || (rs485_dir != NULL) || (rs485_invert)) {

ports/mimxrt10xx/common-hal/busio/UART.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
7676
const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts,
7777
const mcu_pin_obj_t * rs485_dir, bool rs485_invert,
7878
uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop,
79-
mp_float_t timeout, uint16_t receiver_buffer_size) {
79+
mp_float_t timeout, uint16_t receiver_buffer_size, byte* receiver_buffer,
80+
bool sigint_enabled) {
8081

8182
// TODO: Allow none rx or tx
8283

ports/nrf/common-hal/busio/UART.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
134134
const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts,
135135
const mcu_pin_obj_t * rs485_dir, bool rs485_invert,
136136
uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop,
137-
mp_float_t timeout, uint16_t receiver_buffer_size) {
137+
mp_float_t timeout, uint16_t receiver_buffer_size, byte* receiver_buffer,
138+
bool sigint_enabled) {
138139

139140
if ((rts != NULL) || (cts != NULL) || (rs485_dir != NULL) || (rs485_invert)) {
140141
mp_raise_ValueError(translate("RTS/CTS/RS485 Not yet supported on this device"));

ports/stm/boards/nucleo_f746zg/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@
3434
#define FLASH_PAGE_SIZE (0x4000)
3535

3636
#define BOARD_OSC_DIV (8)
37+
38+
#define DEBUG_UART_TX (&pin_PD08)
39+
#define DEBUG_UART_RX (&pin_PD09)

ports/stm/boards/nucleo_f746zg/pins.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
5858
{ MP_ROM_QSTR(MP_QSTR_USB_ID), MP_ROM_PTR(&pin_PA10) },
5959
{ MP_ROM_QSTR(MP_QSTR_USB_DM), MP_ROM_PTR(&pin_PA11) },
6060
{ MP_ROM_QSTR(MP_QSTR_USB_DP), MP_ROM_PTR(&pin_PA12) },
61-
{ MP_ROM_QSTR(MP_QSTR_VCP_TX), MP_ROM_PTR(&pin_PD08) },
62-
{ MP_ROM_QSTR(MP_QSTR_VCP_RX), MP_ROM_PTR(&pin_PD09) },
61+
// As we use these for the debug_console, we won't enable them here.
62+
// { MP_ROM_QSTR(MP_QSTR_VCP_TX), MP_ROM_PTR(&pin_PD08) },
63+
// { MP_ROM_QSTR(MP_QSTR_VCP_RX), MP_ROM_PTR(&pin_PD09) },
6364
{ MP_ROM_QSTR(MP_QSTR_UART2_TX), MP_ROM_PTR(&pin_PD05) },
6465
{ MP_ROM_QSTR(MP_QSTR_UART2_RX), MP_ROM_PTR(&pin_PD06) },
6566
{ MP_ROM_QSTR(MP_QSTR_UART2_RTS), MP_ROM_PTR(&pin_PD04) },

ports/stm/common-hal/busio/UART.c

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "shared-bindings/busio/UART.h"
2929

3030
#include "mpconfigport.h"
31+
#include "lib/mp-readline/readline.h"
3132
#include "lib/utils/interrupt_char.h"
3233
#include "py/gc.h"
3334
#include "py/mperrno.h"
@@ -39,6 +40,7 @@
3940

4041
//arrays use 0 based numbering: UART1 is stored at index 0
4142
STATIC bool reserved_uart[MAX_UART];
43+
STATIC bool never_reset_uart[MAX_UART];
4244
int errflag; //Used to restart read halts
4345

4446
STATIC void uart_clock_enable(uint16_t mask);
@@ -61,19 +63,25 @@ STATIC USART_TypeDef * assign_uart_or_throw(busio_uart_obj_t* self, bool pin_eva
6163
}
6264

6365
void uart_reset(void) {
66+
uint16_t never_reset_mask = 0x00;
6467
for (uint8_t i = 0; i < MAX_UART; i++) {
65-
reserved_uart[i] = false;
66-
MP_STATE_PORT(cpy_uart_obj_all)[i] = NULL;
68+
if (!never_reset_uart[i]) {
69+
reserved_uart[i] = false;
70+
MP_STATE_PORT(cpy_uart_obj_all)[i] = NULL;
71+
} else {
72+
never_reset_mask |= 1 << i;
73+
}
6774
}
68-
uart_clock_disable(ALL_UARTS);
75+
uart_clock_disable(ALL_UARTS & ~(never_reset_mask));
6976
}
7077

7178
void common_hal_busio_uart_construct(busio_uart_obj_t *self,
7279
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx,
7380
const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts,
7481
const mcu_pin_obj_t * rs485_dir, bool rs485_invert,
7582
uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop,
76-
mp_float_t timeout, uint16_t receiver_buffer_size) {
83+
mp_float_t timeout, uint16_t receiver_buffer_size, byte* receiver_buffer,
84+
bool sigint_enabled) {
7785

7886
//match pins to UART objects
7987
USART_TypeDef * USARTx;
@@ -209,8 +217,12 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
209217

210218
// Init buffer for rx and claim pins
211219
if (self->rx != NULL) {
212-
if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
213-
mp_raise_ValueError(translate("UART Buffer allocation error"));
220+
if (receiver_buffer != NULL) {
221+
self->ringbuf = (ringbuf_t){ receiver_buffer, receiver_buffer_size };
222+
} else {
223+
if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
224+
mp_raise_ValueError(translate("UART Buffer allocation error"));
225+
}
214226
}
215227
claim_pin(rx);
216228
}
@@ -219,6 +231,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
219231
}
220232
self->baudrate = baudrate;
221233
self->timeout_ms = timeout * 1000;
234+
self->sigint_enabled = sigint_enabled;
222235

223236
//start the interrupt series
224237
if ((HAL_UART_GetState(&self->handle) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX) {
@@ -234,13 +247,31 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
234247
errflag = HAL_OK;
235248
}
236249

250+
void common_hal_busio_uart_never_reset(busio_uart_obj_t *self) {
251+
for (size_t i = 0; i < MP_ARRAY_SIZE(mcu_uart_banks); i++) {
252+
if (mcu_uart_banks[i] == self->handle.Instance) {
253+
never_reset_uart[i] = true;
254+
never_reset_pin_number(self->tx->pin->port, self->tx->pin->number);
255+
never_reset_pin_number(self->rx->pin->port, self->rx->pin->number);
256+
break;
257+
}
258+
}
259+
}
260+
237261
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
238262
return self->tx->pin == NULL;
239263
}
240264

241265
void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
242266
if (common_hal_busio_uart_deinited(self)) return;
243267

268+
for (size_t i = 0; i < MP_ARRAY_SIZE(mcu_uart_banks); i++) {
269+
if (mcu_uart_banks[i] == self->handle.Instance) {
270+
never_reset_uart[i] = false;
271+
break;
272+
}
273+
}
274+
244275
reset_pin_number(self->tx->pin->port,self->tx->pin->number);
245276
reset_pin_number(self->rx->pin->port,self->rx->pin->number);
246277
self->tx = NULL;
@@ -289,7 +320,8 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
289320
bool write_err = false; //write error shouldn't disable interrupts
290321

291322
HAL_NVIC_DisableIRQ(self->irq);
292-
if (HAL_UART_Transmit(&self->handle, (uint8_t*)data, len, HAL_MAX_DELAY) != HAL_OK) {
323+
HAL_StatusTypeDef ret = HAL_UART_Transmit(&self->handle, (uint8_t*)data, len, HAL_MAX_DELAY);
324+
if (ret != HAL_OK) {
293325
write_err = true;
294326
}
295327
HAL_UART_Receive_IT(&self->handle, &self->rx_char, 1);
@@ -313,6 +345,12 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle)
313345
}
314346
ringbuf_put_n(&context->ringbuf, &context->rx_char, 1);
315347
errflag = HAL_UART_Receive_IT(handle, &context->rx_char, 1);
348+
if (context->sigint_enabled) {
349+
if (context->rx_char == CHAR_CTRL_C) {
350+
common_hal_busio_uart_clear_rx_buffer(context);
351+
mp_keyboard_interrupt();
352+
}
353+
}
316354

317355
return;
318356
}

ports/stm/common-hal/busio/UART.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ typedef struct {
5252

5353
uint32_t baudrate;
5454
uint32_t timeout_ms;
55+
56+
bool sigint_enabled;
5557
} busio_uart_obj_t;
5658

5759
void uart_reset(void);

shared-bindings/busio/UART.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ STATIC mp_obj_t busio_uart_make_new(const mp_obj_type_t *type, size_t n_args, co
140140

141141
common_hal_busio_uart_construct(self, tx, rx, rts, cts, rs485_dir, rs485_invert,
142142
args[ARG_baudrate].u_int, bits, parity, stop, timeout,
143-
args[ARG_receiver_buffer_size].u_int);
143+
args[ARG_receiver_buffer_size].u_int, NULL, false);
144144
return (mp_obj_t)self;
145145
}
146146

shared-bindings/busio/UART.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "common-hal/microcontroller/Pin.h"
3131
#include "common-hal/busio/UART.h"
32+
#include "py/ringbuf.h"
3233

3334
extern const mp_obj_type_t busio_uart_type;
3435

@@ -44,7 +45,8 @@ extern void common_hal_busio_uart_construct(busio_uart_obj_t *self,
4445
const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts,
4546
const mcu_pin_obj_t * rs485_dir, bool rs485_invert,
4647
uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop,
47-
mp_float_t timeout, uint16_t receiver_buffer_size);
48+
mp_float_t timeout, uint16_t receiver_buffer_size, byte* receiver_buffer,
49+
bool sigint_enabled);
4850

4951
extern void common_hal_busio_uart_deinit(busio_uart_obj_t *self);
5052
extern bool common_hal_busio_uart_deinited(busio_uart_obj_t *self);
@@ -66,4 +68,6 @@ extern uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *
6668
extern void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self);
6769
extern bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self);
6870

71+
extern void common_hal_busio_uart_never_reset(busio_uart_obj_t *self);
72+
6973
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_UART_H

shared-module/board/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ mp_obj_t common_hal_board_create_uart(void) {
123123
#endif
124124

125125
common_hal_busio_uart_construct(self, tx, rx, rts, cts, rs485_dir, rs485_invert,
126-
9600, 8, PARITY_NONE, 1, 1.0f, 64);
126+
9600, 8, PARITY_NONE, 1, 1.0f, 64, NULL, false);
127127
MP_STATE_VM(shared_uart_bus) = MP_OBJ_FROM_PTR(self);
128128
return MP_STATE_VM(shared_uart_bus);
129129
}

supervisor/serial.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
FIL* boot_output_file;
3939
#endif
4040

41+
void serial_early_init(void);
4142
void serial_init(void);
4243
void serial_write(const char* text);
4344
// Only writes up to given length. Does not check for null termination at all.

supervisor/shared/serial.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,73 @@
3232
#include "shared-bindings/terminalio/Terminal.h"
3333
#include "supervisor/serial.h"
3434
#include "supervisor/usb.h"
35+
#include "shared-bindings/microcontroller/Pin.h"
3536

3637
#include "tusb.h"
3738

39+
/*
40+
* Note: DEBUG_UART currently only works on STM32,
41+
* enabling on another platform will cause a crash.
42+
*/
43+
44+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
45+
#include "shared-bindings/busio/UART.h"
46+
busio_uart_obj_t debug_uart;
47+
byte buf_array[64];
48+
#endif
49+
50+
void serial_early_init(void) {
51+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
52+
debug_uart.base.type = &busio_uart_type;
53+
54+
const mcu_pin_obj_t* rx = MP_OBJ_TO_PTR(DEBUG_UART_RX);
55+
const mcu_pin_obj_t* tx = MP_OBJ_TO_PTR(DEBUG_UART_TX);
56+
57+
common_hal_busio_uart_construct(&debug_uart, tx, rx, NULL, NULL, NULL,
58+
false, 115200, 8, PARITY_NONE, 1, 1.0f, 64,
59+
buf_array, true);
60+
common_hal_busio_uart_never_reset(&debug_uart);
61+
#endif
62+
}
63+
3864
void serial_init(void) {
3965
usb_init();
4066
}
4167

4268
bool serial_connected(void) {
69+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
70+
return true;
71+
#else
4372
return tud_cdc_connected();
73+
#endif
4474
}
4575

4676
char serial_read(void) {
77+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
78+
if (tud_cdc_connected() && tud_cdc_available() > 0) {
79+
return (char) tud_cdc_read_char();
80+
}
81+
int uart_errcode;
82+
char text;
83+
common_hal_busio_uart_read(&debug_uart, (uint8_t*) &text, 1, &uart_errcode);
84+
return text;
85+
#else
4786
return (char) tud_cdc_read_char();
87+
#endif
4888
}
4989

5090
bool serial_bytes_available(void) {
91+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
92+
return common_hal_busio_uart_rx_characters_available(&debug_uart) || (tud_cdc_available() > 0);
93+
#else
5194
return tud_cdc_available() > 0;
95+
#endif
5296
}
5397

5498
void serial_write_substring(const char* text, uint32_t length) {
99+
if (length == 0) {
100+
return;
101+
}
55102
#if CIRCUITPY_DISPLAYIO
56103
int errcode;
57104
common_hal_terminalio_terminal_write(&supervisor_terminal, (const uint8_t*) text, length, &errcode);
@@ -62,6 +109,11 @@ void serial_write_substring(const char* text, uint32_t length) {
62109
count += tud_cdc_write(text + count, length - count);
63110
usb_background();
64111
}
112+
113+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
114+
int uart_errcode;
115+
common_hal_busio_uart_write(&debug_uart, (const uint8_t*) text, length, &uart_errcode);
116+
#endif
65117
}
66118

67119
void serial_write(const char* text) {

supervisor/stub/serial.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
#include "supervisor/serial.h"
2828

29+
void serial_early_init(void) {
30+
31+
}
32+
2933
void serial_init(void) {
3034

3135
}

0 commit comments

Comments
 (0)