aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorMarti Bolivar <mbolivar@leaflabs.com>2011-04-06 13:57:30 -0400
committerMarti Bolivar <mbolivar@leaflabs.com>2011-04-07 13:18:36 -0400
commit552b8882ef04cb0028ec30a04b5464b1a17bccad (patch)
tree27332e20a6b5cfad0629917eb257fad24a9589a4 /examples
parent012d9ba1c2069d4d072685b990cbd01b611bb751 (diff)
downloadlibrambutan-552b8882ef04cb0028ec30a04b5464b1a17bccad.tar.gz
librambutan-552b8882ef04cb0028ec30a04b5464b1a17bccad.zip
USART refactor.
Diffstat (limited to 'examples')
-rw-r--r--examples/test-serial-flush.cpp29
-rw-r--r--examples/test-session.cpp62
2 files changed, 68 insertions, 23 deletions
diff --git a/examples/test-serial-flush.cpp b/examples/test-serial-flush.cpp
index 1cd82b6..6c4e100 100644
--- a/examples/test-serial-flush.cpp
+++ b/examples/test-serial-flush.cpp
@@ -1,21 +1,26 @@
-// Tests the "flush" Serial function
+/*
+ * Tests the "flush" Serial function.
+ */
#include "wirish.h"
+#define COMM Serial1
+
void setup() {
- /* Send a message out USART2 */
- Serial2.begin(9600);
- Serial2.println("Hello world!");
+ COMM.begin(9600);
+ COMM.println("Hello world!");
}
void loop() {
- Serial2.println("Waiting for multiple input...");
- while(Serial2.available() < 5) { }
- Serial2.println(Serial2.read());
- Serial2.println(Serial2.read());
- Serial2.flush();
- if(Serial2.available()) {
- Serial2.println("FAIL! Still had junk in the buffer...");
+ COMM.println("Waiting for multiple input...");
+ while (COMM.available() < 5)
+ ;
+ COMM.println(COMM.read());
+ COMM.println(COMM.read());
+ COMM.flush();
+
+ if (COMM.available()) {
+ COMM.println("FAIL! Still had junk in the buffer...");
}
}
@@ -28,7 +33,7 @@ __attribute__((constructor)) void premain() {
int main(void) {
setup();
- while (1) {
+ while (true) {
loop();
}
return 0;
diff --git a/examples/test-session.cpp b/examples/test-session.cpp
index a4128ac..01d0fe9 100644
--- a/examples/test-session.cpp
+++ b/examples/test-session.cpp
@@ -23,6 +23,7 @@ void cmd_adc_stats(void);
void cmd_stressful_adc_stats(void);
void cmd_everything(void);
void cmd_serial1_serial3(void);
+void cmd_serial1_echo(void);
void cmd_gpio_monitoring(void);
void cmd_sequential_adc_reads(void);
void cmd_gpio_qa(void);
@@ -35,7 +36,8 @@ void cmd_board_info(void);
// Helper functions
void measure_adc_noise(uint8 pin);
void fast_gpio(int pin);
-void usart_baud_test(HardwareSerial **serials, int n, unsigned baud);
+void serial_baud_test(HardwareSerial **serials, int n, unsigned baud);
+void serial_echo_test(HardwareSerial *serial, unsigned baud);
void init_all_timers(uint16 prescale);
void enable_usarts(void);
void disable_usarts(void);
@@ -104,6 +106,10 @@ void loop () {
cmd_serial1_serial3();
break;
+ case 'E':
+ cmd_serial1_echo();
+ break;
+
case '.':
while (!SerialUSB.available()) {
Serial1.print(".");
@@ -225,8 +231,8 @@ void loop () {
break;
default: // -------------------------------
- SerialUSB.print("Unexpected: ");
- SerialUSB.print(input);
+ SerialUSB.print("Unexpected byte: 0x");
+ SerialUSB.print((int)input, HEX);
SerialUSB.println(", press h for help.");
}
@@ -243,11 +249,13 @@ void cmd_print_help(void) {
SerialUSB.println("\th: print this menu");
SerialUSB.println("\tw: print Hello World on all 3 USARTS");
SerialUSB.println("\tn: measure noise and do statistics");
- SerialUSB.println("\tN: measure noise and do statistics with background stuff");
+ SerialUSB.println("\tN: measure noise and do statistics with background "
+ "stuff");
SerialUSB.println("\ta: show realtime ADC info");
SerialUSB.println("\t.: echo '.' until new input");
SerialUSB.println("\tu: print Hello World on USB");
- SerialUSB.println("\t_: do as little as possible for a couple seconds (delay)");
+ SerialUSB.println("\t_: do as little as possible for a couple seconds "
+ "(delay)");
SerialUSB.println("\tp: test all PWM channels sequentially");
SerialUSB.println("\tW: dump data as fast as possible on all 3 USARTS");
SerialUSB.println("\tU: dump data as fast as possible on USB");
@@ -256,7 +264,9 @@ void cmd_print_help(void) {
SerialUSB.println("\tf: toggle pin 4 as fast as possible in bursts");
SerialUSB.println("\tr: monitor and print GPIO status changes");
SerialUSB.println("\ts: output a sweeping servo PWM on all PWM channels");
- SerialUSB.println("\tm: output data on USART1 and USART3 with various rates");
+ SerialUSB.println("\tm: output data on USART1 and USART3 at various "
+ "baud rates");
+ SerialUSB.println("\tE: echo data on USART1 at various baud rates");
SerialUSB.println("\tb: print information about the board.");
SerialUSB.println("\t+: test shield mode (for quality assurance testing)");
@@ -316,17 +326,17 @@ void cmd_serial1_serial3(void) {
SerialUSB.println("Testing 57600 baud on USART1 and USART3. "
"Press any key to stop.");
- usart_baud_test(serial_1_and_3, 2, 57600);
+ serial_baud_test(serial_1_and_3, 2, 57600);
SerialUSB.read();
SerialUSB.println("Testing 115200 baud on USART1 and USART3. "
"Press any key to stop.");
- usart_baud_test(serial_1_and_3, 2, 115200);
+ serial_baud_test(serial_1_and_3, 2, 115200);
SerialUSB.read();
SerialUSB.println("Testing 9600 baud on USART1 and USART3. "
"Press any key to stop.");
- usart_baud_test(serial_1_and_3, 2, 9600);
+ serial_baud_test(serial_1_and_3, 2, 9600);
SerialUSB.read();
SerialUSB.println("Resetting USART1 and USART3...");
@@ -334,6 +344,27 @@ void cmd_serial1_serial3(void) {
Serial3.begin(BAUD);
}
+void cmd_serial1_echo(void) {
+ SerialUSB.println("Testing serial echo at various baud rates. "
+ "Press any key for next baud rate, or ESC to quit "
+ "early.");
+ while (!SerialUSB.available())
+ ;
+ SerialUSB.read();
+
+ SerialUSB.println("Testing 115200 baud on USART1.");
+ serial_echo_test(&Serial1, 115200);
+ if (SerialUSB.read() == ESC) return;
+
+ SerialUSB.println("Testing 57600 baud on USART1.");
+ serial_echo_test(&Serial1, 57600);
+ if (SerialUSB.read() == ESC) return;
+
+ SerialUSB.println("Testing 9600 baud on USART1.");
+ serial_echo_test(&Serial1, 9600);
+ if (SerialUSB.read() == ESC) return;
+}
+
void cmd_gpio_monitoring(void) {
SerialUSB.println("Monitoring pin state changes. Press any key to stop.");
@@ -615,7 +646,7 @@ void cmd_board_info(void) { // TODO print more information
SerialUSB.println("Board information");
SerialUSB.println("=================");
- SerialUSB.print("* Clock speed (cycles/us): ");
+ SerialUSB.print("* Clock speed (MHz): ");
SerialUSB.println(CYCLES_PER_MICROSECOND);
SerialUSB.print("* BOARD_LED_PIN: ");
@@ -693,7 +724,7 @@ void fast_gpio(int maple_pin) {
gpio_toggle_bit(dev, bit);
}
-void usart_baud_test(HardwareSerial **serials, int n, unsigned baud) {
+void serial_baud_test(HardwareSerial **serials, int n, unsigned baud) {
for (int i = 0; i < n; i++) {
serials[i]->begin(baud);
}
@@ -708,6 +739,15 @@ void usart_baud_test(HardwareSerial **serials, int n, unsigned baud) {
}
}
+void serial_echo_test(HardwareSerial *serial, unsigned baud) {
+ serial->begin(baud);
+ while (!SerialUSB.available()) {
+ if (!serial->available())
+ continue;
+ serial->print(serial->read());
+ }
+}
+
static uint16 init_all_timers_prescale = 0;
static void set_prescale(timer_dev *dev) {