tomjorquera / pico-micropython-lowpower-workaround

Workaround for low power support in micropython for the Raspberry pico
GNU Lesser General Public License v3.0
58 stars 12 forks source link

DS3231 #2

Closed Herbert0209 closed 1 year ago

Herbert0209 commented 3 years ago

I have used the code on a pico with an AZdelivery DS3231 module. I manage to set an alarm and wake up the Pico from dormant mode, but that works only two times. After that the Interrupt is not triggered. No idea why, can anybody help

I needed to change the code a bit, since the DS3231 pulls the alarm pin to low:

import DS3231 i2c = I2C(1, sda = Pin(6), scl=Pin(7)) ds = DS3231.DS3231(i2c)

DORMANT_PIN = 15

def h(irq): sleep(0.5) print ("Button event") blink_n_fast(2) ds.ClearALARM

btn = Pin(DORMANT_PIN, Pin.IN, Pin.PULL_UP) btn.irq(trigger = Pin.IRQ_FALLING,handler=h)

def blink_nfast(n): for in range(n): busy_led.value(1) sleep(0.2) busy_led.value(0) sleep(0.2)

lowpower.dormant_until_pin(DORMANT_PIN, False, False)

Update: The Pico only hangs if a host (RPi in my case) is connected through USB, If the Pico is stand alone the code works!!!

tomjorquera commented 3 years ago

Hey @Herbert0209,

Thanks for the report. I don't have a DS3231 module on hand so I cannot test the issue myself, but I will try to look into it still. If you manage to get the bottom of the problem don't hesitate to share back :)

Regarding your example, I don't see any ds.ALARM(...). Is it missing or do I misunderstand how things work with this module?

Herbert0209 commented 3 years ago

@tomjorquera This is the code that I am using

` from machine import Pin, I2C from time import sleep

import lowpower import DS3231 i2c = I2C(1, sda = Pin(6), scl=Pin(7)) print("I2C Address : "+hex(i2c.scan()[0]).upper()) # Display device address print("I2C Configuration: "+str(i2c)) # Display I2C config ds = DS3231.DS3231(i2c)

DORMANT_PIN = 15

def h(irq): sleep(0.5) print ("Button event") blink_n_fast(2) ds.ClearALARM

btn = Pin(DORMANT_PIN, Pin.IN, Pin.PULL_UP) btn.irq(trigger = Pin.IRQ_FALLING,handler=h)

def blink_nfast(n): for in range(n): busy_led.value(1) sleep(0.2) busy_led.value(0) sleep(0.2)

def blink_ntimes(n): for in range(n): busy_led.value(1) sleep(0.5) busy_led.value(0) sleep(0.5)

while True: t = ds.DateTime() print (t) minute = t[5] + 1 hour = t[4] day = t[2] print (day, hour, minute) ds.ClearALARM() ds.ALARM(day, hour, minute, DS3231.PER_HOUR) LED_BUSY = 25 busy_led = Pin(LED_BUSY, Pin.OUT) blink_ntimes(1) print("before active loop") for in range(10**5): pass print("after active loop")

print("before dormant")
sleep(1)
lowpower.dormant_until_pin(DORMANT_PIN, False, False)

print("after dormant")

`

kenb2054 commented 3 years ago

Hi @tomjorquera, @Herbert0209, I have been experimenting with lowpower.py using the example file that tom provided. i.e. lowpower_example.py I am using a rp2040 Pico and Thonny on windows. I found that, even with the example file, if I ran through the full cycle twice, the Pico stopped responding after the second wake from dormant and I had to depower and repower it. I could consistently repeat this problem. What I also found was that if I disabled all the print statements in the lowpower_example.py file, the Pico would awake from lightsleep and dormant modes for as many times as I cared to keep repeating the cycle of interrupts. Alternatively, if I ran the lowpower_example.py program as main.py and disconnected the USB connection, it would also awaken reliably. I don't know what this means but assume it is related to the use of the Serial/USB connection. I hope this info helps towards finding a resolution.

SchroedersKater commented 2 years ago

Hi all,

I am using an RP2040 Pico and Thonny on MacOS, version 1.17 of MicroPython.

I confirm kenb2054's experience somehow. As soon as I use a print statement, the whole stuff hangs. Without any print statement it seems to work reliable. I'll implement it into my project and let you know how it runs after a few more days.

Thank you very much for this code anyway!

luki34567780 commented 2 years ago

Hi, I found this article, might be related: https://ghubcoder.github.io/posts/awaking-the-pico/ Short version: some clocks get stopped when entering dormant sleep and not get restarted when leaving dormant sleep. Would explain why it hangs as soon as a print statement is used

tomjorquera commented 2 years ago

Hey all,

I know it's been some time, but another user suggested some changes that may (or may not) solve the issue (and involve stopping and restarting the pico clocks as @luki34567780 mentioned).

I could not reproduce the issue so I cannot test it myself, but if some of you are up to it the current modifications live a the clear-clocks branch https://github.com/tomjorquera/pico-micropython-lowpower-workaround/tree/clear-clocks

If by chance you have time to test whether this solves the issue you had, it will be appreciated!

ABelliqueux commented 1 year ago

Hi @tomjorquera,

I've tried the clear-clocks branch and I was able to cycle through dormant cycles and prints without problems.

Yoda-Soda commented 1 year ago

Happy to say the clear-clocks sorted out some hanging and slow downs for me.

tomjorquera commented 1 year ago

Thanks all, closing the issue now then!