Commit e9a83c21 authored by Martin Dold's avatar Martin Dold

satellady.c: added 8-bit CRC calculation and verification

Tested and successfully received more than 1000 lines.
See updated *.ino file for testing.
parent 3b84551f
......@@ -9,7 +9,7 @@
#include "satellady.h"
#include "hal_uart.h"
#define SAT_BUFFER_LEN 100
#define SAT_BUFFER_LEN 200
/* ===========================================================================*/
/* Enums */
......@@ -45,7 +45,9 @@ s_hal_uart_ctx_t *g_host_ctx;
static char g_recv_buffer[SAT_BUFFER_LEN];
uint8_t g_already_read;
uint8_t g_recvCrc[2];
uint8_t g_recvCrc[3];
uint8_t g_crcByte;
char g_crcCalculated;
uint32_t g_dbgCountLines;
uint32_t g_dbgCountError;
......@@ -68,6 +70,7 @@ static void loc_parseGnrmc (char *str, uint16_t len);
static void loc_parseGngns (char *str, uint16_t len);
static void loc_parsePerdcr (char *str, uint16_t len);
static bool loc_cmd_to_uart(char * command);
static char loc_calcCrc(char *data, uint16_t len);
......@@ -342,6 +345,19 @@ static bool loc_cmd_to_uart(char * command) {
return b_return;
}
static char loc_calcCrc(char *data, uint16_t len)
{
char ret = 0;
uint16_t i = 0U;
for (i = 0; i < len; ++i)
{
ret = (ret ^ data[i]);
}
return ret;
}
/* ===========================================================================*/
/* global functions implementation */
/* ===========================================================================*/
......@@ -361,6 +377,10 @@ bool satellady_init() {
memset(g_recv_buffer, 0U, sizeof(g_recv_buffer));
g_already_read = 0U;
memset(g_recvCrc, 0U, sizeof(g_recvCrc));
/* Set string NULL termination here for processing of strtol() in run(). */
g_recvCrc[2] = '\0';
g_crcByte = 0U;
g_crcCalculated = 0U;
g_furuno_gps = g_furuno_gps_default;
gps_data_avail = false;
......@@ -403,7 +423,6 @@ void satellady_run() {
/* Loop as long as there is data available and no END_OF_LINE detected. */
while( hal_uart_read(g_gf8701_ctx, (uint8_t *)(g_recv_buffer + g_already_read), 1U) )
{
if( g_recv_buffer[g_already_read] == '*')
{
/* END_OF_LINE detected. Overwrite our EOL character by
......@@ -415,11 +434,16 @@ void satellady_run() {
g_already_read++;
break;
}
else if(g_already_read > SAT_BUFFER_LEN)
{
/* We are running out of buffers! */
g_satelady_state = E_SATELADY_STATE_ERROR;
break;
}
else
{
g_already_read++;
}
}
break;
......@@ -435,7 +459,20 @@ void satellady_run() {
if( hal_uart_read(g_gf8701_ctx, &g_recvCrc[1], 1U) )
{
/* Validate CRC value here! */
g_satelady_state = E_SATELADY_STATE_FIND_END_OF_LINE_BYTE1;
char *tmp;
g_crcByte = strtol( (char *)&g_recvCrc[0], &tmp, 16);
g_crcCalculated = loc_calcCrc( g_recv_buffer, g_already_read);
if(g_crcCalculated == (char)g_crcByte)
{
/* CRC valid. Go on and process the line. */
g_satelady_state = E_SATELADY_STATE_FIND_END_OF_LINE_BYTE1;
}
else
{
g_satelady_state = E_SATELADY_STATE_ERROR;
}
}
break;
......
......@@ -5,7 +5,7 @@
/* ================== START OF USER DEFINES ===================== */
/* \brief Defines the number of messages to transmit. */
#define MAX_LOOP_COUNT 10000
#define MAX_LOOP_COUNT 100
/* \brief Defines the number of messages to transmit.
......@@ -24,7 +24,7 @@
\warning IMPORTANT! Keep this value in sync with the
TX intervall!
*/
#define SERIAL_BAUD_RATE 115200
#define SERIAL_BAUD_RATE 38400
/* ================== END OF USER DEFINES ===================== */
......@@ -32,68 +32,20 @@
int i;
int loopCounter;
int furuno_txt[] = {
0x24, 0x47, 0x4e, 0x52, 0x4d, 0x43, 0x2c, 0x30, 0x30, 0x30, 0x33, 0x35,
0x39, 0x2e, 0x30, 0x30, 0x30, 0x2c, 0x56, 0x2c, 0x30, 0x30, 0x30, 0x30,
0x2e, 0x30, 0x30, 0x30, 0x30, 0x2c, 0x4e, 0x2c, 0x30, 0x30, 0x30, 0x30,
0x30, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x2c, 0x45, 0x2c, 0x30, 0x2e, 0x30,
0x30, 0x2c, 0x30, 0x2e, 0x30, 0x30, 0x2c, 0x32, 0x32, 0x30, 0x38, 0x39,
0x39, 0x2c, 0x2c, 0x2c, 0x4e, 0x2c, 0x56, 0x2a, 0x31, 0x35, 0x0a, 0x24,
0x47, 0x4e, 0x47, 0x4e, 0x53, 0x2c, 0x30, 0x30, 0x30, 0x33, 0x35, 0x39,
0x2e, 0x30, 0x30, 0x30, 0x2c, 0x30, 0x30, 0x30, 0x30, 0x2e, 0x30, 0x30,
0x30, 0x30, 0x2c, 0x4e, 0x2c, 0x30, 0x30, 0x30, 0x30, 0x30, 0x2e, 0x30,
0x30, 0x30, 0x30, 0x2c, 0x45, 0x2c, 0x4e, 0x4e, 0x4e, 0x2c, 0x30, 0x30,
0x2c, 0x2c, 0x2d, 0x31, 0x38, 0x2e, 0x30, 0x2c, 0x31, 0x38, 0x2e, 0x30,
0x2c, 0x2c, 0x2c, 0x56, 0x2a, 0x36, 0x30, 0x0a, 0x24, 0x47, 0x4e, 0x47,
0x47, 0x41, 0x2c, 0x30, 0x30, 0x30, 0x33, 0x35, 0x39, 0x2e, 0x30, 0x30,
0x30, 0x2c, 0x30, 0x30, 0x30, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x2c,
0x4e, 0x2c, 0x30, 0x30, 0x30, 0x30, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x30,
0x2c, 0x45, 0x2c, 0x30, 0x2c, 0x30, 0x30, 0x2c, 0x2c, 0x2d, 0x31, 0x38,
0x2e, 0x30, 0x2c, 0x4d, 0x2c, 0x31, 0x38, 0x2e, 0x30, 0x2c, 0x4d, 0x2c,
0x2c, 0x2a, 0x37, 0x46, 0x0a, 0x24, 0x47, 0x4e, 0x47, 0x4c, 0x4c, 0x2c,
0x30, 0x30, 0x30, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x2c, 0x4e, 0x2c,
0x30, 0x30, 0x30, 0x30, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x2c, 0x45,
0x2c, 0x30, 0x30, 0x30, 0x33, 0x35, 0x39, 0x2e, 0x30, 0x30, 0x30, 0x2c,
0x56, 0x2c, 0x4e, 0x2a, 0x35, 0x30, 0x0a, 0x24, 0x47, 0x4e, 0x56, 0x54,
0x47, 0x2c, 0x30, 0x2e, 0x30, 0x30, 0x2c, 0x54, 0x2c, 0x2c, 0x4d, 0x2c,
0x30, 0x2e, 0x30, 0x30, 0x2c, 0x4e, 0x2c, 0x30, 0x2e, 0x30, 0x30, 0x2c,
0x4b, 0x2c, 0x4e, 0x2a, 0x32, 0x43, 0x0a, 0x24, 0x47, 0x4e, 0x47, 0x53,
0x41, 0x2c, 0x41, 0x2c, 0x31, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c,
0x2c, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c, 0x31, 0x2a, 0x31,
0x44, 0x0a, 0x24, 0x47, 0x50, 0x5a, 0x44, 0x41, 0x2c, 0x30, 0x30, 0x30,
0x33, 0x35, 0x39, 0x2e, 0x30, 0x30, 0x30, 0x2c, 0x32, 0x32, 0x2c, 0x30,
0x38, 0x2c, 0x31, 0x39, 0x39, 0x39, 0x2c, 0x2b, 0x30, 0x30, 0x2c, 0x30,
0x30, 0x2a, 0x37, 0x32, 0x0a, 0x24, 0x47, 0x50, 0x47, 0x53, 0x56, 0x2c,
0x31, 0x2c, 0x31, 0x2c, 0x30, 0x30, 0x2c, 0x2c, 0x2c, 0x2c, 0x31, 0x2a,
0x34, 0x38, 0x0a, 0x24, 0x47, 0x4c, 0x47, 0x53, 0x56, 0x2c, 0x31, 0x2c,
0x31, 0x2c, 0x30, 0x30, 0x2c, 0x2c, 0x2c, 0x2c, 0x31, 0x2a, 0x35, 0x34,
0x0a, 0x24, 0x50, 0x45, 0x52, 0x44, 0x43, 0x52, 0x57, 0x2c, 0x54, 0x50,
0x53, 0x31, 0x2c, 0x31, 0x39, 0x39, 0x39, 0x30, 0x38, 0x32, 0x32, 0x30,
0x30, 0x30, 0x33, 0x35, 0x39, 0x2c, 0x30, 0x2c, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x2c, 0x2b,
0x31, 0x37, 0x2c, 0x2b, 0x30, 0x30, 0x2c, 0x30, 0x2c, 0x2b, 0x30, 0x30,
0x30, 0x30, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x2c, 0x2b, 0x34, 0x32, 0x36,
0x36, 0x2a, 0x32, 0x45, 0x0a, 0x24, 0x50, 0x45, 0x52, 0x44, 0x43, 0x52,
0x58, 0x2c, 0x54, 0x50, 0x53, 0x32, 0x2c, 0x30, 0x2c, 0x32, 0x2c, 0x30,
0x2c, 0x35, 0x30, 0x30, 0x2c, 0x2b, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x2c, 0x31, 0x2c, 0x31, 0x2c, 0x39, 0x39, 0x39, 0x39, 0x2c, 0x2b, 0x30,
0x2e, 0x30, 0x30, 0x30, 0x2c, 0x30, 0x30, 0x30, 0x30, 0x2c, 0x30, 0x38,
0x33, 0x38, 0x38, 0x36, 0x30, 0x38, 0x2c, 0x2b, 0x30, 0x33, 0x34, 0x38,
0x34, 0x36, 0x2a, 0x30, 0x39, 0x0a, 0x24, 0x50, 0x45, 0x52, 0x44, 0x43,
0x52, 0x59, 0x2c, 0x54, 0x50, 0x53, 0x33, 0x2c, 0x31, 0x2c, 0x31, 0x30,
0x30, 0x30, 0x2c, 0x30, 0x30, 0x30, 0x2c, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x2c, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x2c, 0x32, 0x2c, 0x32,
0x2c, 0x30, 0x30, 0x2c, 0x30, 0x78, 0x32, 0x30, 0x30, 0x30, 0x32, 0x46,
0x38, 0x36, 0x2c, 0x30, 0x78, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33,
0x30, 0x2a, 0x37, 0x38, 0x0a, 0x24, 0x50, 0x45, 0x52, 0x44, 0x43, 0x52,
0x5a, 0x2c, 0x54, 0x50, 0x53, 0x34, 0x2c, 0x30, 0x2c, 0x31, 0x2c, 0x30,
0x31, 0x2c, 0x30, 0x31, 0x2c, 0x2b, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x2c, 0x2b, 0x30, 0x30, 0x30, 0x30, 0x30, 0x2c, 0x30,
0x30, 0x30, 0x30, 0x2c, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x2c,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x2c, 0x2b, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x2a, 0x31, 0x44, 0x0a
};
char furuno[] =
"$GNRMC,000359.000,V,0000.0000,N,00000.0000,E,0.00,0.00,220899,,,N,V*15\r\n"
"$GNGNS,000359.000,0000.0000,N,00000.0000,E,NNN,00,,-18.0,18.0,,,V*60\r\n"
"$GNGGA,000359.000,0000.0000,N,00000.0000,E,0,00,,-18.0,M,18.0,M,,*7F\r\n"
"$GNGLL,0000.0000,N,00000.0000,E,000359.000,V,N*50\r\n"
"$GNVTG,0.00,T,,M,0.00,N,0.00,K,N*2C\r\n"
"$GNGSA,A,1,,,,,,,,,,,,,,,,1*1D\r\n"
"$GPZDA,000359.000,22,08,1999,+00,00*72\r\n"
"$GPGSV,1,1,00,,,,1*48\r\n"
"$GLGSV,1,1,00,,,,1*54\r\n"
"$PERDCRW,TPS1,19990822000359,0,00000000000000,+17,+00,0,+00000.000,+4266*2E\r\n"
"$PERDCRX,TPS2,0,2,0,500,+000000,1,1,9999,+0.000,0000,08388608,+034846*09\r\n"
"$PERDCRY,TPS3,1,1000,000,000000,000000,2,2,00,0x20002F86,0x00000030*78\r\n"
"$PERDCRZ,TPS4,0,1,01,01,+000000000,+00000,0000,0000000,000000,+000000*1D\r\n";
void setup()
{
......@@ -110,18 +62,15 @@ void setup()
void loop() // run over and over
{
delay(TX_INTERVALL_MS);
for(i=0; i < (sizeof(furuno_txt) / sizeof(int)); i++ )
{
Serial.write( furuno_txt[i] );
}
loopCounter++;
if(loopCounter == MAX_LOOP_COUNT)
if(loopCounter <= MAX_LOOP_COUNT)
{
/* Stop here after we send out all messages. */
while(1);
for(i=0; i < (sizeof(furuno) / sizeof(char)); i++ )
{
Serial.write( furuno[i] );
}
loopCounter++;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment