Bootloader Modifications for Pro Mini 8mhz

Hi guys! I searched around for a bit and didn’t find anything on this. I’m trying to reset my Pro Mini to accept a new program, but I’m not using the FTDI cable. I would like to increase the time the bootloader will wait for the new program on the UART pins. I originally tried using a watchdog timer, but while looking through the bootloader code, found there were a few lines that skip programming when the watchdog is used.

I guess these are my questions. Where in the bootloader is the value of time it will wait? I couldn’t find it. Also, in the bootloaders/atmega directory, there are a number of hex files and one c file. How do I create a hex that I know will run on my board? How do I select it in the IDE?

thanks!

Timeout is controlled by MAX_TIME_COUNT which is defined in the makefile (bootloader directory).

If you use Burn Bootloader from the IDE, the correct file gets built/uploaded based on your selection of board.

Awesome, thanks!

So I found the make file and changed:

'-DMAX_TIME_COUNT=F_CPU>>40' '-DNUM_LED_FLASHES=3'

When I upload, I get a successful program, but the board doesn't respond any differently and and the .hex file is labeled as being modified months ago (assume when this version was released). Any ideas?

BTW, I using 0017 on MAC OSX

Thanks!

Also, I noticed that 328P chips have bootloaders that run at 57600, even with 8 mhz chips. I was under the impression this baud rate had error issues? Why is this?

I think you need to either update the timestamp on the bootloader source file (open the source in a text editor and save) or delete the ".hex" file in order to force "make" to rebuild a new bootloader from the source. Otherwise it will assume no changes were made and just skip the rebuild. Apparently a change to the makefile only does not trigger a rebuild.

Thanks Ben, I will try re-saving the source file. I assume that is the .c file right? I did try removing the hex file, but I just got an error when I tried to reload it onto my board.

Ok, so I resaved the .c file an loaded the bootloader with no luck. Next I removed the .hex file and got this message:

avrdude: can't open input file /Users/wingnut87/Desktop/Arduino.app/Contents/Resources/Java/hardware/bootloaders/atmega/ATmegaBOOT_168_atmega328_pro_8MHz.hex: No such file or directory avrdude: write to file '/Users/wingnut87/Desktop/Arduino.app/Contents/Resources/Java/hardware/bootloaders/atmega/ATmegaBOOT_168_atmega328_pro_8MHz.hex' failed

Does it seem like avrdude tries to write a new file and can't? Thanks!

Your topic suggested to me that you were familiar with burning bootloaders. If this is not the case I suggest you read up on the topic first. A good start might be:

http://arduino.cc/en/Hacking/Bootloader?from=Main.Bootloader

Hey Ben, I am familiar with bootloaders to the extent that I know their purpose. I am familiar with some differences between the boards that are available. I also have a programmer and have successfully programmed bootloaders to chips many times.

This is the first time I have tried to change one. All I need is to change the baud rate and reprogram it. I just don't know how to generate a new hex file. It appears that burning the bootloader just takes the hex file in the directory and loads it, but does not apply any changes made to the make file.

What should I do to compile a new hex?

Thanks for your help so far!

Hi,

please look at this thread for compiling the boot loader: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1255016475/1#1

You have to add the following line to get a boot loader for the pro 8 Mhz with ATmega328: %DIRAVRUTIL%\make.exe atmega328_pro8

To increase the wait time, you have to change the Makefile in the opposite way. You are using "F_CPU>>40" = 8M / 2^40 which is exactly 0 in integer arithmetic, so there will be no wait time at all.

Try using something like '-DMAX_TIME_COUNT=F_CPU>>1' which means 8 times (2^3) longer than normal.

You can also use the WDT for the reset, but then you have to change the code a bit. Look at the following code lines:

#ifdef WATCHDOG_MODS
      ch = MCUSR;
      MCUSR = 0;

      WDTCSR |= _BV(WDCE) | _BV(WDE);
      WDTCSR = 0;

      // Check if the WDT was used to reset, in which case we dont bootload and skip straight to the code. woot.
      if (! (ch &  _BV(EXTRF))) // if its a not an external reset...
            app_start();  // skip bootloader
