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 //! Read a byte of EEPROM.
109 u8 avr_peekeeprom(u16 adr){
110 return avrexchange(0xA0, adr>>8, adr&0xFF, 0);
112 //! Read a byte of EEPROM.
113 u8 avr_pokeeeprom(u16 adr, u8 val){
114 return avrexchange(0xC0, adr>>8, adr&0xFF, val);
117 //! Read a byte of Flash
118 u8 avr_peekflash(u16 adr){
120 if(adr&1) //high byte
121 return avrexchange(0x28,a>>8,a&0xff,0);
123 return avrexchange(0x20,a>>8,a&0xff,0);
127 //! Handles an AVR command.
128 void avrhandle(unsigned char app,
133 static u8 connected=0;
135 if(!avr_isready() && connected)
136 debugstr("AVR is not yet ready.");
142 cmddata[i]=avrtrans8(cmddata[i]);
143 txdata(app,verb,len);
149 case START://returns device code
154 cmddata[i]=avr_sig(i);
162 cmddata[0]=avr_lockbits();
167 avr_pokeeeprom(cmddataword[0], cmddata[2]);
170 cmddata[0]=avr_peekeeprom(cmddataword[0]);
174 //cmddata[0]=avr_peekflash(cmddataword[0]);
175 //txdata(app,verb,1);
178 //Fetch large blocks for bulk fetches,
179 //small blocks for individual peeks.
181 len=(cmddataword[1]);//always even.
185 txhead(app,verb,len);
187 serial_tx(avr_peekflash(at++));
192 debugstr("Verb unimplemented in AVR application.");