Skip to content

Commit 0aca15a

Browse files
committed
[HWSerial] Add LPUART1 management
Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 83d872f commit 0aca15a

File tree

4 files changed

+124
-4
lines changed

4 files changed

+124
-4
lines changed

cores/arduino/HardwareSerial.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#if defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3) ||\
3535
defined(HAVE_HWSERIAL4) || defined(HAVE_HWSERIAL5) || defined(HAVE_HWSERIAL6) ||\
3636
defined(HAVE_HWSERIAL7) || defined(HAVE_HWSERIAL8) || defined(HAVE_HWSERIAL8) ||\
37-
defined(HAVE_HWSERIAL10)
37+
defined(HAVE_HWSERIAL10) || defined(HAVE_HWSERIALLP1)
3838
// SerialEvent functions are weak, so when the user doesn't define them,
3939
// the linker just sets their address to 0 (which is checked below).
4040
#if defined(HAVE_HWSERIAL1)
@@ -102,6 +102,11 @@
102102
HardwareSerial Serial10(UART10);
103103
void serialEvent10() __attribute__((weak));
104104
#endif
105+
106+
#if defined(HAVE_HWSERIALLP1)
107+
HardwareSerial SerialLP1(LPUART1);
108+
void serialEventLP1() __attribute__((weak));
109+
#endif
105110
#endif // HAVE_HWSERIALx
106111

107112
void serialEventRun(void)
@@ -136,6 +141,9 @@ void serialEventRun(void)
136141
#if defined(HAVE_HWSERIAL10)
137142
if (serialEventl10 && Serial10.available()) serialEvent10();
138143
#endif
144+
#if defined(HAVE_HWSERIALLP1)
145+
if (serialEventLP1 && SerialLP1.available()) serialEventLP1();
146+
#endif
139147
}
140148

141149
// Constructors ////////////////////////////////////////////////////////////////

cores/arduino/HardwareSerial.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ extern HardwareSerial Serial7;
146146
extern HardwareSerial Serial8;
147147
extern HardwareSerial Serial9;
148148
extern HardwareSerial Serial10;
149+
extern HardwareSerial SerialLP1;
149150

150151
extern void serialEventRun(void) __attribute__((weak));
151152

