Commits

clee  committed 7e655af Draft

nice working tests of serial communication on unloaded maple

  • Participants
  • Parent commits 26720ed

Comments (0)

Files changed (3)

File testserial/results.log

+### tests using maple rev 5 (actually olimexino with sparkfun "ftdi basic" 232R  today) #####
+
+In [55]: print BAUDRATE
+9600
+
+%timeit download_1024_int16()
+1 loops, best of 3: 2.14 s per loop
+
+
+BAUDRATE = 115200
+In [19]: %timeit download_1024_int16()
+10 loops, best of 3: 179 ms per loop
+
+2048.0/0.179
+rate = 11441.340782122905
+
+In [21]: %timeit test_256_download()
+10 loops, best of 3: 23 ms per loop
+
+In [22]: print "rate: ", 256/0.023
+rate:  11130.4347826
+
+
+Opening /dev/ttyUSB0 at 2000000 baud # 8N1
+Number of char in receive buffer:  0
+
+In [26]: %timeit test_256_download()
+100 loops, best of 3: 2.96 ms per loop
+
+In [27]: print "rate: ", 256/0.00296
+rate:  86486.4864865
+
+
+In [28]: %timeit download_1024_int16()
+100 loops, best of 3: 11.5 ms per loop
+
+In [29]: print "rate: ", 2048/0.0115
+rate:  178086.956522 
+
+----
+ran torture test:
+- it does eventually fail
+
+1. after downloading 2048 bytes correctly 58649 times, it looks like I got a timeout
+
+2. it ran sucessfully 91204 times w/o an error -- I had to stop it myself to do something else

File testserial/testserial.pde

+/*
+    the FT232R supports all standard baud rate and non-standard baud rates from 183 to 3Mb/s
+    rate = 3000000/(n+x)
+    n is an integer between 2 and 16384 (2^14)
+    x can be a sub-integer 0,0.125,0.25,0.375, 0.5, 0.625, 0.75, or 0.875
+*/
+
+// Serial3.begin(BAUDRATE);
+// try faster 9600, 19200, 14400, 38400, 57600, 56000, 115200, 128000,
+// 230400,460800, 153600, 256000,921600
+#define BAUDRATE 2000000
+#define NLOOPS 2 // check serial every NLOOPS count
+
+uint32 state = 0;
+int gSeconds =0;
+int g_loop_cnt=0;
+
+void serial_term(void)
+{
+  if (! (g_loop_cnt %NLOOPS) ) {
+    if (Serial3.available()) {
+      uint8 input = Serial3.read();
+      switch(input) {
+      case '\r':
+	break;
+
+      case ' ':
+	//      SerialUSB.println("spacebar, nice!");
+	break;
+
+      case '?':
+      case 'h':
+	// cmd_print_help();
+	break;
+
+      case 'u':
+	// SerialUSB.println("Hello World!");
+	break;
+
+      case 'w':
+	Serial3.println("Hello!");
+	break;
+
+      case 'm':
+	// cmd_serial1_serial3();
+	break;
+
+      case 'E':
+	//cmd_serial1_echo();
+	break;
+
+      case '.':
+	break;
+
+      case 'd':
+	// Dumping from 0 to 1023 int16 data to Serial3 LSB first 
+	for(int16 ii =0; ii < 1024; ii++) {
+	  Serial3.write((char)ii);  
+	  Serial3.write((char) (ii >> 8));
+	}
+
+
+	// SerialUSB.println("Disabling USB.  Press BUT to re-enable.");
+	//  SerialUSB.end();
+	// pinMode(BOARD_BUTTON_PIN, INPUT);
+	//  while (!isButtonPressed())
+	//      ;
+	//  SerialUSB.begin();
+	break;
+
+      case 'n':
+	//cmd_adc_stats();
+	break;
+
+      case 'N':
+	// cmd_stressful_adc_stats();
+	break;
+
+      case 'e':
+	// cmd_everything();
+	break;
+
+      case 'W':
+
+	break;
+
+      case 'U':
+
+	// Dumping data to Serial3. 
+	Serial3.flush();
+	for(int ii =0; ii < 256; ii++) {
+	  Serial3.write((unsigned char)ii); // is this buffered?
+	}
+
+	  
+	break;
+
+      case 'g':
+	// cmd_sequential_gpio_toggling();
+	break;
+
+      case 'G':
+	// cmd_gpio_toggling();
+	break;
+
+      case 'l': // toggle the state (light blinking)
+	state ^= state;
+	toggleLED();
+	break;
+	
+      case 'j':
+	// cmd_sequential_debug_gpio_toggling();
+	break;
+
+      case 'J':
+	// cmd_debug_gpio_toggling();
+	break;
+
+      case 'B':
+	// cmd_but_test();
+	break;
+
+      case 'f':
+	Serial3.println("Wiggling D4 as quickly as possible in bursts. "
+			"Press any key to stop.");
+	pinMode(4, OUTPUT);
+	{
+	  int toggle=0;
+	  while (!Serial3.available()) {
+	    // fast_gpio(4);
+	    // delay(1);
+	    digitalWrite(BOARD_LED_PIN, toggle);
+	    toggle ^= 1;
+	  }
+	}
+	break;
+
+      case 'p':
+	// cmd_sequential_pwm_test();
+	break;
+
+      case '_':
+	// Serial3.println("Delaying for 5 seconds...");
+	// delay(5000);
+	break;
+
+        // Be sure to update cmd_print_help() if you implement these:
+
+      case 't':               // TODO
+
+	break;
+
+      case 'T':               // TODO
+
+	break;
+
+      case 's':
+
+	break;
+
+        // Be sure to update cmd_print_help() if you implement these:
+
+      case 'i':               // TODO
+
+	break;
+
+      case 'I':               // TODO
+	Serial3.println("Unimplemented.");
+	break;
+
+      case 'r':
+	// cmd_gpio_monitoring();
+	break;
+
+      case 'a':
+	// cmd_sequential_adc_reads();
+	break;
+
+      case 'b':
+	// cmd_board_info();
+	break;
+
+      case '+':
+	// cmd_gpio_qa();
+	break;
+
+      default: // -------------------------------
+	// Serial3.print("Unexpected byte: 0x");
+	//  Serial3.print((int)input, HEX);
+	//  Serial3.println(", press h for help.");
+	;
+      }
+
+      // Serial3.print("> ");
+
+
+    }
+
+  }
+
+}
+
+
+void setup()
+{
+    /* Set up the LED to blink  */
+    pinMode(BOARD_LED_PIN, OUTPUT);
+
+    SerialUSB.end(); // turn off SerialUSB because it generates a lot of interrupts in libmaple 
+    /* Set up Serial3 */
+    Serial3.begin(BAUDRATE);
+    
+}
+
+
+
+void loop() {
+  serial_term();
+
+  gSeconds++;
+  g_loop_cnt++;
+}

