Go Down

Topic: ArduinoISP Arduino uno & blank atmega8 bootloader burn trouble (Read 8639 times) previous topic - next topic

mysteredding

Nov 25, 2012, 11:08 am Last Edit: Nov 25, 2012, 11:27 am by mysteredding Reason: 1
i cant believe this 9500 character limit so i had to split it into 7 posts, please go forth and read
pardon if i sound at all rude but ive seen so many tutorials im getting sick of the links to one another,
ive been trying to burn a bootloader to sevral chips using my arduino uno with NO luck,
i just got 3 atmega8 chips completely blank no bootloader, so i tried all methods of burning a bootloader for use with internal 8mhz tutorials,
if you can google it(with all kindsa peramiters in search) ive looked at and tried it,
i DO NOT HAVE and WILL NOT BUY external crystals for sevral reasons, i have NO MONEY to buy any,
and if i dont need one (as it seems) i wont use one for saving on space, time, money, and parts,
if i was ABSOLUTELY SURE it would work if i bought a crystal i would buy 1 but im not so i wont, so please do not ask me to do so
I SERIOUSLY NEED HELP PEOPLE
im no expert at any programming language but im very smart and can follow STEP-BY-STEP instructions,
please for the love of god DO NOT "LINK" TUTORIALS just lay out what i need to do HERE

im running Ubuntu/linux 12.04 w/Arduino IDE 1.0

mysteredding

heres all my arduinoisp sketches

Code: [Select]

#include "pins_arduino.h"  // defines SS,MOSI,MISO,SCK
#define RESET SS

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

#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(7, OUTPUT);
 pulse(7, 2);
 pinMode(8, OUTPUT);
 pulse(8, 2);
 pinMode(9, OUTPUT);
 pulse(9, 2);
}

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(40);
}
 
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 readbytes(int n) {
 for (int x = 0; x < n; x++) {
   buff[x] = Serial.read();
 }
}

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

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 {
   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 {
   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])
 //getch(); // discard second value
 
 // WARNING: not sure about the byte order of the following
 // 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;

 for (w = 0; w < 4; w++) {
   buff[w] = getch();
 }
 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) {
 spi_transaction(0x4C, (addr >> 8) & 0xFF, addr & 0xFF, 0);
}

//#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;
}
uint8_t write_flash(int length) {
 if (param.pagesize < 1) return STK_FAILED;
 //if (param.pagesize != 64) return STK_FAILED;
 int page = current_page(here);
 int x = 0;
 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;
}

uint8_t write_eeprom(int length) {
 // here is a word address, so we use here*2
 // this writes byte-by-byte,
 // page writing may be faster (4 bytes at a time)
 for (int x = 0; x < length; x++) {
   spi_transaction(0xC0, 0x00, here*2+x, buff[x]);
   delay(45);
 }
 return STK_OK;
}

void program_page() {
 char result = (char) STK_FAILED;
 int length = 256 * getch() + getch();
 if (length > 256) {
     Serial.print((char) STK_FAILED);
     return;
 }
 char memtype = getch();
 for (int x = 0; x < length; x++) {
   buff[x] = getch();
 }
 if (CRC_EOP == getch()) {
   Serial.print((char) STK_INSYNC);
   if (memtype == 'F') result = (char)write_flash(length);
   if (memtype == 'E') result = (char)write_eeprom(length);
   Serial.print(result);
 }
 else {
   Serial.print((char) STK_NOSYNC);
 }
}
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
 for (int x = 0; x < length; x++) {
   uint8_t ee = spi_transaction(0xA0, 0x00, here*2+x, 0xFF);
   Serial.print((char) ee);
 }
 return STK_OK;
}

void read_page() {
 char result = (char)STK_FAILED;
 int length = 256 * getch() + getch();
 char memtype = getch();
 if (CRC_EOP != getch()) {
   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()) {
   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);
}
//////////////////////////////////////////
//////////////////////////////////////////

1/2

mysteredding

2/2
Code: [Select]

