LowPowerLab Forum

Hardware support => Moteino => Topic started by: LukaQ on March 08, 2018, 04:14:35 AM

Title: Faster sketch upload speed
Post by: LukaQ on March 08, 2018, 04:14:35 AM
Hey,

Are you tired of waiting for sketch to upload? Does it take far too long for your taste? Have better things to do?
Then I have solution for you!

(If you ever watched any top shop or how is it call elsewhere in the word, TV channel  ;D)


I've been learning a lot on Moteinos and in the process (if someone is interested of doing the same) I found that I can make Moteino start up faster (I won't talk about it here, but you can boot moteino or 328p ~60ms faster on power-up) or how to use pickit2 to upload bootloader from Arduino IDE and how to upload sketches with 500k baud rate instead of standard 115.2k or even slower in case of pro mini's. Difference between those two are 5.49s vs 2.72s without compiling time for WeatherNode. But this will mean you will have to upload new bootloader to 328p. (no big deal, just like sketch, only with external programmer)

All you have to do is make changes to:
Makefile (you'll have it in folder, where you will compile your new bootloader(you can find this tutorial in other pinned thread I made))

Find this line
atmega328: CFLAGS += $(COMMON_OPTIONS)
Below it add this line
atmega328: CFLAGS += '-DLED_START_FLASHES=2' '-DBAUD_RATE=500000'
Other file you need to edit is Optiboot.c from same folder

Find line that contains
#if F_CPU >= 8000000L
Select it and replace it with
#if F_CPU >= 16000000L
#define BAUD_RATE   500000L
#elsif F_CPU >= 8000000L


Last change needs to happen in boards.txt of Moteino framework, which is located in

C:\Users\YOUR_USERNAME\AppData\Local\Arduino15\packages\Moteino\hardware\avr\1.3.0

This 1.3.0 might in the future change, but you will be able to find this with the latest folder name

inside find this line

Moteino.upload.speed=115200

and replace it with

#Moteino.upload.speed=115200
Moteino.upload.speed=500000


Compile bootloader and upload it to 328p.
This will allow for standard Moteino to upload sketches at 500k baud rate. I could go even to 1M, 328p is ok with that, but my usb to serial does not support 1M, it stops at 921.6k

DISCLAIMER: test if everything works, from basic stuff to delays and such
Title: Re: Faster sketch upload speed
Post by: Felix on March 08, 2018, 09:17:50 AM
Hi Luka :)

This is good info!

There are reasons why all the bootloaders have limits of 115200 baud at 16mhz though. In optiboot and dualoptiboot there is an equation that calculates the reliability of the baud rate.
Maybe 500k can work but it's much much higher than 115200, for less than 16mhz all stock bootloaders ive seen work at 57600 or less (for AVR).

Maybe a hardware switch to tell the bootloader to switch to high speed (500k) would be a safer approach?
That way the bootloader works at stock speed, but you can switch to high speed if you'd like, and then use a separate target board in Tools>Boards
Title: Re: Faster sketch upload speed
Post by: LukaQ on March 08, 2018, 10:26:15 AM
Quote from: Felix on March 08, 2018, 09:17:50 AM
Hi Luka :)

This is good info!

There are reasons why all the bootloaders have limits of 115200 baud at 16mhz though. In optiboot and dualoptiboot there is an equation that calculates the reliability of the baud rate.
Maybe 500k can work but it's much much higher than 115200, for less than 16mhz all stock bootloaders ive seen work at 57600 or less (for AVR).

Maybe a hardware switch to tell the bootloader to switch to high speed (500k) would be a safer approach?
That way the bootloader works at stock speed, but you can switch to high speed if you'd like, and then use a separate target board in Tools>Boards
Looking at 328p datasheet, having BR @ 115.2k has 3.5% error, even 57.6k has error of 2.1%. While 250k, 0.5M and 1M don't have error at all. This rates are (And I can't be sure, don't know that much history, from way back then, were this kinda speeds were problem on hardware/software level) not a problem there days. And not only on 16MHz boards, even slower ones also. 8MHz can still do much faster than stock speeds, 250k and 0.5M.
Even Arduino IDE has now nano with newer bootloader, which runs (still) at 115.2k AND older bootloader which was half that (what it always was). Still whole system is still set to 115.2k or below, you do get warnings from avrdude, if you have higher speed than that.

Quote from: Felix on March 08, 2018, 09:17:50 AM
Maybe a hardware switch to tell the bootloader to switch to high speed (500k) would be a safer approach?
That way the bootloader works at stock speed, but you can switch to high speed if you'd like, and then use a separate target board in Tools>Boards
Perhaps, but I do have to say, I don't see why not use it as default speed, avr supports it, avrdude does also (it's not without warning, that you have higher speed then 115.2k).

Also note, that optiboot 6.3 removed 230.4k speed, since it also has few % error. I mean you can't divide 8M or 16M by 115200 or 230400 without errors.


Mind you, this is still not the fastest UART can go, since you have
The transfer rate can be doubled by setting the U2Xn bit in UCSRnA. Setting this bit only has effect for
the asynchronous operation. Set this bit to zero when using synchronous operation.
Title: Re: Faster sketch upload speed
Post by: LukaQ on March 08, 2018, 10:29:57 AM
Quote from: TomWS on March 08, 2018, 10:10:22 AM
I think LukaQ is suggesting using a pickit as the programmer to get these higher speeds.   So it's not a UART protocol with the same baud rate limitation (I think).  I haven't explored using pickit, although I've heard about it.
Not at all, in my case, I use it just to upload bootloader, you could use something else, even another arduino. After that, it's all as it was(is). USB to serial. So it still is UART. You don't need external programer for sketches. If you have use, just plug USB cable and you can program it. Or if you don't have usb to serial on board, something like FTDI converter.