2 \author Travis Goodspeed
3 \brief AVR SPI Programmer
16 //! Handles an AVR command.
17 void avr_handle_fn( uint8_t const app,
21 // define the jtag app's app_t
22 app_t const avr_app = {
34 "\tThe AVR app adds support for debugging AVR based devices.\n"
37 //! Setup the AVR pins.
42 //! Initialized an attached AVR.
45 avrsetup(); //Cut this?
50 //Pulse !RST (SS) at least twice while CLK is low.
65 //! Read and write an SPI byte with delays.
66 u8 avrtrans8(u8 byte){
68 //This function came from the SPI Wikipedia article.
71 for (bit = 0; bit < 8; bit++) {
72 /* write MOSI on trailing edge of previous clock */
82 /* read MISO on trailing edge */
91 //! Perform a 4-byte exchange.
92 u8 avrexchange(u8 a, u8 b, u8 c, u8 d){
96 //debugstr("AVR sync error, b not returned as c.");
102 //! Enable AVR programming mode.
104 avrexchange(0xAC, 0x53, 0, 0);
107 //! Is the AVR ready or busy?
109 return avrexchange(0xF0, 0, 0, 0);
112 //! Read AVR device code.
114 return avrexchange(0x30, //Read signature byte
121 //! Erase an AVR device
123 avrexchange(0xAC, 0x80, 0, 0);
128 return avrexchange(0x58, 0, 0, 0);
131 void avr_setlock(u8 bits){
132 avrexchange(0xAC,0xE0,0x00,
136 //! Read a byte of EEPROM.
137 u8 avr_peekeeprom(u16 adr){
138 return avrexchange(0xA0, adr>>8, adr&0xFF, 0);
140 //! Read a byte of EEPROM.
141 u8 avr_pokeeeprom(u16 adr, u8 val){
142 return avrexchange(0xC0, adr>>8, adr&0xFF, val);
145 //! Read a byte of Flash
146 u8 avr_peekflash(u16 adr){
148 if(adr&1) //high byte
149 return avrexchange(0x28,a>>8,a&0xff,0);
151 return avrexchange(0x20,a>>8,a&0xff,0);
154 void avr_bulk_load(u16 start, u16 len, u8 *data) {
156 for (adr = 0; adr < len; adr++) {
158 avrexchange((adr & 1) ? 0x48 : 0x40,
165 //! Handles an AVR command.
166 void avr_handle_fn( uint8_t const app,
174 if(!avr_isready() && connected)
175 debugstr("AVR is not yet ready.");
182 cmddata[i]=avrtrans8(cmddata[i]);
183 txdata(app,verb,len);
192 break;//Used to fall through here.
195 txdata(app, verb, 0);
198 cmddata[i]=avr_sig(i);
206 cmddata[0]=avr_lockbits();
210 avr_setlock(cmddata[0]);
214 avr_pokeeeprom(cmddataword[0], cmddata[2]);
217 cmddata[0]=avr_peekeeprom(cmddataword[0]);
222 debugstr("Length too short");
226 avr_bulk_load(at, len - 2, cmddata + 2);
227 txdata(app, verb, 0);
231 //cmddata[0]=avr_peekflash(cmddataword[0]);
232 //txdata(app,verb,1);
235 //Fetch large blocks for bulk fetches,
236 //small blocks for individual peeks.
239 l=(cmddataword[1]);//always even.
245 serial_tx(avr_peekflash(at++));
250 debugstr("Verb unimplemented in AVR application.");