Bootloader for Atmega32u4 (store new sketch and jump there)

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi.

I trying to make new bootloader for my Arduino Leonardo (basically just Atmega 32u4), and make it simple as possible.
Just reading from serial (uart) and want to write the data into flash memory (new sketch).

So exactly now, I can read character from UART, that's means I can just push hex files through putty to Atmega32u4, I am already done with this part.
On my bootloader it is a watchdog, who interrupt after 8 sec.
So I want to read from my UART and save the hex file and put them into flash memory. After 8 sec, in my ISR intterup, I want to quit the bootloader and go to start of my new sketch.

With the hex file, I means that I have already write very simple sketch and compile it with Arduino and I have the final hex file, who I want to push through putty to UART.

So I have 2 questions.
- How can I store hex file into flash memory?
Will this work?

    char *rxByte = (char*) malloc(sizeof(char));
    *rxByte = uart_getchar();
    char rxByteValue = *rxByte;
    if(rxByteValue != '\0'){ 
      char *flash[] PROGMEM = {rxByte};
     }

- How can I tell my ISR function, that I want to quit bootloader and go to start of sketch. Where is start of my new sketch and how can I jump there?

Thank you in advance

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No doubt, you know that there are many, many bootloaders in the public domain. You can learn from that source code.

All your questions are answered in others' shared work, and the app notes published by Atmel.

Since you ask how to store into flash memory, it points out that you need to read/learn before inventing the world's next bootloader!

PS: MOst all bootloaders don't use interrupts.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Will this work?

Your code is almost completely non-sensical. Why would you malloc 1 byte?!?

Also you can't just:

char *flash[] PROGMEM = {rxByte}; 

(which again is nonsensical) to write to flash. It depends on the C compiler you use but if avr-gcc have a read of this:

http://www.nongnu.org/avr-libc/u...

The boot_program_page example shown on that page makes the heart of a bootloader. As you can see it is NOT as simple as *flash_addr = byte!

Also hop over to the Tutorial Forum and read the Bootloader FAQ.

Judging by how little you seem to understand C I would suggest that a bootloader is probably way beyond your capabilities at this stage.

Oh and the 32U4 is a USB chip (the U in the name is a clue!) so why on earth would you implement a UART bootloader? Surely you would implement a CDC-ACM class device to do the bootloading and operate it over the chips own D+/D- pins not its TXD/RXD pins??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My problem is 32u4 it a USB chip microcontroller (as you mentioned clawson)
and the most bootloader out there is based to read from D+/D- and write in into flash memory! But I want to read from UART and write in into flash memory.
So instead of using USB (most bootloader for this microcontroller to that), I want to read from UART and save the hex/sketch file into flash memory!

That’s why I decide to write my own bootloader and why I cant use USB as new sketch receiver.

Have anyone suggestion to how I can change the bootloader from Arduino (Caterina) to get the sketch from UART and not USB!?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

arian88 wrote:

Have anyone suggestion to how I can change the bootloader from Arduino (Caterina) to get the sketch from UART and not USB!?

Simply replace the bootloader for USB that you have with a commonplace bootloader for serial/UART. As if the microprocessor was, say, a mega328P.

There are lots of those kind of bootloaders out there. Many in source code form. I'll bet you can find a pre-compiled that will match your UART and crystal. Like Optiboot.
Truly no need to reinvent a new bootloader.

You will need a way to flash the bootloader - typically using an Atmel MK-II ISP ($35 or so).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

You will need a way to flash the bootloader - typically using an Atmel MK-II ISP ($35 or so).

Or a USBAsp from ebay ($3).

If the UART data is coming from a PC you'll also need either a USB-RS232 cable plus a MAX232 (or equivalent) in the AVR circuit or get a USB-TTL cable and make a direct connection.

This does, once again, raise the question of why do it that way - the 32U4 presumably already has a perfectly adequate plain USB cable connection direct to the PC so I still don't wholly understand why you wouldn't use that (and a CDC-ACM class bootloader)?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Guys.
Thanks a lot.

