diff --git a/Modem/cfg/cfg_debug.h b/Modem/cfg/cfg_debug.h index 1063984..cd83989 100644 --- a/Modem/cfg/cfg_debug.h +++ b/Modem/cfg/cfg_debug.h @@ -49,7 +49,7 @@ * Baudrate for the debug console. * $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(). diff --git a/Modem/config.h b/Modem/config.h index 7746c4c..1c58bf1 100644 --- a/Modem/config.h +++ b/Modem/config.h @@ -7,7 +7,7 @@ #define CONFIG_AFSK_DAC_SAMPLERATE 9600 #define CONFIG_AFSK_RXTIMEOUT 0 -#define CONFIG_AFSK_PREAMBLE_LEN 300UL -#define CONFIG_AFSK_TRAILER_LEN 50UL +#define CONFIG_AFSK_PREAMBLE_LEN 500UL +#define CONFIG_AFSK_TRAILER_LEN 100UL #endif \ No newline at end of file diff --git a/Modem/main.c b/Modem/main.c index 39fb162..889bfcd 100644 --- a/Modem/main.c +++ b/Modem/main.c @@ -18,10 +18,14 @@ static MP1 mp1; #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) { - kfile_printf(&ser.fd, "\nMP1 Packet Received:\n"); kfile_printf(&ser.fd, "%.*s\r\n", packet->dataLength, packet->data); } @@ -29,29 +33,46 @@ static void init(void) { IRQ_ENABLE; kdbg_init(); + kprintf("Init\n"); + timer_init(); afsk_init(&afsk, ADC_CH, 0); mp1Init(&mp1, &afsk.fd, mp1Callback); - ser_init(&ser, SER_UART0); - ser_setbaudrate(&ser, 115200); + ser_setbaudrate(&ser, 57600); + //ser_settimeouts(&ser, 0, 0); } int main(void) { init(); ticks_t start = timer_clock(); - + while (1) { 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 - if (timer_clock() - start > ms_to_ticks(4000L)) + if (false && timer_clock() - start > ms_to_ticks(4000L)) { - kputs("Test TX\n"); start = timer_clock(); mp1Send(&mp1, TEST_PACKET, sizeof(TEST_PACKET)); } diff --git a/Modem/protocol/mp1.c b/Modem/protocol/mp1.c index 0cdb3d7..7dc8ee4 100644 --- a/Modem/protocol/mp1.c +++ b/Modem/protocol/mp1.c @@ -1,12 +1,13 @@ #include "mp1.h" #include +#include //#include static void mp1Decode(MP1 *mp1) { MP1Packet packet; // A decoded packet struct uint8_t *buffer = mp1->buffer; // Get the buffer from the protocol context - packet.dataLength = mp1->packetLength; + packet.dataLength = mp1->packetLength - 1; packet.data = buffer; if (mp1->callback) mp1->callback(&packet); @@ -25,12 +26,24 @@ void mp1Poll(MP1 *mp1) { // frame length, which means the flag signifies // the end of the packet. Pass control to the // 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 // beginning of a frame mp1->reading = true; 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, // and reset the length counter. Now we'll // continue to the next byte. @@ -59,6 +72,8 @@ void mp1Poll(MP1 *mp1) { // 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; + //kprintf("Checksum is now %d\n", mp1->checksum_in); mp1->buffer[mp1->packetLength++] = byte; } else { // 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 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 kfile_putc(HDLC_FLAG, mp1->modem); // Continously increment the pointer address // of the buffer while passing it to the byte // 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 kfile_putc(HDLC_FLAG, mp1->modem); diff --git a/Modem/protocol/mp1.h b/Modem/protocol/mp1.h index c439768..289a25a 100644 --- a/Modem/protocol/mp1.h +++ b/Modem/protocol/mp1.h @@ -4,9 +4,10 @@ #include #include -// Frame sizing -#define MP1_MIN_FRAME_LENGTH 1 +// Frame sizing & checksum +#define MP1_MIN_FRAME_LENGTH 3 #define MP1_MAX_FRAME_LENGTH 300 +#define MP1_CHECKSUM_INIT 0xAA // We need to know some basic HDLC flag bytes #define HDLC_FLAG 0x7E @@ -26,6 +27,8 @@ typedef struct MP1 { KFile *modem; // KFile access to the modem size_t packetLength; // Counter for received packet length 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 escape; // We need to know if we are in an escape sequence } MP1; diff --git a/buildrev.h b/buildrev.h index d7fa7e9..df659b7 100644 --- a/buildrev.h +++ b/buildrev.h @@ -1,2 +1,2 @@ -#define VERS_BUILD 148 +#define VERS_BUILD 308 #define VERS_HOST "vixen"