Tools millis()/micro Timer settings, Tools attachInterrupt Mode settings, and TCB use result in broken millis()/delay()
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- arduino, c
- Domain
- embedded-iot
Research direction
Start with wiring_micros_ISR.h in the megaTinyCore megaavr cores folder, which sets up the millis() interrupt for the default, TCB0 and RTC timer sources. Reproduce on an ATtiny3224 with the minimal blink sketch, changing TCB1.CCMPL/CCMPH and the attachInterrupt mode. Done means delay() and millis() stay accurate under each combination, and the root cause is identified.
Written by the indexing model from the issue text.
Description
*Edit2: Target is ATtiny2 ATtiny3224. PA3 used instead of LED_BUILTIN as that pin is open in my circuit.
TCB1 is the default timer for millis().
Minimum Blink example:
void setup() {
pinMode(PIN_PA3, OUTPUT); //Using PA3 instead of LED_BUILTIN(PA7)
TCB1.CCMPL = 0x00; //modify frequency of flash here
TCB1.CCMPH = 0x01;
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(PIN_PA3, HIGH); // change state of the LED by setting the pin to the HIGH voltage level
delay(1000); // wait for a second
digitalWrite(PIN_PA3, LOW); // change state of the LED by setting the pin to the LOW voltage level
delay(1000); // wait for a second
}
attachInterrupt Mode default and millis() source to TCB0. This works:
attachInterrupt Mode PortA and millis() source to TCB0, This doesn't:
attachInterrupt Mode PortA and millis() source to RTC, This does:
attachInterrupt Mode default and millis() source to RTC, This doesn't:
When it works, it flashes at the expected rate. When it doesn't, it flashes at various rates determined by the setting in TCB1.CCMPL/H.
// 2.45 Hz
TCB1.CCMPL = 0x01;
TCB1.CCMPH = 0x00;
// 0.613 Hz
TCB1.CCMPL = 0x00;
TCB1.CCMPH = 0x20;
//10.4 Hz
TCB1.CCMPL = 0xFF;
TCB1.CCMPH = 0x7F;
//5.68 Hz
TCB1.CCMPL = 0xFF;
TCB1.CCMPH = 0xFF;
//20.0 Hz
TCB1.CCMPL = 0x00;
TCB1.CCMPH = 0x01;
I'm not sure what's going on. If I dig further into it, I'm going to begin by looking at how the millis() interrupt is setup when the sources are default/TCB0/RTC. I'd start with wiring_micros_ISR.h
I'm able to work around for my needs without finding the actual source of the behavior. In my case I'm using the RTC as the millis() source, With interrupts set to PORTA, I'm able to use both TCB0 and TCB1 without millis() being broken.
I can probably figure out the root cause, but haven't been able to find it after some digging. If anyone else needs it, I might give it another try. Likely that someone else can quickly figure it out, I don't know much about interrupts and am only starting to learn.
*Edit: corrected after accidental early post.
- Dominant language
- C
- Stars
- 690
- Forks
- 162
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from SpenceKonde/megaTinyCore
-
ATtiny xy2: Missing #else path may lead to inconsistencies in core digital_pin_to_timer[] arrayOpen
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SpenceKonde/megaTinyCore#1294 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
SpenceKonde/megaTinyCore#1281 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
SpenceKonde/megaTinyCore#1271 ·
-
ATtiny 2 series RTC prescaler value incorrect, millis() and delay() run 32X faster than nominal.Possibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 1/5 Under an hour Newbie friendliness 35/100
SpenceKonde/megaTinyCore#1288 · 3 comments · 1 reaction ·
-
Difficulty 1/5 Under an hour Newbie friendliness 10/100
SpenceKonde/megaTinyCore#1287 · 4 comments ·
All issues in SpenceKonde/megaTinyCore
Similar issues
-
agent:WSL code-review linux pri:low refactoring tooling
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
DOC BUG Missing
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SDL-Hercules-390/hyperion#911 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
metacall/core#964 · 1 comment ·
Maintainers usually reply within 1 day
-
build: man page directory doubledPossibly taken @zeusgoose claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
esp-mosaico/esp-mosaico-bsp#40 ·
Maintainers usually reply within 1 day