Added the config.* files as used in the helights case
[cerebrum.git] / msp / comm.c
blob704e9fd2ff08142945f58e0010619862c8f04953
2 #include <stdint.h>
3 #include <stddef.h>
4 #include <msp430.h>
5 #include "comm.h"
6 #include "uart_io.h"
8 void comm_loop(){
9 static uint8_t escape_state = 0;
10 static uint8_t state = 0;
11 static uint16_t pos = 0;
12 static uint16_t funcid = 0;
13 static uint16_t crcbuf = 0;
14 static uint8_t argbuf[ARGBUF_SIZE];
15 static uint16_t arglen = 0;
17 uint16_t v = uart_getc_nonblocking();
18 uint8_t c = v&0xff;
19 v &= 0xff00;
20 if(!v){ //a character was successfully received
21 if(escape_state){
22 if(c == '#'){ //FIXME
23 pos = 0;
24 return;
26 escape_state = 0;
27 }else{
28 if(c == '\\'){
29 escape_state = 1;
30 return;
33 //escape sequence handling completed. 'c' now contains the next char of the payload.
34 switch(state){
35 case 0: //receive funcid, payload length
36 switch(pos){
37 case 0:
38 funcid = c<<8;
39 pos++;
40 break;
41 case 1:
42 funcid |= c;
43 pos++;
44 break;
45 case 2:
46 arglen = c<<8;
47 pos++;
48 break;
49 case 3:
50 pos = c;
51 arglen |= c;
52 state = 1;
53 break;
55 break;
56 case 1: //receive arg payload
57 pos--;
58 argbuf[pos] = c;
59 if(pos == 0)
60 state = 3;
61 break;
62 case 2: //receive and check payload crc. finally, call the function and send the return value
63 if(pos == 0){
64 crcbuf = c<<8;
65 }else{
66 //successfully received the crc
67 //FIXME add crc checking
68 if(funcid < NUM_CALLBACKS){
69 //HACK: The argument buffer is currently passed as the response buffer for ram efficiency.
70 //Only write to the response buffer *after* you are done reading from it.
71 comm_callbacks[funcid](arglen, argbuf);
72 }else{
73 //FIXME error handling
76 break;
81 void putc_escaped(char c){
82 if(c == '\\'){
83 uart_putc(c);
85 uart_putc(c);