@@ -70,6 +70,9 @@ typedef enum {
70
70
#endif
71
71
#if defined(LPUART1_BASE )
72
72
LPUART1_INDEX ,
73
+ #endif
74
+ #if defined(LPUART2_BASE )
75
+ LPUART2_INDEX ,
73
76
#endif
74
77
UART_NUM
75
78
} uart_index_t ;
@@ -210,6 +213,15 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
210
213
obj -> irq = LPUART1_IRQn ;
211
214
}
212
215
#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
213
225
#if defined(UART7_BASE )
214
226
else if (obj -> uart == UART7 ) {
215
227
__HAL_RCC_UART7_FORCE_RESET ();
@@ -299,13 +311,17 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
299
311
/* Set the NVIC priority for future interrupts */
300
312
HAL_NVIC_SetPriority (obj -> irq , UART_IRQ_PRIO , UART_IRQ_SUBPRIO );
301
313
302
- #if defined(LPUART1_BASE )
314
+ #if defined(LPUART1_BASE ) || defined( LPUART2_BASE )
303
315
/*
304
316
* Note that LPUART clock source must be in the range
305
317
* [3 x baud rate, 4096 x baud rate]
306
318
* check Reference Manual
307
319
*/
308
- if (obj -> uart == LPUART1 ) {
320
+ if ((obj -> uart == LPUART1 )
321
+ #if defined(LPUART2_BASE )
322
+ || (obj -> uart == LPUART2 )
323
+ #endif
324
+ ) {
309
325
if (baudrate <= 9600 ) {
310
326
#if defined(USART_CR3_UCESM )
311
327
HAL_UARTEx_EnableClockStopMode (huart );
@@ -326,24 +342,51 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
326
342
if (baudrate <= 9600 ) {
327
343
/* Enable the clock if not already set by user */
328
344
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
331
353
if (HAL_UART_Init (huart ) == HAL_OK ) {
332
354
return ;
333
355
}
334
356
}
335
357
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
337
366
if (HAL_UART_Init (huart ) == HAL_OK ) {
338
367
return ;
339
368
}
340
369
}
341
370
#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
343
379
if (HAL_UART_Init (huart ) == HAL_OK ) {
344
380
return ;
345
381
}
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
347
390
#else
348
391
__HAL_RCC_LPUART1_CONFIG (RCC_LPUART1CLKSOURCE_CSI );
349
392
#endif
@@ -429,6 +472,13 @@ void uart_deinit(serial_t *obj)
429
472
__HAL_RCC_LPUART1_CLK_DISABLE ();
430
473
break ;
431
474
#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
432
482
#if defined(UART7_BASE )
433
483
case UART7_INDEX :
434
484
__HAL_RCC_UART7_FORCE_RESET ();
@@ -544,6 +594,13 @@ void uart_config_lowpower(serial_t *obj)
544
594
__HAL_RCC_LPUART1_CONFIG (RCC_LPUART1CLKSOURCE_HSI );
545
595
}
546
596
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 ;
547
604
#endif
548
605
}
549
606
hsem_unlock (CFG_HW_RCC_CRRCR_CCIPR_SEMID );
@@ -866,7 +923,14 @@ void USART1_IRQHandler(void)
866
923
void USART2_IRQHandler (void )
867
924
{
868
925
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
870
934
}
871
935
#endif
872
936
@@ -907,7 +971,7 @@ void USART3_IRQHandler(void)
907
971
if (uart_handlers [UART4_INDEX ] != NULL ) {
908
972
HAL_UART_IRQHandler (uart_handlers [UART4_INDEX ]);
909
973
}
910
- #if defined(STM32F030xC )
974
+ #if defined(STM32F030xC ) || defined( STM32G0xx ) && defined( LPUART2_BASE )
911
975
if (uart_handlers [UART5_INDEX ] != NULL ) {
912
976
HAL_UART_IRQHandler (uart_handlers [UART5_INDEX ]);
913
977
}
0 commit comments