How do I made the wakeup from deepsleep using the RTC.
This is my current clean code to get the RC oscillator from PIO20&PIO21 which drives the RTC.
Now I'm trying to figure out how to set the RTC to wakeup in deepsleep. I know that the Relax oscillator and the RTC still work on deepsleep
The code
Code: Select all
from machine import Pin,mem32,RTC
from rp2 import PIO, asm_pio
import time
IO_BANK0_BASE = 0x40014000
CLOCKS_BASE = 0x40008000
PADS_BANK0_BASE = 0x4001C000
RTC_BASE = 0x4005c000
RTC_CLKDIV_M1 = 0
RTC_CTRL = 0xc
RTC_0 = 0x1c
CLK_GPOUT0_CTRL = 0
CLK_GOUT0_DIV = 4
CLK_RTC_CTRL = 0x6c
CLK_RTC_DIV = 0x70
atomicXOR = 0x1000
atomicRW = 0
atomicOR = 0x2000
atomicNOR = 0x3000
counter = 0
#set pins for GPIN0
def invertPinOut(pin):
pin_addr = 4 + (pin *8)
mem32[IO_BANK0_BASE | pin_addr | atomicNOR]= (0x3<<8)
#set funcsel
mem32[IO_BANK0_BASE | pin_addr | atomicOR]= (0x1<<8)
pin20 = Pin(20,Pin.ALT,alt=8)
pin21 = Pin(21,Pin.ALT,alt=8)
invertPinOut(21)
#PIO to figure out the clock of the RC network
@asm_pio()
def PIO_COUNTER():
# Initialize x to be -1
mov(x,0)
label('loop')
mov(isr,x)
wait(0,pin,0)
wait(1,pin,0)
jmp(x_dec,'loop')
jmp('loop')
def getPulse():
global counter
global sm1
sm1.exec('push()')
_tcount = sm1.get()
pulseCount = counter - _tcount
counter=_tcount
return pulseCount
rtc = machine.RTC()
# this is to test if the clock of the RTC is ok
def CheckDatetime(header):
print("--- RTC.datetime ",header," ---")
for i in range(5):
getPulse()
time.sleep(1)
t=getPulse()
print("Relax Osc count =",t,end=" ")
print(rtc.datetime())
#state machine to check relaxation osc frequency
sm1 = rp2.StateMachine(1)
sm1.init(PIO_COUNTER,freq=125_000_000,in_base=pin21)
sm1.active(1)
def startRelaxationOsc():
mem32[CLOCKS_BASE|CLK_GPOUT0_CTRL|atomicNOR] = 1<<11
# divide by 1
mem32[CLOCKS_BASE|CLK_GOUT0_DIV]= 1<<8
#enable clocks
mem32[CLOCKS_BASE|CLK_GPOUT0_CTRL|atomicOR] = 1<<11
#set clksrcpin0
mem32[CLOCKS_BASE|CLK_GPOUT0_CTRL|atomicNOR] = (0xF << 5)
mem32[CLOCKS_BASE|CLK_GPOUT0_CTRL|atomicOR] = (0x1 << 5)
def setRTCToRelaxOsc(clock):
mem32[RTC_BASE | RTC_CTRL | atomicNOR] = 1
mem32[CLOCKS_BASE | CLK_RTC_CTRL | atomicNOR] = 1 << 11
mem32[CLOCKS_BASE | CLK_RTC_CTRL | atomicNOR] = 0x7 << 5
mem32[CLOCKS_BASE | CLK_RTC_CTRL | atomicOR] = 0x4 << 5
mem32[CLOCKS_BASE | CLK_RTC_DIV]= 1<<8
mem32[RTC_BASE | RTC_CLKDIV_M1] = clock-1
mem32[RTC_BASE | RTC_CTRL | atomicOR] = 1
mem32[CLOCKS_BASE | CLK_RTC_CTRL | atomicOR] = 1 << 11
getPulse()
CheckDatetime("initial")
startRelaxationOsc()
#record current time and osc counter
getPulse()
start_c = counter
start_t = time.ticks_ms()
CheckDatetime("after Relaxation oscillator")
#record the counter and the time to get an average
getPulse()
end_c=counter
end_t= time.ticks_ms()
avgClock = int((start_c - end_c) * 1000 / (end_t - start_t))
print("Relax. osc average clock = ", avgClock)
setRTCToRelaxOsc(avgClock)
CheckDatetime("after RTC input clock changed")
Code: Select all
>>> %Run -c $EDITOR_CONTENT
--- RTC.datetime initial ---
Relax Osc count = 0 (2022, 7, 14, 3, 23, 8, 50, 0)
Relax Osc count = 0 (2022, 7, 14, 3, 23, 8, 51, 0)
Relax Osc count = 0 (2022, 7, 14, 3, 23, 8, 52, 0)
Relax Osc count = 0 (2022, 7, 14, 3, 23, 8, 53, 0)
Relax Osc count = 0 (2022, 7, 14, 3, 23, 8, 55, 0)
--- RTC.datetime after Relaxation oscillator ---
Relax Osc count = 1697 (2022, 7, 14, 3, 23, 8, 56, 0)
Relax Osc count = 1698 (2022, 7, 14, 3, 23, 8, 57, 0)
Relax Osc count = 1697 (2022, 7, 14, 3, 23, 8, 58, 0)
Relax Osc count = 1699 (2022, 7, 14, 3, 23, 9, 0, 0)
Relax Osc count = 1697 (2022, 7, 14, 3, 23, 9, 1, 0)
Relax. osc average clock = 1529
--- RTC.datetime after RTC input clock changed ---
Relax Osc count = 1698 (2022, 7, 14, 3, 23, 9, 3, 0)
Relax Osc count = 1698 (2022, 7, 14, 3, 23, 9, 4, 0)
Relax Osc count = 1699 (2022, 7, 14, 3, 23, 9, 5, 0)
Relax Osc count = 1698 (2022, 7, 14, 3, 23, 9, 6, 0)
Relax Osc count = 1699 (2022, 7, 14, 3, 23, 9, 8, 0)
>>>