2 \author Travis Goodspeed
3 \brief AVR SPI Programmer
13 //! Handles an AVR command.
14 void avr_handle_fn( uint8_t const app,
18 // define the jtag app's app_t
19 app_t const avr_app = {
31 "\tThe AVR app adds support for debugging AVR based devices.\n"
34 //! Setup the AVR pins.
39 //! Initialized an attached AVR.
42 avrsetup(); //Cut this?
47 //Pulse !RST (SS) at least twice while CLK is low.
62 //! Read and write an SPI byte with delays.
63 u8 avrtrans8(u8 byte){
65 //This function came from the SPI Wikipedia article.
68 for (bit = 0; bit < 8; bit++) {
69 /* write MOSI on trailing edge of previous clock */
79 /* read MISO on trailing edge */
88 //! Perform a 4-byte exchange.
89 u8 avrexchange(u8 a, u8 b, u8 c, u8 d){
93 //debugstr("AVR sync error, b not returned as c.");
99 //! Enable AVR programming mode.
101 avrexchange(0xAC, 0x53, 0, 0);
104 //! Is the AVR ready or busy?
106 return avrexchange(0xF0, 0, 0, 0);
109 //! Read AVR device code.
111 return avrexchange(0x30, //Read signature byte
118 //! Erase an AVR device
120 avrexchange(0xAC, 0x80, 0, 0);
125 return avrexchange(0x58, 0, 0, 0);
128 void avr_setlock(u8 bits){
129 avrexchange(0xAC,0xE0,0x00,
133 //! Read a byte of EEPROM.
134 u8 avr_peekeeprom(u16 adr){
135 return avrexchange(0xA0, adr>>8, adr&0xFF, 0);
137 //! Read a byte of EEPROM.
138 u8 avr_pokeeeprom(u16 adr, u8 val){
139 return avrexchange(0xC0, adr>>8, adr&0xFF, val);
142 //! Read a byte of Flash
143 u8 avr_peekflash(u16 adr){
145 if(adr&1) //high byte
146 return avrexchange(0x28,a>>8,a&0xff,0);
148 return avrexchange(0x20,a>>8,a&0xff,0);
151 void avr_bulk_load(u16 start, u16 len, u8 *data) {
153 for (adr = 0; adr < len; adr++) {
155 avrexchange((adr & 1) ? 0x48 : 0x40,
162 //! Handles an AVR command.
163 void avr_handle_fn( uint8_t const app,
171 if(!avr_isready() && connected)
172 debugstr("AVR is not yet ready.");
179 cmddata[i]=avrtrans8(cmddata[i]);
180 txdata(app,verb,len);
189 break;//Used to fall through here.
192 txdata(app, verb, 0);
195 cmddata[i]=avr_sig(i);
203 cmddata[0]=avr_lockbits();
207 avr_setlock(cmddata[0]);
211 avr_pokeeeprom(cmddataword[0], cmddata[2]);
214 cmddata[0]=avr_peekeeprom(cmddataword[0]);
219 debugstr("Length too short");
223 avr_bulk_load(at, len - 2, cmddata + 2);
224 txdata(app, verb, 0);
228 //cmddata[0]=avr_peekflash(cmddataword[0]);
229 //txdata(app,verb,1);
232 //Fetch large blocks for bulk fetches,
233 //small blocks for individual peeks.
236 l=(cmddataword[1]);//always even.
242 serial_tx(avr_peekflash(at++));
247 debugstr("Verb unimplemented in AVR application.");