diff --git a/README b/README new file mode 100644 index 0000000..df43d75 --- /dev/null +++ b/README @@ -0,0 +1,8 @@ +# USART Commands + +Instead of polling for input we use an interrupt vector +to write data to a rx ring buffer when it's available. + +We parse commands using a state machine so we can deal +with small chunks of characters at a time and spit out +the parsed command when the transmission has finished. diff --git a/src/main.c b/src/main.c index e37c006..adc6b5e 100644 --- a/src/main.c +++ b/src/main.c @@ -3,24 +3,66 @@ #include + +// # Serial Input Data Flow +// USART INTERRUPT -> RING BUFFER -> COMMAND PARSER STATE MACHINE +// +// # Supported Commands +// (Optional decimal point for numbers) +// - START +// - RESET +// - STOP +// - SET TEMP 20 +// - SET DEWP 60.0 + +static int state; + +void parse(char ch) +{ + switch (state) { + case 0: + if (ch == 't') + state = 1; + break; + case 1: + if (ch == 'e') + state = 2; + else + state = 0; + case 2: + if (ch == 's') + state = 3; + else + state = 0; + case 3: + if (ch == 't') + state = 4; + else + state = 0; + case 4: + if (ch == '\n') { + USART_Printf("[CORE] Parsed 'test'!\r\n"); + state = 0; + } else + state = 0; + } +} + int main(void) { unsigned long i; char ch; - // Handle USART_RXC interrupt - UCSRB |= (1 << RXCIE); - sei(); - USART_Init(); for (i = 1;; i++) { if (i >= 99999) i = 1; - ch = USART_GetChar(); + while ((ch = USART_GetChar())) { + parse(ch); + } + USART_Printf("[CORE] Fetching sensors #%05lu...\r\n", i); - USART_Printf("[CORE] USART_GetChar() = '%c'\r\n", - (ch) ? ch : ' '); Sleep(3000); } diff --git a/src/serial.c b/src/serial.c index b011fda..0a9ec7e 100644 --- a/src/serial.c +++ b/src/serial.c @@ -20,6 +20,10 @@ void USART_Init(void) rxhead = 0; rxtail = 0; + // Handle RXC interrupt + UCSRB |= (1 << RXCIE); + sei(); + UCSRB |= BIT(RXEN) | BIT(TXEN); // Enable rx and tx circuitry UCSRC |= BIT(URSEL) | BIT(UCSZ0) | BIT(UCSZ1); // 8-bit chars UBRRH = (USART_BAUD_PRESCALE >> 8); // Baud Rate upper byte