Successfully locate and open stlinkv1 by usb
[fw/stlink] / src / test_sg.c
1 /* 
2  * File:   test_main.c
3  * 
4  * main() ripped out of old stlink-hw.c
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "stlink-common.h"
10
11 int main(int argc, char *argv[]) {
12         // set scpi lib debug level: 0 for no debug info, 10 for lots
13         char *dev_name;
14
15         switch (argc) {
16         case 1:
17                 fputs(
18                         "\nUsage: stlink-access-test /dev/sg0, sg1, ...\n"
19                                 "\n*** Notice: The stlink firmware violates the USB standard.\n"
20                                 "*** If you plug-in the discovery's stlink, wait a several\n"
21                                 "*** minutes to let the kernel driver swallow the broken device.\n"
22                                 "*** Watch:\ntail -f /var/log/messages\n"
23                                 "*** This command sequence can shorten the waiting time and fix some issues.\n"
24                                 "*** Unplug the stlink and execute once as root:\n"
25                                 "modprobe -r usb-storage && modprobe usb-storage quirks=483:3744:lrwsro\n\n",
26                         stderr);
27                 return EXIT_FAILURE;
28         case 2:
29                 dev_name = argv[1];
30                 break;
31         default:
32         fprintf(stderr, "bzzt\n");
33                 return EXIT_FAILURE;
34         }
35
36         stlink_t *sl = stlink_v1_open(dev_name, 99);
37         if (sl == NULL)
38                 return EXIT_FAILURE;
39
40         // we are in mass mode, go to swd
41         stlink_enter_swd_mode(sl);
42         stlink_current_mode(sl);
43         stlink_core_id(sl);
44         //----------------------------------------------------------------------
45
46         stlink_status(sl);
47         //stlink_force_debug(sl);
48         stlink_reset(sl);
49         stlink_status(sl);
50 #if 0
51         // core system control block
52         stlink_read_mem32(sl, 0xe000ed00, 4);
53         DD(sl, "cpu id base register: SCB_CPUID = got 0x%08x expect 0x411fc231", read_uint32(sl->q_buf, 0));
54         // no MPU
55         stlink_read_mem32(sl, 0xe000ed90, 4);
56         DD(sl, "mpu type register: MPU_TYPER = got 0x%08x expect 0x0", read_uint32(sl->q_buf, 0));
57
58         stlink_read_mem32(sl, 0xe000edf0, 4);
59         DD(sl, "DHCSR = 0x%08x", read_uint32(sl->q_buf, 0));
60
61         stlink_read_mem32(sl, 0x4001100c, 4);
62         DD(sl, "GPIOC_ODR = 0x%08x", read_uint32(sl->q_buf, 0));
63 #endif
64 #if 0
65         // happy new year 2011: let blink all the leds
66         // see "RM0041 Reference manual - STM32F100xx advanced ARM-based 32-bit MCUs"
67
68 #define GPIOC           0x40011000 // port C
69 #define GPIOC_CRH       (GPIOC + 0x04) // port configuration register high
70 #define GPIOC_ODR       (GPIOC + 0x0c) // port output data register
71 #define LED_BLUE        (1<<8) // pin 8
72 #define LED_GREEN       (1<<9) // pin 9
73         stlink_read_mem32(sl, GPIOC_CRH, 4);
74         uint32_t io_conf = read_uint32(sl->q_buf, 0);
75         DD(sl, "GPIOC_CRH = 0x%08x", io_conf);
76
77         // set: general purpose output push-pull, output mode, max speed 10 MHz.
78         write_uint32(sl->q_buf, 0x44444411);
79         stlink_write_mem32(sl, GPIOC_CRH, 4);
80
81         clear_buf(sl);
82         for (int i = 0; i < 100; i++) {
83                 write_uint32(sl->q_buf, LED_BLUE | LED_GREEN);
84                 stlink_write_mem32(sl, GPIOC_ODR, 4);
85                 /* stlink_read_mem32(sl, 0x4001100c, 4); */
86                 /* DD(sl, "GPIOC_ODR = 0x%08x", read_uint32(sl->q_buf, 0)); */
87                 delay(100);
88
89                 clear_buf(sl);
90                 stlink_write_mem32(sl, GPIOC_ODR, 4); // PC lo
91                 delay(100);
92         }
93         write_uint32(sl->q_buf, io_conf); // set old state
94
95 #endif
96 #if 0
97         // TODO rtfm: stlink doesn't have flash write routines
98         // writing to the flash area confuses the fw for the next read access
99
100         //stlink_read_mem32(sl, 0, 1024*6);
101         // flash 0x08000000 128kB
102         fputs("++++++++++ read a flash at 0x0800 0000\n", stderr);
103         stlink_read_mem32(sl, 0x08000000, 1024 * 6); //max 6kB
104         clear_buf(sl);
105         stlink_read_mem32(sl, 0x08000c00, 5);
106         stlink_read_mem32(sl, 0x08000c00, 4);
107         mark_buf(sl);
108         stlink_write_mem32(sl, 0x08000c00, 4);
109         stlink_read_mem32(sl, 0x08000c00, 256);
110         stlink_read_mem32(sl, 0x08000c00, 256);
111 #endif
112 #if 0
113         // sram 0x20000000 8kB
114         fputs("\n++++++++++ read/write 8bit, sram at 0x2000 0000 ++++++++++++++++\n\n", stderr);
115         clear_buf(sl);
116         stlink_write_mem8(sl, 0x20000000, 16);
117
118         mark_buf(sl);
119         stlink_write_mem8(sl, 0x20000000, 1);
120         stlink_write_mem8(sl, 0x20000001, 1);
121         stlink_write_mem8(sl, 0x2000000b, 3);
122         stlink_read_mem32(sl, 0x20000000, 16);
123 #endif
124 #if 0
125         // a not aligned mem32 access doesn't work indeed
126         fputs("\n++++++++++ read/write 32bit, sram at 0x2000 0000 ++++++++++++++++\n\n", stderr);
127         clear_buf(sl);
128         stlink_write_mem8(sl, 0x20000000, 32);
129
130         mark_buf(sl);
131         stlink_write_mem32(sl, 0x20000000, 1);
132         stlink_read_mem32(sl, 0x20000000, 16);
133         mark_buf(sl);
134         stlink_write_mem32(sl, 0x20000001, 1);
135         stlink_read_mem32(sl, 0x20000000, 16);
136         mark_buf(sl);
137         stlink_write_mem32(sl, 0x2000000b, 3);
138         stlink_read_mem32(sl, 0x20000000, 16);
139
140         mark_buf(sl);
141         stlink_write_mem32(sl, 0x20000000, 17);
142         stlink_read_mem32(sl, 0x20000000, 32);
143 #endif
144 #if 0
145         // sram 0x20000000 8kB
146         fputs("++++++++++ read/write 32bit, sram at 0x2000 0000 ++++++++++++\n", stderr);
147         mark_buf(sl);
148         stlink_write_mem8(sl, 0x20000000, 64);
149         stlink_read_mem32(sl, 0x20000000, 64);
150
151         mark_buf(sl);
152         stlink_write_mem32(sl, 0x20000000, 1024 * 8); //8kB
153         stlink_read_mem32(sl, 0x20000000, 1024 * 6);
154         stlink_read_mem32(sl, 0x20000000 + 1024 * 6, 1024 * 2);
155 #endif
156 #if 0
157         stlink_read_all_regs(sl);
158         stlink_step(sl);
159         fputs("++++++++++ write r0 = 0x12345678\n", stderr);
160         stlink_write_reg(sl, 0x12345678, 0);
161         stlink_read_reg(sl, 0);
162         stlink_read_all_regs(sl);
163 #endif
164 #if 0
165         stlink_run(sl);
166         stlink_status(sl);
167
168         stlink_force_debug(sl);
169         stlink_status(sl);
170 #endif
171 #if 0 /* read the system bootloader */
172         fputs("\n++++++++++ reading bootloader ++++++++++++++++\n\n", stderr);
173         stlink_fread(sl, "/tmp/barfoo", sl->sys_base, sl->sys_size);
174 #endif
175 #if 0 /* read the flash memory */
176         fputs("\n+++++++ read flash memory\n\n", stderr);
177         /* mark_buf(sl); */
178         stlink_read_mem32(sl, 0x08000000, 4);
179 #endif
180 #if 0 /* flash programming */
181         fputs("\n+++++++ program flash memory\n\n", stderr);
182         stlink_fwrite_flash(sl, "/tmp/foobar", 0x08000000);
183 #endif
184 #if 0 /* check file contents */
185         fputs("\n+++++++ check flash memory\n\n", stderr);
186         {
187           const int res = stlink_fcheck_flash(sl, "/tmp/foobar", 0x08000000);
188           printf("_____ stlink_fcheck_flash() == %d\n", res);
189         }
190 #endif
191 #if 0
192         fputs("\n+++++++ sram write and execute\n\n", stderr);
193         stlink_fwrite_sram(sl, "/tmp/foobar", sl->sram_base);
194         stlink_run_at(sl, sl->sram_base);
195 #endif
196
197 #if 0
198         stlink_run(sl);
199         stlink_status(sl);
200         //----------------------------------------------------------------------
201         // back to mass mode, just in case ...
202         stlink_exit_debug_mode(sl);
203         stlink_current_mode(sl);
204         stlink_close(sl);
205 #endif
206
207         //fflush(stderr); fflush(stdout);
208         return EXIT_SUCCESS;
209 }