Skip to content

bsp: add support for the STM32U5xx soc #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/STM32LowPower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ void STM32LowPower::programRtcWakeUp(uint32_t ms, LP_Mode lp_mode)
break;
default:
case SHUTDOWN_MODE:
#if defined(STM32L4xx) || defined(STM32L5xx)
// For shutdown mode LSE have to be used (STM32L4 or STM32L5 series only)
#if defined(PWR_CR1_LPMS)
// For shutdown mode LSE have to be used
clkSrc = STM32RTC::LSE_CLOCK;
#else
// LSE or LSI
Expand Down
119 changes: 99 additions & 20 deletions src/low_power.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@

#include "Arduino.h"
#include "low_power.h"
#include "stm32yyxx_ll_pwr.h"

#if defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION > 0x01090000) &&\
defined(HAL_PWR_MODULE_ENABLED) && !defined(HAL_PWR_MODULE_ONLY)
#if defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION > 0x01090000) \
&& defined(HAL_PWR_MODULE_ENABLED) && !defined(HAL_PWR_MODULE_ONLY)

#if defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY) \
&& (defined(UART_IT_WUF) || defined(LPUART1_BASE))
#define UART_WKUP_SUPPORT
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if defined(UART_IT_WUF) && defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY)
#if defined(UART_WKUP_SUPPORT)
/* Save UART handler for callback */
static UART_HandleTypeDef *WakeUpUart = NULL;
#endif
Expand All @@ -37,6 +43,11 @@ static void (*WakeUpUartCb)(void) = NULL;

#if defined(PWR_FLAG_WUF)
#define PWR_FLAG_WU PWR_FLAG_WUF
#elif defined(PWR_WAKEUP_ALL_FLAG)
#define PWR_FLAG_WU PWR_WAKEUP_ALL_FLAG
#endif
#if defined(PWR_FLAG_SBF)
#define PWR_FLAG_SB PWR_FLAG_SBF
#endif

