I'm prototyping a mains frequency counter and I decided to use DS3231 RTC for timekeeping. It has a 32768 Hz square wave output, which means I just have to count the pulses to keep time. Sounds simple, but I think I'm hitting the limits of uPy here, which suprised me. I tried the following demo code:
Code: Select all
import time
from machine import Pin, freq
freq(160000000)
clock_pin = Pin(4, Pin.IN, pull=Pin.PULL_UP)
clock_ticks = 0
def clock_cb(p):
global clock_ticks
clock_ticks = clock_ticks + 1
clock_pin.irq(trigger=Pin.IRQ_FALLING, handler=clock_cb)
while True:
clock_ticks = 0
time.sleep(1)
print(clock_ticks)
I tried decorating it with @micropython.native, which did speed it up to about 27200 pulses detected. Is there a way of further optimizing the interrupt callback function?