2015-08-31 22:17:12 +02:00
|
|
|
// ArduinoISP
|
2011-12-29 00:34:05 +01:00
|
|
|
// Copyright (c) 2008-2011 Randall Bohn
|
2013-10-21 09:58:40 +02:00
|
|
|
// If you require a license, see
|
2011-12-29 00:34:05 +01:00
|
|
|
// http://www.opensource.org/licenses/bsd-license.php
|
|
|
|
//
|
|
|
|
// This sketch turns the Arduino into a AVRISP
|
|
|
|
// using the following arduino pins:
|
|
|
|
//
|
2015-08-31 22:17:12 +02:00
|
|
|
// Pin 10 is used to reset the target microcontroller.
|
|
|
|
//
|
2015-09-05 20:43:44 +02:00
|
|
|
// By default, the hardware SPI pins MISO, MOSI and SCK pins are used
|
|
|
|
// to communicate with the target. On all Arduinos, these pins can be found
|
|
|
|
// on the ICSP/SPI header:
|
2015-08-31 22:17:12 +02:00
|
|
|
//
|
|
|
|
// MISO °. . 5V (!) Avoid this pin on Due, Zero...
|
|
|
|
// SCK . . MOSI
|
|
|
|
// . . GND
|
|
|
|
//
|
|
|
|
// On some Arduinos (Uno,...), pins MOSI, MISO and SCK are the same pins
|
|
|
|
// as digital pin 11, 12 and 13, respectively. That is why many tutorials
|
|
|
|
// instruct you to hook up the target to these pins. If you find this wiring
|
|
|
|
// more practical, have a define USE_OLD_STYLE_WIRING. This will work even
|
|
|
|
// even when not using an Uno. (On an Uno this is not needed).
|
2015-09-05 20:43:44 +02:00
|
|
|
//
|
|
|
|
// Alternatively you can use any other digital pin by configuring software ('BitBanged')
|
|
|
|
// SPI and having appropriate defines for PIN_MOSI, PIN_MISO and PIN_SCK.
|
2015-08-31 22:17:12 +02:00
|
|
|
//
|
|
|
|
// IMPORTANT: When using an Arduino that is not 5V tolerant (Due, Zero, ...)
|
|
|
|
// as the programmer, make sure to not expose any of the programmer's pins to 5V.
|
|
|
|
// A simple way to accomplish this is to power the complete system (programmer
|
|
|
|
// and target) at 3V3.
|
2011-12-29 00:34:05 +01:00
|
|
|
//
|
2009-11-01 18:00:31 +01:00
|
|
|
// Put an LED (with resistor) on the following pins:
|
2011-12-29 00:34:05 +01:00
|
|
|
// 9: Heartbeat - shows the programmer is running
|
|
|
|
// 8: Error - Lights up if something goes wrong (use red if that makes sense)
|
2009-11-01 18:00:31 +01:00
|
|
|
// 7: Programming - In communication with the slave
|
|
|
|
//
|
|
|
|
|
2015-07-07 21:42:21 +02:00
|
|
|
#include "Arduino.h"
|
|
|
|
#undef SERIAL
|
|
|
|
|
2009-11-01 18:00:31 +01:00
|
|
|
|
2011-12-29 00:34:05 +01:00
|
|
|
#define PROG_FLICKER true
|
2009-11-01 18:00:31 +01:00
|
|
|
|
2015-09-05 20:41:03 +02:00
|
|
|
// Configure SPI clock (in Hz).
|
|
|
|
// E.g. for an attiny @128 kHz: the datasheet states that both the high
|
|
|
|
// and low spi clock pulse must be > 2 cpu cycles, so take 3 cycles i.e.
|
|
|
|
// divide target f_cpu by 6:
|
|
|
|
// #define SPI_CLOCK (128000/6)
|
2015-07-14 23:06:54 +02:00
|
|
|
//
|
2015-09-05 20:41:03 +02:00
|
|
|
// A clock slow enough for an attiny85 @ 1MHz, is a reasonable default:
|
|
|
|
|
|
|
|
#define SPI_CLOCK (1000000/6)
|
2015-07-14 23:06:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Select hardware or software SPI, depending on SPI clock.
|
|
|
|
// Currently only for AVR, for other archs (Due, Zero,...),
|
|
|
|
// hardware SPI is probably too fast anyway.
|
2015-06-16 22:10:25 +02:00
|
|
|
|
2015-07-14 23:06:54 +02:00
|
|
|
#if defined(ARDUINO_ARCH_AVR)
|
|
|
|
|
|
|
|
#if SPI_CLOCK > (F_CPU / 128)
|
2015-06-16 22:10:25 +02:00
|
|
|
#define USE_HARDWARE_SPI
|
2015-07-14 23:06:54 +02:00
|
|
|
#endif
|
2015-06-16 22:10:25 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-09-05 20:43:44 +02:00
|
|
|
// Configure which pins to use:
|
2015-08-30 20:27:38 +02:00
|
|
|
|
|
|
|
// The standard pin configuration.
|
|
|
|
#ifndef ARDUINO_HOODLOADER2
|
|
|
|
|
|
|
|
#define RESET 10 // Use pin 10 to reset the target rather than SS
|
|
|
|
#define LED_HB 9
|
|
|
|
#define LED_ERR 8
|
|
|
|
#define LED_PMODE 7
|
|
|
|
|
2015-09-05 20:43:44 +02:00
|
|
|
// Uncomment following line to use the old Uno style wiring
|
2015-08-30 20:27:38 +02:00
|
|
|
// (using pin 11, 12 and 13 instead of the SPI header) on Leonardo, Due...
|
|
|
|
|
|
|
|
// #define USE_OLD_STYLE_WIRING
|
|
|
|
|
|
|
|
#ifdef USE_OLD_STYLE_WIRING
|
2015-09-05 20:43:44 +02:00
|
|
|
|
|
|
|
#define PIN_MOSI 11
|
|
|
|
#define PIN_MISO 12
|
|
|
|
#define PIN_SCK 13
|
|
|
|
|
2015-08-30 20:27:38 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// HOODLOADER2 means running sketches on the atmega16u2
|
|
|
|
// serial converter chips on Uno or Mega boards.
|
|
|
|
// We must use pins that are broken out:
|
|
|
|
#else
|
|
|
|
|
2015-09-05 20:43:44 +02:00
|
|
|
#define RESET 4
|
|
|
|
#define LED_HB 7
|
|
|
|
#define LED_ERR 6
|
|
|
|
#define LED_PMODE 5
|
2015-08-30 20:27:38 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-09-05 20:43:44 +02:00
|
|
|
// By default, use hardware SPI pins:
|
|
|
|
#ifndef PIN_MOSI
|
|
|
|
#define PIN_MOSI MOSI
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PIN_MISO
|
|
|
|
#define PIN_MISO MISO
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PIN_SCK
|
|
|
|
#define PIN_SCK SCK
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Force bitbanged SPI if not using the hardware SPI pins:
|
|
|
|
#if (PIN_MISO != MISO) || (PIN_MOSI != MOSI) || (PIN_SCK != SCK)
|
|
|
|
#undef USE_HARDWARE_SPI
|
|
|
|
#endif
|
|
|
|
|
2015-08-30 20:27:38 +02:00
|
|
|
|
2015-07-07 21:42:21 +02:00
|
|
|
// Configure the serial port to use.
|
|
|
|
//
|
|
|
|
// Prefer the USB virtual serial port (aka. native USB port), if the Arduino has one:
|
|
|
|
// - it does not autoreset (except for the magic baud rate of 1200).
|
|
|
|
// - it is more reliable because of USB handshaking.
|
|
|
|
//
|
|
|
|
// Leonardo and similar have an USB virtual serial port: 'Serial'.
|
|
|
|
// Due and Zero have an USB virtual serial port: 'SerialUSB'.
|
|
|
|
//
|
|
|
|
// On the Due and Zero, 'Serial' can be used too, provided you disable autoreset.
|
|
|
|
// To use 'Serial': #define SERIAL Serial
|
|
|
|
|
|
|
|
#ifdef SERIAL_PORT_USBVIRTUAL
|
|
|
|
#define SERIAL SERIAL_PORT_USBVIRTUAL
|
|
|
|
#else
|
|
|
|
#define SERIAL Serial
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-11-01 18:00:31 +01:00
|
|
|
#define HWVER 2
|
|
|
|
#define SWMAJ 1
|
|
|
|
#define SWMIN 18
|
|
|
|
|
|
|
|
// STK Definitions
|
2011-12-29 00:34:05 +01:00
|
|
|
#define STK_OK 0x10
|
|
|
|
#define STK_FAILED 0x11
|
2009-11-01 18:00:31 +01:00
|
|
|
#define STK_UNKNOWN 0x12
|
2011-12-29 00:34:05 +01:00
|
|
|
#define STK_INSYNC 0x14
|
|
|
|
#define STK_NOSYNC 0x15
|
|
|
|
#define CRC_EOP 0x20 //ok it is a space...
|
2009-11-01 18:00:31 +01:00
|
|
|
|
|
|
|
void pulse(int pin, int times);
|
|
|
|
|
2015-06-16 22:10:25 +02:00
|
|
|
#ifdef USE_HARDWARE_SPI
|
|
|
|
#include "SPI.h"
|
|
|
|
#else
|
|
|
|
|
2015-07-14 23:06:54 +02:00
|
|
|
#define SPI_MODE0 0x00
|
|
|
|
|
|
|
|
class SPISettings {
|
|
|
|
public:
|
|
|
|
// clock is in Hz
|
|
|
|
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) : clock(clock){
|
|
|
|
(void) bitOrder;
|
|
|
|
(void) dataMode;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t clock;
|
|
|
|
|
|
|
|
friend class BitBangedSPI;
|
|
|
|
};
|
2015-07-10 14:02:47 +02:00
|
|
|
|
2015-06-16 22:10:25 +02:00
|
|
|
class BitBangedSPI {
|
2015-07-28 22:31:34 +02:00
|
|
|
public:
|
|
|
|
void begin() {
|
2015-09-05 20:43:44 +02:00
|
|
|
digitalWrite(PIN_SCK, LOW);
|
|
|
|
digitalWrite(PIN_MOSI, LOW);
|
|
|
|
pinMode(PIN_SCK, OUTPUT);
|
|
|
|
pinMode(PIN_MOSI, OUTPUT);
|
|
|
|
pinMode(PIN_MISO, INPUT);
|
2015-07-28 22:31:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void beginTransaction(SPISettings settings) {
|
2015-09-05 20:41:03 +02:00
|
|
|
pulseWidth = (500000 + settings.clock - 1) / settings.clock;
|
2015-07-28 22:31:34 +02:00
|
|
|
if (pulseWidth == 0)
|
|
|
|
pulseWidth = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void end() {}
|
|
|
|
|
|
|
|
uint8_t transfer (uint8_t b) {
|
|
|
|
for (unsigned int i = 0; i < 8; ++i) {
|
2015-09-05 20:43:44 +02:00
|
|
|
digitalWrite(PIN_MOSI, (b & 0x80) ? HIGH : LOW);
|
|
|
|
digitalWrite(PIN_SCK, HIGH);
|
2015-07-28 22:31:34 +02:00
|
|
|
delayMicroseconds(pulseWidth);
|
2015-09-05 20:43:44 +02:00
|
|
|
b = (b << 1) | digitalRead(PIN_MISO);
|
|
|
|
digitalWrite(PIN_SCK, LOW); // slow pulse
|
2015-07-28 22:31:34 +02:00
|
|
|
delayMicroseconds(pulseWidth);
|
2015-06-16 22:10:25 +02:00
|
|
|
}
|
2015-07-28 22:31:34 +02:00
|
|
|
return b;
|
|
|
|
}
|
2015-07-14 21:20:39 +02:00
|
|
|
|
2015-07-28 22:31:34 +02:00
|
|
|
private:
|
|
|
|
unsigned long pulseWidth; // in microseconds
|
2015-06-16 22:10:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static BitBangedSPI SPI;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-11-01 18:00:31 +01:00
|
|
|
void setup() {
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.begin(19200);
|
2015-07-14 23:06:54 +02:00
|
|
|
|
2011-12-29 00:34:05 +01:00
|
|
|
pinMode(LED_PMODE, OUTPUT);
|
|
|
|
pulse(LED_PMODE, 2);
|
|
|
|
pinMode(LED_ERR, OUTPUT);
|
|
|
|
pulse(LED_ERR, 2);
|
|
|
|
pinMode(LED_HB, OUTPUT);
|
|
|
|
pulse(LED_HB, 2);
|
2012-02-18 18:11:05 +01:00
|
|
|
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
int error = 0;
|
|
|
|
int pmode = 0;
|
2009-11-01 18:00:31 +01:00
|
|
|
// address for reading and writing, set by 'U' command
|
2015-07-29 21:37:04 +02:00
|
|
|
unsigned int here;
|
2009-11-01 18:00:31 +01:00
|
|
|
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;
|
2012-01-31 13:51:18 +01:00
|
|
|
uint8_t flashpoll;
|
|
|
|
uint16_t eeprompoll;
|
|
|
|
uint16_t pagesize;
|
|
|
|
uint16_t eepromsize;
|
|
|
|
uint32_t flashsize;
|
2012-02-18 18:11:05 +01:00
|
|
|
}
|
2009-11-01 18:00:31 +01:00
|
|
|
parameter;
|
|
|
|
|
|
|
|
parameter param;
|
|
|
|
|
|
|
|
// this provides a heartbeat on pin 9, so you can tell the software is running.
|
2013-10-21 09:58:40 +02:00
|
|
|
uint8_t hbval = 128;
|
|
|
|
int8_t hbdelta = 8;
|
2009-11-01 18:00:31 +01:00
|
|
|
void heartbeat() {
|
2015-02-13 20:48:40 +01:00
|
|
|
static unsigned long last_time = 0;
|
|
|
|
unsigned long now = millis();
|
|
|
|
if ((now - last_time) < 40)
|
|
|
|
return;
|
|
|
|
last_time = now;
|
|
|
|
if (hbval > 192) hbdelta = -hbdelta;
|
|
|
|
if (hbval < 32) hbdelta = -hbdelta;
|
2009-11-01 18:00:31 +01:00
|
|
|
hbval += hbdelta;
|
|
|
|
analogWrite(LED_HB, hbval);
|
|
|
|
}
|
2011-12-29 00:34:05 +01:00
|
|
|
|
2015-02-25 21:52:30 +01:00
|
|
|
static bool rst_active_high;
|
|
|
|
|
2015-07-29 21:37:04 +02:00
|
|
|
void reset_target(bool reset) {
|
2015-02-25 21:52:30 +01:00
|
|
|
digitalWrite(RESET, ((reset && rst_active_high) || (!reset && !rst_active_high)) ? HIGH : LOW);
|
|
|
|
}
|
|
|
|
|
2009-11-01 18:00:31 +01:00
|
|
|
void loop(void) {
|
|
|
|
// is pmode active?
|
2015-07-06 15:18:33 +02:00
|
|
|
if (pmode) {
|
|
|
|
digitalWrite(LED_PMODE, HIGH);
|
|
|
|
} else {
|
|
|
|
digitalWrite(LED_PMODE, LOW);
|
|
|
|
}
|
2009-11-01 18:00:31 +01:00
|
|
|
// is there an error?
|
2015-07-06 15:18:33 +02:00
|
|
|
if (error) {
|
|
|
|
digitalWrite(LED_ERR, HIGH);
|
|
|
|
} else {
|
|
|
|
digitalWrite(LED_ERR, LOW);
|
|
|
|
}
|
2011-12-29 00:34:05 +01:00
|
|
|
|
2009-11-01 18:00:31 +01:00
|
|
|
// light the heartbeat LED
|
|
|
|
heartbeat();
|
2015-07-07 21:42:21 +02:00
|
|
|
if (SERIAL.available()) {
|
2009-11-01 18:00:31 +01:00
|
|
|
avrisp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t getch() {
|
2015-07-07 21:42:21 +02:00
|
|
|
while (!SERIAL.available());
|
|
|
|
return SERIAL.read();
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
2011-12-29 00:34:05 +01:00
|
|
|
void fill(int n) {
|
2009-11-01 18:00:31 +01:00
|
|
|
for (int x = 0; x < n; x++) {
|
2011-12-29 00:34:05 +01:00
|
|
|
buff[x] = getch();
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PTIME 30
|
|
|
|
void pulse(int pin, int times) {
|
|
|
|
do {
|
|
|
|
digitalWrite(pin, HIGH);
|
|
|
|
delay(PTIME);
|
|
|
|
digitalWrite(pin, LOW);
|
|
|
|
delay(PTIME);
|
2015-07-06 15:18:33 +02:00
|
|
|
} while (times--);
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
|
2011-12-29 00:34:05 +01:00
|
|
|
void prog_lamp(int state) {
|
2015-07-06 15:18:33 +02:00
|
|
|
if (PROG_FLICKER) {
|
2011-12-29 00:34:05 +01:00
|
|
|
digitalWrite(LED_PMODE, state);
|
2015-07-06 15:18:33 +02:00
|
|
|
}
|
2011-12-29 00:34:05 +01:00
|
|
|
}
|
|
|
|
|
2009-11-01 18:00:31 +01:00
|
|
|
uint8_t spi_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
|
2012-02-18 18:11:05 +01:00
|
|
|
SPI.transfer(a);
|
2015-07-29 21:37:04 +02:00
|
|
|
SPI.transfer(b);
|
|
|
|
SPI.transfer(c);
|
2012-02-18 18:11:05 +01:00
|
|
|
return SPI.transfer(d);
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void empty_reply() {
|
|
|
|
if (CRC_EOP == getch()) {
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char)STK_INSYNC);
|
|
|
|
SERIAL.print((char)STK_OK);
|
2015-07-06 15:18:33 +02:00
|
|
|
} else {
|
2011-12-29 00:34:05 +01:00
|
|
|
error++;
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char)STK_NOSYNC);
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void breply(uint8_t b) {
|
|
|
|
if (CRC_EOP == getch()) {
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char)STK_INSYNC);
|
|
|
|
SERIAL.print((char)b);
|
|
|
|
SERIAL.print((char)STK_OK);
|
2015-07-06 15:18:33 +02:00
|
|
|
} else {
|
2011-12-29 00:34:05 +01:00
|
|
|
error++;
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char)STK_NOSYNC);
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_version(uint8_t c) {
|
2013-10-21 09:58:40 +02:00
|
|
|
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);
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_parameters() {
|
|
|
|
// call this after reading paramter packet into buff[]
|
|
|
|
param.devicecode = buff[0];
|
2011-12-29 00:34:05 +01:00
|
|
|
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];
|
2013-10-21 09:58:40 +02:00
|
|
|
param.flashpoll = buff[8];
|
2009-11-01 18:00:31 +01:00
|
|
|
// ignore buff[9] (= buff[8])
|
|
|
|
// following are 16 bits (big endian)
|
|
|
|
param.eeprompoll = beget16(&buff[10]);
|
2011-12-29 00:34:05 +01:00
|
|
|
param.pagesize = beget16(&buff[12]);
|
2009-11-01 18:00:31 +01:00
|
|
|
param.eepromsize = beget16(&buff[14]);
|
|
|
|
|
|
|
|
// 32 bits flashsize (big endian)
|
|
|
|
param.flashsize = buff[16] * 0x01000000
|
2013-10-21 09:58:40 +02:00
|
|
|
+ buff[17] * 0x00010000
|
|
|
|
+ buff[18] * 0x00000100
|
|
|
|
+ buff[19];
|
2009-11-01 18:00:31 +01:00
|
|
|
|
2015-02-25 21:52:30 +01:00
|
|
|
// avr devices have active low reset, at89sx are active high
|
|
|
|
rst_active_high = (param.devicecode >= 0xe0);
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void start_pmode() {
|
2015-08-26 22:43:00 +02:00
|
|
|
|
2015-09-05 20:43:44 +02:00
|
|
|
// Reset target before driving PIN_SCK or PIN_MOSI
|
2015-08-26 22:43:00 +02:00
|
|
|
|
|
|
|
// SPI.begin() will configure SS as output,
|
2015-02-25 21:52:30 +01:00
|
|
|
// so SPI master mode is selected.
|
|
|
|
// We have defined RESET as pin 10,
|
|
|
|
// which for many arduino's is not the SS pin.
|
|
|
|
// So we have to configure RESET as output here,
|
2015-08-26 22:43:00 +02:00
|
|
|
// (reset_target() first sets the correct level)
|
|
|
|
reset_target(true);
|
2012-02-18 18:11:05 +01:00
|
|
|
pinMode(RESET, OUTPUT);
|
2015-08-26 22:43:00 +02:00
|
|
|
SPI.begin();
|
|
|
|
SPI.beginTransaction(SPISettings(SPI_CLOCK, MSBFIRST, SPI_MODE0));
|
2015-02-25 21:52:30 +01:00
|
|
|
|
2015-08-26 22:43:00 +02:00
|
|
|
// See avr datasheets, chapter "SERIAL_PRG Programming Algorithm":
|
|
|
|
|
2015-09-05 20:43:44 +02:00
|
|
|
// Pulse RESET after PIN_SCK is low:
|
|
|
|
digitalWrite(PIN_SCK, LOW);
|
|
|
|
delay(20); // discharge PIN_SCK, value arbitrally chosen
|
2015-08-26 22:43:00 +02:00
|
|
|
reset_target(false);
|
|
|
|
// Pulse must be minimum 2 target CPU clock cycles
|
|
|
|
// so 100 usec is ok for CPU speeds above 20KHz
|
|
|
|
delayMicroseconds(100);
|
2015-02-25 21:52:30 +01:00
|
|
|
reset_target(true);
|
2015-08-26 22:43:00 +02:00
|
|
|
|
|
|
|
// Send the enable programming command:
|
|
|
|
delay(50); // datasheet: must be > 20 msec
|
2009-11-01 18:00:31 +01:00
|
|
|
spi_transaction(0xAC, 0x53, 0x00, 0x00);
|
|
|
|
pmode = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void end_pmode() {
|
2012-02-18 18:11:05 +01:00
|
|
|
SPI.end();
|
2015-02-25 21:55:14 +01:00
|
|
|
// We're about to take the target out of reset
|
|
|
|
// so configure SPI pins as input
|
2015-09-05 20:43:44 +02:00
|
|
|
pinMode(PIN_MOSI, INPUT);
|
|
|
|
pinMode(PIN_SCK, INPUT);
|
2015-02-25 21:52:30 +01:00
|
|
|
reset_target(false);
|
2009-11-01 18:00:31 +01:00
|
|
|
pinMode(RESET, INPUT);
|
|
|
|
pmode = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void universal() {
|
|
|
|
uint8_t ch;
|
|
|
|
|
2011-12-29 00:34:05 +01:00
|
|
|
fill(4);
|
2009-11-01 18:00:31 +01:00
|
|
|
ch = spi_transaction(buff[0], buff[1], buff[2], buff[3]);
|
|
|
|
breply(ch);
|
|
|
|
}
|
|
|
|
|
2015-07-29 21:37:04 +02:00
|
|
|
void flash(uint8_t hilo, unsigned int addr, uint8_t data) {
|
2013-10-21 09:58:40 +02:00
|
|
|
spi_transaction(0x40 + 8 * hilo,
|
|
|
|
addr >> 8 & 0xFF,
|
|
|
|
addr & 0xFF,
|
|
|
|
data);
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
2015-07-29 21:37:04 +02:00
|
|
|
void commit(unsigned int addr) {
|
2015-07-06 15:18:33 +02:00
|
|
|
if (PROG_FLICKER) {
|
|
|
|
prog_lamp(LOW);
|
|
|
|
}
|
2009-11-01 18:00:31 +01:00
|
|
|
spi_transaction(0x4C, (addr >> 8) & 0xFF, addr & 0xFF, 0);
|
2011-12-29 00:34:05 +01:00
|
|
|
if (PROG_FLICKER) {
|
|
|
|
delay(PTIME);
|
|
|
|
prog_lamp(HIGH);
|
|
|
|
}
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
|
2015-07-29 21:37:04 +02:00
|
|
|
unsigned int current_page() {
|
2015-07-06 15:18:33 +02:00
|
|
|
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;
|
|
|
|
}
|
2009-11-01 18:00:31 +01:00
|
|
|
return here;
|
|
|
|
}
|
2011-12-29 00:34:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
void write_flash(int length) {
|
|
|
|
fill(length);
|
|
|
|
if (CRC_EOP == getch()) {
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) STK_INSYNC);
|
|
|
|
SERIAL.print((char) write_flash_pages(length));
|
2015-07-06 15:18:33 +02:00
|
|
|
} else {
|
2011-12-29 00:34:05 +01:00
|
|
|
error++;
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) STK_NOSYNC);
|
2011-12-29 00:34:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t write_flash_pages(int length) {
|
2009-11-01 18:00:31 +01:00
|
|
|
int x = 0;
|
2015-07-29 21:37:04 +02:00
|
|
|
unsigned int page = current_page();
|
2009-11-01 18:00:31 +01:00
|
|
|
while (x < length) {
|
2015-07-29 21:37:04 +02:00
|
|
|
if (page != current_page()) {
|
2009-11-01 18:00:31 +01:00
|
|
|
commit(page);
|
2015-07-29 21:37:04 +02:00
|
|
|
page = current_page();
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
flash(LOW, here, buff[x++]);
|
|
|
|
flash(HIGH, here, buff[x++]);
|
|
|
|
here++;
|
|
|
|
}
|
|
|
|
|
|
|
|
commit(page);
|
|
|
|
|
|
|
|
return STK_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-29 00:34:05 +01:00
|
|
|
#define EECHUNK (32)
|
2015-07-29 21:37:04 +02:00
|
|
|
uint8_t write_eeprom(unsigned int length) {
|
2011-12-29 00:34:05 +01:00
|
|
|
// here is a word address, get the byte address
|
2015-07-29 21:37:04 +02:00
|
|
|
unsigned int start = here * 2;
|
|
|
|
unsigned int remaining = length;
|
2011-12-29 00:34:05 +01:00
|
|
|
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
|
2015-07-29 21:37:04 +02:00
|
|
|
uint8_t write_eeprom_chunk(unsigned int start, unsigned int length) {
|
2009-11-01 18:00:31 +01:00
|
|
|
// this writes byte-by-byte,
|
|
|
|
// page writing may be faster (4 bytes at a time)
|
2011-12-29 00:34:05 +01:00
|
|
|
fill(length);
|
|
|
|
prog_lamp(LOW);
|
2015-07-29 21:37:04 +02:00
|
|
|
for (unsigned int x = 0; x < length; x++) {
|
|
|
|
unsigned int addr = start + x;
|
2013-10-21 09:58:40 +02:00
|
|
|
spi_transaction(0xC0, (addr >> 8) & 0xFF, addr & 0xFF, buff[x]);
|
2009-11-01 18:00:31 +01:00
|
|
|
delay(45);
|
2011-12-29 00:34:05 +01:00
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
prog_lamp(HIGH);
|
2009-11-01 18:00:31 +01:00
|
|
|
return STK_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void program_page() {
|
|
|
|
char result = (char) STK_FAILED;
|
2015-07-29 21:37:04 +02:00
|
|
|
unsigned int length = 256 * getch();
|
2011-12-29 00:34:05 +01:00
|
|
|
length += getch();
|
2009-11-01 18:00:31 +01:00
|
|
|
char memtype = getch();
|
2011-12-29 00:34:05 +01:00
|
|
|
// flash memory @here, (length) bytes
|
|
|
|
if (memtype == 'F') {
|
|
|
|
write_flash(length);
|
|
|
|
return;
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
2011-12-29 00:34:05 +01:00
|
|
|
if (memtype == 'E') {
|
|
|
|
result = (char)write_eeprom(length);
|
|
|
|
if (CRC_EOP == getch()) {
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) STK_INSYNC);
|
|
|
|
SERIAL.print(result);
|
2015-07-06 15:18:33 +02:00
|
|
|
} else {
|
2011-12-29 00:34:05 +01:00
|
|
|
error++;
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) STK_NOSYNC);
|
2011-12-29 00:34:05 +01:00
|
|
|
}
|
|
|
|
return;
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char)STK_FAILED);
|
2011-12-29 00:34:05 +01:00
|
|
|
return;
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
2011-12-29 00:34:05 +01:00
|
|
|
|
2015-07-29 21:37:04 +02:00
|
|
|
uint8_t flash_read(uint8_t hilo, unsigned int addr) {
|
2009-11-01 18:00:31 +01:00
|
|
|
return spi_transaction(0x20 + hilo * 8,
|
2013-10-21 09:58:40 +02:00
|
|
|
(addr >> 8) & 0xFF,
|
|
|
|
addr & 0xFF,
|
|
|
|
0);
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char flash_read_page(int length) {
|
2013-10-21 09:58:40 +02:00
|
|
|
for (int x = 0; x < length; x += 2) {
|
2009-11-01 18:00:31 +01:00
|
|
|
uint8_t low = flash_read(LOW, here);
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) low);
|
2009-11-01 18:00:31 +01:00
|
|
|
uint8_t high = flash_read(HIGH, here);
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) high);
|
2009-11-01 18:00:31 +01:00
|
|
|
here++;
|
|
|
|
}
|
|
|
|
return STK_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
char eeprom_read_page(int length) {
|
|
|
|
// here again we have a word address
|
2011-12-29 00:34:05 +01:00
|
|
|
int start = here * 2;
|
2009-11-01 18:00:31 +01:00
|
|
|
for (int x = 0; x < length; x++) {
|
2011-12-29 00:34:05 +01:00
|
|
|
int addr = start + x;
|
|
|
|
uint8_t ee = spi_transaction(0xA0, (addr >> 8) & 0xFF, addr & 0xFF, 0xFF);
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) ee);
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
return STK_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_page() {
|
|
|
|
char result = (char)STK_FAILED;
|
2011-12-29 00:34:05 +01:00
|
|
|
int length = 256 * getch();
|
|
|
|
length += getch();
|
2009-11-01 18:00:31 +01:00
|
|
|
char memtype = getch();
|
|
|
|
if (CRC_EOP != getch()) {
|
2011-12-29 00:34:05 +01:00
|
|
|
error++;
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) STK_NOSYNC);
|
2009-11-01 18:00:31 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) STK_INSYNC);
|
|
|
|
if (memtype == 'F') result = flash_read_page(length);
|
|
|
|
if (memtype == 'E') result = eeprom_read_page(length);
|
|
|
|
SERIAL.print(result);
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void read_signature() {
|
|
|
|
if (CRC_EOP != getch()) {
|
2011-12-29 00:34:05 +01:00
|
|
|
error++;
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) STK_NOSYNC);
|
2009-11-01 18:00:31 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) STK_INSYNC);
|
2009-11-01 18:00:31 +01:00
|
|
|
uint8_t high = spi_transaction(0x30, 0x00, 0x00, 0x00);
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) high);
|
2009-11-01 18:00:31 +01:00
|
|
|
uint8_t middle = spi_transaction(0x30, 0x00, 0x01, 0x00);
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) middle);
|
2009-11-01 18:00:31 +01:00
|
|
|
uint8_t low = spi_transaction(0x30, 0x00, 0x02, 0x00);
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) low);
|
|
|
|
SERIAL.print((char) STK_OK);
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
//////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////
|
|
|
|
////////////////////////////////////
|
2015-07-29 21:37:04 +02:00
|
|
|
void avrisp() {
|
2009-11-01 18:00:31 +01:00
|
|
|
uint8_t ch = getch();
|
|
|
|
switch (ch) {
|
2013-10-21 09:58:40 +02:00
|
|
|
case '0': // signon
|
|
|
|
error = 0;
|
|
|
|
empty_reply();
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
if (getch() == CRC_EOP) {
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) STK_INSYNC);
|
|
|
|
SERIAL.print("AVR ISP");
|
|
|
|
SERIAL.print((char) STK_OK);
|
2013-10-21 09:58:40 +02:00
|
|
|
}
|
2012-02-18 17:11:58 +01:00
|
|
|
else {
|
|
|
|
error++;
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) STK_NOSYNC);
|
2012-02-18 17:11:58 +01:00
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
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':
|
2015-07-13 21:49:42 +02:00
|
|
|
if (!pmode)
|
2012-02-18 15:18:29 +01:00
|
|
|
start_pmode();
|
2013-10-21 09:58:40 +02:00
|
|
|
empty_reply();
|
|
|
|
break;
|
|
|
|
case 'U': // set address (word)
|
|
|
|
here = getch();
|
|
|
|
here += 256 * getch();
|
|
|
|
empty_reply();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x60: //STK_PROG_FLASH
|
2015-07-29 21:37:04 +02:00
|
|
|
getch(); // low addr
|
|
|
|
getch(); // high addr
|
2013-10-21 09:58:40 +02:00
|
|
|
empty_reply();
|
|
|
|
break;
|
|
|
|
case 0x61: //STK_PROG_DATA
|
2015-07-29 21:37:04 +02:00
|
|
|
getch(); // data
|
2013-10-21 09:58:40 +02:00
|
|
|
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;
|
|
|
|
|
2015-07-06 15:18:33 +02:00
|
|
|
// expecting a command, not CRC_EOP
|
|
|
|
// this is how we can get back in sync
|
2013-10-21 09:58:40 +02:00
|
|
|
case CRC_EOP:
|
|
|
|
error++;
|
2015-07-07 21:42:21 +02:00
|
|
|
SERIAL.print((char) STK_NOSYNC);
|
2013-10-21 09:58:40 +02:00
|
|
|
break;
|
2011-12-29 00:34:05 +01:00
|
|
|
|
2015-07-06 15:18:33 +02:00
|
|
|
// anything else we will return STK_UNKNOWN
|
2013-10-21 09:58:40 +02:00
|
|
|
default:
|
|
|
|
error++;
|
2015-07-07 21:42:21 +02:00
|
|
|
if (CRC_EOP == getch())
|
|
|
|
SERIAL.print((char)STK_UNKNOWN);
|
|
|
|
else
|
|
|
|
SERIAL.print((char)STK_NOSYNC);
|
2009-11-01 18:00:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|