2
\$\begingroup\$

I have burned an arduino bootloader to an ATmega168PA. I want to use the internal 8MHz oscillator for the ATmega, and I used an arduino Uno as an ISP. For that, I did the following:

  • Created the directory at Documents/Arduino/hardware/breadboard/avr. In there I placed a boards.txt file, and a bootloaders folder, which contained the .hex file. Both files can be found at the end.

  • I found the hex file I used in this link. From it, I used the optiboot_atmega168pa_8000000L_57600.hex

The burning of the bootloader completed successfully. Also I successfully uploaded a blink example to my ATmega168PA via the arduino ISP and the Arduino IDE.

The problem is that when I try to upload something a second time, I get this error:

enter image description here.

If I reburn the bootloader, I can again successfully upload a sketch. But once more, I cannot upload a second time.

optiboot_atmega168pa_8000000L_57600.hex file:

 :103E00001F92CDB7DEB7112484B714BE982F9D70D2
:103E100009F0C4D082E08093C00088E18093C100A3
:103E200086E08093C20080E18093C4008EE0A1D040
:103E3000812C912C93E0F92EEE24E39425E0D22EF0
:103E400031E1C32E8AD0813479F487D0898397D029
:103E50008981823811F482E005C0813811F486E04E
:103E600001C083E073D06FC0823411F484E103C0D9
:103E7000853419F485E08BD066C0853541F46DD06A
:103E8000882E6BD0912C982A880C991C5BC08635A3
:103E900021F484E07CD080E0E5CF843609F036C0A0
:103EA0005CD05BD0B82E59D0A82E00E011E055D0E0
:103EB000F80181938F01BE12FACF61D0F5E4AF1201
:103EC00001C0FFCFF401F7BEE89507B600FCFDCFB7
:103ED000B401A0E0B1E02C911296AD01415051091E
:103EE000FA01808130E0382BFB010901E7BEE8953B
:103EF00011246E5F7F4FBA12EECFF401D7BEE89562
:103F000007B600FCFDCFC7BEE8951DC0843769F435
:103F100024D023D0B82E21D032D08401F80185914D
:103F20008F0114D0BA94D1F70EC0853739F427D059
:103F30008EE10CD084E90AD08BE094CF813511F466
:103F400088E017D01CD080E101D07CCF9091C000D8
:103F500095FFFCCF8093C60008958091C00087FF35
:103F6000FCCF8091C00084FD01C0A8958091C6005F
:103F70000895E0E6F0E098E1908380830895EDDF16
:103F8000803219F088E0F5DFFFCF84E1DFCFCF93F7
:103F9000C82FE3DFC150E9F7CF91F1CF282E80E0A1
:083FA000E8DFE0E0FF270994CF
:023FFE000206B9
:0400000300003E00BB
:00000001FF

Boards.txt file:

atmega168pabb.name=ATmega168PA on a breadboard (8 MHz internal clock)

atmega168pabb.upload.protocol=arduino
atmega168pabb.upload.maximum_size=14336
atmega168pabb.upload.maximum_data_size=1024

atmega168pabb.upload.speed=19200
#atmega168pabb.upload.speed=115200
atmega168pabb.upload.tool=arduino:avrdude

atmega168pabb.bootloader.tool=arduino:avrdude
atmega168pabb.bootloader.low_fuses=0x62
atmega168pabb.bootloader.high_fuses=0xDF
atmega168pabb.bootloader.extended_fuses=0xF8
atmega168pabb.bootloader.file=optiboot_atmega168pa_8000000L_57600.hex
atmega168pabb.bootloader.unlock_bits=0x3F
atmega168pabb.bootloader.lock_bits=0x3F

atmega168pabb.build.mcu=atmega168p
atmega168pabb.build.f_cpu=8000000L
atmega168pabb.build.core=arduino:arduino
atmega168pabb.build.variant=arduino:standard
atmega168pabb.build.board=ATmega168PA_BreadBoard
\$\endgroup\$
11
  • \$\begingroup\$ Do you do an erase before the write? \$\endgroup\$
    – Ron Beyer
    Apr 4, 2020 at 19:38
  • \$\begingroup\$ @Ron Beyer I do not think I do. I have been using the Arduino IDE to upload the code, so I use the standard procedure one would do in order to reprogram the arduino. What would you suggest? \$\endgroup\$
    – NickG
    Apr 6, 2020 at 10:35
  • 1
    \$\begingroup\$ if you upload a sketch over ISP, you delete the bootloader \$\endgroup\$
    – Juraj
    Apr 7, 2020 at 9:07
  • \$\begingroup\$ @RonBeyer How do I do the erase? \$\endgroup\$
    – NickG
    Apr 29, 2020 at 10:51
  • \$\begingroup\$ @Juraj Isn't the bootloader necessary in order to run the arduino code and use arduino IDE and libraries to standalone atmegas? How does it work if the bootloader is deleted? \$\endgroup\$
    – NickG
    Apr 29, 2020 at 10:52

1 Answer 1

1
\$\begingroup\$

It appears that you haven't written the lock bit after loading the bootloader. If the lock bit isn't set to its "locked" state, the data will be overwritten by the code that you load and you will not be able to load a sketch a second time or any thereafter without erasing the memory and writing a new bootloader. You can make use of avrdude through the terminal, where you can specify the bootloader hex file you wish to load, and you can write the fuse bits and the lock bit as required for your microcontroller version.

You might find this blog entry useful: https://foraysinto3d.blogspot.com/2011/09/chapter-17-in-which-i-finally-burn.html

It also appears your boards.txt file has the same value written for lock_bits and unlock_bits:

atmega168pabb.bootloader.unlock_bits=0x3F
atmega168pabb.bootloader.lock_bits=0x3F

These entries should have different values so that you can toggle the lock bit (it will not work if they are the same).

Here you can read about avrdude: https://avrdudes.github.io/avrdude/7.1/avrdude.html#Introduction

If you choose to make use of avrdude from the command line, the entries should look like this:

$ /usr/local/bin/avrdude -p atmega168p -c avrisp -P /dev/ttyUSB0 -b 115200 -e -v -C /home/myusername/Arduino/hardware/MiniCore/avr/avrdude.conf -Ulock:w:0xff:m -Uefuse:w:0xfc:m -Uhfuse:w:0xdf:m -Ulfuse:w:0xf7:m -Uflash:w:/home/myusername/Arduino/hardware/MiniCore/avr/bootloaders/optiboot_flash/bootloaders/atmega168p/16000000L/optiboot_flash_atmega168p_UART0_115200_16000000L_B5.hex:i 
$ /usr/local/bin/avrdude -p atmega168p -c avrisp -P /dev/ttyUSB0 -b 115200 -v -D -Ulock:w:0xcf:m 

In the first line, I write the lock bit OFF (unlocked):

-Ulock:w:0xff:m

and in the second line, once the bootloader is written and verified, I write the lock bit ON (locked):

-Ulock:w:0xcf:m

Of course, you should become familiar with the format for avrdude, and you should identify the specific command line options for your microcontroller. In these lines you will see that I used Minicore, which offers support for your version of the ATmega168 (i.e. version PA). Make sure that the values you write for the fuses match those in the boards.txt file (in my case, the one from Minicore).

See Minicore: https://github.com/MCUdude/MiniCore

Also, please note that this method requires the use of an ICSP (I used an arduino pro mini loaded with the example ArduinoISP sketch that comes in the Arduino IDE, only having modified the baudrate based on my requirements, and it worked for me).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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