Skip to content

Commit 7c2fc8e

Browse files
committed
Enable showing the console on a debug uart
1 parent 4e786fa commit 7c2fc8e

File tree

8 files changed

+100
-9
lines changed

8 files changed

+100
-9
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/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/common-hal/busio/UART.c

Lines changed: 34 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,24 @@ 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, ringbuf_t *static_rbuf) {
7784

7885
//match pins to UART objects
7986
USART_TypeDef * USARTx;
@@ -209,8 +216,12 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
209216

210217
// Init buffer for rx and claim pins
211218
if (self->rx != NULL) {
212-
if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
213-
mp_raise_ValueError(translate("UART Buffer allocation error"));
219+
if (static_rbuf) {
220+
self->ringbuf = *static_rbuf;
221+
} else {
222+
if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
223+
mp_raise_ValueError(translate("UART Buffer allocation error"));
224+
}
214225
}
215226
claim_pin(rx);
216227
}
@@ -234,6 +245,17 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
234245
errflag = HAL_OK;
235246
}
236247

248+
void common_hal_busio_uart_never_reset(busio_uart_obj_t *self) {
249+
for (size_t i = 0; i < MP_ARRAY_SIZE(mcu_uart_banks); i++) {
250+
if (mcu_uart_banks[i] == self->handle.Instance) {
251+
never_reset_uart[i] = true;
252+
never_reset_pin_number(self->tx->pin->port, self->tx->pin->number);
253+
never_reset_pin_number(self->rx->pin->port, self->rx->pin->number);
254+
break;
255+
}
256+
}
257+
}
258+
237259
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
238260
return self->tx->pin == NULL;
239261
}
@@ -289,7 +311,8 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
289311
bool write_err = false; //write error shouldn't disable interrupts
290312

291313
HAL_NVIC_DisableIRQ(self->irq);
292-
if (HAL_UART_Transmit(&self->handle, (uint8_t*)data, len, HAL_MAX_DELAY) != HAL_OK) {
314+
HAL_StatusTypeDef ret = HAL_UART_Transmit(&self->handle, (uint8_t*)data, len, HAL_MAX_DELAY);
315+
if (ret != HAL_OK) {
293316
write_err = true;
294317
}
295318
HAL_UART_Receive_IT(&self->handle, &self->rx_char, 1);
@@ -313,6 +336,10 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle)
313336
}
314337
ringbuf_put_n(&context->ringbuf, &context->rx_char, 1);
315338
errflag = HAL_UART_Receive_IT(handle, &context->rx_char, 1);
339+
if (context->rx_char == CHAR_CTRL_C) {
340+
common_hal_busio_uart_clear_rx_buffer(context);
341+
mp_keyboard_interrupt();
342+
}
316343

317344
return;
318345
}

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);
144144
return (mp_obj_t)self;
145145
}
146146

shared-bindings/busio/UART.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ extern void common_hal_busio_uart_construct(busio_uart_obj_t *self,
4444
const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts,
4545
const mcu_pin_obj_t * rs485_dir, bool rs485_invert,
4646
uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop,
47-
mp_float_t timeout, uint16_t receiver_buffer_size);
47+
mp_float_t timeout, uint16_t receiver_buffer_size, ringbuf_t *static_rbuf);
4848

4949
extern void common_hal_busio_uart_deinit(busio_uart_obj_t *self);
5050
extern bool common_hal_busio_uart_deinited(busio_uart_obj_t *self);
@@ -66,4 +66,6 @@ extern uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *
6666
extern void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self);
6767
extern bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self);
6868

69+
extern void common_hal_busio_uart_never_reset(busio_uart_obj_t *self);
70+
6971
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_UART_H

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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,72 @@
3232
#include "shared-bindings/terminalio/Terminal.h"
3333
#include "supervisor/serial.h"
3434
#include "supervisor/usb.h"
35+
#include "shared-bindings/busio/UART.h"
36+
#include "shared-bindings/microcontroller/Pin.h"
3537

3638
#include "tusb.h"
3739

40+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
41+
busio_uart_obj_t debug_uart;
42+
byte buf_array[64];
43+
ringbuf_t rbuf;
44+
#endif
45+
46+
void serial_early_init(void) {
47+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
48+
rbuf = (ringbuf_t){ buf_array, sizeof(buf_array) };
49+
assert_pin_free(DEBUG_UART_TX);
50+
assert_pin_free(DEBUG_UART_RX);
51+
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, false,
58+
115200, 8, PARITY_NONE, 1, 1.0f, 64, &rbuf);
59+
common_hal_busio_uart_never_reset(&debug_uart);
60+
#endif
61+
}
62+
3863
void serial_init(void) {
3964
usb_init();
4065
}
4166

4267
bool serial_connected(void) {
68+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
69+
return true;
70+
#else
4371
return tud_cdc_connected();
72+
#endif
4473
}
4574

4675
char serial_read(void) {
76+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
77+
if (tud_cdc_connected() && tud_cdc_available() > 0) {
78+
return (char) tud_cdc_read_char();
79+
}
80+
int uart_errcode;
81+
char text;
82+
common_hal_busio_uart_read(&debug_uart, (uint8_t*) &text, 1, &uart_errcode);
83+
return text;
84+
#else
4785
return (char) tud_cdc_read_char();
86+
#endif
4887
}
4988

5089
bool serial_bytes_available(void) {
90+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
91+
return common_hal_busio_uart_rx_characters_available(&debug_uart) || (tud_cdc_available() > 0);
92+
#else
5193
return tud_cdc_available() > 0;
94+
#endif
5295
}
5396

5497
void serial_write_substring(const char* text, uint32_t length) {
98+
if (length == 0) { // TODO(mark) this is needed because of 'lib/mp-readline/readline.c:71'
99+
return;
100+
}
55101
#if CIRCUITPY_DISPLAYIO
56102
int errcode;
57103
common_hal_terminalio_terminal_write(&supervisor_terminal, (const uint8_t*) text, length, &errcode);
@@ -62,6 +108,11 @@ void serial_write_substring(const char* text, uint32_t length) {
62108
count += tud_cdc_write(text + count, length - count);
63109
usb_background();
64110
}
111+
112+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
113+
int uart_errcode;
114+
common_hal_busio_uart_write(&debug_uart, (const uint8_t*) text, length, &uart_errcode);
115+
#endif
65116
}
66117

67118
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)