1 /* MSPDebug - debugging tool for MSP430 MCUs
2  * Copyright (C) 2009, 2010 Daniel Beer
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 #ifndef DEVICE_H_
20 #define DEVICE_H_
21 
22 #include <stdint.h>
23 #include "util.h"
24 #include "powerbuf.h"
25 #include "chipinfo.h"
26 #include "bytes.h"
27 
28 struct device;
29 typedef struct device *device_t;
30 
31 typedef enum {
32 	DEVICE_CTL_RESET,
33 	DEVICE_CTL_RUN,
34 	DEVICE_CTL_HALT,
35 	DEVICE_CTL_STEP,
36 	DEVICE_CTL_SECURE
37 } device_ctl_t;
38 
39 typedef enum {
40 	DEVICE_STATUS_HALTED,
41 	DEVICE_STATUS_RUNNING,
42 	DEVICE_STATUS_INTR,
43 	DEVICE_STATUS_ERROR
44 } device_status_t;
45 
46 typedef enum {
47 	DEVICE_ERASE_ALL,
48 	DEVICE_ERASE_MAIN,
49 	DEVICE_ERASE_SEGMENT
50 } device_erase_type_t;
51 
52 #define DEVICE_NUM_REGS		16
53 #define DEVICE_MAX_BREAKPOINTS  32
54 
55 #define DEVICE_BP_ENABLED       0x01
56 #define DEVICE_BP_DIRTY         0x02
57 
58 typedef enum {
59 	DEVICE_BPTYPE_BREAK,
60 	DEVICE_BPTYPE_WATCH,
61 	DEVICE_BPTYPE_READ,
62 	DEVICE_BPTYPE_WRITE
63 } device_bptype_t;
64 
65 struct device_breakpoint {
66 	device_bptype_t		type;
67 	address_t		addr;
68 	int			flags;
69 };
70 
71 #define DEVICE_FLAG_JTAG	0x01 /* default is SBW */
72 #define DEVICE_FLAG_LONG_PW	0x02
73 #define DEVICE_FLAG_TTY		0x04 /* default is USB */
74 #define DEVICE_FLAG_FORCE_RESET	0x08
75 #define DEVICE_FLAG_DO_FWUPDATE 0x10
76 #define DEVICE_FLAG_SKIP_CLOSE	0x20
77 #define DEVICE_FLAG_BSL_NME     0x40 /* BSL no-mass-erase */
78 
79 struct device_args {
80 	int			flags;
81 	int			vcc_mv;
82 	const char		*path;
83 	const char		*forced_chip_id;
84 	const char		*requested_serial;
85 	const char		*require_fwupdate;
86 	const char		*bsl_entry_seq;
87 	int			bsl_gpio_used;
88 	int			bsl_gpio_rts;
89 	int			bsl_gpio_dtr;
90 	uint8_t                 bsl_entry_password[32];
91 };
92 
93 struct device_class {
94 	const char		*name;
95 	const char		*help;
96 
97 	/* Create a new device */
98 	device_t (*open)(const struct device_args *args);
99 
100 	/* Close the connection to the device and destroy the driver object */
101 	void (*destroy)(device_t dev);
102 
103 	/* Read/write memory */
104 	int (*readmem)(device_t dev, address_t addr,
105 		       uint8_t *mem, address_t len);
106 	int (*writemem)(device_t dev, address_t addr,
107 			const uint8_t *mem, address_t len);
108 
109 	/* Erase memory */
110 	int (*erase)(device_t dev, device_erase_type_t type,
111 		     address_t address);
112 
113 	/* Read/write registers */
114 	int (*getregs)(device_t dev, address_t *regs);
115 	int (*setregs)(device_t dev, const address_t *regs);
116 
117 	/* CPU control */
118 	int (*ctl)(device_t dev, device_ctl_t op);
119 
120 	/* Wait a little while for the CPU to change state */
121 	device_status_t (*poll)(device_t dev);
122 
123 	/* Get the configuration fuse values */
124 	int (*getconfigfuses)(device_t dev);
125 };
126 
127 struct device {
128 	const struct device_class	*type;
129 
130 	uint8_t				dev_id[3];
131 
132 	/* Breakpoint table. This should not be modified directly.
133 	 * Instead, you should use the device_setbrk() helper function. This
134 	 * will set the appropriate flags and ensure that the breakpoint is
135 	 * reloaded before the next run.
136 	 */
137 	int max_breakpoints;
138 	struct device_breakpoint breakpoints[DEVICE_MAX_BREAKPOINTS];
139 
140 	/* Power sample buffer, if power profiling is supported by this
141 	 * device.
142 	 */
143 	powerbuf_t power_buf;
144 
145 	/* Chip information data.
146 	 */
147 	const struct chipinfo *chip;
148 	int need_probe;
149 };
150 
151 /* Probe the device memory and extract ID bytes. This should be called
152  * after the device structure is ready.
153  */
154 int device_probe_id(device_t dev, const char *force_id);
155 
156 /* Determine, from the device ID bytes, whether this chip is an FRAM or
157  * flash-based device.
158  */
159 int device_is_fram(device_t dev);
160 
161 /* Set or clear a breakpoint. The index of the modified entry is
162  * returned, or -1 if no free entries were available. The modified
163  * entry is flagged so that it will be reloaded on the next run.
164  *
165  * If which is specified, a particular breakpoint slot is
166  * modified. Otherwise, if which < 0, breakpoint slots are selected
167  * automatically.
168  */
169 int device_setbrk(device_t dev, int which, int enabled, address_t address,
170 		  device_bptype_t type);
171 
172 extern device_t device_default;
173 
174 /* Helper macros for operating on the default device */
175 #define device_destroy() device_default->type->destroy(device_default)
176 #define device_readmem(addr, mem, len) \
177 	device_default->type->readmem(device_default, addr, mem, len)
178 #define device_writemem(addr, mem, len) \
179 	device_default->type->writemem(device_default, addr, mem, len)
180 #define device_getregs(regs) \
181 	device_default->type->getregs(device_default, regs)
182 #define device_setregs(regs) \
183 	device_default->type->setregs(device_default, regs)
184 #define device_ctl(op) \
185 	device_default->type->ctl(device_default, op)
186 #define device_poll() \
187 	device_default->type->poll(device_default)
188 
189 int device_erase(device_erase_type_t et, address_t addr);
190 
191 address_t check_range(const struct chipinfo *chip,
192 			     address_t addr, address_t size,
193 			     const struct chipinfo_memory **m_ret);
194 
195 int readmem(device_t dev, address_t addr,
196 		uint8_t *mem, address_t len,
197 		int (*read_words)(device_t dev,
198 			const struct chipinfo_memory *m,
199 			address_t addr, address_t len,
200 			uint8_t *data)
201 		);
202 
203 int writemem(device_t dev, address_t addr,
204 		const uint8_t *mem, address_t len,
205 		int (*write_words)(device_t dev,
206 			const struct chipinfo_memory *m,
207 			address_t addr, address_t len,
208 			const uint8_t *data),
209 		int (*read_words)(device_t dev,
210 			const struct chipinfo_memory *m,
211 			address_t addr, address_t len,
212 			uint8_t *data)
213 		);
214 
215 #endif
216