1 /*
2  * Copyright (C) 2005-2019 Darron Broad
3  * All rights reserved.
4  *
5  * This file is part of Pickle Microchip PIC ICSP.
6  *
7  * Pickle Microchip PIC ICSP is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as published
9  * by the Free Software Foundation.
10  *
11  * Pickle Microchip PIC ICSP is distributed in the hope that it will be
12  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with Pickle Microchip PIC ICSP. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef _PIC_H
21 #define _PIC_H
22 
23 /* ICSP DATA */
24 #define PIC_BYTLEN (256)
25 typedef struct {
26 	uint32_t address;     /* Address of data */
27 	uint16_t nbytes;      /* Number of bytes */
28 	uint8_t bytes[PIC_BYTLEN]; /* Data bytes */
29 } pic_data;
30 
31 /* ISCP OPERATIONS */
32 struct pic_ops {
33 	uint32_t arch;	/* bit mask */
34 	uint16_t align;	/* hex input alignment */
35 	void (*selector)(void);
36 	void (*bootloader)(void);
37 	void (*program_begin)(void);
38 	uint32_t (*program_data)(uint32_t, pic_data *);
39 	void (*program_end)(int);
40 	void (*verify_begin)(void);
41 	uint32_t (*verify_data)(uint32_t, pic_data *, uint32_t *);
42 	void (*verify_end)(void);
43 	void (*view_data)(pic_data *);
44 	int (*read_config_memory)(void);
45 	uint32_t (*get_program_count)(void);
46 	uint32_t (*get_program_size)(uint32_t *, uint32_t);
47 	uint32_t (*get_data_size)(uint32_t *);
48 	uint32_t (*get_executive_size)(uint32_t *);
49 	uint32_t (*get_boot_size)(uint32_t *);
50 	uint32_t (*read_program_memory_block)(uint32_t *, uint32_t, uint32_t);
51 	uint32_t (*read_data_memory_block)(uint16_t *, uint32_t, uint16_t);
52 	void (*write_panel)(uint32_t, uint32_t, uint32_t *, uint32_t);
53 	void (*bulk_erase)(void);
54 	uint32_t (*write_osccal)(uint16_t);
55 	uint32_t (*write_bandgap)(uint16_t);
56 	uint32_t (*write_calib)(uint16_t, uint16_t);
57 	void (*row_erase)(uint32_t, uint32_t);
58 	void (*dumpdeviceid)(void);
59 	void (*dumpconfig)(uint32_t, uint32_t);
60 	void (*dumposccal)(void);
61 	void (*dumpdevice)(void);
62 	uint32_t dumpadj;
63 	void (*dumphexcode)(uint32_t, uint32_t, uint32_t *);
64 	void (*dumpinhxcode)(uint32_t, uint32_t, uint32_t *);
65 	void (*dumphexdata)(uint32_t, uint32_t, uint16_t *);
66 	void (*dumpinhxdata)(uint32_t, uint32_t, uint16_t *);
67 	void (*debug)(void);
68 };
69 
70 /*
71  * 32-bit keys
72  */
73 
74 /* ENHANCED MID-RANGE ICSP             = MCHP (0x4D434850)          */
75 #define MCHPKEY (0x4D434850)
76 
77 /* PIC18/dsPIC33/PIC24 EXECUTIVE       = MCHP (0x4D434850) REVERSED */
78 #define PHCMKEY (0x0A12C2B2)
79 
80 /* PIC24 ICSP                          = MCHQ (0x4D434851) REVERSED */
81 #define QHCMKEY (0x8A12C2B2)
82 
83 /* dsPIC33F DS70152B-page 37 ICSP/STDP = ???? (0x5B982073) REVERSED */
84 #define P30FKEY (0xCE0419DA)
85 
86 uint32_t pic_arch(const char *);
87 
88 int pic_cmp(const void *, const void *);
89 #define PIC_NCOLS (4)
90 void pic_selector(void);
91 void pic_bootloader(void);
92 
93 void pic_program_begin(void);
94 uint32_t pic_program_data(uint32_t, pic_data *);
95 void pic_program_end(int);
96 
97 void pic_verify_begin(void);
98 uint32_t pic_verify_data(uint32_t, pic_data *, uint32_t *);
99 void pic_verify_end(void);
100 
101 uint8_t *pic_pe_read_file(char *, uint32_t *);
102 int pic_pe_lookup(char *, const char *);
103 
104 int pic_read_config(void);
105 
106 uint32_t pic_get_program_size(uint32_t *, uint32_t);
107 uint32_t pic_get_data_size(uint32_t *);
108 uint32_t pic_get_executive_size(uint32_t *);
109 uint32_t pic_get_boot_size(uint32_t *);
110 
111 uint32_t pic_read_program_memory_block(uint32_t *, uint32_t, uint32_t);
112 uint32_t pic_read_data_memory_block(uint16_t *, uint32_t, uint16_t);
113 
114 #define PIC_VOID (0xFFFF)
115 void pic_program(char *, int);
116 uint32_t pic_verify(char *);
117 void pic_view(char *, int);
118 
119 void pic_writebandgap(uint16_t);
120 void pic_writeosccal(uint16_t);
121 void pic_bulk_erase(void);
122 void pic_blank(int);
123 
124 #define PIC_ERASE_ID (UINT32_MAX)
125 #define PIC_ERASE_CONFIG (PIC_ERASE_ID - 1)
126 #define PIC_ERASE_EEPROM (PIC_ERASE_ID - 2)
127 void pic_erase(uint32_t, uint32_t);
128 
129 void pic_dumpdeviceid(void);
130 
131 #define PIC_BRIEF   (0)
132 #define PIC_VERBOSE (1)
133 void pic_dumpconfig(void);
134 void pic_dumposccal(void);
135 
136 void pic_dumpaddr(uint32_t, int);
137 void pic_dumpbyte(uint32_t, uint8_t);
138 void pic_dumpword16(uint32_t, uint16_t);
139 void pic_dumpword32(uint32_t, uint32_t);
140 
141 void pic_dumpdevice(void);
142 
143 void pic_dumpprogram(uint32_t, uint32_t);
144 void pic_dumpdata(void);
145 void pic_dumpexec(uint32_t, uint32_t);
146 void pic_dumpboot(uint32_t, uint32_t);
147 
148 #define PIC_HEXDEC (1)
149 #define PIC_INHX32 (2)
150 #define PIC_CHAR(X) ((((X) >= ' ' && (X) < 127) ? (X) : ('.')))
151 void pic_dump_program(uint32_t, uint32_t, int);
152 int pic_mtcode(uint32_t, uint32_t, uint32_t *);
153 void pic_dumphexcode(uint32_t, uint32_t, uint32_t *);
154 void pic_dumpinhxcode(uint32_t, uint32_t, uint32_t *);
155 void pic_dump_data(uint32_t, uint32_t, int);
156 int pic_mtdata(uint16_t, uint32_t, uint16_t *);
157 void pic_dumphexdata(uint32_t, uint32_t, uint16_t *);
158 void pic_dumpinhxdata(uint32_t, uint32_t, uint16_t *);
159 
160 void pic_debug(void);
161 
162 /* MEMORY REGIONS */
163 #define PIC_REGIONNOTSUP (0)	/* NOT BELOW                         */
164 #define PIC_REGIONCODE   (1)	/* PROGRAM CODE                      */
165 #define PIC_REGIONCONFIG (2)	/* CONFIG WORD(S)                    */
166 #define PIC_REGIONDATA   (3)	/* DATA EEPROM / FLASH               */
167 #define PIC_REGIONID     (4)	/* USER ID                           */
168 #define PIC_REGIONEXEC   (5)	/* EXECUTIVE, UNIT / FUID, UDID, OTP */
169 #define PIC_REGIONBOOT   (6)	/* PIC32 BOOT SECTOR / PIC24 FBOOT   */
170 #define PIC_REGIONPART   (7)	/* PARTITIONED PROGRAM CODE          */
171 
172 /* PANEL WRITING */
173 #define PIC_PANEL_BEGIN  (1)
174 #define PIC_PANEL_UPDATE (2)
175 #define PIC_PANEL_END    (3)
176 #define PIC_TIMEOUT      (1) /* 1 second */
177 void pic_write_panel(int, uint32_t, uint32_t);
178 
179 /* PARTITIONS */
180 #define PIC_PART1 (0)
181 #define PIC_PART2 (1)
182 
183 #endif /* !_PIC_H */
184