2 \author Travis Goodspeed
3 \brief AVR SPI Programmer
16 //! Setup the AVR pins.
21 //! Initialized an attached AVR.
24 avrsetup(); //Cut this?
27 //Pulse !RST (SS) at least twice while CLK is low.
41 //! Read and write an SPI byte with delays.
42 u8 avrtrans8(u8 byte){
44 //This function came from the SPI Wikipedia article.
47 for (bit = 0; bit < 8; bit++) {
48 /* write MOSI on trailing edge of previous clock */
58 /* read MISO on trailing edge */
67 //! Perform a 4-byte exchange.
68 u8 avrexchange(u8 a, u8 b, u8 c, u8 d){
72 //debugstr("AVR sync error, b not returned as c.");
78 //! Enable AVR programming mode.
80 avrexchange(0xAC, 0x53, 0, 0);
83 //! Is the AVR ready or busy?
85 return avrexchange(0xF0, 0, 0, 0);
88 //! Read AVR device code.
90 return avrexchange(0x30, //Read signature byte
97 //! Erase an AVR device
99 avrexchange(0xAC, 0x80, 0, 0);
104 return avrexchange(0x58, 0, 0, 0);
107 void avr_setlock(u8 bits){
108 avrexchange(0xAC,0xE0,0x00,
112 //! Read a byte of EEPROM.
113 u8 avr_peekeeprom(u16 adr){
114 return avrexchange(0xA0, adr>>8, adr&0xFF, 0);
116 //! Read a byte of EEPROM.
117 u8 avr_pokeeeprom(u16 adr, u8 val){
118 return avrexchange(0xC0, adr>>8, adr&0xFF, val);
121 //! Read a byte of Flash
122 u8 avr_peekflash(u16 adr){
124 if(adr&1) //high byte
125 return avrexchange(0x28,a>>8,a&0xff,0);
127 return avrexchange(0x20,a>>8,a&0xff,0);
131 //! Handles an AVR command.
132 void avrhandle(unsigned char app,
139 if(!avr_isready() && connected)
140 debugstr("AVR is not yet ready.");
147 cmddata[i]=avrtrans8(cmddata[i]);
148 txdata(app,verb,len);
157 break;//Used to fall through here.
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.");