Skip to content

Commit 4fad7b8

Browse files
committed
Enable showing the console on the STM32 VCP
1 parent 4d7e341 commit 4fad7b8

File tree

9 files changed

+297
-38
lines changed

9 files changed

+297
-38
lines changed

main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "supervisor/shared/safe_mode.h"
5656
#include "supervisor/shared/status_leds.h"
5757
#include "supervisor/shared/stack.h"
58+
#include "supervisor/debug.h"
5859
#include "supervisor/serial.h"
5960

6061
#include "boards/board.h"
@@ -219,6 +220,8 @@ bool run_code_py(safe_mode_t safe_mode) {
219220
}
220221
#endif
221222

223+
debug_enable_interrupt();
224+
222225
pyexec_result_t result;
223226

224227
result.return_code = 0;
@@ -247,6 +250,8 @@ bool run_code_py(safe_mode_t safe_mode) {
247250
serial_write_compressed(translate("WARNING: Your code filename has two extensions\n"));
248251
}
249252
}
253+
debug_disable_interrupt();
254+
250255
cleanup_after_vm(heap);
251256

252257
if (result.return_code & PYEXEC_FORCED_EXIT) {

ports/stm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ CFLAGS += $(MCU_FLAGS_$(MCU_SERIES))
112112
CFLAGS += -DSTM32_HAL_H='<stm32$(MCU_SERIES_LOWER)xx_hal.h>'
113113

114114
CFLAGS += -DSTM32_SERIES_LOWER='"stm32$(MCU_SERIES_LOWER)"'
115+
CFLAGS += -DUSE_HAL_UART_REGISTER_CALLBACKS
115116

116117
# Floating point settings
117118
ifeq ($(MCU_VARIANT),$(filter $(MCU_VARIANT),STM32F765xx STM32F767xx STM32F769xx STM32H743xx))

ports/stm/boards/nucleo_f746zg/mpconfigboard.h

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

3636
#define BOARD_OSC_DIV (8)
37+
38+
#define USART3_UNAVAILABLE
39+
40+
#define DEBUG_UART USART3
41+
#define DEBUG_UART_TX_CLK_ENABLE __HAL_RCC_GPIOD_CLK_ENABLE
42+
#define DEBUG_UART_TX_PORT GPIOD
43+
#define DEBUG_UART_TX_PIN GPIO_PIN_8
44+
#define DEBUG_UART_TX_AF GPIO_AF7_USART3
45+
#define DEBUG_UART_RX_CLK_ENABLE __HAL_RCC_GPIOD_CLK_ENABLE
46+
#define DEBUG_UART_RX_PORT GPIOD
47+
#define DEBUG_UART_RX_PIN GPIO_PIN_9
48+
#define DEBUG_UART_RX_AF GPIO_AF7_USART3
49+
#define DEBUG_UART_IRQn USART3_IRQn
50+
#define DEBUG_UART_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE
51+
#define DEBUG_UART_RCC_PERIPHCLK RCC_PERIPHCLK_USART3
52+
#define DEBUG_UART_RCC_PERIPHCLK_SELECTION Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1
53+
#define DEBUG_UART_TX_PIN_NUM 0xaf
54+
#define DEBUG_UART_RX_PIN_NUM 0x39
55+

ports/stm/boards/nucleo_f746zg/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ USB_MANUFACTURER = "STMicroelectronics"
55
USB_DEVICES = "CDC,MSC"
66

77
INTERNAL_FLASH_FILESYSTEM = 1
8+
DEBUG_SERIAL = 1
89

910
MCU_SERIES = F7
1011
MCU_VARIANT = STM32F746xx

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

Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "py/runtime.h"
3535
#include "py/stream.h"
3636
#include "supervisor/shared/translate.h"
37+
#include "supervisor/debug.h"
3738

3839
#define ALL_UARTS 0xFFFF
3940

@@ -68,6 +69,24 @@ void uart_reset(void) {
6869
uart_clock_disable(ALL_UARTS);
6970
}
7071

72+
void uart_rxcpltcallback(UART_HandleTypeDef *handle)
73+
{
74+
for (int i = 0; i < 7; i++) {
75+
//get context pointer and cast it as struct pointer
76+
busio_uart_obj_t * context = (busio_uart_obj_t*)MP_STATE_PORT(cpy_uart_obj_all)[i];
77+
if (handle == &context->handle) {
78+
//check if transaction is ongoing
79+
if ((HAL_UART_GetState(handle) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX) {
80+
return;
81+
}
82+
ringbuf_put_n(&context->rbuf, &context->rx_char, 1);
83+
errflag = HAL_UART_Receive_IT(handle, &context->rx_char, 1);
84+
85+
return;
86+
}
87+
}
88+
}
89+
7190
void common_hal_busio_uart_construct(busio_uart_obj_t *self,
7291
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx,
7392
const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts,
@@ -232,6 +251,10 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
232251
HAL_NVIC_SetPriority(self->irq, UART_IRQPRI, UART_IRQSUB_PRI);
233252
HAL_NVIC_EnableIRQ(self->irq);
234253

254+
HAL_UART_RegisterCallback(&self->handle,
255+
HAL_UART_RX_COMPLETE_CB_ID, uart_rxcpltcallback);
256+
257+
235258
errflag = HAL_OK;
236259
}
237260

@@ -309,24 +332,6 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
309332
return len;
310333
}
311334

312-
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle)
313-
{
314-
for (int i = 0; i < 7; i++) {
315-
//get context pointer and cast it as struct pointer
316-
busio_uart_obj_t * context = (busio_uart_obj_t*)MP_STATE_PORT(cpy_uart_obj_all)[i];
317-
if (handle == &context->handle) {
318-
//check if transaction is ongoing
319-
if ((HAL_UART_GetState(handle) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX) {
320-
return;
321-
}
322-
ringbuf_put_n(&context->rbuf, &context->rx_char, 1);
323-
errflag = HAL_UART_Receive_IT(handle, &context->rx_char, 1);
324-
325-
return;
326-
}
327-
}
328-
}
329-
330335
void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
331336
{
332337
if (__HAL_UART_GET_FLAG(UartHandle, UART_FLAG_PE) != RESET) {
@@ -403,94 +408,118 @@ STATIC void call_hal_irq(int uart_num) {
403408

404409
// UART/USART IRQ handlers
405410
void USART1_IRQHandler(void) {
411+
#ifdef USART1_UNAVAILABLE
412+
debug_irq_handler();
413+
#else
406414
call_hal_irq(1);
415+
#endif
407416
}
408417

409418
void USART2_IRQHandler(void) {
419+
#ifdef USART2_UNAVAILABLE
420+
debug_irq_handler();
421+
#else
410422
call_hal_irq(2);
423+
#endif
411424
}
412425

413426
void USART3_IRQHandler(void) {
427+
#ifdef USART3_UNAVAILABLE
428+
debug_irq_handler();
429+
#else
414430
call_hal_irq(3);
431+
#endif
415432
}
416433

417434
void UART4_IRQHandler(void) {
435+
#ifdef USART4_UNAVAILABLE
436+
debug_irq_handler();
437+
#else
418438
call_hal_irq(4);
439+
#endif
419440
}
420441

421442
void UART5_IRQHandler(void) {
443+
#ifdef USART5_UNAVAILABLE
444+
debug_irq_handler();
445+
#else
422446
call_hal_irq(5);
447+
#endif
423448
}
424449

425450
void USART6_IRQHandler(void) {
451+
#ifdef USART6_UNAVAILABLE
452+
debug_irq_handler();
453+
#else
426454
call_hal_irq(6);
455+
#endif
427456
}
428457

429458
STATIC void uart_clock_enable(uint16_t mask) {
430-
#ifdef USART1
459+
#if defined(USART1) && !defined(USART1_UNAVAILABLE)
431460
if (mask & (1 << 0)) {
432461
__HAL_RCC_USART1_FORCE_RESET();
433462
__HAL_RCC_USART1_RELEASE_RESET();
434463
__HAL_RCC_USART1_CLK_ENABLE();
435464
}
436465
#endif
437-
#ifdef USART2
466+
#if defined(USART2) && !defined(USART2_UNAVAILABLE)
438467
if (mask & (1 << 1)) {
439468
__HAL_RCC_USART2_FORCE_RESET();
440469
__HAL_RCC_USART2_RELEASE_RESET();
441470
__HAL_RCC_USART2_CLK_ENABLE();
442471
}
443472
#endif
444-
#ifdef USART3
473+
#if defined(USART3) && !defined(USART3_UNAVAILABLE)
445474
if (mask & (1 << 2)) {
446475
__HAL_RCC_USART3_FORCE_RESET();
447476
__HAL_RCC_USART3_RELEASE_RESET();
448477
__HAL_RCC_USART3_CLK_ENABLE();
449478
}
450479
#endif
451-
#ifdef UART4
480+
#if defined(UART4) && !defined(USART4_UNAVAILABLE)
452481
if (mask & (1 << 3)) {
453482
__HAL_RCC_UART4_FORCE_RESET();
454483
__HAL_RCC_UART4_RELEASE_RESET();
455484
__HAL_RCC_UART4_CLK_ENABLE();
456485
}
457486
#endif
458-
#ifdef UART5
487+
#if defined(UART5) && !defined(USART5_UNAVAILABLE)
459488
if (mask & (1 << 4)) {
460489
__HAL_RCC_UART5_FORCE_RESET();
461490
__HAL_RCC_UART5_RELEASE_RESET();
462491
__HAL_RCC_UART5_CLK_ENABLE();
463492
}
464493
#endif
465-
#ifdef USART6
494+
#if defined(USART6) && !defined(USART6_UNAVAILABLE)
466495
if (mask & (1 << 5)) {
467496
__HAL_RCC_USART6_FORCE_RESET();
468497
__HAL_RCC_USART6_RELEASE_RESET();
469498
__HAL_RCC_USART6_CLK_ENABLE();
470499
}
471500
#endif
472-
#ifdef UART7
501+
#if defined(UART7) && !defined(USART7_UNAVAILABLE)
473502
if (mask & (1 << 6)) {
474503
__HAL_RCC_UART7_FORCE_RESET();
475504
__HAL_RCC_UART7_RELEASE_RESET();
476505
__HAL_RCC_UART7_CLK_ENABLE();
477506
}
478507
#endif
479-
#ifdef UART8
508+
#if defined(UART8) && !defined(USART8_UNAVAILABLE)
480509
if (mask & (1 << 7)) {
481510
__HAL_RCC_UART8_FORCE_RESET();
482511
__HAL_RCC_UART8_RELEASE_RESET();
483512
__HAL_RCC_UART8_CLK_ENABLE();
484513
}
485514
#endif
486-
#ifdef UART9
515+
#if defined(UART9) && !defined(USART9_UNAVAILABLE)
487516
if (mask & (1 << 8)) {
488517
__HAL_RCC_UART9_FORCE_RESET();
489518
__HAL_RCC_UART9_RELEASE_RESET();
490519
__HAL_RCC_UART9_CLK_ENABLE();
491520
}
492521
#endif
493-
#ifdef UART10
522+
#if defined(UART10) && !defined(USART10_UNAVAILABLE)
494523
if (mask & (1 << 9)) {
495524
__HAL_RCC_UART10_FORCE_RESET();
496525
__HAL_RCC_UART10_RELEASE_RESET();
@@ -500,70 +529,70 @@ STATIC void uart_clock_enable(uint16_t mask) {
500529
}
501530

502531
STATIC void uart_clock_disable(uint16_t mask) {
503-
#ifdef USART1
532+
#if defined(USART1) && !defined(USART1_UNAVAILABLE)
504533
if (mask & (1 << 0)) {
505534
__HAL_RCC_USART1_FORCE_RESET();
506535
__HAL_RCC_USART1_RELEASE_RESET();
507536
__HAL_RCC_USART1_CLK_DISABLE();
508537
}
509538
#endif
510-
#ifdef USART2
539+
#if defined(USART2) && !defined(USART2_UNAVAILABLE)
511540
if (mask & (1 << 1)) {
512541
__HAL_RCC_USART2_FORCE_RESET();
513542
__HAL_RCC_USART2_RELEASE_RESET();
514543
__HAL_RCC_USART2_CLK_DISABLE();
515544
}
516545
#endif
517-
#ifdef USART3
546+
#if defined(USART3) && !defined(USART3_UNAVAILABLE)
518547
if (mask & (1 << 2)) {
519548
__HAL_RCC_USART3_FORCE_RESET();
520549
__HAL_RCC_USART3_RELEASE_RESET();
521550
__HAL_RCC_USART3_CLK_DISABLE();
522551
}
523552
#endif
524-
#ifdef UART4
553+
#if defined(UART4) && !defined(USART4_UNAVAILABLE)
525554
if (mask & (1 << 3)) {
526555
__HAL_RCC_UART4_FORCE_RESET();
527556
__HAL_RCC_UART4_RELEASE_RESET();
528557
__HAL_RCC_UART4_CLK_DISABLE();
529558
}
530559
#endif
531-
#ifdef UART5
560+
#if defined(UART5) && !defined(USART5_UNAVAILABLE)
532561
if (mask & (1 << 4)) {
533562
__HAL_RCC_UART5_FORCE_RESET();
534563
__HAL_RCC_UART5_RELEASE_RESET();
535564
__HAL_RCC_UART5_CLK_DISABLE();
536565
}
537566
#endif
538-
#ifdef USART6
567+
#if defined(USART6) && !defined(USART6_UNAVAILABLE)
539568
if (mask & (1 << 5)) {
540569
__HAL_RCC_USART6_FORCE_RESET();
541570
__HAL_RCC_USART6_RELEASE_RESET();
542571
__HAL_RCC_USART6_CLK_DISABLE();
543572
}
544573
#endif
545-
#ifdef UART7
574+
#if defined(UART7) && !defined(USART7_UNAVAILABLE)
546575
if (mask & (1 << 6)) {
547576
__HAL_RCC_UART7_FORCE_RESET();
548577
__HAL_RCC_UART7_RELEASE_RESET();
549578
__HAL_RCC_UART7_CLK_DISABLE();
550579
}
551580
#endif
552-
#ifdef UART8
581+
#if defined(UART8) && !defined(USART8_UNAVAILABLE)
553582
if (mask & (1 << 7)) {
554583
__HAL_RCC_UART8_FORCE_RESET();
555584
__HAL_RCC_UART8_RELEASE_RESET();
556585
__HAL_RCC_UART8_CLK_DISABLE();
557586
}
558587
#endif
559-
#ifdef UART9
588+
#if defined(UART9) && !defined(USART9_UNAVAILABLE)
560589
if (mask & (1 << 8)) {
561590
__HAL_RCC_UART9_FORCE_RESET();
562591
__HAL_RCC_UART9_RELEASE_RESET();
563592
__HAL_RCC_UART9_CLK_DISABLE();
564593
}
565594
#endif
566-
#ifdef UART10
595+
#if defined(UART10) && !defined(USART10_UNAVAILABLE)
567596
if (mask & (1 << 9)) {
568597
__HAL_RCC_UART10_FORCE_RESET();
569598
__HAL_RCC_UART10_RELEASE_RESET();

0 commit comments

Comments
 (0)