Add STlink terminal draft
[fw/stlink] / src / st-term.c
1 #include <stdio.h>
2 #include "stlink-common.h"
3
4 #define STLINKY_MAGIC 0xDEADF00D
5
6 struct stlinky {
7         stlink_t *sl;
8         uint32_t off;
9         size_t bufsize;
10 };
11
12 void dump_qbuf(stlink_t* s)
13 {
14         printf("== 0x%x\n", *(uint32_t*)s->q_buf);
15 }
16
17 /* Detects stlinky in RAM, returns handler */ 
18 struct stlinky*  stlinky_detect(stlink_t* sl) 
19 {
20         static const uint32_t sram_base = 0x20000000;
21         struct stlinky* st = malloc(sizeof(struct stlinky));
22         st->sl = sl;
23         printf("sram: 0x%x bytes @ 0x%x\n", sl->sram_base, sl->sram_size);
24         uint32_t off;
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)
28                 {
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);
34                         return st;
35                 }
36         }
37         return NULL;
38 }
39
40
41 size_t stlinky_rx(struct stlinky *st, char* buffer)
42 {
43         unsigned char tx = 0;
44         while(tx == 0) {
45                 stlink_read_mem32(st->sl, st->off+4, 4);
46                 tx = (unsigned char) st->sl->q_buf[1];
47         }
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);
51         *st->sl->q_buf=0x0;
52         stlink_write_mem8(st->sl, st->off+5, 1);
53 }
54
55
56 int main(int ac, char** av) {
57     stlink_t* sl;
58     reg regs;
59
60     /* unused */
61     ac = ac;
62     av = av;
63
64     sl = stlink_open_usb(10);
65     if (sl != NULL) {
66         printf("ST Link terminal :: Created by Necromant\n");
67         stlink_version(sl);
68         
69         printf("mode before doing anything: %d\n", stlink_current_mode(sl));
70
71         if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) {
72             printf("-- exit_dfu_mode\n");
73             stlink_exit_dfu_mode(sl);
74         }
75
76         printf("-- enter_swd_mode\n");
77         stlink_enter_swd_mode(sl);
78         
79         printf("-- mode after entering swd mode: %d\n", stlink_current_mode(sl));
80
81         printf("-- chip id: %#x\n", sl->chip_id);
82         printf("-- core_id: %#x\n", sl->core_id);
83
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);
88
89         stlink_reset(sl);
90         stlink_force_debug(sl);
91         stlink_run(sl);
92         stlink_status(sl);
93         /* wait for device to boot */
94
95         sleep(1);
96
97         struct stlinky *st = stlinky_detect(sl);
98         if (st == NULL)
99         {
100                 printf("stlinky magic not found in sram :(");
101         }
102
103         while(1) {
104                 stlinky_rx(st, NULL);
105         }
106         //sleep(90);
107
108         printf("-- read_sram\n");
109         //     static const uint32_t sram_base = 0x8000000;
110         // uint32_t off;
111         //for (off = 0; off < 16; off += 4)
112         //    stlink_read_mem32(sl, sram_base + off, 4);
113         
114         //printf("FP_CTRL\n");
115         //stlink_read_mem32(sl, CM3_REG_FP_CTRL, 4);
116         
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); */
123
124         /* Test 32 bit Write */
125         write_uint32(sl->q_buf,0x01234567);
126         stlink_write_mem32(sl,0x200000a8,4);
127 #if 0
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);
132         
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);
140
141         printf("-- status\n");
142         stlink_status(sl);
143
144         printf("-- reset\n");
145         stlink_reset(sl);
146         stlink_force_debug(sl);
147         /* Test reg write*/
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, &regs);
153         
154        
155         stlink_read_all_regs(sl, &regs);
156
157         printf("-- status\n");
158         stlink_status(sl);
159
160         printf("-- step\n");
161         stlink_step(sl);
162
163         printf("-- run\n");
164         stlink_run(sl);
165
166         printf("-- exit_debug_mode\n");
167 #endif
168         stlink_exit_debug_mode(sl);
169
170         stlink_close(sl);
171     }
172
173     return 0;
174 }