/**
Expand Down Expand Up @@ -82,7 +93,14 @@ void LowPower_EnableWakeUpPin(uint32_t pin, uint32_t mode)
PinName p = digitalPinToPinName(pin);
if (p != NC) {
#ifdef PWR_WAKEUP_PIN1
if (p == SYS_WKUP1) {
if ((p == SYS_WKUP1)
#ifdef PWR_WAKEUP_PIN1_1
|| (p == SYS_WKUP1_1)
#endif
#ifdef PWR_WAKEUP_PIN1_2
|| (p == SYS_WKUP1_2)
#endif
) {
wkup_pin = PWR_WAKEUP_PIN1;
#ifdef PWR_WAKEUP_PIN1_HIGH
if (mode != RISING) {
Expand All @@ -92,7 +110,14 @@ void LowPower_EnableWakeUpPin(uint32_t pin, uint32_t mode)
}
#endif /* PWR_WAKEUP_PIN1 */
#ifdef PWR_WAKEUP_PIN2
if (p == SYS_WKUP2) {
if ((p == SYS_WKUP2)
#ifdef PWR_WAKEUP_PIN2_1
|| (p == SYS_WKUP2_1)
#endif
#ifdef PWR_WAKEUP_PIN2_2
|| (p == SYS_WKUP2_2)
#endif
) {
wkup_pin = PWR_WAKEUP_PIN2;
#ifdef PWR_WAKEUP_PIN2_HIGH
if (mode != RISING) {
Expand All @@ -102,7 +127,14 @@ void LowPower_EnableWakeUpPin(uint32_t pin, uint32_t mode)
}
#endif /* PWR_WAKEUP_PIN2 */
#ifdef PWR_WAKEUP_PIN3
if (p == SYS_WKUP3) {
if ((p == SYS_WKUP3)
#ifdef PWR_WAKEUP_PIN3_1
|| (p == SYS_WKUP3_1)
#endif
#ifdef PWR_WAKEUP_PIN3_2
|| (p == SYS_WKUP3_2)
#endif
) {
wkup_pin = PWR_WAKEUP_PIN3;
#ifdef PWR_WAKEUP_PIN3_HIGH
if (mode != RISING) {
Expand All @@ -112,7 +144,14 @@ void LowPower_EnableWakeUpPin(uint32_t pin, uint32_t mode)
}
#endif /* PWR_WAKEUP_PIN3 */
#ifdef PWR_WAKEUP_PIN4
if (p == SYS_WKUP4) {
if ((p == SYS_WKUP4)
#ifdef PWR_WAKEUP_PIN4_1
|| (p == SYS_WKUP4_1)
#endif
#ifdef PWR_WAKEUP_PIN4_2
|| (p == SYS_WKUP4_2)
#endif
) {
wkup_pin = PWR_WAKEUP_PIN4;
#ifdef PWR_WAKEUP_PIN4_HIGH
if (mode != RISING) {
Expand All @@ -122,7 +161,14 @@ void LowPower_EnableWakeUpPin(uint32_t pin, uint32_t mode)
}
#endif /* PWR_WAKEUP_PIN4 */
#ifdef PWR_WAKEUP_PIN5
if (p == SYS_WKUP5) {
if ((p == SYS_WKUP5)
#ifdef PWR_WAKEUP_PIN5_1
|| (p == SYS_WKUP5_1)
#endif
#ifdef PWR_WAKEUP_PIN5_2
|| (p == SYS_WKUP5_2)
#endif
) {
wkup_pin = PWR_WAKEUP_PIN5;
#ifdef PWR_WAKEUP_PIN5_HIGH
if (mode != RISING) {
Expand All @@ -132,7 +178,14 @@ void LowPower_EnableWakeUpPin(uint32_t pin, uint32_t mode)
}
#endif /* PWR_WAKEUP_PIN5 */
#ifdef PWR_WAKEUP_PIN6
if (p == SYS_WKUP6) {
if ((p == SYS_WKUP6)
#ifdef PWR_WAKEUP_PIN6_1
|| (p == SYS_WKUP6_1)
#endif
#ifdef PWR_WAKEUP_PIN6_2
|| (p == SYS_WKUP6_2)
#endif
) {
wkup_pin = PWR_WAKEUP_PIN6;
#ifdef PWR_WAKEUP_PIN6_HIGH
if (mode != RISING) {
Expand All @@ -142,12 +195,26 @@ void LowPower_EnableWakeUpPin(uint32_t pin, uint32_t mode)
}
#endif /* PWR_WAKEUP_PIN6 */
#ifdef PWR_WAKEUP_PIN7
if (p == SYS_WKUP7) {
if ((p == SYS_WKUP7)
#ifdef PWR_WAKEUP_PIN7_1
|| (p == SYS_WKUP7_1)
#endif
#ifdef PWR_WAKEUP_PIN7_2
|| (p == SYS_WKUP7_2)
#endif
) {
wkup_pin = PWR_WAKEUP_PIN7;
}
#endif /* PWR_WAKEUP_PIN7 */
#ifdef PWR_WAKEUP_PIN8
if (p == SYS_WKUP8) {
if ((p == SYS_WKUP8)
#ifdef PWR_WAKEUP_PIN8_1
|| (p == SYS_WKUP8_1)
#endif
#ifdef PWR_WAKEUP_PIN8_2
|| (p == SYS_WKUP8_2)
#endif
) {
wkup_pin = PWR_WAKEUP_PIN8;
}
#endif /* PWR_WAKEUP_PIN8 */
Expand Down Expand Up @@ -191,7 +258,7 @@ void LowPower_stop(serial_t *obj)
{
__disable_irq();

#if defined(UART_IT_WUF) && defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY)
#if defined(UART_WKUP_SUPPORT)
if (WakeUpUart != NULL) {
HAL_UARTEx_EnableStopMode(WakeUpUart);
}
Expand All @@ -201,6 +268,10 @@ void LowPower_stop(serial_t *obj)
/* Enable Ultra low power mode */
HAL_PWREx_EnableUltraLowPower();
#endif
#if defined(PWR_CR1_ULPMEN) || defined(PWR_CR3_ULPMEN)
/* Enable Ultra low power mode */
HAL_PWREx_EnableUltraLowPowerMode();
#endif
#if defined(PWR_CR_FWU)
/* Enable the fast wake up from Ultra low power mode */
HAL_PWREx_EnableFastWakeUp();
Expand All @@ -215,10 +286,15 @@ void LowPower_stop(serial_t *obj)
#endif

/* Enter Stop mode */
#if defined(PWR_CPUCR_RETDS_CD) || defined(PWR_CR1_LPMS_STOP2) ||\
defined(PWR_LOWPOWERMODE_STOP2)

if ((WakeUpUart == NULL) || (WakeUpUart->Instance == (USART_TypeDef *)LPUART1_BASE)) {
#if defined(UART_WKUP_SUPPORT) && (defined(PWR_CPUCR_RETDS_CD) \
|| defined(PWR_CR1_LPMS_STOP2) || defined(PWR_LOWPOWERMODE_STOP2) \
|| defined(LL_PWR_STOP2_MODE))
if ((WakeUpUart == NULL)
|| (WakeUpUart->Instance == (USART_TypeDef *)LPUART1_BASE)
#ifdef LPUART2_BASE
|| (WakeUpUart->Instance == (USART_TypeDef *)LPUART2_BASE)
#endif
) {
// STM32L4xx supports STOP2 mode which halves consumption
HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
} else
Expand All @@ -229,7 +305,7 @@ void LowPower_stop(serial_t *obj)

/* Exit Stop mode reset clocks */
SystemClock_ConfigFromStop();
#if defined(UART_IT_WUF) && defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY)
#if defined(UART_WKUP_SUPPORT)
if (WakeUpUart != NULL) {
/* In case of WakeUp from UART, reset its clock source to HSI */
uart_config_lowpower(obj);
Expand Down Expand Up @@ -277,7 +353,7 @@ void LowPower_standby()
void LowPower_shutdown()
{
__disable_irq();
#if defined(PWR_LOWPOWERMODE_SHUTDOWN) || defined(PWR_CR1_LPMS_SHUTDOWN)
#if defined(PWR_CR1_LPMS)
/* LSE must be on to use shutdown mode */
if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == SET) {
HAL_PWREx_EnterSHUTDOWNMode();
Expand All @@ -299,7 +375,8 @@ void LowPower_shutdown()
*/
void LowPower_EnableWakeUpUart(serial_t *serial, void (*FuncPtr)(void))
{
#if defined(UART_IT_WUF) && defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY)
#if defined(UART_WKUP_SUPPORT)
#ifdef IS_UART_WAKEUP_SELECTION
UART_WakeUpTypeDef WakeUpSelection;
if (serial == NULL) {
return;
Expand All @@ -318,9 +395,11 @@ void LowPower_EnableWakeUpUart(serial_t *serial, void (*FuncPtr)(void))
*/
WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_READDATA_NONEMPTY;
HAL_UARTEx_StopModeWakeUpSourceConfig(WakeUpUart, WakeUpSelection);

#endif
#if defined(UART_IT_WUF)
/* Enable the UART Wake UP from STOPx mode Interrupt */
__HAL_UART_ENABLE_IT(WakeUpUart, UART_IT_WUF);
#endif
#else
UNUSED(serial);
#endif
Expand Down