Skip to content

Commit 3091896

Browse files
committed
chore: support LPUART3
Signed-off-by: Frederic Pillon <[email protected]>
1 parent 3d23c44 commit 3091896

File tree

6 files changed

+125
-13
lines changed

6 files changed

+125
-13
lines changed

cores/arduino/HardwareSerial.cpp

+26-13
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
#if defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3) ||\
3131
defined(HAVE_HWSERIAL4) || defined(HAVE_HWSERIAL5) || defined(HAVE_HWSERIAL6) ||\
3232
defined(HAVE_HWSERIAL7) || defined(HAVE_HWSERIAL8) || defined(HAVE_HWSERIAL9) ||\
33-
defined(HAVE_HWSERIAL10) || defined(HAVE_HWSERIALLP1) || defined(HAVE_HWSERIALLP2)
33+
defined(HAVE_HWSERIAL10) || defined(HAVE_HWSERIALLP1) || defined(HAVE_HWSERIALLP2) ||\
34+
defined(HAVE_HWSERIALLP3)
3435
// SerialEvent functions are weak, so when the user doesn't define them,
3536
// the linker just sets their address to 0 (which is checked below).
3637
#if defined(HAVE_HWSERIAL1)
@@ -112,6 +113,10 @@
112113
HardwareSerial SerialLP2(LPUART2);
113114
void serialEventLP2() __attribute__((weak));
114115
#endif
116+
#if defined(HAVE_HWSERIALLP3)
117+
HardwareSerial SerialLP2(LPUART3);
118+
void serialEventLP3() __attribute__((weak));
119+
#endif
115120
#endif // HAVE_HWSERIALx
116121

117122
// Constructors ////////////////////////////////////////////////////////////////
@@ -267,22 +272,30 @@ HardwareSerial::HardwareSerial(void *peripheral, HalfDuplexMode_t halfDuplex)
267272
setTx(PIN_SERIALLP2_TX);
268273
} else
269274
#endif
275+
#if defined(PIN_SERIALLP3_TX) && defined(LPUART3_BASE)
276+
if (peripheral == LPUART2) {
277+
#if defined(PIN_SERIALLP3_RX)
278+
setRx(PIN_SERIALLP3_RX);
279+
#endif
280+
setTx(PIN_SERIALLP3_TX);
281+
} else
282+
#endif
270283
#if defined(PIN_SERIAL_TX)
271-
// If PIN_SERIAL_TX is defined but Serial is mapped on other peripheral
272-
// (usually SerialUSB) use the pins defined for specified peripheral
273-
// instead of the first one found
274-
if ((pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX) == peripheral)) {
284+
// If PIN_SERIAL_TX is defined but Serial is mapped on other peripheral
285+
// (usually SerialUSB) use the pins defined for specified peripheral
286+
// instead of the first one found
287+
if ((pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX) == peripheral)) {
275288
#if defined(PIN_SERIAL_RX)
276-
setRx(PIN_SERIAL_RX);
289+
setRx(PIN_SERIAL_RX);
277290
#endif
278-
setTx(PIN_SERIAL_TX);
279-
} else
291+
setTx(PIN_SERIAL_TX);
292+
} else
280293
#endif
281-
{
282-
// else get the pins of the first peripheral occurrence in PinMap
283-
_serial.pin_rx = pinmap_pin(peripheral, PinMap_UART_RX);
284-
_serial.pin_tx = pinmap_pin(peripheral, PinMap_UART_TX);
285-
}
294+
{
295+
// else get the pins of the first peripheral occurrence in PinMap
296+
_serial.pin_rx = pinmap_pin(peripheral, PinMap_UART_RX);
297+
_serial.pin_tx = pinmap_pin(peripheral, PinMap_UART_TX);
298+
}
286299
if (halfDuplex == HALF_DUPLEX_ENABLED) {
287300
_serial.pin_rx = NC;
288301
}

cores/arduino/HardwareSerial.h

