Skip to content

Commit d04cf3d

Browse files
committed
uart: G0: add LPUART2 support
Signed-off-by: Frederic Pillon <[email protected]>
1 parent b3f5a2f commit d04cf3d

File tree

7 files changed

+160
-28
lines changed

7 files changed

+160
-28
lines changed

cores/arduino/HardwareSerial.cpp

+19-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
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)
33+
defined(HAVE_HWSERIAL10) || defined(HAVE_HWSERIALLP1) || defined(HAVE_HWSERIALLP2)
3434
// SerialEvent functions are weak, so when the user doesn't define them,
3535
// the linker just sets their address to 0 (which is checked below).
3636
#if defined(HAVE_HWSERIAL1)
@@ -107,6 +107,11 @@
107107
HardwareSerial SerialLP1(LPUART1);
108108
void serialEventLP1() __attribute__((weak));
109109
#endif
110+
111+
#if defined(HAVE_HWSERIALLP2)
112+
HardwareSerial SerialLP2(LPUART2);
113+
void serialEventLP2() __attribute__((weak));
114+
#endif
110115
#endif // HAVE_HWSERIALx
111116

112117
// Constructors ////////////////////////////////////////////////////////////////
@@ -252,11 +257,19 @@ HardwareSerial::HardwareSerial(void *peripheral, HalfDuplexMode_t halfDuplex)
252257
setTx(PIN_SERIALLP1_TX);
253258
} else
254259
#endif
255-
// else get the pins of the first peripheral occurence in PinMap
256-
{
257-
_serial.pin_rx = pinmap_pin(peripheral, PinMap_UART_RX);
258-
_serial.pin_tx = pinmap_pin(peripheral, PinMap_UART_TX);
259-
}
260+
#if defined(PIN_SERIALLP2_TX) && defined(LPUART2_BASE)
261+
if (peripheral == LPUART2) {
262+
#if defined(PIN_SERIALLP2_RX)
263+
setRx(PIN_SERIALLP2_RX);
264+
#endif
265+
setTx(PIN_SERIALLP2_TX);
266+
} else
267+
#endif
268+
// else get the pins of the first peripheral occurence in PinMap
269+
{
270+
_serial.pin_rx = pinmap_pin(peripheral, PinMap_UART_RX);
271+
_serial.pin_tx = pinmap_pin(peripheral, PinMap_UART_TX);
272+
}
260273
if (halfDuplex == HALF_DUPLEX_ENABLED) {
261274
_serial.pin_rx = NC;
262275
}