cores/arduino/stm32/uart.c

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@
7676
#define UART_NUM (10)
7777
#elif defined(STM32F0xx) || defined(STM32F7xx)
7878
#define UART_NUM (8)
79-
#elif defined(STM32F2xx)
79+
#elif defined(STM32F2xx) || defined(STM32L4xx)
8080
#define UART_NUM (6)
8181
#elif defined(STM32F1xx) || defined(STM32F3xx) ||\
82-
defined(STM32L0xx) || defined(STM32L1xx) || defined(STM32L4xx)
82+
defined(STM32L0xx) || defined(STM32L1xx)
8383
#define UART_NUM (5)
8484
#else
8585
#error "Unknown Family - unknown UART_NUM"
@@ -198,6 +198,19 @@ void uart_init(serial_t *obj)
198198
obj->irq = USART6_IRQn;
199199
}
200200
#endif
201+
#if defined(LPUART1_BASE)
202+
else if(obj->uart == LPUART1) {
203+
__HAL_RCC_LPUART1_FORCE_RESET();
204+
__HAL_RCC_LPUART1_RELEASE_RESET();
205+
__HAL_RCC_LPUART1_CLK_ENABLE();
206+
#if !defined(USART3_BASE)
207+
obj->index = 2;
208+
#else
209+
obj->index = 5;
210+
#endif
211+
obj->irq = LPUART1_IRQn;
212+
}
213+
#endif
201214
#if defined(UART7_BASE)
202215
else if(obj->uart == UART7) {
203216
__HAL_RCC_UART7_FORCE_RESET();
@@ -280,6 +293,7 @@ void uart_init(serial_t *obj)
280293
#endif /* STM32F1xx */
281294
HAL_GPIO_Init(port, &GPIO_InitStruct);
282295

296+
283297
//Configure uart
284298
uart_handlers[obj->index] = huart;
285299
huart->Instance = (USART_TypeDef *)(obj->uart);
@@ -296,6 +310,56 @@ void uart_init(serial_t *obj)
296310
#endif
297311
// huart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
298312

313+
#if defined(LPUART1_BASE)
314+
/* Note that LPUART clock source must be in the range [3 x baud rate, 4096 x baud rate], check Ref Manual */
315+
if(obj->uart == LPUART1) {
316+
if (obj->baudrate <= 9600) {
317+
HAL_UARTEx_EnableClockStopMode(huart);
318+
HAL_UARTEx_EnableStopMode(huart);
319+
} else {
320+
HAL_UARTEx_DisableClockStopMode(huart);
321+
HAL_UARTEx_DisableStopMode(huart);
322+
}
323+
/* Trying default LPUART clock source */
324+
if (HAL_UART_Init(huart) == HAL_OK) {
325+
return;
326+
}
327+
/* Trying to change LPUART clock source */
328+
/* If baudrate is lower than or equal to 9600 try to change to LSE */
329+
if(obj->baudrate <= 9600) {
330+
/* Enable the clock if not already set by user */
331+
if(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) {
332+
#ifdef __HAL_RCC_LSEDRIVE_CONFIG
333+
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
334+
#endif
335+
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
336+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
337+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
338+
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
339+
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
340+
Error_Handler();
341+
}
342+
}
343+
344+
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_LSE);
345+
if (HAL_UART_Init(huart) == HAL_OK) {
346+
return;
347+
}
348+
}
349+
if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) {
350+
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_HSI);
351+
if (HAL_UART_Init(huart) == HAL_OK) {
352+
return;
353+
}
354+
}
355+
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_PCLK1);
356+
if (HAL_UART_Init(huart) == HAL_OK) {
357+
return;
358+
}
359+
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_SYSCLK);
360+
}
361+
#endif
362+
299363
if(HAL_UART_Init(huart) != HAL_OK) {
300364
return;
301365
}
@@ -330,6 +394,12 @@ void uart_deinit(serial_t *obj)
330394
__HAL_RCC_USART3_RELEASE_RESET();
331395
__HAL_RCC_USART3_CLK_DISABLE();
332396
break;
397+
#elif defined(LPUART1_BASE)
398+
case 2:
399+
__HAL_RCC_LPUART1_FORCE_RESET();
400+
__HAL_RCC_LPUART1_RELEASE_RESET();
401+
__HAL_RCC_LPUART1_CLK_DISABLE();
402+
break;
333403
#endif
334404
#if defined(UART4_BASE)
335405
case 3:
@@ -363,6 +433,12 @@ void uart_deinit(serial_t *obj)
363433
__HAL_RCC_USART6_RELEASE_RESET();
364434
__HAL_RCC_USART6_CLK_DISABLE();
365435
break;
436+
#elif defined(LPUART1_BASE)
437+
case 5:
438+
__HAL_RCC_LPUART1_FORCE_RESET();
439+
__HAL_RCC_LPUART1_RELEASE_RESET();
440+
__HAL_RCC_LPUART1_CLK_DISABLE();
441+
break;
366442
#endif
367443
#if defined(UART7_BASE)
368444
case 6:
@@ -467,6 +543,17 @@ void uart_config_lowpower(serial_t *obj)
467543
__HAL_RCC_UART5_CONFIG(RCC_UART5CLKSOURCE_HSI);
468544
}
469545
break;
546+
#endif
547+
#if defined(LPUART1_BASE) && defined(__HAL_RCC_LPUART1_CONFIG)
548+
#if !defined(USART3_BASE)
549+
case 2:
550+
#else
551+
case 5:
552+
#endif
553+
if (__HAL_RCC_GET_LPUART1_SOURCE() != RCC_LPUART1CLKSOURCE_HSI) {
554+
__HAL_RCC_LPUART1_CONFIG(RCC_LPUART1CLKSOURCE_HSI);
555+
}
556+
break;
470557
#endif
471558
}
472559
}
@@ -883,6 +970,19 @@ void USART6_IRQHandler(void)
883970
}
884971
#endif
885972

973+
/**
974+
* @brief LPUART 1 IRQ handler
975+
* @param None
976+
* @retval None
977+
*/
978+
#if defined(LPUART1_BASE)
979+
void LPUART1_IRQHandler(void)
980+
{
981+
HAL_NVIC_ClearPendingIRQ(LPUART1_IRQn);
982+
HAL_UART_IRQHandler(uart_handlers[5]);
983+
}
984+
#endif
985+
886986
/**
887987
* @brief UART 7 IRQ handler
888988
* @param None

cores/arduino/stm32/uart.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@
5252
#else
5353
#if !defined(HWSERIAL_NONE) && defined(SERIAL_UART_INSTANCE)
5454

55-
#if SERIAL_UART_INSTANCE == 1
55+
#if SERIAL_UART_INSTANCE == 0
56+
#define ENABLE_HWSERIALLP1
57+
#if !defined(Serial)
58+
#define Serial SerialLP1
59+
#define serialEvent serialEventLP1
60+
#endif
61+
#elif SERIAL_UART_INSTANCE == 1
5662
#define ENABLE_HWSERIAL1
5763
#if !defined(Serial)
5864
#define Serial Serial1
@@ -119,6 +125,11 @@
119125
#endif // SERIAL_UART_INSTANCE == x
120126
#endif // !HWSERIAL_NONE && SERIAL_UART_INSTANCE
121127

128+
#if defined(ENABLE_HWSERIALLP1)
129+
#if defined(LPUART1_BASE)
130+
#define HAVE_HWSERIALLP1
131+
#endif
132+
#endif
122133
#if defined(ENABLE_HWSERIAL1)
123134
#if defined(USART1_BASE)
124135
#define HAVE_HWSERIAL1

0 commit comments

Comments
 (0)