Autotuner is ready for action\!
[goodfet] / firmware / include / dspic33f.h
1 /*! \file dspic33f.h
2
3   \author Scott Livingston
4
5   \brief Definitions for the dsPIC33F programmer application. Note
6          that the programming for dsPIC33F/PIC24H chips is
7          non-standard 2-wire SPI; hence, I do not use the existing
8          GoodFET SPI routines.
9
10   \date March-June 2010
11
12 */
13
14
15 /*! Magic, device family specific constants (these are drawn from the
16     dsPIC33F/PIC24H Flash Programming Specification). Note that the
17     ICSP key is in bit-reversed order, since it is the only thing that
18     is sent MSb first (hence, we flip the bit order and use our usual
19     LSb-first routines).
20
21     Per the dsPIC33F/PIC24H and PIC24F flash programming
22     specifications, the ICSP key is 0x4D434851. */
23 #define ICSP_KEY_LOW 0xC2B2
24 #define ICSP_KEY_HIGH 0x8A12
25 #define APPID_ADDR 0x8007F0
26
27 //! I/O (pin level); follows spi.h
28 #define PGD BIT1
29 #define PGC BIT3
30 #define MCLR BIT0
31
32 //Handle bidirectionality of PGD
33 #define DIR_PGD_RD P5DIR&=~PGD
34 #define DIR_PGD_WR P5DIR|=PGD
35
36 #define SET_MCLR P5OUT|=MCLR
37 #define CLR_MCLR P5OUT&=~MCLR
38 #define SET_PGD P5OUT|=PGD
39 #define CLR_PGD P5OUT&=~PGD
40 #define SET_PGC P5OUT|=PGC
41 #define CLR_PGC P5OUT&=~PGC
42
43 #define READ_PGD (P5IN&PGD?1:0)
44
45
46 //! Set pins as appropriate for dsPIC33F/PIC24H
47 void pic33f_setup();
48
49 //! Start ICSP with an attached dsPIC33F/PIC24H
50 void pic33f_connect();
51
52 /*! Stop ICSP session; maybe not necessary, but cleaner since a
53     minimum delay is required before dropping MCLR pin after last
54     transmission to dsPIC33F/PIC24H chip. */
55 void pic33f_disconnect();
56
57 //! ICSP SIX command: execute single command on attached dsPIC33F/PIC24H.
58 void pic33f_six( unsigned int highb, unsigned int loww );
59
60 //! ICSP REGOUT command: read contents of VISI register
61 unsigned int pic33f_regout();
62
63 //! Execute a list of commands on attached dsPIC33F/PIC24H.
64 void pic33f_sixlist( unsigned int list_len );
65
66 //! Start Enhanced ICSP session with dsPIC33F/PIC24H (assumes Programming Executive is present).
67 void pic33f_eicsp_connect();
68
69 /*! Get Application ID (u8), Device ID (i.e. DEVID; u16) and hardware
70     revision (u16). Results are dumped to the host connection (a la
71     txdata, app is PIC and verb is PIC_DEVID33F).
72
73     This function assumes no ICSP (or Enhanced ICSP) session is currently open!
74
75         Returns number of bytes written to cmddata buffer.
76 */
77 unsigned int pic33f_getid();
78
79 //! Write word
80 void pic33f_trans16( unsigned int word );
81
82 //! Write byte
83 void pic33f_trans8( unsigned char byte );
84
85  
86 //Command codes
87 #define PIC_DEVID33F   0x81 //! Read Device and Application ID
88 #define PIC_SIX33F     0x82 //! ICSP six command; execute instruction on target.
89 #define PIC_REGOUT33F  0x83 //! Read out VISI register.
90 #define PIC_SIXLIST33F 0x86 /* Buffers list of instructions to MSP430,
91                                                            then executes them over ICSP session
92                                                            with target dsPIC33F/PIC24H chip. */
93
94 #define PIC_RESET33F   0x87 // Reset attached dsPIC33F/PIC24H chip.
95 #define PIC_START33F   0x84 // Start ICSP session
96 #define PIC_STOP33F    0x85 // Stop ICSP (basically, drop !MCLR pin and pause briefly)