Skip to content

Commit aeaed27

Browse files
Randall Bohn (Huckle)Peter Van Hoyweghen
Randall Bohn (Huckle)
authored and
Peter Van Hoyweghen
committed
Use SPI library.
1 parent 8dfe833 commit aeaed27

File tree

1 file changed

+16
-36
lines changed

1 file changed

+16
-36
lines changed

build/shared/examples/11.ArduinoISP/ArduinoISP/ArduinoISP.ino

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
// - The SPI functions herein were developed for the AVR910_ARD programmer
4444
// - More information at http://code.google.com/p/mega-isp
4545

46+
#include "SPI.h"
4647
#include "pins_arduino.h"
4748
#define RESET SS
4849

@@ -67,12 +68,17 @@ void pulse(int pin, int times);
6768

6869
void setup() {
6970
Serial.begin(9600);
71+
SPI.setDataMode(0);
72+
SPI.setBitOrder(MSBFIRST);
73+
// Clock Div can be 2,4,8,16,32,64, or 128
74+
SPI.setClockDivider(SPI_CLOCK_DIV128);
7075
pinMode(LED_PMODE, OUTPUT);
7176
pulse(LED_PMODE, 2);
7277
pinMode(LED_ERR, OUTPUT);
7378
pulse(LED_ERR, 2);
7479
pinMode(LED_HB, OUTPUT);
7580
pulse(LED_HB, 2);
81+
7682
}
7783

7884
int error = 0;
@@ -96,7 +102,7 @@ typedef struct param {
96102
uint16_t pagesize;
97103
uint16_t eepromsize;
98104
uint32_t flashsize;
99-
}
105+
}
100106
parameter;
101107

102108
parameter param;
@@ -164,33 +170,13 @@ void prog_lamp(int state) {
164170
}
165171
}
166172

167-
void spi_init() {
168-
uint8_t x;
169-
SPCR = 0x53;
170-
x = SPSR;
171-
x = SPDR;
172-
}
173-
174-
void spi_wait() {
175-
do {
176-
} while (!(SPSR & (1 << SPIF)));
177-
}
178-
179-
uint8_t spi_send(uint8_t b) {
180-
uint8_t reply;
181-
SPDR = b;
182-
spi_wait();
183-
reply = SPDR;
184-
return reply;
185-
}
186-
187173
uint8_t spi_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
188174
uint8_t n;
189-
spi_send(a);
190-
n = spi_send(b);
175+
SPI.transfer(a);
176+
n = SPI.transfer(b);
191177
//if (n != a) error = -1;
192-
n = spi_send(c);
193-
return spi_send(d);
178+
n = SPI.transfer(c);
179+
return SPI.transfer(d);
194180
}
195181

196182
void empty_reply() {
@@ -259,25 +245,19 @@ void set_parameters() {
259245
}
260246

261247
void start_pmode() {
262-
spi_init();
263-
// following delays may not work on all targets...
264-
pinMode(RESET, OUTPUT);
248+
SPI.begin();
265249
digitalWrite(RESET, HIGH);
266-
pinMode(SCK, OUTPUT);
250+
pinMode(RESET, OUTPUT);
267251
digitalWrite(SCK, LOW);
268-
delay(50);
252+
delay(20);
269253
digitalWrite(RESET, LOW);
270-
delay(50);
271-
pinMode(MISO, INPUT);
272-
pinMode(MOSI, OUTPUT);
273254
spi_transaction(0xAC, 0x53, 0x00, 0x00);
274255
pmode = 1;
275256
}
276257

277258
void end_pmode() {
278-
pinMode(MISO, INPUT);
279-
pinMode(MOSI, INPUT);
280-
pinMode(SCK, INPUT);
259+
SPI.end();
260+
digitalWrite(RESET, HIGH);
281261
pinMode(RESET, INPUT);
282262
pmode = 0;
283263
}

0 commit comments

Comments
 (0)