Fix all compilation warnings
[fw/stlink] / src / st-term.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 /* According to POSIX.1-2001 */
4 #include <sys/select.h>
5 #include <string.h>
6 #include <sys/time.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <termios.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include "stlink-common.h"
13
14 #define STLINKY_MAGIC 0xDEADF00D
15
16 #define READ_UINT32_LE(buf)  ((uint32_t) (   buf[0]         \
17                                            | buf[1] <<  8   \
18                                            | buf[2] << 16   \
19                                            | buf[3] << 24))
20
21 struct stlinky {
22         stlink_t *sl;
23         uint32_t off;
24         size_t bufsize;
25 };
26
27
28 /* Detects stlinky in RAM, returns handler */
29 struct stlinky*  stlinky_detect(stlink_t* sl)
30 {
31         static const uint32_t sram_base = 0x20000000;
32         struct stlinky* st = malloc(sizeof(struct stlinky));
33         st->sl = sl;
34         printf("sram: 0x%x bytes @ 0x%zx\n", sl->sram_base, sl->sram_size);
35         uint32_t off;
36         for (off = 0; off < sl->sram_size; off += 4) {
37                 stlink_read_mem32(sl, sram_base + off, 4);
38                 if (STLINKY_MAGIC == READ_UINT32_LE(sl->q_buf))
39                 {
40                         printf("stlinky detected at 0x%x\n", sram_base + off);
41                         st->off = sram_base + off;
42                         stlink_read_mem32(sl, st->off + 4, 4);
43                         st->bufsize = (size_t) *(unsigned char*) sl->q_buf;
44                         printf("stlinky buffer size 0x%zu \n", st->bufsize);
45                         return st;
46                 }
47         }
48         return NULL;
49 }
50
51 int stlinky_canrx(struct stlinky *st)
52 {
53         stlink_read_mem32(st->sl, st->off+4, 4);
54         unsigned char tx = (unsigned char) st->sl->q_buf[1];
55         return (int) tx;
56 }
57
58 size_t stlinky_rx(struct stlinky *st, char* buffer)
59 {
60         unsigned char tx = 0;
61         while(tx == 0) {
62                 stlink_read_mem32(st->sl, st->off+4, 4);
63                 tx = (unsigned char) st->sl->q_buf[1];
64         }
65         size_t rs = tx + (4 - (tx % 4)); /* voodoo */
66         stlink_read_mem32(st->sl, st->off+8, rs);
67         memcpy(buffer, st->sl->q_buf, (size_t) tx);
68         *st->sl->q_buf=0x0;
69         stlink_write_mem8(st->sl, st->off+5, 1);
70         return (size_t) tx;
71 }
72
73 size_t stlinky_tx(struct stlinky *st, char* buffer, size_t sz)
74 {
75         unsigned char rx = 1;
76         while(rx != 0) {
77                 stlink_read_mem32(st->sl, st->off+4, 4);
78                 rx = (unsigned char) st->sl->q_buf[2];
79         }
80         memcpy(st->sl->q_buf, buffer, sz);
81         size_t rs = sz + (4 - (sz % 4)); /* voodoo */
82         stlink_write_mem32(st->sl, st->off+8+st->bufsize, rs);
83         *st->sl->q_buf=(unsigned char) sz;
84         stlink_write_mem8(st->sl, st->off+6, 1);
85         return (size_t) rx;
86 }
87
88 int kbhit()
89 {
90         struct timeval tv;
91         fd_set fds;
92         tv.tv_sec = 0;
93         tv.tv_usec = 0;
94         FD_ZERO(&fds);
95         FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
96         select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
97         return FD_ISSET(STDIN_FILENO, &fds);
98 }
99
100 void nonblock(int state)
101 {
102         struct termios ttystate;
103
104         //get the terminal state
105         tcgetattr(STDIN_FILENO, &ttystate);
106
107         if (state==1)
108         {
109                 //turn off canonical mode
110                 ttystate.c_lflag &= ~ICANON;
111                 ttystate.c_lflag &= ~ECHO;
112                 //minimum of number input read.
113                 ttystate.c_cc[VMIN] = 1;
114         }
115         else if (state==0)
116         {
117                 //turn on canonical mode
118                 ttystate.c_lflag |= ICANON | ECHO;
119         }
120         //set the terminal attributes.
121         tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
122
123 }
124
125 static int keep_running = 1;
126 static int sigcount=0;
127 void cleanup(int dummy)
128 {
129         (void) dummy;
130         sigcount++;
131         keep_running = 0;
132         printf("\n\nGot a signal\n");
133         if (sigcount==2) {
134                 printf("\n\nGot a second signal - bailing out\n");
135                 exit(1);
136         }
137 }
138
139
140 int main(int ac, char** av) {
141         stlink_t* sl;
142
143         /* unused */
144         ac = ac;
145         av = av;
146         sl = stlink_open_usb(10, 1);
147         if (sl != NULL) {
148                 printf("ST-Linky proof-of-concept terminal :: Created by Necromant for lulz\n");
149                 stlink_version(sl);
150                 stlink_enter_swd_mode(sl);
151                 printf("chip id: %#x\n", sl->chip_id);
152                 printf("core_id: %#x\n", sl->core_id);
153
154                 cortex_m3_cpuid_t cpuid;
155                 stlink_cpu_id(sl, &cpuid);
156                 printf("cpuid:impl_id = %0#x, variant = %#x\n", cpuid.implementer_id, cpuid.variant);
157                 printf("cpuid:part = %#x, rev = %#x\n", cpuid.part, cpuid.revision);
158
159                 stlink_reset(sl);
160                 stlink_force_debug(sl);
161                 stlink_run(sl);
162                 stlink_status(sl);
163
164                 /* wait for device to boot */
165                 /* TODO: Make timeout adjustable via command line */
166                 sleep(1);
167
168                 struct stlinky *st = stlinky_detect(sl);
169                 if (st == NULL)
170                 {
171                         printf("stlinky magic not found in sram :(\n");
172                         goto bailout;
173                 }
174                 char* rxbuf = malloc(st->bufsize);
175                 char* txbuf = malloc(st->bufsize);
176                 size_t tmp;
177                 nonblock(1);
178                 int fd = fileno(stdin);
179                 int saved_flags = fcntl(fd, F_GETFL);
180                 fcntl(fd, F_SETFL, saved_flags & ~O_NONBLOCK);
181                 signal(SIGINT, cleanup);
182                 printf("Entering interactive terminal. CTRL+C to exit\n\n\n");
183                 while(1) {
184                         if (stlinky_canrx(st)) {
185                                 tmp = stlinky_rx(st, rxbuf);
186                                 fwrite(rxbuf,tmp,1,stdout);
187                                 fflush(stdout);
188                         }
189                         if (kbhit()) {
190                                 tmp = read(fd, txbuf, st->bufsize);
191                                 stlinky_tx(st,txbuf,tmp);
192                         }
193                         if (!keep_running)
194                                 break;
195                 }
196         bailout:
197                 nonblock(0);
198                 stlink_exit_debug_mode(sl);
199                 stlink_close(sl);
200         }
201         return 0;
202 }