Skip to content

Improve ArduinoISP sketch #3500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Oct 7, 2015
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a2a06f5
Be more specific about datatypes in the parameter struct.
Jan 31, 2012
d968c7a
Don't start_pmode if we're already in pmode.
Feb 18, 2012
8dfe833
Set error if GET_SIGN_ON not followed by CRC_EOP
Feb 18, 2012
aeaed27
Use SPI library.
Feb 18, 2012
ad2a32f
Avoid delay in heartbeat. That way we can also set the baudrate back …
Feb 13, 2015
93b31b6
Simply use pin 10 to reset the target. It works for all Arduino's and…
Feb 25, 2015
56e0910
The delay between reset and the enter progmode command got lost when …
Feb 25, 2015
15da182
Support at89sx mcu's by adding the possibility to specify the polarit…
Feb 25, 2015
a17a757
Configure SPI pins as input when target is not in reset.
Feb 25, 2015
64e52d8
Implement bitbang SPI for slow clock devices. (Sylvan Butler).
Jun 16, 2015
c8b5e99
Configure the serial port to use. Prefer native USB port if the Ardui…
Jul 7, 2015
eca360b
Fix typo: SPI_CLOCK_DIV128
Jul 10, 2015
5a04ab2
Don't pulse error led if avrdude enters programming mode twice
Jul 13, 2015
50f9e53
Ensure minimum spi pulse width.
Jul 14, 2015
89184a3
Use new style SPI::beginTransaction() api, make SPI_CLOCK configurabl…
Jul 14, 2015
8d95899
Call SPI.beginTransaction() after SPI.begin()
Jul 28, 2015
d271c1c
BitBangedSPI::begin(): initialize levels of SCK and MOSI. Correct ind…
Jul 28, 2015
044a7b7
Fix warnings. Use unsigned int to represent a (word) address: the com…
Jul 29, 2015
13cc57e
Reset target before driving SCK or MOSI, reset sequence as in AVR dat…
Aug 26, 2015
e3866e0
Elaborate configuration of which pins to use: support HOODLOADER2 and…
Aug 30, 2015
2c1728f
Use digitalWrite() only with values HIGH and LOW.
Aug 30, 2015
82e5d4a
Remove change history to make room for documentation.
Aug 31, 2015
bfffb17
Improve SPI clock configuration.
Sep 5, 2015
d2a38f2
Rework pin configuration.
Sep 5, 2015
ff1158a
Add a define for baudrate configuration.
Sep 5, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Configure the serial port to use. Prefer native USB port if the Ardui…
…no has one.
  • Loading branch information
Peter Van Hoyweghen authored and Peter Van Hoyweghen committed Jul 13, 2015
commit c8b5e99267d4eb0979b8f7586dbb2dbb54276251
112 changes: 64 additions & 48 deletions build/shared/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
// - 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"
#include "Arduino.h"
#undef SERIAL

// Use pin 10 to reset the target
#define RESET 10

Expand All @@ -63,6 +65,26 @@

#endif


// 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


#define HWVER 2
#define SWMAJ 1
#define SWMIN 18
Expand Down Expand Up @@ -118,7 +140,7 @@ static BitBangedSPI SPI;
#endif

void setup() {
Serial.begin(19200);
SERIAL.begin(19200);
SPI.setDataMode(0);
SPI.setBitOrder(MSBFIRST);
// Select the slowest possible clock
Expand Down Expand Up @@ -196,14 +218,14 @@ void loop(void) {

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

uint8_t getch() {
while (!Serial.available());
return Serial.read();
while (!SERIAL.available());
return SERIAL.read();
}
void fill(int n) {
for (int x = 0; x < n; x++) {
Expand Down Expand Up @@ -238,22 +260,22 @@ uint8_t spi_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {

void empty_reply() {
if (CRC_EOP == getch()) {
Serial.print((char)STK_INSYNC);
Serial.print((char)STK_OK);
SERIAL.print((char)STK_INSYNC);
SERIAL.print((char)STK_OK);
} else {
error++;
Serial.print((char)STK_NOSYNC);
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);
SERIAL.print((char)STK_INSYNC);
SERIAL.print((char)b);
SERIAL.print((char)STK_OK);
} else {
error++;
Serial.print((char)STK_NOSYNC);
SERIAL.print((char)STK_NOSYNC);
}
}

Expand Down Expand Up @@ -380,11 +402,11 @@ int current_page(int addr) {
void write_flash(int length) {
fill(length);
if (CRC_EOP == getch()) {
Serial.print((char) STK_INSYNC);
Serial.print((char) write_flash_pages(length));
SERIAL.print((char) STK_INSYNC);
SERIAL.print((char) write_flash_pages(length));
} else {
error++;
Serial.print((char) STK_NOSYNC);
SERIAL.print((char) STK_NOSYNC);
}
}

Expand Down Expand Up @@ -451,15 +473,15 @@ void program_page() {
if (memtype == 'E') {
result = (char)write_eeprom(length);
if (CRC_EOP == getch()) {
Serial.print((char) STK_INSYNC);
Serial.print(result);
SERIAL.print((char) STK_INSYNC);
SERIAL.print(result);
} else {
error++;
Serial.print((char) STK_NOSYNC);
SERIAL.print((char) STK_NOSYNC);
}
return;
}
Serial.print((char)STK_FAILED);
SERIAL.print((char)STK_FAILED);
return;
}

Expand All @@ -473,9 +495,9 @@ uint8_t flash_read(uint8_t hilo, int addr) {
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);
SERIAL.print((char) low);
uint8_t high = flash_read(HIGH, here);
Serial.print((char) high);
SERIAL.print((char) high);
here++;
}
return STK_OK;
Expand All @@ -487,7 +509,7 @@ char eeprom_read_page(int length) {
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);
SERIAL.print((char) ee);
}
return STK_OK;
}
Expand All @@ -499,34 +521,29 @@ void read_page() {
char memtype = getch();
if (CRC_EOP != getch()) {
error++;
Serial.print((char) STK_NOSYNC);
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;
SERIAL.print((char) STK_INSYNC);
if (memtype == 'F') result = flash_read_page(length);
if (memtype == 'E') result = eeprom_read_page(length);
SERIAL.print(result);
}

void read_signature() {
if (CRC_EOP != getch()) {
error++;
Serial.print((char) STK_NOSYNC);
SERIAL.print((char) STK_NOSYNC);
return;
}
Serial.print((char) STK_INSYNC);
SERIAL.print((char) STK_INSYNC);
uint8_t high = spi_transaction(0x30, 0x00, 0x00, 0x00);
Serial.print((char) high);
SERIAL.print((char) high);
uint8_t middle = spi_transaction(0x30, 0x00, 0x01, 0x00);
Serial.print((char) middle);
SERIAL.print((char) middle);
uint8_t low = spi_transaction(0x30, 0x00, 0x02, 0x00);
Serial.print((char) low);
Serial.print((char) STK_OK);
SERIAL.print((char) low);
SERIAL.print((char) STK_OK);
}
//////////////////////////////////////////
//////////////////////////////////////////
Expand All @@ -544,13 +561,13 @@ int avrisp() {
break;
case '1':
if (getch() == CRC_EOP) {
Serial.print((char) STK_INSYNC);
Serial.print("AVR ISP");
Serial.print((char) STK_OK);
SERIAL.print((char) STK_INSYNC);
SERIAL.print("AVR ISP");
SERIAL.print((char) STK_OK);
}
else {
error++;
Serial.print((char) STK_NOSYNC);
SERIAL.print((char) STK_NOSYNC);
}
break;
case 'A':
Expand Down Expand Up @@ -615,17 +632,16 @@ int avrisp() {
// this is how we can get back in sync
case CRC_EOP:
error++;
Serial.print((char) STK_NOSYNC);
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);
}
if (CRC_EOP == getch())
SERIAL.print((char)STK_UNKNOWN);
else
SERIAL.print((char)STK_NOSYNC);
}
}