2 #include "stlink-common.h"
4 #define STLINKY_MAGIC 0xDEADF00D
12 void dump_qbuf(stlink_t* s)
14 printf("== 0x%x\n", *(uint32_t*)s->q_buf);
17 /* Detects stlinky in RAM, returns handler */
18 struct stlinky* stlinky_detect(stlink_t* sl)
20 static const uint32_t sram_base = 0x20000000;
21 struct stlinky* st = malloc(sizeof(struct stlinky));
23 printf("sram: 0x%x bytes @ 0x%x\n", sl->sram_base, sl->sram_size);
25 for (off = 0; off < sl->sram_size; off += 4) {
26 stlink_read_mem32(sl, sram_base + off, 4);
27 if ( STLINKY_MAGIC== *(uint32_t*) sl->q_buf)
29 printf("stlinky detected at 0x%x\n", sram_base + off);
30 st->off = sram_base + off;
31 stlink_read_mem32(sl, st->off + 4, 4);
32 st->bufsize = (size_t) *(unsigned char*) sl->q_buf;
33 printf("stlinky buffer size 0x%zu (%hhx)\n", st->bufsize);
41 size_t stlinky_rx(struct stlinky *st, char* buffer)
45 stlink_read_mem32(st->sl, st->off+4, 4);
46 tx = (unsigned char) st->sl->q_buf[1];
48 size_t rs = tx + (4 - (tx % 4)); /* voodoo */
49 stlink_read_mem32(st->sl, st->off+8, rs);
50 printf(st->sl->q_buf);
52 stlink_write_mem8(st->sl, st->off+5, 1);
56 int main(int ac, char** av) {
64 sl = stlink_open_usb(10);
66 printf("ST Link terminal :: Created by Necromant\n");
69 printf("mode before doing anything: %d\n", stlink_current_mode(sl));
71 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) {
72 printf("-- exit_dfu_mode\n");
73 stlink_exit_dfu_mode(sl);
76 printf("-- enter_swd_mode\n");
77 stlink_enter_swd_mode(sl);
79 printf("-- mode after entering swd mode: %d\n", stlink_current_mode(sl));
81 printf("-- chip id: %#x\n", sl->chip_id);
82 printf("-- core_id: %#x\n", sl->core_id);
84 cortex_m3_cpuid_t cpuid;
85 stlink_cpu_id(sl, &cpuid);
86 printf("cpuid:impl_id = %0#x, variant = %#x\n", cpuid.implementer_id, cpuid.variant);
87 printf("cpuid:part = %#x, rev = %#x\n", cpuid.part, cpuid.revision);
90 stlink_force_debug(sl);
93 /* wait for device to boot */
97 struct stlinky *st = stlinky_detect(sl);
100 printf("stlinky magic not found in sram :(");
104 stlinky_rx(st, NULL);
108 printf("-- read_sram\n");
109 // static const uint32_t sram_base = 0x8000000;
111 //for (off = 0; off < 16; off += 4)
112 // stlink_read_mem32(sl, sram_base + off, 4);
114 //printf("FP_CTRL\n");
115 //stlink_read_mem32(sl, CM3_REG_FP_CTRL, 4);
117 // no idea what reg this is.. */
118 // stlink_read_mem32(sl, 0xe000ed90, 4);
119 // no idea what register this is...
120 // stlink_read_mem32(sl, 0xe000edf0, 4);
121 // offset 0xC into TIM11 register? TIMx_DIER?
122 // stlink_read_mem32(sl, 0x4001100c, 4); */
124 /* Test 32 bit Write */
125 write_uint32(sl->q_buf,0x01234567);
126 stlink_write_mem32(sl,0x200000a8,4);
128 write_uint32(sl->q_buf,0x89abcdef);
129 stlink_write_mem32(sl,0x200000ac, 4);
130 stlink_read_mem32(sl, 0x200000a8, 4);
131 stlink_read_mem32(sl, 0x200000ac, 4);
133 /* Test 8 bit write */
134 write_uint32(sl->q_buf,0x01234567);
135 stlink_write_mem8(sl,0x200001a8,3);
136 write_uint32(sl->q_buf,0x89abcdef);
137 stlink_write_mem8(sl, 0x200001ac, 3);
138 stlink_read_mem32(sl, 0x200001a8, 4);
139 stlink_read_mem32(sl, 0x200001ac, 4);
141 printf("-- status\n");
144 printf("-- reset\n");
146 stlink_force_debug(sl);
148 stlink_write_reg(sl, 0x01234567, 3);
149 stlink_write_reg(sl, 0x89abcdef, 4);
150 stlink_write_reg(sl, 0x12345678, 15);
151 for (off = 0; off < 21; off += 1)
152 stlink_read_reg(sl, off, ®s);
155 stlink_read_all_regs(sl, ®s);
157 printf("-- status\n");
166 printf("-- exit_debug_mode\n");
168 stlink_exit_debug_mode(sl);