NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Speed up the Nerdkit

September 02, 2010
by Rick_S
Rick_S's Avatar

I have to admit, I like the serial bootloader over ISP... even though I have both. However, I wanted to run my micro closer to it's rated 20MHz rather than the 14.7456MHz the Nerdkit is clocked at.

I realized that the reason this value was chosen was to maintain an error free standard baud rate over the serial port for programs requiring communications to the PC.

Then I came across the section in the data sheet that shows the margain of error for serial communications with various crystal speeds and noticed that an 18.432Mhz crystal also offered an error free communication.

I thought I'd pass the process along in case anyone else is interested.

WARNING: IF YOU HAVE NEVER FLASHED A BOOTLOADER OR USED AN ISP PROGRAMMER BEWARE --- I AM NOT RESPONSIBLE IF YOU MAKE YOUR BOOTLOADER UNRESPONSIVE. WHILE THIS WORKES GREAT FOR ME YMMV!

  1. You need to have an ISP programmer of some sort to re-program the bootloader on the chip AND know how to use it. I used a USBASP variant I got off ebay.
  2. You will need to modify 2 of the files in the bootloader folder and re-compile it.
  3. You will need to flash the modified bootloader to the chip.
  4. If you wish to use serial communications in your programs, you will need to modify the uart.c file in the libnerdkits folder.

In step 2, the files in the bootloader folder that need modified are:

config.mk uart.h

In config.mk, you need to make sure the settings for the ISP_DEV and ISP_PROG match that of your ISP programming device. You also need to change the frequency for F_CPU. In my case, these lines looked like this for my 328p micro.

ISP_DEV=usb
ISP_PROG=usbasp
AVRDUDE_BAUDRATE=115200
MCU=atmega328p
F_CPU=18432000

In uart.h, you have to add an elif for the new frequency. This is the section from my uart.h

/* define UBRR value (see datasheet */
#if (F_CPU == 16000000)
#define UART_UBRR 8 /* 16mhz, 115200 */
/* #define UART_UBRR 25 */ /* 16mhz, 38400 */
/* #define UART_UBRR 51 */ /* 16mhz, 19200 */
#elif (F_CPU == 20000000)
#define UART_UBRR 10 /* 20mhz, 115200 */
#elif (F_CPU == 8000000)
#define UART_UBRR 25 /* 8mhz, 19200 */
/* #define UART_UBRR 3 */ /* 8mhz, 115200 */
#elif (F_CPU == 14745600)
#define UART_UBRR 7 /* 14.7456mhz, 115200 */
#elif (F_CPU == 18432000)
#define UART_UBRR 9 /* 18.432mhz, 115200 */
#else
#error this cpu frequency is not supported by uart.h yet!
#error (see datasheet for UBRR value at 115200 baud)
#endif

Once this is done, you will need to re-compile the foodloader.hex and flash it to the chip.

At this point, the serial bootloader should work exactly as before only now with the 18.432MHz crystal (Note: the old crystal will no longer work with the bootloader at this point so do not do this without the new crystal in hand)

The only other change is to make sure UBRROL is set to 9 instead of 7 in the uart.c file in the libnerdkits folder.

void uart_init() {
  // set baud rate
  UBRR0H = 0;
  UBRR0L = 9;   // for 115200bps with 18.432MHz clock
  // enable uart RX and TX
  UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  // set 8N1 frame format
  UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);

  // set up STDIO handlers so you can use printf, etc
  fdevopen(&uart_putchar, &uart_getchar);
}

Now I made this post just before going to work... so I'm a bit rushed :D so please do not attempt in case you are 100% sure what you are doing.

Rick

September 03, 2010
by Rick_S
Rick_S's Avatar

Quick question for the guru's. Were the changes I made all that needed to be done to implement the new freqency crystal I used? It all seems to be working fine, I just want to make sure my instructions are OK in case anyone else wants to increase the speed by 25%.

Thanks,

Rick

September 03, 2010
by Ralphxyz
Ralphxyz's Avatar

Rick, you know you are becoming a guru.

And I sure appreciate it.

Ralph

September 03, 2010
by Rick_S
Rick_S's Avatar

Thanks Ralph for the compliment, however, there are many here more guru-ish than I. :D

May 18, 2011
by kle8309
kle8309's Avatar

If I just wanted to recompile the foodloader to get the foodloader.hex, do I need to make any change to the setting. Because when I try to make all I'm keep getting error about the device connection. But all I want is to compile and not upload. It is almost if I have to make the right hardware setting before it will compile.

Is there away around this. I have the stk500 usp programmer.

May 18, 2011
by Rick_S
Rick_S's Avatar

Did you make changes to the bootloader that you are wanting to compile? If so, you can just modify the make file so it doesn't do anything but compile, or just type the steps individually to build it. If you didn't change the bootloader, there is already a compiled hex in the folder.

Rick

May 18, 2011
by Ralphxyz
Ralphxyz's Avatar

There is a nice breakdown of the Makefile in the Nerdkit's Library.

Rick, I know your Makefile for the I2C Real Time clock uses "make" to just compile the code, which lines in your Makefile does that?

Also "make program" compiles and loads the mcu, which lines do that?

Ralph

May 18, 2011
by kle8309
kle8309's Avatar

Rick, can you please post your Makefile

so I guess we have to write our own makefile to compile to get foodloader.hex because this is all that is in the makefile:

AVRDUDEFLAGS=-c dapa -pm328p

all: fuses install

fuses:
    avrdude ${AVRDUDEFLAGS} -U lock:w:0x2f:m
    avrdude ${AVRDUDEFLAGS} -U efuse:w:0x05:m
    avrdude ${AVRDUDEFLAGS} -U hfuse:w:0xd2:m
    avrdude ${AVRDUDEFLAGS} -U lfuse:w:0xf7:m

