changeset 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 1969a435a7a2
files commands.cc gpib.cc gpib.h main.cc usb_serial.c usb_serial.h
diffstat 6 files changed, 67 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/commands.cc	Tue Apr 01 20:35:06 2014 +0100
     1.2 +++ b/commands.cc	Fri Apr 11 17:21:26 2014 +0100
     1.3 @@ -350,13 +350,6 @@
     1.4           {
     1.5              tail++;
     1.6  
     1.7 -            //GPIB.EOI(eoi && *tail=='"');
     1.8 -            //byte=GPIB.Out(c);
     1.9 -            //GPIB.EOI(false);
    1.10 -            //
    1.11 -            //if(byte)
    1.12 -            //   goto fail;
    1.13 -
    1.14              cmd=GPIB.CMD_SOURCE | (c&0xff);
    1.15              if(atn)
    1.16                 cmd|=GPIB.CMD_ATN;
    1.17 @@ -556,6 +549,28 @@
    1.18  
    1.19  /* ..........................................................................
    1.20   *
    1.21 + * ++echo <mode>
    1.22 + *     if mode==1: echo commands
    1.23 + *     else: do not echo
    1.24 + */
    1.25 +extern bool Echo;
    1.26 +int16_t Command_echo(char *tail)
    1.27 +{
    1.28 +   switch(*tail)
    1.29 +   {  case 0:
    1.30 +      case '?':
    1.31 +         return Echo;
    1.32 +      case '0':
    1.33 +      case '1':
    1.34 +         Echo=(*tail&1);
    1.35 +         return NONE;
    1.36 +   }
    1.37 +
    1.38 +   return EVALUE;
    1.39 +}
    1.40 +
    1.41 +/* ..........................................................................
    1.42 + *
    1.43   * ++EOI <value>
    1.44   * ++EOI? -> <value>
    1.45   *
    1.46 @@ -949,6 +964,7 @@
    1.47   */
    1.48  int16_t Command_rst(char *tail)
    1.49  {
    1.50 +   Echo=true;
    1.51     GPIB.Initialize();
    1.52     return NONE;
    1.53  }
    1.54 @@ -1136,7 +1152,7 @@
    1.55  STRING(rsp);    STRING(rst);    STRING(rsv);    STRING(savecfg);
    1.56  STRING(self);   STRING(sic);    STRING(spoll);  STRING(sre);
    1.57  STRING(SRQ);    STRING(status); STRING(tmo);    STRING(trg);
    1.58 -STRING(ver);    STRING(write);  STRING(wrt);
    1.59 +STRING(ver);    STRING(write);  STRING(wrt);    STRING(echo);
    1.60  
    1.61  #define COMMAND(s) { String_##s, Command_##s }
    1.62  
    1.63 @@ -1149,7 +1165,7 @@
    1.64  
    1.65     COMMAND(addr),   COMMAND(auto),   COMMAND(cac),
    1.66     COMMAND(clr),    COMMAND(cmd),    COMMAND(data),
    1.67 -   COMMAND(eoi),    COMMAND(eos),    COMMAND(eot_char),
    1.68 +   COMMAND(echo),   COMMAND(eoi),    COMMAND(eos),    COMMAND(eot_char),
    1.69     COMMAND(eot_enable),
    1.70     COMMAND(error),  COMMAND(gts),    COMMAND(help),
    1.71     COMMAND(lines),  COMMAND(llo),    COMMAND(loc),    COMMAND(lon),
     2.1 --- a/gpib.cc	Tue Apr 01 20:35:06 2014 +0100
     2.2 +++ b/gpib.cc	Fri Apr 11 17:21:26 2014 +0100
     2.3 @@ -79,8 +79,8 @@
     2.4  
     2.5  #undef DEBUG
     2.6  
     2.7 -Queue<128> GPIBDriver::CommandQueue;
     2.8 -Queue<128> GPIBDriver::ReadQueue;
     2.9 +Queue<GPIBDriver::QUEUE_SIZE> GPIBDriver::CommandQueue;
    2.10 +Queue<GPIBDriver::QUEUE_SIZE> GPIBDriver::ReadQueue;
    2.11  
    2.12  uint16_t GPIBDriver::MyAddress;
    2.13  uint16_t GPIBDriver::DeviceAddress;
    2.14 @@ -278,6 +278,10 @@
    2.15  #ifdef DEBUG
    2.16     USB<<ROMS("Command(");
    2.17     USB.Hex(data);
    2.18 +
    2.19 +   uint16_t d=data&(CMD_SOURCE|CMD_ATN|0xff);
    2.20 +   if(d>=CMD_SOURCE+32 && d<CMD_SOURCE+127)
    2.21 +      USB<<'='<<'\''<<char(d)<<'\'';
    2.22     USB<<ROMS(")\r\n");
    2.23  #endif
    2.24     if(data==0)
    2.25 @@ -298,7 +302,7 @@
    2.26  int8_t GPIBDriver::Poll()
    2.27  {
    2.28     // tune this so we go round the loop often enough to send/receive one byte
    2.29 -   for(uint8_t loop=0; loop<100; loop++)
    2.30 +   for(uint8_t loop=0; loop<7; loop++)
    2.31     {
    2.32     #ifdef DEBUG
    2.33        if(Timer.Read()%128==1 && Timer.Read()!=lasttimer)
    2.34 @@ -508,7 +512,7 @@
    2.35              USB<<ROMS("data=");
    2.36              USB.Hex(data);
    2.37              if((data&0xff)>=32 && (data&0xff)<127)
    2.38 -            {  USB<<' '<<'\''<<char(data)<<'\'';
    2.39 +            {  USB<<'='<<'\''<<char(data)<<'\'';
    2.40              }
    2.41              USB<<endl;
    2.42           #endif
     3.1 --- a/gpib.h	Tue Apr 01 20:35:06 2014 +0100
     3.2 +++ b/gpib.h	Fri Apr 11 17:21:26 2014 +0100
     3.3 @@ -53,7 +53,8 @@
     3.4        if(In==o)
     3.5           return 0;
     3.6        uint16_t value=Data[o];
     3.7 -      if(++o>=SIZE)
     3.8 +      o++;
     3.9 +      if(SIZE<256 && o>=SIZE)
    3.10           o=0;
    3.11        Out=o;
    3.12        return value;
    3.13 @@ -61,7 +62,7 @@
    3.14     __attribute__((noinline)) bool Put(uint16_t value)
    3.15     {
    3.16        uint8_t i=In+1;
    3.17 -      if(i>=SIZE)
    3.18 +      if(SIZE<256 && i>=SIZE)
    3.19           i=0;
    3.20        if(i==Out)
    3.21           return false;
    3.22 @@ -101,9 +102,9 @@
    3.23     static ::Timer Timer;
    3.24  
    3.25     // command and read queues
    3.26 -   static const unsigned QUEUE_SIZE=128;
    3.27 -   static Queue<128> CommandQueue;
    3.28 -   static Queue<128> ReadQueue;
    3.29 +   static const unsigned QUEUE_SIZE=256;
    3.30 +   static Queue<QUEUE_SIZE> CommandQueue;
    3.31 +   static Queue<QUEUE_SIZE> ReadQueue;
    3.32  
    3.33     // GPIB state machine
    3.34     enum State_t { RESET, IDLE,
     4.1 --- a/main.cc	Tue Apr 01 20:35:06 2014 +0100
     4.2 +++ b/main.cc	Fri Apr 11 17:21:26 2014 +0100
     4.3 @@ -46,8 +46,7 @@
     4.4  
     4.5  void Version();
     4.6  int16_t Command(char *);
     4.7 -
     4.8 -
     4.9 +bool Echo=true;
    4.10  
    4.11  int main(void)
    4.12  {
    4.13 @@ -96,7 +95,8 @@
    4.14        while(1)
    4.15        {
    4.16           //GPIB.Report();
    4.17 -         USB<<ROMS("++");
    4.18 +         if(Echo)
    4.19 +            USB<<ROMS("++");
    4.20  
    4.21           blinky(5);
    4.22  
    4.23 @@ -109,12 +109,14 @@
    4.24  
    4.25           if(n>3 && buf[0]=='+' && buf[1]=='+')
    4.26           {
    4.27 -            USB<<endl;
    4.28 +            if(Echo)
    4.29 +               USB<<endl;
    4.30              Command(buf+2);
    4.31           }
    4.32           else if(!DirectMode)
    4.33           {
    4.34 -            USB<<endl;
    4.35 +            if(Echo)
    4.36 +               USB<<endl;
    4.37              Command(buf);
    4.38           }
    4.39           else
    4.40 @@ -173,14 +175,17 @@
    4.41           if(r=='\b' && count>0)
    4.42           {  --buf;
    4.43              --count;
    4.44 -            usb_serial_putchar(r);
    4.45 -            usb_serial_putchar(' ');
    4.46 -            usb_serial_putchar(r);
    4.47 +            if(Echo)
    4.48 +            {  usb_serial_putchar(r);
    4.49 +               usb_serial_putchar(' ');
    4.50 +               usb_serial_putchar(r);
    4.51 +            }
    4.52           }
    4.53           else if(r >= ' ' && r <= '~')
    4.54           {
    4.55              *buf++ = r;
    4.56 -            usb_serial_putchar(r);
    4.57 +            if(Echo)
    4.58 +               usb_serial_putchar(r);
    4.59              count++;
    4.60           }
    4.61        }
     5.1 --- a/usb_serial.c	Tue Apr 01 20:35:06 2014 +0100
     5.2 +++ b/usb_serial.c	Fri Apr 11 17:21:26 2014 +0100
     5.3 @@ -84,7 +84,7 @@
     5.4  // bits on a wire where nobody is listening, except you get an error
     5.5  // code which you can ignore for serial-like discard of data, or
     5.6  // use to know your data wasn't sent.
     5.7 -#define TRANSMIT_TIMEOUT   25   /* in milliseconds */
     5.8 +#define TRANSMIT_TIMEOUT   100   /* in milliseconds */
     5.9  
    5.10  // USB devices are supposed to implment a halt feature, which is
    5.11  // rarely (if ever) used.  If you comment this line out, the halt
    5.12 @@ -404,13 +404,15 @@
    5.13     }
    5.14  }
    5.15  
    5.16 -// transmit a character.  0 returned on success, -1 on error
    5.17 +// transmit a character.  0 returned on success, -1 on timeout or -9 on error
    5.18  int8_t usb_serial_putchar(uint8_t c)
    5.19  {
    5.20     uint8_t timeout, intr_state;
    5.21  
    5.22     // if we're not online (enumerated and configured), error
    5.23 -   if (!usb_configuration) return -1;
    5.24 +   if (!usb_configuration)
    5.25 +      return -9;
    5.26 +
    5.27     // interrupts are disabled so these functions can be
    5.28     // used from the main program or interrupt context,
    5.29     // even both in the same program!
    5.30 @@ -438,7 +440,8 @@
    5.31           return -1;
    5.32        }
    5.33        // has the USB gone offline?
    5.34 -      if (!usb_configuration) return -1;
    5.35 +      if (!usb_configuration)
    5.36 +         return -9;
    5.37        // get ready to try checking again
    5.38        intr_state = SREG;
    5.39        cli();
    5.40 @@ -455,7 +458,7 @@
    5.41  
    5.42  
    5.43  // transmit a character, but do not wait if the buffer is full,
    5.44 -//   0 returned on success, -1 on buffer full or error
    5.45 +//   0 returned on success, -1 on timeout, -2 on buffer full or -9 for error
    5.46  int8_t usb_serial_putchar_nowait(uint8_t c)
    5.47  {
    5.48     uint8_t intr_state;
    5.49 @@ -467,7 +470,7 @@
    5.50     if (!(UEINTX & (1<<RWAL))) {
    5.51        // buffer is full
    5.52        SREG = intr_state;
    5.53 -      return -1;
    5.54 +      return -2;
    5.55     }
    5.56     // actually write the byte into the FIFO
    5.57     UEDATX = c;
     6.1 --- a/usb_serial.h	Tue Apr 01 20:35:06 2014 +0100
     6.2 +++ b/usb_serial.h	Fri Apr 11 17:21:26 2014 +0100
     6.3 @@ -143,7 +143,11 @@
     6.4     }
     6.5  
     6.6     static __attribute__((noinline)) void Send_(char c)
     6.7 -   {  usb_serial_putchar(c);
     6.8 +   {
     6.9 +      // wait forever
    6.10 +      while(int8_t r=usb_serial_putchar(c))
    6.11 +         if(r==-9)
    6.12 +            return;
    6.13     }
    6.14  
    6.15     // immediately transmit any buffered output