Fix SPR2 register compilation error for 8MHz ATmega32U4 boards #6

Merged
Lay31415 merged 1 commits from Lay31415-patch-1 into main 2025-06-01 02:22:52 +03:00
Lay31415 commented 2025-05-31 19:08:43 +03:00 (Migrated from github.com)

Problem

The code currently uses SPR2 register bit which does not exist in ATmega32U4, causing compilation errors on some development environments:

error: 'SPR2' was not declared in this scope

This issue primarily affects 3.3V 8MHz ATmega32U4 boards (such as some SparkFun Pro Micro 3.3V variants) when compiled with strict register checking.

Background

  • Most ATmega32U4 boards (Leonardo, Micro, standard Pro Micro) operate at 5V 16MHz and work fine with the current code
  • The issue specifically occurs with 3.3V 8MHz variants where the SPR2 register reference causes compilation failures
  • The original code attempts to set SPI clock to 250kHz using non-existent SPR2 bit

Solution

Replace the non-existent SPR2 with the correct combination of SPR1 and SPI2X bits to achieve the same Fosc/32 clock division:

  • SPR1=1, SPR0=0 → Fosc/64
  • SPI2X=1 → Double speed mode → Fosc/32
  • Result: 8MHz/32 = 250kHz (maintains original target frequency)
## Problem The code currently uses `SPR2` register bit which does not exist in ATmega32U4, causing compilation errors on some development environments: ``` error: 'SPR2' was not declared in this scope ``` This issue primarily affects **3.3V 8MHz ATmega32U4 boards** (such as some SparkFun Pro Micro 3.3V variants) when compiled with strict register checking. ## Background - Most ATmega32U4 boards (Leonardo, Micro, standard Pro Micro) operate at **5V 16MHz** and work fine with the current code - The issue specifically occurs with **3.3V 8MHz variants** where the SPR2 register reference causes compilation failures - The original code attempts to set SPI clock to 250kHz using non-existent SPR2 bit ## Solution Replace the non-existent `SPR2` with the correct combination of `SPR1` and `SPI2X` bits to achieve the same Fosc/32 clock division: - `SPR1=1, SPR0=0` → Fosc/64 - `SPI2X=1` → Double speed mode → Fosc/32 - Result: 8MHz/32 = 250kHz (maintains original target frequency)
veroxzik commented 2025-06-01 02:23:03 +03:00 (Migrated from github.com)

Thanks for the PR!

Thanks for the PR!
Sign in to join this conversation.