+3
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,7 @@ class HardwareSerial : public Stream {
223223
#if defined(LPUART2)
224224
extern HardwareSerial SerialLP2;
225225
#endif
226+
#if defined(LPUART3)
227+
extern HardwareSerial SerialLP3;
228+
#endif
226229
#endif

cores/arduino/WSerial.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ WEAK void serialEventRun(void)
6262
serialEventLP2();
6363
}
6464
#endif
65+
#if defined(HAVE_HWSERIALLP3)
66+
if (serialEventLP3 && SerialLP3.available()) {
67+
serialEventLP3();
68+
}
69+
#endif
6570
#if defined(HAVE_SERIALUSB)
6671
if (serialEventUSB && SerialUSB.available()) {
6772
serialEventUSB();

cores/arduino/WSerial.h

+14
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656
#define Serial SerialLP2
5757
#define serialEvent serialEventLP2
5858
#endif
59+
#elif SERIAL_UART_INSTANCE == 103
60+
#define ENABLE_HWSERIALLP3
61+
#if !defined(Serial)
62+
#define Serial SerialLP3
63+
#define serialEvent serialEventLP3
64+
#endif
5965
#elif SERIAL_UART_INSTANCE == 1
6066
#define ENABLE_HWSERIAL1
6167
#if !defined(Serial)
@@ -133,6 +139,11 @@
133139
#define HAVE_HWSERIALLP2
134140
#endif
135141
#endif
142+
#if defined(ENABLE_HWSERIALLP3)
143+
#if defined(LPUART3_BASE)
144+
#define HAVE_HWSERIALLP3
145+
#endif
146+
#endif
136147
#if defined(ENABLE_HWSERIAL1)
137148
#if defined(USART1_BASE)
138149
#define HAVE_HWSERIAL1
@@ -220,6 +231,9 @@
220231
#if defined(HAVE_HWSERIALLP2)
221232
extern void serialEventLP2(void) __attribute__((weak));
222233
#endif
234+
#if defined(HAVE_HWSERIALLP3)
235+
extern void serialEventLP3(void) __attribute__((weak));
236+
#endif
223237
#endif /* HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY */
224238

225239
extern void serialEventRun(void);

libraries/SrcWrapper/inc/uart.h

+6
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ struct serial_s {
246246
#endif /* STM32U0xx */
247247
#endif
248248

249+
#if defined(LPUART3_BASE) && !defined(LPUART3_IRQn)
250+
#if defined(STM32U0xx)
251+
#define LPUART3_IRQn USART4_LPUART3_IRQn
252+
#endif /* STM32U0xx */
253+
#endif
254+
249255
/* Exported macro ------------------------------------------------------------*/
250256
/* Exported functions ------------------------------------------------------- */
251257
void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t parity, uint32_t stopbits);

libraries/SrcWrapper/src/stm32/uart.c

+71
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ typedef enum {
7373
#endif
7474
#if defined(LPUART2_BASE)
7575
LPUART2_INDEX,
76+
#endif
77+
#if defined(LPUART3_BASE)
78+
LPUART3_INDEX,
7679
#endif
7780
UART_NUM
7881
} uart_index_t;
@@ -253,6 +256,15 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
253256
obj->irq = LPUART2_IRQn;
254257
}
255258
#endif
259+
#if defined(LPUART3_BASE)
260+
else if (obj->uart == LPUART3) {
261+
__HAL_RCC_LPUART3_FORCE_RESET();
262+
__HAL_RCC_LPUART3_RELEASE_RESET();
263+
__HAL_RCC_LPUART3_CLK_ENABLE();
264+
obj->index = LPUART3_INDEX;
265+
obj->irq = LPUART3_IRQn;
266+
}
267+
#endif
256268
#if defined(UART7_BASE)
257269
else if (obj->uart == UART7) {
258270
__HAL_RCC_UART7_FORCE_RESET();
@@ -362,6 +374,9 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
362374
if ((obj->uart == LPUART1)
363375
#if defined(LPUART2_BASE)
364376
|| (obj->uart == LPUART2)
377+
#endif
378+
#if defined(LPUART3_BASE)
379+
|| (obj->uart == LPUART3)
365380
#endif
366381
) {
367382
if (baudrate <= 9600) {
@@ -395,6 +410,11 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
395410
if (obj->uart == LPUART2) {
396411
__HAL_RCC_LPUART2_CONFIG(RCC_LPUART2CLKSOURCE_LSE);
397412
}
413+
#endif
414+
#if defined(LPUART3_BASE)
415+
if (obj->uart == LPUART3) {
416+
__HAL_RCC_LPUART3_CONFIG(RCC_LPUART3CLKSOURCE_LSE);
417+
}
398418
#endif
399419
if (uart_rx == NP) {
400420
if (HAL_HalfDuplex_Init(huart) == HAL_OK) {
@@ -412,6 +432,11 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
412432
if (obj->uart == LPUART2) {
413433
__HAL_RCC_LPUART2_CONFIG(RCC_LPUART2CLKSOURCE_HSI);
414434
}
435+
#endif
436+
#if defined(LPUART3_BASE)
437+
if (obj->uart == LPUART3) {
438+
__HAL_RCC_LPUART3_CONFIG(RCC_LPUART3CLKSOURCE_HSI);
439+
}
415440
#endif
416441
if (uart_rx == NP) {
417442
if (HAL_HalfDuplex_Init(huart) == HAL_OK) {
@@ -434,6 +459,11 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
434459
if (obj->uart == LPUART2) {
435460
__HAL_RCC_LPUART2_CONFIG(RCC_LPUART2CLKSOURCE_PCLK1);
436461
}
462+
#endif
463+
#if defined(LPUART3_BASE)
464+
if (obj->uart == LPUART3) {
465+
__HAL_RCC_LPUART3_CONFIG(RCC_LPUART3CLKSOURCE_PCLK1);
466+
}
437467
#endif
438468
if (uart_rx == NP) {
439469
if (HAL_HalfDuplex_Init(huart) == HAL_OK) {
@@ -451,6 +481,11 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
451481
if (obj->uart == LPUART2) {
452482
__HAL_RCC_LPUART2_CONFIG(RCC_LPUART2CLKSOURCE_SYSCLK);
453483
}
484+
#endif
485+
#if defined(LPUART3_BASE)
486+
if (obj->uart == LPUART3) {
487+
__HAL_RCC_LPUART3_CONFIG(RCC_LPUART3CLKSOURCE_SYSCLK);
488+
}
454489
#endif
455490
}
456491
#endif
@@ -541,6 +576,13 @@ void uart_deinit(serial_t *obj)
541576
__HAL_RCC_LPUART2_CLK_DISABLE();
542577
break;
543578
#endif
579+
#if defined(LPUART3_BASE)
580+
case LPUART3_INDEX:
581+
__HAL_RCC_LPUART3_FORCE_RESET();
582+
__HAL_RCC_LPUART3_RELEASE_RESET();
583+
__HAL_RCC_LPUART3_CLK_DISABLE();
584+
break;
585+
#endif
544586
#if defined(UART7_BASE)
545587
case UART7_INDEX:
546588
__HAL_RCC_UART7_FORCE_RESET();
@@ -666,6 +708,13 @@ void uart_config_lowpower(serial_t *obj)
666708
__HAL_RCC_LPUART2_CONFIG(RCC_LPUART2CLKSOURCE_HSI);
667709
}
668710
break;
711+
#endif
712+
#if defined(LPUART3_BASE) && defined(__HAL_RCC_LPUART3_CONFIG)
713+
case LPUART3_INDEX:
714+
if (__HAL_RCC_GET_LPUART3_SOURCE() != RCC_LPUART3CLKSOURCE_HSI) {
715+
__HAL_RCC_LPUART3_CONFIG(RCC_LPUART3CLKSOURCE_HSI);
716+
}
717+
break;
669718
#endif
670719
}
671720
#if defined(UART_WAKEUP_EXTI_LINE)
@@ -1074,6 +1123,28 @@ void USART4_5_IRQHandler(void)
10741123
#endif
10751124
#endif
10761125

1126+
/**
1127+
* @brief USART 4 IRQ handler
1128+
* @param None
1129+
* @retval None
1130+
*/
1131+
#if defined(STM32U0xx)
1132+
#if defined(USART4_BASE)
1133+
void USART4_IRQHandler(void)
1134+
{
1135+
HAL_NVIC_ClearPendingIRQ(USART4_IRQn);
1136+
if (uart_handlers[UART4_INDEX] != NULL) {
1137+
HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]);
1138+
}
1139+
#if defined(LPUART3_BASE)
1140+
if (uart_handlers[LPUART3_INDEX] != NULL) {
1141+
HAL_UART_IRQHandler(uart_handlers[LPUART3_INDEX]);
1142+
}
1143+
#endif
1144+
}
1145+
#endif
1146+
#endif
1147+
10771148
/**
10781149
* @brief USART 5 IRQ handler
10791150
* @param None

0 commit comments

Comments
 (0)