File testserial/testserialclient.py

+# FTDI basic client
+# ser.getSupportedBaudrates()
+# Out[116]: 
+# [('50', 50),
+#  ('75', 75),
+#  ('110', 110),
+#  ('134', 134),
+#  ('150', 150),
+#  ('200', 200),
+#  ('300', 300),
+#  ('600', 600),
+#  ('1200', 1200),
+#  ('1800', 1800),
+#  ('2400', 2400),
+#  ('4800', 4800),
+#  ('9600', 9600),
+#  ('19200', 19200),
+#  ('38400', 38400),
+#  ('57600', 57600),
+#  ('115200', 115200),
+#  ('230400', 230400),
+#  ('460800', 460800),
+#  ('500000', 500000),
+#  ('576000', 576000),
+#  ('921600', 921600),
+#  ('1000000', 1000000),
+#  ('1152000', 1152000),
+#  ('1500000', 1500000),
+#  ('2000000', 2000000),
+#  ('2500000', 2500000),
+#  ('3000000', 3000000),
+#  ('3500000', 3500000),
+#  ('4000000', 4000000)]
+
+
+import serial, glob, sys
+import numpy as np
+
+# ls /dev/ttyUSB*  # to see what ports are available
+# ser = serial.Serial(r'/dev/ttyUSB2', 9600)   # safe, works
+# ser = serial.Serial(r'/dev/ttyUSB2', 921600) # works
+
+# 9600, 115200
+BAUDRATE = 2000000
+def guess_ftdi_port():
+    
+    # for linux, discover likely port to use; others require different methods
+    port = r'/dev/ttyUSB2'
+    possibleports = glob.glob(r'/dev/ttyUSB*')
+    try:
+        port = possibleports[0]
+        return port
+    except IndexError:
+        print "no ftdi USB to serial port found in /dev/ttyUSB*"
+        raise
+
+
+PORT = guess_ftdi_port()
+print "Opening %s at %s baud" % (PORT, BAUDRATE)
+ser = serial.Serial(PORT, BAUDRATE, timeout=5.0)
+
+print "Number of char in receive buffer: ", ser.inWaiting()
+# can also flushInput() and flushOutput()
+
+def hello():
+    ser.write('w')
+    resp = ser.readline()
+    print resp
+    if not resp:
+        print "!!! got no response---timed out"
+        
+
+
+def test_256_download():
+    """
+    downloads 256 char which are expected to b numbers from 0..255
+    and tests that this is so
+    """
+    ser.write('U')
+    # rec = read_chars(256)
+    rec = ser.read(256)
+    assert rec
+    uirec = [ord(ii) for ii in rec]
+    assert np.all([ii == uirec[ii] for ii in range(256)])
+    return uirec, rec
+
+def read_chars(n):
+    buf = []    
+    for ii in xrange(n):
+        c = ser.read()
+        buf.append(c)
+    return buf
+
+
+
+
+def download_1024_int16():
+    ser.write('d')
+    resp = ser.read(size=2048) # two bytes per uint16
+    assert len(resp) == 2048
+    iarr= np.fromstring(resp, dtype='<i2')
+    tarr = np.arange(1024, dtype=np.int16)
+    assert np.all(iarr==tarr)
+    return iarr, resp 
+
+
+def upload_int16arr(int16arr):
+    ser.write('D')
+    s = int16arr.tostring()
+    ser.write(s)
+    
+
+def assemble_uints(bytearray):
+    # probably there is a better way to do this ? see structure or array package
+    iarr = map(ord,bytearray)
+    return [ iarr[2*ii] + (iarr[2*ii+1] << 8) for ii in xrange(len(iarr)/2) ]
+        
+def torture_test():
+    ii = 0
+    while 1:
+        a,b = download_1024_int16()
+        print ii
+        ii += 1