problems with the fuses on an atmega8

Go To Last Post
61 posts / 0 new

Pages

Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everybody.

 

I´m a newbye in AVR programming. I built an Usb programmer using Thomas Fischl website´s info, and because I start with an atmega8 chip with no bootloader, I need to program the chip. I don´t have another programmer so I decided to use my arduino diecimila as ISP programmer. Ok. Searching the web I discovered Avrdude. Using Avrdude and arduino I can now flash a file into the atmega, and read the fuses and the lock byte, I´ve done it with good results (or at least is what AVRdude says) but everytime I try to WRITE on the fuses or write on the lock byte AVRdude fails and skips this task. I´m searching the web and learning about AVR, but by now I have no found the the reason for this behaviour. The system seems to be working all right in all the other tasks.

 

Thanks in advance.

Regards.

This topic has a solution.

Last Edited: Sun. Nov 23, 2014 - 02:11 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
AVRdude fails and skips this task
How exactly?  Post the the command line for and output from avrdude.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Hello, thanks for your help, here is the command line

Regards.

 

D:\1>avrdude -c arduino -p m168 -P com9 -b 19200 -u -U flash:w:usbasp.atmega8.20

11-05-28.hex -U lock:w:0xFF:m

 

avrdude: AVR device initialized and ready to accept instructions

 

Reading | ################################################## | 100% 0.02s

 

avrdude: Device signature = 0x1e9406

avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed

 

         To disable this feature, specify the -D option.

avrdude: erasing chip

avrdude: reading input file "usbasp.atmega8.2011-05-28.hex"

avrdude: input file usbasp.atmega8.2011-05-28.hex auto detected as Intel Hex

avrdude: writing flash (4700 bytes):

 

Writing | ################################################## | 100% 3.55s

 

avrdude: 4700 bytes of flash written

avrdude: verifying flash memory against usbasp.atmega8.2011-05-28.hex:

avrdude: load data flash data from input file usbasp.atmega8.2011-05-28.hex:

avrdude: input file usbasp.atmega8.2011-05-28.hex auto detected as Intel Hex

avrdude: input file usbasp.atmega8.2011-05-28.hex contains 4700 bytes

avrdude: reading on-chip flash data:

 

Reading | ################################################## | 100% 3.54s

 

avrdude: verifying ...

avrdude: 4700 bytes of flash verified

avrdude: reading input file "0xFF"

avrdude: writing lock (1 bytes):

 

Writing |                                                    | 0% 0.00s ***faile

d;

Writing | ################################################## | 100% 0.13s

 

avrdude: 1 bytes of lock written

avrdude: verifying lock memory against 0xFF:

avrdude: load data lock data from input file 0xFF:

avrdude: input file 0xFF contains 1 bytes

avrdude: reading on-chip lock data:

 

Reading | ################################################## | 100% 0.02s

 

avrdude: verifying ...

avrdude: verification error, first mismatch at byte 0x0000

         0xff != 0x00

avrdude: verification error; content mismatch

 

avrdude done.  Thank you.

 

 

D:\1>

Last Edited: Sat. Nov 22, 2014 - 11:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your .hex file has a name which suggests to me it was built for an ATmega8.  Is this so?  If it is, you should be aware that the ATmega8 is not a drop-in replacement for the ATmega88 and friends.  Interrupt vectors are different, I/O register address are different, many other differences.  Have a look at the migration note:

http://www.atmel.com/images/doc2553.pdf

 

Why are you trying to write the lock bits to 0xFF?  That leaves them unlocked, which is their default state after a chip erase anyway.

 

As for the verification errors on the lock bits, what version of avrdude are you using?  Can you post (attach) the avrdude.conf file?

 

I assume you're passing -u to avrdude because safe mode was failing due to fuse read errors?  Otherwise the use of -u is ill-advised.  Once you solve the problem I'd suggest dropping it.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Sun. Nov 23, 2014 - 01:40 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I´m following theese instructions

 

http://www.fischl.de/usbasp/

 

http://www.fischl.de/usbasp/Read...

 

It is posible to build the programmer with an atmega88, as well as with an atmega8. I chose to do it with the atmega8. Because the name of the .hex file I´m almost sure that this file is specific for the atmega8. I was trying to write 0xFF on the lock bits just trying to find if this maybe could be this the problem that makes I can´t write the fuses. Acording to the given instructions I have to write 0xc9 on Hfuse and 0xef on lfuse. When I try to write on the fuses bits, Avr seems to fail just like the lock bits. My Avrdude version is 5.10

 

I attach the avrdude.conf file (I added .txt to the filename in order to being able to upload it to this forum)

 

Thankyou for your help.

Regards.

Attachment(s): 

Last Edited: Sun. Nov 23, 2014 - 02:10 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The command line you've constructed for avrdude include the switch -p m168 which specifies a the target device as the ATmega168.  The signature read by avrdude is 0x1e9406, which is the correct signature for an ATmega168.  This forces to me to conclude that you've attached an ATmega168 to your ArduinoISP programmer.

 

But you are programming a .hex file built for the ATmega8.  If it works it's entirely by accident.

 

If you must program onto the 168, the better bet would be usbasp.atmega88.2011-05-28.hex, since the ATmega88 and the ATmega168 are essentially the same device, except for flash size, interrupt vector size, and JMP/CALL instructions.  However that won't work because the usbasp code makes use of the INT0 external interrupt.  With the differently sized interrupt vectors, the 88 .hex file won't work properly on the 168.  Also the 88 firmware makes use of at least one RJMP instruction which is meant to wrap 'backwards'.  This works on a device with 8KB of flash, but will reach the wrong address on a device with 16KB of flash.

 

You'll need to rebuild the .hex file for the 168 (and probably will need to massage the source code a bit for I/O register and bit name difference [see the migration app note I linked above]) or just go out and buy an ATmega8 or ATmega88.

 

None of this explains the lock byte verify failure.  I don't see a problem with the part definition for the m168 in your avrdude.conf file.

 

Maybe someone else will have an idea.

 

In the meantime you've got the ArduinoISP you can use as a programmer.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

If you want to use the ArduinoISP sketch running on your (meg168) Deciemillia,  

1. connect the 6 ISP wires from your Arduino to the target mega8.    (and the heartbeat LED if you like)

2. upload and run the ArduinoISP sketch.

3. then type:

avrdude -c stk500v1 -p m8 -P com9 -b 19200 -U flash:w:usbasp.atmega8.2011-05-28.hex

Quite honestly,   if you 'built' the usbasp.hex from the Fischl website,   you would be able to configure the Makefile to do this for you.

 

Note that you can buy a ready-made Chinese USBASP dongle for about $3.60 (free postage)

 

I use Duemilanove or UNO boards for 'anything'.     If I want to upload a non-Arduino hex file via the bootloader,  I say:

avrdude -c arduino -p m328p -P com17 -b 57600 -U flash:w:duemilanove.hex

The Uno can upload at 115200 baud:

avrdude -c arduino -p m328p -P com20 -b 115200 -U flash:w:uno.hex

 

David.

Last Edited: Sun. Nov 23, 2014 - 08:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK. first of all sorry, I´m a very newbye, I´ve been only 2 days learning AVR programming.

 

It seems that I was confused about the -p m168 switch. I used it in order to specify the AVR programmer chip, that is an error because the switch "-p" is used to specify the TARGET AVR.

 

I´m using an arduino diecimila as programmer, that has an atmega168 inside, and this was the reason for that "-p m168 switch", but the chip I need to program, and the chip I have is an atmega8, and it´s placed on the programmer I built.

 

David, I didn´t buid the usbasp.hex file, I built the usb programmer described in Fischl website, I´m talking about the physical object, I made the PCB and soldered the components in. I have no idea about programing languages, except for a little bit of arduino, and a little bit of basic, that I used 20 years ago on my commodore64. 

 

