I want to run my 32u4 in 2MHz. According to gammon I will gain 4mA in runtime. To my understanding this changes all the buses speed like UART, USB?, I2C?** and SPI?*. Probably I will have problems to solve since I have connected devices on all buses. Most importantly I am afraid that I will loose the connection to re-program it via USB (adafruit feather 32u4).
So, the bootloader will be fine if clock_prescale_set(clock_div_4);
after reset of the device or I need ISP programming to re-gain access?
I don't want - currently - to play with fuses.
* for example SPI currently works with SPISettings(4000000,...)
so I suppose I will have to use 1Mhz since 2Mhz would be on the limit?
** I2C is at 100Khz in arduino. So my 2Mhz μCU should be fine?
1 Answer 1
If I use clock_prescale_set in my sketch I will loose access to adafruit feather 32u4?
TL;DR: The answer is "no."
All data sheet references base on this version. You need to study it and to do some careful experiments to learn how it works actually.
To my understanding this changes all the buses speed like UART, USB?, I2C? and SPI?.
This is only true for all named peripherals but USB. Figure 6.1 shows the clock system.
The USB module needs a clock of 48 MHz (chapter 21.2) that is generated via a PLL from an 8 MHz input clock.
As figure 6.1 shows, the PLL taps the clock before the prescaler for the system clock. Therefore, you can use any prescaler divider you want, it does not change the USB clock. (If you use USB in your application at all.)
Most importantly I am afraid that I will loose the connection to re-program it via USB (adafruit feather 32u4).
As the bootloader runs before your application, the latter does not change clock settings of the former. So, no, your bootloader will continue to work.
So, the bootloader will be fine if clock_prescale_set(clock_div_4); after reset of the device or I need ISP programming to re-gain access?
A reset will also reset registers, specifically CLKPS[3..0]
will be 0000 or 0011, depending on fuse CKDIV8
(chapter 6.11.4).
See above, the bootloader will be fine. It will set its own values where necessary.
for example SPI currently works with SPISettings(4000000,...) so I suppose I will have to use 1Mhz since 2Mhz would be on the limit?
The SPI uses a prescaler that divides at least by 2. If you want to use a system clock of 2 MHz, the fastest SPI clock will be 1 MHz (fOSC/2) if you use SPI master mode. In SPI slave mode, the SPI clock can be at max 500 kHz (fOSC/4).
Chapter 17 has all the details.
I2C is at 100Khz in arduino. So my 2Mhz μCU should be fine?
According to chapter 20.5.2, the maximum SCL frequency is fOSC/16. So 100 kHz is possible.
Note: In the first version of my answer I was under the impression that the questioner planned to swap the external crystal. That was my mistake.
-
Thank you for the analytical answer and the references to datasheet. Wow, it seems it's a master game that. Sorry, I didn't realized that those info are on datasheet although I read it for other reasons. I had the hope that bootloader had his own divider command for defaults to not loose access. Thank you again.krg– krg01/21/2025 04:42:09Commented Jan 21 at 4:42
-
1@krg Sorry for the inconvenience, I misread your question. Actually it is the other way, so just go ahead and save some current.the busybee– the busybee01/21/2025 07:33:53Commented Jan 21 at 7:33
-
No worries. You where very helpful even with wrong assumption. So no risks involved, since the bootloader will reset the clock_divider. Great! The problem I see is that I have to change UART bps speed - one used for debugging and other for GPS - and possibly SPI and I2C for other peripherals. Maybe the modules don't support different speeds. THANK YOU!krg– krg01/22/2025 09:37:49Commented Jan 22 at 9:37
-
Today I managed to test and everything went almost fine. In adafruit feather 32u4 I loose debuging serial with all dividers except
_div_2
. I also loose UART connection. I tried9600 * 2
baud rate but the connection is not reliable. But with another device withFTDI
serial I went down to_div_32
. UART not tested. But SPI is working without any modification. I2C untested. I went from 12mA to 1.6mA. That's great. @the busybee: THANK YOU!krg– krg03/16/2025 09:07:32Commented Mar 16 at 9:07
Explore related questions
See similar questions with these tags.
clock_prescale_set(clock_div_2)
if you need USB port. 2) The max speed of SPI is F_CPU/2 or F_CPU/4 for SPI slave. All those information are available in the datasheet._div_2
is suitable. But mind you this is4Mhz
, not8Mhz
. SPI just works, I didn't changed anything. I went down to_div_32