3 /* According to POSIX.1-2001 */
4 #include <sys/select.h>
12 #include "stlink-common.h"
14 #define STLINKY_MAGIC 0xDEADF00D
16 #define READ_UINT32_LE(buf) ((uint32_t) ( buf[0] \
28 /* Detects stlinky in RAM, returns handler */
29 struct stlinky* stlinky_detect(stlink_t* sl)
31 static const uint32_t sram_base = 0x20000000;
32 struct stlinky* st = malloc(sizeof(struct stlinky));
35 printf("sram: 0x%x bytes @ 0x%zx\n", sl->sram_base, sl->sram_size);
37 for (off = 0; off < sl->sram_size; off += 4) {
38 stlink_read_mem32(sl, sram_base + off, 4);
39 if (STLINKY_MAGIC == READ_UINT32_LE(sl->q_buf))
42 printf("WARNING: another ");
43 printf("stlinky detected at 0x%x\n", sram_base + off);
44 st->off = sram_base + off;
45 stlink_read_mem32(sl, st->off + 4, 4);
46 st->bufsize = (size_t) *(unsigned char*) sl->q_buf;
47 printf("stlinky buffer size 0x%zu \n", st->bufsize);
53 printf("Using last stlinky structure detected\n");
60 int stlinky_canrx(struct stlinky *st)
62 stlink_read_mem32(st->sl, st->off+4, 4);
63 unsigned char tx = (unsigned char) st->sl->q_buf[1];
67 size_t stlinky_rx(struct stlinky *st, char* buffer)
71 stlink_read_mem32(st->sl, st->off+4, 4);
72 tx = (unsigned char) st->sl->q_buf[1];
74 size_t rs = tx + (4 - (tx % 4)); /* voodoo */
75 stlink_read_mem32(st->sl, st->off+8, rs);
76 memcpy(buffer, st->sl->q_buf, (size_t) tx);
78 stlink_write_mem8(st->sl, st->off+5, 1);
82 size_t stlinky_tx(struct stlinky *st, char* buffer, size_t sz)
86 stlink_read_mem32(st->sl, st->off+4, 4);
87 rx = (unsigned char) st->sl->q_buf[2];
89 memcpy(st->sl->q_buf, buffer, sz);
90 size_t rs = sz + (4 - (sz % 4)); /* voodoo */
91 stlink_write_mem32(st->sl, st->off+8+st->bufsize, rs);
92 *st->sl->q_buf=(unsigned char) sz;
93 stlink_write_mem8(st->sl, st->off+6, 1);
104 FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
105 select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
106 return FD_ISSET(STDIN_FILENO, &fds);
109 void nonblock(int state)
111 struct termios ttystate;
113 //get the terminal state
114 tcgetattr(STDIN_FILENO, &ttystate);
118 //turn off canonical mode
119 ttystate.c_lflag &= ~ICANON;
120 ttystate.c_lflag &= ~ECHO;
121 //minimum of number input read.
122 ttystate.c_cc[VMIN] = 1;
126 //turn on canonical mode
127 ttystate.c_lflag |= ICANON | ECHO;
129 //set the terminal attributes.
130 tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
134 static int keep_running = 1;
135 static int sigcount=0;
136 void cleanup(int dummy)
141 printf("\n\nGot a signal\n");
143 printf("\n\nGot a second signal - bailing out\n");
149 int main(int ac, char** av) {
155 sl = stlink_open_usb(10, 1);
157 printf("ST-Linky proof-of-concept terminal :: Created by Necromant for lulz\n");
159 stlink_enter_swd_mode(sl);
160 printf("chip id: %#x\n", sl->chip_id);
161 printf("core_id: %#x\n", sl->core_id);
163 cortex_m3_cpuid_t cpuid;
164 stlink_cpu_id(sl, &cpuid);
165 printf("cpuid:impl_id = %0#x, variant = %#x\n", cpuid.implementer_id, cpuid.variant);
166 printf("cpuid:part = %#x, rev = %#x\n", cpuid.part, cpuid.revision);
169 stlink_force_debug(sl);
173 /* wait for device to boot */
174 /* TODO: Make timeout adjustable via command line */
177 struct stlinky *st = stlinky_detect(sl);
180 printf("stlinky magic not found in sram :(\n");
183 char* rxbuf = malloc(st->bufsize);
184 char* txbuf = malloc(st->bufsize);
187 int fd = fileno(stdin);
188 int saved_flags = fcntl(fd, F_GETFL);
189 fcntl(fd, F_SETFL, saved_flags & ~O_NONBLOCK);
190 signal(SIGINT, cleanup);
191 printf("Entering interactive terminal. CTRL+C to exit\n\n\n");
193 if (stlinky_canrx(st)) {
194 tmp = stlinky_rx(st, rxbuf);
195 fwrite(rxbuf,tmp,1,stdout);
199 tmp = read(fd, txbuf, st->bufsize);
200 stlinky_tx(st,txbuf,tmp);
207 stlink_exit_debug_mode(sl);