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