Add B command to set serial baud rate
[fw/altos] / src / ao_serial.c
index 7bc11b979ac38137abd5e62c4a263711c5f097bf..59110354efd3651192c7a4dd7199d03b7333152f 100644 (file)
@@ -75,6 +75,13 @@ ao_serial_putchar(char c) __critical
        ao_serial_tx1_start();
 }
 
+static void
+ao_serial_drain(void) __critical
+{
+       while (!ao_fifo_empty(ao_usart1_tx_fifo))
+               ao_sleep(&ao_usart1_tx_fifo);
+}
+
 static void
 send_serial(void)
 {
@@ -92,9 +99,17 @@ monitor_serial(void)
        serial_echo = ao_cmd_lex_i != 0;
 }
 
+static void
+serial_baud(void)
+{
+       ao_cmd_hex();
+       ao_serial_set_speed(ao_cmd_lex_i);
+}
+
 __code struct ao_cmds ao_serial_cmds[] = {
        { 'S', send_serial,             "S <data>                           Send data to serial line" },
        { 'M', monitor_serial,          "M <enable>                         Monitor serial data" },
+       { 'B', serial_baud,             "B <0 = 4800, 1 = 57600>            Set serial baud rate" },
        { 0, send_serial, NULL },
 };
 
@@ -115,6 +130,9 @@ static const struct {
 void
 ao_serial_set_speed(uint8_t speed)
 {
+       ao_serial_drain();
+       if (speed > AO_SERIAL_SPEED_57600)
+               return;
        U1BAUD = ao_serial_speeds[speed].baud;
        U1GCR = ao_serial_speeds[speed].gcr;
 }