Skip to content

Commit 7932501

Browse files
committed
WIP
1 parent 2d7cf4b commit 7932501

File tree

9 files changed

+285
-57
lines changed

9 files changed

+285
-57
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_reinit();
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
@@ -111,6 +111,7 @@ CFLAGS += $(MCU_FLAGS_$(MCU_SERIES))
111111
CFLAGS += -DSTM32_HAL_H='<stm32$(MCU_SERIES_LOWER)xx_hal.h>'
112112

113113
CFLAGS += -DSTM32_SERIES_LOWER='"stm32$(MCU_SERIES_LOWER)"'
114+
CFLAGS += -DUSE_HAL_UART_REGISTER_CALLBACKS
114115

115116
# Floating point settings
116117
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: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929

3030
#include "mpconfigport.h"
3131
#include "lib/utils/interrupt_char.h"
32+
#include "lib/mp-readline/readline.h"
3233
#include "py/gc.h"
3334
#include "py/mperrno.h"
3435
#include "py/runtime.h"
3536
#include "py/stream.h"
3637
#include "supervisor/shared/translate.h"
38+
#include "supervisor/debug.h"
3739

3840
#include "tick.h"
3941

@@ -64,12 +66,32 @@ STATIC USART_TypeDef * assign_uart_or_throw(busio_uart_obj_t* self, bool pin_eva
6466

6567
void uart_reset(void) {
6668
for (uint8_t i = 0; i < MAX_UART; i++) {
67-
reserved_uart[i] = false;
68-
MP_STATE_PORT(cpy_uart_obj_all)[i] = NULL;
69+
if (i != 2) {
70+
reserved_uart[i] = false;
71+
MP_STATE_PORT(cpy_uart_obj_all)[i] = NULL;
72+
}
6973
}
7074
uart_clock_disable(ALL_UARTS);
7175
}
7276

77+
void uart_rxcpltcallback(UART_HandleTypeDef *handle)
78+
{
79+
for (int i = 0; i < 7; i++) {
80+
//get context pointer and cast it as struct pointer
81+
busio_uart_obj_t * context = (busio_uart_obj_t*)MP_STATE_PORT(cpy_uart_obj_all)[i];
82+
if (handle == &context->handle) {
83+
//check if transaction is ongoing
84+
if ((HAL_UART_GetState(handle) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX) {
85+
return;
86+
}
87+
ringbuf_put_n(&context->rbuf, &context->rx_char, 1);
88+
errflag = HAL_UART_Receive_IT(handle, &context->rx_char, 1);
89+
90+
return;
91+
}
92+
}
93+
}
94+
7395
void common_hal_busio_uart_construct(busio_uart_obj_t *self,
7496
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx,
7597
const mcu_pin_obj_t * rts, const mcu_pin_obj_t * cts,
@@ -234,6 +256,10 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
234256
HAL_NVIC_SetPriority(self->irq, UART_IRQPRI, UART_IRQSUB_PRI);
235257
HAL_NVIC_EnableIRQ(self->irq);
236258

259+
HAL_UART_RegisterCallback(&self->handle,
260+
HAL_UART_RX_COMPLETE_CB_ID, uart_rxcpltcallback);
261+
262+
237263
errflag = HAL_OK;
238264
}
239265

@@ -311,24 +337,6 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
311337
return len;
312338
}
313339

314-
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle)
315-
{
316-
for (int i = 0; i < 7; i++) {
317-
//get context pointer and cast it as struct pointer
318-
busio_uart_obj_t * context = (busio_uart_obj_t*)MP_STATE_PORT(cpy_uart_obj_all)[i];
319-
if (handle == &context->handle) {
320-
//check if transaction is ongoing
321-
if ((HAL_UART_GetState(handle) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX) {
322-
return;
323-
}
324-
ringbuf_put_n(&context->rbuf, &context->rx_char, 1);
325-
errflag = HAL_UART_Receive_IT(handle, &context->rx_char, 1);
326-
327-
return;
328-
}
329-
}
330-
}
331-
332340
void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
333341
{
334342
if (__HAL_UART_GET_FLAG(UartHandle, UART_FLAG_PE) != RESET) {
@@ -413,7 +421,8 @@ void USART2_IRQHandler(void) {
413421
}
414422

415423
void USART3_IRQHandler(void) {
416-
call_hal_irq(3);
424+
// call_hal_irq(3);
425+
debug_irq_handler();
417426
}
418427

419428
void UART4_IRQHandler(void) {
@@ -444,11 +453,11 @@ STATIC void uart_clock_enable(uint16_t mask) {
444453
}
445454
#endif
446455
#ifdef USART3
447-
if (mask & (1 << 2)) {
448-
__HAL_RCC_USART3_FORCE_RESET();
449-
__HAL_RCC_USART3_RELEASE_RESET();
450-
__HAL_RCC_USART3_CLK_ENABLE();
451-
}
456+
// if (mask & (1 << 2)) {
457+
// __HAL_RCC_USART3_FORCE_RESET();
458+
// __HAL_RCC_USART3_RELEASE_RESET();
459+
// __HAL_RCC_USART3_CLK_ENABLE();
460+
// }
452461
#endif
453462
#ifdef UART4
454463
if (mask & (1 << 3)) {
@@ -516,12 +525,12 @@ STATIC void uart_clock_disable(uint16_t mask) {
516525
__HAL_RCC_USART2_CLK_DISABLE();
517526
}
518527
#endif
519-
#ifdef USART3
520-
if (mask & (1 << 2)) {
521-
__HAL_RCC_USART3_FORCE_RESET();
522-
__HAL_RCC_USART3_RELEASE_RESET();
523-
__HAL_RCC_USART3_CLK_DISABLE();
524-
}
528+
#if defined(USART3)
529+
// if (mask & (1 << 2)) {
530+
// __HAL_RCC_USART3_FORCE_RESET();
531+
// __HAL_RCC_USART3_RELEASE_RESET();
532+
// __HAL_RCC_USART3_CLK_DISABLE();
533+
// }
525534
#endif
526535
#ifdef UART4
527536
if (mask & (1 << 3)) {
@@ -586,9 +595,9 @@ STATIC void uart_assign_irq(busio_uart_obj_t *self, USART_TypeDef * USARTx) {
586595
}
587596
#endif
588597
#ifdef USART3
589-
if (USARTx == USART3) {
590-
self->irq = USART3_IRQn;
591-
}
598+
// if (USARTx == USART3) {
599+
// self->irq = USART3_IRQn;
600+
// }
592601
#endif
593602
#ifdef UART4
594603
if (USARTx == UART4) {

ports/stm/supervisor/debug.c

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
37+
void debug_rxcpltcallback(UART_HandleTypeDef *handle) {
38+
if ((HAL_UART_GetState(handle) & HAL_UART_STATE_BUSY_RX) == HAL_UART_STATE_BUSY_RX) {
39+
return;
40+
}
41+
uint8_t rx_char;
42+
HAL_UART_Receive_IT(handle, &rx_char, 1);
43+
if (rx_char == CHAR_CTRL_C) {
44+
HAL_NVIC_DisableIRQ(DEBUG_UART_IRQn);
45+
__HAL_UART_DISABLE_IT(handle, UART_IT_RXNE);
46+
mp_keyboard_interrupt();
47+
}
48+
}
49+
50+
void debug_reinit(void) {
51+
HAL_NVIC_EnableIRQ(DEBUG_UART_IRQn);
52+
__HAL_UART_ENABLE_IT(&huart, UART_IT_RXNE);
53+
}
54+
55+
void debug_init(void) {
56+
GPIO_InitTypeDef GPIO_InitStruct;
57+
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
58+
59+
DEBUG_UART_CLK_ENABLE();
60+
DEBUG_UART_TX_CLK_ENABLE();
61+
DEBUG_UART_RX_CLK_ENABLE();
62+
63+
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3; // TODO
64+
PeriphClkInitStruct.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1; // TODO
65+
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
66+
67+
GPIO_InitStruct.Pin = DEBUG_UART_TX_PIN;
68+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
69+
GPIO_InitStruct.Pull = GPIO_NOPULL;
70+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
71+
GPIO_InitStruct.Alternate = DEBUG_UART_TX_AF;
72+
HAL_GPIO_Init(DEBUG_UART_TX_PORT, &GPIO_InitStruct);
73+
74+
GPIO_InitStruct.Pin = DEBUG_UART_RX_PIN;
75+
GPIO_InitStruct.Alternate = DEBUG_UART_RX_AF;
76+
HAL_GPIO_Init(DEBUG_UART_RX_PORT, &GPIO_InitStruct);
77+
78+
never_reset_pin_number(3, 8); // TODO
79+
never_reset_pin_number(3, 9); // TODO
80+
81+
huart.Instance = DEBUG_UART;
82+
huart.Init.BaudRate = 115200;
83+
huart.Init.WordLength = UART_WORDLENGTH_8B;
84+
huart.Init.StopBits = UART_STOPBITS_1;
85+
huart.Init.Parity = UART_PARITY_NONE;
86+
huart.Init.Mode = UART_MODE_TX_RX;
87+
huart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
88+
huart.Init.OverSampling = UART_OVERSAMPLING_16;
89+
HAL_UART_Init(&huart);
90+
91+
HAL_UART_RegisterCallback(&huart,
92+
HAL_UART_RX_COMPLETE_CB_ID, debug_rxcpltcallback);
93+
94+
HAL_NVIC_DisableIRQ(DEBUG_UART_IRQn); //prevent handle lock contention
95+
uint8_t rx_char;
96+
HAL_UART_Receive_IT(&huart, &rx_char, 1);
97+
HAL_NVIC_SetPriority(DEBUG_UART_IRQn, 1, 0);
98+
HAL_NVIC_EnableIRQ(DEBUG_UART_IRQn);
99+
__HAL_UART_ENABLE_IT(&huart, UART_IT_RXNE);
100+
}
101+
102+
char debug_read(void) {
103+
uint8_t data;
104+
HAL_UART_Receive(&huart, &data, 1, 500);
105+
return data;
106+
}
107+
108+
bool debug_bytes_available(void) {
109+
return __HAL_UART_GET_FLAG(&huart, UART_FLAG_RXNE);
110+
}
111+
112+
void debug_write_substring(const char *text, uint32_t len) {
113+
if (len == 0) {
114+
return;
115+
}
116+
HAL_UART_Transmit(&huart, (uint8_t*)text, len, 5000);
117+
}
118+
119+
void debug_irq_handler(void) {
120+
HAL_NVIC_ClearPendingIRQ(DEBUG_UART_IRQn);
121+
HAL_UART_IRQHandler(&huart);
122+
}

ports/stm/supervisor/serial.c

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,46 @@
2828
#include "py/mphal.h"
2929
#include <string.h>
3030
#include "supervisor/serial.h"
31-
#include "stm32f4xx_hal.h"
32-
#include "stm32f4/gpio.h"
31+
#include STM32_HAL_H
3332

34-
UART_HandleTypeDef huart2;
33+
UART_HandleTypeDef huart;
34+
35+
// TODO Make this file generic
3536

3637
void serial_init(void) {
37-
huart2.Instance = USART2;
38-
huart2.Init.BaudRate = 115200;
39-
huart2.Init.WordLength = UART_WORDLENGTH_8B;
40-
huart2.Init.StopBits = UART_STOPBITS_1;
41-
huart2.Init.Parity = UART_PARITY_NONE;
42-
huart2.Init.Mode = UART_MODE_TX_RX;
43-
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
44-
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
45-
if (HAL_UART_Init(&huart2) == HAL_OK)
46-
{
47-
stm32f4_peripherals_status_led(1,1);
48-
}
38+
GPIO_InitTypeDef GPIO_InitStruct;
39+
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
40+
41+
__HAL_RCC_USART3_CLK_ENABLE();
42+
__HAL_RCC_GPIOD_CLK_ENABLE();
43+
44+
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3;
45+
PeriphClkInitStruct.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
46+
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
47+
48+
GPIO_InitStruct.Pin = CONSOLE_UART_TX_PIN;
49+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
50+
GPIO_InitStruct.Pull = GPIO_NOPULL;
51+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
52+
GPIO_InitStruct.Alternate = CONSOLE_UART_TX_AF;
53+
HAL_GPIO_Init(CONSOLE_UART_TX_PORT, &GPIO_InitStruct);
54+
55+
GPIO_InitStruct.Pin = CONSOLE_UART_RX_PIN;
56+
GPIO_InitStruct.Alternate = CONSOLE_UART_RX_AF;
57+
HAL_GPIO_Init(CONSOLE_UART_RX_PORT, &GPIO_InitStruct);
58+
59+
never_reset_pin_number(3, 8);
60+
never_reset_pin_number(3, 9);
61+
62+
huart.Instance = CONSOLE_UART;
63+
huart.Init.BaudRate = 115200;
64+
huart.Init.WordLength = UART_WORDLENGTH_8B;
65+
huart.Init.StopBits = UART_STOPBITS_1;
66+
huart.Init.Parity = UART_PARITY_NONE;
67+
huart.Init.Mode = UART_MODE_TX_RX;
68+
huart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
69+
huart.Init.OverSampling = UART_OVERSAMPLING_16;
70+
HAL_UART_Init(&huart);
4971
}
5072

5173
bool serial_connected(void) {
@@ -54,12 +76,12 @@ bool serial_connected(void) {
5476

5577
char serial_read(void) {
5678
uint8_t data;
57-
HAL_UART_Receive(&huart2, &data, 1,500);
79+
HAL_UART_Receive(&huart, &data, 1, 500);
5880
return data;
5981
}
6082

6183
bool serial_bytes_available(void) {
62-
return __HAL_UART_GET_FLAG(&huart2, UART_FLAG_RXNE);
84+
return __HAL_UART_GET_FLAG(&huart, UART_FLAG_RXNE);
6385
}
6486

6587
void serial_write(const char* text) {
@@ -70,6 +92,6 @@ void serial_write_substring(const char *text, uint32_t len) {
7092
if (len == 0) {
7193
return;
7294
}
73-
HAL_UART_Transmit(&huart2, (uint8_t*)text, len, 5000);
95+
HAL_UART_Transmit(&huart, (uint8_t*)text, len, 5000);
7496
}
7597

0 commit comments

Comments
 (0)