install:
    avrdude ${AVRDUDEFLAGS} -U flash:w:foodloader.hex:a

Which basically just change the fuses and upload the hex file and not recompile it.

May 18, 2011
by kle8309
kle8309's Avatar

Oh, I found the makefile to compile it

it's the makefile.fl

all I have to do is change the name to makefile so programmer notepad can make.

I also have to take out the other upload makefile.

It made the .hex I wanted but also showed an error:

> "make.exe" all
avr-objcopy -O ihex -R .eeprom foodloader foodloader.hex
make -C launcher
make[1]: Entering directory `/cygdrive/c/Users/Kelvin/Desktop/MCU Resources/Code (2)/Code/bootloader328P/launcher'
gcc -Wall -Wstrict-prototypes -std=gnu99 -fno-inline -O2 -Werror    launch-bootloader.c   -o launch-bootloader
launch-bootloader.c: In function `main':
launch-bootloader.c:90: warning: implicit declaration of function `cfsetspeed'
make[1]: *** [launch-bootloader] Error 1
make[1]: Leaving directory `/cygdrive/c/Users/Kelvin/Desktop/MCU Resources/Code (2)/Code/bootloader328P/launcher'
make: *** [launcher] Error 2

> Process Exit Code: 2
> Time Taken: 00:01
May 18, 2011
by kle8309
kle8309's Avatar

Which header file contain the definition for cfsetispeed

I can't find it

May 18, 2011
by Rick_S
Rick_S's Avatar

That s the nerdkit makefile for installing the pre-compiled hex file they include. There is also a file called makefile.fl in that folder. Just rename Makefile to something else then rename Makefile.fl to Makefile. That makefile will compile the bootloader for you.

Rick

May 18, 2011
by Rick_S
Rick_S's Avatar

If I compile, I don't get an error using the default files. Did you change something?

May 19, 2011
by kle8309
kle8309's Avatar

I think I have changed the avr.mk and config.mk.

# Programmer used for In System Programming
ISP_PROG = dapa
# device the ISP programmer is connected to
ISP_DEV = /dev/parport0
# Programmer used for serial programming (using the bootloader)
SERIAL_PROG = avr109
# device the serial programmer is connected to
SERIAL_DEV = /dev/ttyS0

Let me try to compile w/o changing anything.

Rick, Did you comment out the SERIAL_DEV in the config.mk?

Also you mention about the changing the only config.mk above and the uart. So what about

the avr.mk settings? Will config.mk override the avr.mk

May 19, 2011
by Rick_S
Rick_S's Avatar

This isn't used for compiling, only for sending to the chip and then only if you are using a serial based programmer. But no, I didn't change any of that.

Rick

May 19, 2011
by kle8309
kle8309's Avatar

OK, I got it to work. I must have changed something when I messed around. I have downloaded the original NK code and compiled that. I wished there was a README file explaining the renaming steps

There are two ways to compile this:

Using terminal cmd

  make -f makefile.lf

Using programmer's notepad (I used)

change the makefile.fl to makefile (must also rename the other makefile to makefile.somethingelse). Then click make all.

the original makefile is just fuse/lock settings and uploading the foodloader.hex

I think it would be easier to understand if I change the uploading makefile

From

AVRDUDEFLAGS=-c dapa -pm328p

all: fuses install

fuses:
    avrdude ${AVRDUDEFLAGS} -U lock:w:0x2f:m
    avrdude ${AVRDUDEFLAGS} -U efuse:w:0x05:m
    avrdude ${AVRDUDEFLAGS} -U hfuse:w:0xd2:m
    avrdude ${AVRDUDEFLAGS} -U lfuse:w:0xf7:m

install:
    avrdude ${AVRDUDEFLAGS} -U flash:w:foodloader.hex:a

To

AVRDUDEFLAGS=-c stk500 -pm328p -P COM5

all: fuses install

fuses:
    avrdude ${AVRDUDEFLAGS} -U lock:w:0x2f:m
    avrdude ${AVRDUDEFLAGS} -U efuse:w:0x05:m
    avrdude ${AVRDUDEFLAGS} -U hfuse:w:0xd2:m
    avrdude ${AVRDUDEFLAGS} -U lfuse:w:0xf7:m

install:
    avrdude ${AVRDUDEFLAGS} -U flash:w:foodloader.hex:a
May 19, 2011
by Rick_S
Rick_S's Avatar

Glad to see you got it. Was the reason you needed to compile the source because you plan to make changes?

Rick

May 19, 2011
by kle8309
kle8309's Avatar

Yes Rick, I just bought 2 new 40 pin MCUs: Atmega644 and Atmega32

I want to have them work with the foodloader

But behind all of this, I want to drive the graphic lcd from allelectronics.com

I found a group of people working on the glcd but with Atmega644.

glcd project

May 20, 2011
by Rick_S
Rick_S's Avatar

That reminds me, I bought a 40pin atmega324p a while back and was going to do the same thing... Just never got around to it.

Rick

May 20, 2011
by Ralphxyz
Ralphxyz's Avatar

Kevin, keep us posted on whatever you do with the ATmega32. One came with my Dragon programmer so I have one to play around with also.

I also just picked up some ATmega64M1 mcus to see if I can do something with the OBD-II CAN bus.

It would be good to document loading the bootloader onto different mcus.

Ralph

Post a Reply

Please log in to post a reply.

Did you know that you can make a capacitive proximity sensor with some aluminum foil and paperclips? Learn more...