I used 19200 bauds communication because I leave untouched the arduino sketch that I´m using (I´m talking about the arduinoISP sketch, located in the arduino examples library). When I tried to change the baud rate all the system fails, I don´t know the reason.

 

by now, when I try to change the "-p m168" switch to a "-p m8" the system fails at signature reading...

 

D:\1>avrdude -c arduino -p m8 -P com9 -b 19200 -u -U flash:w:usbasp.atmega8.2011

-05-28.hex -U lock:w:0xFF:m

 

avrdude: AVR device initialized and ready to accept instructions

 

Reading | ################################################## | 100% 0.02s

 

avrdude: Device signature = 0x1e9406

avrdude: Expected signature for ATMEGA8 is 1E 93 07

         Double check chip, or use -F to override this check.

 

avrdude done.  Thank you.

 

 

D:\1>

 

 

OK, I´m going to do some more tests...I think that overriding this point is not the solution.

 

Thank u guys for your help an patience, I´ll post again later.

 

 

 

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

You talk to the mega168 on your diecimila with "avrdude -c arduino -p m168" (bootloader)

You talk to the mega8 on your target pcb with "avrdude -c stk500v1 -p m8" (ArduinoISP sketch)

 

There are many ArduinoISP tutorials with pictures.    Note that you are programming an external chip.  

 

avrdude can tell the difference between bootloader and ArduinoISP by the fact that you use different baudrates.

 

Why are you building a home-made USBASP.    It will cost more than buying a Chinese one!

 

David.

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

david.prentice wrote:
You talk to the mega168 on your diecimila with "avrdude -c arduino -p m168" (bootloader)

You talk to the mega8 on your target pcb with "avrdude -c stk500v1 -p m8" (ArduinoISP sketch)

Very good.  I'd missed that... (I have to stop spending time on the forum after drinking sleepy-time tea ;) )

 

This also explains the problem with programming lock bits and fuses.  With your avrdude command line, avrdude was programming the UNO, not the target.  It was doing so using the bootloader on the UNO (called Optiboot).  Bootloaders are (generally) unable to alter fuses and lock bits.

 

Follow David's advice:

avrdude -c stk500v1 -p m8 -P com9 -b 19200 -U flash:w:usbasp.atmega8.2011-05-28.hex -U hfuse:w:0xc9:m -U lfuse:w:0xff:m

Your problems should vanish ;)

 

Note how I've changed the low fuse from 0xef to 0xff.  Honestly I'm not certain why they recommend 0xef.  It amounts to a shorter start-up time (4 ms instead of 65).  Longer startup times are always a safer bet unless the app demands it, and a programmer certainly doesn't.

 

Mind you be sure that you have assembled your usbasp board correctly, notably that the crystal and caps are properly installed.  Once you successfully program the fuses into the ATmega8 it will no longer be possible to reprogram it without a functioning crystal oscillator.

 

Should this fail for some reason, all is not lost.  There are a number of ways to recover from a 'fuse accident', but let's not worry about that for now.

 

BTW, your fist step needs to be reprogramming the UNO with the ArduinoISP sketch.  It was replaced by the usbasp hex file with your last programming attempt.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Sun. Nov 23, 2014 - 07:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I built the Usbasp because ordering a chinese one I would have to wait for for a month for the delivery. I´m familiar with PCB making and this is a really simple PCB with common components that I had at home.

 

Maybe I´m doing something wrong.

 

I conected the arduino to the computer by USB, and the USBasp with the atmega8 inside that I want to be programmed is connected to the arduino by arduino´s 6 pins ICSP.

 

Then I run the ISP programmer sketch in arduino with no changes in the code, and then I open  a windows command line console and use AVRdude.

 

I´m going to read documentation again about avrdude and isp programming and try to find what I´m doing wrong.

 

The switck -c is used to define the programmer and -p is used to define the target chip, Am I wrong?

 

Thanks guys.

 

 

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

Hello joeymorin. I didn´t refresh this window so I have just now read your post.

 

What you tell me explains to me many things...I´m going to follow your advices and try...

 

Thanks!! I´ll post again.

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

You are quite correct.

 

However,  it is worth following a ArduinoISP tutorial.   Especially with pictures.

 

Before you send a 'full command',  just check that you have communication ok:

avrdude -c stk500v1 -p m8 -P com9 -b 19200

If you connect the heartbeat LED,   you can see that ArduinoISP is working.

 

Incidentally,   you can buy Chinese USBASPs locally.    You just pay a bit more.

 

David.

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

It seems that I have communication with the arduino´s atmega168, but no communication with the atmega8, when I use the switch -p m8 I got a "no in sync" error message.

 

Maybe I´m using the wrong pins of the arduino (I´m using the 6 ICSP pins that are separated from the regular digital/analog pins)

 

Searching the web, I have no found connection diagrams for the atmega8, but I´ve found examples for the atmega16.

 

http://hardwarefun.com/tutorials...

 

He´s using the regular digital pins (10 to 13), maybe coud be this the problem.

 

I´m going on searching for info and trying to solve the problem.

 

Thanks, I appreciate your help.

 

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

From http://arduino.cc/en/Tutorial/ArduinoISP:

 

 

 

You can't use the 6-pin ISP connector on the Arduino-as-ISP to connect directly to the target m8 on your usbasp.  That connector is meant for doing ISP programming of the m168 on the Arduino directly from another programmer.  This isn't normally necessary as the Arduino's AVR is programmed with a bootloader, the purpose of which is to permit programming an application over serial/USB without the need of an ISP programmer.

 

The ArduinoISP sketch uses a GPIO pin (Arduino pin 10 as depicted above) to manipulate the RESET pin of the target in order to place the target into ISP programming mode.  However the remaining pins are the same (MISO, MOSI, SCK, VCC, GND).

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Mon. Nov 24, 2014 - 05:42 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks joeymorin.

 

I´ve mounted the system on a breadboard following the diagram, but I got the same behaviour and the same results.

 

I´ve detected one point that maybe could be the cause of the problem.

I´ve detected that I´m using an atmega8L-8PU chip. According with the datasheet, the "L" model is slightly different from the common atmega8. The "L" mode is designed to work with a crystal up to 8 Mhz, the common atmega does it up to 16Mhz. I´m using a 12 Mhz crystal, that´s out of the atmega8L range.

I´m almost sure that it has to be the cause of the fail.

 

I´m going now to a local store and try to get an atmega8 chip, or maybe a 8Mhz crystal...

 

I´ll post again.

 

Thanks for your help dudes!!

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

The 8L model should work just fine.    Modern production is likely to be 8A.

 

Quite honestly,   you can't get much clearer than Joey's picture.

 

A virgin ATmega8 does not need the crystal and capacitors.

As soon as you change the fuses to enable the crystal oscillator,   the crystal must be there.

 

Note that the crystal's capacitors are 22pF and not 22nF.

 

You must live in a very exotic country where you can walk into an AVR shop!

In most countries,  you rely on mail-order.

 

David.

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

8L should work fine? Ok. Good. I´ve call the local shops and they haven´t got the atmega8 in stock. I live in Seville, in the south of Spain. Here there are some establishments that sell electronic components but not really specialized, (they don´t have a big stock) I usually order components by mail.

 

I´m using 20pf ceramic disc capacitors with the crystal because this is the crystal´s specification marked on its side. I suppose this isn´t a problem.

 

Joey´s picture is absolutely clear, I´ve mounted my breadboard following this picture. (the resistor is brown black orange, 10k, am I wrong?)

 

But somehow, Avrdude still shows the same behaviour. It seems that it only communicates with the arduino, but not with the atmega8L.

 

