8000 stm32/uart: Add support for LPUART1 on L0, L4, H7 and WB MCUs. · larsks/micropython@9d674cf · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d674cf

Browse files
chrismas9dpgeorge
authored andcommitted
stm32/uart: Add support for LPUART1 on L0, L4, H7 and WB MCUs.
Add LPUART1 as a standard UART. No low power features are supported, yet. LPUART1 is enabled as the next available UART after the standard U(S)ARTs: STM32WB: LPUART1 = UART(2) STM32L0: LPUART1 = UART(6) STM32L4: LPUART1 = UART(6) STM32H7: LPUART1 = UART(9) On all ports: LPUART1 = machine.UART('LP1') LPUART1 is enabled by defining MICROPY_HW_LPUART1_TX and MICROPY_HW_LPUART1_RX in mpconfigboard.h. Signed-off-by: Chris Mason <c.mason@inchipdesign.com.au>
1 parent 1342deb commit 9d674cf

File tree

8 files changed

+108
-7
lines changed

8 files changed

+108
-7
lines changed

ports/stm32/boards/make-pins.py

Lines changed: 2 additions & 0 deletions
< 8000 td data-grid-cell-id="diff-8646e8a3fa2a0abbb62bbd64d4634a86e1a48a403530ee909a0e1038395115a6-28-30-0" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">28
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"I2S": ["CK", "MCK", "SD", "WS", "EXTSD"],
1515
"USART": ["RX", "TX", "CTS", "RTS", "CK"],
1616
"UART": ["RX", "TX", "CTS", "RTS"],
17+
"LPUART": ["RX", "TX", "CTS", "RTS"],
1718
"SPI": ["NSS", "SCK", "MISO", "MOSI"],
1819
"SDMMC": ["CK", "CMD", "D0", "D1", "D2", "D3"],
1920
"CAN": ["TX", "RX"],
@@ -24,6 +25,7 @@
2425
"I2S": "MICROPY_HW_ENABLE_I2S{num}",
2526
"SPI": "MICROPY_HW_SPI{num}_SCK",
2627
"UART": "MICROPY_HW_UART{num}_TX",
28+
"LPUART": "MICROPY_HW_LPUART{num}_TX",
2729
"USART": "MICROPY_HW_UART{num}_TX",
30
"SDMMC": "MICROPY_HW_SDMMC{num}_CK",
2931
"CAN": "MICROPY_HW_CAN{num}_TX",

ports/stm32/machine_uart.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@
7676
STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
7777
pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
7878
if (!self->is_enabled) {
79-
mp_printf(print, "UART(%u)", self->uart_id);
79+
#ifdef LPUART1
80+
if (self->uart_id == PYB_LPUART_1) {
81+
mp_printf(print, "UART('LP1')");
82+
} else
83+
#endif
84+
{
85+
mp_printf(print, "UART(%u)", self->uart_id);
86+
}
8087
} else {
8188
mp_int_t bits;
8289
uint32_t cr1 = self->uartx->CR1;
@@ -98,8 +105,16 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k
98105
if (cr1 & USART_CR1_PCE) {
99106
bits -= 1;
100107
}
101-
mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=",
102-
self->uart_id, uart_get_baudrate(self), bits);
108+
#ifdef LPUART1
109+
if (self->uart_id == PYB_LPUART_1) {
110+
mp_printf(print, "UART('LP1', baudrate=%u, bits=%u, parity=",
111+
uart_get_baudrate(self), bits);
112+
} else
113+
#endif
114+
{
115+
mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=",
116+
self->uart_id, uart_get_baudrate(self), bits);
117+
}
103118
if (!(cr1 & USART_CR1_PCE)) {
104119
mp_print_str(print, "None");
105120
} else if (!(cr1 & USART_CR1_PS)) {
@@ -335,6 +350,14 @@ STATIC mp_obj_t pyb_uart_make_new(const mp_obj_type_t *type, size_t n_args, size
335350
} else if (strcmp(port, MICROPY_HW_UART10_NAME) == 0) {
336351
uart_id = PYB_UART_10;
337352
#endif
353+
#ifdef MICROPY_HW_LPUART1_NAME
354+
} else if (strcmp(port, MICROPY_HW_LPUART1_NAME) == 0) {
355+
uart_id = PYB_LPUART_1;
356+
#endif
357+
#ifdef LPUART1
358+
} else if (strcmp(port, "LP1") == 0 && uart_exists(PYB_LPUART_1)) {
359+
uart_id = PYB_LPUART_1;
360+
#endif
338361
} else {
339362
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%s) doesn't exist"), port);
340363
}

