From 0feec301505146206d6ae78189bacc6aedcd3491 Mon Sep 17 00:00:00 2001 From: uni369 Date: Thu, 20 Nov 2025 00:36:12 -0500 Subject: [PATCH 1/3] Ch32v303 lowpower standbyMode example. --- .../lowpower_standbyMode_test/Makefile | 12 +++ .../lowpower_standbyMode_test/funconfig.h | 13 +++ .../lowpower_standbyMode_test.c | 83 +++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 examples_v30x/lowpower_standbyMode_test/Makefile create mode 100644 examples_v30x/lowpower_standbyMode_test/funconfig.h create mode 100644 examples_v30x/lowpower_standbyMode_test/lowpower_standbyMode_test.c diff --git a/examples_v30x/lowpower_standbyMode_test/Makefile b/examples_v30x/lowpower_standbyMode_test/Makefile new file mode 100644 index 00000000..d6460c4f --- /dev/null +++ b/examples_v30x/lowpower_standbyMode_test/Makefile @@ -0,0 +1,12 @@ +all : flash + +TARGET:=lowpower_standbyMode_test +TARGET_MCU:=CH32V303 +TARGET_MCU_PACKAGE:=CH32V303 + +include ../../ch32fun/ch32fun.mk + +flash : cv_flash +clean : cv_clean + + diff --git a/examples_v30x/lowpower_standbyMode_test/funconfig.h b/examples_v30x/lowpower_standbyMode_test/funconfig.h new file mode 100644 index 00000000..18486fdd --- /dev/null +++ b/examples_v30x/lowpower_standbyMode_test/funconfig.h @@ -0,0 +1,13 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define FUNCONF_SYSTICK_USE_HCLK 1 +#define FUNCONF_USE_HSI 1 +#define FUNCONF_USE_PLL 0 +#define FUNCONF_PLL_MULTIPLIER 2 + +#define FUNCONF_USE_DEBUGPRINTF 1 +#define FUNCONF_SYSTEM_CORE_CLOCK 8 * 1000 * 1000 + +#endif + diff --git a/examples_v30x/lowpower_standbyMode_test/lowpower_standbyMode_test.c b/examples_v30x/lowpower_standbyMode_test/lowpower_standbyMode_test.c new file mode 100644 index 00000000..402f78df --- /dev/null +++ b/examples_v30x/lowpower_standbyMode_test/lowpower_standbyMode_test.c @@ -0,0 +1,83 @@ +// Simple example showing how to use the low power standby mode +// The current consumption while sleep is around 2uA + +// Setup: +// PB10 - LED indicator + +// Program behavior: +// 1. 5 second delay to allow reprogramming +// 2. Set all GPIOs to input pull up +// 3. Enter standby +// 4. Wake up using the IWDG timer (after 8 seconds), the indicator Led will blink 5 times upon wakeup +// 5. Go back to sleep and repeat + + +#include "ch32fun.h" +#include + +#define LED_PIN PB10 + +void blink_led(int time) { + for (int i = 0; i < time; i++) { + funDigitalWrite(LED_PIN, 1); + Delay_Ms( 50 ); + funDigitalWrite(LED_PIN, 0); + Delay_Ms( 50 ); + } +} + +void IWDG_init(u16 prescaler, u16 reload) { + // 1. Enable access to IWDG registers + IWDG->CTLR = IWDG_WriteAccess_Enable; + + // 2. Set prescaler and reload value + IWDG->PSCR = prescaler; + IWDG->RLDR = reload; + + // 3. Enable IWDG (cannot be disabled after this!) + IWDG->CTLR = CTLR_KEY_Reload; + IWDG->CTLR = CTLR_KEY_Enable; + + // 4. wait for LSI ready + while(!(RCC->RSTSCKR & RCC_LSIRDY)); +} + +int main() { + SystemInit(); + Delay_Ms(100); + funGpioInitA(); + funGpioInitB(); + + printf("\n~ Low Power Stop Mode Test ~\n"); + //! WARNING: need 5 seconds to allow reprogramming on power up + Delay_Ms(5000); + + // Set all GPIOs to input pull up to reduce power consumption + GPIOA->CFGLR = 0x88888888; + GPIOB->CFGLR = 0x88888888; + GPIOC->CFGLR = 0x88888888; + GPIOD->CFGLR = 0x88888888; + GPIOE->CFGLR = 0x88888888; + + GPIOA->CFGHR = 0x88888888; + GPIOB->CFGHR = 0x88888888; + GPIOC->CFGHR = 0x88888888; + GPIOD->CFGHR = 0x88888888; + GPIOE->CFGHR = 0x88888888; + + funPinMode(LED_PIN, GPIO_CFGLR_OUT_10Mhz_PP); + blink_led(5); + + // Enable PWR clock + RCC->APB1PCENR |= RCC_APB1Periph_PWR; + + // Enter standby mode + PWR->CTLR |= PWR_CTLR_PDDS; + NVIC->SCTLR |= (1 << 2); + + // reload value for 8 second timeout = (8 * 40KHz / 128) - 1 = 2499 + IWDG_init(IWDG_Prescaler_128, 2499); + __WFI(); + + while(1); +} \ No newline at end of file From c8294a980b269fd718b49f652a9f668155ce3a6c Mon Sep 17 00:00:00 2001 From: uni369 Date: Thu, 20 Nov 2025 00:44:59 -0500 Subject: [PATCH 2/3] update. --- .../lowpower_standbyMode_test.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/examples_v30x/lowpower_standbyMode_test/lowpower_standbyMode_test.c b/examples_v30x/lowpower_standbyMode_test/lowpower_standbyMode_test.c index 402f78df..da5a26b5 100644 --- a/examples_v30x/lowpower_standbyMode_test/lowpower_standbyMode_test.c +++ b/examples_v30x/lowpower_standbyMode_test/lowpower_standbyMode_test.c @@ -8,10 +8,9 @@ // 1. 5 second delay to allow reprogramming // 2. Set all GPIOs to input pull up // 3. Enter standby -// 4. Wake up using the IWDG timer (after 8 seconds), the indicator Led will blink 5 times upon wakeup +// 4. Wake up using the IWDG timer (after 5 seconds), the indicator Led will blink upon wakeup // 5. Go back to sleep and repeat - #include "ch32fun.h" #include @@ -49,8 +48,14 @@ int main() { funGpioInitB(); printf("\n~ Low Power Stop Mode Test ~\n"); + funPinMode(LED_PIN, GPIO_CFGLR_OUT_10Mhz_PP); + blink_led(1); + //! WARNING: need 5 seconds to allow reprogramming on power up - Delay_Ms(5000); + //! DONT set GPIOA->CFGHR to pullup before the 5 seconds delay because they are used for SWCLK and SWDIO + // if you want to get around this 5s delay you can configure a GPIO input and check it to determine + // if it's in bypass mode so you can reprogram it + Delay_Ms(4000); // Set all GPIOs to input pull up to reduce power consumption GPIOA->CFGLR = 0x88888888; @@ -65,9 +70,6 @@ int main() { GPIOD->CFGHR = 0x88888888; GPIOE->CFGHR = 0x88888888; - funPinMode(LED_PIN, GPIO_CFGLR_OUT_10Mhz_PP); - blink_led(5); - // Enable PWR clock RCC->APB1PCENR |= RCC_APB1Periph_PWR; @@ -75,8 +77,8 @@ int main() { PWR->CTLR |= PWR_CTLR_PDDS; NVIC->SCTLR |= (1 << 2); - // reload value for 8 second timeout = (8 * 40KHz / 128) - 1 = 2499 - IWDG_init(IWDG_Prescaler_128, 2499); + // reload value for 5 second timeout = (5 * 40KHz / 128) - 1 = 1561 + IWDG_init(IWDG_Prescaler_128, 1561); __WFI(); while(1); From 334a6b6efd028af99bdd8d57e4605bb3e5e4c478 Mon Sep 17 00:00:00 2001 From: uni369 Date: Thu, 20 Nov 2025 00:47:32 -0500 Subject: [PATCH 3/3] cleanup. --- .../lowpower_standbyMode_test/lowpower_standbyMode_test.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples_v30x/lowpower_standbyMode_test/lowpower_standbyMode_test.c b/examples_v30x/lowpower_standbyMode_test/lowpower_standbyMode_test.c index da5a26b5..cf7b20cb 100644 --- a/examples_v30x/lowpower_standbyMode_test/lowpower_standbyMode_test.c +++ b/examples_v30x/lowpower_standbyMode_test/lowpower_standbyMode_test.c @@ -44,8 +44,7 @@ void IWDG_init(u16 prescaler, u16 reload) { int main() { SystemInit(); Delay_Ms(100); - funGpioInitA(); - funGpioInitB(); + funGpioInitAll(); printf("\n~ Low Power Stop Mode Test ~\n"); funPinMode(LED_PIN, GPIO_CFGLR_OUT_10Mhz_PP);