Work on error correction
This commit is contained in:
parent
13af4dcd2b
commit
ae3bfffd28
|
@ -3,6 +3,25 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <drv/ser.h>
|
#include <drv/ser.h>
|
||||||
|
|
||||||
|
static uint8_t lastByte = 0x00;
|
||||||
|
static bool sendParityBlock = false;
|
||||||
|
|
||||||
|
INLINE bool BIT(uint8_t byte, int n) { return (byte & BV(n)); }
|
||||||
|
static uint8_t mp1ParityBlock(uint8_t first, uint8_t other) {
|
||||||
|
uint8_t parity = 0x00;
|
||||||
|
parity ^= (BIT(first, 1) ^ BIT(first, 2) ^ BIT(first, 4) ^ BIT(first, 5) ^ BIT(first, 7)) << 7;
|
||||||
|
parity ^= (BIT(first, 1) ^ BIT(first, 3) ^ BIT(first, 4) ^ BIT(first, 6) ^ BIT(first, 7)) << 6;
|
||||||
|
parity ^= (BIT(first, 2) ^ BIT(first, 3) ^ BIT(first, 4) ^ BIT(first, 8)) << 5;
|
||||||
|
parity ^= (BIT(first, 5) ^ BIT(first, 6) ^ BIT(first, 7) ^ BIT(first, 8)) << 4;
|
||||||
|
|
||||||
|
parity ^= BIT(other, 1) ^ BIT(other, 2) ^ BIT(other, 4) ^ BIT(other, 5) ^ BIT(other, 7) << 3;
|
||||||
|
parity ^= BIT(other, 1) ^ BIT(other, 3) ^ BIT(other, 4) ^ BIT(other, 6) ^ BIT(other, 7) << 2;
|
||||||
|
parity ^= BIT(other, 2) ^ BIT(other, 3) ^ BIT(other, 4) ^ BIT(other, 8) << 1;
|
||||||
|
parity ^= BIT(other, 5) ^ BIT(other, 6) ^ BIT(other, 7) ^ BIT(other, 8);
|
||||||
|
|
||||||
|
return parity;
|
||||||
|
}
|
||||||
|
|
||||||
static void mp1Decode(MP1 *mp1) {
|
static void mp1Decode(MP1 *mp1) {
|
||||||
// This decode function is basic and bare minimum.
|
// This decode function is basic and bare minimum.
|
||||||
// It does nothing more than extract the data
|
// It does nothing more than extract the data
|
||||||
|
@ -31,69 +50,90 @@ void mp1Poll(MP1 *mp1) {
|
||||||
|
|
||||||
// Read bytes from the modem until we reach EOF
|
// Read bytes from the modem until we reach EOF
|
||||||
while ((byte = kfile_getc(mp1->modem)) != EOF) {
|
while ((byte = kfile_getc(mp1->modem)) != EOF) {
|
||||||
if (!mp1->escape && byte == HDLC_FLAG) {
|
// We have a byte, increment our read counter
|
||||||
// We are not in an escape sequence and we
|
mp1->readLength++;
|
||||||
// found a HDLC_FLAG. This can mean two things:
|
|
||||||
if (mp1->packetLength >= MP1_MIN_FRAME_LENGTH) {
|
if (mp1->readLength % 3 != 0) {
|
||||||
// We already have more data than the minimum
|
// This is not a parity byte
|
||||||
// frame length, which means the flag signifies
|
if (!mp1->escape && byte == HDLC_FLAG) {
|
||||||
// the end of the packet. Pass control to the
|
// We are not in an escape sequence and we
|
||||||
// decoder.
|
// found a HDLC_FLAG. This can mean two things:
|
||||||
if ((mp1->checksum_in & 0xff) == 0x00) {
|
if (mp1->packetLength >= MP1_MIN_FRAME_LENGTH) {
|
||||||
mp1Decode(mp1);
|
// We already have more data than the minimum
|
||||||
|
// frame length, which means the flag signifies
|
||||||
|
// the end of the packet. Pass control to the
|
||||||
|
// decoder.
|
||||||
|
if ((mp1->checksum_in & 0xff) == 0x00) {
|
||||||
|
mp1Decode(mp1);
|
||||||
|
} else {
|
||||||
|
// Checksum was incorrect, we don't do anything,
|
||||||
|
// but you can enable the decode anyway, if you
|
||||||
|
// need it for testing or debugging
|
||||||
|
// mp1Decode(mp1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If the above is not the case, this must be the
|
||||||
|
// beginning of a frame
|
||||||
|
mp1->reading = true;
|
||||||
|
mp1->packetLength = 0;
|
||||||
|
mp1->readLength = 0;
|
||||||
|
mp1->checksum_in = MP1_CHECKSUM_INIT;
|
||||||
|
|
||||||
|
// We have indicated that we are reading,
|
||||||
|
// and reset the length counter. Now we'll
|
||||||
|
// continue to the next byte.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!mp1->escape && byte == HDLC_RESET) {
|
||||||
|
// Not good, we got a reset. The transmitting
|
||||||
|
// party may have encountered an error. We'll
|
||||||
|
// stop receiving this packet immediately.
|
||||||
|
mp1->reading = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This should be a parity byte
|
||||||
|
if (mp1->readLength % 3 == 0) {
|
||||||
|
uint8_t calculatedParity = mp1ParityBlock(mp1->buffer[mp1->packetLength-2], mp1->buffer[mp1->packetLength-1]);
|
||||||
|
if (byte == calculatedParity) {
|
||||||
|
// Parity match, block is correct
|
||||||
} else {
|
} else {
|
||||||
// Checksum was incorrect, we don't do anything,
|
// Parity differ, transmission error ocurred
|
||||||
// but you can enable the decode anyway, if you
|
kprintf("Parity mismatch");
|
||||||
// need it for testing or debugging
|
}
|
||||||
// mp1Decode(mp1);
|
mp1->readLength = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!mp1->escape && byte == AX25_ESC) {
|
||||||
|
// We found an escape character. We'll set
|
||||||
|
// the escape seqeunce indicator so we don't
|
||||||
|
// interpret the next byte as a reset or flag
|
||||||
|
mp1->escape = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now let's get to the actual reading of the data
|
||||||
|
if (mp1->reading) {
|
||||||
|
if (mp1->packetLength < MP1_MAX_FRAME_LENGTH) {
|
||||||
|
// If the length of the current incoming frame is
|
||||||
|
// still less than our max length, put the incoming
|
||||||
|
// byte in the buffer.
|
||||||
|
if (!mp1->escape) mp1->checksum_in = mp1->checksum_in ^ byte;
|
||||||
|
mp1->buffer[mp1->packetLength++] = byte;
|
||||||
|
} else {
|
||||||
|
// If not, we have a problem: The buffer has overrun
|
||||||
|
// We need to stop receiving, and the packet will be
|
||||||
|
// dropped :(
|
||||||
|
mp1->reading = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If the above is not the case, this must be the
|
// We need to set the escape sequence indicator back
|
||||||
// beginning of a frame
|
// to false after each byte.
|
||||||
mp1->reading = true;
|
mp1->escape = false;
|
||||||
mp1->packetLength = 0;
|
} else {
|
||||||
mp1->checksum_in = MP1_CHECKSUM_INIT;
|
|
||||||
|
|
||||||
// We have indicated that we are reading,
|
|
||||||
// and reset the length counter. Now we'll
|
|
||||||
// continue to the next byte.
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mp1->escape && byte == HDLC_RESET) {
|
|
||||||
// Not good, we got a reset. The transmitting
|
|
||||||
// party may have encountered an error. We'll
|
|
||||||
// stop receiving this packet immediately.
|
|
||||||
mp1->reading = false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!mp1->escape && byte == AX25_ESC) {
|
|
||||||
// We found an escape character. We'll set
|
|
||||||
// the escape seqeunce indicator so we don't
|
|
||||||
// interpret the next byte as a reset or flag
|
|
||||||
mp1->escape = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now let's get to the actual reading of the data
|
|
||||||
if (mp1->reading) {
|
|
||||||
if (mp1->packetLength < MP1_MAX_FRAME_LENGTH) {
|
|
||||||
// If the length of the current incoming frame is
|
|
||||||
// still less than our max length, put the incoming
|
|
||||||
// byte in the buffer.
|
|
||||||
if (!mp1->escape) mp1->checksum_in = mp1->checksum_in ^ byte;
|
|
||||||
mp1->buffer[mp1->packetLength++] = byte;
|
|
||||||
} else {
|
|
||||||
// If not, we have a problem: The buffer has overrun
|
|
||||||
// We need to stop receiving, and the packet will be
|
|
||||||
// dropped :(
|
|
||||||
mp1->reading = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// We need to set the escape sequence indicator back
|
|
||||||
// to false after each byte.
|
|
||||||
mp1->escape = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kfile_error(mp1->modem)) {
|
if (kfile_error(mp1->modem)) {
|
||||||
|
@ -111,10 +151,21 @@ static void mp1Putbyte(MP1 *mp1, uint8_t byte) {
|
||||||
byte == HDLC_RESET ||
|
byte == HDLC_RESET ||
|
||||||
byte == AX25_ESC) {
|
byte == AX25_ESC) {
|
||||||
kfile_putc(AX25_ESC, mp1->modem);
|
kfile_putc(AX25_ESC, mp1->modem);
|
||||||
|
lastByte = AX25_ESC;
|
||||||
|
sendParityBlock ^= true;
|
||||||
}
|
}
|
||||||
|
|
||||||
kfile_putc(byte, mp1->modem);
|
kfile_putc(byte, mp1->modem);
|
||||||
|
|
||||||
|
if (sendParityBlock) {
|
||||||
|
kfile_putc(mp1ParityBlock(lastByte, byte), mp1->modem);
|
||||||
|
}
|
||||||
|
|
||||||
|
lastByte = byte;
|
||||||
|
sendParityBlock ^= true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void mp1Send(MP1 *mp1, const void *_buffer, size_t length) {
|
void mp1Send(MP1 *mp1, const void *_buffer, size_t length) {
|
||||||
// Get the transmit data buffer
|
// Get the transmit data buffer
|
||||||
const uint8_t *buffer = (const uint8_t *)_buffer;
|
const uint8_t *buffer = (const uint8_t *)_buffer;
|
||||||
|
|
|
@ -26,6 +26,7 @@ typedef struct MP1 {
|
||||||
uint8_t buffer[MP1_MAX_FRAME_LENGTH]; // A buffer for incoming packets
|
uint8_t buffer[MP1_MAX_FRAME_LENGTH]; // A buffer for incoming packets
|
||||||
KFile *modem; // KFile access to the modem
|
KFile *modem; // KFile access to the modem
|
||||||
size_t packetLength; // Counter for received packet length
|
size_t packetLength; // Counter for received packet length
|
||||||
|
size_t readLength; // This is the full read length, including parity bytes
|
||||||
mp1_callback_t callback; // The function to call when a packet has been received
|
mp1_callback_t callback; // The function to call when a packet has been received
|
||||||
uint8_t checksum_in; // Rolling checksum for incoming packets
|
uint8_t checksum_in; // Rolling checksum for incoming packets
|
||||||
uint8_t checksum_out; // Rolling checksum for outgoing packets
|
uint8_t checksum_out; // Rolling checksum for outgoing packets
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
#define VERS_BUILD 577
|
#define VERS_BUILD 602
|
||||||
#define VERS_HOST "vixen"
|
#define VERS_HOST "vixen"
|
||||||
|
|
Loading…
Reference in New Issue