ports/stm32/mpconfigboard_common.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
#define MICROPY_HW_MAX_I2C (2)
182182
#define MICROPY_HW_MAX_TIMER (17)
183183
#define MICROPY_HW_MAX_UART (8)
184+
#define MICROPY_HW_MAX_LPUART (0)
184185

185186
// Configuration for STM32F4 series
186187
#elif defined(STM32F4)
@@ -200,6 +201,7 @@
200201
#else
201202
#define MICROPY_HW_MAX_UART (6)
202203
#endif
204+
#define MICROPY_HW_MAX_LPUART (0)
203205

204206
// Configuration for STM32F7 series
205207
#elif defined(STM32F7)
@@ -214,6 +216,7 @@
214216
#define MICROPY_HW_MAX_I2C (4)
215217
#define MICROPY_HW_MAX_TIMER (17)
216218
#define MICROPY_HW_MAX_UART (8)
219+
#define MICROPY_HW_MAX_LPUART (0)
217220

218221
// Configuration for STM32H7 series
219222
#elif defined(STM32H7)
@@ -223,6 +226,7 @@
223226
#define MICROPY_HW_MAX_I2C (4)
224227
#define MICROPY_HW_MAX_TIMER (17)
225228
#define MICROPY_HW_MAX_UART (8)
229+
#define MICROPY_HW_MAX_LPUART (1)
226230

227231
// Configuration for STM32L0 series
228232
#elif defined(STM32L0)
@@ -232,6 +236,7 @@
232236
#define MICROPY_HW_MAX_I2C (3)
233237
#define MICROPY_HW_MAX_TIMER (22)
234238
#define MICROPY_HW_MAX_UART (5)
239+
#define MICROPY_HW_MAX_LPUART (1)
235240

236241
// Configuration for STM32L4 series
237242
#elif defined(STM32L4)
@@ -240,7 +245,8 @@
240245
#define PYB_EXTI_NUM_VECTORS (23)
241246
#define MICROPY_HW_MAX_I2C (4)
242247
#define MICROPY_HW_MAX_TIMER (17)
243-
#define MICROPY_HW_MAX_UART (6)
248+
#define MICROPY_HW_MAX_UART (5)
249+
#define MICROPY_HW_MAX_LPUART (1)
244250

245251
// Configuration for STM32WB series
246252
#elif defined(STM32WB)
@@ -250,6 +256,7 @@
250256
#define MICROPY_HW_MAX_I2C (3)
251257
#define MICROPY_HW_MAX_TIMER (17)
252258
#define MICROPY_HW_MAX_UART (1)
259+
#define MICROPY_HW_MAX_LPUART (1)
253260

254261
#ifndef MICROPY_HW_STM32WB_FLASH_SYNCRONISATION
255262
#define MICROPY_HW_STM32WB_FLASH_SYNCRONISATION (1)

ports/stm32/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ struct _mp_bluetooth_btstack_root_pointers_t;
375375
struct _pyb_uart_obj_t *pyb_stdio_uart; \
376376
\
377377
/* pointers to all UART objects (if they have been created) */ \
378-
struct _pyb_uart_obj_t *pyb_uart_obj_all[MICROPY_HW_MAX_UART]; \
378+
struct _pyb_uart_obj_t *pyb_uart_obj_all[MICROPY_HW_MAX_UART + MICROPY_HW_MAX_LPUART]; \
379379
\
380380
/* pointers to all CAN objects (if they have been created) */ \
381381
struct _pyb_can_obj_t *pyb_can_obj_all[MICROPY_HW_MAX_CAN]; \

