Swap build.sh for a real Makefile
[fw/stlink] / src / stlink-hw.h
1 #ifndef _STLINK_HW_H_
2 #define _STLINK_HW_H_
3
4 #include <stdint.h>
5
6 // device access
7 #define RDWR            0
8 #define RO              1
9 #define SG_TIMEOUT_SEC  1 // actually 1 is about 2 sec
10 // Each CDB can be a total of 6, 10, 12, or 16 bytes, later version
11 // of the SCSI standard also allow for variable-length CDBs (min. CDB is 6).
12 // the stlink needs max. 10 bytes.
13 #define CDB_6           6
14 #define CDB_10          10
15 #define CDB_12          12
16 #define CDB_16          16
17
18 #define CDB_SL          10
19
20 // Query data flow direction.
21 #define Q_DATA_OUT      0
22 #define Q_DATA_IN       1
23
24 // The SCSI Request Sense command is used to obtain sense data
25 // (error information) from a target device.
26 // http://en.wikipedia.org/wiki/SCSI_Request_Sense_Command
27 #define SENSE_BUF_LEN           32
28
29 // Max data transfer size.
30 // 6kB = max mem32_read block, 8kB sram
31 //#define Q_BUF_LEN     96
32 #define Q_BUF_LEN       1024 * 100
33
34 // st-link vendor cmd's
35 #define USB_ST_VID                      0x0483
36 #define USB_STLINK_PID                  0x3744
37
38 // STLINK_DEBUG_RESETSYS, etc:
39 #define STLINK_OK                       0x80
40 #define STLINK_FALSE                    0x81
41 #define STLINK_CORE_RUNNING             0x80
42 #define STLINK_CORE_HALTED              0x81
43 #define STLINK_CORE_STAT_UNKNOWN        -1
44
45 #define STLINK_GET_VERSION              0xf1
46 #define STLINK_GET_CURRENT_MODE 0xf5
47
48 #define STLINK_DEBUG_COMMAND            0xF2
49 #define STLINK_DFU_COMMAND              0xF3
50 #define STLINK_DFU_EXIT         0x07
51
52 // STLINK_GET_CURRENT_MODE
53 #define STLINK_DEV_DFU_MODE             0x00
54 #define STLINK_DEV_MASS_MODE            0x01
55 #define STLINK_DEV_DEBUG_MODE           0x02
56 #define STLINK_DEV_UNKNOWN_MODE -1
57
58 // jtag mode cmds
59 #define STLINK_DEBUG_ENTER              0x20
60 #define STLINK_DEBUG_EXIT               0x21
61 #define STLINK_DEBUG_READCOREID 0x22
62 #define STLINK_DEBUG_GETSTATUS          0x01
63 #define STLINK_DEBUG_FORCEDEBUG 0x02
64 #define STLINK_DEBUG_RESETSYS           0x03
65 #define STLINK_DEBUG_READALLREGS        0x04
66 #define STLINK_DEBUG_READREG            0x05
67 #define STLINK_DEBUG_WRITEREG           0x06
68 #define STLINK_DEBUG_READMEM_32BIT      0x07
69 #define STLINK_DEBUG_WRITEMEM_32BIT     0x08
70 #define STLINK_DEBUG_RUNCORE            0x09
71 #define STLINK_DEBUG_STEPCORE           0x0a
72 #define STLINK_DEBUG_SETFP              0x0b
73 #define STLINK_DEBUG_WRITEMEM_8BIT      0x0d
74 #define STLINK_DEBUG_CLEARFP            0x0e
75 #define STLINK_DEBUG_WRITEDEBUGREG      0x0f
76 #define STLINK_DEBUG_ENTER_SWD          0xa3
77 #define STLINK_DEBUG_ENTER_JTAG 0x00
78
79 typedef struct {
80         uint32_t r[16];
81         uint32_t xpsr;
82         uint32_t main_sp;
83         uint32_t process_sp;
84         uint32_t rw;
85         uint32_t rw2;
86 } reg;
87
88 typedef uint32_t stm32_addr_t;
89
90 enum transport_type
91 {
92   TRANSPORT_TYPE_ZERO = 0,
93 #if CONFIG_USE_LIBSG
94   TRANSPORT_TYPE_LIBSG,
95 #endif /* CONFIG_USE_LIBSG */
96 #if CONFIG_USE_LIBUSB
97   TRANSPORT_TYPE_LIBUSB,
98 #endif /* CONFIG_USE_LIBUSB */
99   TRANSPORT_TYPE_INVALID
100 };
101
102 struct stlink_libusb
103 {
104   libusb_device_handle* usb_handle;
105   struct libusb_transfer* req_trans;
106   struct libusb_transfer* rep_trans;
107   unsigned int ep_req;
108   unsigned int ep_rep;
109 };
110
111 struct stlink_libsg {
112         int sg_fd;
113         int do_scsi_pt_err;
114         // sg layer verboseness: 0 for no debug info, 10 for lots
115         int verbose;
116
117         unsigned char cdb_cmd_blk[CDB_SL];
118
119         // Data transferred from or to device
120         unsigned char q_buf[Q_BUF_LEN];
121         int q_len;
122         int q_data_dir; // Q_DATA_IN, Q_DATA_OUT
123         // the start of the query data in the device memory space
124         uint32_t q_addr;
125
126         // Sense (error information) data
127         unsigned char sense_buf[SENSE_BUF_LEN];
128
129         uint32_t st_vid;
130         uint32_t stlink_pid;
131         uint32_t stlink_v;
132         uint32_t jtag_v;
133         uint32_t swim_v;
134         uint32_t core_id;
135
136         reg reg;
137         int core_stat;
138
139         /* medium density stm32 flash settings */
140 #define STM32_FLASH_BASE 0x08000000
141 #define STM32_FLASH_SIZE (128 * 1024)
142 #define STM32_FLASH_PGSZ 1024
143         stm32_addr_t flash_base;
144         size_t flash_size;
145         size_t flash_pgsz;
146
147         /* in flash system memory */
148 #define STM32_SYSTEM_BASE 0x1ffff000
149 #define STM32_SYSTEM_SIZE (2 * 1024)
150         stm32_addr_t sys_base;
151         size_t sys_size;
152
153         /* sram settings */
154 #define STM32_SRAM_BASE 0x20000000
155 #define STM32_SRAM_SIZE (8 * 1024)
156         stm32_addr_t sram_base;
157         size_t sram_size;
158 };
159
160 struct stlink
161 {
162         enum transport_type tt;
163         union {
164 #if CONFIG_USE_LIBUSB
165             struct stlink_libusb *libusb;
166 #endif /* CONFIG_USE_LIBUSB */
167 #if CONFIG_USE_LIBSG
168             struct stlink_libsg *libsg;
169 #endif /* CONFIG_USE_LIBSG */
170         } transport;
171
172         unsigned char q_buf[64];
173         size_t q_len;
174
175         /* layer independant */
176         uint32_t core_id;
177 };
178
179
180 struct stlink* stlink_quirk_open(enum transport_type tt, const char *dev_name, const int verbose);
181 int stlink_current_mode(struct stlink *sl);
182 void stlink_enter_swd_mode(struct stlink *sl);
183 void stlink_enter_jtag_mode(struct stlink *sl);
184 void stlink_exit_debug_mode(struct stlink *sl);
185 void stlink_core_id(struct stlink *sl);
186 void stlink_status(struct stlink *sl);
187 void stlink_force_debug(struct stlink *sl);
188 void stlink_reset(struct stlink *sl);
189 void stlink_run(struct stlink *sl);
190 void stlink_step(struct stlink *sl);
191 void stlink_read_all_regs(struct stlink *sl);
192 void stlink_read_reg(struct stlink *sl, int r_idx);
193 void stlink_write_reg(struct stlink *sl, uint32_t reg, int idx);
194 void stlink_read_mem32(struct stlink *sl, uint32_t addr, uint16_t len);
195 void stlink_write_mem8(struct stlink *sl, uint32_t addr, uint16_t len);
196 void stlink_write_mem32(struct stlink *sl, uint32_t addr, uint16_t len);
197 void stlink_close(struct stlink *sl);
198
199 int stlink_erase_flash_page(struct stlink* sl, stm32_addr_t page);
200 int stlink_erase_flash_mass(struct stlink* sl);
201 int stlink_write_flash(struct stlink* sl, stm32_addr_t address, uint8_t* data, unsigned length);
202
203 #endif