I'm working with an ATmega168 with a Linux based board (TI DM365). The ATmgea168 communicates with the TI over UART.

The Atmel is programmed using AVRMKII and is running fine. I was wondering if there was way to utilize the UART lines and program the Atmel from a Linux environment?

Right now the UART is set up to receive basic (single char) commands from the TI in interrupt routines and echo out the results. I suppose I need to modify this to make the programming over UART work.

EDIT: (should have noticed this earlier) The atmel is actually controlling the power to all the devices, including the TI. So if I try to reprogram the atmel while the system is up and running, the TI will definitely lose power. So, with a set up like this, it will be very difficult to reprogram the atmel. But any ideas or suggestions would be great.

  • \$\begingroup\$ Please update your question with that new information; Then all the comments can be thrown away, and the question still makes sense. It may be feasible to enable the ATmega to reprogram itself. However, we are going to need to know more. Is their enough time between the commands from the TI DM365 to the ATmega for the ATmega to reprogram itself? Or could the commands be delayed for long enough to allow it to happen? \$\endgroup\$
    – gbulmer
    Sep 25, 2014 at 1:47
  • \$\begingroup\$ The atmel is setup to continuously monitor some external buttons. However, the TI only communicates once in a while. So the commands from the TI can be stopped while the update is being performed. \$\endgroup\$ Sep 25, 2014 at 16:38
  • \$\begingroup\$ The only real idea at this point would be to utilize the RWW and the NRWW sections of the atmel (ATmega168 datasheet, section 27). Where in theory it is possible to have 2 'partitions'. Anyone ever used this? \$\endgroup\$ Sep 25, 2014 at 16:42
  • \$\begingroup\$ That is how Arduino's Flash is used. I've updated my answer. \$\endgroup\$
    – gbulmer
    Sep 25, 2014 at 16:51
  • \$\begingroup\$ So as far as I understand: I keep the bootloader(enabling UART comm) in the RWW section, along with basic gpio controls to keep the TI alive. In the NRWW section, i keep the rest of the functions and interrupts which is allowed to be overwritten. This will obviously take me some time to get it right, but is there a basic tutorial to do this? \$\endgroup\$ Sep 25, 2014 at 16:58

1 Answer 1


Arduino's 'bootload' their programs themselves via a USB-to-serial interface.

So, if you loaded an Arduino ATmega168 bootloader (there are several to choose from), it would do exactly what you want.

This Arduino Bootloader page explains how to do it, and gives links to bootloader source code.

Remember, there are several different ATmega's used in Arduino' so be sure to get the ATmega168 version.

An ATmega88/168/328 has two Flash memory sections called 'Read-While-Write' (RWW) and 'No Read-While-Write' (NRWW). They are described in Section "27.4 Read-While-Write and No Read-While-Write Flash Sections" of the ATmega48/88/168/328 manual.

The difference between the two Flash sections is an ATmega88/168/328 can run a program in NRWW at the same time as programming RWW Flash. However, the Atmega CPU is halted when NRWW Flash is being programmed, i.e. it will not run any program while NRWW in being programmed. So NRWW Flash memory can only be programmed using an external programmer.

Further, the manual warns against trying to read RWW Flash while it is being programmed, for example the CPU must not try to execute a program from RWW Flash while RWW Flash is being programmed:

During an on- going programming, the software must ensure that the RWW section never is being read. If the user software is trying to read code that is located inside the RWW section (i.e., by a call/jmp/lpm or an interrupt) during programming, the software might end up in an unknown state.

It continues to explain that interrupts should either be disabled, or be handled within the same NRWW section as the bootloader. So, the existing UART code, and bootloader code will need to be combined, and the combination must be contained completely within the NRWW section, or it won't reliably work to reprogram the 'application' in RWW Flash,

Also ATmega has its Flash memory partitioned into two areas which can protected using fuse bits, independently of the other, from being accidentally overwritten. It might be worth using this feature to protect the UART interrupt routine and bootloader from accidental damage.

The problem of loading code is not solved by an in-Flash bootloader alone. You will also need a program on the host PC to upload the program. Many ATmega tool chains, including the Arduino IDE, use avrdude. If you install that, and follow the documentation, avrdude will upload to an Arduino bootloader.

The good thing about this approach is you can install the Arduino IDE on a machine, and test the entire process using a toolchain which is known to work, and supports ATmega168. So it should be relatively straightforward to get it working.

There is also a AVR bootloader project called kavr which claims to be only 512bytes. I have no experience of it.

  • \$\begingroup\$ @Ricardo - It looks like I was editing at the same time, and overwrote your corrections. I think I assumed I had it open for editing in two tabs or something, but I'm not sure. I found a note in 'activities. So I have re-applied your corrections. Thank you for improving the answer. \$\endgroup\$
    – gbulmer
    Sep 24, 2014 at 18:37
  • \$\begingroup\$ Thank you for re-applying my edit. But they were minor, anyway. Edit colisions happen sometimes, but they usually are not a big deal. I thought that could happen as your last edits were still fresh, but I went ahead anyway. But the site keeps enough edit history information that allows us to rebuild the content without loosing any improvements. \$\endgroup\$
    – Ricardo
    Sep 24, 2014 at 18:48
  • \$\begingroup\$ I don't understand "But the site keeps enough edit history information that allows us to rebuild the content without loosing any improvements". My changes over-wrote yours. Are you saying the site has software which applies 'lost' fixes (which seems quite clever) or are you saying folks trawl through change histories looking for 'lost' edits? \$\endgroup\$
    – gbulmer
    Sep 24, 2014 at 18:53
  • \$\begingroup\$ No, there's no merge of edits so mine were effectively lost. But I meant that you could look up the edit history and see what I had done and apply my lost changes manually. \$\endgroup\$
    – Ricardo
    Sep 24, 2014 at 19:18
  • \$\begingroup\$ The kavr example was a good one, this was what I was talking about. But unfortunately, it will not work for my set up. The atmel is actually controlling the power to all the devices in the board, including the TI. If I use kavr, it will delete everything first and only then will reflash the chip. So the TI will definitely lose power and will effectively brick the system. I've updated my question to reflect this. \$\endgroup\$ Sep 24, 2014 at 19:25

Your Answer

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

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