view main.cc @ 6:acbd0ddd002b

Add shared 1ms timer code Now blinks in a slightly useful manner Now has several submodes for acceptor state machine Clean ups
author Frank Kingswood <frank@kingswood-consulting.co.uk>
date Tue, 01 Apr 2014 20:35:06 +0100
parents 1a405bda2ffe
children ef38703286dd
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 *);
52 int main(void)
53 {
54 uint8_t n;
55 static char buf[100];
57 // set for 16 MHz clock, and turn on the LED
58 CPU_PRESCALE(0);
60 GPIB.Initialize();
61 Serial.Initialize();
62 blinky(255,1);
64 // initialize the USB, and then wait for the host
65 // to set configuration. If the Teensy is powered
66 // without a PC connected to the USB port, this
67 // will wait forever.
68 USB.Initialize();
69 while (!usb_configured())
70 { }
71 _delay_ms(1000);
73 while (1)
74 {
75 blinky(255,1);
77 // wait for the user to run their terminal emulator program
78 // which sets DTR to indicate it is ready to receive.
79 while (!(usb_serial_get_control() & USB_SERIAL_DTR))
80 { }
82 blinky(100,3);
84 // discard anything that was received prior. Sometimes the
85 // operating system or other software will send a modem
86 // "AT command", which can still be buffered.
87 usb_serial_flush_input();
89 // print a nice welcome message
90 Version();
92 // state
93 bool DirectMode=false;
95 // and then listen for commands and process them
96 while(1)
97 {
98 //GPIB.Report();
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 USB<<endl;
113 Command(buf+2);
114 }
115 else if(!DirectMode)
116 {
117 USB<<endl;
118 Command(buf);
119 }
120 else
121 {
122 // send GPIB
123 }
124 }
125 }
126 }
128 // Receive a string from the USB serial port. The string is stored
129 // in the buffer and this function will not exceed the buffer size.
130 // A carriage return or newline completes the string, and is not
131 // stored into the buffer.
132 // The return value is the number of characters received, or 255 if
133 // the virtual serial connection was closed while waiting.
134 //
135 uint8_t recv_str(char *buf, uint8_t size)
136 {
137 int16_t r;
138 uint8_t count=0;
140 while(count < size)
141 {
142 // TODO: move to main
143 r=GPIB.Poll();
144 if(r)
145 USB<<ROMS("++error ")<<r<<endl;
147 r=GPIB.Read();
148 if(r)
149 {
150 r&=0xff;
151 if(r==10)
152 USB<<ROMS("\\012\r\n");
153 else if(r=='\\')
154 USB<<ROMS("\\134");
155 else
156 USB.SendSafeChar(r);
157 }
159 r = usb_serial_getchar();
160 if(r==-1)
161 {
162 if(!usb_configured() || !(usb_serial_get_control() & USB_SERIAL_DTR))
163 {
164 // user no longer connected
165 return 255;
166 }
167 // just a normal timeout, keep waiting
168 }
169 else
170 {
171 if(r=='\r' || r=='\n')
172 return count;
173 if(r=='\b' && count>0)
174 { --buf;
175 --count;
176 usb_serial_putchar(r);
177 usb_serial_putchar(' ');
178 usb_serial_putchar(r);
179 }
180 else if(r >= ' ' && r <= '~')
181 {
182 *buf++ = r;
183 usb_serial_putchar(r);
184 count++;
185 }
186 }
187 }
188 return count;
189 }
191 /* ----- EOF main.cc ----- */