2 \author Travis Goodspeed
3 \brief AVR SPI Programmer
15 //! Setup the AVR pins.
20 //! Initialized an attached AVR.
25 //Pulse !RST (SS) at least twice while CLK is low.
39 //! Read and write an SPI byte with delays.
40 unsigned char avrtrans8(unsigned char byte){
41 register unsigned int bit;
42 //This function came from the SPI Wikipedia article.
45 for (bit = 0; bit < 8; bit++) {
46 /* write MOSI on trailing edge of previous clock */
56 /* read MISO on trailing edge */
65 //! Perform a 4-byte exchange.
66 u8 avrexchange(u8 a, u8 b, u8 c, u8 d){
70 debugstr("AVR sync error, b not returned as c.");
76 //! Enable AVR programming mode.
78 avrexchange(0xAC, 0x53, 0, 0);
81 //! Is the AVR ready or busy?
83 return avrexchange(0xF0, 0, 0, 0);
86 //! Read AVR device code.
88 return avrexchange(0x30, //Read signature byte
95 //! Erase an AVR device
97 avrexchange(0xAC, 0x80, 0, 0);
102 return avrexchange(0x58, 0, 0, 0);
105 //! Read a byte of EEPROM.
106 u8 avr_peekeeprom(u16 adr){
107 return avrexchange(0xA0, adr>>8, adr&0xFF, 0);
109 //! Read a byte of EEPROM.
110 u8 avr_pokeeeprom(u16 adr, u8 val){
111 return avrexchange(0xC0, adr>>8, adr&0xFF, val);
116 //! Handles an AVR command.
117 void avrhandle(unsigned char app,
121 static u8 connected=0;
123 if(!avr_isready() && connected)
124 debugstr("AVR is not yet ready.");
130 cmddata[i]=avrtrans8(cmddata[i]);
131 txdata(app,verb,len);
137 case START://returns device code
142 cmddata[i]=avr_sig(i);
150 cmddata[0]=avr_lockbits();
155 avr_pokeeeprom(cmddataword[0], cmddata[2]);
158 cmddata[0]=avr_peekeeprom(cmddataword[0]);
164 debugstr("Verb unimplemented in AVR application.");