"L" model should work fine...but not with an 12 Mhz crystal. Am I wrong? I understand that I´ve got to remove the crystal or replace it with an 8Mhz one, and configure the fuses according to it.

 

I don´t have an 8Mhz crystal so I should go to the local shop.

 

Thanks David
 

 

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

I am guessing that you have somehow b*ggered the ATmega8 clock fuses.

 

The 8L will work fine with 12MHz.    Ok,  the data sheet may not recommend it.    The 8A will certainly be ok.

 

I have posted a modified ArduinoISP sketch that provides a 1MHz clock signal on several occasions.

Just add a wire from digital#3 to XTAL1 on your target AVR.

// ArduinoISP version 04m3
// Copyright (c) 2008-2011 Randall Bohn
// If you require a license, see 
//     http://www.opensource.org/licenses/bsd-license.php
//
// This sketch turns the Arduino into a AVRISP
// using the following arduino pins:
//
// pin name:    not-mega:         mega(1280 and 2560)
// slave reset: 10:               53 
// MOSI:        11:               51 
// MISO:        12:               50 
// SCK:         13:               52 
//
// CLOCK_1MHZ    3:               .kbv
//
// Put an LED (with resistor) on the following pins:
// 9: Heartbeat   - shows the programmer is running
// 8: Error       - Lights up if something goes wrong (use red if that makes sense)
// 7: Programming - In communication with the slave
//
// 23 July 2011 Randall Bohn
// -Address Arduino issue 509 :: Portability of ArduinoISP
// http://code.google.com/p/arduino/issues/detail?id=509
//
// October 2010 by Randall Bohn
// - Write to EEPROM > 256 bytes
// - Better use of LEDs:
// -- Flash LED_PMODE on each flash commit
// -- Flash LED_PMODE while writing EEPROM (both give visual feedback of writing progress)
// - Light LED_ERR whenever we hit a STK_NOSYNC. Turn it off when back in sync.
// - Use pins_arduino.h (should also work on Arduino Mega)
//
// October 2009 by David A. Mellis
// - Added support for the read signature command
// 
// February 2009 by Randall Bohn
// - Added support for writing to EEPROM (what took so long?)
// Windows users should consider WinAVR's avrdude instead of the
// avrdude included with Arduino software.
//
// January 2008 by Randall Bohn
// - Thanks to Amplificar for helping me with the STK500 protocol
// - The AVRISP/STK500 (mk I) protocol is used in the arduino bootloader
// - The SPI functions herein were developed for the AVR910_ARD programmer 
// - More information at http://code.google.com/p/mega-isp

#include "pins_arduino.h"
#define RESET     SS

#define LED_HB    9
#define LED_ERR   8
#define LED_PMODE 7
#define PROG_FLICKER true

#define HWVER 2
#define SWMAJ 1
#define SWMIN 18

// STK Definitions
#define STK_OK      0x10
#define STK_FAILED  0x11
#define STK_UNKNOWN 0x12
#define STK_INSYNC  0x14
#define STK_NOSYNC  0x15
#define CRC_EOP     0x20 //ok it is a space...

void pulse(int pin, int times);

void setup() {
  Serial.begin(19200);
  pinMode(LED_PMODE, OUTPUT);
  pulse(LED_PMODE, 2);
  pinMode(LED_ERR, OUTPUT);
  pulse(LED_ERR, 2);
  pinMode(LED_HB, OUTPUT);
  pulse(LED_HB, 2);

  DDRD |= (1<<3);                    // make o/p
  OCR2A = F_CPU/2/1000000 - 1;       // CTC toggle @ 1MHz
  OCR2B = OCR2A;                     // match B 
  TCCR2A = (1<<COM2B0)|(1<<WGM21);   // Toggle OC2B in CTC mode
  TCCR2B = (1 << CS20);              // run timer2 at div1
}

int error=0;
int pmode=0;
// address for reading and writing, set by 'U' command
int here;
uint8_t buff[256]; // global block storage

#define beget16(addr) (*addr * 256 + *(addr+1) )
typedef struct param {
  uint8_t devicecode;
  uint8_t revision;
  uint8_t progtype;
  uint8_t parmode;
  uint8_t polling;
  uint8_t selftimed;
  uint8_t lockbytes;
  uint8_t fusebytes;
  int flashpoll;
  int eeprompoll;
  int pagesize;
  int eepromsize;
  int flashsize;
} 
parameter;

parameter param;

// this provides a heartbeat on pin 9, so you can tell the software is running.
uint8_t hbval=128;
int8_t hbdelta=8;
void heartbeat() {
  if (hbval > 192) hbdelta = -hbdelta;
  if (hbval < 32) hbdelta = -hbdelta;
  hbval += hbdelta;
  analogWrite(LED_HB, hbval);
  delay(20);
}


void loop(void) {
  // is pmode active?
  if (pmode) digitalWrite(LED_PMODE, HIGH); 
  else digitalWrite(LED_PMODE, LOW);
  // is there an error?
  if (error) digitalWrite(LED_ERR, HIGH); 
  else digitalWrite(LED_ERR, LOW);

  // light the heartbeat LED
  heartbeat();
  if (Serial.available()) {
    avrisp();
  }
}

uint8_t getch() {
  while(!Serial.available());
  return Serial.read();
}
void fill(int n) {
  for (int x = 0; x < n; x++) {
    buff[x] = getch();
  }
}

#define PTIME 30
void pulse(int pin, int times) {
  do {
    digitalWrite(pin, HIGH);
    delay(PTIME);
    digitalWrite(pin, LOW);
    delay(PTIME);
  } 
  while (times--);
}

void prog_lamp(int state) {
  if (PROG_FLICKER)
    digitalWrite(LED_PMODE, state);
}

void spi_init() {
  uint8_t x;
  SPCR = 0x53;
  x=SPSR;
  x=SPDR;
}

void spi_wait() {
  do {
  } 
  while (!(SPSR & (1 << SPIF)));
}

uint8_t spi_send(uint8_t b) {
  uint8_t reply;
  SPDR=b;
  spi_wait();
  reply = SPDR;
  return reply;
}

uint8_t spi_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
  uint8_t n;
  spi_send(a); 
  n=spi_send(b);
  //if (n != a) error = -1;
  n=spi_send(c);
  return spi_send(d);
}

void empty_reply() {
  if (CRC_EOP == getch()) {
    Serial.print((char)STK_INSYNC);
    Serial.print((char)STK_OK);
  } 
  else {
    error++;
    Serial.print((char)STK_NOSYNC);
  }
}

void breply(uint8_t b) {
  if (CRC_EOP == getch()) {
    Serial.print((char)STK_INSYNC);
    Serial.print((char)b);
    Serial.print((char)STK_OK);
  } 
  else {
    error++;
    Serial.print((char)STK_NOSYNC);
  }
}

void get_version(uint8_t c) {
  switch(c) {
  case 0x80:
    breply(HWVER);
    break;
  case 0x81:
    breply(SWMAJ);
    break;
  case 0x82:
    breply(SWMIN);
    break;
  case 0x93:
    breply('S'); // serial programmer
    break;
  default:
    breply(0);
  }
}

void set_parameters() {
  // call this after reading paramter packet into buff[]
  param.devicecode = buff[0];
  param.revision   = buff[1];
  param.progtype   = buff[2];
  param.parmode    = buff[3];
  param.polling    = buff[4];
  param.selftimed  = buff[5];
  param.lockbytes  = buff[6];
  param.fusebytes  = buff[7];
  param.flashpoll  = buff[8]; 
  // ignore buff[9] (= buff[8])
  // following are 16 bits (big endian)
  param.eeprompoll = beget16(&buff[10]);
  param.pagesize   = beget16(&buff[12]);
  param.eepromsize = beget16(&buff[14]);

  // 32 bits flashsize (big endian)
  param.flashsize = buff[16] * 0x01000000
    + buff[17] * 0x00010000
    + buff[18] * 0x00000100
    + buff[19];

}

