[SOLVED] Bootloader for ATmega8 with internal oscillator doesn't work.

A standalone ATmega8 with internal oscillator at 8MHz can't communicate with avrdude to upload a sketch.

Everything else works:

  • De standard bootloader with external crystal of 16MHz works. I can upload sketches without problem
  • The reset, TX and RX are fine. I can upload a sketch with a programmer when using the internal 8MHz (with the 8MHz ATmega8 in boards.txt). So I tested the Serial communication and the reset and it works without problem.

I tried this: http://www.robertoinzerillo.com/wordpress/?p=45
And I made my own Optiboot 5.0 bootloader for the ATmega8 for internal oscillator.
Optiboot: Google Code Archive - Long-term storage for Google Code Project Hosting.
Both have the same result, when trying to upload a sketch, the board is not detected at all.

I would like to make a few minimal circuits, which have a serial connector to upload a sketch and I2C to communicate with others.

Does anyone have the Optiboot working for the ATmega8 with internal oscillator at 8MHz ?

At what speed? 115200 is apparently unlikely to work at 8mhz; I tend toward recommending the stock 16M/115200 optiboot, configured in boards.txt to upload at 57600...

I tried also 9600 baud for the sketch upload, but I have always the same result. It is the same respons if I don't connect the board.

I am able to upload with an ATmega8L @ 8MHz internal, and baud 57.6K with Optibootv5.0.

I used these settings for optiboot:

make atmega8 AVR_FREQ=8000000L BAUD_RATE=57600 UART=0 HFUSE=0xD4 LFUSE=0xE4 LED=B5 LED_START_FLASHES=3

I also tried optiboot at 9600 baud and it uploads fine.

Thanks!
That was what I was looking for. I will try it later this day.

Thanks again hiduino,
I have it working after some time. The explanation is below in the definition for boards.txt

##############################################################
#
#  Optiboot 5.0 for ATmega8 with internal oscillator at 8MHz
#  With options by user hiduino
#  http://forum.arduino.cc/index.php?topic=170157.0
#  Build in Arduino 1.0.5 Windows environment with omake:
#    make atmega8 AVR_FREQ=8000000L BAUD_RATE=57600 UART=0 HFUSE=0xD4 LFUSE=0xE4 LED=B5 LED_START_FLASHES=3
#  Afterwards renamed to optiboot_atmega8_osc8.hex
#  The protocol=arduino works, using stk500 didn't work.
#
atmega8_osc8.name=[Optiboot] ATmega8 osc 8MHz
atmega8_osc8.upload.protocol=arduino
atmega8_osc8.upload.maximum_size=7680
atmega8_osc8.upload.speed=57600
atmega8_osc8.bootloader.low_fuses=0xe4
atmega8_osc8.bootloader.high_fuses=0xd4
atmega8_osc8.bootloader.path=optiboot
atmega8_osc8.bootloader.file=optiboot_atmega8_osc8.hex
atmega8_osc8.bootloader.unlock_bits=0x3F
atmega8_osc8.bootloader.lock_bits=0x0F
atmega8_osc8.build.mcu=atmega8
atmega8_osc8.build.f_cpu=8000000L
atmega8_osc8.build.core=arduino:arduino
atmega8_osc8.build.variant=arduino:standard
#
##############################################################

Since it runs at 8MHz, I might just as well buy some ATmega8L, with the extra advantage that it will also run at 3V. The signature of both is the same anyway.

When the internal oscillator is used, the two pins for the X-tal can be used as digital pins.

They don't go well with the Arduino library. I made macros that do almost the same as the Arduino functions.

