X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=src%2Fao_serial.c;h=3f10376658cb10d02d627a9eed6c5b42be24b1c7;hp=ce11694002162ea5b70ab650c25f2c6dc14cae5f;hb=e6d6ad66ce177eb6e49eaa04b1ecc6426f4bbdbf;hpb=210dbaa23cdacf3a6f2d6e23493e96ee2ac9bca7 diff --git a/src/ao_serial.c b/src/ao_serial.c index ce116940..3f103766 100644 --- a/src/ao_serial.c +++ b/src/ao_serial.c @@ -50,6 +50,8 @@ ao_serial_tx1_isr(void) interrupt 14 ao_wakeup(&ao_usart1_tx_fifo); } +static __pdata serial_echo; + char ao_serial_getchar(void) __critical { @@ -57,6 +59,13 @@ ao_serial_getchar(void) __critical while (ao_fifo_empty(ao_usart1_rx_fifo)) ao_sleep(&ao_usart1_rx_fifo); ao_fifo_remove(ao_usart1_rx_fifo, c); + if (serial_echo) { + printf("%02x ", ((int) c) & 0xff); + if (c >= ' ') + putchar(c); + putchar('\n'); + flush(); + } return c; } @@ -70,20 +79,52 @@ ao_serial_putchar(char c) __critical } static void -send_serial(void) +ao_serial_drain(void) __critical { - ao_cmd_white(); - while (ao_cmd_lex_c != '\n') { - ao_serial_putchar(ao_cmd_lex_c); - ao_cmd_lex(); - } + while (!ao_fifo_empty(ao_usart1_tx_fifo)) + ao_sleep(&ao_usart1_tx_fifo); +} + +static void +monitor_serial(void) +{ + ao_cmd_hex(); + serial_echo = ao_cmd_lex_i != 0; } __code struct ao_cmds ao_serial_cmds[] = { - { 'S', send_serial, "S Send data to serial line" }, - { 0, send_serial, NULL }, + { 'M', monitor_serial, "M Monitor serial data" }, + { 0, monitor_serial, NULL }, +}; + +static const struct { + uint8_t baud; + uint8_t gcr; +} ao_serial_speeds[] = { + /* [AO_SERIAL_SPEED_4800] = */ { + /* .baud = */ 163, + /* .gcr = */ (7 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB + }, + /* [AO_SERIAL_SPEED_9600] = */ { + /* .baud = */ 163, + /* .gcr = */ (8 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB + }, + /* [AO_SERIAL_SPEED_57600] = */ { + /* .baud = */ 59, + /* .gcr = */ (11 << UxGCR_BAUD_E_SHIFT) | UxGCR_ORDER_LSB + }, }; +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; +} + void ao_serial_init(void) { @@ -99,8 +140,7 @@ ao_serial_init(void) U1CSR = (UxCSR_MODE_UART | UxCSR_RE); /* Pick a 4800 baud rate */ - U1BAUD = 163; /* 4800 */ - U1GCR = 7 << UxGCR_BAUD_E_SHIFT; /* 4800 */ + ao_serial_set_speed(AO_SERIAL_SPEED_4800); /* Reasonable serial parameters */ U1UCR = (UxUCR_FLUSH |