void start_pmode() {
  spi_init();
  // following delays may not work on all targets...
  pinMode(RESET, OUTPUT);
  digitalWrite(RESET, HIGH);
  pinMode(SCK, OUTPUT);
  digitalWrite(SCK, LOW);
  delay(50);
  digitalWrite(RESET, LOW);
  delay(50);
  pinMode(MISO, INPUT);
  pinMode(MOSI, OUTPUT);
  spi_transaction(0xAC, 0x53, 0x00, 0x00);
  pmode = 1;
}

void end_pmode() {
  pinMode(MISO, INPUT);
  pinMode(MOSI, INPUT);
  pinMode(SCK, INPUT);
  pinMode(RESET, INPUT);
  pmode = 0;
}

void universal() {
  int w;
  uint8_t ch;

  fill(4);
  ch = spi_transaction(buff[0], buff[1], buff[2], buff[3]);
  breply(ch);
}

void flash(uint8_t hilo, int addr, uint8_t data) {
  spi_transaction(0x40+8*hilo, 
  addr>>8 & 0xFF, 
  addr & 0xFF,
  data);
}
void commit(int addr) {
  if (PROG_FLICKER) prog_lamp(LOW);
  spi_transaction(0x4C, (addr >> 8) & 0xFF, addr & 0xFF, 0);
  if (PROG_FLICKER) {
    delay(PTIME);
    prog_lamp(HIGH);
  }
}

//#define _current_page(x) (here & 0xFFFFE0)
int current_page(int addr) {
  if (param.pagesize == 32)  return here & 0xFFFFFFF0;
  if (param.pagesize == 64)  return here & 0xFFFFFFE0;
  if (param.pagesize == 128) return here & 0xFFFFFFC0;
  if (param.pagesize == 256) return here & 0xFFFFFF80;
  return here;
}


void write_flash(int length) {
  fill(length);
  if (CRC_EOP == getch()) {
    Serial.print((char) STK_INSYNC);
    Serial.print((char) write_flash_pages(length));
  } 
  else {
    error++;
    Serial.print((char) STK_NOSYNC);
  }
}

uint8_t write_flash_pages(int length) {
  int x = 0;
  int page = current_page(here);
  while (x < length) {
    if (page != current_page(here)) {
      commit(page);
      page = current_page(here);
    }
    flash(LOW, here, buff[x++]);
    flash(HIGH, here, buff[x++]);
    here++;
  }

  commit(page);

  return STK_OK;
}

#define EECHUNK (32)
uint8_t write_eeprom(int length) {
  // here is a word address, get the byte address
  int start = here * 2;
  int remaining = length;
  if (length > param.eepromsize) {
    error++;
    return STK_FAILED;
  }
  while (remaining > EECHUNK) {
    write_eeprom_chunk(start, EECHUNK);
    start += EECHUNK;
    remaining -= EECHUNK;
  }
  write_eeprom_chunk(start, remaining);
  return STK_OK;
}
// write (length) bytes, (start) is a byte address
uint8_t write_eeprom_chunk(int start, int length) {
  // this writes byte-by-byte,
  // page writing may be faster (4 bytes at a time)
  fill(length);
  prog_lamp(LOW);
  for (int x = 0; x < length; x++) {
    int addr = start+x;
    spi_transaction(0xC0, (addr>>8) & 0xFF, addr & 0xFF, buff[x]);
    delay(45);
  }
  prog_lamp(HIGH); 
  return STK_OK;
}

void program_page() {
  char result = (char) STK_FAILED;
  int length = 256 * getch();
  length += getch();
  char memtype = getch();
  // flash memory @here, (length) bytes
  if (memtype == 'F') {
    write_flash(length);
    return;
  }
  if (memtype == 'E') {
    result = (char)write_eeprom(length);
    if (CRC_EOP == getch()) {
      Serial.print((char) STK_INSYNC);
      Serial.print(result);
    } 
    else {
      error++;
      Serial.print((char) STK_NOSYNC);
    }
    return;
  }
  Serial.print((char)STK_FAILED);
  return;
}

uint8_t flash_read(uint8_t hilo, int addr) {
  return spi_transaction(0x20 + hilo * 8,
  (addr >> 8) & 0xFF,
  addr & 0xFF,
  0);
}

char flash_read_page(int length) {
  for (int x = 0; x < length; x+=2) {
    uint8_t low = flash_read(LOW, here);
    Serial.print((char) low);
    uint8_t high = flash_read(HIGH, here);
    Serial.print((char) high);
    here++;
  }
  return STK_OK;
}

char eeprom_read_page(int length) {
  // here again we have a word address
  int start = here * 2;
  for (int x = 0; x < length; x++) {
    int addr = start + x;
    uint8_t ee = spi_transaction(0xA0, (addr >> 8) & 0xFF, addr & 0xFF, 0xFF);
    Serial.print((char) ee);
  }
  return STK_OK;
}

void read_page() {
  char result = (char)STK_FAILED;
  int length = 256 * getch();
  length += getch();
  char memtype = getch();
  if (CRC_EOP != getch()) {
    error++;
    Serial.print((char) STK_NOSYNC);
    return;
  }
  Serial.print((char) STK_INSYNC);
  if (memtype == 'F') result = flash_read_page(length);
  if (memtype == 'E') result = eeprom_read_page(length);
  Serial.print(result);
  return;
}

void read_signature() {
  if (CRC_EOP != getch()) {
    error++;
    Serial.print((char) STK_NOSYNC);
    return;
  }
  Serial.print((char) STK_INSYNC);
  uint8_t high = spi_transaction(0x30, 0x00, 0x00, 0x00);
  Serial.print((char) high);
  uint8_t middle = spi_transaction(0x30, 0x00, 0x01, 0x00);
  Serial.print((char) middle);
  uint8_t low = spi_transaction(0x30, 0x00, 0x02, 0x00);
  Serial.print((char) low);
  Serial.print((char) STK_OK);
}
//////////////////////////////////////////
//////////////////////////////////////////


////////////////////////////////////
////////////////////////////////////
int avrisp() { 
  uint8_t data, low, high;
  uint8_t ch = getch();
  switch (ch) {
  case '0': // signon
    error = 0;
    empty_reply();
    break;
  case '1':
    if (getch() == CRC_EOP) {
      Serial.print((char) STK_INSYNC);
      Serial.print("AVR ISP");
      Serial.print((char) STK_OK);
    }
    break;
  case 'A':
    get_version(getch());
    break;
  case 'B':
    fill(20);
    set_parameters();
    empty_reply();
    break;
  case 'E': // extended parameters - ignore for now
    fill(5);
    empty_reply();
    break;

  case 'P':
    start_pmode();
    empty_reply();
    break;
  case 'U': // set address (word)
    here = getch();
    here += 256 * getch();
    empty_reply();
    break;

  case 0x60: //STK_PROG_FLASH
    low = getch();
    high = getch();
    empty_reply();
    break;
  case 0x61: //STK_PROG_DATA
    data = getch();
    empty_reply();
    break;

  case 0x64: //STK_PROG_PAGE
    program_page();
    break;

  case 0x74: //STK_READ_PAGE 't'
    read_page();    
    break;

  case 'V': //0x56
    universal();
    break;
  case 'Q': //0x51
    error=0;
    end_pmode();
    empty_reply();
    break;

  case 0x75: //STK_READ_SIGN 'u'
    read_signature();
    break;

    // expecting a command, not CRC_EOP
    // this is how we can get back in sync
  case CRC_EOP:
    error++;
    Serial.print((char) STK_NOSYNC);
    break;

    // anything else we will return STK_UNKNOWN
  default:
    error++;
    if (CRC_EOP == getch()) 
      Serial.print((char)STK_UNKNOWN);
    else
      Serial.print((char)STK_NOSYNC);
  }
}



