-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtsip.c
331 lines (283 loc) · 9.08 KB
/
tsip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*! \file tsip.c \brief TSIP (Trimble Standard Interface Protocol) function library. */
//*****************************************************************************
//
// File Name : 'tsip.c'
// Title : TSIP (Trimble Standard Interface Protocol) function library
// Author : Pascal Stang - Copyright (C) 2002-2003
// Created : 2002.08.27
// Revised : 2003.07.17
// Version : 0.1
// Target MCU : Atmel AVR Series
// Editor Tabs : 4
//
// NOTE: This code is currently below version 1.0, and therefore is considered
// to be lacking in some functionality or documentation, or may not be fully
// tested. Nonetheless, you can expect most functions to work.
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************
#ifndef WIN32
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <math.h>
#include <stdlib.h>
#endif
#include "global.h"
#include "buffer.h"
#include "rprintf.h"
#include "uart2.h"
#include "gps.h"
#include "tsip.h"
// Program ROM constants
// Global variables
extern GpsInfoType GpsInfo;
#define BUFFERSIZE 0x40
u08 TsipPacket[BUFFERSIZE];
u08 debug;
// function pointer to single byte output routine
static void (*TsipTxByteFunc)(unsigned char c);
void tsipInit(void (*txbytefunc)(unsigned char c))
{
// set transmit function
// (this function will be used for all SendPacket commands)
TsipTxByteFunc = txbytefunc;
// set debug status
debug = 0;
// compose GPS receiver configuration packet
u08 packet[4];
packet[0] = BV(POS_LLA);
packet[1] = BV(VEL_ENU);
packet[2] = 0;
packet[3] = 0;
// send configuration
tsipSendPacket(TSIPTYPE_SET_IO_OPTIONS, 4, packet);
}
void tsipSendPacket(u08 tsipType, u08 dataLength, u08* data)
{
u08 i;
u08 dataIdx = 0;
// start of packet
TsipPacket[dataIdx++] = DLE;
// packet type
TsipPacket[dataIdx++] = tsipType;
// add packet data
for(i=0; i<dataLength; i++)
{
if(*data == DLE)
{
// do double-DLE escape sequence
TsipPacket[dataIdx++] = *data;
TsipPacket[dataIdx++] = *data++;
}
else
TsipPacket[dataIdx++] = *data++;
}
// end of packet
TsipPacket[dataIdx++] = DLE;
TsipPacket[dataIdx++] = ETX;
for(i=0; i<dataIdx; i++)
TsipTxByteFunc(TsipPacket[i]);
}
u08 tsipProcess(cBuffer* rxBuffer)
{
u08 foundpacket = FALSE;
u08 startFlag = FALSE;
u08 data;
u08 i,j,k;
u08 TsipPacketIdx;
// process the receive buffer
// go through buffer looking for packets
while(rxBuffer->datalength > 1)
{
// look for a potential start of TSIP packet
if(bufferGetAtIndex(rxBuffer,0) == DLE)
{
// make sure the next byte is not DLE or ETX
data = bufferGetAtIndex(rxBuffer,1);
if((data != DLE) && (data != ETX))
{
// found potential start
startFlag = TRUE;
// done looking for start
break;
}
}
else
// not DLE, dump character from buffer
bufferGetFromFront(rxBuffer);
}
// if we detected a start, look for end of packet
if(startFlag)
{
for(i=1; i<(rxBuffer->datalength)-1; i++)
{
// check for potential end of TSIP packet
if((bufferGetAtIndex(rxBuffer,i) == DLE) && (bufferGetAtIndex(rxBuffer,i+1) == ETX))
{
// have a packet end
// dump initial DLE
bufferGetFromFront(rxBuffer);
// copy data to TsipPacket
TsipPacketIdx = 0;
for(j=0; j<(i-1); j++)
{
data = bufferGetFromFront(rxBuffer);
if(data == DLE)
{
if(bufferGetAtIndex(rxBuffer,0) == DLE)
{
// found double-DLE escape sequence, skip one of them
bufferGetFromFront(rxBuffer);
j++;
}
}
TsipPacket[TsipPacketIdx++] = data;
}
// dump ending DLE+ETX
bufferGetFromFront(rxBuffer);
bufferGetFromFront(rxBuffer);
// found a packet
if(debug)
{
rprintf("Rx TSIP packet type: 0x%x len: %d rawlen: %d\r\n",
TsipPacket[0],
TsipPacketIdx,
i);
for(k=0; k<TsipPacketIdx; k++)
{
rprintfu08(TsipPacket[k]);
rprintfChar(' ');
}
//rprintfu08(bufferGetFromFront(rxBuffer)); rprintfChar(' ');
//rprintfu08(bufferGetFromFront(rxBuffer)); rprintfChar(' ');
rprintfCRLF();
}
// done with this processing session
foundpacket = TRUE;
break;
}
}
}
if(foundpacket)
{
// switch on the packet type
switch(TsipPacket[0])
{
case TSIPTYPE_GPSTIME: tsipProcessGPSTIME(TsipPacket); break;
case TSIPTYPE_POSFIX_XYZ_SP: tsipProcessPOSFIX_XYZ_SP(TsipPacket); break;
case TSIPTYPE_VELFIX_XYZ: tsipProcessVELFIX_XYZ(TsipPacket); break;
case TSIPTYPE_POSFIX_LLA_SP: tsipProcessPOSFIX_LLA_SP(TsipPacket); break;
case TSIPTYPE_VELFIX_ENU: tsipProcessVELFIX_ENU(TsipPacket); break;
case TSIPTYPE_RAWDATA: break;
default:
//if(debug) rprintf("Unhandled TSIP packet type: 0x%x\r\n",TsipPacket[0]);
break;
}
}
return foundpacket;
}
void tsipProcessGPSTIME(u08* packet)
{
// NOTE: check endian-ness if porting to processors other than the AVR
GpsInfo.TimeOfWeek.b[3] = packet[1];
GpsInfo.TimeOfWeek.b[2] = packet[2];
GpsInfo.TimeOfWeek.b[1] = packet[3];
GpsInfo.TimeOfWeek.b[0] = packet[4];
GpsInfo.WeekNum = ((u16)packet[5]<<8)|((u16)packet[6]);
GpsInfo.UtcOffset.b[3] = packet[7];
GpsInfo.UtcOffset.b[2] = packet[8];
GpsInfo.UtcOffset.b[1] = packet[9];
GpsInfo.UtcOffset.b[0] = packet[10];
}
void tsipProcessPOSFIX_XYZ_SP(u08* packet)
{
// NOTE: check endian-ness if porting to processors other than the AVR
GpsInfo.PosECEF.x.b[3] = packet[1];
GpsInfo.PosECEF.x.b[2] = packet[2];
GpsInfo.PosECEF.x.b[1] = packet[3];
GpsInfo.PosECEF.x.b[0] = packet[4];
GpsInfo.PosECEF.y.b[3] = packet[5];
GpsInfo.PosECEF.y.b[2] = packet[6];
GpsInfo.PosECEF.y.b[1] = packet[7];
GpsInfo.PosECEF.y.b[0] = packet[8];
GpsInfo.PosECEF.z.b[3] = packet[9];
GpsInfo.PosECEF.z.b[2] = packet[10];
GpsInfo.PosECEF.z.b[1] = packet[11];
GpsInfo.PosECEF.z.b[0] = packet[12];
GpsInfo.PosECEF.TimeOfFix.b[3] = packet[13];
GpsInfo.PosECEF.TimeOfFix.b[2] = packet[14];
GpsInfo.PosECEF.TimeOfFix.b[1] = packet[15];
GpsInfo.PosECEF.TimeOfFix.b[0] = packet[16];
GpsInfo.PosECEF.updates++;
// GpsInfo.TimeOfFix_ECEF.f = *((float*)&packet[13]);
}
void tsipProcessVELFIX_XYZ(u08* packet)
{
}
void tsipProcessPOSFIX_LLA_SP(u08* packet)
{
// NOTE: check endian-ness if porting to processors other than the AVR
GpsInfo.PosLLA.lat.b[3] = packet[1];
GpsInfo.PosLLA.lat.b[2] = packet[2];
GpsInfo.PosLLA.lat.b[1] = packet[3];
GpsInfo.PosLLA.lat.b[0] = packet[4];
GpsInfo.PosLLA.lon.b[3] = packet[5];
GpsInfo.PosLLA.lon.b[2] = packet[6];
GpsInfo.PosLLA.lon.b[1] = packet[7];
GpsInfo.PosLLA.lon.b[0] = packet[8];
GpsInfo.PosLLA.alt.b[3] = packet[9];
GpsInfo.PosLLA.alt.b[2] = packet[10];
GpsInfo.PosLLA.alt.b[1] = packet[11];
GpsInfo.PosLLA.alt.b[0] = packet[12];
GpsInfo.PosLLA.TimeOfFix.b[3] = packet[17];
GpsInfo.PosLLA.TimeOfFix.b[2] = packet[18];
GpsInfo.PosLLA.TimeOfFix.b[1] = packet[18];
GpsInfo.PosLLA.TimeOfFix.b[0] = packet[20];
GpsInfo.PosLLA.updates++;
}
void tsipProcessVELFIX_ENU(u08* packet)
{
// NOTE: check endian-ness if porting to processors other than the AVR
GpsInfo.VelENU.east.b[3] = packet[1];
GpsInfo.VelENU.east.b[2] = packet[2];
GpsInfo.VelENU.east.b[1] = packet[3];
GpsInfo.VelENU.east.b[0] = packet[4];
GpsInfo.VelENU.north.b[3] = packet[5];
GpsInfo.VelENU.north.b[2] = packet[6];
GpsInfo.VelENU.north.b[1] = packet[7];
GpsInfo.VelENU.north.b[0] = packet[8];
GpsInfo.VelENU.up.b[3] = packet[9];
GpsInfo.VelENU.up.b[2] = packet[10];
GpsInfo.VelENU.up.b[1] = packet[11];
GpsInfo.VelENU.up.b[0] = packet[12];
GpsInfo.VelENU.TimeOfFix.b[3] = packet[17];
GpsInfo.VelENU.TimeOfFix.b[2] = packet[18];
GpsInfo.VelENU.TimeOfFix.b[1] = packet[19];
GpsInfo.VelENU.TimeOfFix.b[0] = packet[20];
GpsInfo.VelENU.updates++;
}
void tsipProcessRAWDATA(cBuffer* packet)
{
/*
char oft = 1;
// process the data in TSIPdata
unsigned char SVnum = TSIPdata[oft];
unsigned __int32 SNR32 = (TSIPdata[oft+5] << 24) + (TSIPdata[oft+6] << 16) + (TSIPdata[oft+7] << 8) + (TSIPdata[oft+8]);
unsigned __int32 codephase32 = (TSIPdata[oft+9] << 24) + (TSIPdata[oft+10] << 16) + (TSIPdata[oft+11] << 8) + (TSIPdata[oft+12]);
unsigned __int32 doppler32 = (TSIPdata[oft+13] << 24) + (TSIPdata[oft+14] << 16) + (TSIPdata[oft+15] << 8) + (TSIPdata[oft+16]);
unsigned __int64 meastimeH32 = (TSIPdata[oft+17] << 24) | (TSIPdata[oft+18] << 16) | (TSIPdata[oft+19] << 8) | (TSIPdata[oft+20]);
unsigned __int64 meastimeL32 = (TSIPdata[oft+21] << 24) | (TSIPdata[oft+22] << 16) | (TSIPdata[oft+23] << 8) | (TSIPdata[oft+24]);
unsigned __int64 meastime64 = (meastimeH32 << 32) | (meastimeL32);
float SNR = *((float*) &SNR32);
float codephase = *((float*) &codephase32);
float doppler = *((float*) &doppler32);
double meastime = *((double*) &meastime64);
// output to screen
printf("SV%2d SNR: %5.2f PH: %11.4f DOP: %11.4f TIME: %5.0I64f EPOCH: %7.2I64f\n",SVnum,SNR,codephase,doppler,meastime,meastime/1.5);
//printf("SV%2d SNR: %5.2f PH: %10.4f DOP: %10.4f TIME: %I64x\n",SVnum,SNR,codephase,doppler,meastime64);
// output to file
fprintf( logfile, "%2d %5.2f %11.4f %11.4f %5.0I64f %7.2I64f\n",SVnum,SNR,codephase,doppler,meastime,meastime/1.5);
*/
}