Skip to content

Commit fa295bb

Browse files
committed
Enable showing the console on a debug uart
1 parent 90625d1 commit fa295bb

File tree

7 files changed

+104
-6
lines changed

7 files changed

+104
-6
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: 43 additions & 6 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,11 +63,16 @@ 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,
@@ -209,8 +216,10 @@ 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 (self->ringbuf.buf == NULL) {
220+
if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
221+
mp_raise_ValueError(translate("UART Buffer allocation error"));
222+
}
214223
}
215224
claim_pin(rx);
216225
}
@@ -234,6 +243,17 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
234243
errflag = HAL_OK;
235244
}
236245

246+
void common_hal_busio_uart_never_reset(busio_uart_obj_t *self) {
247+
for (size_t i = 0; i < MP_ARRAY_SIZE(mcu_uart_banks); i++) {
248+
if (mcu_uart_banks[i] == self->handle.Instance) {
249+
never_reset_uart[i] = true;
250+
never_reset_pin_number(self->tx->pin->port, self->tx->pin->number);
251+
never_reset_pin_number(self->rx->pin->port, self->rx->pin->number);
252+
break;
253+
}
254+
}
255+
}
256+
237257
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
238258
return self->tx->pin == NULL;
239259
}
@@ -245,6 +265,14 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
245265
reset_pin_number(self->rx->pin->port,self->rx->pin->number);
246266
self->tx = NULL;
247267
self->rx = NULL;
268+
269+
for (size_t i = 0; i < MP_ARRAY_SIZE(mcu_uart_banks); i++) {
270+
if (mcu_uart_banks[i] == self->handle.Instance) {
271+
never_reset_uart[i] = false;
272+
break;
273+
}
274+
}
275+
248276
ringbuf_free(&self->ringbuf);
249277
}
250278

@@ -289,7 +317,8 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
289317
bool write_err = false; //write error shouldn't disable interrupts
290318

291319
HAL_NVIC_DisableIRQ(self->irq);
292-
if (HAL_UART_Transmit(&self->handle, (uint8_t*)data, len, HAL_MAX_DELAY) != HAL_OK) {
320+
HAL_StatusTypeDef ret = HAL_UART_Transmit(&self->handle, (uint8_t*)data, len, HAL_MAX_DELAY);
321+
if (ret != HAL_OK) {
293322
write_err = true;
294323
}
295324
HAL_UART_Receive_IT(&self->handle, &self->rx_char, 1);
@@ -313,6 +342,14 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle)
313342
}
314343
ringbuf_put_n(&context->ringbuf, &context->rx_char, 1);
315344
errflag = HAL_UART_Receive_IT(handle, &context->rx_char, 1);
345+
if (context->rx_char == CHAR_CTRL_C) {
346+
common_hal_busio_uart_clear_rx_buffer(context);
347+
mp_keyboard_interrupt();
348+
}
349+
if (context->rx_char == CHAR_CTRL_U) {
350+
common_hal_busio_uart_deinit(context);
351+
352+
}
316353

317354
return;
318355
}

shared-bindings/busio/UART.h

Lines changed: 3 additions & 0 deletions
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

@@ -66,4 +67,6 @@ extern uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *
6667
extern void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self);
6768
extern bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self);
6869

70+
extern void common_hal_busio_uart_never_reset(busio_uart_obj_t *self);
71+
6972
#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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,68 @@
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+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
40+
#include "shared-bindings/busio/UART.h"
41+
busio_uart_obj_t debug_uart;
42+
byte buf_array[64];
43+
#endif
44+
45+
void serial_early_init(void) {
46+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
47+
debug_uart.ringbuf = (ringbuf_t){ buf_array, sizeof(buf_array) };
48+
debug_uart.base.type = &busio_uart_type;
49+
50+
const mcu_pin_obj_t* rx = MP_OBJ_TO_PTR(DEBUG_UART_RX);
51+
const mcu_pin_obj_t* tx = MP_OBJ_TO_PTR(DEBUG_UART_TX);
52+
53+
common_hal_busio_uart_construct(&debug_uart, tx, rx, NULL, NULL, NULL, false,
54+
115200, 8, PARITY_NONE, 1, 1.0f, 64);
55+
common_hal_busio_uart_never_reset(&debug_uart);
56+
#endif
57+
}
58+
3859
void serial_init(void) {
3960
usb_init();
4061
}
4162

4263
bool serial_connected(void) {
64+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
65+
return true;
66+
#else
4367
return tud_cdc_connected();
68+
#endif
4469
}
4570

4671
char serial_read(void) {
72+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
73+
if (tud_cdc_connected() && tud_cdc_available() > 0) {
74+
return (char) tud_cdc_read_char();
75+
}
76+
int uart_errcode;
77+
char text;
78+
common_hal_busio_uart_read(&debug_uart, (uint8_t*) &text, 1, &uart_errcode);
79+
return text;
80+
#else
4781
return (char) tud_cdc_read_char();
82+
#endif
4883
}
4984

5085
bool serial_bytes_available(void) {
86+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
87+
return common_hal_busio_uart_rx_characters_available(&debug_uart) || (tud_cdc_available() > 0);
88+
#else
5189
return tud_cdc_available() > 0;
90+
#endif
5291
}
5392

5493
void serial_write_substring(const char* text, uint32_t length) {
94+
if (length == 0) { // TODO(mark) this is needed because of 'lib/mp-readline/readline.c:71'
95+
return;
96+
}
5597
#if CIRCUITPY_DISPLAYIO
5698
int errcode;
5799
common_hal_terminalio_terminal_write(&supervisor_terminal, (const uint8_t*) text, length, &errcode);
@@ -62,6 +104,11 @@ void serial_write_substring(const char* text, uint32_t length) {
62104
count += tud_cdc_write(text + count, length - count);
63105
usb_background();
64106
}
107+
108+
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
109+
int uart_errcode;
110+
common_hal_busio_uart_write(&debug_uart, (const uint8_t*) text, length, &uart_errcode);
111+
#endif
65112
}
66113

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