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