2 \author Travis Goodspeed
3 \brief AVR SPI Programmer
16 //! Setup the AVR pins.
23 //! Initialized an attached AVR.
28 //Pulse !RST (SS) at least twice while CLK is low.
42 //! Read and write an SPI byte with delays.
43 unsigned char avrtrans8(unsigned char byte){
44 register unsigned int bit;
45 //This function came from the SPI Wikipedia article.
48 for (bit = 0; bit < 8; bit++) {
49 /* write MOSI on trailing edge of previous clock */
59 /* read MISO on trailing edge */
68 //! Perform a 4-byte exchange.
69 u8 avrexchange(u8 a, u8 b, u8 c, u8 d){
73 //debugstr("AVR sync error, b not returned as c.");
79 //! Enable AVR programming mode.
81 avrexchange(0xAC, 0x53, 0, 0);
84 //! Is the AVR ready or busy?
86 return avrexchange(0xF0, 0, 0, 0);
89 //! Read AVR device code.
91 return avrexchange(0x30, //Read signature byte
98 //! Erase an AVR device
100 avrexchange(0xAC, 0x80, 0, 0);
105 return avrexchange(0x58, 0, 0, 0);
108 void avr_setlock(u8 bits){
109 avrexchange(0xAC,0xE0,0x00,
113 //! Read a byte of EEPROM.
114 u8 avr_peekeeprom(u16 adr){
115 return avrexchange(0xA0, adr>>8, adr&0xFF, 0);
117 //! Read a byte of EEPROM.
118 u8 avr_pokeeeprom(u16 adr, u8 val){
119 return avrexchange(0xC0, adr>>8, adr&0xFF, val);
122 //! Read a byte of Flash
123 u8 avr_peekflash(u16 adr){
125 if(adr&1) //high byte
126 return avrexchange(0x28,a>>8,a&0xff,0);
128 return avrexchange(0x20,a>>8,a&0xff,0);
132 //! Handles an AVR command.
133 void avrhandle(unsigned char app,
138 static u8 connected=0;
141 if(!avr_isready() && connected)
142 debugstr("AVR is not yet ready.");
149 cmddata[i]=avrtrans8(cmddata[i]);
150 txdata(app,verb,len);
156 case START://returns device code
161 cmddata[i]=avr_sig(i);
169 cmddata[0]=avr_lockbits();
173 avr_setlock(cmddata[0]);
177 avr_pokeeeprom(cmddataword[0], cmddata[2]);
180 cmddata[0]=avr_peekeeprom(cmddataword[0]);
184 //cmddata[0]=avr_peekflash(cmddataword[0]);
185 //txdata(app,verb,1);
188 //Fetch large blocks for bulk fetches,
189 //small blocks for individual peeks.
191 len=(cmddataword[1]);//always even.
195 txhead(app,verb,len);
197 serial_tx(avr_peekflash(at++));
202 debugstr("Verb unimplemented in AVR application.");