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