Switching to 8Mhz internal oscillator [solution]

Started by davegravy, April 27, 2016, 02:13:26 PM

joelucid

When I use dualoptiboot derived bootloaders I do recompile them for 8mhz. The reason is that the serial baud rate clock is generated internally based on the system clock and the UBBR register which is programmed according to F_CPU. If you don't recompile it should still work but at half the baud rate specified at build time.

Timeouts are generated by the WDT so for them no recompile is necessary.

Joe

kobuki

OK, thanks, so you mean to say I need to set half of the BR for programming with 8 MHz compared to 16 MHz to be able to use the same real BR. That sounds reasonable. Well, if I rely on the OTA functionality it would be alright I guess but otherwise it still is recommended to recompile so an initial FW for testing is uploadable via serial. Is that right?

perky

Also any delays like delay_ms() will also be dependent on F_CPU, so timeouts might not be as accurate. Blocking bit-banged interfaces that are timed with those delays will be out as well.

Mark.

kobuki

Well, I think it's only relevant in the bootloader, since the sketches are a separate compile phase, using settings for 8 MHz operation so no change is expected in timing constants there. Or do you mean to look out for the relative inaccuracy of the internal RC osc?

TomWS

#34
Quote from: joelucid on June 17, 2017, 12:50:57 AM
...The reason is that the serial baud rate clock is generated internally based on the system clock and the UBBR register which is programmed according to F_CPU.
F_CPU tracks the fuse settings.  Without changing the bootloader, F_CPU reads 8000000 on my Motes.  I know this because I used to print F_CPU when I was first testing battery operated Motes just to confirm that I had set the fuses correctly (Don't ask me why I needed to do this...   ;)

Tom

kobuki

#35
Quote from: TomWS on June 17, 2017, 08:59:37 AM
F_CPU tracks the fuse settings.  Without changing the bootloader, F_CPU reads 8000000 on my Motes.

That is expected in a sketch but in the bootloader F_CPU is a #define provided in the Makefile so indeed, to fix it a recompile is needed. Or one can live with the fact that serial speeds need to be adjusted but the rest of the bootloader works as expected. I think I'll just compile a variant for 8MHz.

EDIT: well, for normal sketches F_CPU is most probably also a define in the build environment but considers the target settings which should use a 8 MHz clock setting for this kind of usage - bootloader was compiled with F_CPU=16MHz, sketch is compiled with F_CPU=8MHz.

perky

F_CPU is normally either a #define in the source, or set in the IDE and ultimately sent as a -D parameter to the compiler. In any case I don't know how this can be set by fuses as the final F_CPU may depend on run-time settings like prescalers.

Mark.

kobuki

Just finished compiling the 8MHz variant. I'm attaching the .hex, someone might find it useful.

Info:
- used https://github.com/Optiboot/optiboot as devenv, replacing optiboot.c with Dualoptiboot.c
- atmega328 target change: --section-start=.text=0x7c00
- command line: make atmega328 -e BAUD_RATE=57600 -e AVR_FREQ=8000000L
- used Arduino 1.8.1 and mingw tools on windows

I set the BR to 57600 to be safe.

TomWS

Quote from: perky on June 17, 2017, 12:53:12 PM
F_CPU is normally either a #define in the source, or set in the IDE and ultimately sent as a -D parameter to the compiler. In any case I don't know how this can be set by fuses as the final F_CPU may depend on run-time settings like prescalers.

Mark.
Whoops.  You're right, it is in the BOARDS file for the MoteinoLP (8MHz) I have: MoteinoLP.build.f_cpu=8000000L
Sorry about the misdirection.

Tom

perky


Uncle Buzz

Sorry for reviving such an old thread (©Kobuki)
I want to run moteino (and moteino like) from LifePo4 without LDO, so between 3.6V and somewhere below 3V, and have to run at 8MHz.

If I run with internal 8MHz clock, could I save some place and remove 16MHz crystal ? (crystal pins should be unconnected or connected to ground or something else ?)
Internal clock is less precise, do you have an idea how much ?
Is it possible to use an external 8MHz crystal like with the 16MHz to keep relative precision on clock instead of the internal one in low voltage condition ?

TomWS

#41
Quote from: Uncle Buzz on July 27, 2018, 12:28:45 PM
Sorry for reviving such an old thread (©Kobuki)
I want to run moteino (and moteino like) from LifePo4 without LDO, so between 3.6V and somewhere below 3V, and have to run at 8MHz.
My standard configuration uses Lithium/Iron Disulfide (Li/FeS2) batteries (Eveready Ultimate Lithium) with fuses set to 8MHz internal, No BOD, and no external crystal (oscillator pins left unattached), and no VR - run directly off the battery for processor & radio.

UPDATE: The problem you'll have with LifePo4 is if you want to charge it while installed.  In this case the voltage could exceed the maximum specified for the radio.  While the Li/FeS2 batteries are not rechargeable, they last so long that they'll probably only need to be replaced once in the life of your mote (this is based on my well founded pessimism about mote lifespan primarily governed by corrosion or obsolescence).
Quote
If I run with internal 8MHz clock, could I save some place and remove 16MHz crystal ? (crystal pins should be unconnected or connected to ground or something else ?)
you can leave the crystal installed or removed, it doesn't matter - leave the pins disconnected if you have no crystal.
Quote
Internal clock is less precise, do you have an idea how much ?
Is it possible to use an external 8MHz crystal like with the 16MHz to keep relative precision on clock instead of the internal one in low voltage condition ?
It's good enough to run serial port reliably at 57.6K and certainly far better than the WDT.  I don't rely on the processor clock for critical timing.

Tom

Uncle Buzz

Thank you for your reply.

For LiFePo4, for low power board, I don't need to charge battery on the node, and for those with high power consumption, battery is isolated from the board while charging.

I need to use some SPI devices (at least the radio), is the internal RC oscillator accuracy sufficient in this case ? I could use an 8MHz resonator instead of the 16MHz, but those I found are with 30pF, it exceeds the 12-22pF from the datasheet, should I use a crystal + 2 capacitors instead (not possible on moteino) ?

Felix

Uncle Buzz,
Do you have specific SPI speeds of 4mhz or 8mhz? Or are you just "worried" the RC won't be precise enough?
I think you're fine with the internal RC, the library divides the SPI clock so at 8mhz it will be half that at 16Mhz.

Uncle Buzz

I'm just worried.
I wonder how accurate is the timing from internal RC, for fast communication like SPI (but the moteino is driving the clock, so there's no problem with synchronisation), and for synchronisation between moteino, if I plan to reserve a window for each moteino to communicate with the gateway, how often will I have to resync the moteino with gateway to avoid collision.