2 \author Travis Goodspeed
3 \brief AVR SPI Programmer
16 //! Setup the AVR pins.
21 //! Initialized an attached AVR.
24 avrsetup(); //Cut this?
29 //Pulse !RST (SS) at least twice while CLK is low.
44 //! Read and write an SPI byte with delays.
45 u8 avrtrans8(u8 byte){
47 //This function came from the SPI Wikipedia article.
50 for (bit = 0; bit < 8; bit++) {
51 /* write MOSI on trailing edge of previous clock */
61 /* read MISO on trailing edge */
70 //! Perform a 4-byte exchange.
71 u8 avrexchange(u8 a, u8 b, u8 c, u8 d){
75 //debugstr("AVR sync error, b not returned as c.");
81 //! Enable AVR programming mode.
83 avrexchange(0xAC, 0x53, 0, 0);
86 //! Is the AVR ready or busy?
88 return avrexchange(0xF0, 0, 0, 0);
91 //! Read AVR device code.
93 return avrexchange(0x30, //Read signature byte
100 //! Erase an AVR device
102 avrexchange(0xAC, 0x80, 0, 0);
107 return avrexchange(0x58, 0, 0, 0);
110 void avr_setlock(u8 bits){
111 avrexchange(0xAC,0xE0,0x00,
115 //! Read a byte of EEPROM.
116 u8 avr_peekeeprom(u16 adr){
117 return avrexchange(0xA0, adr>>8, adr&0xFF, 0);
119 //! Read a byte of EEPROM.
120 u8 avr_pokeeeprom(u16 adr, u8 val){
121 return avrexchange(0xC0, adr>>8, adr&0xFF, val);
124 //! Read a byte of Flash
125 u8 avr_peekflash(u16 adr){
127 if(adr&1) //high byte
128 return avrexchange(0x28,a>>8,a&0xff,0);
130 return avrexchange(0x20,a>>8,a&0xff,0);
134 //! Handles an AVR command.
135 void avrhandle(unsigned char app,
142 if(!avr_isready() && connected)
143 debugstr("AVR is not yet ready.");
150 cmddata[i]=avrtrans8(cmddata[i]);
151 txdata(app,verb,len);
160 break;//Used to fall through here.
163 cmddata[i]=avr_sig(i);
171 cmddata[0]=avr_lockbits();
175 avr_setlock(cmddata[0]);
179 avr_pokeeeprom(cmddataword[0], cmddata[2]);
182 cmddata[0]=avr_peekeeprom(cmddataword[0]);
186 //cmddata[0]=avr_peekflash(cmddataword[0]);
187 //txdata(app,verb,1);
190 //Fetch large blocks for bulk fetches,
191 //small blocks for individual peeks.
193 len=(cmddataword[1]);//always even.
197 txhead(app,verb,len);
199 serial_tx(avr_peekflash(at++));
204 debugstr("Verb unimplemented in AVR application.");