openwch / arduino_core_ch32

Core library for CH32duino
266 stars 51 forks source link

STOP mode for CH32V003? #106

Open radwouters opened 5 months ago

radwouters commented 5 months ago

In the datasheet of the CH32V003, section 2.3, it says that sleep is supported. When looking at ch32v00x_pwr.h, however, only the standby is implemented. Is there a reason for this? Would it be possible to use the same implementation as other chips and create a PR for this?

For my application, I would like to use sleep instead of standby, since I require a precise clock quickly after waking up. Also piggybacking a bit on #102

maxgerhardt commented 4 months ago

See https://www.wch-ic.com/downloads/CH32V003RM_PDF.html and https://www.wch-ic.com/downloads/CH32V003RM_PDF.html

grafik grafik

So, "sleep" mode in the RISC-V core is just entered with the wait for interrupt (WFI) or wait for event (WFE) instruction, these are already exposed via other file, e.g. core_riscv.h,

// set PDDS = 0
PWR->CTLR &= ~(1u << 1u);
// set SLEEPDEEP=0 to not enter into standby mode, only sleep mode
PFIC->SCTLR &= ~(1u << 2u);
// goodnight
__WFI();
radwouters commented 4 months ago

I think I understand what is happening here, but I am not sure how you got to it. Is there a place where I can see this translation from register to c code? For example how PWR->CTLR is PDDS?

It would also be possible to make a PR for this, right? So add a PWR_EnterSLEEPMode-method below here?

Please let me know if that would help the project :) Otherwise I'll just stay away from it!

maxint-rd commented 3 months ago

FYI: I'm currently working on some easy functions in a fork of Adafruit's SleepyDog. I have something working for the CH32V003 (using the functions you mentioned) and intend to publish my work-in-progress in my GitHub account some time soon. To be continued...

maxint-rd commented 1 month ago

Hello @radwouters , I published my fork of Adafruit_SleepyDog that supports the CH32V003. It includes simple examples for using the watchdog and for using sleep. For longer sleeps I implemented repeated naps. Although not highly accurate, the millis counter is updated to reflect the time spent sleeping. Would be nice if you coukd give it a test and let me know your findings.