ports/stm32/pin_defs_stm32.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ enum {
4747
AF_FN_I2C,
4848
AF_FN_USART,
4949
AF_FN_UART = AF_FN_USART,
50+
AF_FN_LPUART,
5051
AF_FN_SPI,
5152
AF_FN_I2S,
5253
AF_FN_SDMMC,
@@ -77,6 +78,10 @@ enum {
7778
AF_PIN_TYPE_UART_RX = AF_PIN_TYPE_USART_RX,
7879
AF_PIN_TYPE_UART_CTS = AF_PIN_TYPE_USART_CTS,
7980
AF_PIN_TYPE_UART_RTS = AF_PIN_TYPE_USART_RTS,
81+
AF_PIN_TYPE_LPUART_TX = AF_PIN_TYPE_USART_TX,
82+
AF_PIN_TYPE_LPUART_RX = AF_PIN_TYPE_USART_RX,
83+
AF_PIN_TYPE_LPUART_CTS = AF_PIN_TYPE_USART_CTS,
84+
AF_PIN_TYPE_LPUART_RTS = AF_PIN_TYPE_USART_RTS,
8085

8186
AF_PIN_TYPE_SPI_MOSI = 0,
8287
AF_PIN_TYPE_SPI_MISO,

ports/stm32/stm32_it.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,11 +742,13 @@ void USART1_IRQHandler(void) {
742742
IRQ_EXIT(USART1_IRQn);
743743
}
744744

745+
#if defined(USART2)
745746
void USART2_IRQHandler(void) {
746747
IRQ_ENTER(USART2_IRQn);
747748
uart_irq_handler(2);
748749
IRQ_EXIT(USART2_IRQn);
749750
}
751+
#endif
750752

751753
#if defined(STM32F0)
752754

@@ -772,29 +774,37 @@ void USART4_5_IRQHandler(void) {
772774

773775
#else
774776

777+
#if defined(USART3)
775778
void USART3_IRQHandler(void) {
776779
IRQ_ENTER(USART3_IRQn);
777780
uart_irq_handler(3);
778781
IRQ_EXIT(USART3_IRQn);
779782
}
783+
#endif
780784

785+
#if defined(UART4)
781786
void UART4_IRQHandler(void) {
782787
IRQ_ENTER(UART4_IRQn);
783788
uart_irq_handler(4);
784789
IRQ_EXIT(UART4_IRQn);
785790
}
791+
#endif
786792

793+
#if defined(UART5)
787794
void UART5_IRQHandler(void) {
788795
IRQ_ENTER(UART5_IRQn);
789796
uart_irq_handler(5);
790797
IRQ_EXIT(UART5_IRQn);
791798
}
799+
#endif
792800

801+
#if defined(USART6)
793802
void USART6_IRQHandler(void) {
794803
IRQ_ENTER(USART6_IRQn);
795804
uart_irq_handler(6);
796805
IRQ_EXIT(USART6_IRQn);
797806
}
807+
#endif
798808

799809
#if defined(UART7)
800810
void UART7_IRQHandler(void) {
@@ -830,6 +840,14 @@ void UART10_IRQHandler(void) {
830840

831841
#endif
832842

843+
#if defined(LPUART1)
844+
void LPUART1_IRQHandler(void) {
845+
IRQ_ENTER(LPUART1_IRQn);
846+
uart_irq_handler(PYB_LPUART_1);
847+
IRQ_EXIT(LPUART1_IRQn);
848+
}
849+
#endif
850+
833851
#if MICROPY_PY_PYB_LEGACY
834852

835853
#if defined(MICROPY_HW_I2C1_SCL)

ports/stm32/uart.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ bool uart_exists(int uart_id) {
201201
return true;
202202
#endif
203203

204+
#if defined(MICROPY_HW_LPUART1_TX) && defined(MICROPY_HW_LPUART1_RX)
205+
case PYB_LPUART_1:
206+
return true;
207+
#endif
208+
204209
default:
205210
return false;
206211
}
@@ -211,6 +216,7 @@ bool uart_init(pyb_uart_obj_t *uart_obj,
211216
uint32_t baudrate, uint32_t bits, uint32_t parity, uint32_t stop, uint32_t flow) {
212217
USART_TypeDef *UARTx;
213218
IRQn_Type irqn;
219+
uint8_t uart_fn = AF_FN_UART;
214220
int uart_unit;
215221

216222
const pin_obj_t *pins[4] = {0};
@@ -406,6 +412,28 @@ bool uart_init(pyb_uart_obj_t *uart_obj,
406412
break;
407413
#endif
408414

415+
#if defined(MICROPY_HW_LPUART1_TX) && defined(MICROPY_HW_LPUART1_RX)
416+
case PYB_LPUART_1:
417+
uart_fn = AF_FN_LPUART;
418+
uart_unit = 1;
419+
UARTx = LPUART1;
420+
irqn = LPUART1_IRQn;
421+
pins[0] = MICROPY_HW_LPUART1_TX;
422+
pins[1] = MICROPY_HW_LPUART1_RX;
423+
#if defined(MICROPY_HW_LPUART1_RTS)
424+
if (flow & UART_HWCONTROL_RTS) {
425+
pins[2] = MICROPY_HW_LPUART1_RTS;
426+
}
427+
#endif
428+
#if defined(MICROPY_HW_LPUART1_CTS)
429+
if (flow & UART_HWCONTROL_CTS) {
430+
pins[3] = MICROPY_HW_LPUART1_CTS;
431+
}
432+
#endif
433+
__HAL_RCC_LPUART1_CLK_ENABLE();
434+
break;
435+
#endif
436+
409437
default:
410438
// UART does not exist or is not configured for this board
411439
return false;
@@ -416,7 +444,7 @@ bool uart_init(pyb_uart_obj_t *uart_obj,
416444

417445
for (uint i = 0; i < 4; i++) {
418446
if (pins[i] != NULL) {
419-
bool ret = mp_hal_pin_config_alt(pins[i], mode, pull, AF_FN_UART, uart_unit);
447+
bool ret = mp_hal_pin_config_alt(pins[i], mode, pull, uart_fn, uart_unit);
420448
if (!ret) {
421449
return false;
422450
}
@@ -596,6 +624,13 @@ void uart_deinit(pyb_uart_obj_t *self) {
596624
__HAL_RCC_UART10_RELEASE_RESET();
597625
__HAL_RCC_UART10_CLK_DISABLE();
598626
#endif
627+
#if defined(LPUART1)
628+
} else if (self->uart_id == PYB_LPUART_1) {
629+
HAL_NVIC_DisableIRQ(LPUART1_IRQn);
630+
__HAL_RCC_LPUART1_FORCE_RESET();
631+
__HAL_RCC_LPUART1_RELEASE_RESET();
632+
__HAL_RCC_LPUART1_CLK_DISABLE();
633+
#endif
599634
}
600635
}
601636

@@ -677,7 +712,15 @@ uint32_t uart_get_source_freq(pyb_uart_obj_t *self) {
677712

678713
uint32_t uart_get_baudrate(pyb_uart_obj_t *self) {
679714
// This formula assumes UART_OVERSAMPLING_16
680-
return uart_get_source_freq(self) / self->uartx->BRR;
715+
uint32_t source_freq = uart_get_source_freq(self);
716+
#if defined(LPUART1)
717+
if (self->uart_id == PYB_LPUART_1) {
718+
return source_freq / (self->uartx->BRR >> 8);
719+
} else
720+
#endif
721+
{
722+
return source_freq / self->uartx->BRR;
723+
}
681724
}
682725

683726
void uart_set_baudrate(pyb_uart_obj_t *self, uint32_t baudrate) {

ports/stm32/uart.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ typedef enum {
4040
PYB_UART_8 = 8,
4141
PYB_UART_9 = 9,
4242
PYB_UART_10 = 10,
43+
#ifdef LPUART1
44+
PYB_LPUART_1 = MICROPY_HW_MAX_UART + 1,
45+
#endif
4346
} pyb_uart_t;
4447

4548
#define CHAR_WIDTH_8BIT (0)

0 commit comments

Comments
 (0)
0