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

0 commit comments

Comments
 (0)