#else

You have to comment out the last two lines when you want to enter the bootloader using the WDT:

//if (! (ch &  _BV(EXTRF))) // if its a not an external reset...
//      app_start();  // skip bootloader

Also, I noticed that 328P chips have bootloaders that run at 57600, even with 8 mhz chips. I was under the impression this baud rate had error issues?

Whether this baud rate has issues, depends very much on the other side of the serial cable. What cable are you using?

To improve the baudrate calculation, you can change the baud-rate calculation in the bootloader to the formula of HardwareSerial which is more exact because of better rounding:

UBRR0H = ((F_CPU / 16 + BAUD_RATE / 2) / BAUD_RATE - 1) >> 8;
UBRR0L = ((F_CPU / 16 + BAUD_RATE / 2) / BAUD_RATE - 1);

MikeT

Hey thanks a ton MikeT! I will try it as soon as I get home. I thought it was funny that the guy you helped in the other post was wyngnut.

What I normally do is to use the makefile directly from a command prompt. This ads some challenges since you need to set up the PATH to the build tools correctly (probably why they added "Burn Bootloader" as an option from within the IDE). Also you need a valid reference to the avrdude config file and approriate defines for your programmer.

To deal with above I modified the makefile to reflect my specific configuration and also made it reference a copy of the bootfile source (MyBoot.c). If you want to go down this path you may get some inspiration from the following (shows the modified parts):

program name should not be changed...

PROGRAM = MyBoot_328

enter the parameters for the avrdude isp tool

ISPTOOL = stk500v2 ISPPORT = COM4 ISPSPEED = -b 115200 CFGFILE = d:\arduino\hardware\tools\avr\etc\avrdude.conf

ISPFUSES = avrdude -C $(CFGFILE) -c $(ISPTOOL) -p $(MCU_TARGET) -P $(ISPPORT) $(ISPSPEED) \ -e -u -U lock:w:0x3f:m -U efuse:w:0x$(EFUSE):m -U hfuse:w:0x$(HFUSE):m -U lfuse:w:0x$(LFUSE):m ISPFLASH = avrdude -C $(CFGFILE) -c $(ISPTOOL) -p $(MCU_TARGET) -P $(ISPPORT) $(ISPSPEED) \ -U flash:w:$(PROGRAM)_$(TARGET).hex -U lock:w:0x0f:m

To build I use the connand:

make atmega328_pro8

and to upload

make atmega328_pro8_isp

Can I use this way on ATMEGA32L-8PU (8MHz) ? I have some ones here... :)

Mike T, thanks again for your help! I was able to make a script to compile the bootloader for the pro 8. Now I would like to tweak the loader for the Arduino BT, but there is now makefile, just the c file. Is there a way to compile this? Thanks!

Hi wingnut87,

Now I would like to tweak the loader for the Arduino BT, but there is now makefile

just copy the Makefile from the "atmega" directory and insert the following section (just before "isp: $(TARGET)" line):

bt: TARGET = bt
bt: CFLAGS += '-DMAX_TIME_COUNT=F_CPU>>1' '-DNUM_LED_FLASHES=3' -DBAUD_RATE=115200L '-DARDUINO_BT' '-DWATCHDOG_MODS'
bt: AVR_FREQ = 16000000L 
bt: $(PROGRAM)_bt.hex

bt_isp: bt
bt_isp: TARGET = bt
bt_isp: HFUSE = DD
bt_isp: LFUSE = FF
bt_isp: EFUSE = 00
bt_isp: isp

To compile the bootloader use

make bt

I modified the wait time before leaving the bootloader and starting the sketch (-DMAX_TIME_COUNT=F_CPU>>1) and enabled the watchdog timer support (-DWATCHDOG_MODS).

Theoretically you can use the "bt_isp" target to program the ATmega, but I never tried this because I use a different programmer.

MikeT

Hey Mike, I was able to generate a hex file, but when I tried to burn the bootloader through the IDE, I got this message:

avrdude: ERROR: address 0x4010 out of range at line 129 of /Applications/Arduino.app/Contents/Resources/Java/hardware/bootloaders/bt/ATmegaBOOT_168.hex avrdude: write to file '/Applications/Arduino.app/Contents/Resources/Java/hardware/bootloaders/bt/ATmegaBOOT_168.hex' failed

I also tried compiling the c file with avr studio. It uploaded to the chip, but does not seem to do anything. Led does not blink. Any ideas?

Thanks!

Hi wingnut87,

the ATmega168 on the Arduino-BT has 16kB flash and 2kB boot loader section (when fuses BOOTSZ0=0 and BOOTSZ1=0) at the end, starting at 0x3800. It seems your bootloader is some bytes too large, the maximum address must be less than 0x4000.

Have you used the original bootloader or did you modify it?

You can omit the following code from the boot loader, because it is not really needed:

   //message("SET BT PAGEMODE 3 2000 1");    
putch('S');
putch('E');
putch('T');
putch(' ');
putch('B');
putch('T');
putch(' ');
putch('P');
putch('A');
putch('G');
putch('E');
putch('M');
putch('O');
putch('D');
putch('E');
putch(' ');
putch('3');
putch(' ');
putch('2');
putch('0');
putch('0');
putch('0');
putch(' ');
putch('1');
putch(0x0D);

  
        //put_s("SET BT ROLE 0 f 7d00");  
      putch('S');
      putch('E');
      putch('T');
      putch(' ');
      putch('B');
      putch('T');
      putch(' ');
      putch('R');
      putch('O');
      putch('L');
      putch('E');
      putch(' ');
      putch('0');
      putch(' ');
      putch('f');
      putch(' ');
      putch('7');
      putch('d');
      putch('0');
      putch('0');
      putch(0x0D);

These commands make sure, that some settings of the WT-11 are restored to usable values.

If you need this feature, you better send the following commands once to the WT-11 in command mode (refer also to http://www.arduino.cc/en/Main/ArduinoBTInitializationSketch):

Serial.println(" SET CONTROL BIND 1 20 RISE SET BT AUTH * 12345");
  Serial.println(" SET CONTROL BIND 2 20 RISE SET CONTROL BAUD 115200,8n1");
  Serial.println(" SET CONTROL BIND 3 20 RISE SET BT PAGEMODE 3 2000 1");
  Serial.println(" SET CONTROL BIND 4 20 RISE SET BT ROLE 0 f 7d00");
  Serial.println(" SET CONTROL BIND 5 20 RISE SET CONTROL ECHO 0");
  Serial.println(" SET CONTROL BIND 6 20 RISE SET CONTROL ESCAPE - 00 1");
  Serial.println(" SET CONTROL BIND 7 20 RISE SET CONTROL CONFIG 102D");

To activate this commands, you need to apply LOW->HIGH edge (3,3V) at GPIO pin 5 of the WT-11 (pin 9 of connector SV2).

MikeT

Hey Mike, I was able to get everything working. Now I'm just wondering why it works. I basically have a circuit similar to the arduino bt, but am running it at 8mhz for power reasons. I ended up using a pro mini 168 8mhz bootloader. I read through the bootloader and makefile and it should be running at 19200. Then my program is running at 19200 for serial communication. The weird thing is that is only works if my bluetooth module is configured to 115200 communication. Why does this work, and only work this way? I can communicate with my program via bluetooth, (i.e. serial commands control leds), but the arduino serial window is set at 19200. I'm confused :(

thanks so much for your help so far!

Hi wingnut87,

the baudrate of UART interface of the WT-11 bluetooth chip is totally independent of the transmission speed of the Bluetooth interface. The same is true for the baudrate of virtual Bluetooth COM ports on the PC, this means the PC baudrate is not relevant for this type of ports.

Which program do you mean with

Then my program is running at 19200 for serial communication.

the program on the ATmega or on the PC side?

The weird thing is that is only works if my bluetooth module is configured to 115200 communication

Which Bluetooth module do you mean? A) the Bluetooth module connected to the ATmega B) an extra Bluetooth module connected to a UART COM port of you PC C) an extra Bluetooth module connected to a USB COM port of you PC (e.g. via FTDI) D) a Bluetooth (USB) dongle

MikeT