David.

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

Thanks David.

 

I´m going crazy, I´ve tried your sketch using the wire from arduino digital#3 to atmega8 pin9 (xtal1). I´ve tried it with & without the crystals & caps. I´ve got the same results. Somehow Avrdude is always reading the arduino signature, when I use the "-m8" switch avrdude stops at this point because of a signature mismatch. When I use the "-m168" I can go further and flash a file, and read fuses and lock, but I can´t write on the fuses.

 

I´m almost sure I have a base problem, I think somehow there´s no communication with the atmega8 at all.

 

I´ve tried with the two atmega8 chips that I have, so the problem musn´t be a bad chip.

 

When I disconnect completely the atmega8, and I leave the arduino conected only to the computer I´ve got the same results.

Somehow it´s like if the atmega didn´t exist.

 

 

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

vintageman wrote:
I´m going crazy, I´ve tried your sketch using the wire from arduino digital#3 to atmega8 pin9 (xtal1). I´ve tried it with & without the crystals & caps. I´ve got the same results. Somehow Avrdude is always reading the arduino signature, when I use the "-m8" switch avrdude stops at this point because of a signature mismatch. When I use the "-m168" I can go further and flash a file, and read fuses and lock, but I can´t write on the fuses.
This suggests that you're not using the correct -c argument.  It should be -c stk500v1.  If you use -c arduino then avrdude will be talking to the bootloader on your Arduino instead of the m8 target via the ArduinoISP app code.

 

I assume however that you are in fact specifying the correct argument to -c, and that something else is going wrong.

 

Although it shouldn't be necessary, you may want to try placing a 10 µF capacitor across the Arduino's RESET and GND pins.  If using an electrolytic cap, put the negative lead on GND.  This will prevent the FTDI USB-to-serial chip from resetting the Arduino when avrdude opens the connection.  As I say, the avrdude session should 'fall through' to the ArduinoISP sketch and the m8 target even without a cap, but at this point I think it's worth a try.

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Hey!!!!! IT WORKS!!!!

 

I was using "-c arduino". I thought that the "-c" switch was for telling the avrdude what kind of programmer we´re using, so I used -c arduino.

I don´t completely understand it.

 

But it works!!! this is a fact!! Now the fuses are writtable.

 

Thankyou man!!!

 

Thank you both Joey and David!!!

 

Now I can finnish the USB programmer!!

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

vintageman wrote:
I was using "-c arduino". I thought that the "-c" switch was for telling the avrdude what kind of programmer we´re using, so I used -c arduino.

I don´t completely understand it.

Well that is what it's for.

 

The ArduinoISP is a sketch which turns an Arudino into a programmer.  Specifically, it emulates (a subset of) the stk500v1 programming protocol.

 

The m168 on your Arduino has by the kind folks who built it been programmed with a bootloader.  That bootloader is a special program which acts as a 'standin' for a programmer, and itself emulates a smaller subset of the stk500v1 programming protocol.  The folks at avrdude decided to support the Arduino bootloader with a special programmer profile called arduino.  This has caused some confusion ;)

 

Glad you solved it!

 

Here is a list of all of the supported programmers (version 5.1):

$ avrdude -c ?

Valid programmers are:
  c2n232i  = serial port banging, reset=dtr sck=!rts mosi=!txd miso=!cts [/etc/avrdude.conf:876]
  dasa3    = serial port banging, reset=!dtr sck=rts mosi=txd miso=cts [/etc/avrdude.conf:863]
  dasa     = serial port banging, reset=rts sck=dtr mosi=txd miso=cts [/etc/avrdude.conf:850]
  siprog   = Lancos SI-Prog <http://www.lancos.com/siprogsch.html> [/etc/avrdude.conf:837]
  ponyser  = design ponyprog serial, reset=!txd sck=rts mosi=dtr miso=cts [/etc/avrdude.conf:824]
  89isp    = Atmel at89isp cable            [/etc/avrdude.conf:791]
  frank-stk200 = Frank STK200                   [/etc/avrdude.conf:777]
  blaster  = Altera ByteBlaster             [/etc/avrdude.conf:764]
  ere-isp-avr = ERE ISP-AVR <http://www.ere.co.th/download/sch050713.pdf> [/etc/avrdude.conf:754]
  atisp    = AT-ISP V1.1 programming cable for AVR-SDK1 from <http://micro-research.co.th/>  [/etc/avrdude.conf:744]
  dapa     = Direct AVR Parallel Access cable [/etc/avrdude.conf:733]
  xil      = Xilinx JTAG cable              [/etc/avrdude.conf:720]
  futurlec = Futurlec.com programming cable. [/etc/avrdude.conf:703]
  abcmini  = ABCmini Board, aka Dick Smith HOTCHIP [/etc/avrdude.conf:693]
  picoweb  = Picoweb Programming Cable, http://www.picoweb.net/ [/etc/avrdude.conf:683]
  sp12     = Steve Bolt's Programmer        [/etc/avrdude.conf:672]
  alf      = Nightshade ALF-PgmAVR, http://nightshade.homeip.net/ [/etc/avrdude.conf:656]
  bascom   = Bascom SAMPLE programming cable [/etc/avrdude.conf:646]
  dt006    = Dontronics DT006               [/etc/avrdude.conf:636]
  pony-stk200 = Pony Prog STK200               [/etc/avrdude.conf:624]
  stk200   = STK200                         [/etc/avrdude.conf:608]
  bsd      = Brian Dean's Programmer, http://www.bsdhome.com/avrdude/ [/etc/avrdude.conf:597]
  pavr     = Jason Kyle's pAVR Serial Programmer [/etc/avrdude.conf:589]
  dragon_pdi = Atmel AVR Dragon in PDI mode   [/etc/avrdude.conf:582]
  dragon_dw = Atmel AVR Dragon in debugWire mode [/etc/avrdude.conf:574]
  dragon_hvsp = Atmel AVR Dragon in HVSP mode  [/etc/avrdude.conf:566]
  dragon_pp = Atmel AVR Dragon in PP mode    [/etc/avrdude.conf:558]
  dragon_isp = Atmel AVR Dragon in ISP mode   [/etc/avrdude.conf:550]
  dragon_jtag = Atmel AVR Dragon in JTAG mode  [/etc/avrdude.conf:542]
  jtag2pdi = Atmel JTAG ICE mkII PDI mode   [/etc/avrdude.conf:534]
  jtag2avr32 = Atmel JTAG ICE mkII im AVR32 mode [/etc/avrdude.conf:526]
  jtagmkII_avr32 = Atmel JTAG ICE mkII im AVR32 mode [/etc/avrdude.conf:518]
  jtag2dw  = Atmel JTAG ICE mkII in debugWire mode [/etc/avrdude.conf:510]
  jtag2isp = Atmel JTAG ICE mkII in ISP mode [/etc/avrdude.conf:502]
  jtag2    = Atmel JTAG ICE mkII            [/etc/avrdude.conf:494]
  jtag2fast = Atmel JTAG ICE mkII            [/etc/avrdude.conf:486]
  jtag2slow = Atmel JTAG ICE mkII            [/etc/avrdude.conf:478]
  jtagmkII = Atmel JTAG ICE mkII            [/etc/avrdude.conf:470]
  jtag1slow = Atmel JTAG ICE (mkI)           [/etc/avrdude.conf:463]
  jtag1    = Atmel JTAG ICE (mkI)           [/etc/avrdude.conf:455]
  jtagmkI  = Atmel JTAG ICE (mkI)           [/etc/avrdude.conf:447]
  avr911   = Atmel AppNote AVR911 AVROSP    [/etc/avrdude.conf:441]
  avr109   = Atmel AppNote AVR109 Boot Loader [/etc/avrdude.conf:435]
  butterfly = Atmel Butterfly Development Board [/etc/avrdude.conf:429]
  usbtiny  = USBtiny simple USB programmer, http://www.ladyada.net/make/usbtinyisp/ [/etc/avrdude.conf:423]
  usbasp   = USBasp, http://www.fischl.de/usbasp/ [/etc/avrdude.conf:417]
  avr910   = Atmel Low Cost Serial Programmer [/etc/avrdude.conf:411]
  stk600hvsp = Atmel STK600 in high-voltage serial programming mode [/etc/avrdude.conf:405]
  stk600pp = Atmel STK600 in parallel programming mode [/etc/avrdude.conf:399]
  stk600   = Atmel STK600                   [/etc/avrdude.conf:393]
  stk500hvsp = Atmel STK500 V2 in high-voltage serial programming mode [/etc/avrdude.conf:387]
  stk500pp = Atmel STK500 V2 in parallel programming mode [/etc/avrdude.conf:381]
  stk500v2 = Atmel STK500 Version 2.x firmware [/etc/avrdude.conf:375]
  mib510   = Crossbow MIB510 programming board [/etc/avrdude.conf:369]
  stk500v1 = Atmel STK500 Version 1.x firmware [/etc/avrdude.conf:363]
  stk500   = Atmel STK500                   [/etc/avrdude.conf:357]
  buspirate = The Bus Pirate                 [/etc/avrdude.conf:347]
  avrisp2  = Atmel AVR ISP mkII             [/etc/avrdude.conf:341]
  avrispmkII = Atmel AVR ISP mkII             [/etc/avrdude.conf:335]
  avrispv2 = Atmel AVR ISP V2               [/etc/avrdude.conf:329]
  avrisp   = Atmel AVR ISP                  [/etc/avrdude.conf:323]
  arduino  = Arduino                        [/etc/avrdude.conf:317]

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

