feat: ll rtc alarm wakeup

This commit is contained in:
IOsetting 2023-02-18 15:25:20 +08:00
parent b8043bd5d8
commit 37afb31856
11 changed files with 272 additions and 13 deletions

View File

@ -17,7 +17,7 @@ static void APP_ADCConfig(void);
int main(void) int main(void)
{ {
// Set system clock to 48MHz // Set system clock to 48MHz
BSP_HSI_PLL_48MConfig(); BSP_RCC_HSI_PLL48MConfig();
// Initialize UART on PA2:TX PA3:RX // Initialize UART on PA2:TX PA3:RX
BSP_USART_Config(115200); BSP_USART_Config(115200);
printf("PY32F0 Internal Temperature Sensor Demo\r\nClock: %ld\r\n", SystemCoreClock); printf("PY32F0 Internal Temperature Sensor Demo\r\nClock: %ld\r\n", SystemCoreClock);

View File

@ -14,7 +14,7 @@ static void APP_DMAConfig(void);
int main(void) int main(void)
{ {
BSP_HSI_PLL_48MConfig(); BSP_RCC_HSI_PLL48MConfig();
BSP_USART_Config(115200); BSP_USART_Config(115200);
printf("ADC Timer Trigger DMA Demo\r\nClock: %ld\r\n", SystemCoreClock); printf("ADC Timer Trigger DMA Demo\r\nClock: %ld\r\n", SystemCoreClock);

View File

@ -35,7 +35,7 @@ int main(void)
{ {
uint32_t index, crc; uint32_t index, crc;
// Set system clock to 48MHz // Set system clock to 48MHz
BSP_HSI_PLL_48MConfig(); BSP_RCC_HSI_PLL48MConfig();
// Initialize UART on PA2:TX PA3:RX // Initialize UART on PA2:TX PA3:RX
BSP_USART_Config(115200); BSP_USART_Config(115200);
printf("PY32F0 CRC Demo\r\nClock: %ld\r\n", SystemCoreClock); printf("PY32F0 CRC Demo\r\nClock: %ld\r\n", SystemCoreClock);

View File

@ -24,7 +24,7 @@ static void APP_FlashWriteVerify(void);
int main(void) int main(void)
{ {
BSP_HSI_PLL_48MConfig(); BSP_RCC_HSI_PLL48MConfig();
BSP_USART_Config(115200); BSP_USART_Config(115200);
printf("Flash Read&Write Demo\r\nClock: %ld\r\n", SystemCoreClock); printf("Flash Read&Write Demo\r\nClock: %ld\r\n", SystemCoreClock);

View File

@ -0,0 +1,156 @@
/**
* Demo of wakeup by RTC alarm
*/
#include "main.h"
#include "py32f0xx_bsp_clock.h"
#include "py32f0xx_bsp_printf.h"
// 0x7FFF = 32,768 - 1
#define RTC_ASYNCH_PREDIV ((uint32_t)0x7FFF)
static void APP_GPIO_Config(void);
static void APP_RTC_Init(void);
static void APP_RTC_AlarmConfig(void);
static void APP_RTC_InterruptConfig(void);
static void APP_EnterStopMode(void);
int main(void)
{
// Set HSI 8MHz as system clock source
BSP_RCC_HSI_8MConfig();
// Initialize UART on PA2:TX PA3:RX
BSP_USART_Config(115200);
printf("PY32F0 RTC Wakeup Demo\r\nClock: %ld\r\n", SystemCoreClock);
APP_GPIO_Config();
APP_RTC_Init();
APP_RTC_InterruptConfig();
while (1)
{
LL_RTC_WaitForSynchro(RTC);
// Update alarm clock
APP_RTC_AlarmConfig();
printf("Entering stop mode... ");
APP_EnterStopMode();
printf("wakeup\r\n");
}
}
static void APP_GPIO_Config(void)
{
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_5, LL_GPIO_MODE_OUTPUT);
}
static void APP_RTC_Init(void)
{
LL_RTC_InitTypeDef rtc_initstruct;
LL_RTC_TimeTypeDef rtc_time_initstruct;
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
LL_PWR_EnableBkUpAccess();
/*
* Enable LSI and set it as RTC clock source
*/
LL_RCC_LSI_Enable();
while (LL_RCC_LSI_IsReady() != 1);
if (LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSI)
{
LL_RCC_ForceBackupDomainReset();
LL_RCC_ReleaseBackupDomainReset();
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSI);
}
// Enable RTC clock and alarm
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_RTC);
LL_RCC_EnableRTC();
if (LL_RTC_DeInit(RTC) != SUCCESS)
{
printf("error\r\n");
}
rtc_initstruct.AsynchPrescaler = RTC_ASYNCH_PREDIV;
rtc_initstruct.OutPutSource = LL_RTC_CALIB_OUTPUT_NONE;
if (LL_RTC_Init(RTC, &rtc_initstruct) != SUCCESS)
{
printf("error\r\n");
}
// Time = 11:59:35
rtc_time_initstruct.Hours =11;
rtc_time_initstruct.Minutes =59;
rtc_time_initstruct.Seconds =35;
if (LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BIN, &rtc_time_initstruct) != SUCCESS)
{
printf("error\r\n");
}
}
static void APP_RTC_AlarmConfig(void)
{
uint32_t ts;
LL_RTC_AlarmTypeDef rtc_alarm_initstruct;
ts = LL_RTC_TIME_Get(RTC) + 2;
rtc_alarm_initstruct.AlarmTime.Hours = ts/3600;
rtc_alarm_initstruct.AlarmTime.Minutes = (ts % 3600) / 60;
rtc_alarm_initstruct.AlarmTime.Seconds = (ts % 3600) % 60;
if (LL_RTC_ALARM_Init(RTC, LL_RTC_FORMAT_BIN, &rtc_alarm_initstruct) != SUCCESS)
{
printf("error\r\n");
}
if (LL_RTC_ExitInitMode(RTC) != SUCCESS)
{
printf("error\r\n");
}
}
static void APP_RTC_InterruptConfig(void)
{
LL_RTC_DisableWriteProtection(RTC);
// Clear alarm interrupt flag
LL_RTC_ClearFlag_ALR(RTC);
// Enable Alarm interrupt
LL_RTC_EnableIT_ALR(RTC);
LL_RTC_EnableWriteProtection(RTC);
NVIC_SetPriority(RTC_IRQn, 0x00);
NVIC_EnableIRQ(RTC_IRQn);
LL_EXTI_EnableIT(LL_EXTI_LINE_19);
}
static void APP_EnterStopMode(void)
{
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
LL_LPM_EnableDeepSleep();
__WFI();
}
void RTC_IRQHandler(void)
{
if (LL_RTC_IsActiveFlag_ALR(RTC))
{
LL_GPIO_TogglePin(GPIOB,LL_GPIO_PIN_5);
LL_RTC_ClearFlag_ALR(RTC);
}
}
void APP_ErrorHandler(void)
{
while (1);
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
while (1);
}
#endif /* USE_FULL_ASSERT */

View File

@ -0,0 +1,27 @@
#ifndef __MAIN_H
#define __MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
#include "py32f0xx_ll_rtc.h"
#include "py32f0xx_ll_bus.h"
#include "py32f0xx_ll_cortex.h"
#include "py32f0xx_ll_dma.h"
#include "py32f0xx_ll_exti.h"
#include "py32f0xx_ll_gpio.h"
#include "py32f0xx_ll_pwr.h"
#include "py32f0xx_ll_rcc.h"
#include "py32f0xx_ll_system.h"
#include "py32f0xx_ll_tim.h"
#include "py32f0xx_ll_utils.h"
void APP_ErrorHandler(void);
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */

View File

@ -0,0 +1,42 @@
#include "main.h"
#include "py32f0xx_it.h"
extern void APP_TransferCompleteCallback(void);
/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
}
/**
* @brief This function handles Hard fault interrupt.
*/
void HardFault_Handler(void)
{
while (1)
{
}
}
/**
* @brief This function handles System service call via SWI instruction.
*/
void SVC_Handler(void)
{
}
/**
* @brief This function handles Pendable request for system service.
*/
void PendSV_Handler(void)
{
}
/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
}

View File

@ -0,0 +1,18 @@
#ifndef __PY32F0XX_IT_H
#define __PY32F0XX_IT_H
#ifdef __cplusplus
extern "C" {
#endif
void NMI_Handler(void);
void HardFault_Handler(void);
void SVC_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
#ifdef __cplusplus
}
#endif
#endif /* __PY32F0XX_IT_H */

View File

@ -14,7 +14,7 @@ static void APP_RTC_InterruptConfig(void);
int main(void) int main(void)
{ {
// Set system clock to 48MHz // Set system clock to 48MHz
BSP_HSI_PLL_48MConfig(); BSP_RCC_HSI_PLL48MConfig();
// Initialize UART on PA2:TX PA3:RX // Initialize UART on PA2:TX PA3:RX
BSP_USART_Config(115200); BSP_USART_Config(115200);
printf("PY32F0 RTC Demo\r\nClock: %ld\r\n", SystemCoreClock); printf("PY32F0 RTC Demo\r\nClock: %ld\r\n", SystemCoreClock);

View File

@ -24,10 +24,11 @@ extern "C" {
#include "py32f0xx_ll_gpio.h" #include "py32f0xx_ll_gpio.h"
#include "py32f0xx_ll_usart.h" #include "py32f0xx_ll_usart.h"
void BSP_HSI_PLL_48MConfig(void); void BSP_RCC_HSI_PLL48MConfig(void);
void BSP_HSI_24MConfig(void); void BSP_RCC_HSI_24MConfig(void);
void BSP_HSE_PLL_Config(void); void BSP_RCC_HSI_8MConfig(void);
void BSP_HSE_Config(void); void BSP_RCC_HSE_PLLConfig(void);
void BSP_RCC_HSE_Config(void);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,6 +1,6 @@
#include "py32f0xx_bsp_clock.h" #include "py32f0xx_bsp_clock.h"
void BSP_HSI_PLL_48MConfig(void) void BSP_RCC_HSI_PLL48MConfig(void)
{ {
LL_UTILS_ClkInitTypeDef UTILS_ClkInitStruct; LL_UTILS_ClkInitTypeDef UTILS_ClkInitStruct;
@ -17,7 +17,7 @@ void BSP_HSI_PLL_48MConfig(void)
LL_Init1msTick(48000000); LL_Init1msTick(48000000);
} }
void BSP_HSI_24MConfig(void) void BSP_RCC_HSI_24MConfig(void)
{ {
LL_RCC_HSI_Enable(); LL_RCC_HSI_Enable();
LL_RCC_HSI_SetCalibFreq(LL_RCC_HSICALIBRATION_24MHz); LL_RCC_HSI_SetCalibFreq(LL_RCC_HSICALIBRATION_24MHz);
@ -37,7 +37,22 @@ void BSP_HSI_24MConfig(void)
LL_Init1msTick(24000000); LL_Init1msTick(24000000);
} }
void BSP_HSE_PLL_Config(void) void BSP_RCC_HSI_8MConfig(void)
{
LL_RCC_HSI_Enable();
while(LL_RCC_HSI_IsReady() != 1);
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSISYS);
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_HSISYS);
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
LL_SetSystemCoreClock(8000000);
LL_Init1msTick(8000000);
}
void BSP_RCC_HSE_PLLConfig(void)
{ {
LL_UTILS_ClkInitTypeDef UTILS_ClkInitStruct; LL_UTILS_ClkInitTypeDef UTILS_ClkInitStruct;
@ -52,7 +67,7 @@ void BSP_HSE_PLL_Config(void)
LL_Init1msTick(HSE_VALUE * 2); LL_Init1msTick(HSE_VALUE * 2);
} }
void BSP_HSE_Config(void) void BSP_RCC_HSE_Config(void)
{ {
LL_RCC_HSE_Enable(); LL_RCC_HSE_Enable();
LL_RCC_HSE_SetFreqRegion(LL_RCC_HSE_16_32MHz); LL_RCC_HSE_SetFreqRegion(LL_RCC_HSE_16_32MHz);