2

I would like to use an Atmega328p micro-controller with a 4 Mhz oscillator in order get some low power consumptions. I am using an Arduino board to flash the bootloader into the Atmega.

EDIT

In order to compile the optiboot bootloader I had to install the avr-libc package and clone the repo

sudo apt-get install avr-libc
git clone https://github.com/Optiboot/optiboot.git

and edited the Makefile in /optiboot/bootloaders/optiboot/Makefile by adding a new entry called atmega328_4

HELPTEXT += "target atmega328     - ATmega328p 4Mhz\n"
atmega328_4: TARGET = atmega328
atmega328_4: MCU_TARGET = atmega328p
atmega328_4: CFLAGS += $(COMMON_OPTIONS)
atmega328_4: AVR_FREQ ?= 4000000L
ifndef BIGBOOT
atmega328_4: LDSECTIONS  = -Wl,--section-start=.text=0x7e00 -Wl,--section-start=.version=0x7ffe
else
# bigboot version is 1k long; starts earlier
atmega328_4: LDSECTIONS  = -Wl,--section-start=.text=0x7c00 -Wl,--section-start=.version=0x7ffe
endif
atmega328_4: $(PROGRAM)_atmega328_4.hex
atmega328_4: $(PROGRAM)_atmega328_4.lst

atmega328_4_isp: atmega328
atmega328_4_isp: TARGET = atmega328
atmega328_4_isp: MCU_TARGET = atmega328p
ifndef BIGBOOT
# 512 byte boot, SPIEN
atmega328_4_isp: HFUSE ?= D9
else
# 1k byte boot, SPIEN
atmega328_4_isp: HFUSE ?= D9
endif
# Low power xtal 46MHz) 16KCK/14CK+65ms
atmega328_4_isp: LFUSE ?= 7D
# 2.7V brownout
atmega328_4_isp: EFUSE ?= FE
atmega328_4_isp: isp

I saved the file, run the make command and got the following error:

userk@norepinephrine:~/development/git/optiboot/optiboot/bootloaders/optiboot$ make atmega328_4
avr-gcc (GCC) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

BAUD RATE CHECK: Desired: 115200, Real: 125000, UBRRL = 3, Difference=8.5%
avr-gcc -g -Wall -Os -fno-split-wide-types -mrelax -mmcu=atmega328p -DF_CPU=4000000L  -DBAUD_RATE=115200 -DLED_START_FLASHES=3           -c -o optiboot.o optiboot.c
optiboot.c:348:6: error: #error BAUD_RATE off by greater than 5%
     #error BAUD_RATE off by greater than 5%
      ^
optiboot.c:359:2: error: #error Unachievable baud rate (too fast) BAUD_RATE
 #error Unachievable baud rate (too fast) BAUD_RATE 
  ^
<builtin>: recipe for target 'optiboot.o' failed
make: *** [optiboot.o] Error 1

So I changed the -DBAUD_RATE parameter to 19200 and rerun the make command

BAUD_RATE_CMD = -DBAUD_RATE=19200

Then 2 new files were created: optiboot_atmega328_4.elf and optiboot_atmega328_4.hex

Then I copied these files into the arduino configuration folder located at: /home/userk/.arduino15/packages/arduino/hardware/avr/1.6.21/bootloaders/optiboot

TIDE

I am using a custom board configuration file board.txt

##############################################################

nz.name=Arduino Noize

nz.vid.0=0x2341
nz.pid.0=0x0043
nz.vid.1=0x2341
nz.pid.1=0x0001
nz.vid.2=0x2A03
nz.pid.2=0x0043
nz.vid.3=0x2341
nz.pid.3=0x0243

nz.upload.tool=avrdude
nz.upload.protocol=arduino
nz.upload.maximum_size=32256
nz.upload.maximum_data_size=2048
nz.upload.speed=19200

nz.bootloader.tool=avrdude
nz.bootloader.low_fuses=0x7D
nz.bootloader.high_fuses=0xD9
nz.bootloader.extended_fuses=0xFE
nz.bootloader.unlock_bits=0x3F
nz.bootloader.lock_bits=0x0F
nz.bootloader.file=optiboot/optiboot_atmega328_4.hex

nz.build.mcu=atmega328p
nz.build.f_cpu=4000000L
nz.build.board=AVR_UNO
nz.build.core=arduino
nz.build.variant=standard

Flashing the Bootloader I have followed these steps:

  • Uploaded the ArduinoISP sketch onto the Arduino UNO board.

  • Wired up the Arduino board and microcontroller as shown in the diagram below

enter image description here

  • Select "Arduino Noize" from the Tools > Board menu.

  • Select "Arduino as ISP" from Tools > Programmer Run Tools > Burn Bootloader

At this point I got a success message from IDE. So the bootloader has been correctly flashed into the Atmega328p

The Problem

When I try to upload a sample sketch (blink) using the wiring below I get a timeout from the IDE.

enter image description here

I have double checked the connections but the sketch doesn't upload. Do you have any advice?

  • 2
    How did you compile the optiboot bootloader? You need to tell the bootloader at what speed it's running. – Gerben Sep 6 '18 at 18:13
  • Thanks, I am updating the post with the optiboot's Makefile – UserK Sep 10 '18 at 11:33
  • So you compiled optiboot with a baudrate of 19200, but specify 9600 in your boards.txt. – Gerben Sep 11 '18 at 14:37
  • Sorry I didn't update the question after the addition of the optiboot configuration. Now it is updated. I still have the same timeout problem. – UserK Sep 11 '18 at 15:13
2

As Gerben notes in the comments, the bootloader has to know how fast the board is running. It uses this to work out the baud rate settings for the serial communication.

If you haven't compiled the bootloader yourself with the right clock settings you will probably find that it is operating at a quarter of the speed you expect it to. Dividing your upload baud rate by four in your board settings may make it work, but you really should compile the bootloader to match the board properly.

  • Thanks, I have edited the question with the information related to the optiboot bootloader compilation – UserK Sep 10 '18 at 12:23
2

As mentioned before, you need to compile the Optiboot bootloader with the correct clock frequency. Here's a guide that shows you how to do that, as well as adding an option to the Arduino IDE menu to select the right clock speed without having to add your own board definition.

Optiboot - ATmega328P at custom frequency

  • Good tutorial, I am trying to follow the provided steps but the example is for 12Mhz clock. Please see my edits – UserK Sep 10 '18 at 11:34
  • 1
    The principle is exactly the same for clock speeds other than 12 MHz. I think that you selected the incorrect baud rate for uploading. Either change nc.upload.speed in your boards file to 115200 baud, or recompile the bootloader with a baud rate of 19200. – tttapa Sep 10 '18 at 11:40
1
nz.bootloader.low_fuses=0x7D
nz.bootloader.high_fuses=0xD9
nz.bootloader.extended_fuses=0xFE

Extended fuse byte. bits 2,1,0 determine the brown out level (bits 7-3 not used) 110 = 1.7 to 2.0V, 1.8 typical

Low fuse byte bits 3,2,1 determine the clock source 110 = low power crystal oscillator in 3-8 MHz range.

So those settings look okay.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.