OK, first attempt and I´ve alredy done something wrong. Amazing but true!!!

 

I´ve written c9 in the low fuse (error, I should have written ef). I used "-c stk500v1" and it worked fine, signature check was OK.

Then, in order to solve my error, I tried to writte again the fuses, but this time, the signature reading is 0x000000, and Avr fails writting fuses or even flashing a file.

I´ve tried many times more with no results. It seems that this time is true, I´ve b+ggered the ATmega8 clock fuses.

 

I´ve got 2 ATmega8 chips, so I tried with the other and this time it worked fine. I´ve flash the .hex file and written the fuses. I´ve put the chip on the programmer and this time it appears as USBasp in the devide manager. I´ve installed the drivers with no problems. I have not tested it programming a chip but, but I would say that it´s O.K.

 

Well, now I would like to bring back to life the first chip.

 

It reads signature 0x00000 and it fails every time I try to write. It´s like if it wasn´t connected.

I´ve tried again your sketch David, but with no results.

It seems that writting c9 on low fuse, I´ve marked CKSEL1, CKSEL2, SUT0 and SUT1 and I´ve unmarked CKSEL0, CKSEL3, BODEN and BODLEVEL, but I don´t know really well what it means.

I´m going to continue doing some test, tryng to recover this chip, I think I´ve got to find what are the clock settings now on the atmega8 config.

 

 

Thanks for your help guys. And sorry for being so dumb to break the fuses!!

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

Thanks Joey for such a good explanation about arduino and the skt500v1 protocol.

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

Quote:
I´ve written c9 in the low fuse
Aw, cr@p ;)

 

Well, that selects the low frequency crystal oscillator.  This is meant for a 32.768 kHz watch crystal, and is intended for very low power applications which don't require speed, like a real-time-clock.

 

The best way to recover from this fuse accident is to get your hands on a 32.768 kHz watch crystal and hook it up to XTAL1/XTAL2.  This will have the m8 run at that very low speed.

 

You will also need to change the programming clock speed of the programmer.  You can use the ArduinoISP sketch on the UNO, but now that you've got a functioning usbasp, why not use it instead?  Kind of poetic that it's first job will be reviving it's brother ;)

 

When programming over ISP, the programming clock applied to SCK by the programmer must be less than 1/4 the target's system clock speed.  Since that clock speed is 32.768 kHz, you must ensure that the programming clock is slower than 8.192 kHz.  That corresponds to a clock period of greater than 122 µs.  Avrdude has a command line option to set the programming clock period:

-B 200

... will set the period to 200 µs, or 5 kHz.

 

Note that it will take a long time for avrdude to complete an ISP session at this speed.  Once you've recovered and restored the correct fuse settings, you should be able to program the m8 normally.

 

It might be possible to recover from this with David's sketch above with digital pin 3 tied to the m8's XTAL1, but I'm made to understand by others that injecting an external clock in this case usually doesn't work.  You'd probably have to modify the ArduinoISP sketch further to reduce the programming clock manually (ArduinoISP doesn't support the -B command line option), and that would mean ditching hardware SPI for a software version since the hardware SPI can only go as slow as 125 kHz.  You'd also have to slow down the system clock for injection into the target on pin 3 to something close to 32 kHz.

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Tue. Nov 25, 2014 - 07:24 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hehehe, OK.

 

I´ve realized that I´ve got a RTC clock module for arduino that has a 32.768 crystal inside. I don´t even have to desolder the crystal, When I get off the socket the IC in the module (a DS1302) both crystal legs get pretty isolated. Somehow I´ve got to wire it to the breadboard.

 

Do I have to use capacitors with the crystal? I´m almost sure that I don´t, because the RTC module don´t carry any cap.

 

Thanks for your help!! I´m going to test my new programmer and try to revive the chip!!

 

 

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

Try my sketch first.   i.e. with the external 1MHz clock signal.   Just talk to the mega8:

avrdude -c stk500v1 -p m8 -P com9 -b 19200

If that works,   just fix the fuses:

avrdude -c stk500v1 -p m8 -P com9 -b 19200 -U lfuse:w:0xe1:m -U hfuse:w:0xd9:m

Then you can start again with a 'virgin' ATmega8.   i.e. running on "Internal 1MHz RC"

 

None of this is very complicated.    However,  you do need to follow the steps carefully.

 

Yes,    it is me that reported difficulties with external signal when your fuses are set for LF crystal.

It is still worth trying.   If it does not work,  use a real watch crystal.

 

David.

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

vintageman wrote:
I don´t even have to desolder the crystal, When I get off the socket the IC in the module (a DS1302) both crystal legs get pretty isolated. Somehow I´ve got to wire it to the breadboard.
These crystals undergo and extremely delicate and low-power oscillation.  Any disturbance and they probably won't sing.  Leaving it connected to another circuit (the DS1302) or having more than a couple of centimetres of wire between the crystal and the m8 would probably prevent it from oscillating.

 

Quote:
Do I have to use capacitors with the crystal? I´m almost sure that I don´t, because the RTC module don´t carry any cap.
Generally, no.  If you want an accurate clock source, then yes.  See appnote AVR4100 if you're curious, but it's beyond your needs at the moment.

 

