Skip to content

Commit adb8831

Browse files
Randall Bohn (Huckle)
authored andcommitted
Use SPI library.
1 parent cf13508 commit adb8831

File tree

1 file changed

+16
-37
lines changed

1 file changed

+16
-37
lines changed

build/shared/examples/ArduinoISP/ArduinoISP.ino

Lines changed: 16 additions & 37 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;
@@ -154,34 +160,13 @@ void prog_lamp(int state) {
154160
digitalWrite(LED_PMODE, state);
155161
}
156162

157-
void spi_init() {
158-
uint8_t x;
159-
SPCR = 0x53;
160-
x = SPSR;
161-
x = SPDR;
162-
}
163-
164-
void spi_wait() {
165-
do {
166-
}
167-
while (!(SPSR & (1 << SPIF)));
168-
}
169-
170-
uint8_t spi_send(uint8_t b) {
171-
uint8_t reply;
172-
SPDR = b;
173-
spi_wait();
174-
reply = SPDR;
175-
return reply;
176-
}
177-
178163
uint8_t spi_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
179164
uint8_t n;
180-
spi_send(a);
181-
n = spi_send(b);
165+
SPI.transfer(a);
166+
n = SPI.transfer(b);
182167
//if (n != a) error = -1;
183-
n = spi_send(c);
184-
return spi_send(d);
168+
n = SPI.transfer(c);
169+
return SPI.transfer(d);
185170
}
186171

187172
void empty_reply() {
@@ -252,25 +237,19 @@ void set_parameters() {
252237
}
253238

254239
void start_pmode() {
255-
spi_init();
256-
// following delays may not work on all targets...
257-
pinMode(RESET, OUTPUT);
240+
SPI.begin();
258241
digitalWrite(RESET, HIGH);
259-
pinMode(SCK, OUTPUT);
242+
pinMode(RESET, OUTPUT);
260243
digitalWrite(SCK, LOW);
261-
delay(50);
244+
delay(20);
262245
digitalWrite(RESET, LOW);
263-
delay(50);
264-
pinMode(MISO, INPUT);
265-
pinMode(MOSI, OUTPUT);
266246
spi_transaction(0xAC, 0x53, 0x00, 0x00);
267247
pmode = 1;
268248
}
269249

270250
void end_pmode() {
271-
pinMode(MISO, INPUT);
272-
pinMode(MOSI, INPUT);
273-
pinMode(SCK, INPUT);
251+
SPI.end();
252+
digitalWrite(RESET, HIGH);
274253
pinMode(RESET, INPUT);
275254
pmode = 0;
276255
}

0 commit comments

Comments
 (0)