summaryrefslogtreecommitdiffstats
path: root/serial_link/protocol/byte_stuffer.c
diff options
context:
space:
mode:
authorFred Sundvik <fsundvik@gmail.com>2016-02-20 12:06:23 +0200
committerFred Sundvik <fsundvik@gmail.com>2016-02-20 12:06:23 +0200
commit5a1600e24f2ca0cf8e9d75e23691bc1971657934 (patch)
treec80ed5168cfc3bbe0abb56285a1d562ebe3f6c3c /serial_link/protocol/byte_stuffer.c
parent419908e5ef15481bd22dcd4173a25562f6153a5a (diff)
Add link parameter
Diffstat (limited to 'serial_link/protocol/byte_stuffer.c')
-rw-r--r--serial_link/protocol/byte_stuffer.c75
1 files changed, 42 insertions, 33 deletions
diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c
index e5a26c2c2e..05b7aa6f80 100644
--- a/serial_link/protocol/byte_stuffer.c
+++ b/serial_link/protocol/byte_stuffer.c
@@ -32,6 +32,7 @@ SOFTWARE.
// http://www.stuartcheshire.org/papers/COBSforToN.pdf
#define MAX_FRAME_SIZE 1024
+#define NUM_LINKS 2
typedef struct byte_stuffer_state {
uint16_t next_zero;
@@ -40,70 +41,78 @@ typedef struct byte_stuffer_state {
uint8_t data[MAX_FRAME_SIZE];
}byte_stuffer_state_t;
-static byte_stuffer_state_t state;
+static byte_stuffer_state_t states[NUM_LINKS];
+
+void init_byte_stuffer_state(byte_stuffer_state_t* state) {
+ state->next_zero = 0;
+ state->data_pos = 0;
+ state->long_frame = false;
+}
void init_byte_stuffer(void) {
- state.next_zero = 0;
- state.data_pos = 0;
- state.long_frame = false;
+ int i;
+ for (i=0;i<NUM_LINKS;i++) {
+ init_byte_stuffer_state(&states[i]);
+ }
}
-void recv_byte(uint8_t data) {
+void recv_byte(uint8_t link, uint8_t data) {
+ byte_stuffer_state_t* state = &states[link];
// Start of a new frame
- if (state.next_zero == 0) {
- state.next_zero = data;
- state.long_frame = data == 0xFF;
- state.data_pos = 0;
+ if (state->next_zero == 0) {
+ state->next_zero = data;
+ state->long_frame = data == 0xFF;
+ state->data_pos = 0;
return;
}
- state.next_zero--;
+ state->next_zero--;
if (data == 0) {
- if (state.next_zero == 0) {
+ if (state->next_zero == 0) {
// The frame is completed
- if (state.data_pos > 0) {
- validator_recv_frame(state.data, state.data_pos);
+ if (state->data_pos > 0) {
+ validator_recv_frame(link, state->data, state->data_pos);
}
}
else {
// The frame is invalid, so reset
- init_byte_stuffer();
+ init_byte_stuffer_state(state);
}
}
else {
- if (state.data_pos == MAX_FRAME_SIZE) {
+ if (state->data_pos == MAX_FRAME_SIZE) {
// We exceeded our maximum frame size
// therefore there's nothing else to do than reset to a new frame
- state.next_zero = data;
- state.long_frame = data == 0xFF;
- state.data_pos = 0;
+ state->next_zero = data;
+ state->long_frame = data == 0xFF;
+ state->data_pos = 0;
}
- else if (state.next_zero == 0) {
- if (state.long_frame) {
+ else if (state->next_zero == 0) {
+ if (state->long_frame) {
// This is part of a long frame, so continue
- state.next_zero = data;
- state.long_frame = data == 0xFF;
+ state->next_zero = data;
+ state->long_frame = data == 0xFF;
}
else {
// Special case for zeroes
- state.next_zero = data;
- state.data[state.data_pos++] = 0;
+ state->next_zero = data;
+ state->data[state->data_pos++] = 0;
}
}
else {
- state.data[state.data_pos++] = data;
+ state->data[state->data_pos++] = data;
}
}
}
-static void send_block(uint8_t* start, uint8_t* end, uint8_t num_non_zero) {
- send_data(&num_non_zero, 1);
+static void send_block(uint8_t link, uint8_t* start, uint8_t* end, uint8_t num_non_zero) {
+ send_data(link, &num_non_zero, 1);
if (end > start) {
- send_data(start, end-start);
+ send_data(link, start, end-start);
}
}
-void send_frame(uint8_t* data, uint16_t size) {
+void send_frame(uint8_t link, uint8_t* data, uint16_t size) {
const uint8_t zero = 0;
if (size > 0) {
uint16_t num_non_zero = 1;
@@ -113,14 +122,14 @@ void send_frame(uint8_t* data, uint16_t size) {
if (num_non_zero == 0xFF) {
// There's more data after big non-zero block
// So send it, and start a new block
- send_block(start, data, num_non_zero);
+ send_block(link, start, data, num_non_zero);
start = data;
num_non_zero = 1;
}
else {
if (*data == 0) {
// A zero encountered, so send the block
- send_block(start, data, num_non_zero);
+ send_block(link, start, data, num_non_zero);
start = data + 1;
num_non_zero = 1;
}
@@ -130,7 +139,7 @@ void send_frame(uint8_t* data, uint16_t size) {
++data;
}
}
- send_block(start, data, num_non_zero);
- send_data(&zero, 1);
+ send_block(link, start, data, num_non_zero);
+ send_data(link, &zero, 1);
}
}