Make _stlink_usb_reset use hardreset
[fw/stlink] / src / stlink-common.h
1 /*
2  * File:   stlink-common.h
3  * Bulk import from stlink-hw.h
4  *
5  * This should contain all the common top level stlink interfaces, regardless
6  * of how the backend does the work....
7  */
8
9 #ifndef STLINK_COMMON_H
10 #define STLINK_COMMON_H
11
12 #ifdef  __cplusplus
13 extern "C" {
14 #endif
15
16 #include <stdint.h>
17
18     // Max data transfer size.
19     // 6kB = max mem32_read block, 8kB sram
20     //#define Q_BUF_LEN 96
21 #define Q_BUF_LEN                       (1024 * 100)
22
23     // st-link vendor cmd's
24 #define USB_ST_VID                      0x0483
25 #define USB_STLINK_PID                  0x3744
26 #define USB_STLINK_32L_PID              0x3748
27 #define USB_STLINK_NUCLEO_PID   0x374b
28
29     // STLINK_DEBUG_RESETSYS, etc:
30 #define STLINK_OK                       0x80
31 #define STLINK_FALSE                    0x81
32 #define STLINK_CORE_RUNNING             0x80
33 #define STLINK_CORE_HALTED              0x81
34 #define STLINK_CORE_STAT_UNKNOWN        -1
35
36 #define STLINK_GET_VERSION              0xf1
37 #define STLINK_GET_CURRENT_MODE 0xf5
38 #define STLINK_GET_TARGET_VOLTAGE       0xF7
39
40 #define STLINK_DEBUG_COMMAND            0xF2
41 #define STLINK_DFU_COMMAND              0xF3
42 #define STLINK_DFU_EXIT         0x07
43     // enter dfu could be 0x08?
44
45     // STLINK_GET_CURRENT_MODE
46 #define STLINK_DEV_DFU_MODE             0x00
47 #define STLINK_DEV_MASS_MODE            0x01
48 #define STLINK_DEV_DEBUG_MODE           0x02
49 #define STLINK_DEV_UNKNOWN_MODE -1
50
51     // jtag mode cmds
52 #define STLINK_DEBUG_ENTER              0x20
53 #define STLINK_DEBUG_EXIT               0x21
54 #define STLINK_DEBUG_READCOREID 0x22
55 #define STLINK_DEBUG_GETSTATUS          0x01
56 #define STLINK_DEBUG_FORCEDEBUG 0x02
57 #define STLINK_DEBUG_RESETSYS           0x03
58 #define STLINK_DEBUG_READALLREGS        0x04
59 #define STLINK_DEBUG_READREG            0x05
60 #define STLINK_DEBUG_WRITEREG           0x06
61 #define STLINK_DEBUG_READMEM_32BIT      0x07
62 #define STLINK_DEBUG_WRITEMEM_32BIT     0x08
63 #define STLINK_DEBUG_RUNCORE            0x09
64 #define STLINK_DEBUG_STEPCORE           0x0a
65 #define STLINK_DEBUG_SETFP              0x0b
66 #define STLINK_DEBUG_WRITEMEM_8BIT      0x0d
67 #define STLINK_DEBUG_CLEARFP            0x0e
68 #define STLINK_DEBUG_WRITEDEBUGREG      0x0f
69 #define STLINK_DEBUG_ENTER_SWD          0xa3
70 #define STLINK_DEBUG_ENTER_JTAG 0x00
71 #define STLINK_DEBUG_HARDRESET          0x3c
72
73     // TODO - possible poor names...
74 #define STLINK_SWD_ENTER 0x30
75 #define STLINK_SWD_READCOREID 0x32  // TBD
76 #define STLINK_JTAG_WRITEDEBUG_32BIT 0x35
77 #define STLINK_JTAG_READDEBUG_32BIT 0x36
78 #define STLINK_JTAG_DRIVE_NRST 0x3c
79 #define STLINK_JTAG_DRIVE_NRST 0x3c
80
81     // cortex m3 technical reference manual
82 #define CM3_REG_CPUID 0xE000ED00
83 #define CM3_REG_FP_CTRL 0xE0002000
84 #define CM3_REG_FP_COMP0 0xE0002008
85
86     /* cortex core ids */
87     // TODO clean this up...
88 #define STM32VL_CORE_ID 0x1ba01477
89 #define STM32L_CORE_ID 0x2ba01477
90 #define STM32F3_CORE_ID 0x2ba01477
91 #define STM32F4_CORE_ID 0x2ba01477
92 #define STM32F0_CORE_ID 0xbb11477
93 #define CORE_M3_R1 0x1BA00477
94 #define CORE_M3_R2 0x4BA00477
95 #define CORE_M4_R0 0x2BA01477
96
97     /*
98      * Chip IDs are explained in the appropriate programming manual for the
99      * DBGMCU_IDCODE register (0xE0042000)
100      */
101     // stm32 chipids, only lower 12 bits..
102 #define STM32_CHIPID_F1_MEDIUM      0x410
103 #define STM32_CHIPID_F2             0x411
104 #define STM32_CHIPID_F1_LOW         0x412
105 #define STM32_CHIPID_F4             0x413
106 #define STM32_CHIPID_F1_HIGH        0x414
107 #define STM32_CHIPID_L4             0x415       /* Seen on L4x6 (RM0351) */
108 #define STM32_CHIPID_L1_MEDIUM      0x416
109 #define STM32_CHIPID_L0             0x417
110 #define STM32_CHIPID_F1_CONN        0x418
111 #define STM32_CHIPID_F4_HD          0x419
112 #define STM32_CHIPID_F1_VL_MEDIUM_LOW 0x420
113
114 #define STM32_CHIPID_F446           0x421
115 #define STM32_CHIPID_F3             0x422
116 #define STM32_CHIPID_F4_LP          0x423
117
118 #define STM32_CHIPID_F411RE         0x431
119
120 #define STM32_CHIPID_L1_MEDIUM_PLUS 0x427
121 #define STM32_CHIPID_F1_VL_HIGH     0x428
122 #define STM32_CHIPID_L1_CAT2        0x429
123
124 #define STM32_CHIPID_F1_XL          0x430
125
126 #define STM32_CHIPID_F37x           0x432
127 #define STM32_CHIPID_F4_DE          0x433
128
129 #define STM32_CHIPID_L1_HIGH        0x436
130 #define STM32_CHIPID_L152_RE        0x437
131 #define STM32_CHIPID_F334           0x438
132
133 #define STM32_CHIPID_F3_SMALL       0x439
134 #define STM32_CHIPID_F0             0x440
135 #define STM32_CHIPID_F09X           0x442
136 #define STM32_CHIPID_F0_SMALL       0x444
137
138 #define STM32_CHIPID_F04            0x445
139
140 #define STM32_CHIPID_F303_HIGH      0x446
141
142 #define STM32_CHIPID_F0_CAN         0x448
143
144 #define STM32_CHIPID_F7             0x449
145
146     /*
147      * 0x436 is actually assigned to some L1 chips that are called "Medium-Plus"
148      * and some that are called "High".  0x427 is assigned to the other "Medium-
149      * plus" chips.  To make it a bit simpler we just call 427 MEDIUM_PLUS and
150      * 0x436 HIGH.
151      */
152
153     // Constant STM32 memory map figures
154 #define STM32_FLASH_BASE 0x08000000
155 #define STM32_SRAM_BASE 0x20000000
156
157     /* Cortex™-M3 Technical Reference Manual */
158     /* Debug Halting Control and Status Register */
159 #define DHCSR 0xe000edf0
160 #define DCRSR 0xe000edf4
161 #define DCRDR 0xe000edf8
162 #define DBGKEY 0xa05f0000
163
164     /* Enough space to hold both a V2 command or a V1 command packaged as generic scsi*/
165 #define C_BUF_LEN 32
166
167     typedef struct chip_params_ {
168         uint32_t chip_id;
169         char* description;
170         uint32_t flash_size_reg;
171         uint32_t flash_pagesize;
172         uint32_t sram_size;
173         uint32_t bootrom_base, bootrom_size;
174     } chip_params_t;
175
176
177     // These maps are from a combination of the Programming Manuals, and
178     // also the Reference manuals.  (flash size reg is normally in ref man)
179     static const chip_params_t devices[] = {
180         {
181             //RM0385 and DS10916 document was used to find these paramaters
182             .chip_id = STM32_CHIPID_F7,
183             .description = "F7 device",
184             .flash_size_reg = 0x1ff0f442,      // section 41.2
185             .flash_pagesize = 0x800,           // No flash pages
186             .sram_size = 0x50000,              // "SRAM" byte size in hex from DS Fig 18
187             .bootrom_base = 0x00100000,        // "System memory" starting address from DS Fig 18
188             .bootrom_size = 0xEDC0             // "System memory" byte size in hex from DS Fig 18
189         },
190         { // table 2, PM0063
191             .chip_id = STM32_CHIPID_F1_MEDIUM,
192             .description = "F1 Medium-density device",
193             .flash_size_reg = 0x1ffff7e0,
194             .flash_pagesize = 0x400,
195             .sram_size = 0x5000,
196             .bootrom_base = 0x1ffff000,
197             .bootrom_size = 0x800
198         },
199         {  // table 1, PM0059
200             .chip_id = STM32_CHIPID_F2,
201             .description = "F2 device",
202             .flash_size_reg = 0x1fff7a22, /* As in RM0033 Rev 5*/
203             .flash_pagesize = 0x20000,
204             .sram_size = 0x20000,
205             .bootrom_base = 0x1fff0000,
206             .bootrom_size = 0x7800
207         },
208         { // PM0063
209             .chip_id = STM32_CHIPID_F1_LOW,
210             .description = "F1 Low-density device",
211             .flash_size_reg = 0x1ffff7e0,
212             .flash_pagesize = 0x400,
213             .sram_size = 0x2800,
214             .bootrom_base = 0x1ffff000,
215             .bootrom_size = 0x800
216         },
217         {
218             .chip_id = STM32_CHIPID_F4,
219             .description = "F4 device",
220             .flash_size_reg = 0x1FFF7A22,  /* As in rm0090 since Rev 2*/
221             .flash_pagesize = 0x4000,
222             .sram_size = 0x30000,
223             .bootrom_base = 0x1fff0000,
224             .bootrom_size = 0x7800
225         },
226         {
227             .chip_id = STM32_CHIPID_F4_HD,
228             .description = "F42x and F43x device",
229             .flash_size_reg = 0x1FFF7A22,  /* As in rm0090 since Rev 2*/
230             .flash_pagesize = 0x4000,
231             .sram_size = 0x40000,
232             .bootrom_base = 0x1fff0000,
233             .bootrom_size = 0x7800
234         },
235         {
236             .chip_id = STM32_CHIPID_F4_LP,
237             .description = "F4 device (low power)",
238             .flash_size_reg = 0x1FFF7A22,
239             .flash_pagesize = 0x4000,
240             .sram_size = 0x10000,
241             .bootrom_base = 0x1fff0000,
242             .bootrom_size = 0x7800
243         },
244         {
245             .chip_id = STM32_CHIPID_F411RE,
246             .description = "F4 device (low power) - stm32f411re",
247             .flash_size_reg = 0x1FFF7A22,
248             .flash_pagesize = 0x4000,
249             .sram_size = 0x20000,
250             .bootrom_base = 0x1fff0000,
251             .bootrom_size = 0x7800
252         },
253         {
254             .chip_id = STM32_CHIPID_F4_DE,
255             .description = "F4 device (Dynamic Efficency)",
256             .flash_size_reg = 0x1FFF7A22,
257             .flash_pagesize = 0x4000,
258             .sram_size = 0x18000,
259             .bootrom_base = 0x1fff0000,
260             .bootrom_size = 0x7800
261         },
262         {
263             .chip_id = STM32_CHIPID_F1_HIGH,
264             .description = "F1 High-density device",
265             .flash_size_reg = 0x1ffff7e0,
266             .flash_pagesize = 0x800,
267             .sram_size = 0x10000,
268             .bootrom_base = 0x1ffff000,
269             .bootrom_size = 0x800
270         },
271         {
272             // This ignores the EEPROM! (and uses the page erase size,
273             // not the sector write protection...)
274             .chip_id = STM32_CHIPID_L1_MEDIUM,
275             .description = "L1 Med-density device",
276             .flash_size_reg = 0x1ff8004c,
277             .flash_pagesize = 0x100,
278             .sram_size = 0x4000,
279             .bootrom_base = 0x1ff00000,
280             .bootrom_size = 0x1000
281         },
282         {
283             .chip_id = STM32_CHIPID_L1_CAT2,
284             .description = "L1 Cat.2 device",
285             .flash_size_reg = 0x1ff8004c,
286             .flash_pagesize = 0x100,
287             .sram_size = 0x8000,
288             .bootrom_base = 0x1ff00000,
289             .bootrom_size = 0x1000
290         },
291         {
292             .chip_id = STM32_CHIPID_L1_MEDIUM_PLUS,
293             .description = "L1 Medium-Plus-density device",
294             .flash_size_reg = 0x1ff800cc,
295             .flash_pagesize = 0x100,
296             .sram_size = 0x8000,/*Not completely clear if there are some with 48K*/
297             .bootrom_base = 0x1ff00000,
298             .bootrom_size = 0x1000
299         },
300         {
301             .chip_id = STM32_CHIPID_L1_HIGH,
302             .description = "L1 High-density device",
303             .flash_size_reg = 0x1ff800cc,
304             .flash_pagesize = 0x100,
305             .sram_size = 0xC000, /*Not completely clear if there are some with 32K*/
306             .bootrom_base = 0x1ff00000,
307             .bootrom_size = 0x1000
308         },
309         {
310             .chip_id = STM32_CHIPID_L152_RE,
311             .description = "L152RE",
312             .flash_size_reg = 0x1ff800cc,
313             .flash_pagesize = 0x100,
314             .sram_size = 0x14000, /*Not completely clear if there are some with 32K*/
315             .bootrom_base = 0x1ff00000,
316             .bootrom_size = 0x1000
317         },
318         {
319             .chip_id = STM32_CHIPID_F1_CONN,
320             .description = "F1 Connectivity line device",
321             .flash_size_reg = 0x1ffff7e0,
322             .flash_pagesize = 0x800,
323             .sram_size = 0x10000,
324             .bootrom_base = 0x1fffb000,
325             .bootrom_size = 0x4800
326         },
327         {//Low and Medium density VL have same chipid. RM0041 25.6.1
328             .chip_id = STM32_CHIPID_F1_VL_MEDIUM_LOW,
329             .description = "F1 Medium/Low-density Value Line device",
330             .flash_size_reg = 0x1ffff7e0,
331             .flash_pagesize = 0x400,
332             .sram_size = 0x2000,//0x1000 for low density devices
333             .bootrom_base = 0x1ffff000,
334             .bootrom_size = 0x800
335         },
336         {
337             // STM32F446x family. Support based on DM00135183.pdf (RM0390) document.
338             .chip_id = STM32_CHIPID_F446,
339             .description = "F446 device",
340             .flash_size_reg = 0x1fff7a22,
341             .flash_pagesize = 0x20000,
342             .sram_size = 0x20000,
343             .bootrom_base = 0x1fff0000,
344             .bootrom_size = 0x7800
345         },
346         {
347             // This is STK32F303VCT6 device from STM32 F3 Discovery board.
348             // Support based on DM00043574.pdf (RM0316) document.
349             .chip_id = STM32_CHIPID_F3,
350             .description = "F3 device",
351             .flash_size_reg = 0x1ffff7cc,
352             .flash_pagesize = 0x800,
353             .sram_size = 0xa000,
354             .bootrom_base = 0x1ffff000,
355             .bootrom_size = 0x800
356         },
357         {
358             // This is STK32F373VCT6 device from STM32 F373 eval board
359             // Support based on 303 above (37x and 30x have same memory map)
360             .chip_id = STM32_CHIPID_F37x,
361             .description = "F3 device",
362             .flash_size_reg = 0x1ffff7cc,
363             .flash_pagesize = 0x800,
364             .sram_size = 0xa000,
365             .bootrom_base = 0x1ffff000,
366             .bootrom_size = 0x800
367         },
368         {
369             .chip_id = STM32_CHIPID_F1_VL_HIGH,
370             .description = "F1 High-density value line device",
371             .flash_size_reg = 0x1ffff7e0,
372             .flash_pagesize = 0x800,
373             .sram_size = 0x8000,
374             .bootrom_base = 0x1ffff000,
375             .bootrom_size = 0x800
376         },
377         {
378             .chip_id = STM32_CHIPID_F1_XL,
379             .description = "F1 XL-density device",
380             .flash_size_reg = 0x1ffff7e0,
381             .flash_pagesize = 0x800,
382             .sram_size = 0x18000,
383             .bootrom_base = 0x1fffe000,
384             .bootrom_size = 0x1800
385         },
386         {
387             //Use this as an example for mapping future chips:
388             //RM0091 document was used to find these paramaters
389             .chip_id = STM32_CHIPID_F0_CAN,
390             .description = "F07x device",
391             .flash_size_reg = 0x1ffff7cc,      // "Flash size data register" (pg735)
392             .flash_pagesize = 0x800,           // Page sizes listed in Table 4
393             .sram_size = 0x4000,               // "SRAM" byte size in hex from Table 2
394             .bootrom_base = 0x1fffC800,                // "System memory" starting address from Table 2
395             .bootrom_size = 0x3000             // "System memory" byte size in hex from Table 2
396         },
397         {
398             //Use this as an example for mapping future chips:
399             //RM0091 document was used to find these paramaters
400             .chip_id = STM32_CHIPID_F0,
401             .description = "F0 device",
402             .flash_size_reg = 0x1ffff7cc,       // "Flash size data register" (pg735)
403             .flash_pagesize = 0x400,            // Page sizes listed in Table 4
404             .sram_size = 0x2000,                // "SRAM" byte size in hex from Table 2
405             .bootrom_base = 0x1fffec00,         // "System memory" starting address from Table 2
406             .bootrom_size = 0xC00               // "System memory" byte size in hex from Table 2
407         },
408         {
409             .chip_id = STM32_CHIPID_F09X,
410             .description = "F09X device",
411             .flash_size_reg = 0x1ffff7cc,       // "Flash size data register" (pg735)
412             .flash_pagesize = 0x800,            // Page sizes listed in Table 4 (pg 56)
413             .sram_size = 0x8000,                // "SRAM" byte size in hex from Table 2 (pg 50)
414             .bootrom_base = 0x1fffd800,         // "System memory" starting address from Table 2
415             .bootrom_size = 0x2000              // "System memory" byte size in hex from Table 2
416         },
417         {
418             //Use this as an example for mapping future chips:
419             //RM0091 document was used to find these paramaters
420             .chip_id = STM32_CHIPID_F04,
421             .description = "F04x device",
422             .flash_size_reg = 0x1ffff7cc,       // "Flash size data register" (pg735)
423             .flash_pagesize = 0x400,            // Page sizes listed in Table 4
424             .sram_size = 0x1800,                // "SRAM" byte size in hex from Table 2
425             .bootrom_base = 0x1fffec00,         // "System memory" starting address from Table 2
426             .bootrom_size = 0xC00               // "System memory" byte size in hex from Table 2
427         },
428         {
429             //Use this as an example for mapping future chips:
430             //RM0091 document was used to find these paramaters
431             .chip_id = STM32_CHIPID_F0_SMALL,
432             .description = "F0 small device",
433             .flash_size_reg = 0x1ffff7cc,       // "Flash size data register" (pg735)
434             .flash_pagesize = 0x400,            // Page sizes listed in Table 4
435             .sram_size = 0x1000,                // "SRAM" byte size in hex from Table 2
436             .bootrom_base = 0x1fffec00,         // "System memory" starting address from Table 2
437             .bootrom_size = 0xC00               // "System memory" byte size in hex from Table 2
438         },
439         {
440             // STM32F30x
441             .chip_id = STM32_CHIPID_F3_SMALL,
442             .description = "F3 small device",
443             .flash_size_reg = 0x1ffff7cc,
444             .flash_pagesize = 0x800,
445             .sram_size = 0xa000,
446             .bootrom_base = 0x1fffd800,
447             .bootrom_size = 0x2000
448         },
449         {
450             // STM32L0x
451             // RM0367,RM0377 documents was used to find these parameters
452             .chip_id = STM32_CHIPID_L0,
453             .description = "L0x3 device",
454             .flash_size_reg = 0x1ff8007c,
455             .flash_pagesize = 0x80,
456             .sram_size = 0x2000,
457             .bootrom_base = 0x1ff0000,
458             .bootrom_size = 0x1000
459         },
460         {
461             // STM32F334
462             // RM0364 document was used to find these parameters
463             .chip_id = STM32_CHIPID_F334,
464             .description = "F334 device",
465             .flash_size_reg = 0x1ffff7cc,
466             .flash_pagesize = 0x800,
467             .sram_size = 0x3000,
468             .bootrom_base = 0x1fffd800,
469             .bootrom_size = 0x2000
470         },
471         {
472             // This is STK32F303RET6 device from STM32 F3 Nucelo board.
473             // Support based on DM00043574.pdf (RM0316) document rev 5.
474             .chip_id = STM32_CHIPID_F303_HIGH,
475             .description = "F303 high density device",
476             .flash_size_reg = 0x1ffff7cc,    // 34.2.1 Flash size data register
477             .flash_pagesize = 0x800,         // 4.2.1 Flash memory organization
478             .sram_size = 0x10000,            // 3.3 Embedded SRAM
479             .bootrom_base = 0x1fffd800,      // 3.3.2 / Table 4 System Memory
480             .bootrom_size = 0x2000
481         },
482         {
483             // STM32L4x6
484             // From RM0351.
485             .chip_id = STM32_CHIPID_L4,
486             .description = "L4 device",
487             .flash_size_reg = 0x1fff75e0,    // "Flash size data register" (sec 45.2, page 1671)
488             .flash_pagesize = 0x800,         // 2K (sec 3.2, page 78; also appears in sec 3.3.1 and tables 4-6 on pages 79-81)
489             // SRAM1 is "up to" 96k in the standard Cortex-M memory map;
490             // SRAM2 is 32k mapped at at 0x10000000 (sec 2.3, page 73 for
491             // sizes; table 2, page 74 for SRAM2 location)
492             .sram_size = 0x18000,
493             .bootrom_base = 0x1fff0000,      // Tables 4-6, pages 80-81 (Bank 1 system memory)
494             .bootrom_size = 0x7000           // 28k (per bank), same source as base
495         },
496
497  };
498
499
500     typedef struct {
501         uint32_t r[16];
502         uint32_t s[32];
503         uint32_t xpsr;
504         uint32_t main_sp;
505         uint32_t process_sp;
506         uint32_t rw;
507         uint32_t rw2;
508         uint8_t control;
509         uint8_t faultmask;
510         uint8_t basepri;
511         uint8_t primask;
512         uint32_t fpscr;
513     } reg;
514
515     typedef uint32_t stm32_addr_t;
516
517     typedef struct _cortex_m3_cpuid_ {
518         uint16_t implementer_id;
519         uint16_t variant;
520         uint16_t part;
521         uint8_t revision;
522     } cortex_m3_cpuid_t;
523
524     typedef struct stlink_version_ {
525         uint32_t stlink_v;
526         uint32_t jtag_v;
527         uint32_t swim_v;
528         uint32_t st_vid;
529         uint32_t stlink_pid;
530     } stlink_version_t;
531
532     typedef struct flash_loader {
533         stm32_addr_t loader_addr; /* loader sram adddr */
534         stm32_addr_t buf_addr; /* buffer sram address */
535     } flash_loader_t;
536
537     enum transport_type {
538         TRANSPORT_TYPE_ZERO = 0,
539         TRANSPORT_TYPE_LIBSG,
540         TRANSPORT_TYPE_LIBUSB,
541         TRANSPORT_TYPE_INVALID
542     };
543
544     typedef struct _stlink stlink_t;
545
546     typedef struct _stlink_backend {
547         void (*close) (stlink_t * sl);
548         void (*exit_debug_mode) (stlink_t * sl);
549         void (*enter_swd_mode) (stlink_t * sl);
550         void (*enter_jtag_mode) (stlink_t * stl);
551         void (*exit_dfu_mode) (stlink_t * stl);
552         void (*core_id) (stlink_t * stl);
553         void (*reset) (stlink_t * stl);
554         void (*jtag_reset) (stlink_t * stl, int value);
555         void (*run) (stlink_t * stl);
556         void (*status) (stlink_t * stl);
557         void (*version) (stlink_t *sl);
558         uint32_t (*read_debug32) (stlink_t *sl, uint32_t addr);
559         void (*read_mem32) (stlink_t *sl, uint32_t addr, uint16_t len);
560         void (*write_debug32) (stlink_t *sl, uint32_t addr, uint32_t data);
561         void (*write_mem32) (stlink_t *sl, uint32_t addr, uint16_t len);
562         void (*write_mem8) (stlink_t *sl, uint32_t addr, uint16_t len);
563         void (*read_all_regs) (stlink_t *sl, reg * regp);
564         void (*read_reg) (stlink_t *sl, int r_idx, reg * regp);
565         void (*read_all_unsupported_regs) (stlink_t *sl, reg *regp);
566         void (*read_unsupported_reg) (stlink_t *sl, int r_idx, reg *regp);
567         void (*write_unsupported_reg) (stlink_t *sl, uint32_t value, int idx, reg *regp);
568         void (*write_reg) (stlink_t *sl, uint32_t reg, int idx);
569         void (*step) (stlink_t * stl);
570         int (*current_mode) (stlink_t * stl);
571         void (*force_debug) (stlink_t *sl);
572         int32_t (*target_voltage) (stlink_t *sl);
573     } stlink_backend_t;
574
575     struct _stlink {
576         struct _stlink_backend *backend;
577         void *backend_data;
578
579         // Room for the command header
580         unsigned char c_buf[C_BUF_LEN];
581         // Data transferred from or to device
582         unsigned char q_buf[Q_BUF_LEN];
583         int q_len;
584
585         // transport layer verboseness: 0 for no debug info, 10 for lots
586         int verbose;
587         uint32_t core_id;
588         uint32_t chip_id;
589         int core_stat;
590
591 #define STM32_FLASH_PGSZ 1024
592 #define STM32L_FLASH_PGSZ 256
593
594 #define STM32F4_FLASH_PGSZ 16384
595 #define STM32F4_FLASH_SIZE (128 * 1024 * 8)
596
597         stm32_addr_t flash_base;
598         size_t flash_size;
599         size_t flash_pgsz;
600
601         /* sram settings */
602 #define STM32_SRAM_SIZE (8 * 1024)
603 #define STM32L_SRAM_SIZE (16 * 1024)
604         stm32_addr_t sram_base;
605         size_t sram_size;
606
607         // bootloader
608         stm32_addr_t sys_base;
609         size_t sys_size;
610
611         struct stlink_version_ version;
612     };
613
614     //stlink_t* stlink_quirk_open(const char *dev_name, const int verbose);
615
616     // delegated functions...
617     void stlink_enter_swd_mode(stlink_t *sl);
618     void stlink_enter_jtag_mode(stlink_t *sl);
619     void stlink_exit_debug_mode(stlink_t *sl);
620     void stlink_exit_dfu_mode(stlink_t *sl);
621     void stlink_close(stlink_t *sl);
622     uint32_t stlink_core_id(stlink_t *sl);
623     void stlink_reset(stlink_t *sl);
624     void stlink_jtag_reset(stlink_t *sl, int value);
625     void stlink_run(stlink_t *sl);
626     void stlink_status(stlink_t *sl);
627     void stlink_version(stlink_t *sl);
628     uint32_t stlink_read_debug32(stlink_t *sl, uint32_t addr);
629     void stlink_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len);
630     void stlink_write_debug32(stlink_t *sl, uint32_t addr, uint32_t data);
631     void stlink_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len);
632     void stlink_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len);
633     void stlink_read_all_regs(stlink_t *sl, reg *regp);
634     void stlink_read_all_unsupported_regs(stlink_t *sl, reg *regp);
635     void stlink_read_reg(stlink_t *sl, int r_idx, reg *regp);
636     void stlink_read_unsupported_reg(stlink_t *sl, int r_idx, reg *regp);
637     void stlink_write_unsupported_reg(stlink_t *sl, uint32_t value, int r_idx, reg *regp);
638     void stlink_write_reg(stlink_t *sl, uint32_t reg, int idx);
639     void stlink_step(stlink_t *sl);
640     int stlink_current_mode(stlink_t *sl);
641     void stlink_force_debug(stlink_t *sl);
642     int stlink_target_voltage(stlink_t *sl);
643
644
645     // unprocessed
646     int stlink_erase_flash_mass(stlink_t* sl);
647     int stlink_write_flash(stlink_t* sl, stm32_addr_t address, uint8_t* data, uint32_t length);
648     int stlink_fwrite_flash(stlink_t *sl, const char* path, stm32_addr_t addr);
649     int stlink_fwrite_sram(stlink_t *sl, const char* path, stm32_addr_t addr);
650     int stlink_verify_write_flash(stlink_t *sl, stm32_addr_t address, uint8_t *data, uint32_t length);
651
652     // PUBLIC
653     uint32_t stlink_chip_id(stlink_t *sl);
654     void stlink_cpu_id(stlink_t *sl, cortex_m3_cpuid_t *cpuid);
655
656     // privates, publics, the rest....
657     // TODO sort what is private, and what is not
658     int stlink_erase_flash_page(stlink_t* sl, stm32_addr_t flashaddr);
659     uint32_t stlink_calculate_pagesize(stlink_t *sl, uint32_t flashaddr);
660     uint16_t read_uint16(const unsigned char *c, const int pt);
661     void stlink_core_stat(stlink_t *sl);
662     void stlink_print_data(stlink_t *sl);
663     unsigned int is_bigendian(void);
664     uint32_t read_uint32(const unsigned char *c, const int pt);
665     void write_uint32(unsigned char* buf, uint32_t ui);
666     void write_uint16(unsigned char* buf, uint16_t ui);
667     unsigned int is_core_halted(stlink_t *sl);
668     int write_buffer_to_sram(stlink_t *sl, flash_loader_t* fl, const uint8_t* buf, size_t size);
669     int write_loader_to_sram(stlink_t *sl, stm32_addr_t* addr, size_t* size);
670     int stlink_fread(stlink_t* sl, const char* path, stm32_addr_t addr, size_t size);
671     int run_flash_loader(stlink_t *sl, flash_loader_t* fl, stm32_addr_t target, const uint8_t* buf, size_t size);
672     int stlink_load_device_params(stlink_t *sl);
673
674
675
676 #include "stlink-sg.h"
677 #include "stlink-usb.h"
678
679
680
681 #ifdef  __cplusplus
682 }
683 #endif
684
685 #endif  /* STLINK_COMMON_H */