mirror of
https://github.com/markqvist/OpenModem.git
synced 2025-05-19 14:50:27 -04:00
Implemented checksumming
This commit is contained in:
parent
b350d3cd8c
commit
0c4bc75be9
6 changed files with 67 additions and 16 deletions
|
@ -49,7 +49,7 @@
|
||||||
* Baudrate for the debug console.
|
* Baudrate for the debug console.
|
||||||
* $WIZ$ type = "int"; min = 300
|
* $WIZ$ type = "int"; min = 300
|
||||||
*/
|
*/
|
||||||
#define CONFIG_KDEBUG_BAUDRATE 115200UL
|
#define CONFIG_KDEBUG_BAUDRATE 57600UL
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clock source for the UART module. You need to write the code to reprogram the respective clock at the required frequency in your project before calling kdbg_init().
|
* Clock source for the UART module. You need to write the code to reprogram the respective clock at the required frequency in your project before calling kdbg_init().
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
#define CONFIG_AFSK_DAC_SAMPLERATE 9600
|
#define CONFIG_AFSK_DAC_SAMPLERATE 9600
|
||||||
#define CONFIG_AFSK_RXTIMEOUT 0
|
#define CONFIG_AFSK_RXTIMEOUT 0
|
||||||
|
|
||||||
#define CONFIG_AFSK_PREAMBLE_LEN 300UL
|
#define CONFIG_AFSK_PREAMBLE_LEN 500UL
|
||||||
#define CONFIG_AFSK_TRAILER_LEN 50UL
|
#define CONFIG_AFSK_TRAILER_LEN 100UL
|
||||||
|
|
||||||
#endif
|
#endif
|
35
Modem/main.c
35
Modem/main.c
|
@ -18,10 +18,14 @@ static MP1 mp1;
|
||||||
|
|
||||||
#define ADC_CH 0
|
#define ADC_CH 0
|
||||||
|
|
||||||
#define TEST_PACKET "Test MP1 AFSK Packet!"
|
#define TEST_PACKET "Test MP1 AFSK Packet! This one is longer and probably more prone to errors..."
|
||||||
|
|
||||||
|
static uint8_t serialBuffer[MP1_MAX_FRAME_LENGTH];
|
||||||
|
static int sbyte;
|
||||||
|
static uint8_t serialLen = 0;
|
||||||
|
static bool sertx = false;
|
||||||
|
|
||||||
static void mp1Callback(struct MP1Packet *packet) {
|
static void mp1Callback(struct MP1Packet *packet) {
|
||||||
kfile_printf(&ser.fd, "\nMP1 Packet Received:\n");
|
|
||||||
kfile_printf(&ser.fd, "%.*s\r\n", packet->dataLength, packet->data);
|
kfile_printf(&ser.fd, "%.*s\r\n", packet->dataLength, packet->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,29 +33,46 @@ static void init(void)
|
||||||
{
|
{
|
||||||
IRQ_ENABLE;
|
IRQ_ENABLE;
|
||||||
kdbg_init();
|
kdbg_init();
|
||||||
|
kprintf("Init\n");
|
||||||
|
|
||||||
timer_init();
|
timer_init();
|
||||||
|
|
||||||
afsk_init(&afsk, ADC_CH, 0);
|
afsk_init(&afsk, ADC_CH, 0);
|
||||||
mp1Init(&mp1, &afsk.fd, mp1Callback);
|
mp1Init(&mp1, &afsk.fd, mp1Callback);
|
||||||
|
|
||||||
|
|
||||||
ser_init(&ser, SER_UART0);
|
ser_init(&ser, SER_UART0);
|
||||||
ser_setbaudrate(&ser, 115200);
|
ser_setbaudrate(&ser, 57600);
|
||||||
|
//ser_settimeouts(&ser, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
ticks_t start = timer_clock();
|
ticks_t start = timer_clock();
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
mp1Poll(&mp1);
|
mp1Poll(&mp1);
|
||||||
|
|
||||||
|
sbyte = ser_getchar_nowait(&ser);
|
||||||
|
if (sbyte != EOF) {
|
||||||
|
if (serialLen < MP1_MAX_FRAME_LENGTH && sbyte != 138) {
|
||||||
|
serialBuffer[serialLen] = sbyte;
|
||||||
|
serialLen++;
|
||||||
|
} else {
|
||||||
|
sertx = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sertx) {
|
||||||
|
mp1Send(&mp1, serialBuffer, serialLen);
|
||||||
|
sertx = false;
|
||||||
|
serialLen = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Periodically send test data
|
// Periodically send test data
|
||||||
if (timer_clock() - start > ms_to_ticks(4000L))
|
if (false && timer_clock() - start > ms_to_ticks(4000L))
|
||||||
{
|
{
|
||||||
kputs("Test TX\n");
|
|
||||||
start = timer_clock();
|
start = timer_clock();
|
||||||
mp1Send(&mp1, TEST_PACKET, sizeof(TEST_PACKET));
|
mp1Send(&mp1, TEST_PACKET, sizeof(TEST_PACKET));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
#include "mp1.h"
|
#include "mp1.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <drv/ser.h>
|
||||||
//#include <ctype.h>
|
//#include <ctype.h>
|
||||||
|
|
||||||
static void mp1Decode(MP1 *mp1) {
|
static void mp1Decode(MP1 *mp1) {
|
||||||
MP1Packet packet; // A decoded packet struct
|
MP1Packet packet; // A decoded packet struct
|
||||||
uint8_t *buffer = mp1->buffer; // Get the buffer from the protocol context
|
uint8_t *buffer = mp1->buffer; // Get the buffer from the protocol context
|
||||||
|
|
||||||
packet.dataLength = mp1->packetLength;
|
packet.dataLength = mp1->packetLength - 1;
|
||||||
packet.data = buffer;
|
packet.data = buffer;
|
||||||
|
|
||||||
if (mp1->callback) mp1->callback(&packet);
|
if (mp1->callback) mp1->callback(&packet);
|
||||||
|
@ -25,12 +26,24 @@ void mp1Poll(MP1 *mp1) {
|
||||||
// frame length, which means the flag signifies
|
// frame length, which means the flag signifies
|
||||||
// the end of the packet. Pass control to the
|
// the end of the packet. Pass control to the
|
||||||
// decoder.
|
// decoder.
|
||||||
mp1Decode(mp1);
|
kprintf("Got checksum: %d.\n", mp1->buffer[mp1->packetLength-1]);
|
||||||
|
if ((mp1->checksum_in & 0xff) == 0x00) {
|
||||||
|
//kprintf("Correct checksum. Found %d.\n", mp1->buffer[mp1->packetLength-1]);
|
||||||
|
mp1Decode(mp1);
|
||||||
|
} else {
|
||||||
|
// Checksum was incorrect
|
||||||
|
mp1Decode(mp1);
|
||||||
|
//kprintf("Incorrect checksum. Found %d.\n", mp1->buffer[mp1->packetLength]);
|
||||||
|
//kprintf("should be %d", mp1->checksum_in);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// If the above is not the case, this must be the
|
// If the above is not the case, this must be the
|
||||||
// beginning of a frame
|
// beginning of a frame
|
||||||
mp1->reading = true;
|
mp1->reading = true;
|
||||||
mp1->packetLength = 0;
|
mp1->packetLength = 0;
|
||||||
|
mp1->checksum_in = MP1_CHECKSUM_INIT;
|
||||||
|
//kprintf("Checksum init with %d\n", mp1->checksum_in);
|
||||||
|
|
||||||
// We have indicated that we are reading,
|
// We have indicated that we are reading,
|
||||||
// and reset the length counter. Now we'll
|
// and reset the length counter. Now we'll
|
||||||
// continue to the next byte.
|
// continue to the next byte.
|
||||||
|
@ -59,6 +72,8 @@ void mp1Poll(MP1 *mp1) {
|
||||||
// If the length of the current incoming frame is
|
// If the length of the current incoming frame is
|
||||||
// still less than our max length, put the incoming
|
// still less than our max length, put the incoming
|
||||||
// byte in the buffer.
|
// byte in the buffer.
|
||||||
|
if (!mp1->escape) mp1->checksum_in = mp1->checksum_in ^ byte;
|
||||||
|
//kprintf("Checksum is now %d\n", mp1->checksum_in);
|
||||||
mp1->buffer[mp1->packetLength++] = byte;
|
mp1->buffer[mp1->packetLength++] = byte;
|
||||||
} else {
|
} else {
|
||||||
// If not, we have a problem: The buffer has overrun
|
// If not, we have a problem: The buffer has overrun
|
||||||
|
@ -95,13 +110,25 @@ 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;
|
||||||
|
|
||||||
|
// Initialize checksum
|
||||||
|
mp1->checksum_out = MP1_CHECKSUM_INIT;
|
||||||
|
//kprintf("Checksum init with %d\n", mp1->checksum_out);
|
||||||
|
|
||||||
// Transmit the HDLC_FLAG to signify start of TX
|
// Transmit the HDLC_FLAG to signify start of TX
|
||||||
kfile_putc(HDLC_FLAG, mp1->modem);
|
kfile_putc(HDLC_FLAG, mp1->modem);
|
||||||
|
|
||||||
// Continously increment the pointer address
|
// Continously increment the pointer address
|
||||||
// of the buffer while passing it to the byte
|
// of the buffer while passing it to the byte
|
||||||
// output function
|
// output function
|
||||||
while (length--) mp1Putbyte(mp1, *buffer++);
|
while (length--) {
|
||||||
|
mp1->checksum_out = mp1->checksum_out ^ *buffer;
|
||||||
|
//kprintf("Checksum is now %d\n", mp1->checksum_out);
|
||||||
|
mp1Putbyte(mp1, *buffer++);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write checksum to end of packet
|
||||||
|
kprintf("Checksum of this packet is %d\n", mp1->checksum_out);
|
||||||
|
mp1Putbyte(mp1, mp1->checksum_out);
|
||||||
|
|
||||||
// Transmit a HDLC_FLAG to signify end of TX
|
// Transmit a HDLC_FLAG to signify end of TX
|
||||||
kfile_putc(HDLC_FLAG, mp1->modem);
|
kfile_putc(HDLC_FLAG, mp1->modem);
|
||||||
|
|
|
@ -4,9 +4,10 @@
|
||||||
#include <cfg/compiler.h>
|
#include <cfg/compiler.h>
|
||||||
#include <io/kfile.h>
|
#include <io/kfile.h>
|
||||||
|
|
||||||
// Frame sizing
|
// Frame sizing & checksum
|
||||||
#define MP1_MIN_FRAME_LENGTH 1
|
#define MP1_MIN_FRAME_LENGTH 3
|
||||||
#define MP1_MAX_FRAME_LENGTH 300
|
#define MP1_MAX_FRAME_LENGTH 300
|
||||||
|
#define MP1_CHECKSUM_INIT 0xAA
|
||||||
|
|
||||||
// We need to know some basic HDLC flag bytes
|
// We need to know some basic HDLC flag bytes
|
||||||
#define HDLC_FLAG 0x7E
|
#define HDLC_FLAG 0x7E
|
||||||
|
@ -26,6 +27,8 @@ typedef struct MP1 {
|
||||||
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
|
||||||
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_out; // Rolling checksum for outgoing packets
|
||||||
bool reading; // True when we have seen a HDLC flag
|
bool reading; // True when we have seen a HDLC flag
|
||||||
bool escape; // We need to know if we are in an escape sequence
|
bool escape; // We need to know if we are in an escape sequence
|
||||||
} MP1;
|
} MP1;
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
#define VERS_BUILD 148
|
#define VERS_BUILD 308
|
||||||
#define VERS_HOST "vixen"
|
#define VERS_HOST "vixen"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue