Direct serial line in/out. P-persistent CSMA. SLIP compatibility.

This commit is contained in:
Mark Qvist 2014-04-27 18:54:01 +02:00
parent 74542aa87a
commit bd11c5ee83
9 changed files with 180 additions and 52 deletions

View file

@ -1,8 +1,10 @@
#include "mp1.h"
#include "hardware.h"
#include "config.h"
#include <stdlib.h> // Used for random
#include <string.h>
#include <drv/ser.h>
#include <drv/timer.h> // Timer driver from BertOS
#include "compression/heatshrink_encoder.h"
#include "compression/heatshrink_decoder.h"
@ -51,7 +53,7 @@ static uint8_t mp1ParityBlock(uint8_t first, uint8_t other) {
return parity;
}
// This deode function retrieves the buffer of
// This decode function retrieves the buffer of
// received, deinterleaved and error-corrected
// bytes, inspects the header and determines
// whether there is padding to be removed, and
@ -79,7 +81,7 @@ static void mp1Decode(MP1 *mp1) {
// Set the payload length of the packet to the counted
// length minus 1, so we remove the checksum
packet.dataLength = mp1->packetLength - 2 - (header & 0x01)*padding;
packet.dataLength = mp1->packetLength - 2 - (header & MP1_HEADER_PADDED)*padding;
// Check if we have received a compressed packet
if (header & MP1_HEADER_COMPRESSION) {
@ -89,11 +91,20 @@ static void mp1Decode(MP1 *mp1) {
size_t decompressedSize = decompress(buffer, packet.dataLength);
if (SERIAL_DEBUG) kprintf("[DS=%d]", decompressedSize);
packet.dataLength = decompressedSize;
memcpy(buffer, compressionBuffer, decompressedSize);
memcpy(mp1->buffer, compressionBuffer, decompressedSize);
} else {
// If the packet was not compressed, we shift
// the data in our buffer back down to the actual
// beginning of the buffer array, since we incremented
// the pointer address for removing the header and
// padding.
for (unsigned long i = 0; i < packet.dataLength; i++) {
mp1->buffer[i] = buffer[i];
}
}
// Set the data field of the packet to our buffer
packet.data = buffer;
packet.data = mp1->buffer;
// If a callback have been specified, let's
// call it and pass the decoded packet
@ -112,7 +123,9 @@ void mp1Poll(MP1 *mp1) {
// Read bytes from the modem until we reach EOF
while ((byte = kfile_getc(mp1->modem)) != EOF) {
// We have a byte, increment our read counter
// We read something from the modem, so we
// set the settleTimer
mp1->settleTimer = timer_clock();
/////////////////////////////////////////////
// This following block handles forward //
@ -127,6 +140,7 @@ void mp1Poll(MP1 *mp1) {
if ((mp1->reading && (byte != AX25_ESC )) || (mp1->reading && (mp1->escape && (byte == AX25_ESC || byte == HDLC_FLAG || byte == HDLC_RESET)))) {
// We have a byte, increment our read counter
mp1->readLength++;
// Check if we have read three bytes. If we
@ -269,6 +283,10 @@ void mp1Poll(MP1 *mp1) {
// frame length, which means the flag signifies
// the end of the packet. Pass control to the
// decoder.
//
// We also set the settle timer to indicate
// the time the frame completed reading.
mp1->settleTimer = timer_clock();
if ((mp1->checksum_in & 0xff) == 0x00) {
if (SERIAL_DEBUG) kprintf("[CHK-OK] [C=%d] ", mp1->correctionsMade);
mp1Decode(mp1);
@ -322,7 +340,6 @@ void mp1Poll(MP1 *mp1) {
// byte in the buffer. When we have collected 3
// bytes, they will be processed by the error
// correction part above.
mp1->buffer[mp1->packetLength++] = byte;
} else {
// If not, we have a problem: The buffer has overrun
@ -381,6 +398,13 @@ static void mp1Putbyte(MP1 *mp1, uint8_t byte) {
// to be transmitted, and structures it into
// a valid packet.
void mp1Send(MP1 *mp1, void *_buffer, size_t length) {
// Open transmitter and wait for MP1_TXDELAY msecs
AFSK_HW_PTT_ON();
ticks_t start = timer_clock();
while (timer_clock() - start < ms_to_ticks(MP1_TXDELAY)) {
cpu_relax();
}
// Get the transmit data buffer
uint8_t *buffer = (uint8_t *)_buffer;
@ -493,6 +517,9 @@ void mp1Send(MP1 *mp1, void *_buffer, size_t length) {
// And transmit a HDLC_FLAG to signify
// end of the transmission.
kfile_putc(HDLC_FLAG, mp1->modem);
// Turn off manual PTT
AFSK_HW_PTT_OFF();
}
// This function will simply initialize
@ -505,6 +532,34 @@ void mp1Init(MP1 *mp1, KFile *modem, mp1_callback_t callback) {
// a callback for when a packet has been decoded
mp1->modem = modem;
mp1->callback = callback;
mp1->settleTimer = timer_clock();
mp1->randomSeed = 0;
}
// A simple form of P-persistent CSMA.
// Everytime we have heard activity
// on the channel, we wait at least
// MP1_SETTLE_TIME milliseconds after the
// activity has ceased. We then pick a random
// number, and if it is less than
// MP1_P_PERSISTENCE, we transmit.
bool mp1CarrierSense(MP1 *mp1) {
if (mp1->randomSeed == 0) {
mp1->randomSeed = timer_clock();
srand(mp1->randomSeed);
}
if (timer_clock() - mp1->settleTimer > ms_to_ticks(MP1_SETTLE_TIME)) {
uint8_t r = rand() % 255;
if (r < MP1_P_PERSISTENCE) {
return false;
} else {
mp1->settleTimer = timer_clock();
return true;
}
} else {
return true;
}
}
// A handy debug function that can determine

View file

@ -6,11 +6,20 @@
// Frame sizing & checksum
#define MP1_INTERLEAVE_SIZE 12
#define MP1_MAX_FRAME_LENGTH 22 * MP1_INTERLEAVE_SIZE
#define MP1_HEADER_SIZE 1
#define MP1_CHECKSUM_SIZE 1
#define MP1_MAX_DATA_SIZE MP1_MAX_FRAME_LENGTH - MP1_HEADER_SIZE - MP1_CHECKSUM_SIZE
#define MP1_MIN_FRAME_LENGTH MP1_INTERLEAVE_SIZE
#define MP1_DATA_BLOCK_SIZE ((MP1_INTERLEAVE_SIZE/3)*2)
#define MP1_MAX_FRAME_LENGTH 250
#define MP1_CHECKSUM_INIT 0xAA
// These two parameters are used for
// P-persistent CSMA
#define MP1_SETTLE_TIME 100UL // The minimum wait time before considering sending
#define MP1_P_PERSISTENCE 85UL // The probability (between 0 and 255) for sending
#define MP1_TXDELAY 150UL // Delay between turning on the transmitter and sending
// We need to know some basic HDLC flag bytes
#define HDLC_FLAG 0x7E
#define HDLC_RESET 0x7F
@ -44,10 +53,12 @@ typedef struct MP1 {
uint8_t checksum_out; // Rolling checksum for outgoing packets
bool reading; // True when we have seen a HDLC flag
bool escape; // We need to know if we are in an escape sequence
ticks_t settleTimer; // Timer used for carrier sense settling
long correctionsMade; // A counter for how many corrections were made to a packet
uint8_t interleaveCounter; // Keeps track of when we have received an entire interleaved block
uint8_t interleaveOut[MP1_INTERLEAVE_SIZE]; // A buffer for interleaving bytes before they are sent
uint8_t interleaveIn[MP1_INTERLEAVE_SIZE]; // A buffer for storing interleaved bytes before they are deinterleaved
uint8_t randomSeed; // A seed for the pseudo-random number generator
} MP1;
// A struct encapsulating a network packet
@ -61,6 +72,7 @@ void mp1Init(MP1 *mp1, KFile *modem, mp1_callback_t callback);
void mp1Read(MP1 *mp1, int byte);
void mp1Poll(MP1 *mp1);
void mp1Send(MP1 *mp1, void *_buffer, size_t length);
bool mp1CarrierSense(MP1 *mp1);
int freeRam(void);
size_t compress(uint8_t *input, size_t length);