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