spi_console: use print/scanf instead of ncurses, hack 64 bit.
[avr_work.git] / common / spi_console_pc / spiconsole.c
blobd68d1f6b38555964ecdf0149139942bc05112098
1 /*
2 * spiconsole - A simple interface to usbtinyisp to act as a 'console'
3 * using SPI
4 * Copyright (C) 2008 Matt Anderson, adapted from usbtiny.c from avrdude
6 * avrdude - A Downloader/Uploader for AVR device programmers
7 * Copyright (C) 2007 Dick Streefland, adapted for 5.4 by Limor Fried
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <unistd.h>
28 #include <string.h>
29 #include <ncurses.h>
30 #include <ctype.h>
32 #include "usbtiny.h"
33 #include "usbtiny_def.h"
35 /* Why not. */
36 const char *progname;
40 static void
41 do_ncurses_ui(void)
43 uint8_t running = 1;
44 char **disp;
46 int row, col;
47 int posx = 0;
48 int ix;
50 // Do ncurses init stuffs.
51 initscr();
52 raw();
53 noecho();
54 timeout(0);
56 getmaxyx(stdscr, row, col);
58 row -= 1;
60 disp = (char**) malloc(sizeof(char*) * row);
62 for (ix = 0; ix < row; ++ix)
64 disp[ix] = (char*) malloc(sizeof(char) * (col+1));
65 disp[ix][col] = '\0';
68 while (running)
70 int c;
72 unsigned char input[4] = { 0, 0, 0, 0 };
73 unsigned char output[4];
75 ix = 0;
77 while ((c = getch()) && ix < 4)
79 if (c == '\003')
80 running = 0;
82 input[ix] = c;
83 ++ix;
86 // Exchange 4 bytes with usbtiny.
87 usbtiny_spi(input, output);
89 // If we received bytes in return, write them out.
90 for (ix = 0; ix < 4; ++ix)
92 if (output[ix] && isascii(output[ix])) {
93 if (output[ix] == '\n') posx=col;
94 else
95 sprintf(&disp[row-1][posx++], "%c", output[ix]);
97 #if 0
98 else
99 sprintf(&disp[row-1][posx++], ".");
100 #endif
102 if (posx == col)
104 int iy;
106 for(iy = 0; iy < row - 1; ++iy)
107 strcpy(disp[iy], disp[iy+1]);
109 disp[row-1][0] = '\0';
110 posx = 0;
114 for (ix = 0; ix < row; ++ix)
115 mvprintw(ix, 0, disp[ix]);
117 clrtoeol();
118 refresh();
120 usleep(1000);
123 clrtoeol();
124 refresh();
125 endwin();
129 void do_printf_ui(void) {
131 for(;;) {
132 uint8_t input[4] = { 0, 0, 0, 0 };
133 uint8_t output[4];
137 uint8_t c;
138 uint8_t i;
139 while((c = getchar()) && i < 4)
141 if (c == '\003')
142 goto end;
144 input[i] = c;
145 i++;
149 usbtiny_spi(input,output);
151 for(uint8_t i = 0; i < sizeof(input); i++ ) {
152 if (output[i] && isascii(output[i]))
153 putchar(output[i]);
155 usleep(1000);
157 end:
158 return;
161 int main(int argc, char **argv)
163 // XXX: bitclock always default for now.
164 // any faster than this with 1Mhz avr clock and bit errors are too likely.
165 double bitclock = 0.0002200;
167 progname = argv[0];
169 /* Find and open usbtiny. */
170 usbtiny_open();
172 // Check for bit-clock and tell the usbtiny to adjust itself
173 usbtiny_set_sck_period(bitclock);
176 // Let the device wake up.
177 usleep(50000);
179 do_ncurses_ui();
180 //do_printf_ui();
182 usb_control(USBTINY_POWERDOWN, 0, 0); // Send USB control command to device
183 usbtiny_close();
185 return 0;