CH32V003P4 - Sleep and Wakeup

Thats ok take your time :slight_smile:

I have a code for sleep for X seconds and wakeup for check if signal is true ..... it works pefect

void enterSleepMode(uint32_t sleepDurationSec) {
  RCC->APB1PCENR |= RCC_APB1Periph_PWR;
  RCC->RSTSCKR |= RCC_LSION;
  while ((RCC->RSTSCKR & RCC_LSIRDY) == 0) {} 
  EXTI->EVENR |= EXTI_Line9; 
  EXTI->FTENR |= EXTI_Line9;

  if (sleepDurationSec == 1) { 
    PWR->AWUPSC = PWR_AWU_Prescaler_4096; 
  } else {
    PWR->AWUPSC = PWR_AWU_Prescaler_61440; 
  }

  PWR->AWUWR &= ~0x3F;  
  
  if (sleepDurationSec == 1) { 
    PWR->AWUWR |= 32; 
  } else {
    PWR->AWUWR |= 8; 
  }

  PWR->AWUCSR |= (1 << 1); 
  PWR->CTLR |= PWR_CTLR_PDDS; 
  PFIC->SCTLR |= (1 << 2); 
  __WFE();
}

void loop() {

      while (var < 40)

      {

       digitalWrite(D14, HIGH);
       delay(25);
       var++;

         if(digitalRead(D2) == LOW) 
         { 
         handleIRSignal(); 
         }

      }
         var = 0;
         digitalWrite(D14, LOW);
         enterSleepMode(4);
         enterSleepMode(4);   

    // Return to the beginning and repeat
}

Now i'm looking for a code sleep until to PIN "X" goes low ( doenst matter) case pin X is low wakeup and do something

I just uploaded the fix. I've tested it with v003 using updated core and the included Adafruit sleep example. Watchdog.sleep() without parameter will sleep for about 30 sec. You can specify a sleep value in ms. The code will do repeated naps to match the parameter and does allow longer sleeps. To reprogram it's easiest to first power-down using the LinkE.

Thank you showing for your code. Maybe I can use bits to improve this library. I haven't done anything with pin interrupts yet. I'll do some more experiments once I find some more spare time. (I have too many other things to do at the moment...)

giving you a feedback ... now your code run :slight_smile:

Please add a

void setup() {
  delay(5000); // IMPORTANT: if you miss that, you'll have a hard time reprogramming the CV32V003

On your code because on the sleepmode it's to hard programing again Uc

I Just make the new file WatchdogCH32 download and put on the folder.

Tks

Great! Thank you for testing!

because on the sleepmode it's to hard programing again

I know what you mean. The programming pin doesn't work during sleep. The sleep example is the unchanged standard Adafruit example. Because you still need to power off the uC before sleep works, I found it easiest to just use LinkE to power off before programming. During programming it will be switched on automatically and then you need to power off/on again to test the new program.