b902c1762073810c69d6bc634555ff281d07d9d9
[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 struct stlink {
91         int sg_fd;
92         int do_scsi_pt_err;
93         // sg layer verboseness: 0 for no debug info, 10 for lots
94         int verbose;
95
96         unsigned char cdb_cmd_blk[CDB_SL];
97
98         // Data transferred from or to device
99         unsigned char q_buf[Q_BUF_LEN];
100         int q_len;
101         int q_data_dir; // Q_DATA_IN, Q_DATA_OUT
102         // the start of the query data in the device memory space
103         uint32_t q_addr;
104
105         // Sense (error information) data
106         unsigned char sense_buf[SENSE_BUF_LEN];
107
108         uint32_t st_vid;
109         uint32_t stlink_pid;
110         uint32_t stlink_v;
111         uint32_t jtag_v;
112         uint32_t swim_v;
113         uint32_t core_id;
114
115         reg reg;
116         int core_stat;
117
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;
123         size_t flash_size;
124         size_t flash_pgsz;
125
126         /* in flash system memory */
127 #define STM32_SYSTEM_BASE 0x1ffff000
128 #define STM32_SYSTEM_SIZE (2 * 1024)
129         stm32_addr_t sys_base;
130         size_t sys_size;
131
132         /* sram settings */
133 #define STM32_SRAM_BASE 0x20000000
134 #define STM32_SRAM_SIZE (8 * 1024)
135         stm32_addr_t sram_base;
136         size_t sram_size;
137 };
138
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);
157
158 int stlink_erase_flash_page(struct stlink* sl, stm32_addr_t page);
159 int stlink_erase_flash_mass(struct stlink* sl);
160
161 #endif