Skip to content

Commit 5a4752a

Browse files
committed
WIP
1 parent 242063e commit 5a4752a

File tree

8 files changed

+250
-20
lines changed

8 files changed

+250
-20
lines changed

main.c

Lines changed: 3 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;

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,16 @@
3434
#define FLASH_PAGE_SIZE (0x4000)
3535

3636
#define BOARD_OSC_DIV (8)
37+
38+
#define DEBUG_UART USART3
39+
#define DEBUG_UART_TX_CLK_ENABLE __HAL_RCC_GPIOD_CLK_ENABLE
40+
#define DEBUG_UART_TX_PORT GPIOD
41+
#define DEBUG_UART_TX_PIN GPIO_PIN_8
42+
#define DEBUG_UART_TX_AF GPIO_AF7_USART3
43+
#define DEBUG_UART_RX_CLK_ENABLE __HAL_RCC_GPIOD_CLK_ENABLE
44+
#define DEBUG_UART_RX_PORT GPIOD
45+
#define DEBUG_UART_RX_PIN GPIO_PIN_9
46+
#define DEBUG_UART_RX_AF GPIO_AF7_USART3
47+
#define DEBUG_UART_IRQn USART3_IRQn
48+
#define DEBUG_UART_CLK_ENABLE __HAL_RCC_USART3_CLK_ENABLE
49+

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

