RTC Bug?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

RTC Bug?

Post by JimTal001 » Wed Sep 23, 2015 6:14 pm

I may have found a bug related to the RTC and battery backup.

Background
I have a 3V coin battery connected to VBAT and Gnd. The RTC battery has been connected for around a week and the date/time was set last week after adding the RTC battery. I've had the board powered off for 5 days now. Today I power on the pyboard and checked the time in the terminal and it was close to where it should be, maybe 20-30 seconds off. The program I have been running uses the sleep functionality of the pyboard (rtc.wakeup(...)) and uses interrupts. The program was working fine even after adding the new RTC battery backup.

Today I ran my program and it failed to run properly. I finally identified that once rtc.stop() was issued it never wakes again. I wrote a simple program to verify this:

Code: Select all

import pyb
import micropython
from pyb import Pin


YEL_LED = pyb.Pin('X4', pyb.Pin.OUT_PP, pull=pyb.Pin.PULL_UP)
rtc = pyb.RTC()

def toggle_YEL_LED(dt = 200):
	YEL_LED.high()
	pyb.delay(dt)
	YEL_LED.low()
	pyb.delay(dt)

def mainloop():
	while True:
		print("RTC Wake Event")	
		toggle_YEL_LED()
		pyb.stop() # go to sleep until next wakeup

rtc.wakeup(15000) 	
mainloop()
Running the code above, the message "RTC Wake Event" would print to the terminal once and not again. Same with the LED flash.
Therefore, I powered off the pyboard and disconnected the power and Gnd from the RTC backup battery. Then I powered on the pyboard and ran the above program and the sleep/wake cycle started working again! I then re-connected VBAT, set the clock and ran the above program and it worked as expected (no issue). Therefore, is there a bug which caused sleep/wake to stop working (related to the battery) and perhaps since the pyboard was not powered for days? For me this is a serious issue.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: RTC Bug?

Post by pythoncoder » Thu Sep 24, 2015 7:35 am

The following works here if the Pyboard is powered normally (print statements are questionable when the Pyboard spends time stopped):

Code: Select all

import pyb
import micropython
from pyb import Pin

YEL_LED = pyb.Pin('X4', pyb.Pin.OUT_PP, pull=pyb.Pin.PULL_UP)
rtc = pyb.RTC()

def toggle_YEL_LED(dt = 200):
   YEL_LED.high()
   pyb.delay(dt)
   YEL_LED.low()
   pyb.delay(dt)

def mainloop():
   while True:
#      print("RTC Wake Event")   
      toggle_YEL_LED()
      pyb.stop() # go to sleep until next wakeup
rtc.wakeup(2000)    
mainloop()
Despite having re-read your post I haven't followed your power up and down sequence. At risk of stating the obvious, when you power the Pyboard using vbat only the RTC continues to run but all program state is lost. If you subsequently additionally apply power to Vin the Pyboard will reboot and run your program from the start (assuming it's called from main.py).

Apologies if I've misunderstood your issue here.
Peter Hinch
Index to my micropython libraries.

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: RTC Bug?

Post by JimTal001 » Thu Sep 24, 2015 1:27 pm

Thanks for trying to understand my post but you did not get my point.

short and simple:
The RTC battery somehow hung the RTC wake event until I disconnected my RTC battery and I am 100% certain of this.

Power cycles of the pyboard would not clear the issue until I removed power from the RTC battery.

I've not been able to reproduce the issue yet but if it happened once it can happen again (hopefully not in the field collecting data).

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: RTC Bug?

Post by pythoncoder » Fri Sep 25, 2015 6:32 am

It does sound like a bug but until you can produce a simple repeatable test case it's going to be hard for the maintainers to address. Is it possible that the fault occurs when a wake event takes place but the Pyboard is unpowered and hence unable to respond? While I don't claim a chip-level understanding of the RTC the RM0090 manual P784 states:
The WUTF flag must then be cleared by software.
The thought occurred to me that the wakeup mechanism might be coded in a way which assumes that the Pyboard is capable of responding to the event. This is conjecture and needs testing but if proven would qualify as a bug (in my view).
Peter Hinch
Index to my micropython libraries.

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: RTC Bug?

Post by JimTal001 » Fri Sep 25, 2015 1:36 pm

