summaryrefslogtreecommitdiffstats
path: root/serial_link/protocol
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
parent419908e5ef15481bd22dcd4173a25562f6153a5a (diff)
Add link parameter
Diffstat (limited to 'serial_link/protocol')
-rw-r--r--serial_link/protocol/byte_stuffer.c75
-rw-r--r--serial_link/protocol/byte_stuffer.h4
-rw-r--r--serial_link/protocol/frame_router.h2
-rw-r--r--serial_link/protocol/frame_validator.c8
-rw-r--r--serial_link/protocol/frame_validator.h4
-rw-r--r--serial_link/protocol/physical.h2
6 files changed, 52 insertions, 43 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);
}
}
diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h
index 4dcdd24c11..cd94b45828 100644
--- a/serial_link/protocol/byte_stuffer.h
+++ b/serial_link/protocol/byte_stuffer.h
@@ -23,5 +23,5 @@ SOFTWARE.
*/
void init_byte_stuffer(void);
-void recv_byte(uint8_t data);
-void send_frame(uint8_t* data, uint16_t size);
+void recv_byte(uint8_t link, uint8_t data);
+void send_frame(uint8_t link, uint8_t* data, uint16_t size);
diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h
index dd1bfb1230..3906b06c71 100644
--- a/serial_link/protocol/frame_router.h
+++ b/serial_link/protocol/frame_router.h
@@ -22,4 +22,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
-void route_incoming_frame(uint8_t* data, uint16_t size);
+void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size);
diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c
index f326097ffa..80e662b2e8 100644
--- a/serial_link/protocol/frame_validator.c
+++ b/serial_link/protocol/frame_validator.c
@@ -102,19 +102,19 @@ static uint32_t crc32_byte(uint8_t *p, uint32_t bytelength)
return (crc ^ 0xffffffff);
}
-void validator_recv_frame(uint8_t* data, uint16_t size) {
+void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) {
if (size > 4) {
uint32_t frame_crc;
memcpy(&frame_crc, data + size -4, 4);
uint32_t expected_crc = crc32_byte(data, size - 4);
if (frame_crc == expected_crc) {
- route_incoming_frame(data, size-4);
+ route_incoming_frame(link, data, size-4);
}
}
}
-void validator_send_frame(uint8_t* data, uint16_t size) {
+void validator_send_frame(uint8_t link, uint8_t* data, uint16_t size) {
uint32_t crc = crc32_byte(data, size);
memcpy(data + size, &crc, 4);
- send_frame(data, size + 4);
+ send_frame(link, data, size + 4);
}
diff --git a/serial_link/protocol/frame_validator.h b/serial_link/protocol/frame_validator.h
index 2e518894fb..c35fc27260 100644
--- a/serial_link/protocol/frame_validator.h
+++ b/serial_link/protocol/frame_validator.h
@@ -22,6 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
-void validator_recv_frame(uint8_t* data, uint16_t size);
+void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size);
// The buffer pointed to by the data needs 4 additional bytes
-void validator_send_frame(uint8_t* data, uint16_t size);
+void validator_send_frame(uint8_t link, uint8_t* data, uint16_t size);
diff --git a/serial_link/protocol/physical.h b/serial_link/protocol/physical.h
index 73a7855dfb..ee5883d36b 100644
--- a/serial_link/protocol/physical.h
+++ b/serial_link/protocol/physical.h
@@ -22,4 +22,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
-void send_data(const uint8_t* data, uint16_t size);
+void send_data(uint8_t link, const uint8_t* data, uint16_t size);