2 \author Travis Goodspeed
3 \brief AVR SPI Programmer
16 //! Setup the AVR pins.
21 //! Initialized an attached AVR.
26 //Pulse !RST (SS) at least twice while CLK is low.
40 //! Read and write an SPI byte with delays.
41 u8 avrtrans8(u8 byte){
43 //This function came from the SPI Wikipedia article.
46 for (bit = 0; bit < 8; bit++) {
47 /* write MOSI on trailing edge of previous clock */
57 /* read MISO on trailing edge */
66 //! Perform a 4-byte exchange.
67 u8 avrexchange(u8 a, u8 b, u8 c, u8 d){
71 //debugstr("AVR sync error, b not returned as c.");
77 //! Enable AVR programming mode.
79 avrexchange(0xAC, 0x53, 0, 0);
82 //! Is the AVR ready or busy?
84 return avrexchange(0xF0, 0, 0, 0);
87 //! Read AVR device code.
89 return avrexchange(0x30, //Read signature byte
96 //! Erase an AVR device
98 avrexchange(0xAC, 0x80, 0, 0);
103 return avrexchange(0x58, 0, 0, 0);
106 void avr_setlock(u8 bits){
107 avrexchange(0xAC,0xE0,0x00,
111 //! Read a byte of EEPROM.
112 u8 avr_peekeeprom(u16 adr){
113 return avrexchange(0xA0, adr>>8, adr&0xFF, 0);
115 //! Read a byte of EEPROM.
116 u8 avr_pokeeeprom(u16 adr, u8 val){
117 return avrexchange(0xC0, adr>>8, adr&0xFF, val);
120 //! Read a byte of Flash
121 u8 avr_peekflash(u16 adr){
123 if(adr&1) //high byte
124 return avrexchange(0x28,a>>8,a&0xff,0);
126 return avrexchange(0x20,a>>8,a&0xff,0);
130 //! Handles an AVR command.
131 void avrhandle(unsigned char app,
136 static u8 connected=0;
139 if(!avr_isready() && connected)
140 debugstr("AVR is not yet ready.");
147 cmddata[i]=avrtrans8(cmddata[i]);
148 txdata(app,verb,len);
154 case START://returns device code
159 cmddata[i]=avr_sig(i);
167 cmddata[0]=avr_lockbits();
171 avr_setlock(cmddata[0]);
175 avr_pokeeeprom(cmddataword[0], cmddata[2]);
178 cmddata[0]=avr_peekeeprom(cmddataword[0]);
182 //cmddata[0]=avr_peekflash(cmddataword[0]);
183 //txdata(app,verb,1);
186 //Fetch large blocks for bulk fetches,
187 //small blocks for individual peeks.
189 len=(cmddataword[1]);//always even.
193 txhead(app,verb,len);
195 serial_tx(avr_peekflash(at++));
200 debugstr("Verb unimplemented in AVR application.");