////////////////////////////////////
////////////////////////////////////
int avrisp() {
 uint8_t data, low, high;
 uint8_t ch = getch();
 switch (ch) {
 case '0': // signon
   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':
   readbytes(20);
   set_parameters();
   empty_reply();
   break;
 case 'E': // extended parameters - ignore for now
   readbytes(5);
   empty_reply();
   break;

 case 'P':
   start_pmode();
   empty_reply();
   break;
 case 'U':
   here = getch() + 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
   read_page();    
   break;

 case 'V':
   universal();
   break;
 case 'Q':
   error=0;
   end_pmode();
   empty_reply();
   break;
   
 case 0x75: //STK_READ_SIGN
   read_signature();
   break;

 // expecting a command, not CRC_EOP
 // this is how we can get back in sync
 case CRC_EOP:
   Serial.print((char) STK_NOSYNC);
   break;
   
 // anything else we will return STK_UNKNOWN
 default:
   if (CRC_EOP == getch())
     Serial.print((char)STK_UNKNOWN);
   else
     Serial.print((char)STK_NOSYNC);
 }
}


mysteredding

slight alteration to delay(40) to (20) and i think thats it
Code: [Select]
// this sketch turns the Arduino into a AVRISP
// using the following pins:
// 10: slave reset
// 11: MOSI
// 12: MISO
// 13: SCK

// 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
//
// 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"  // defines SS,MOSI,MISO,SCK
#define RESET SS

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

#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(9600);
 pinMode(7, OUTPUT);
 pulse(7, 2);
 pinMode(8, OUTPUT);
 pulse(8, 2);
 pinMode(9, OUTPUT);
 pulse(9, 2);
}

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 readbytes(int n) {
 for (int x = 0; x < n; x++) {
   buff[x] = Serial.read();
 }
}

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

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 {
   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 {
   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])
 //getch(); // discard second value
 
 // WARNING: not sure about the byte order of the following
 // 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;

 for (w = 0; w < 4; w++) {
   buff[w] = getch();
 }
 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) {
 spi_transaction(0x4C, (addr >> 8) & 0xFF, addr & 0xFF, 0);
}

//#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;
}
uint8_t write_flash(int length) {
 if (param.pagesize < 1) return STK_FAILED;
 //if (param.pagesize != 64) return STK_FAILED;
 int page = current_page(here);
 int x = 0;
 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;
}

uint8_t write_eeprom(int length) {
 // here is a word address, so we use here*2
 // this writes byte-by-byte,
 // page writing may be faster (4 bytes at a time)
 for (int x = 0; x < length; x++) {
   spi_transaction(0xC0, 0x00, here*2+x, buff[x]);
   delay(45);
 }
 return STK_OK;
}

void program_page() {
 char result = (char) STK_FAILED;
 int length = 256 * getch() + getch();
 if (length > 256) {
     Serial.print((char) STK_FAILED);
     return;
 }
 char memtype = getch();
 for (int x = 0; x < length; x++) {
   buff[x] = getch();
 }
 if (CRC_EOP == getch()) {
   Serial.print((char) STK_INSYNC);
   if (memtype == 'F') result = (char)write_flash(length);
   if (memtype == 'E') result = (char)write_eeprom(length);
   Serial.print(result);
 }
 else {
   Serial.print((char) STK_NOSYNC);
 }
}
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
 for (int x = 0; x < length; x++) {
   uint8_t ee = spi_transaction(0xA0, 0x00, here*2+x, 0xFF);
   Serial.print((char) ee);
 }
 return STK_OK;
}

void read_page() {
 char result = (char)STK_FAILED;
 int length = 256 * getch() + getch();
 char memtype = getch();
 if (CRC_EOP != getch()) {
   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()) {
   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);
}
//////////////////////////////////////////
//////////////////////////////////////////

1/2

mysteredding


2/2
Code: [Select]

////////////////////////////////////
////////////////////////////////////
int avrisp() {
  uint8_t data, low, high;
  uint8_t ch = getch();
  switch (ch) {
  case '0': // signon
    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':
    readbytes(20);
    set_parameters();
    empty_reply();
    break;
  case 'E': // extended parameters - ignore for now
    readbytes(5);
    empty_reply();
    break;

  case 'P':
    start_pmode();
    empty_reply();
    break;
  case 'U':
    here = getch() + 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
    read_page();   
    break;

  case 'V':
    universal();
    break;
  case 'Q':
    error=0;
    end_pmode();
    empty_reply();
    break;
   
  case 0x75: //STK_READ_SIGN
    read_signature();
    break;

  // expecting a command, not CRC_EOP
  // this is how we can get back in sync
  case CRC_EOP:
    Serial.print((char) STK_NOSYNC);
    break;
   
  // anything else we will return STK_UNKNOWN
  default:
    if (CRC_EOP == getch())
      Serial.print((char)STK_UNKNOWN);
    else
      Serial.print((char)STK_NOSYNC);
  }
}


mysteredding

2011 version
Code: [Select]

#include "SPI.h"
#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(9600);
  SPI.setDataMode(0);
  SPI.setBitOrder(MSBFIRST);
  // Clock Div can be 2,4,8,16,32,64, or 128
  SPI.setClockDivider(SPI_CLOCK_DIV128);
  pinMode(LED_PMODE, OUTPUT);
  pulse(LED_PMODE, 2);
  pinMode(LED_ERR, OUTPUT);
  pulse(LED_ERR, 2);
  pinMode(LED_HB, OUTPUT);
  pulse(LED_HB, 2);
 
}

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;
  uint8_t flashpoll;
  uint16_t eeprompoll;
  uint16_t pagesize;
  uint16_t eepromsize;
  uint32_t 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(40);
}


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);
}

uint8_t spi_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
  uint8_t n;
  SPI.transfer(a);
  n=SPI.transfer(b);
  //if (n != a) error = -1;
  n=SPI.transfer(c);
  return SPI.transfer(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.begin();
  digitalWrite(RESET, HIGH);
  pinMode(RESET, OUTPUT);
  digitalWrite(SCK, LOW);
  delay(20);
  digitalWrite(RESET, LOW);
  spi_transaction(0xAC, 0x53, 0x00, 0x00);
  pmode = 1;
}

void end_pmode() {
  SPI.end();
  digitalWrite(RESET, HIGH);
  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);
}
//////////////////////////////////////////
//////////////////////////////////////////

1/2

mysteredding

2/2
Code: [Select]

////////////////////////////////////
////////////////////////////////////
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);
    } else {
      error++;
      Serial.print((char) STK_NOSYNC);
    }
    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':
    if (pmode) {
      pulse(LED_ERR, 3);
    } else {
      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);
  }
}



mysteredding

heres my boards.txt, this isnt the full list just what im trying to bootload
Code: [Select]

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

atmega8.name=ATmega8-noxtal @8MHz

atmega8.upload.protocol=arduino
atmega8.upload.maximum_size=7168
atmega8.upload.speed=38400

atmega8.bootloader.low_fuses=0xe4
atmega8.bootloader.high_fuses=0xc2
atmega8.bootloader.path=atmega8_noxtal
atmega8.bootloader.file=ATmegaBOOT.hex
atmega8.bootloader.unlock_bits=0x3F
atmega8.bootloader.lock_bits=0x0F

atmega8.build.mcu=atmega8
atmega8.build.f_cpu=8000000L
atmega8.build.core=arduino
atmega8.build.variant=standard

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

atmega8.name=Arduino NG or older w/ ATmega8

atmega8.upload.protocol=stk500
atmega8.upload.maximum_size=7168
atmega8.upload.speed=19200

atmega8.bootloader.low_fuses=0xdf
atmega8.bootloader.high_fuses=0xca
atmega8.bootloader.path=atmega8
atmega8.bootloader.file=ATmegaBOOT.hex
atmega8.bootloader.unlock_bits=0x3F
atmega8.bootloader.lock_bits=0x0F

atmega8.build.mcu=atmega8
atmega8.build.f_cpu=16000000L
atmega8.build.core=arduino

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

atmega8bb.name=ATmega8 on a breadboard (8 MHz internal clock)

atmega8bb.upload.protocol=stk500
atmega8bb.upload.maximum_size=7168
atmega8bb.upload.speed=19200

atmega8bb.bootloader.low_fuses=0xD4
atmega8bb.bootloader.high_fuses=0xCA
atmega8bb.bootloader.path=arduino:atmega8
atmega8bb.bootloader.file=ATmegaBOOT.hex
atmega8bb.bootloader.unlock_bits=0x3F
atmega8bb.bootloader.lock_bits=0x2F

atmega8bb.build.mcu=atmega8
atmega8bb.build.f_cpu=8000000L
atmega8bb.build.core=arduino:arduino

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

atmega8noxtal.name=ATmega8-noxtal @8MHz

atmega8noxtal.upload.protocol=stk500
atmega8noxtal.upload.maximum_size=7168
atmega8noxtal.upload.speed=19200

atmega8noxtal.bootloader.low_fuses=0xe4
atmega8noxtal.bootloader.high_fuses=0xc4
atmega8noxtal.bootloader.path=atmega8
atmega8noxtal.bootloader.file=ATmegaBOOT.hex
atmega8noxtal.bootloader.unlock_bits=0x3F
atmega8noxtal.bootloader.lock_bits=0x0F

