aboutsummaryrefslogtreecommitdiff
path: root/fw/protocol.c
diff options
context:
space:
mode:
authorjaseg <git@jaseg.net>2019-01-12 12:31:59 +0900
committerjaseg <git@jaseg.net>2019-01-12 12:54:29 +0900
commit43f64f0e1fad6e3586cdd5b6f0243f03db9adc23 (patch)
treeb2dc5c38e5b60230970b2e50e06de974b4b702fd /fw/protocol.c
parentc6547c6e6fa381a7811d0c47603291d82be7e440 (diff)
download8seg-43f64f0e1fad6e3586cdd5b6f0243f03db9adc23.tar.gz
8seg-43f64f0e1fad6e3586cdd5b6f0243f03db9adc23.tar.bz2
8seg-43f64f0e1fad6e3586cdd5b6f0243f03db9adc23.zip
Split receiver into logical parts
Diffstat (limited to 'fw/protocol.c')
-rw-r--r--fw/protocol.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/fw/protocol.c b/fw/protocol.c
new file mode 100644
index 0000000..2940aca
--- /dev/null
+++ b/fw/protocol.c
@@ -0,0 +1,43 @@
+
+#include "protocol.h"
+#include "8b10b.h"
+
+void receive_symbol(struct proto_rx_st *st, int symbol) {
+ if (symbol == -K28_1) { /* Comma/frame delimiter */
+ st->rxpos = 0;
+ /* Fall through and return and just ignore incomplete packets */
+
+ } else if (symbol == -DECODING_ERROR) {
+ st->rxpos = -1;
+
+ } else if (symbol < 0) { /* Unknown comma symbol or error */
+ st->rxpos = -1;
+
+ } else if (st->rxpos == -1) {
+ return;
+
+ } else if (st->rxpos == 0) { /* First data symbol, and not an error or comma symbol */
+ st->packet_type = symbol & ~PKT_TYPE_BULK_FLAG;
+ if (st->packet_type >= st->cmd_if->packet_type_max) {
+ st->rxpos = -1;
+ return;
+ }
+
+ st->is_bulk = symbol & PKT_TYPE_BULK_FLAG;
+ st->offset = (st->is_bulk) ? st->address*st->cmd_if->payload_len[st->packet_type]+1 : 2;
+ st->rxpos++;
+
+ } else if (!st->is_bulk && st->rxpos == 1) {
+ st->rxpos = (symbol == st->address) ? 2 : -1;
+
+ } else {
+ st->argbuf[st->rxpos - st->offset] = symbol;
+ st->rxpos++;
+
+ if (st->rxpos - st->offset == st->cmd_if->payload_len[st->packet_type]) {
+ handle_command(st->packet_type, (uint8_t *)st->argbuf);
+ st->rxpos = -1;
+ }
+ }
+}
+