// Macros for the two extra pins if the X-tal is not used.
// With the macros the library don't have to be changed.
// These macros are (somewhat) compatible with the Arduino functions.
// They can only be used with ATmega8, ATmega168, ATmega328P.
// _1X = PB6 = ATmega pin 9  = the pin near the top
// _2X = PB7 = ATmega pin 10 = the pin near the bottom
// It is assumed that HIGH is 1 and LOW is 0.
// If they get mixed with the Arduino functions, any output could change.
// So it is dangerous to use.
#define _1X 101
#define _2X 102
#define _digitalWriteX(a,b) bitWrite( PORTB, (a) == _1X ? 6 : 7, (b))
#define _digitalReadX(a) bitRead( PINB, (a) == _1X ? 6 : 7)
#define _pinModeX(a,b) if ( (b) == INPUT) { bitClear( DDRB, (a) == _1X ? 6 : 7); bitClear( PORTB, (a) == _1X ? 6 : 7); } \
  else if ((b) == INPUT_PULLUP) { bitClear( DDRB, (a) == _1X ? 6 : 7); bitSet( PORTB, (a) == _1X ? 6 : 7); } \
  else if ((b) == OUTPUT) { bitSet( DDRB, (a) == _1X ? 6 : 7); }

...

// Code example
_pinModeX( _1X, INPUT_PULLUP); // set as input with pull-up
_pinModeX( _2X, OUTPUT);           // set as output

...

_digitalWriteX( _2X, HIGH);       // pin high
delay(300);
_digitalWriteX( _2X, LOW);        // pin low
delay(300);

int i = _digitalReadX( _1X);       // read input
Serial.println( i);

You can make a variant and add the two pins to a \variant\morepins\pins_arduino.h.

const uint8_t PROGMEM digital_pin_to_port_PGM[] = {
	PD, /* 0 */
	PD,
	PD,
	PD,
	PD,
	PD,
	PD,
	PD,
	PB, /* 8 */
	PB,
	PB,
	PB,
	PB,
	PB,
	PC, /* 14 */
	PC,
	PC,
	PC,
	PC,
	PC,
	PB,  /* added digital pin 20 (PB6, XTAL1) */
	PB,  /* added digital pin 21 (PB7, XTAL2) */
};

const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = {
	_BV(0), /* 0, port D */
	_BV(1),
	_BV(2),
	_BV(3),
	_BV(4),
	_BV(5),
	_BV(6),
	_BV(7),
	_BV(0), /* 8, port B */
	_BV(1),
	_BV(2),
	_BV(3),
	_BV(4),
	_BV(5),
	_BV(0), /* 14, port C */
	_BV(1),
	_BV(2),
	_BV(3),
	_BV(4),
	_BV(5),
	_BV(6),  /* added digital pin 20 (PB6, XTAL1) */
	_BV(7),  /* added digital pin 21 (PB7, XTAL2) */
};

