3 \author Scott Livingston
5 \brief dsPIC33F programmer application for the GoodFET. Structure
6 and style is somewhat modeled after avr.c
21 // Initialize pins; do NOT begin transaction.
23 P5REN &= ~(PGC|PGD|MCLR);
24 DIR_PGD_WR; // Initially PGD in write mode
30 prep_timer(); // What better time than now?
34 //! Handle a PIC command; currently assumes dsPIC33F/PIC24H
35 void pichandle( unsigned char app,
39 unsigned int nb; // Number of bytes
40 unsigned int highb, loww; // Used for ICSP commands
51 loww |= (*(cmddata+1)) << 8;
53 pic33f_six( highb, loww );
58 pic33f_sixlist( len ); // Reply to host is handled by pic33f_sixlist.
62 loww = pic33f_regout();
63 *cmddata = loww & 0xff;
64 *(cmddata+1) = loww >> 8;
85 debugstr( "Verb unimplemented in PIC application." );
93 void pic33f_trans8( unsigned char byte )
95 /* We only twiddle the PGD and PGC lines.
96 MCLR is assumed to be in the correct state. */
99 DIR_PGD_WR; // Write mode
116 DIR_PGD_RD; // Read mode
119 void pic33f_trans16( unsigned int word )
121 pic33f_trans8( word & 0xff );
122 pic33f_trans8( word >> 8 );
126 void pic33f_six( unsigned int highb, unsigned int loww )
128 /* dsPIC33F/PIC24H instructions have width 24 bits, so we use the
129 lower 8 bits of highb and (all 16 bits of) loww to form the
132 Shift in the instruction. Note that it does not execute until
133 the next 4 clock cycles (which also corresponds to a command
139 for (i = 0; i < 4; i++) {
145 pic33f_trans16( loww );
146 pic33f_trans8( highb );
151 unsigned int pic33f_regout()
154 unsigned int result = 0x0000;
158 // Shift in command (REGOUT: 0001b).
168 for (i = 0; i < 3; i++) {
175 // Pump clock for 8 cycles, and switch PGD direction to read.
176 for (i = 0; i < 7; i++) {
184 /* Now read VISI register (LSb first, as usual).
185 Note that when reading from attached device, data is valid (to
186 be read) on falling clock edges. */
187 for (i = 0; i < 16; i++) {
191 result |= READ_PGD << i;
195 /* One last tick apparently is needed here, at least by the
196 dsPIC33FJ128GP708 chip that I am working with. Note that this
197 is not in the flash programming specs. */
207 void pic33f_sixlist( unsigned int list_len )
210 unsigned int instr_loww;
212 // Bound to Rx buffer size.
213 if (list_len > CMDDATALEN)
214 list_len = CMDDATALEN;
216 // Run each instruction!
217 for (k = 0; k < list_len-2; k+=3) {
218 instr_loww = *(cmddata+k);
219 instr_loww |= (*(cmddata+k+1)) << 8;
220 pic33f_six( *(cmddata+k+2), instr_loww );
223 // Reply with total number of bytes used from Rx buffer.
224 txdata( PIC, PIC_SIXLIST33F, k );
228 unsigned int pic33f_getid()
235 // Read application ID.
236 pic33f_six( 0x04, 0x0200 ); // goto 0x200 (i.e. reset)
237 pic33f_six( 0x00, 0x0000 ); // nop
238 pic33f_six( 0x20, 0x0800 ); // mov #0x80, W0
239 pic33f_six( 0x88, 0x0190 ); // mov W0, TBLPAG
240 pic33f_six( 0x20, 0x7F00 ); // mov #0x7F0, W0
241 pic33f_six( 0x20, 0x7841 ); // mov #VISI, W1
242 pic33f_six( 0x00, 0x0000 ); // nop
243 pic33f_six( 0xBA, 0x0890 ); // TBLRDL [W0], [W1]
244 pic33f_six( 0x00, 0x0000 ); // nop
245 pic33f_six( 0x00, 0x0000 ); // nop
246 result = pic33f_regout();
247 *cmddata = result & 0xff;
251 pic33f_six( 0x20, 0x0FF0 ); // mov #0xFF, W0
252 pic33f_six( 0x88, 0x0190 ); // mov W0, TBLPAG
253 pic33f_six( 0xEB, 0x0000 ); // clr W0
254 pic33f_six( 0x00, 0x0000 ); // nop
255 pic33f_six( 0xBA, 0x08B0 ); // TBLRDL [W0++], [W1]
256 pic33f_six( 0x00, 0x0000 ); // nop
257 pic33f_six( 0x00, 0x0000 ); // nop
258 result = pic33f_regout();
259 *(cmddata+1) = result & 0xff;
260 *(cmddata+2) = result >> 8;
263 // Read hardware revision.
264 pic33f_six( 0xBA, 0x0890 ); // TBLRDL [W0++], [W1]
265 pic33f_six( 0x00, 0x0000 ); // nop
266 pic33f_six( 0x00, 0x0000 ); // nop
267 result = pic33f_regout();
268 *(cmddata+3) = result & 0xff;
269 *(cmddata+4) = result >> 8;
278 void pic33f_connect()
280 unsigned int key_low;
281 unsigned int key_high;
283 key_low = ICSP_KEY_LOW;
284 key_high = ICSP_KEY_HIGH;
299 pic33f_trans8( key_low & 0xff );
300 key_low = key_low >> 8;
301 pic33f_trans8( key_low & 0xff );
302 pic33f_trans8( key_high & 0xff );
303 key_high = key_high >> 8;
304 pic33f_trans8( key_high & 0xff );
307 SET_MCLR; // ...and pull MCLR pin back up.
308 delay_ms(25); // Now wait about 25 ms (required per spec!).
310 /* The first ICSP command must be a SIX, and further, 9 bits are
311 required before the instruction (to be executed), rather than
312 the typical 4 bits. Thus, to simplify code, I simply load a nop
313 here; hence 33 bits are shifted into the dsPIC33F/PIC24H. */
317 for (key_low = 0; key_low < 33; key_low++) {
328 void pic33f_disconnect()