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 debugstr("Setting lock bits.");
110 avrexchange(0xAC,0xE0,0x00,
114 //! Read a byte of EEPROM.
115 u8 avr_peekeeprom(u16 adr){
116 return avrexchange(0xA0, adr>>8, adr&0xFF, 0);
118 //! Read a byte of EEPROM.
119 u8 avr_pokeeeprom(u16 adr, u8 val){
120 return avrexchange(0xC0, adr>>8, adr&0xFF, val);
123 //! Read a byte of Flash
124 u8 avr_peekflash(u16 adr){
126 if(adr&1) //high byte
127 return avrexchange(0x28,a>>8,a&0xff,0);
129 return avrexchange(0x20,a>>8,a&0xff,0);
133 //! Handles an AVR command.
134 void avrhandle(unsigned char app,
139 static u8 connected=0;
141 if(!avr_isready() && connected)
142 debugstr("AVR is not yet ready.");
148 cmddata[i]=avrtrans8(cmddata[i]);
149 txdata(app,verb,len);
155 case START://returns device code
160 cmddata[i]=avr_sig(i);
168 cmddata[0]=avr_lockbits();
172 avr_setlock(cmddata[0]);
176 avr_pokeeeprom(cmddataword[0], cmddata[2]);
179 cmddata[0]=avr_peekeeprom(cmddataword[0]);
183 //cmddata[0]=avr_peekflash(cmddataword[0]);
184 //txdata(app,verb,1);
187 //Fetch large blocks for bulk fetches,
188 //small blocks for individual peeks.
190 len=(cmddataword[1]);//always even.
194 txhead(app,verb,len);
196 serial_tx(avr_peekflash(at++));
201 debugstr("Verb unimplemented in AVR application.");