Works well for arduino library calls:

  digitalWrite(20, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(20, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second

If I change the library, I have to remember to change it every time with a new Arduino version.
I didn't check the library what would happen if a sketch is using 'A0' (to be used as a digital pin).

I modified my post and named them "_...X", hopefully that distinguishes them enough from the normal library functions.

No, I create the boards.txt and variants in the sketch hardwares folder. That way it's easy to upgrade the IDE without worrying about your changes. I don't ever touch the IDE folders. That goes the same for adding various cores, arduino-tiny, mighty-1284p, ...

So I create in the sketches folder;

\My Documents\Arduino\hardware\Arduino\boards.txt
\My Documents\Arduino\hardware\Arduino\variants\morepins\pins_arduino.h
\My Documents\Arduino\hardware\Arduino\bootloader\optiboot\optiboot_atmega328.hex

boards.txt:

##############################################################

atmega328bb2.name=ATmega328(8 MHz internal clock, D20,D21)

atmega328bb2.upload.protocol=arduino
atmega328bb2.upload.maximum_size=30720
atmega328bb2.upload.speed=57600

atmega328bb2.bootloader.low_fuses=0xE2
atmega328bb2.bootloader.high_fuses=0xD6
atmega328bb2.bootloader.extended_fuses=0x05
atmega328bb2.bootloader.path=optiboot
atmega328bb2.bootloader.file=optiboot_atmega328.hex
atmega328bb2.bootloader.unlock_bits=0x3F
atmega328bb2.bootloader.lock_bits=0x0F

atmega328bb2.build.mcu=atmega328p
atmega328bb2.build.f_cpu=8000000L
atmega328bb2.build.core=arduino:arduino
atmega328bb2.build.variant=morepins

Thanks, I didn't even know that was possible.

theoretically high baud with 8mhz crystal is marginal at best due to divisor error. even 16mhz is iffy. strangely ive had pretty much perfect success using the internal osc at high baud. i also find using the same boot code for 8mhz and 16mhz makes sense. i just use 57kbps for former and 115kbps for latter no problemo. for excellent stability/accuracy poking a half dozen bytes of code to read ee and poke osccal reg has given far more reliable results than using a crystal.

I tried using the osccal once, but I think the value depends on the temperature.

I read that avrdude has some kind of oscillator tuning. Do you know how to use that ?

As for the baudrate, if a clock is 10% off, a high baudrate will be 10% off, and also a low baudrate will be 10% off. Perhaps I have it wrong, but 10% is 10% no matter if the baudrate is high or low.

ive never used avrdude calibration but instead have a small routine that pokes ee based on start pulse of cr key. then every app from then on reads ee and puts it in the osccal reg. error always fraction of a percent regardless of voltage or temperature. with crystal youre lucky to get %4.

optiboot for atmega8 is not [solved] Most of the time it locks you out with lock byte E4 when using Low E4 & Hi DC. Strangely enough, it puts the Low E4 into the lock byte, rendering the chip un-bootloaderable

I should have said the problem is in optiloader.

[optiloader] for atmega8 is not [solved] Most of the time it locks you out with lock byte E4 when using Low E4 & Hi DC. Strangely enough, it puts the Low E4 into the lock byte, rendering the chip un-bootloaderable

Huh? Where did you put the E4/DC? What does it say during programming?
Optiloader doesn't pay any attention to boards.txt; all of it's fuse values are internal. And it will always configure for external crystal unless you've modified the sketch code.

I first used optiloader with with your BF CC for crystal 8mHz and it flashed it ok, and I hooked up 8mHz to the chip and the board detector confirmed it was OK, but I could not upload sketches to it. Then E4 DC was substituted for BF CC in both boards.txt and optiloader. like so:
image_t PROGMEM image_8 = {
{
"optiboot_atmega8.hex" }
,
{
"atmega8" }
,
0x9307, /* Signature bytes for 8 */
{
0x3F, 0xE4, 0xDC, 0, 0 }
,
{
0x2F, 0xE4, 0xDC, 0, 0 }
,
64,
For Internal 8 mHz. The first chip seemed to be bootloaded ok, according to Atmega_board_detector but I tried everything in the world to download a sketch using FTDI. I changed baud rate in boards.txt to upload.speed=57600, first used protocol=stk500 then tried protocol=arduino; tried everything I could find with Google. I then tried optiboot again on the chip (don't remember why, my brain is fogged from 6 hours of trying to get it to work.) All the fuses, etc. were the same in optiloader but now the board detector says I'm locked out with lock byte = E4, the same old thing that happened a month ago with my first 5 chips. I'ts always E4, the low byte. I was locked out and had to try a new chip and it did the same thing, tried 2 more... always lock = E4. I now have NINE atmega8 with lock byte E4. I have just now ordered 5 more of them.

Have you ever actually tried to get a bootloader in a atmega8 using optiloader for a internal 8 mHz RC osc? If you can get it to work, I will give you 9 chips with lock byte = E4 and if you can reveal to me how you managed to do it, I will even give you 5 brand new virgin mega8s.

Have you ever actually tried to get a bootloader in a atmega8 using optiloader for a internal 8 mHz RC osc?

No, actually not. I supposed I could give it a try :slight_smile:

I tried loading a sketch using SPI to one of the chips that had lock byte E4 and then used the board detector on it. It showed the bootloader area was erased and the lock byte was now FF. I didn't think SPI could erase it with lock byte E4. Either I misread the Atmel data (most likely) or the board detector is lying to me (;>(
I again tried optiloader on the chip and once again board detector shows E4.

Optiloader always thinks things went well, but it doesn't read back these bytes after it thinks it wrote to them. It must be locked out of updating this byte by the time it is tidying things up. I am trying to find a way to put some debug lines in there, but have not yet figured out how to read the lock byte back. I'm sure you could tell me since you're the author and that would save me a lot of time.