view main.cc @ 7:ef38703286dd

add ++echo command, bigger queues, infinite timeout on usb
author Frank Kingswood <frank@kingswood-consulting.co.uk>
date Fri, 11 Apr 2014 17:21:26 +0100
parents acbd0ddd002b
children
line wrap: on
line source
1 /* #------------------------------------------------------------------------#
2 | |
3 | main.cc |
4 | |
5 | USB-GPIB startup and main loop. |
6 | |
7 | Based in part on Simple example for Teensy USB Development Board. |
8 | http://www.pjrc.com/teensy/ Copyright (c) 2008 PJRC.COM, LLC |
9 | |
10 | Copyright 2014, Frank A. Kingswood, www.kingswood-consulting.co.uk |
11 | |
12 #------------------------------------------------------------------------# */
14 #include <avr/io.h>
15 #include <avr/pgmspace.h>
16 #include <stdint.h>
17 #include <util/delay.h>
18 #include "usb_serial.h"
19 #include "serial.h"
20 #include "gpib.h"
22 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
24 uint8_t recv_str(char *buf, uint8_t size);
25 const UsbSerial __attribute__((__progmem__)) USB;
28 /* -------------------------------------------------------------------------- */
30 // Command line state machine
31 enum State_t { UNCONNECTED, IDLE,
32 // source handshake
33 SH_START, SH_DATA, SH_WAIT_NRFD, SH_DAV, SH_WAIT_NDAC, SH_N_DAV,
34 // acceptor handshake
35 AH_START, AH_N_ATN, AH_READY, AH_WAIT_DAV, AH_NRFD, AH_N_NDAC,
36 AH_WAIT_NDAV, AH_NDAC, AH_N_NDAC_LAST, AH_WAIT_NDAV_LAST,
37 // become active controller
38 CAC_START, CAC_WAIT, CAC_COMPLETE,
39 // send interface clear
40 SIC_START, SIC_WAIT, SIC_COMPLETE,
41 // parallel poll
42 PP_START, PP_COMPLETE
43 // serial poll
44 };
45 State_t State;
47 void Version();
48 int16_t Command(char *);
49 bool Echo=true;
51 int main(void)
52 {
53 uint8_t n;
54 static char buf[100];
56 // set for 16 MHz clock, and turn on the LED
57 CPU_PRESCALE(0);
59 GPIB.Initialize();
60 Serial.Initialize();
61 blinky(255,1);
63 // initialize the USB, and then wait for the host
64 // to set configuration. If the Teensy is powered
65 // without a PC connected to the USB port, this
66 // will wait forever.
67 USB.Initialize();
68 while (!usb_configured())
69 { }
70 _delay_ms(1000);
72 while (1)
73 {
74 blinky(255,1);
76 // wait for the user to run their terminal emulator program
77 // which sets DTR to indicate it is ready to receive.
78 while (!(usb_serial_get_control() & USB_SERIAL_DTR))
79 { }
81 blinky(100,3);
83 // discard anything that was received prior. Sometimes the
84 // operating system or other software will send a modem
85 // "AT command", which can still be buffered.
86 usb_serial_flush_input();
88 // print a nice welcome message
89 Version();
91 // state
92 bool DirectMode=false;
94 // and then listen for commands and process them
95 while(1)
96 {
97 //GPIB.Report();
98 if(Echo)
99 USB<<ROMS("++");
101 blinky(5);
103 n = recv_str(buf, sizeof(buf));
104 if(n == 255)
105 break;
106 buf[n]=0;
108 blinky(7);
110 if(n>3 && buf[0]=='+' && buf[1]=='+')
111 {
112 if(Echo)
113 USB<<endl;
114 Command(buf+2);
115 }
116 else if(!DirectMode)
117 {
118 if(Echo)
119 USB<<endl;
120 Command(buf);
121 }
122 else
123 {
124 // send GPIB
125 }
126 }
127 }
128 }
130 // Receive a string from the USB serial port. The string is stored
131 // in the buffer and this function will not exceed the buffer size.
132 // A carriage return or newline completes the string, and is not
133 // stored into the buffer.
134 // The return value is the number of characters received, or 255 if
135 // the virtual serial connection was closed while waiting.
136 //
137 uint8_t recv_str(char *buf, uint8_t size)
138 {
139 int16_t r;
140 uint8_t count=0;
142 while(count < size)
143 {
144 // TODO: move to main
145 r=GPIB.Poll();
146 if(r)
147 USB<<ROMS("++error ")<<r<<endl;
149 r=GPIB.Read();
150 if(r)
151 {
152 r&=0xff;
153 if(r==10)
154 USB<<ROMS("\\012\r\n");
155 else if(r=='\\')
156 USB<<ROMS("\\134");
157 else
158 USB.SendSafeChar(r);
159 }
161 r = usb_serial_getchar();
162 if(r==-1)
163 {
164 if(!usb_configured() || !(usb_serial_get_control() & USB_SERIAL_DTR))
165 {
166 // user no longer connected
167 return 255;
168 }
169 // just a normal timeout, keep waiting
170 }
171 else
172 {
173 if(r=='\r' || r=='\n')
174 return count;
175 if(r=='\b' && count>0)
176 { --buf;
177 --count;
178 if(Echo)
179 { usb_serial_putchar(r);
180 usb_serial_putchar(' ');
181 usb_serial_putchar(r);
182 }
183 }
184 else if(r >= ' ' && r <= '~')
185 {
186 *buf++ = r;
187 if(Echo)
188 usb_serial_putchar(r);
189 count++;
190 }
191 }
192 }
193 return count;
194 }
196 /* ----- EOF main.cc ----- */