diff --git a/Examples/LL/PWR/PVD/main.c b/Examples/LL/PWR/PVD/main.c new file mode 100644 index 0000000..dff92b7 --- /dev/null +++ b/Examples/LL/PWR/PVD/main.c @@ -0,0 +1,90 @@ +/** + * Power Voltage Detector(PVD) Demo +*/ +#include "main.h" +#include "py32f0xx_bsp_clock.h" +#include "py32f0xx_bsp_printf.h" + +static void APP_GPIO_Config(void); +static void APP_ExtiConfig(void); +static void APP_PVD_Config(void); + +int main(void) +{ + // Set HSI 24MHz as system clock source + BSP_RCC_HSI_24MConfig(); + // Initialize UART on PA2:TX PA3:RX + BSP_USART_Config(115200); + printf("PY32F0 PVD Demo\r\nClock: %ld\r\n", SystemCoreClock); + // Set PB5 for LED output + APP_GPIO_Config(); + // Set PB7 as analog input + APP_ExtiConfig(); + APP_PVD_Config(); + // Enable PVD + LL_PWR_EnablePVD(); + + while (1); +} + +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_ExtiConfig(void) +{ + LL_GPIO_InitTypeDef GPIO_InitStruct; + LL_EXTI_InitTypeDef EXTI_InitStruct; + + LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB); + + // PB7 + GPIO_InitStruct.Pin = LL_GPIO_PIN_7; + GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = LL_GPIO_PULL_UP; + LL_GPIO_Init(GPIOB, &GPIO_InitStruct); + // Line 16 is for PVD output + EXTI_InitStruct.Line = LL_EXTI_LINE_16; + EXTI_InitStruct.LineCommand = ENABLE; + EXTI_InitStruct.Mode = LL_EXTI_MODE_IT; + EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_RISING_FALLING; + LL_EXTI_Init(&EXTI_InitStruct); +} + +static void APP_PVD_Config(void) +{ + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR); + LL_PWR_SetPVDLevel(LL_PWR_PVDLEVEL_0); + LL_PWR_DisablePVDFilter(); + LL_PWR_SetPVDFilter(LL_PWR_PVD_FILTER_1CLOCK); + // PB7 + LL_PWR_SetPVDSource(LL_PWR_PVD_SOURCE_PB7); + NVIC_SetPriority(PVD_IRQn, 1); + NVIC_EnableIRQ(PVD_IRQn); +} + +void PVD_IRQHandler(void) +{ + if (LL_PWR_IsActiveFlag_PVDO()) + { + LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_5); + } + else + { + LL_GPIO_ResetOutputPin(GPIOB, LL_GPIO_PIN_5); + } +} + +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 */ diff --git a/Examples/LL/PWR/PVD/main.h b/Examples/LL/PWR/PVD/main.h new file mode 100644 index 0000000..5a96e01 --- /dev/null +++ b/Examples/LL/PWR/PVD/main.h @@ -0,0 +1,28 @@ +#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_lptim.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 */ diff --git a/Examples/LL/PWR/PVD/py32f0xx_it.c b/Examples/LL/PWR/PVD/py32f0xx_it.c new file mode 100644 index 0000000..458707c --- /dev/null +++ b/Examples/LL/PWR/PVD/py32f0xx_it.c @@ -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) +{ +} diff --git a/Examples/LL/PWR/PVD/py32f0xx_it.h b/Examples/LL/PWR/PVD/py32f0xx_it.h new file mode 100644 index 0000000..f1403ae --- /dev/null +++ b/Examples/LL/PWR/PVD/py32f0xx_it.h @@ -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 */