WEAKDEFed ejtaghandle.
[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, April 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 #define ICSP_KEY_LOW 0xC2B2
21 #define ICSP_KEY_HIGH 0x8A12
22 #define APPID_ADDR 0x8007F0
23
24 //! I/O (pin level); follows spi.h
25 #define PGD BIT1
26 #define PGC BIT3
27 #define MCLR BIT0
28
29 //Handle bidirectionality of PGD
30 #define DIR_PGD_RD P5DIR&=~PGD
31 #define DIR_PGD_WR P5DIR|=PGD
32
33 #define SET_MCLR P5OUT|=MCLR
34 #define CLR_MCLR P5OUT&=~MCLR
35 #define SET_PGD P5OUT|=PGD
36 #define CLR_PGD P5OUT&=~PGD
37 #define SET_PGC P5OUT|=PGC
38 #define CLR_PGC P5OUT&=~PGC
39
40 #define READ_PGD (P5IN&PGD?1:0)
41
42
43 //! Set pins as appropriate for dsPIC33F/PIC24H
44 void pic33f_setup();
45
46 //! Start ICSP with an attached dsPIC33F/PIC24H
47 void pic33f_connect();
48
49 /*! Stop ICSP session; maybe not necessary, but cleaner since a
50     minimum delay is required before dropping MCLR pin after last
51     transmission to dsPIC33F/PIC24H chip. */
52 void pic33f_disconnect();
53
54 //! ICSP SIX command: execute single command on attached dsPIC33F/PIC24H.
55 void pic33f_six( unsigned int highb, unsigned int loww );
56
57 //! ICSP REGOUT command: read contents of VISI register
58 unsigned int pic33f_regout();
59
60 //! Execute a list of commands on attached dsPIC33F/PIC24H.
61 void pic33f_sixlist( unsigned int list_len );
62
63 //! Start Enhanced ICSP session with dsPIC33F/PIC24H (assumes Programming Executive is present).
64 void pic33f_eicsp_connect();
65
66 /*! Get Application ID (u8), Device ID (i.e. DEVID; u16) and hardware
67     revision (u16). Results are dumped to the host connection (a la
68     txdata, app is PIC and verb is PIC_DEVID33F).
69
70     This function assumes no ICSP (or Enhanced ICSP) session is currently open!
71
72         Returns number of bytes written to cmddata buffer.
73 */
74 unsigned int pic33f_getid();
75
76 //! Write word
77 void pic33f_trans16( unsigned int word );
78
79 //! Write byte
80 void pic33f_trans8( unsigned char byte );
81
82  
83 //Command codes
84 #define PIC_DEVID33F   0x81 //! Read Device and Application ID
85 #define PIC_SIX33F     0x82 //! ICSP six command; execute instruction on target.
86 #define PIC_REGOUT33F  0x83 //! Read out VISI register.
87 #define PIC_SIXLIST33F 0x86 /* Buffers list of instructions to MSP430,
88                                                            then executes them over ICSP session
89                                                            with target dsPIC33F/PIC24H chip. */
90
91 #define PIC_START33F   0x84 // Start ICSP session
92 #define PIC_STOP33F    0x85 // Stop ICSP (basically, drop !MCLR pin and pause briefly)