00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #include <stdio.h>
00046 #include <stdlib.h>
00047 #include <string.h>
00048 #include <stdarg.h>
00049 #include <vcs_bsp/vcsbsp.h>
00050 #include "gps.h"
00051
00052 gps_t buf_struct;
00053
00054 int init_gps(void) {
00055 buf_struct.db[0].status=EMPTY;
00056 buf_struct.db[1].status=EMPTY;
00057
00058 gps_send("$PGRMC,,,,,,,,,,,,2,,");
00059 gps_send("$PGRMC1,,,,,,,,,,,,,2");
00060 gps_send("$PGRMO,,2");
00061 while (!flush_uart1());
00062 gps_send("$PGRMO,PGRMF,1");
00063 gps_send("$PGRMO,PGRME,1");
00064 gps_send("$PGRMO,PGRMV,1");
00065 return (0);
00066 }
00067
00068 int get_field(char *sentence,char *field,int field_num) {
00069
00070
00071
00072
00073 char *field_ptr=0;
00074 char *field_start=0;
00075 char ret_val=0;
00076 field_ptr=field_start=sentence;
00077 while (*field_ptr) {
00078 if ((*field_ptr==',')||(*field_ptr=='*')) {
00079 if (!field_num) {
00080 while (field_start<field_ptr) {
00081 *field++=*field_start++;
00082 }
00083 *field=0;
00084 ret_val=1;
00085 break;
00086 }
00087 else {
00088 field_start=field_ptr+1;
00089 field_num--;
00090 }
00091 }
00092 field_ptr++;
00093 }
00094 return (ret_val);
00095 }
00096
00097 void get_field_test(void) {
00098 char sentence[]="$foo,,,3,4,5,6,7,8,9,10,11,12*5E";
00099 char field_buf[20]="";
00100 int i;
00101 for (i=0;i<16;i++) {
00102 if (get_field(sentence,field_buf,i)) {
00103 printf("%s\n",field_buf);
00104 }
00105 else {
00106 printf("returned 0\n");
00107 }
00108 flush_uart0();
00109 }
00110 }
00111
00112 int gps_parse (char *sentence) {
00113 static int gps_avail=0;
00114 char buf[20];
00115 const char PGRMF[]="$PGRMF";
00116 const char PGRME[]="$PGRME";
00117 const char PGRMV[]="$PGRMV";
00118 int ret_val=0;
00119
00120 if (get_field(sentence,buf,0)) {
00121 if (!strcmp(buf,PGRMF)) {
00122 if (get_field(sentence,buf,FIX)) {
00123 if ((gps_avail=atoi(buf))) {
00124 if (get_field(sentence,buf,DATE)) {
00125 printf("DATE=%s ",buf);
00126 }
00127 if (get_field(sentence,buf,TIME)) {
00128 printf("TIME=%s ",buf);
00129 }
00130 if (get_field(sentence,buf,LATITUDE)) {
00131 printf("LAT=%s ",buf);
00132 }
00133 if (get_field(sentence,buf,LONGITUDE)) {
00134 printf("LON=%s ",buf);
00135 }
00136 }
00137 else {
00138 printf("NO FIX\n");
00139 }
00140 flush_uart0();
00141 }
00142 }
00143 else if (!strcmp(buf,PGRME)) {
00144 if (gps_avail) {
00145 if (get_field(sentence,buf,HPE)) {
00146 printf("ERR=%s ",buf);
00147 flush_uart0();
00148 }
00149 }
00150 }
00151 else if (!strcmp(buf,PGRMV)) {
00152 if (gps_avail) {
00153 if (get_field(sentence,buf,NVEL)) {
00154 printf("NVEL=%s ",buf);
00155 }
00156 if (get_field(sentence,buf,EVEL)) {
00157 printf("EVEL=%s ",buf);
00158 }
00159 printf("\n");
00160 flush_uart0();
00161 }
00162 }
00163 }
00164 return(ret_val);
00165 }
00166
00167 char *gps_get(void) {
00168 char *ret_val=0;
00169 static int gps_buf=0;
00170 if (buf_struct.db[gps_buf].status==READY) {
00171 ret_val=buf_struct.db[gps_buf].data;
00172 buf_struct.db[gps_buf++].status=EMPTY;
00173 if (gps_buf==GPS_BUF_NUM) {
00174 gps_buf=0;
00175 }
00176 }
00177 return (ret_val);
00178 }
00179
00180 int evaluate_gps(void) {
00181 static char message_buffer[100]="";
00182 static char *buf_ptr = message_buffer;
00183 enum {LOCATE_SENTINEL,CRLF};
00184 static char parser_state=LOCATE_SENTINEL;
00185 static int char_counter=0;
00186 static int current_buffer=0;
00187 char temp;
00188 int ret_val=0;
00189 while ((temp=getchar_gps_nb())) {
00190 switch (parser_state) {
00191 case LOCATE_SENTINEL:
00192 if ((temp=='$')&&(buf_struct.db[current_buffer].status==EMPTY)) {
00193 buf_ptr=buf_struct.db[current_buffer].data;
00194 *buf_ptr++=temp;
00195 parser_state=CRLF;
00196 char_counter=1;
00197 }
00198 break;
00199 case CRLF:
00200 if (temp=='\r') {
00201 *buf_ptr++='\n';
00202 *buf_ptr=0;
00203 buf_struct.db[current_buffer++].status=READY;
00204 if (current_buffer==GPS_BUF_NUM) {
00205 current_buffer=0;
00206 }
00207 parser_state=LOCATE_SENTINEL;
00208 }
00209 else {
00210 if (char_counter++<100) {
00211 *buf_ptr++=temp;
00212 }
00213 else {
00214 parser_state=LOCATE_SENTINEL;
00215 }
00216 }
00217 break;
00218 }
00219 }
00220 return (ret_val);
00221 }
00222
00223 int gps_show_raw(void) {
00224 char temp;
00225 char message_buffer[100]="";
00226 char *buf_ptr = message_buffer;
00227
00228 while ((temp=getchar_gps_nb())) {
00229 *buf_ptr++=temp;
00230 }
00231 *buf_ptr=0;
00232 printf("%s",message_buffer);
00233 flush_uart0();
00234 return (1);
00235 }
00236
00237 int gps_add_checksum(char *sentence) {
00238 unsigned char sum = '\0';
00239 char c, *p = sentence;
00240 int ret_val=0;
00241
00242 if (*p == '$') {
00243 p++;
00244 }
00245 else {
00246 ret_val=1;
00247 }
00248 while ( ((c = *p) != '*') && (c != '\0')) {
00249 sum ^= c;
00250 p++;
00251 }
00252 *p++ = '*';
00253 (void)snprintf(p, 5, "%02X\r\n", (unsigned)sum);
00254 return (ret_val);
00255 }
00256
00257 int gps_send(const char *fmt, ... ) {
00258 static int init=0;
00259 int ret_val=0;
00260 char buf[100];
00261 if (!init) {
00262 uprintf(&putchar_gps,"\r\n");
00263 init=1;
00264 }
00265 va_list ap;
00266 va_start(ap, fmt) ;
00267 (void)vsnprintf(buf, sizeof(buf)-5, fmt, ap);
00268 va_end(ap);
00269 if (fmt[0] == '$') {
00270 strcat(buf, "*");
00271 if (gps_add_checksum(buf)) {
00272 ret_val=1;
00273 }
00274 }
00275 else {
00276 strcat(buf, "\r\n");
00277 }
00278 if (!ret_val) {
00279 uprintf(&putchar_gps,"%s",buf);
00280 flush_uart1();
00281 }
00282 return (ret_val);
00283 }
00284
00285