Skip to content

Commit

Permalink
#20 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
daveythacher committed Sep 24, 2023
1 parent 2259f32 commit 58647f9
Showing 1 changed file with 87 additions and 20 deletions.
107 changes: 87 additions & 20 deletions LED_Matrix/lib/Serial/serial_uart/wip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,54 @@
#include <stdint.h>
#include <algorithm>

const uint8_t header_checksum[1][4] = { {0} };

// Preamble must be half of packet
struct Packet {
uint8_t preamble[32];
uint8_t inverse[16];
uint32_t length;
uint32_t command;
uint32_t marker;
uint32_t checksum;
uint8_t inverse[12];
uint8_t length[4];
uint8_t command[4];
uint8_t marker[4];
uint8_t header_checksum[4];
uint8_t payload_checksum[4];
};

static uint16_t index = 0;
constexpr uint8_t command_count = 1;
const uint8_t marker[4] = { 0x11, 0x19, 0x19, 0x90 };
extern const uint8_t header_checksum[command_count][4];

//uint8_t iterator(Packet *p1, Packet *p2, uint16_t i);

void __attribute__ ((noinline)) other_task();
void __attribute__ ((noinline)) task();

constexpr uint8_t rows = 3;
constexpr uint8_t columns = 16;
volatile bool isReady = false;

// Test misalignment (simulates errors in stream as seen by receiver)
typedef Packet my_name[columns][rows];
#define MISALIGN
#ifdef MISALIGN
Packet array2[columns*2][rows];
uint8_t *vector = (uint8_t *) &array2[0][0].preamble[0];
my_name *array;
#else
my_name array2;
my_name *array;// = &array2;
#endif

int main(int argc, char **argv) {
int LED_PIN = 0;

#ifdef MISALIGN
array = (my_name *) &vector[15];
#else
array = &array2;
#endif

gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);

Expand Down Expand Up @@ -49,7 +77,8 @@ constexpr uint8_t iterator(Packet *p1, Packet *p2, uint16_t i) {
}

bool search(Packet *p1, Packet *p2) {
int i, j;
int i, j, k;
uint32_t c;

// Look for start of preamble
for (i = std::max(index - 4, 0); i < std::min(index, (uint16_t) 4); i++) {
Expand All @@ -63,9 +92,33 @@ bool search(Packet *p1, Packet *p2) {
return false;
}

j += 4;
// Verify Inverse
for (k = j; k < (int) (j + sizeof(Packet::inverse)); k++) {
if (iterator(p1, p2, k) != ~0x19)
return false;
}
j = k;

// TODO: Extract command and length
c %= command_count;
j += 8;

// Verify Marker
for (k = j; k < (int) (j + sizeof(Packet::marker)); k++) {
if (iterator(p1, p2, k) != marker[k - j])
return false;
}
j = k;

// Verify header checksum (hard coded by compiler for each command)
// Length is a hard coded by compiler (this is factored into the checksum)
for (k = j; k < (int) (j + sizeof(Packet::header_checksum)); k++) {
if (iterator(p1, p2, k) != header_checksum[c][k - j])
return false;
}
j = k;

// TODO: Verify checksum, inverse, marker
// TODO: Extract payload checksum

// Stay in phase or realign phase
index = i;
Expand All @@ -83,21 +136,15 @@ bool isNewFrame(Packet *p1, Packet *p2) {
break;
}

// Spot check for preamble (fast reject/jump out)
if (*(ptr + index) == 0x19)
return search(p1, p2);
else
index += sizeof(Packet) / 4;
index++;
}

return false;
}

constexpr uint8_t rows = 3;
constexpr uint8_t columns = 16;
Packet array[columns][rows];
volatile bool isReady = false;

void reset_state_machine() {
// TODO
}
Expand All @@ -115,20 +162,20 @@ void task() {

if (isReady) {
// Handle boundary between rows
if (isNewFrame(&array[columns - 1][last], &array[0][(last + 1) % rows]))
if (isNewFrame(array[columns - 1][last], array[0][(last + 1) % rows]))
reset_state_machine();
state_machine(&array[columns - 1][last]);
state_machine(array[columns - 1][last]);

// Advance to next row
last = (last + 1) % rows;
isReady = false;

// Handle columns in row
for (int i = 0; i < (columns - 1); i++) {
if (isNewFrame(&array[i][last], &array[i + 1][last]))
if (isNewFrame(array[i][last], array[i + 1][last]))
reset_state_machine();

state_machine(&array[i][last]);
state_machine(array[i][last]);
}

flush_state_machine();
Expand All @@ -139,8 +186,28 @@ void other_task() {
static uint8_t num = 0;

for (int i = 0; i < columns; i++) {
for (int j = 0; j < (int) sizeof(Packet::command); j++) {
array[i][num]->command[j] = 0;
}

for (int j = 0; j < (int) sizeof(Packet::length); j++) {
array[i][num]->length[j] = (4096 >> (j * 8)) & 0xFF;
}

for (int j = 0; j < (int) sizeof(Packet::preamble); j++) {
array[i][num].preamble[j] = 0x19;
array[i][num]->preamble[j] = 0x19;
}

for (int j = 0; j < (int) sizeof(Packet::inverse); j++) {
array[i][num]->inverse[j] = ~0x19;
}

for (int j = 0; j < (int) sizeof(Packet::header_checksum); j++) {
array[i][num]->header_checksum[j] = header_checksum[0][j];
}

for (int j = 0; j < (int) sizeof(Packet::marker); j++) {
array[i][num]->marker[j] = marker[j];
}
}

Expand Down

0 comments on commit 58647f9

Please sign in to comment.