The m8 low-frequency oscillator has internal capacitors activated by the CKOPT fuse in the high fuse byte.  It's impossible to read fuses at the moment, so you can't be sure whether or not their enabled :(

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Thanks guys!!

 

David, I´ve tried your sketch but got no results. It still reads a 0x000000 signature.

 

Then I´ve tried the 32.768Mhz crystal. I´ve connected it with the shortests wires I could. (between 1 and 2 cm aprox.) But now I got a problem, every time I tried to change the baud rate in the Avrdude (-b command) or in arduino ISP sketck (serial.begin() command), or both to 200, I´ve got a sync error. It seems that it doesn´t even communicate with the arduino ??

 

Maybe further modifications of the arduino ISP sketch are needed?

 

I´ve alredy tried to use the new USBasp programmer, but it means re-wiring all the system and trying new AVRdude commands and lines. I tried once or twice, It didn´t worked and at this point I think that using the USBasp is going into more complexity, joining two problems, the "broken" fuses and the fact that I´m not familiar with the new USBasp.

 

Because my arduino system finally worked, by now I´m using it instead of the USBasp, in order to get the simplest solution.

 

I´m going to continue tring to revive the chip.

Last Edited: Tue. Nov 25, 2014 - 07:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't want to sound too pedantic,  but you need a 32kHz not a 32MHz crystal.

Why do you want to change the baud rate?    ArduinoISP runs at 19200 baud.

 

I hoped that the 1MHz clock signal would have won.     Obviously not.     (I warned that it might not work)

 

Anyway,   if your external AVR is running at 32kHz with a watch crystal,    it does mean that you need a very slow (8kHz) SCK frequency.    This is -B125 for the avrdude command line.

 

Unfortunately,   it looks as if ArduinoISP does not support different SCK frequencies.     It just runs at a fixed 125kHz.

 

I suppose that I could write you a special bit-banged ArduinoISP.     I have not got the enthusiasm at the moment.

 

Personally,    I would buy a real Chinese USBASP for $3.60.    You may have to wait for 14 days or so.     You have got a perfectly good Diecimillia to keep you busy.     I run most projects on Arduino hardware.     It is so convenient to avoid having extra wires.    One USB cable and you have UART and programming.

 

David.

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

vintageman wrote:
But now I got a problem, every time I tried to change the baud rate in the Avrdude (-b command)
That's -B (upper-case B).

 

Quote:
or in arduino ISP sketck (baudrate command),
It doesn't work that way.  The baud rate in the ArduinoISP sketch is the speed between avrdude and the programmer (i.e. the sketch), not the speed to the target.

 

A while ago a posted a modified version of the ArduinoISP sketch to permit recovering from a clock accident like this (specifically, the WDT oscillator).  It's here:

http://pastebin.com/Mngw3UH9

You'll need to change this line:

#define MINIMUM_TARGET_CLOCK_SPEED 128000

To this:

#define MINIMUM_TARGET_CLOCK_SPEED 32000

Read all the comments at the beginning of the sketch to understand how to use it, there are some differences from the original.

 

Note that this sketch doesn't push out a clock signal.  You'll still need to connect the 32.768 kHz crystal to the target.

 

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Thanks guys, I appreciate your help

 

David, you´re right, I wrote Mhz by mistake, but the crystal I have is 32.768 Khz so it´s right.

I´ll do some test to learn the use of the USBasp, if it doesn´t work I´ll order a chinesse one.

 

Joey, I´ve tried the sketch changing the line, but with no results

 

I use

avrdude -c stk500v1 -p m8 -P com9 -b 19200 -B 200

 

but I got a 0x000000 signature reading.

 

Maybe the the capacitance of the breadboard is causing that the crystal isn´t doing its work properly.

 

 

 

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

Not only do you need to edit the

#define MINIMUM_TARGET_CLOCK_SPEED 32000

but you also need to connect digital#9 pin to GND to make it go into SLOW mode.

 

I have only glanced at the sketch.    I suggest that you read it carefully.    (just the comment lines,   there is no need to understand any of the code)

 

David.

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

Hey man!!! I´ve wired digital#9 to ground and it works!!!

 

now it reads 0x1e9307 signature and flashes an .hex file O.K.

 

but for any reason it fails writting the fuses

 

I´ve used :

 

avrdude -c stk500v1 -p m8 -P com9 -b 1920 -B 200 -u -U flash:Arcade_Joystick.hex -U hfuse:w:0xbf:m -U lfuse:w:0xbf:m

 

 

 

Last Edited: Tue. Nov 25, 2014 - 11:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok.

 

It fails trying to writte the high fuse, but for any reason I does write on the low one.

I´ve written the low fuse with 0xd9

 

odd?

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

It is not odd at all.    It is simply refusing to remove the SPIEN fuse.

 

Please go to bed and get some sleep.

Then think carefully before you go writing fuses.

 

Surely you have experienced enough grief already!!!

 

David.

Last Edited: Tue. Nov 25, 2014 - 11:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hahahaha

 

thankyou David!

 

I´ll follow your advice and got some sleep

 

Tomorrow I´ll try to get some info about the spien fuse.

 

Have a good night

Last Edited: Tue. Nov 25, 2014 - 11:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
I suggest that you read it carefully.    (just the comment lines,   there is no need to understand any of the code)
Indeed ;)
joeymorin wrote:
Read all the comments at the beginning of the sketch to understand how to use it, there are some differences from the original.

 

vintageman wrote:

-U hfuse:w:0xbf:m -U lfuse:w:0xbf:m

Those aren't the factory default fuse values.  As David has noted, you're trying to alter a fuse which cannot be altered over ISP.  Nevertheless avrdude tries, and fails when the read-back of that fuse shows that the change didn't take.

 

The factory default fuse values are:

-U hfuse:w:0xd9:m -U lfuse:w:0xe1:m

Note that it is the high fuse which has a default of 0xD9.  Writing the low fuse to that value is how you got into trouble in the first place! :)  The low nybble (0x9) specifies the use of the low-frequency watch crystal.

 

You want eventually to select the fuses recommended by the usbasp documentation, just as you did for the other m8.

 

You can burn those fuses right now, or you can burn default fuses first.  The latter is only of value to confirm that you got the darned thing working correctly.  If you manage to restore factory default fuses, then you should be able to program the m8 without any special effort either with a stock ArduinoISP sketch or with your functioning usbasp, and without the need to connect a crystal of any kind since the default fuses specify the use of the completely self-contained internal RC oscillator.  Note that once you have successfully programmed the usbasp firmware and correctly set the fuses, you will need the proper 16 MHz crystal in order to perform any additional programming operations.

 

Bottom line is that to do ISP, the target must have a running system clock.  If the target has previously been told to use an external crystal as a clock, then it won't have a clock unless that crystal is connected.

 

But I'm glad you've got this far.  The finish line is in sight ;)

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Wed. Nov 26, 2014 - 01:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I´ve realized that it depends on the values I try to write on the high fuse, it fails or succeed.

 

Every time I tried to write "bf" on hfuse it failed....but....

When I tried to write "d9", the default value, it worked!!

 

So I´ve written both fuses and left them with the default values.

I removed the 32.768 Khz crystal and mounted the old 12 Mhz with caps.

I tried to connect with the original arduino ISP programmer sketch and...it worked!!!

 

It seems that the chip has come back home!!

 

Now...My goal was to program the second chip as an joystick controller following this web´s instructions

 

http://denki.world3.net/arcade.html

 

The web gives the next values in order to program the atmega8:

 

Low: 0xd9
High: 0xbf

Ext. Crystal/Res High Freq. Start-up Time: 16k + 64ms
Brown-out detection enabled
Brown-out level VCC=2.7V
Boot Flash size = 1024 words Start address $0c00
SPI enabled

 

Ok,...."bf" on high fuse...but even now, every time I try to write "bf" on high fuse it fails.

 

The avrdude shows the fail message, and one weird thing happen, the hfuse is written with "9f"

When I read it, it shows "9f"...not the initial value "D9" nor the value I tried to write "bf"

 

Ok, at this point I loose connection with the AVR and I start to read 0x000000 signatures, then I mount the 32.768 Khz crystal again, and use your modified sketch, Joey, and I start again and write the default fuse values.

 

By now I don´t know why I can´t write "bf" in hfuse.

 

