arduino / arduino/ArduinoCore-avr

Core code causes timing issues at some alternative clock speeds

Open
#151 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
1.5k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

I recently tried to run my Atmega328p with a 1.8432Mhz crystal (to use a faster baud rate). I managed to recompile the bootloader by changing AVR_FREQ and FUSES in Optiboot Makefile, boards.txt

However I found out some timing issue with Arduino's implementation of delay, millis, micros....
As defined
in Arduino.h (line 96)
`#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )`
in wiring.c (line 103)

```
unsigned long micros() {
...
return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
}
```

This integer division really limit the choices of clock!

The baud rate is correct and avr version of delay (_delay_ms) work correctly for me. I am not sure if any other libraries are affected.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the clockCyclesPerMicrosecond() definition in Arduino.h and the micros() implementation in wiring.c, then inspect the related delay and millis timing code. Reproduce the behavior at 1.8432 MHz and verify that the timing APIs work correctly without the integer-division limitation; check Optiboot Makefile and boards.txt only for clock configuration context.

Written by the indexing model from the issue text.

Assessment

Tech stack
arduino, c
Domain
embedded-iot
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.