58,8 → 58,6 |
#include "config.h" |
#include "uart1.h" |
#include "mkprotocol.h" |
#include "gpspos.h" |
#include "main.h" |
|
// the tx buffer |
#define UART2_TX_BUFFER_LEN 150 |
66,14 → 64,6 |
u8 UART2_tbuffer[UART2_TX_BUFFER_LEN]; |
Buffer_t UART2_tx_buffer; |
|
unsigned char NMEA_In_Lat_Txt[16]; |
unsigned char NMEA_In_Lon_Txt[16]; |
unsigned char NMEA_In_Alt_Txt[10]; |
unsigned char NMEA_In_Fix_Txt[10]; |
u16 NMEA_cnt,NMEA_cnt_gga; |
|
GPS_Pos_t NMEA_Position = {0,0,0, INVALID}; |
|
/********************************************************/ |
/* Initialize UART2 */ |
/********************************************************/ |
179,320 → 169,13 |
UART1_PutString("ok"); |
} |
|
|
u8 CheckAscii(u8 c) |
{ |
if((c >= '0' && c <= '9') || c == '.') return(1); |
return(0); |
} |
|
u8 CheckNmea(void) |
{ |
u8 i; |
if(NMEA_In_Fix_Txt[0] <= '0') return(0); |
if(NMEA_In_Lat_Txt[0] == 0) return(0); |
if(NMEA_In_Lon_Txt[0] == 0) return(0); |
if(NMEA_In_Lat_Txt[3] != '.') return(0); |
if(NMEA_In_Lon_Txt[4] != '.') return(0); |
for(i = 1; i < 13; i++) |
{ |
if(!CheckAscii(NMEA_In_Lat_Txt[i])) return(0); |
if(!CheckAscii(NMEA_In_Lon_Txt[i])) return(0); |
} |
return(1); |
} |
|
u8 AsciiToNum(u8 c) |
{ |
if(c == '.') return(0); |
return(c - '0'); |
} |
|
void ConvertNmeaToPos(GPS_Pos_t *pos) |
{ |
s32 tmp,tmp2,i; |
if(!CheckNmea()) |
{ |
if(NMEA_Position.Status != INVALID) ErrorNMEAFixLost = 1; |
pos->Status = INVALID; |
return; |
} |
|
tmp = AsciiToNum(NMEA_In_Lat_Txt[1]) * 100000000L; |
tmp += AsciiToNum(NMEA_In_Lat_Txt[2]) * 10000000L; |
// '.' |
tmp2 = AsciiToNum(NMEA_In_Lat_Txt[4]) * 10000000L; |
tmp2 += AsciiToNum(NMEA_In_Lat_Txt[5]) * 1000000L; |
tmp2 += AsciiToNum(NMEA_In_Lat_Txt[6]) * 100000L; |
tmp2 += AsciiToNum(NMEA_In_Lat_Txt[7]) * 10000L; |
tmp2 += AsciiToNum(NMEA_In_Lat_Txt[8]) * 1000L; |
tmp2 += AsciiToNum(NMEA_In_Lat_Txt[9]) * 100L; |
tmp2 += AsciiToNum(NMEA_In_Lat_Txt[10]) * 10L; |
tmp2 += AsciiToNum(NMEA_In_Lat_Txt[11]) * 1L; |
|
tmp = tmp + tmp2/6; // to convert from Minutes to Decimal degree |
if(NMEA_In_Lat_Txt[0] == '-') pos->Latitude = -tmp; else pos->Latitude = tmp; |
|
tmp = AsciiToNum(NMEA_In_Lon_Txt[1]) * 1000000000L; |
tmp += AsciiToNum(NMEA_In_Lon_Txt[2]) * 100000000L; |
tmp += AsciiToNum(NMEA_In_Lon_Txt[3]) * 10000000L; |
// '.' |
tmp2 = AsciiToNum(NMEA_In_Lon_Txt[5]) * 10000000L; |
tmp2 += AsciiToNum(NMEA_In_Lon_Txt[6]) * 1000000L; |
tmp2 += AsciiToNum(NMEA_In_Lon_Txt[7]) * 100000L; |
tmp2 += AsciiToNum(NMEA_In_Lon_Txt[8]) * 10000L; |
tmp2 += AsciiToNum(NMEA_In_Lon_Txt[9]) * 1000L; |
tmp2 += AsciiToNum(NMEA_In_Lon_Txt[10]) * 100L; |
tmp2 += AsciiToNum(NMEA_In_Lon_Txt[11]) * 10L; |
tmp2 += AsciiToNum(NMEA_In_Lon_Txt[12]) * 1L; |
|
tmp = tmp + tmp2/6; // to convert from Minutes to Decimal degree |
if(NMEA_In_Lon_Txt[0] == '-') pos->Longitude = -tmp; else pos->Longitude = tmp; |
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
// + Alitutude |
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
i = 0; |
tmp2 = 1000; // Millimeter |
tmp = 0; |
if(!CheckAscii(NMEA_In_Alt_Txt[0])) i++; // '-' |
while(CheckAscii(NMEA_In_Alt_Txt[i]) && NMEA_In_Alt_Txt[i] != '.') |
{ |
tmp += tmp2 * AsciiToNum(NMEA_In_Alt_Txt[i++]); |
tmp2 *= 10; |
} |
if(NMEA_In_Alt_Txt[i++] == '.') // Zentimeter |
{ |
if(CheckAscii(NMEA_In_Alt_Txt[i])) |
{ |
tmp +=100 * AsciiToNum(NMEA_In_Alt_Txt[i++]); |
if(CheckAscii(NMEA_In_Alt_Txt[i])) |
{ |
tmp += 10 * AsciiToNum(NMEA_In_Alt_Txt[i++]); |
if(CheckAscii(NMEA_In_Alt_Txt[i])) tmp += 1 * AsciiToNum(NMEA_In_Alt_Txt[i++]); |
} |
} |
} |
if(NMEA_In_Alt_Txt[0] == '-') tmp = -tmp; |
pos->Altitude = tmp; |
pos->Status = NEWDATA; |
} |
|
|
void NEMA_Parser(unsigned char data) |
{ |
static u16 state = 0, fx,la,lo,i,a; |
if(data == '$') state = 0; |
static unsigned char lat_txt[16], lon_txt[16],fix_txt[10],alt_txt[10]; |
|
//$GPGGA,HHMMSS.ss,BBBB.BBBB,b,LLLLL.LLLL,l,Q,NN,D.D,H.H,h,G.G,g,A.A,RRRR*PP |
//$GPGGA,191410,4735.5634,N,00739.3538,E,1,04,4.4,351.5,M,48.0,M,,*45 |
//$GPRMC,162614.123,A,5230.5900,N,01322.3900,E,10.0,90.0,131006,1.2,E,A*13 |
//$GPRMC,HHMMSS.sss,A,BBBB.BBBB,b,LLLLL.LLLL,l,GG.G,RR.R,DDMMYY,M.M,m,F*PP |
//http://www.kowoma.de/gps/zusatzerklaerungen/NMEA.htm |
switch(state) |
{ |
case 0: if(data == '$') state++;// $ |
fx = 0; |
la = 1; |
lo = 1; |
fix_txt[0] = 0; |
lat_txt[0] = 0;lon_txt[0] = 0;alt_txt[0] = 0; |
break; |
case 1: if(data == 'G') state++;// $G |
break; |
case 2: if(data == 'P') state++;// $GP |
NMEA_cnt = 3; |
break; |
//+++++++++++++++++++++++++++++++++++++++++++++++++++ |
case 3: if(data == 'R') state++;// $GPR |
if(data == 'G') state = 6;// $GPG |
break; |
case 4: if(data == 'M') state++;// $GPRM |
break; |
case 5: if(data == 'C') state = 100;// $GPRMC |
break; |
//+++++++++++++++++++++++++++++++++++++++++++++++++++ |
case 6: if(data == 'G') state++;// $GPGG |
break; |
case 7: if(data == 'A') state = 200;// $GPGGA |
break; |
//+++++++++++++++++++++++++++++++++++++++++++++++++++ |
// $GPRMC (RMC = recommended minimum sentence C, empfohlener Minimumdatensatz) |
//+++++++++++++++++++++++++++++++++++++++++++++++++++ |
/* |
case 100: if(data == ',') state++; // $GPRMC, |
NMEA_cnt_rmc %= 1000; |
NMEA_cnt_rmc++; |
break; |
case 101: if(data == ',') state = 110; // $GPRMC,162614.123, // time scipped |
break; |
case 110: if(data == ',') state++; |
else fix_txt[0] = data; // $GPRMC,162614.123,A A = Active V = Void |
break; |
case 111: if(data == ',') state++; // / $GPRMC,162614.123,A, |
break; |
case 112: if(data == ',') state++; // $GPRMC,162614.123,A,5317.123456789 Latitude |
else |
{ |
if(la < 16) lat_txt[la++] = data; |
lat_txt[la] = 0; |
} |
break; |
case 113: if(data == ',') state++; //$GPRMC,162614.123,A,5230.5900,N, |
if(data == 'N') lat_txt[0] = '+'; |
if(data == 'S') lat_txt[0] = '-'; |
break; |
case 114: if(data == ',') state++; //$GPRMC,162614.123,A,5230.5900,N,00722.123456789 |
else |
{ |
if(lo < 16) lon_txt[lo++] = data; |
lon_txt[lo] = 0; |
} |
break; |
case 115: if(data == ',') state++; //$GPRMC,162614.123,A,5230.5900,N,01322.3900,E, |
if(data == 'O') lon_txt[0] = '+'; |
if(data == 'E') lon_txt[0] = '+'; |
if(data == 'W') lon_txt[0] = '-'; |
break; |
case 116: if(data == ',') state++; //$GPRMC,162614.123,A,5230.5900,N,01322.3900,E,10.0, |
break; |
case 117: if(data == ',') state++; //$GPRMC,162614.123,A,5230.5900,N,01322.3900,E,10.0,90.0, |
break; |
case 118: if(data == ',') state++; //$GPRMC,162614.123,A,5230.5900,N,01322.3900,E,10.0,90.0,131006, |
break; |
case 119: |
if(data == ',') |
{ |
state++; //$GPRMC,162614.123,A,5230.5900,N,01322.3900,E,10.0,90.0,131006,1.2, |
NMEA_In_Lat_Txt[0] = lat_txt[0]; |
NMEA_In_Lat_Txt[1] = lat_txt[1]; |
NMEA_In_Lat_Txt[2] = lat_txt[2]; |
NMEA_In_Lat_Txt[3] = '.'; |
NMEA_In_Lat_Txt[4] = lat_txt[3]; |
NMEA_In_Lat_Txt[5] = lat_txt[4]; |
for(i=5; i<16; i++) |
{ |
if(lat_txt[i] == '.') i++; |
NMEA_In_Lat_Txt[i] = lat_txt[i]; |
} |
NMEA_In_Lon_Txt[0] = lon_txt[0]; |
NMEA_In_Lon_Txt[1] = lon_txt[1]; |
NMEA_In_Lon_Txt[2] = lon_txt[2]; |
NMEA_In_Lon_Txt[3] = lon_txt[3]; |
NMEA_In_Lon_Txt[4] = '.'; |
NMEA_In_Lon_Txt[5] = lon_txt[4]; |
NMEA_In_Lon_Txt[6] = lon_txt[5]; |
for(i=5; i<16; i++) |
{ |
if(lon_txt[i] == '.') i++; |
NMEA_In_Lon_Txt[i] = lon_txt[i]; |
} |
} |
break; |
case 120: if(data == ',') state++; //$GPRMC,162614.123,A,5230.5900,N,01322.3900,E,10.0,90.0,131006,1.2,E, |
break; |
case 121: state = 0; //$GPRMC,162614.123,A,5230.5900,N,01322.3900,E,10.0,90.0,131006,1.2,E,A*13 |
fix_txt[1] = data; |
fix_txt[2] = 0; |
NMEA_In_Fix_Txt[0] = fix_txt[0]; |
NMEA_In_Fix_Txt[1] = fix_txt[1]; |
break; |
*/ |
//+++++++++++++++++++++++++++++++++++++++++++++++++++ |
// $GPGGA |
//+++++++++++++++++++++++++++++++++++++++++++++++++++ |
case 200: if(data == ',') state++; //$GPGGA, |
NMEA_cnt_gga %= 1000; |
NMEA_cnt_gga++; |
break; |
case 201: if(data == ',') state = 212; //$GPGGA,191410 // time scipped |
break; |
case 212: if(data == ',') state++; //$GPGGA,191410,4735.5634 Latitude |
else |
{ |
if(la < 16) lat_txt[la++] = data; |
lat_txt[la] = 0; |
} |
break; |
case 213: if(data == ',') state++; //$GPGGA,191410,4735.5634,N |
if(data == 'N') lat_txt[0] = '+'; |
if(data == 'S') lat_txt[0] = '-'; |
break; |
case 214: if(data == ',') state++; //$GPGGA,191410,4735.5634,N,00739.3538 |
else |
{ |
if(lo < 16) lon_txt[lo++] = data; |
lon_txt[lo] = 0; |
} |
break; |
case 215: if(data == ',') state++; //$GPGGA,191410,4735.5634,N,00739.3538,E |
if(data == 'O') lon_txt[0] = '+'; |
if(data == 'E') lon_txt[0] = '+'; |
if(data == 'W') lon_txt[0] = '-'; |
i = 0; a = 0; |
break; |
case 216: //$GPGGA,191410,4735.5634,N,00739.3538,E,1 -> 0 = No Fix, 1, = Fix, 2 = DGPS, ... |
case 217: //$GPGGA,191410,4735.5634,N,00739.3538,E,1,04 -> Anzahl Sat |
case 218: //$GPGGA,191410,4735.5634,N,00739.3538,E,1,04,4.4 HDOP |
if(data == ',') |
{ |
if(++state != 219) // letztes Leerzeiten entfernen |
fix_txt[i++] = ' '; // komma durch leerzeichen ersetzten |
} |
else if(i < 9) fix_txt[i++] = data; |
fix_txt[i] = 0; |
break; |
case 219: if(data == ',') state++; //$GPGGA,191410,4735.5634,N,00739.3538,E,1,04,4.4,351.5 |
else if(a < 9) alt_txt[a++] = data; |
fix_txt[i] = 0; |
break; |
case 220: |
if(data == ',') |
{ |
state++; //$GPGGA,191410,4735.5634,N,00739.3538,E,1,04,4.4,351.5,M |
NMEA_In_Lat_Txt[0] = lat_txt[0]; |
NMEA_In_Lat_Txt[1] = lat_txt[1]; |
NMEA_In_Lat_Txt[2] = lat_txt[2]; |
NMEA_In_Lat_Txt[3] = '.'; |
NMEA_In_Lat_Txt[4] = lat_txt[3]; |
NMEA_In_Lat_Txt[5] = lat_txt[4]; |
for(i=5; i<16; i++) |
{ |
if(lat_txt[i] == '.') i++; |
NMEA_In_Lat_Txt[i] = lat_txt[i]; |
} |
NMEA_In_Lon_Txt[0] = lon_txt[0]; |
NMEA_In_Lon_Txt[1] = lon_txt[1]; |
NMEA_In_Lon_Txt[2] = lon_txt[2]; |
NMEA_In_Lon_Txt[3] = lon_txt[3]; |
NMEA_In_Lon_Txt[4] = '.'; |
NMEA_In_Lon_Txt[5] = lon_txt[4]; |
NMEA_In_Lon_Txt[6] = lon_txt[5]; |
for(i=6; i<16; i++) |
{ |
if(lon_txt[i] == '.') i++; |
NMEA_In_Lon_Txt[i] = lon_txt[i]; |
} |
} |
if(NMEA_In_Fix_Txt[i] > '0' && fix_txt[i] == '0') ErrorNMEAFixLost = 1; |
for(i=0; i<10; i++) |
{ |
NMEA_In_Alt_Txt[i] = alt_txt[i]; |
NMEA_In_Fix_Txt[i] = fix_txt[i]; |
} |
if(NMEA_In_Fix_Txt[0] > '0') ErrorNMEAFixLost = 0; |
ConvertNmeaToPos(&NMEA_Position); |
break; |
default: state = 0; |
} |
} |
|
|
/********************************************************/ |
/* UART2 Interrupt Handler */ |
/********************************************************/ |
void UART2_IRQHandler(void) |
{ |
|
|
// if receive irq or receive timeout irq has occured |
if((UART_GetITStatus(UART2, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART2, UART_IT_ReceiveTimeOut) != RESET) ) |
{ |
499,8 → 182,8 |
UART_ClearITPendingBit(UART2, UART_IT_Receive); // clear receive interrupt flag |
UART_ClearITPendingBit(UART2, UART_IT_ReceiveTimeOut); // clear receive timeout interrupt flag |
|
// if debug UART is UART2 -> old FC connects to FC through the Uart |
if((DebugUART == UART2) && (UART_VersionInfo.HWMajor < 30)) |
// if debug UART is UART2 |
if (DebugUART == UART2) |
{ // forward received data to the UART1 tx buffer |
while(UART_GetFlagStatus(UART2, UART_FLAG_RxFIFOEmpty) != SET) |
{ |
507,7 → 190,7 |
// wait for space in the tx buffer of the UART1 |
while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) == SET) {}; |
// move the byte from the rx buffer of UART2 to the tx buffer of UART1 |
UART_SendData(UART1, UART_ReceiveData(UART2)); |
UART_SendData(UART1, UART_ReceiveData(UART2)); |
} |
} |
else |
515,9 → 198,7 |
// ignore serial data from the FC |
while(UART_GetFlagStatus(UART2, UART_FLAG_RxFIFOEmpty) != SET) |
{ |
unsigned char c; |
c = UART_ReceiveData(UART2); |
if(UART_VersionInfo.HWMajor >= 30) NEMA_Parser(c); |
UART_ReceiveData(UART2); |
} |
} |
} // eof receive irq or receive timeout irq |