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;
93 // sg layer verboseness: 0 for no debug info, 10 for lots
96 unsigned char cdb_cmd_blk[CDB_SL];
98 // Data transferred from or to device
99 unsigned char q_buf[Q_BUF_LEN];
101 int q_data_dir; // Q_DATA_IN, Q_DATA_OUT
102 // the start of the query data in the device memory space
105 // Sense (error information) data
106 unsigned char sense_buf[SENSE_BUF_LEN];
118 /* medium density stm32 flash settings */
119 #define STM32_FLASH_BASE 0x08000000
120 #define STM32_FLASH_SIZE (128 * 1024)
121 #define STM32_FLASH_PGSZ 1024
122 stm32_addr_t flash_base;
126 /* in flash system memory */
127 #define STM32_SYSTEM_BASE 0x1ffff000
128 #define STM32_SYSTEM_SIZE (2 * 1024)
129 stm32_addr_t sys_base;
133 #define STM32_SRAM_BASE 0x20000000
134 #define STM32_SRAM_SIZE (8 * 1024)
135 stm32_addr_t sram_base;
139 struct stlink* stlink_quirk_open(const char *dev_name, const int verbose);
140 int stlink_current_mode(struct stlink *sl);
141 void stlink_enter_swd_mode(struct stlink *sl);
142 void stlink_enter_jtag_mode(struct stlink *sl);
143 void stlink_exit_debug_mode(struct stlink *sl);
144 void stlink_core_id(struct stlink *sl);
145 void stlink_status(struct stlink *sl);
146 void stlink_force_debug(struct stlink *sl);
147 void stlink_reset(struct stlink *sl);
148 void stlink_run(struct stlink *sl);
149 void stlink_step(struct stlink *sl);
150 void stlink_read_all_regs(struct stlink *sl);
151 void stlink_read_reg(struct stlink *sl, int r_idx);
152 void stlink_write_reg(struct stlink *sl, uint32_t reg, int idx);
153 void stlink_read_mem32(struct stlink *sl, uint32_t addr, uint16_t len);
154 void stlink_write_mem8(struct stlink *sl, uint32_t addr, uint16_t len);
155 void stlink_write_mem32(struct stlink *sl, uint32_t addr, uint16_t len);
156 void stlink_close(struct stlink *sl);
158 int stlink_erase_flash_page(struct stlink* sl, stm32_addr_t page);
159 int stlink_erase_flash_mass(struct stlink* sl);
160 int stlink_write_flash(struct stlink* sl, stm32_addr_t address, uint8_t* data, unsigned length);