atmega8noxtal.build.mcu=atmega8
atmega8noxtal.build.f_cpu=8000000L
atmega8noxtal.build.core=arduino

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

atmega8.name=Arduino NG8 mhz

atmega8.upload.protocol=stk500
atmega8.upload.maximum_size=7168
atmega8.upload.speed=19200

atmega8.bootloader.low_fuses=0xe4
atmega8.bootloader.high_fuses=0xd1
atmega8.bootloader.path=atmega8
atmega8.bootloader.file=ATmegaBOOT.hex
atmega8.bootloader.unlock_bits=0x3F
atmega8.bootloader.lock_bits=0x0F

atmega8.build.mcu=atmega8
atmega8.build.f_cpu=8000000L
atmega8.build.core=arduino

as you can see ive got quite a few boards ive tried already with avrdude: stk500_recv(): programmer is not responding when i use a pull-up resistor and avrdude: stk500_getsync(): not in sync: resp=0x## with different # each time and even sometimes get avrdude: stk500_getsync(): not in sync: expect:0x## or something to that effect, im stressing hard guys i need your help somebody anybody tell me what im doing wrong and what i need to do to fix this problem, or atleast a step in the right direction, please for the love of god DO NOT "LINK" TUTORIALS just lay out what i need to do HERE

simplex

Follow this tutorial:
http://pdp11.byethost12.com/AVR/ArduinoAsProgrammer.htm
Replace the atmega328p with your atmega8 and do not add any crystal and capacitors.

By doing so you can burn any code you like in your atmega8, including a bootloader if you really want to write one. In most cases, for stand alone applications, the bootloader is not necessary.

dc42

If you don't use a crystal or ceramic resonator on your atmel mcu, then the clock frequency will very probably not be accurate enough for the bootloader to work even if you do manage to burn it. However, you can still use ArduinoISP to upload your sketch directly, without a bootloader. See http://miscsolutions.wordpress.com/2011/08/09/prototyping-small-embedded-projects-with-arduino/ for how I used to do it before I purchased an AVRISPmkII.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

retrolefty


If you don't use a crystal or ceramic resonator on your atmel mcu, then the clock frequency will very probably not be accurate enough for the bootloader to work even if you do manage to burn it. However, you can still use ArduinoISP to upload your sketch directly, without a bootloader. See http://miscsolutions.wordpress.com/2011/08/09/prototyping-small-embedded-projects-with-arduino/ for how I used to do it before I purchased an AVRISPmkII.


Good point. And now that the IDE has a 'upload using programmer' option one can really easily live without messing with bootloaders at all. And seeing that an arduino board running the arduinoISP sketch becomes a hardware programmer, there is a lot to be said for just not messing with bootloaders.

Just think your mega boards won't suffer if you want to use the WDT or if your compiled code happens to have a !!! string for random data in it, it won't hang the upload. One could make a good argument to 'just say no' when the subject of bootloaders come up.  :D

Lefty

mysteredding


Follow this tutorial:
http://pdp11.byethost12.com/AVR/ArduinoAsProgrammer.htm
Replace the atmega328p with your atmega8 and do not add any crystal and capacitors.

By doing so you can burn any code you like in your atmega8, including a bootloader if you really want to write one. In most cases, for stand alone applications, the bootloader is not necessary.

ive been to that page (which i said not to link) and it failed with same errors, try again and this time READ MY POST

Nick Gammon


i cant believe this 9500 character limit so i had to split it into 7 posts, please go forth and read


You can attach files to your post you know. Making 7 posts just to attach files is a mis-use of the forum. You can attach multiple files, or make a single .zip file.

Quote
please for the love of god DO NOT "LINK" TUTORIALS just lay out what i need to do HERE


If there is a tutorial, that explains what to do, why should it be copied and pasted here, when you just have to go to a link? The tutorial might have photos, etc.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

mysteredding



i cant believe this 9500 character limit so i had to split it into 7 posts, please go forth and read


You can attach files to your post you know. Making 7 posts just to attach files is a mis-use of the forum. You can attach multiple files, or make a single .zip file.

Quote
please for the love of god DO NOT "LINK" TUTORIALS just lay out what i need to do HERE


If there is a tutorial, that explains what to do, why should it be copied and pasted here, when you just have to go to a link? The tutorial might have photos, etc.
ive never used forums before and so i didnt know you could attach files, as for the linking im just tired of sifting thru bullshit just to find out it didnt work, i just want short simple answers to whats wrong

AWOL

OP, please DO NOT hit the "report to moderator" control again.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up