Lines changed: 33 additions & 20 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) {
@@ -411,7 +416,11 @@ void USART2_IRQHandler(void) {
411416
}
412417

413418
void USART3_IRQHandler(void) {
419+
#ifdef DEBUG_UART // TODO Make generic
420+
debug_irq_handler();
421+
#else
414422
call_hal_irq(3);
423+
#endif
415424
}
416425

417426
void UART4_IRQHandler(void) {
@@ -441,12 +450,14 @@ STATIC void uart_clock_enable(uint16_t mask) {
441450
__HAL_RCC_USART2_CLK_ENABLE();
442451
}
443452
#endif
444-
#ifdef USART3
453+
#ifdef USART3 // TODO Make generic
454+
#ifndef DEBUG_UART
445455
if (mask & (1 << 2)) {
446456
__HAL_RCC_USART3_FORCE_RESET();
447457
__HAL_RCC_USART3_RELEASE_RESET();
448458
__HAL_RCC_USART3_CLK_ENABLE();
449459
}
460+
#endif
450461
#endif
451462
#ifdef UART4
452463
if (mask & (1 << 3)) {
@@ -514,12 +525,14 @@ STATIC void uart_clock_disable(uint16_t mask) {
514525
__HAL_RCC_USART2_CLK_DISABLE();
515526
}
516527
#endif
517-
#ifdef USART3
528+
#if defined(USART3) // TODO Make generic
529+
#ifndef DEBUG_UART
518530
if (mask & (1 << 2)) {
519531
__HAL_RCC_USART3_FORCE_RESET();
520532
__HAL_RCC_USART3_RELEASE_RESET();
521533
__HAL_RCC_USART3_CLK_DISABLE();
522534
}
535+
#endif
523536
#endif
524537
#ifdef UART4
525538
if (mask & (1 << 3)) {

ports/stm/supervisor/debug.c

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Mark Olsson <[email protected]>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/mphal.h"
28+
#include <string.h>
29+
#include "supervisor/debug.h"
30+
#include "common-hal/microcontroller/Pin.h"
31+
#include "lib/utils/interrupt_char.h"
32+
#include "lib/mp-readline/readline.h"
33+
#include STM32_HAL_H
34+
35+
UART_HandleTypeDef huart;
36+
uint8_t debug_rx_char;
37+
38+
void debug_rxcpltcallback(UART_HandleTypeDef *handle) {
39+
if ((HAL_UART_GetState(handle) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX) {
40+
return;
41+
}
42+
if (debug_rx_char == CHAR_CTRL_C) {
43+
debug_disable_interrupt();
44+
mp_keyboard_interrupt();
45+
return;
46+
}
47+
HAL_UART_Receive_IT(handle, &debug_rx_char, 1);
48+
}
49+
50+
void debug_enable_interrupt(void) {
51+
HAL_UART_RegisterCallback(&huart, HAL_UART_RX_COMPLETE_CB_ID, debug_rxcpltcallback);
52+
HAL_NVIC_DisableIRQ(DEBUG_UART_IRQn); //prevent handle lock contention
53+
HAL_UART_Receive_IT(&huart, &debug_rx_char, 1);
54+
HAL_NVIC_SetPriority(DEBUG_UART_IRQn, 0, 0);
55+
HAL_NVIC_EnableIRQ(DEBUG_UART_IRQn);
56+
__HAL_UART_ENABLE_IT(&huart, UART_IT_RXNE);
57+
}
58+
59+
void debug_disable_interrupt(void) {
60+
HAL_UART_UnRegisterCallback(&huart, HAL_UART_RX_COMPLETE_CB_ID);
61+
__HAL_UART_DISABLE_IT(&huart, UART_IT_RXNE);
62+
HAL_NVIC_DisableIRQ(DEBUG_UART_IRQn);
63+
HAL_UART_AbortReceive(&huart);
64+
}
65+
66+
void debug_init(void) {
67+
GPIO_InitTypeDef GPIO_InitStruct;
68+
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
69+
70+
DEBUG_UART_CLK_ENABLE();
71+
DEBUG_UART_TX_CLK_ENABLE();
72+
DEBUG_UART_RX_CLK_ENABLE();
73+
74+
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3; // TODO
75+
PeriphClkInitStruct.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1; // TODO
76+
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
77+
78+
GPIO_InitStruct.Pin = DEBUG_UART_TX_PIN;
79+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
80+
GPIO_InitStruct.Pull = GPIO_NOPULL;
81+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
82+
GPIO_InitStruct.Alternate = DEBUG_UART_TX_AF;
83+
HAL_GPIO_Init(DEBUG_UART_TX_PORT, &GPIO_InitStruct);
84+
85+
GPIO_InitStruct.Pin = DEBUG_UART_RX_PIN;
86+
GPIO_InitStruct.Alternate = DEBUG_UART_RX_AF;
87+
HAL_GPIO_Init(DEBUG_UART_RX_PORT, &GPIO_InitStruct);
88+
89+
never_reset_pin_number(3, 8); // TODO
90+
never_reset_pin_number(3, 9); // TODO
91+
92+
huart.Instance = DEBUG_UART;
93+
huart.Init.BaudRate = 115200;
94+
huart.Init.WordLength = UART_WORDLENGTH_8B;
95+
huart.Init.StopBits = UART_STOPBITS_1;
96+
huart.Init.Parity = UART_PARITY_NONE;
97+
huart.Init.Mode = UART_MODE_TX_RX;
98+
huart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
99+
huart.Init.OverSampling = UART_OVERSAMPLING_16;
100+
HAL_UART_Init(&huart);
101+
102+
debug_enable_interrupt();
103+
}
104+
105+
char debug_read(void) {
106+
uint8_t data;
107+
HAL_StatusTypeDef ret;
108+
ret = HAL_UART_Receive(&huart, &data, 1, 500);
109+
if (ret != HAL_OK) {
110+
return 0;
111+
}
112+
return data;
113+
}
114+
115+
bool debug_bytes_available(void) {
116+
return __HAL_UART_GET_FLAG(&huart, UART_FLAG_RXNE);
117+
}
118+
119+
void debug_write_substring(const char *text, uint32_t len) {
120+
if (len == 0) {
121+
return;
122+
}
123+
HAL_UART_Transmit(&huart, (uint8_t*)text, len, 5000);
124+
}
125+
126+
void debug_irq_handler(void) {
127+
HAL_NVIC_ClearPendingIRQ(DEBUG_UART_IRQn);
128+
HAL_UART_IRQHandler(&huart);
129+
}

supervisor/debug.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Mark Olsson <[email protected]>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SUPERVISOR_DEBUG_H
28+
#define MICROPY_INCLUDED_SUPERVISOR_DEBUG_H
29+
30+
#include <stdbool.h>
31+
#include <stdint.h>
32+
33+
#include "py/mpconfig.h"
34+
35+
void debug_init(void);
36+
// Only writes up to given length. Does not check for null termination at all.
37+
void debug_write_substring(const char* text, uint32_t length);
38+
char debug_read(void);
39+
bool debug_bytes_available(void);
40+
void debug_irq_handler(void);
41+
void debug_enable_interrupt(void);
42+
void debug_disable_interrupt(void);
43+
44+
#endif // MICROPY_INCLUDED_SUPERVISOR_DEBUG_H

supervisor/shared/serial.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,49 @@
3030

3131
#include "supervisor/shared/display.h"
3232
#include "shared-bindings/terminalio/Terminal.h"
33+
#ifdef DEBUG_UART
34+
#include "supervisor/debug.h"
35+
#endif
3336
#include "supervisor/serial.h"
3437
#include "supervisor/usb.h"
3538

3639
#include "tusb.h"
3740

3841
void serial_init(void) {
3942
usb_init();
43+
#ifdef DEBUG_UART
44+
debug_init();
45+
#endif
4046
}
4147

4248
bool serial_connected(void) {
49+
#ifdef DEBUG_UART
50+
return true;
51+
#else
4352
return tud_cdc_connected();
53+
#endif
4454
}
4555

4656
char serial_read(void) {
57+
#ifdef DEBUG_UART
58+
if (tud_cdc_connected() && tud_cdc_available() > 0) {
59+
return (char) tud_cdc_read_char();
60+
}
61+
return debug_read();
62+
#else
4763
return (char) tud_cdc_read_char();
64+
#endif
4865
}
4966

5067
bool serial_bytes_available(void) {
68+
#ifdef DEBUG_UART
69+
return debug_bytes_available() || (tud_cdc_available() > 0);
70+
#else
5171
return tud_cdc_available() > 0;
72+
#endif
5273
}
5374

75+
5476
void serial_write_substring(const char* text, uint32_t length) {
5577
#if CIRCUITPY_DISPLAYIO
5678
int errcode;
@@ -62,6 +84,10 @@ void serial_write_substring(const char* text, uint32_t length) {
6284
count += tud_cdc_write(text + count, length - count);
6385
usb_background();
6486
}
87+
88+
#ifdef DEBUG_UART
89+
debug_write_substring(text, length);
90+
#endif
6591
}
6692

6793
void serial_write(const char* text) {

supervisor/supervisor.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ else
7575
lib/tinyusb/src/class/cdc/cdc_device.c \
7676
lib/tinyusb/src/tusb.c \
7777
supervisor/shared/serial.c \
78+
supervisor/debug.c \
7879
supervisor/usb.c \
7980
supervisor/shared/usb/usb_desc.c \
8081
supervisor/shared/usb/usb.c \

0 commit comments

Comments
 (0)