One thing I´ve got to say is that I read all the comments in your sketch, I´m even using the common cathode RGB led...beautiful fx, hehe

 

Thanks guys!!!

 

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

Sorry, I think I confused you with another thread where the OP had two programmers... :)

 

OK, so the reason programming the high fuse with 0xbf is failing is as mentioned before you can't unprogram the SPIEN fuse over ISP.  With that fuse byte (0xbf = 0b10111111) you get:
 

RSTDISBL    = 1 (unprogrammed)
WDTON       = 0 (programmed)
SPIEN       = 1 (unprogrammed)
CKOPT       = 1 (unprogrammed)
EESAVE      = 1 (unprogrammed)
BOOTSZ[1:0] = 0b11
BOOTRST     = 1 (unprogrammed)

Before you ask, yes, zero equals programmed.

 

I must say I'm confused from where you get L:0xd9 and H:0xbf.  The link you posted has a link to the firmware source code at the very bottom of the page.  It's a zip file which contains among other things a text file called fuses.txt:

Low: 0x9f
High: 0xc9

Ext. Crystal/Res High Freq. Start-up Time: 16k + 0ms
Brown-out detection enabled
Brown-out level VCC=2.7V
Boot Flash size = 1024 words Start address $0c00
SPI enabled
CKOPT

As for what appears to be a mixup between H and L fuses on the part of avrdude when reporting fuse values, I believe that is a known bug in avrdude version 6.0.1 :( ...  What version are you using?

 

EDIT: By the way, while I posted that sketch on pastebin, I didn't write most of it.  It's actually mostly the same ArduinoISP example sketch that comes with the Arduino IDE.  I found a version on pastebin that used slow software SPI, by a user named smeezekitty.  I modified it further to allow it to use both the original fast hardware SPI and the slow software SPI depending on the state of pin 9 after a power-up or reset of the Arduino.  I also changed the speed of the heartbeat on the LEDs to indicate the current mode.  Not much of a programming effort.  I slapped it together after a clock fuse accident of my own, and posted it back to the pastebin.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Wed. Nov 26, 2014 - 05:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I´m using Avrdude 6.1

 

In the web that we were talking about, we can find a zip file called JoystickControllerV1.1 that contains a fuses.txt with the values I posted before.

There´s also another zip file called Arcade_Joystick with another fuses.txt with different values.

 

I´m confused, I´m wondering which values are the right values for the ATmega fuses for this project.

 

And about the SPIEN...I´ve search the net and found that in order to unprogram the SPIEN fuse it would be necessary a high voltage programmer. I´m thinking that it would be nice to have one. It could be a useful tool.

 

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

vintageman wrote:
In the web that we were talking about, we can find a zip file called JoystickControllerV1.1 that contains a fuses.txt with the values I posted before.
I'm confident that those are wrong.  The low fuse will definitely ruin your day again, it calls for a low-frequency watch crystal.  The high fuse makes no sense because it enables the watchdog permanently but the software itself never uses it, and it tries to unprogram the SPIEN fuse which is impossible over ISP.

 

Ignore this file!

 

Quote:
There´s also another zip file called Arcade_Joystick with another fuses.txt with different values.
That's the one I mention in my earlier post.  That's the one that also makes sense.  WDT disabled, SPIEN enabled, sensible clock selection.

 

Trust this file.

 

Interestingly, the two zip files are supposed to be from the same project.  One is the burnable .hex file, the other is the source code from which that hex file was built.  Why the two versions of fuses.txt are different is anyone's guess.

 

Quote:
And about the SPIEN...I´ve search the net and found that in order to unprogram the SPIEN fuse it would be necessary a high voltage programmer. I´m thinking that it would be nice to have one. It could be a useful tool.
Indeed it would be useful, but there aren't many good reasons to have one anymore.  ISP programmers and bootloaders have largely made HVPP and HVSP programmers obsolete.  One good reason to have one around is to recover from fuse accidents.  There are some accidents recovery from which can only be done with an HVPP/HVSP programmer.  However since you will be careful programming fuses in the future smiley ... and since an AVR costs only a few bucks, I'd put that on the back burner for now.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

We have no idea what part of the world you live in.

 

It appears that you have had zero sleep for several days.

This is not a good way to make any progress in this subject.

 

Please add your country to your profile.

 

David.

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

David, you catch me, I´ve had strange sleep times last days. :)

 

Well, finally I burnt the atmega with correct fuses & .hex file (the second one "Arcade_Joystick.es) with no problems.

 

Then I built the PCB for the arcade joystick interface and I´ve mounted the atmega8 in.

 

I´ve plugged it into the computer, but for some reason the PC doesn´t detects it. It detects nothing at all.

 

So it´s time for me to debug the PCB and do some tests, If I find nothing I´ll test the atmega again with the arduino as programmer.

 

Thanks again for your help guys, you´re great masters!!

 

 

 

Last Edited: Wed. Nov 26, 2014 - 08:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How did you build the PCB?  Did you make your own from the layout available?  Did you use a stripboard?  Breadboard?

 

I note that the stripboard layout on the site has some flaws... For example AVcc isn't connected, neither is its ground.  Shouldn't affect USB, but who knows...

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

I would guess that the author wasn't very diligent about documentation.  In the source code .asm file, the comments at the beginning of the file say:

/* USB Arcade Controller with dual USB/Playstation compatibility
 *
 * (C) 2008 Paul Qureshi aka MoJo
 * With USB stack code (C) 2003 Ing. Igor Cesko (http://www.cesko.host.sk)
 *
 * Version:		1.0
 * Target:		ATmega8
 * Clock:		12MHz ext. crystal
 * Fuses:		Low 0xd9, High 0xbf
 *				- Ext. Crystal/Res High Freq. Start-up Time: 16k + 64ms
 *				- Brown-out detection enabled
 *				- Brown-out level VCC=2.7V
 *				- Boot Flash size = 1024 words Start address $0c00
 *				SPI enabled
 * Contact:		mojo@world3.net
 *				http://joystick.world3.net
 *
 * History:		V1.0
 *				- First version
 */

Note that these are the same wrong fuses you found in the .hex file's .zip!

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

I used a stripboard and the layout posted in the web. I think the same than you, the author maybe isn´t very very dilligent with the info.

 

Ok I plugged on the solder in order to wire the AVcc and its ground, and waiting for the solder to get hot I decided to do some checks to the PCB. I got strange readings around D2, I desoldered and checked it, and that´s all. D2 was not working properly, I substituted it and plugged the interface to the computer.

 

And it´s working!!!!!!!

 

The PC recognises it, no drivers needed.

 

On next days I´ll make the physical part of the arcade controller.

 

About the USBasp programmer, maybe I´ve made a futile object...all the things I can do with it, could be done with an arduino. But no problem, learning does not harm.

 

I´ve got a couple of ATmega368 chips without bootloader that could be nice for testing the programmer.

 

It´s really interesting programming microcontrollers.

 

Thankyou very much you both for your help. You´re good teachers!!!

 

 

 

 

 

 

 

 

 

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

Glad those troubles are behind you!

 

I often use an ArduinoISP-equipped UNO, but sometimes you just don't have enough UNOs around.  It's always handy having a dedicated programmer in your kit.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

By the way if you haven't already you should go ahead and connect AVcc and that 2nd ground, and add a 100 nF cap across them.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

OK, I will do. Thanks for remembering it to me.

 

I would like to continue doing some things with microcontrollers and learning, but by now I´ve got nothing more to do, I´ve finnished all my projects, so I will search the web for more interesting things.

 

I think learning C is too much for me, I don´t want to become crazy. But I´m learning the arduino language, and I´m doing some tests with sensors and modules. Really interesting.

 

And absolutely addictive!!! hehe

Pages