1 /* simple wrapper around the stlink_flash_write function */
3 // TODO - this should be done as just a simple flag to the st-util command line...
10 #include <sys/types.h>
11 #include "stlink-common.h"
13 #define DEBUG_LOG_LEVEL 100
14 #define STND_LOG_LEVEL 50
16 stlink_t *connected_stlink = NULL;
18 static void cleanup(int signal __attribute__((unused))) {
19 if (connected_stlink) {
20 /* Switch back to mass storage mode before closing. */
21 stlink_run(connected_stlink);
22 stlink_exit_debug_mode(connected_stlink);
23 stlink_close(connected_stlink);
29 enum st_cmds {DO_WRITE = 0, DO_READ = 1, DO_ERASE = 2};
42 static void usage(void)
44 puts("stlinkv1 command line: ./st-flash [--debug] [--reset] [--serial <iSerial>] {read|write} /dev/sgX path addr <size>");
45 puts("stlinkv1 command line: ./st-flash [--debug] /dev/sgX erase");
46 puts("stlinkv2 command line: ./st-flash [--debug] [--reset] [--serial <iSerial>] {read|write} path addr <size>");
47 puts("stlinkv2 command line: ./st-flash [--debug] [--serial <iSerial>] erase");
48 puts(" use hex format for addr, <iSerial> and <size>");
51 static int get_opts(struct opts* o, int ac, char** av)
53 /* stlinkv1 command line: ./st-flash {read|write} /dev/sgX path addr <size> */
54 /* stlinkv2 command line: ./st-flash {read|write} path addr <size> */
58 if (ac < 1) return -1;
60 if (strcmp(av[0], "--debug") == 0)
62 o->log_level = DEBUG_LOG_LEVEL;
68 o->log_level = STND_LOG_LEVEL;
71 if (strcmp(av[0], "--reset") == 0)
82 if (strcmp(av[0], "--serial") == 0)
88 puts("no valid hex value, length must be multiple of 2\n");
94 memcpy(buffer,&av[0][i],2);
95 o->serial[12-j] = (char)strtol((const char*)buffer,NULL, 16);
107 if (ac < 1) return -1;
112 if (strcmp(av[0], "erase") == 0)
124 if (ac < 3) return -1;
125 if (strcmp(av[0], "read") == 0)
136 o->size = strtoul(av[i + 3], NULL, 16);
138 else if (strcmp(av[0], "write") == 0)
155 o->filename = av[i + 1];
156 o->addr = strtoul(av[i + 2], NULL, 16);
162 int main(int ac, char** av)
166 char serial_buffer[13] = {0};
167 o.serial = serial_buffer;
171 if (get_opts(&o, ac - 1, av + 1) == -1)
173 printf("invalid command line\n");
178 if (o.devname != NULL) /* stlinkv1 */
180 sl = stlink_v1_open(o.log_level, 1);
181 if (sl == NULL) goto on_error;
182 sl->verbose = o.log_level;
186 sl = stlink_open_usb(o.log_level, 1, o.serial);
187 if (sl == NULL) goto on_error;
188 sl->verbose = o.log_level;
191 connected_stlink = sl;
192 signal(SIGINT, &cleanup);
193 signal(SIGTERM, &cleanup);
194 signal(SIGSEGV, &cleanup);
196 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
197 stlink_exit_dfu_mode(sl);
199 if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
200 stlink_enter_swd_mode(sl);
203 stlink_jtag_reset(sl,2);
207 // Disable DMA - Set All DMA CCR Registers to zero. - AKS 1/7/2013
208 if (sl->chip_id == STM32_CHIPID_F4)
210 memset(sl->q_buf,0,4);
211 for (int i=0;i<8;i++) {
212 stlink_write_mem32(sl,0x40026000+0x10+0x18*i,4);
213 stlink_write_mem32(sl,0x40026400+0x10+0x18*i,4);
214 stlink_write_mem32(sl,0x40026000+0x24+0x18*i,4);
215 stlink_write_mem32(sl,0x40026400+0x24+0x18*i,4);
219 // Core must be halted to use RAM based flashloaders
220 stlink_force_debug(sl);
223 if (o.cmd == DO_WRITE) /* write */
225 if ((o.addr >= sl->flash_base) &&
226 (o.addr < sl->flash_base + sl->flash_size)) {
227 err = stlink_fwrite_flash(sl, o.filename, o.addr);
230 printf("stlink_fwrite_flash() == -1\n");
234 else if ((o.addr >= sl->sram_base) &&
235 (o.addr < sl->sram_base + sl->sram_size)) {
236 err = stlink_fwrite_sram(sl, o.filename, o.addr);
239 printf("stlink_sram_flash() == -1\n");
243 } else if (o.cmd == DO_ERASE)
245 err = stlink_erase_flash_mass(sl);
248 printf("stlink_fwrite_flash() == -1\n");
254 if ((o.addr >= sl->flash_base) && (o.size == 0) &&
255 (o.addr < sl->flash_base + sl->flash_size))
256 o.size = sl->flash_size;
257 else if ((o.addr >= sl->sram_base) && (o.size == 0) &&
258 (o.addr < sl->sram_base + sl->sram_size))
259 o.size = sl->sram_size;
260 err = stlink_fread(sl, o.filename, o.addr, o.size);
263 printf("stlink_fread() == -1\n");
269 stlink_jtag_reset(sl,2);
279 stlink_exit_debug_mode(sl);