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.
20 // Query data flow direction.
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
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
34 // st-link vendor cmd's
35 #define USB_ST_VID 0x0483
36 #define USB_STLINK_PID 0x3744
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
45 #define STLINK_GET_VERSION 0xf1
46 #define STLINK_GET_CURRENT_MODE 0xf5
48 #define STLINK_DEBUG_COMMAND 0xF2
49 #define STLINK_DFU_COMMAND 0xF3
50 #define STLINK_DFU_EXIT 0x07
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
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
88 typedef uint32_t stm32_addr_t;
92 TRANSPORT_TYPE_ZERO = 0,
95 #endif /* CONFIG_USE_LIBSG */
97 TRANSPORT_TYPE_LIBUSB,
98 #endif /* CONFIG_USE_LIBUSB */
99 TRANSPORT_TYPE_INVALID
104 libusb_device_handle* usb_handle;
105 struct libusb_transfer* req_trans;
106 struct libusb_transfer* rep_trans;
111 struct stlink_libsg {
114 // sg layer verboseness: 0 for no debug info, 10 for lots
117 unsigned char cdb_cmd_blk[CDB_SL];
119 // Data transferred from or to device
120 unsigned char q_buf[Q_BUF_LEN];
122 int q_data_dir; // Q_DATA_IN, Q_DATA_OUT
123 // the start of the query data in the device memory space
126 // Sense (error information) data
127 unsigned char sense_buf[SENSE_BUF_LEN];
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;
147 /* in flash system memory */
148 #define STM32_SYSTEM_BASE 0x1ffff000
149 #define STM32_SYSTEM_SIZE (2 * 1024)
150 stm32_addr_t sys_base;
154 #define STM32_SRAM_BASE 0x20000000
155 #define STM32_SRAM_SIZE (8 * 1024)
156 stm32_addr_t sram_base;
162 enum transport_type tt;
164 #if CONFIG_USE_LIBUSB
165 struct stlink_libusb *libusb;
166 #endif /* CONFIG_USE_LIBUSB */
168 struct stlink_libsg *libsg;
169 #endif /* CONFIG_USE_LIBSG */
172 unsigned char q_buf[64];
175 /* layer independant */
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);
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);