cores/arduino/HardwareSerial.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -207,5 +207,7 @@ class HardwareSerial : public Stream {
207207
#if defined(LPUART1)
208208
extern HardwareSerial SerialLP1;
209209
#endif
210-
210+
#if defined(LPUART2)
211+
extern HardwareSerial SerialLP2;
212+
#endif
211213
#endif

cores/arduino/WSerial.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ WEAK void serialEventRun(void)
5757
serialEventLP1();
5858
}
5959
#endif
60+
#if defined(HAVE_HWSERIALLP2)
61+
if (serialEventLP2 && SerialLP2.available()) {
62+
serialEventLP2();
63+
}
64+
#endif
6065
#if defined(HAVE_SERIALUSB)
6166
if (serialEventUSB && SerialUSB.available()) {
6267
serialEventUSB();

cores/arduino/WSerial.h

+14
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
#define Serial SerialLP1
4747
#define serialEvent serialEventLP1
4848
#endif
49+
#elif SERIAL_UART_INSTANCE == 102
50+
#define ENABLE_HWSERIALLP2
51+
#if !defined(Serial)
52+
#define Serial SerialLP2
53+
#define serialEvent serialEventLP2
54+
#endif
4955
#elif SERIAL_UART_INSTANCE == 1
5056
#define ENABLE_HWSERIAL1
5157
#if !defined(Serial)
@@ -118,6 +124,11 @@
118124
#define HAVE_HWSERIALLP1
119125
#endif
120126
#endif
127+
#if defined(ENABLE_HWSERIALLP2)
128+
#if defined(LPUART2_BASE)
129+
#define HAVE_HWSERIALLP2
130+
#endif
131+
#endif
121132
#if defined(ENABLE_HWSERIAL1)
122133
#if defined(USART1_BASE)
123134
#define HAVE_HWSERIAL1
@@ -202,6 +213,9 @@
202213
#if defined(HAVE_HWSERIALLP1)
203214
extern void serialEventLP1(void) __attribute__((weak));
204215
#endif
216+
#if defined(HAVE_HWSERIALLP2)
217+
extern void serialEventLP2(void) __attribute__((weak));
218+
#endif
205219
#endif /* HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY */
206220

207221
extern void serialEventRun(void);

cores/arduino/stm32/uart.h

+45-12
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ struct serial_s {
8686
/* Exported constants --------------------------------------------------------*/
8787
#define TX_TIMEOUT 1000
8888

89+
#if defined(USART2_BASE) && !defined(USART2_IRQn)
90+
#if defined(STM32G0xx)
91+
#if defined(LPUART2_BASE)
92+
#define USART2_IRQn USART2_LPUART2_IRQn
93+
#define USART2_IRQHandler USART2_LPUART2_IRQHandler
94+
#endif
95+
#endif /* STM32G0xx */
96+
#endif
97+
8998
#if defined(USART3_BASE) && !defined(USART3_IRQn)
9099
#if defined(STM32F0xx)
91100
#if defined(STM32F091xC) || defined (STM32F098xx)
@@ -98,17 +107,18 @@ struct serial_s {
98107
#define USART3_IRQn USART3_4_IRQn
99108
#define USART3_IRQHandler USART3_4_IRQHandler
100109
#endif /* STM32F091xC || STM32F098xx */
101-
#endif /* STM32F0xx */
102-
103-
#if defined(STM32G0xx)
104-
#if defined(LPUART1_BASE)
110+
#elif defined(STM32G0xx)
111+
#if defined(LPUART2_BASE)
112+
#define USART3_IRQn USART3_4_5_6_LPUART1_IRQn
113+
#define USART3_IRQHandler USART3_4_5_6_LPUART1_IRQHandler
114+
#elif defined(LPUART1_BASE)
105115
#define USART3_IRQn USART3_4_LPUART1_IRQn
106116
#define USART3_IRQHandler USART3_4_LPUART1_IRQHandler
107117
#else
108118
#define USART3_IRQn USART3_4_IRQn
109119
#define USART3_IRQHandler USART3_4_IRQHandler
110120
#endif
111-
#endif /* STM32G0xx */
121+
#endif /* STM32F0xx */
112122
#endif
113123

114124
#if defined(USART4_BASE) && !defined(USART4_IRQn)
@@ -123,15 +133,15 @@ struct serial_s {
123133
#endif /* STM32F091xC || STM32F098xx */
124134
#elif defined(STM32L0xx)
125135
#define USART4_IRQn USART4_5_IRQn
126-
#endif /* STM32F0xx */
127-
#if defined(STM32G0xx)
128-
#if defined(LPUART1_BASE)
136+
#elif defined(STM32G0xx)
137+
#if defined(LPUART2_BASE)
138+
#define USART4_IRQn USART3_4_5_6_LPUART1_IRQn
139+
#elif defined(LPUART1_BASE)
129140
#define USART4_IRQn USART3_4_LPUART1_IRQn
130141
#else
131142
#define USART4_IRQn USART3_4_IRQn
132143
#endif
133144
#endif /* STM32G0xx */
134-
135145
#endif
136146

137147
#if defined(USART5_BASE) && !defined(USART5_IRQn)
@@ -142,40 +152,63 @@ struct serial_s {
142152
#elif defined(STM32F030xC)
143153
#define USART5_IRQn USART3_6_IRQn
144154
#endif /* STM32F091xC || STM32F098xx */
155+
#elif defined(STM32G0xx)
156+
#if defined(LPUART2_BASE)
157+
#define USART5_IRQn USART3_4_5_6_LPUART1_IRQn
158+
#endif
145159
#elif defined(STM32L0xx)
146160
#define USART5_IRQn USART4_5_IRQn
147161
#endif /* STM32F0xx */
148162
#endif
149163

150-
#if defined (STM32F0xx)
151164
/* IRQHandler is mapped on USART3_IRQHandler for STM32F0xx */
152165
#if defined(USART6_BASE) && !defined(USART6_IRQn)
166+
#if defined (STM32F0xx)
153167
#if defined(STM32F091xC) || defined (STM32F098xx)
154168
#define USART6_IRQn USART3_8_IRQn
155169
#elif defined(STM32F030xC)
156170
#define USART6_IRQn USART3_6_IRQn
157171
#endif /* STM32F091xC || STM32F098xx */
172+
#elif defined(STM32G0xx)
173+
#if defined(LPUART2_BASE)
174+
#define USART6_IRQn USART3_4_5_6_LPUART1_IRQn
175+
#endif
176+
#endif /* STM32F0xx */
158177
#endif
159178

160179
#if defined(USART7_BASE) && !defined(USART7_IRQn)
180+
#if defined (STM32F0xx)
161181
#if defined(STM32F091xC) || defined (STM32F098xx)
162182
#define USART7_IRQn USART3_8_IRQn
163183
#endif /* STM32F091xC || STM32F098xx */
184+
#endif /* STM32F0xx */
164185
#endif
165186

166187
#if defined(USART8_BASE) && !defined(USART8_IRQn)
188+
#if defined (STM32F0xx)
167189
#if defined(STM32F091xC) || defined (STM32F098xx)
168190
#define USART8_IRQn USART3_8_IRQn
169191
#endif /* STM32F091xC || STM32F098xx */
170-
#endif
171192
#endif /* STM32F0xx */
193+
#endif
172194

173195
#if defined(LPUART1_BASE) && !defined(LPUART1_IRQn)
174-
#if defined(STM32G0xx) && defined(USART3_BASE)
196+
#if defined(STM32G0xx)
197+
#if defined(LPUART2_BASE)
198+
#define LPUART1_IRQn USART3_4_5_6_LPUART1_IRQn
199+
#elif defined(USART3_BASE)
175200
#define LPUART1_IRQn USART3_4_LPUART1_IRQn
201+
#endif
176202
#endif /* STM32G0xx */
177203
#endif
178204

205+
#if defined(LPUART2_BASE) && !defined(LPUART2_IRQn)
206+
#if defined(STM32G0xx)
207+
#if defined(LPUART2_BASE)
208+
#define LPUART2_IRQn USART2_LPUART2_IRQn
209+
#endif
210+
#endif /* STM32G0xx */
211+
#endif
179212

180213
/* Exported macro ------------------------------------------------------------*/
181214
/* Exported functions ------------------------------------------------------- */

keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,7 @@ UART9 LITERAL1
816816
UART10 LITERAL1
817817
USART10 LITERAL1
818818
LPUART1 LITERAL1
819+
LPUART2 LITERAL1
819820

820821
# Port
821822
GPIOA_BASE LITERAL1

libraries/SrcWrapper/src/stm32/uart.c

+73-9
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ typedef enum {
7070
#endif
7171
#if defined(LPUART1_BASE)
7272
LPUART1_INDEX,
73+
#endif
74+
#if defined(LPUART2_BASE)
75+
LPUART2_INDEX,
7376
#endif
7477
UART_NUM
7578
} uart_index_t;
@@ -210,6 +213,15 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
210213
obj->irq = LPUART1_IRQn;
211214
}
212215
#endif
216+
#if defined(LPUART2_BASE)
217+
else if (obj->uart == LPUART2) {
218+
__HAL_RCC_LPUART2_FORCE_RESET();
219+
__HAL_RCC_LPUART2_RELEASE_RESET();
220+
__HAL_RCC_LPUART2_CLK_ENABLE();
221+
obj->index = LPUART2_INDEX;
222+
obj->irq = LPUART2_IRQn;
223+
}
224+
#endif
213225
#if defined(UART7_BASE)
214226
else if (obj->uart == UART7) {
215227
__HAL_RCC_UART7_FORCE_RESET();
@@ -299,13 +311,17 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
299311
/* Set the NVIC priority for future interrupts */
300312
HAL_NVIC_SetPriority(obj->irq, UART_IRQ_PRIO, UART_IRQ_SUBPRIO);
301313

302-
#if defined(LPUART1_BASE)
314+
#if defined(LPUART1_BASE) || defined(LPUART2_BASE)
303315
/*
304316
* Note that LPUART clock source must be in the range
305317
* [3 x baud rate, 4096 x baud rate]
306318
* check Reference Manual
307319
*/
308-
if (obj->uart == LPUART1) {
320+
if ((obj->uart == LPUART1)
321+
#if defined(LPUART2_BASE)
322+
|| (obj->uart == LPUART2)
323+
#endif
324+
) {
309325
if (baudrate <= 9600) {
310326
#if defined(USART_CR3_UCESM)
311327
HAL_UARTEx_EnableClockStopMode(huart);
@@ -326,24 +342,51 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
326342
if (baudrate <= 9600) {
327343
/* Enable the clock if not already set by user */
328344
enableClock(LSE_CLOCK);
329-
330-
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_LSE);
345+
if (obj->uart == LPUART1) {
346+
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_LSE);
347+
}
348+
#if defined(LPUART2_BASE)
349+
if (obj->uart == LPUART2) {
350+
__HAL_RCC_LPUART2_CONFIG(RCC_LPUART2CLKSOURCE_LSE);
351+
}
352+
#endif
331353
if (HAL_UART_Init(huart) == HAL_OK) {
332354
return;
333355
}
334356
}
335357
if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) {
336-
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_HSI);
358+
if (obj->uart == LPUART1) {
359+
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_HSI);
360+
}
361+
#if defined(LPUART2_BASE)
362+
if (obj->uart == LPUART2) {
363+
__HAL_RCC_LPUART2_CONFIG(RCC_LPUART2CLKSOURCE_HSI);
364+
}
365+
#endif
337366
if (HAL_UART_Init(huart) == HAL_OK) {
338367
return;
339368
}
340369
}
341370
#ifndef STM32H7xx
342-
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_PCLK1);
371+
if (obj->uart == LPUART1) {
372+
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_PCLK1);
373+
}
374+
#if defined(LPUART2_BASE)
375+
if (obj->uart == LPUART2) {
376+
__HAL_RCC_LPUART2_CONFIG(RCC_LPUART2CLKSOURCE_PCLK1);
377+
}
378+
#endif
343379
if (HAL_UART_Init(huart) == HAL_OK) {
344380
return;
345381
}
346-
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_SYSCLK);
382+
if (obj->uart == LPUART1) {
383+
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_SYSCLK);
384+
}
385+
#if defined(LPUART2_BASE)
386+
if (obj->uart == LPUART2) {
387+
__HAL_RCC_LPUART2_CONFIG(RCC_LPUART2CLKSOURCE_SYSCLK);
388+
}
389+
#endif
347390
#else
348391
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_CSI);
349392
#endif
@@ -429,6 +472,13 @@ void uart_deinit(serial_t *obj)
429472
__HAL_RCC_LPUART1_CLK_DISABLE();
430473
break;
431474
#endif
475+
#if defined(LPUART2_BASE)
476+
case LPUART2_INDEX:
477+
__HAL_RCC_LPUART2_FORCE_RESET();
478+
__HAL_RCC_LPUART2_RELEASE_RESET();
479+
__HAL_RCC_LPUART2_CLK_DISABLE();
480+
break;
481+
#endif
432482
#if defined(UART7_BASE)
433483
case UART7_INDEX:
434484
__HAL_RCC_UART7_FORCE_RESET();
@@ -544,6 +594,13 @@ void uart_config_lowpower(serial_t *obj)
544594
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_HSI);
545595
}
546596
break;
597+
#endif
598+
#if defined(LPUART2_BASE) && defined(__HAL_RCC_LPUART2_CONFIG)
599+
case LPUART2_INDEX:
600+
if (__HAL_RCC_GET_LPUART2_SOURCE() != RCC_LPUART2CLKSOURCE_HSI) {
601+
__HAL_RCC_LPUART2_CONFIG(RCC_LPUART2CLKSOURCE_HSI);
602+
}
603+
break;
547604
#endif
548605
}
549606
hsem_unlock(CFG_HW_RCC_CRRCR_CCIPR_SEMID);
@@ -866,7 +923,14 @@ void USART1_IRQHandler(void)
866923
void USART2_IRQHandler(void)
867924
{
868925
HAL_NVIC_ClearPendingIRQ(USART2_IRQn);
869-
HAL_UART_IRQHandler(uart_handlers[UART2_INDEX]);
926+
if (uart_handlers[UART2_INDEX] != NULL) {
927+
HAL_UART_IRQHandler(uart_handlers[UART2_INDEX]);
928+
}
929+
#if defined(STM32G0xx) && defined(LPUART2_BASE)
930+
if (uart_handlers[LPUART2_INDEX] != NULL) {
931+
HAL_UART_IRQHandler(uart_handlers[LPUART2_INDEX]);
932+
}
933+
#endif
870934
}
871935
#endif
872936

@@ -907,7 +971,7 @@ void USART3_IRQHandler(void)
907971
if (uart_handlers[UART4_INDEX] != NULL) {
908972
HAL_UART_IRQHandler(uart_handlers[UART4_INDEX]);
909973
}
910-
#if defined(STM32F030xC)
974+
#if defined(STM32F030xC) || defined(STM32G0xx) && defined(LPUART2_BASE)
911975
if (uart_handlers[UART5_INDEX] != NULL) {
912976
HAL_UART_IRQHandler(uart_handlers[UART5_INDEX]);
913977
}

0 commit comments

Comments
 (0)