- flash the boot is not a problem at all! I have different ISP programmers and know how to do that.

- why I want to send sketch through UART and not USB is because the I want to send sketch through Bluetooth, and the module that I use have just simple RX/TX.

- I have tried to find pre-compiled bootloaders that use UART (and not USB) and in the same way they are compatible with 32u4, but I didn’t find any thing.
For example, optiboot using UART but I don’t think its compatible with 32u4!
Caterina are compatible with 32u4 but using USB!

Hopefully you are now more familiar with my problem.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What do you think might change between a mega 328 and a 32u4? A bootloader tends to use minimal resources, so one would expect changes would be minimal. Much easier than writing it yourself.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

arian88 wrote:

- I have tried to find pre-compiled bootloaders that use UART (and not USB) and in the same way they are compatible with 32u4, but I didn’t find any thing.
For example, optiboot using UART but I don’t think its compatible with 32u4!

Look for a bootloader for the mega328P.

Does the bootloader need to initialize/pair/manage the BT radio?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I appreciate to you all answers guys.
I am totally I agree that the best solution is to just modify a bootloader.
But the question is which booloader and how to do that?

- As I mentioned I have tried to understand two bootloaders.
- Caterina who is original for Atmega32u4, but just using USB for getting of new sketch
- Optiboot that getting sketch from RX/TX but are not compatible with atmea32u4 (but are with Atmega328P / Arduino Uno)

I have already used atmega328P just for simple test, and everything works fine!
I used optiboot and I could just push new sketch from Bluetooth easily!
But the most bootloaders that I found and who are compatible with Atmega32u4, using USB and not RX/TX.

initialize/pair/manage is not a issue, because I can do that with direct communication between my computer (source of new sketch) and Bluetooth module.

By the way, is that any different between registers in atmega328P and atmega32u4?
Our they use exactly the same name on their registers

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
- Optiboot that getting sketch from RX/TX but are not compatible with atmea32u4 (but are with Atmega328P / Arduino Uno)

Do you know what the UART differences are between those two processors? ANd that the m32u4 HAS a UART?

I'm getting the feeling that this goal is too advanced for OP's current knowledge base.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

stevech wrote:
Quote:
- Optiboot that getting sketch from RX/TX but are not compatible with atmea32u4 (but are with Atmega328P / Arduino Uno)

Do you know what the UART differences are between those two processors? ANd that the m32u4 HAS a UART?

I'm getting the feeling that this goal is too advanced for OP's current knowledge base.

Don't get that feeling at all ;) and don't judge too early
Atmega32u4, have USART and I have already write functions to read and write from it! So I can connect it to my FTDI breadboard and have communication with atmega32u4 with putty on my laptop.
PD2 => RX
PD3 => TX

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You've tried a m328 with optiboot and it has worked? What is the problem of using optiboot for the 32u4? The modifications would be minimal methinks. The first step would be compiling it for the 32u4 and see what errors you get.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi everyone.

I have now modified optiboot bootloader!
There was some registers that I changed the names of them with respect to atmega32u4.
Most of them was for USART communication!
E.g. RX0 => RX1 etc.

After few hours, I can now compile the new bootloader without any error!
And I getting hex file out..
I have tried to give correct baud rate and CPU speed in each place that I saw.
And I can now push new bootloader into the microcontroller with avrdude.

But the problem now is, my computer doesn’t recognize the microcontroller anymore. When I connect it, my computer doesn’t recognize the board / USB anymore.

Some suggestions where the problem might be?
In issues like that, is that any typical place that I have to check one more time?

I will attach complier information...

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

well, you just killed off the usb boot loader and replaced it with optiboot. That was what you wanted wasn't it?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've written (moddified) a UART bootloader for 32U4 for wireless programming. You can use that.

Attachment(s):