Switching to 8Mhz internal oscillator [solution]

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

davegravy

I can program the moteino with stock fuse settings no problem via Arduino IDE.

I've uploaded fuse settings using rasperry pi GPIO pins and avrdude to use the 8Mhz internal oscillator.

Fuse settings:

l:d2 
h:dc
e:fd


I've added a section to boards.txt:

Moteino8m.name=Moteino8m
Moteino8m.upload.tool=arduino:avrdude
Moteino8m.upload.protocol=arduino
Moteino8m.upload.maximum_size=31744
Moteino8m.upload.speed=57600
Moteino8m.bootloader.low_fuses=0xD2
Moteino8m.bootloader.high_fuses=0xDC
Moteino8m.bootloader.extended_fuses=0xFD
Moteino8m.bootloader.path=Moteino/DualOptiboot_V5.0_atmega328_BlinkD9.hex
Moteino8m.bootloader.unlock_bits=0x3F
Moteino8m.bootloader.lock_bits=0xCF
Moteino8m.build.mcu=atmega328p
Moteino8m.build.f_cpu=8000000L
Moteino8m.build.core=arduino:arduino
Moteino8m.build.variant=arduino:standard
Moteino8m.build.board=AVR_MOTEINO
##############################################################


When i try to upload the same sketch using the Moteino8m board via Arduino IDE and FTDI adapter on /dev/ttyUSB0 I get
avrdude: Version 6.1, compiled on Jul  7 2015 at 10:29:47
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/usr/share/arduino/hardware/tools/avrdude.conf"
         User configuration file is "/root/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyUSB0
         Using Programmer              : arduino
         Overriding Baud Rate          : 57600
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: S [53] 
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x53
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: t [74] 
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x74
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: a [61] 
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x61
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: r [72] 
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x72
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: t [74] 
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x74
avrdude: Send: 0 [30]   [20] 
avrdude: Recv:   [20] 
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x20
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: n [6e] 
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x6e
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: o [6f] 
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x6f
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: d [64] 
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x64
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: e [65] 
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x65


Something I'm missing?

joelucid

Has the bootloader been compiled for 8mhz and 57600 baud? The default is likely 16mhz and 115200 baud.

Joe

TomWS

Quote from: joelucid on April 27, 2016, 02:51:12 PM
Has the bootloader been compiled for 8mhz and 57600 baud? The default is likely 16mhz and 115200 baud.

Joe
The dual OptiBootloader will autoswitch baud rate.  You don't need to rebuild it for 8MHz or a specific baud rate.

The settings look good to me, but I'll post my boards.txt file tomorrow when I get back to my office.

Tom

davegravy

This round goes to joelucid.

make AVR_FREQ=8000000L BAUD_RATE=57600 atmel328p


...did the trick. Wonder why auto-switch baud rate didn't work?

joelucid

QuoteThe dual OptiBootloader will autoswitch baud rate.  You don't need to rebuild it for 8MHz or a specific baud rate.

Looking at the source code I don't think that's correct. There are several references to F_CPU and BAUDRATE. Now running a 115200 / 16 MHz binary on a 8 MHz Moteino might actually give you something that works with 57600 because both changes counteract each other. But one would have to look more closely than I have to see if that causes bad side effects.

perky

If it's using delay functions, like _delay_ms(), these are pre-compliled using F_CPU so the delays would be a factor of 2 out.
Mark.

TomWS

Quote from: joelucid on April 28, 2016, 12:27:55 AM
QuoteThe dual OptiBootloader will autoswitch baud rate.  You don't need to rebuild it for 8MHz or a specific baud rate.

Looking at the source code I don't think that's correct. There are several references to F_CPU and BAUDRATE. Now running a 115200 / 16 MHz binary on a 8 MHz Moteino might actually give you something that works with 57600 because both changes counteract each other. But one would have to look more closely than I have to see if that causes bad side effects.
Curious.  Almost ALL of my motes are re-fused for 8MHz and I still use the out of the box bootloader.

Here is the entry in boards.txt:
##############################################################
MoteinoLP.name=Moteino8MHz
MoteinoLP.upload.tool=avrdude
MoteinoLP.upload.protocol=arduino
MoteinoLP.upload.maximum_size=31744
MoteinoLP.upload.maximum_data_size=2048
MoteinoLP.upload.speed=57600
MoteinoLP.bootloader.low_fuses=0xE2
MoteinoLP.bootloader.high_fuses=0xDA
MoteinoLP.bootloader.extended_fuses=0x05
MoteinoLP.bootloader.path=Moteino
MoteinoLP.bootloader.file=DualOptiboot_V5.0_atmega328_BlinkD9.hex
MoteinoLP.bootloader.unlock_bits=0x3F
MoteinoLP.bootloader.lock_bits=0xCF
MoteinoLP.build.mcu=atmega328p
MoteinoLP.build.f_cpu=8000000L
MoteinoLP.build.core=arduino:arduino
MoteinoLP.build.variant=arduino:standard
MoteinoLP.build.board=AVR_MOTEINO
##############################################################



WhiteHare

#7
Just to put this in context, Felix's advice on September 20, 2014 was:

Quote from: Felix on September 20, 2014, 04:42:12 PM
If you change the resonator to 8mhz your code will run half as fast (or 50% slower). You would need to recompile the bootloader and then compile your sketches for 8mhz.
https://lowpowerlab.com/forum/index.php/topic,650.msg3821.html#msg3821

Looking at github, the last commit for dualoptiboot was September 3, 2014.  So, presumably (?), Felix's same advice would hold today.

Unfortunately, it's a hassle. 
@Tom From what you're saying, it sounds as though you've somehow managed to avoid that hassle, so it would be great if you could perhaps run through an example, in detail, of your own particular conversion process so that everyone else can also avoid the hassle too.

ssmall

I took Tom's MoteinoLP definition and put it into my boards.txt file.  When I used that board definition to upload a blink sketch I received a programmer not responding error, similar to what @davegravy posted.  I then changed the MoteinoLP.upload.speed to 115200 and that worked.  The led blinked every 2 seconds.  In the sketch I used delay(1000), so that seems to make sense since it's running at 1/2 speed.  Is this one way to deal with the slower speed if you use some delay() calls in your code?  http://playground.arduino.cc/Code/Prescaler

TomWS

Quote from: ssmall on May 01, 2016, 06:30:15 PM
I took Tom's MoteinoLP definition and put it into my boards.txt file.  When I used that board definition to upload a blink sketch I received a programmer not responding error, similar to what @davegravy posted.  I then changed the MoteinoLP.upload.speed to 115200 and that worked.  The led blinked every 2 seconds.  In the sketch I used delay(1000), so that seems to make sense since it's running at 1/2 speed.  Is this one way to deal with the slower speed if you use some delay() calls in your code?  http://playground.arduino.cc/Code/Prescaler
You didn't say that you also changed the fuses to 8MHz internal clock.  Did you?   With the fuses as I've cited and the boards.txt file I've posted I haven't had to change anything to get accurate delay() times and my uploads work reliably (other than the occasional failure when I forget to attach the battery  ::)

The prescaler library you've posted is useful as I do sometimes drop the 8MHz to 1MHz to minimize processor current (mostly when running from a coin cell).  Thanks!

Tom

ssmall

@Tom:  I just copied the entry you posted for MoteinoLP.  I assumed that changed the fuses.  What do I need to do to change the fuses?

TomWS

Quote from: ssmall on May 01, 2016, 06:58:44 PM
@Tom:  I just copied the entry you posted for MoteinoLP.  I assumed that changed the fuses.  What do I need to do to change the fuses?
Ah, ok, makes total sense now.  Your Moteino is still running at 16MHz external crsytal so the boards.txt section will be incorrect settings.

To 'burn' the fuses, you will need to hook up a device that uses the 'ISP' interface (this uses the SPI pins in a special way).   I use a Pocket AVR Programmer from Sparkfun: https://www.sparkfun.com/products/9825  Others don't think this is 'reliable' enough, but I've never had a problem (you do need pullup resistors on all the native SPI device CS pins).  I've made a pogo pin adapter for a Moteino card (it requires about six real pins and a couple of corner pins for physical support).  Sparkfun has a pretty good tutorial on using the board and how to hook it up under a variety of situations.

Tom


ssmall

Thanks for the help Tom.  I knew it couldn't be that easy.  I will look over documentation on the programmer.

ssmall

I purchased the Pocket AVR Programmer from Sparkfun: https://www.sparkfun.com/products/9825.  Using an Arduino Uno I have, I was able to follow the tutorial and program the Arduino with a sketch and read the fuses with the AVR Programmer.  :)
For my next step I would like to try and read the fuses on a Moteino.  After reading different posts in the forum, I know I should not use the AVR Programmer to power the Moteino and I need a 10K pullup resistor on D10.  How should I power the Moteino?   I am assuming that the AVR Programmer is 5v so I need to do logic level conversion? :o

TomWS

Quote from: ssmall on May 15, 2016, 04:21:51 PM
I purchased the Pocket AVR Programmer from Sparkfun: https://www.sparkfun.com/products/9825.  Using an Arduino Uno I have, I was able to follow the tutorial and program the Arduino with a sketch and read the fuses with the AVR Programmer.  :)
For my next step I would like to try and read the fuses on a Moteino.  After reading different posts in the forum, I know I should not use the AVR Programmer to power the Moteino and I need a 10K pullup resistor on D10.  How should I power the Moteino?   I am assuming that the AVR Programmer is 5v so I need to do logic level conversion? :o
You will find that there is a switch on the pocket programmer that selects 'PWR/No Power'.  Set it to 'No Power' meaning that the device being programmed supplies its own power.

I would recommend supplying some voltage > 3.4V to Vin on the Moteino so that you have a nominal 3.3V supply AT the processor.

In this case, when the ISP pins are wired correctly to a Moteino, the 3.3V pin should be connected to the Pocket Programmer's '5V' signal (it should be named PWR, not 5V) and the level translator on the Pocket Programmer will supply 3.3V signaling to the 328P on the Moteino.  The 10K pullup should be tied to 3.3V also.

Makes sense?

Tom