I can say for sure that when the pyboard was sleeping I cut power to the pyboard.
Another thing is that when I ran the program all the interrupts were working correctly, like when I press a button it's interrupt would read battery level, flash an LED and print to screen. Only the RTC interrupt/callback was not being raised (where I collect data, flash LED and write to file). The sample program above also validated the issue. After power cycling VBAT the issues went away. So there is some FLAG in the RTC (as you suggest) which was not cleared/reset on pyboard. I've not seen the issue again but Murphy's law tell me I will.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: RTC Bug?

Post by pythoncoder » Fri Sep 25, 2015 4:19 pm

I can say for sure that when the pyboard was sleeping I cut power to the pyboard.
My question is whether this is a valid thing to do if you're expecting the RTC subsequently to wake the Pyboard. If an RTC interrupt occurs when the Pyboard is powered down then any code which might clear down the flag won't be executed.

This is merely a guess at what might be going on, but it's readily testable.
Peter Hinch
Index to my micropython libraries.

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: RTC Bug?

Post by JimTal001 » Fri Sep 25, 2015 11:00 pm

The issue happened again today but in a slightly different way. When I ran my program today which employs the RTC sleep (RTC sleep/wake cycle set for a 30 second) it started waking at 5 second intervals (rather than 30 sec). Yesterday, I was running a test with a 5 second sleep/wake cycle. The pyboard was powered off all night but the RTC battery was connected. To fix the issue I simply disconnected VBAT and power cycled the pyboard.
My question is whether this is a valid thing to do if you're expecting the RTC subsequently to wake the Pyboard.
Maybe its not the correct thing to do but for a data logger in the field running on battery power the same thing will happen if the battery dies and the RTC battery is still running.
If an RTC interrupt occurs when the Pyboard is powered down then any code which might clear down the flag won't be executed.
I don't believe it is an RTC interrup occuring when the Pyboard is powered down because I cut power to the pyboard immediately after the RTC interrupt (flash of an LED at interrupt).

Is there a way to clear the RTC flag at the start of a program ?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: RTC Bug?

Post by pythoncoder » Sat Sep 26, 2015 7:18 am

Maybe its not the correct thing to do but for a data logger in the field running on battery power the same thing will happen if the battery dies and the RTC battery is still running.
I'd have thought that the way to manage data logging equipment would be to calculate battery usage and perform field replacements before they run out: after all, if they do run out the equipment has failed. In this case a field visit is required so is a brief interruption to the RTC battery a real problem? With careful design the Pyboard can achieve long periods of running on battery power and it may be that there's no need to have an RTC battery at all.

I've written some thoughts, calculations and measurements on the subject of long term battery powered operation of the Pyboard here https://github.com/peterhinch/micropyth ... opower.git which you might find relevant.

As to why your program wakes at 5S intervals instead of 30S, you'll need to post example code before anyone can sensibly comment.

Regarding the WUTF flag, according to the chip documentation this is bit 10 of the RTC ISR. You could try issuing

Code: Select all

stm.mem32[stm.RTC + stm.RTC_ISR] &= 0xfbff 
Once again this is untested, just an idea based on reading the datasheet.
Peter Hinch
Index to my micropython libraries.

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: RTC Bug?

Post by JimTal001 » Sat Sep 26, 2015 1:04 pm

As to why your program wakes at 5S intervals instead of 30S, you'll need to post example code before anyone can sensibly comment.
I don't believe it has anything to do with my code. Even a simple example like the case below would wake at 5 second intervals and print to screen. Once I disconnected VBAT it worked correctly at 30 sec intervals.

Code: Select all

import pyb
import micropython
micropython.alloc_emergency_exception_buf(100)

rtc = pyb.RTC()
sleepInterval = 30000 # in ms
interrupt_RTC = 0 # RTC Wake Event

def callback_RTC(line):
	global interrupt_RTC
	interrupt_RTC = 1

def mainloop():
	global interrupt_RTC
	while True:
		if(interrupt_RTC == 1):
			interrupt_RTC = 0
			print("RTC Interrupt Event")	
		
		pyb.stop() # go to sleep until next wakeup

rtc.wakeup(sleepInterval,callback_RTC) # every sleepInterval wake	
mainloop()
Last edited by JimTal001 on Sat Sep 26, 2015 8:27 pm, edited 3 times in total.

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: RTC Bug?

Post by JimTal001 » Sat Sep 26, 2015 7:48 pm

Great Information Peter. I will study it carefully.

Post Reply