3 \author Scott Livingston
5 \brief dsPIC33F programmer application for the GoodFET. Structure
6 and style is somewhat modeled after avr.c
8 \date March, April 2010
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;
79 debugstr( "Verb unimplemented in PIC application." );
87 void pic33f_trans8( unsigned char byte )
89 /* We only twiddle the PGD and PGC lines.
90 MCLR is assumed to be in the correct state. */
93 DIR_PGD_WR; // Write mode
110 DIR_PGD_RD; // Read mode
113 void pic33f_trans16( unsigned int word )
115 pic33f_trans8( word & 0xff );
116 pic33f_trans8( word >> 8 );
120 void pic33f_six( unsigned int highb, unsigned int loww )
122 /* dsPIC33F/PIC24H instructions have width 24 bits, so we use the
123 lower 8 bits of highb and (all 16 bits of) loww to form the
126 Shift in the instruction. Note that it does not execute until
127 the next 4 clock cycles (which also corresponds to a command
133 for (i = 0; i < 4; i++) {
139 pic33f_trans16( loww );
140 pic33f_trans8( highb );
145 unsigned int pic33f_regout()
148 unsigned int result = 0x0000;
152 // Shift in command (REGOUT: 0001b).
162 for (i = 0; i < 3; i++) {
169 // Pump clock for 8 cycles, and switch PGD direction to read.
170 for (i = 0; i < 7; i++) {
178 /* Now read VISI register (LSb first, as usual).
179 Note that when reading from attached device, data is valid (to
180 be read) on falling clock edges. */
181 for (i = 0; i < 16; i++) {
185 result |= READ_PGD << i;
189 /* One last tick apparently is needed here, at least by the
190 dsPIC33FJ128GP708 chip that I am working with. Note that this
191 is not in the flash programming specs. */
201 void pic33f_sixlist( unsigned int list_len )
204 unsigned int instr_loww;
206 // Bound to Rx buffer size.
207 if (list_len > CMDDATALEN)
208 list_len = CMDDATALEN;
210 // Run each instruction!
211 for (k = 0; k < list_len-2; k+=3) {
212 instr_loww = *(cmddata+k);
213 instr_loww |= (*(cmddata+k+1)) << 8;
214 pic33f_six( *(cmddata+k+2), instr_loww );
217 // Reply with total number of bytes used from Rx buffer.
218 txdata( PIC, PIC_SIXLIST33F, k );
222 unsigned int pic33f_getid()
229 // Read application ID.
230 pic33f_six( 0x04, 0x0200 ); // goto 0x200 (i.e. reset)
231 pic33f_six( 0x00, 0x0000 ); // nop
232 pic33f_six( 0x20, 0x0800 ); // mov #0x80, W0
233 pic33f_six( 0x88, 0x0190 ); // mov W0, TBLPAG
234 pic33f_six( 0x20, 0x7F00 ); // mov #0x7F0, W0
235 pic33f_six( 0x20, 0x7841 ); // mov #VISI, W1
236 pic33f_six( 0x00, 0x0000 ); // nop
237 pic33f_six( 0xBA, 0x0890 ); // TBLRDL [W0], [W1]
238 pic33f_six( 0x00, 0x0000 ); // nop
239 pic33f_six( 0x00, 0x0000 ); // nop
240 result = pic33f_regout();
241 *cmddata = result & 0xff;
245 pic33f_six( 0x20, 0x0FF0 ); // mov #0xFF, W0
246 pic33f_six( 0x88, 0x0190 ); // mov W0, TBLPAG
247 pic33f_six( 0xEB, 0x0000 ); // clr W0
248 pic33f_six( 0x00, 0x0000 ); // nop
249 pic33f_six( 0xBA, 0x08B0 ); // TBLRDL [W0++], [W1]
250 pic33f_six( 0x00, 0x0000 ); // nop
251 pic33f_six( 0x00, 0x0000 ); // nop
252 result = pic33f_regout();
253 *(cmddata+1) = result & 0xff;
254 *(cmddata+2) = result >> 8;
257 // Read hardware revision.
258 pic33f_six( 0xBA, 0x0890 ); // TBLRDL [W0++], [W1]
259 pic33f_six( 0x00, 0x0000 ); // nop
260 pic33f_six( 0x00, 0x0000 ); // nop
261 result = pic33f_regout();
262 *(cmddata+3) = result & 0xff;
263 *(cmddata+4) = result >> 8;
272 void pic33f_connect()
274 unsigned int key_low;
275 unsigned int key_high;
277 key_low = ICSP_KEY_LOW;
278 key_high = ICSP_KEY_HIGH;
293 pic33f_trans8( key_low & 0xff );
294 key_low = key_low >> 8;
295 pic33f_trans8( key_low & 0xff );
296 pic33f_trans8( key_high & 0xff );
297 key_high = key_high >> 8;
298 pic33f_trans8( key_high & 0xff );
301 SET_MCLR; // ...and pull MCLR pin back up.
302 delay_ms(25); // Now wait about 25 ms (required per spec!).
304 /* The first ICSP command must be a SIX, and further, 9 bits are
305 required before the instruction (to be executed), rather than
306 the typical 4 bits. Thus, to simplify code, I simply load a nop
307 here; hence 33 bits are shifted into the dsPIC33F/PIC24H. */
311 for (key_low = 0; key_low < 33; key_low++) {
322 void pic33f_disconnect()