* add C027_Support fork
Fork of C027_Support by
GPS.cpp@95:8282dbbe1492, 2014-06-17 (annotated)
- Committer:
- mazgch
- Date:
- Tue Jun 17 07:03:48 2014 +0000
- Revision:
- 95:8282dbbe1492
- Parent:
- 76:f7c3dd568dae
added use of mutex and wait for Rtos systems
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mazgch | 2:b6012cd91657 | 1 | #include "mbed.h" |
mazgch | 2:b6012cd91657 | 2 | #include <ctype.h> |
mazgch | 2:b6012cd91657 | 3 | #include "GPS.h" |
mazgch | 74:208e3e32d263 | 4 | #ifdef TARGET_UBLOX_C027 |
mazgch | 74:208e3e32d263 | 5 | #include "C027_api.h" |
mazgch | 74:208e3e32d263 | 6 | #endif |
mazgch | 74:208e3e32d263 | 7 | |
mazgch | 74:208e3e32d263 | 8 | void GPSParser::powerOff(void) |
mazgch | 74:208e3e32d263 | 9 | { |
mazgch | 74:208e3e32d263 | 10 | // set the gps into backup mode using the command RMX-LPREQ |
mazgch | 74:208e3e32d263 | 11 | struct { unsigned long dur; unsigned long flags; } msg = {0/*endless*/,0/*backup*/}; |
mazgch | 74:208e3e32d263 | 12 | sendUbx(0x02, 0x41, &msg, sizeof(msg)); |
mazgch | 74:208e3e32d263 | 13 | } |
mazgch | 2:b6012cd91657 | 14 | |
mazgch | 2:b6012cd91657 | 15 | int GPSParser::_getMessage(Pipe<char>* pipe, char* buf, int len) |
mazgch | 2:b6012cd91657 | 16 | { |
mazgch | 2:b6012cd91657 | 17 | int unkn = 0; |
mazgch | 2:b6012cd91657 | 18 | int sz = pipe->size(); |
mazgch | 9:e7a5959ffae1 | 19 | int fr = pipe->free(); |
mazgch | 2:b6012cd91657 | 20 | if (len > sz) |
mazgch | 2:b6012cd91657 | 21 | len = sz; |
mazgch | 2:b6012cd91657 | 22 | while (len > 0) |
mazgch | 2:b6012cd91657 | 23 | { |
mazgch | 2:b6012cd91657 | 24 | // NMEA protocol |
mazgch | 21:c4d64830bf02 | 25 | pipe->set(unkn); |
mazgch | 2:b6012cd91657 | 26 | int nmea = _parseNmea(pipe,len); |
mazgch | 9:e7a5959ffae1 | 27 | if ((nmea != NOT_FOUND) && (unkn > 0)) |
mazgch | 31:a0bed6c1e05d | 28 | return UNKNOWN | pipe->get(buf,unkn); |
mazgch | 9:e7a5959ffae1 | 29 | if (nmea == WAIT && fr) |
mazgch | 9:e7a5959ffae1 | 30 | return WAIT; |
mazgch | 9:e7a5959ffae1 | 31 | if (nmea > 0) |
mazgch | 9:e7a5959ffae1 | 32 | return NMEA | pipe->get(buf,nmea); |
mazgch | 2:b6012cd91657 | 33 | // UBX protocol |
mazgch | 21:c4d64830bf02 | 34 | |
mazgch | 21:c4d64830bf02 | 35 | pipe->set(unkn); |
mazgch | 2:b6012cd91657 | 36 | int ubx = _parseUbx(pipe,len); |
mazgch | 9:e7a5959ffae1 | 37 | if ((ubx != NOT_FOUND) && (unkn > 0)) |
mazgch | 31:a0bed6c1e05d | 38 | return UNKNOWN | pipe->get(buf,unkn); |
mazgch | 9:e7a5959ffae1 | 39 | if (ubx == WAIT && fr) |
mazgch | 9:e7a5959ffae1 | 40 | return WAIT; |
mazgch | 9:e7a5959ffae1 | 41 | if (ubx > 0) |
mazgch | 9:e7a5959ffae1 | 42 | return UBX | pipe->get(buf,ubx); |
mazgch | 21:c4d64830bf02 | 43 | |
mazgch | 2:b6012cd91657 | 44 | // UNKNOWN |
mazgch | 2:b6012cd91657 | 45 | unkn ++; |
mazgch | 2:b6012cd91657 | 46 | len--; |
mazgch | 2:b6012cd91657 | 47 | } |
mazgch | 21:c4d64830bf02 | 48 | if (unkn > 0) |
mazgch | 31:a0bed6c1e05d | 49 | return UNKNOWN | pipe->get(buf,unkn); |
mazgch | 2:b6012cd91657 | 50 | return WAIT; |
mazgch | 2:b6012cd91657 | 51 | } |
mazgch | 2:b6012cd91657 | 52 | |
mazgch | 2:b6012cd91657 | 53 | int GPSParser::_parseNmea(Pipe<char>* pipe, int len) |
mazgch | 2:b6012cd91657 | 54 | { |
mazgch | 7:9aa830f5811e | 55 | int o = 0; |
mazgch | 7:9aa830f5811e | 56 | int c = 0; |
mazgch | 7:9aa830f5811e | 57 | char ch; |
mazgch | 7:9aa830f5811e | 58 | if (++o > len) return WAIT; |
mazgch | 2:b6012cd91657 | 59 | if ('$' != pipe->next()) return NOT_FOUND; |
mazgch | 4:c959dd4c5fe8 | 60 | // this needs to be extended by crc checking |
mazgch | 2:b6012cd91657 | 61 | for (;;) |
mazgch | 2:b6012cd91657 | 62 | { |
mazgch | 7:9aa830f5811e | 63 | if (++o > len) return WAIT; |
mazgch | 7:9aa830f5811e | 64 | ch = pipe->next(); |
mazgch | 7:9aa830f5811e | 65 | if ('*' == ch) break; // crc delimiter |
mazgch | 7:9aa830f5811e | 66 | if (!isprint(ch)) return NOT_FOUND; |
mazgch | 7:9aa830f5811e | 67 | c ^= ch; |
mazgch | 2:b6012cd91657 | 68 | } |
mazgch | 7:9aa830f5811e | 69 | if (++o > len) return WAIT; |
mazgch | 7:9aa830f5811e | 70 | ch = toHex[(c >> 4) & 0xF]; // high nibble |
mazgch | 7:9aa830f5811e | 71 | if (ch != pipe->next()) return NOT_FOUND; |
mazgch | 7:9aa830f5811e | 72 | if (++o > len) return WAIT; |
mazgch | 7:9aa830f5811e | 73 | ch = toHex[(c >> 0) & 0xF]; // low nibble |
mazgch | 7:9aa830f5811e | 74 | if (ch != pipe->next()) return NOT_FOUND; |
mazgch | 7:9aa830f5811e | 75 | if (++o > len) return WAIT; |
mazgch | 7:9aa830f5811e | 76 | if ('\r' != pipe->next()) return NOT_FOUND; |
mazgch | 7:9aa830f5811e | 77 | if (++o > len) return WAIT; |
mazgch | 7:9aa830f5811e | 78 | if ('\n' != pipe->next()) return NOT_FOUND; |
mazgch | 7:9aa830f5811e | 79 | return o; |
mazgch | 2:b6012cd91657 | 80 | } |
mazgch | 2:b6012cd91657 | 81 | |
mazgch | 2:b6012cd91657 | 82 | int GPSParser::_parseUbx(Pipe<char>* pipe, int l) |
mazgch | 2:b6012cd91657 | 83 | { |
mazgch | 2:b6012cd91657 | 84 | int o = 0; |
mazgch | 2:b6012cd91657 | 85 | if (++o > l) return WAIT; |
mazgch | 2:b6012cd91657 | 86 | if ('\xB5' != pipe->next()) return NOT_FOUND; |
mazgch | 2:b6012cd91657 | 87 | if (++o > l) return WAIT; |
mazgch | 2:b6012cd91657 | 88 | if ('\x62' != pipe->next()) return NOT_FOUND; |
mazgch | 2:b6012cd91657 | 89 | o += 4; |
mazgch | 2:b6012cd91657 | 90 | if (o > l) return WAIT; |
mazgch | 2:b6012cd91657 | 91 | int i,j,ca,cb; |
mazgch | 2:b6012cd91657 | 92 | i = pipe->next(); ca = i; cb = ca; // cls |
mazgch | 2:b6012cd91657 | 93 | i = pipe->next(); ca += i; cb += ca; // id |
mazgch | 2:b6012cd91657 | 94 | i = pipe->next(); ca += i; cb += ca; // len_lsb |
mazgch | 2:b6012cd91657 | 95 | j = pipe->next(); ca += j; cb += ca; // len_msb |
mazgch | 2:b6012cd91657 | 96 | j = i + (j << 8); |
mazgch | 2:b6012cd91657 | 97 | while (j--) |
mazgch | 2:b6012cd91657 | 98 | { |
mazgch | 2:b6012cd91657 | 99 | if (++o > l) return WAIT; |
mazgch | 2:b6012cd91657 | 100 | i = pipe->next(); |
mazgch | 2:b6012cd91657 | 101 | ca += i; |
mazgch | 2:b6012cd91657 | 102 | cb += ca; |
mazgch | 2:b6012cd91657 | 103 | } |
mazgch | 2:b6012cd91657 | 104 | ca &= 0xFF; cb &= 0xFF; |
mazgch | 2:b6012cd91657 | 105 | if (++o > l) return WAIT; |
mazgch | 2:b6012cd91657 | 106 | if (ca != pipe->next()) return NOT_FOUND; |
mazgch | 2:b6012cd91657 | 107 | if (++o > l) return WAIT; |
mazgch | 2:b6012cd91657 | 108 | if (cb != pipe->next()) return NOT_FOUND; |
mazgch | 2:b6012cd91657 | 109 | return o; |
mazgch | 2:b6012cd91657 | 110 | } |
mazgch | 2:b6012cd91657 | 111 | |
mazgch | 4:c959dd4c5fe8 | 112 | int GPSParser::send(const char* buf, int len) |
mazgch | 4:c959dd4c5fe8 | 113 | { |
mazgch | 4:c959dd4c5fe8 | 114 | return _send(buf, len); |
mazgch | 4:c959dd4c5fe8 | 115 | } |
mazgch | 4:c959dd4c5fe8 | 116 | |
mazgch | 3:c7cd4887560d | 117 | int GPSParser::sendNmea(const char* buf, int len) |
mazgch | 2:b6012cd91657 | 118 | { |
mazgch | 3:c7cd4887560d | 119 | char head[1] = { '$' }; |
mazgch | 3:c7cd4887560d | 120 | char tail[5] = { '*', 0x00/*crc_high*/, 0x00/*crc_low*/, '\r', '\n' }; |
mazgch | 3:c7cd4887560d | 121 | int i; |
mazgch | 3:c7cd4887560d | 122 | int crc = 0; |
mazgch | 3:c7cd4887560d | 123 | for (i = 0; i < len; i ++) |
mazgch | 3:c7cd4887560d | 124 | crc ^= *buf++; |
mazgch | 4:c959dd4c5fe8 | 125 | i = _send(head, sizeof(head)); |
mazgch | 4:c959dd4c5fe8 | 126 | i += _send(buf, len); |
mazgch | 3:c7cd4887560d | 127 | tail[1] = toHex[(crc > 4) & 0xF0]; |
mazgch | 3:c7cd4887560d | 128 | tail[2] = toHex[(crc > 0) & 0x0F]; |
mazgch | 4:c959dd4c5fe8 | 129 | i += _send(tail, sizeof(tail)); |
mazgch | 3:c7cd4887560d | 130 | return i; |
mazgch | 2:b6012cd91657 | 131 | } |
mazgch | 2:b6012cd91657 | 132 | |
mazgch | 11:b084552b03fe | 133 | int GPSParser::sendUbx(unsigned char cls, unsigned char id, const void* buf /*= NULL*/, int len /*= 0*/) |
mazgch | 2:b6012cd91657 | 134 | { |
mazgch | 3:c7cd4887560d | 135 | char head[6] = { 0xB5, 0x62, cls, id, len >> 0, len >> 8 }; |
mazgch | 3:c7cd4887560d | 136 | char crc[2]; |
mazgch | 3:c7cd4887560d | 137 | int i; |
mazgch | 3:c7cd4887560d | 138 | int ca = 0; |
mazgch | 3:c7cd4887560d | 139 | int cb = 0; |
mazgch | 3:c7cd4887560d | 140 | for (i = 2; i < 6; i ++) |
mazgch | 2:b6012cd91657 | 141 | { |
mazgch | 3:c7cd4887560d | 142 | ca += head[i]; |
mazgch | 3:c7cd4887560d | 143 | cb += ca; |
mazgch | 2:b6012cd91657 | 144 | } |
mazgch | 3:c7cd4887560d | 145 | for (i = 0; i < len; i ++) |
mazgch | 3:c7cd4887560d | 146 | { |
mazgch | 3:c7cd4887560d | 147 | ca += ((char*)buf)[i]; |
mazgch | 11:b084552b03fe | 148 | cb += ca; |
mazgch | 3:c7cd4887560d | 149 | } |
mazgch | 4:c959dd4c5fe8 | 150 | i = _send(head, sizeof(head)); |
mazgch | 4:c959dd4c5fe8 | 151 | i += _send(buf, len); |
mazgch | 3:c7cd4887560d | 152 | crc[0] = ca & 0xFF; |
mazgch | 3:c7cd4887560d | 153 | crc[1] = cb & 0xFF; |
mazgch | 4:c959dd4c5fe8 | 154 | i += _send(crc, sizeof(crc)); |
mazgch | 3:c7cd4887560d | 155 | return i; |
mazgch | 2:b6012cd91657 | 156 | } |
mazgch | 2:b6012cd91657 | 157 | |
mazgch | 2:b6012cd91657 | 158 | const char* GPSParser::findNmeaItemPos(int ix, const char* start, const char* end) |
mazgch | 2:b6012cd91657 | 159 | { |
mazgch | 2:b6012cd91657 | 160 | // find the start |
mazgch | 2:b6012cd91657 | 161 | for (; (start < end) && (ix > 0); start ++) |
mazgch | 2:b6012cd91657 | 162 | { |
mazgch | 2:b6012cd91657 | 163 | if (*start == ',') |
mazgch | 2:b6012cd91657 | 164 | ix --; |
mazgch | 2:b6012cd91657 | 165 | } |
mazgch | 2:b6012cd91657 | 166 | // found and check bounds |
mazgch | 2:b6012cd91657 | 167 | if ((ix == 0) && (start < end) && |
mazgch | 2:b6012cd91657 | 168 | (*start != ',') && (*start != '*') && (*start != '\r') && (*start != '\n')) |
mazgch | 2:b6012cd91657 | 169 | return start; |
mazgch | 2:b6012cd91657 | 170 | else |
mazgch | 2:b6012cd91657 | 171 | return NULL; |
mazgch | 2:b6012cd91657 | 172 | } |
mazgch | 2:b6012cd91657 | 173 | |
mazgch | 2:b6012cd91657 | 174 | bool GPSParser::getNmeaItem(int ix, char* buf, int len, double& val) |
mazgch | 2:b6012cd91657 | 175 | { |
mazgch | 2:b6012cd91657 | 176 | char* end = &buf[len]; |
mazgch | 2:b6012cd91657 | 177 | const char* pos = findNmeaItemPos(ix, buf, end); |
mazgch | 2:b6012cd91657 | 178 | // find the start |
mazgch | 2:b6012cd91657 | 179 | if (!pos) |
mazgch | 2:b6012cd91657 | 180 | return false; |
mazgch | 2:b6012cd91657 | 181 | val = strtod(pos, &end); |
mazgch | 2:b6012cd91657 | 182 | // restore the last character |
mazgch | 2:b6012cd91657 | 183 | return (end > pos); |
mazgch | 2:b6012cd91657 | 184 | } |
mazgch | 2:b6012cd91657 | 185 | |
mazgch | 2:b6012cd91657 | 186 | bool GPSParser::getNmeaItem(int ix, char* buf, int len, int& val, int base /*=10*/) |
mazgch | 2:b6012cd91657 | 187 | { |
mazgch | 2:b6012cd91657 | 188 | char* end = &buf[len]; |
mazgch | 2:b6012cd91657 | 189 | const char* pos = findNmeaItemPos(ix, buf, end); |
mazgch | 2:b6012cd91657 | 190 | // find the start |
mazgch | 2:b6012cd91657 | 191 | if (!pos) |
mazgch | 2:b6012cd91657 | 192 | return false; |
mazgch | 2:b6012cd91657 | 193 | val = (int)strtol(pos, &end, base); |
mazgch | 2:b6012cd91657 | 194 | return (end > pos); |
mazgch | 2:b6012cd91657 | 195 | } |
mazgch | 2:b6012cd91657 | 196 | |
mazgch | 2:b6012cd91657 | 197 | bool GPSParser::getNmeaItem(int ix, char* buf, int len, char& val) |
mazgch | 2:b6012cd91657 | 198 | { |
mazgch | 2:b6012cd91657 | 199 | const char* end = &buf[len]; |
mazgch | 2:b6012cd91657 | 200 | const char* pos = findNmeaItemPos(ix, buf, end); |
mazgch | 2:b6012cd91657 | 201 | // find the start |
mazgch | 2:b6012cd91657 | 202 | if (!pos) |
mazgch | 2:b6012cd91657 | 203 | return false; |
mazgch | 2:b6012cd91657 | 204 | // skip leading spaces |
mazgch | 2:b6012cd91657 | 205 | while ((pos < end) && isspace(*pos)) |
mazgch | 2:b6012cd91657 | 206 | pos++; |
mazgch | 2:b6012cd91657 | 207 | // check bound |
mazgch | 2:b6012cd91657 | 208 | if ((pos < end) && |
mazgch | 2:b6012cd91657 | 209 | (*pos != ',') && (*pos != '*') && (*pos != '\r') && (*pos != '\n')) |
mazgch | 2:b6012cd91657 | 210 | { |
mazgch | 2:b6012cd91657 | 211 | val = *pos; |
mazgch | 2:b6012cd91657 | 212 | return true; |
mazgch | 2:b6012cd91657 | 213 | } |
mazgch | 2:b6012cd91657 | 214 | return false; |
mazgch | 2:b6012cd91657 | 215 | } |
mazgch | 2:b6012cd91657 | 216 | |
mazgch | 20:535ef78655df | 217 | bool GPSParser::getNmeaAngle(int ix, char* buf, int len, double& val) |
mazgch | 18:e5697801df29 | 218 | { |
mazgch | 18:e5697801df29 | 219 | char ch; |
mazgch | 18:e5697801df29 | 220 | if (getNmeaItem(ix,buf,len,val) && getNmeaItem(ix+1,buf,len,ch) && |
mazgch | 18:e5697801df29 | 221 | ((ch == 'S') || (ch == 'N') || (ch == 'E') || (ch == 'W'))) |
mazgch | 18:e5697801df29 | 222 | { |
mazgch | 18:e5697801df29 | 223 | val *= 0.01; |
mazgch | 20:535ef78655df | 224 | int i = (int)val; |
mazgch | 18:e5697801df29 | 225 | val = (val - i) / 0.6 + i; |
mazgch | 18:e5697801df29 | 226 | if (ch == 'S' || ch == 'W') |
mazgch | 18:e5697801df29 | 227 | val = -val; |
mazgch | 18:e5697801df29 | 228 | return true; |
mazgch | 18:e5697801df29 | 229 | } |
mazgch | 18:e5697801df29 | 230 | return false; |
mazgch | 18:e5697801df29 | 231 | } |
mazgch | 18:e5697801df29 | 232 | |
mazgch | 2:b6012cd91657 | 233 | const char GPSParser::toHex[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; |
mazgch | 2:b6012cd91657 | 234 | |
mazgch | 2:b6012cd91657 | 235 | // ---------------------------------------------------------------- |
mazgch | 2:b6012cd91657 | 236 | // Serial Implementation |
mazgch | 2:b6012cd91657 | 237 | // ---------------------------------------------------------------- |
mazgch | 2:b6012cd91657 | 238 | |
mazgch | 9:e7a5959ffae1 | 239 | GPSSerial::GPSSerial(PinName tx /*= GPSTXD*/, PinName rx /*= GPSRXD*/, int baudrate /*= GPSBAUD*/, |
mazgch | 9:e7a5959ffae1 | 240 | int rxSize /*= 256*/, int txSize /*= 128*/) : |
mazgch | 15:5eda64e5b9d1 | 241 | SerialPipe(tx, rx, rxSize, txSize) |
mazgch | 2:b6012cd91657 | 242 | { |
mazgch | 2:b6012cd91657 | 243 | baud(baudrate); |
mazgch | 74:208e3e32d263 | 244 | #ifdef TARGET_UBLOX_C027 |
mazgch | 74:208e3e32d263 | 245 | _onboard = (tx == GPSTXD) || (rx == GPSRXD); |
mazgch | 74:208e3e32d263 | 246 | if (_onboard) |
mazgch | 74:208e3e32d263 | 247 | c027_gps_powerOn(); |
mazgch | 74:208e3e32d263 | 248 | #endif |
mazgch | 74:208e3e32d263 | 249 | } |
mazgch | 74:208e3e32d263 | 250 | |
mazgch | 76:f7c3dd568dae | 251 | GPSSerial::~GPSSerial(void) |
mazgch | 76:f7c3dd568dae | 252 | { |
mazgch | 76:f7c3dd568dae | 253 | powerOff(); |
mazgch | 76:f7c3dd568dae | 254 | #ifdef TARGET_UBLOX_C027 |
mazgch | 76:f7c3dd568dae | 255 | if (_onboard) |
mazgch | 76:f7c3dd568dae | 256 | c027_gps_powerOff(); |
mazgch | 76:f7c3dd568dae | 257 | #endif |
mazgch | 76:f7c3dd568dae | 258 | } |
mazgch | 76:f7c3dd568dae | 259 | |
mazgch | 75:ce6e12067d0c | 260 | bool GPSSerial::init(PinName pn) |
mazgch | 74:208e3e32d263 | 261 | { |
mazgch | 74:208e3e32d263 | 262 | // send a byte to wakup the device again |
mazgch | 95:8282dbbe1492 | 263 | putc(0xFF); |
mazgch | 74:208e3e32d263 | 264 | // wait until we get some bytes |
mazgch | 74:208e3e32d263 | 265 | int size = _pipeRx.size(); |
mazgch | 95:8282dbbe1492 | 266 | Timer timer; |
mazgch | 95:8282dbbe1492 | 267 | timer.start(); |
mazgch | 95:8282dbbe1492 | 268 | while ((100 > timer.read_ms()) && (size == _pipeRx.size())) |
mazgch | 95:8282dbbe1492 | 269 | /* nothing / just wait */; |
mazgch | 95:8282dbbe1492 | 270 | return (size != _pipeRx.size()); |
mazgch | 2:b6012cd91657 | 271 | } |
mazgch | 2:b6012cd91657 | 272 | |
mazgch | 2:b6012cd91657 | 273 | int GPSSerial::getMessage(char* buf, int len) |
mazgch | 2:b6012cd91657 | 274 | { |
mazgch | 9:e7a5959ffae1 | 275 | return _getMessage(&_pipeRx, buf, len); |
mazgch | 2:b6012cd91657 | 276 | } |
mazgch | 2:b6012cd91657 | 277 | |
mazgch | 4:c959dd4c5fe8 | 278 | int GPSSerial::_send(const void* buf, int len) |
mazgch | 3:c7cd4887560d | 279 | { |
mazgch | 9:e7a5959ffae1 | 280 | return put((const char*)buf, len, true/*=blocking*/); |
mazgch | 3:c7cd4887560d | 281 | } |
mazgch | 3:c7cd4887560d | 282 | |
mazgch | 2:b6012cd91657 | 283 | // ---------------------------------------------------------------- |
mazgch | 2:b6012cd91657 | 284 | // I2C Implementation |
mazgch | 2:b6012cd91657 | 285 | // ---------------------------------------------------------------- |
mazgch | 2:b6012cd91657 | 286 | |
mazgch | 9:e7a5959ffae1 | 287 | GPSI2C::GPSI2C(PinName sda /*= GPSSDA*/, PinName scl /*= GPSSCL*/, |
mazgch | 19:2b5d097ca15d | 288 | unsigned char i2cAdr /*=GPSADR*/, int rxSize /*= 256*/) : |
mazgch | 2:b6012cd91657 | 289 | I2C(sda,scl), |
mazgch | 19:2b5d097ca15d | 290 | _pipe(rxSize), |
mazgch | 19:2b5d097ca15d | 291 | _i2cAdr(i2cAdr) |
mazgch | 2:b6012cd91657 | 292 | { |
mazgch | 45:ebc2fd8dcf21 | 293 | frequency(100000); |
mazgch | 74:208e3e32d263 | 294 | #ifdef TARGET_UBLOX_C027 |
mazgch | 74:208e3e32d263 | 295 | _onboard = (sda == GPSSDA) && (scl == GPSSCL); |
mazgch | 74:208e3e32d263 | 296 | if (_onboard) |
mazgch | 74:208e3e32d263 | 297 | c027_gps_powerOn(); |
mazgch | 74:208e3e32d263 | 298 | #endif |
mazgch | 2:b6012cd91657 | 299 | } |
mazgch | 2:b6012cd91657 | 300 | |
mazgch | 76:f7c3dd568dae | 301 | GPSI2C::~GPSI2C(void) |
mazgch | 76:f7c3dd568dae | 302 | { |
mazgch | 76:f7c3dd568dae | 303 | powerOff(); |
mazgch | 76:f7c3dd568dae | 304 | #ifdef TARGET_UBLOX_C027 |
mazgch | 76:f7c3dd568dae | 305 | if (_onboard) |
mazgch | 76:f7c3dd568dae | 306 | c027_gps_powerOff(); |
mazgch | 76:f7c3dd568dae | 307 | #endif |
mazgch | 76:f7c3dd568dae | 308 | } |
mazgch | 76:f7c3dd568dae | 309 | |
mazgch | 75:ce6e12067d0c | 310 | bool GPSI2C::init(PinName pn) |
mazgch | 2:b6012cd91657 | 311 | { |
mazgch | 75:ce6e12067d0c | 312 | if (pn != NC) { |
mazgch | 75:ce6e12067d0c | 313 | DigitalOut pin(pn, 0); |
mazgch | 95:8282dbbe1492 | 314 | ::wait_us(1); |
mazgch | 75:ce6e12067d0c | 315 | pin = 1; |
mazgch | 95:8282dbbe1492 | 316 | ::wait_ms(100); |
mazgch | 75:ce6e12067d0c | 317 | } |
mazgch | 74:208e3e32d263 | 318 | return !I2C::write(_i2cAdr,®STREAM,sizeof(REGSTREAM)); |
mazgch | 2:b6012cd91657 | 319 | } |
mazgch | 2:b6012cd91657 | 320 | |
mazgch | 2:b6012cd91657 | 321 | int GPSI2C::getMessage(char* buf, int len) |
mazgch | 2:b6012cd91657 | 322 | { |
mazgch | 6:775aef3f1d1f | 323 | // fill the pipe |
mazgch | 6:775aef3f1d1f | 324 | int sz = _pipe.free(); |
mazgch | 6:775aef3f1d1f | 325 | if (sz) |
mazgch | 6:775aef3f1d1f | 326 | sz = _get(buf, sz); |
mazgch | 2:b6012cd91657 | 327 | if (sz) |
mazgch | 2:b6012cd91657 | 328 | _pipe.put(buf, sz); |
mazgch | 6:775aef3f1d1f | 329 | // now parse it |
mazgch | 2:b6012cd91657 | 330 | return _getMessage(&_pipe, buf, len); |
mazgch | 2:b6012cd91657 | 331 | } |
mazgch | 2:b6012cd91657 | 332 | |
mazgch | 4:c959dd4c5fe8 | 333 | int GPSI2C::send(const char* buf, int len) |
mazgch | 4:c959dd4c5fe8 | 334 | { |
mazgch | 4:c959dd4c5fe8 | 335 | int sent = 0; |
mazgch | 4:c959dd4c5fe8 | 336 | if (len) |
mazgch | 4:c959dd4c5fe8 | 337 | { |
mazgch | 19:2b5d097ca15d | 338 | if (!I2C::write(_i2cAdr,®STREAM,sizeof(REGSTREAM),true)) |
mazgch | 11:b084552b03fe | 339 | sent = send(buf, len); |
mazgch | 4:c959dd4c5fe8 | 340 | stop(); |
mazgch | 4:c959dd4c5fe8 | 341 | } |
mazgch | 4:c959dd4c5fe8 | 342 | return sent; |
mazgch | 4:c959dd4c5fe8 | 343 | } |
mazgch | 4:c959dd4c5fe8 | 344 | |
mazgch | 3:c7cd4887560d | 345 | int GPSI2C::sendNmea(const char* buf, int len) |
mazgch | 3:c7cd4887560d | 346 | { |
mazgch | 3:c7cd4887560d | 347 | int sent = 0; |
mazgch | 19:2b5d097ca15d | 348 | if (!I2C::write(_i2cAdr,®STREAM,sizeof(REGSTREAM),true)) |
mazgch | 11:b084552b03fe | 349 | sent = GPSParser::sendNmea(buf, len); |
mazgch | 11:b084552b03fe | 350 | stop(); |
mazgch | 3:c7cd4887560d | 351 | return sent; |
mazgch | 3:c7cd4887560d | 352 | } |
mazgch | 3:c7cd4887560d | 353 | |
mazgch | 3:c7cd4887560d | 354 | int GPSI2C::sendUbx(unsigned char cls, unsigned char id, const void* buf, int len) |
mazgch | 3:c7cd4887560d | 355 | { |
mazgch | 3:c7cd4887560d | 356 | int sent = 0; |
mazgch | 19:2b5d097ca15d | 357 | if (!I2C::write(_i2cAdr,®STREAM,sizeof(REGSTREAM),true)) |
mazgch | 11:b084552b03fe | 358 | sent = GPSParser::sendUbx(cls, id, buf, len); |
mazgch | 15:5eda64e5b9d1 | 359 | I2C::stop(); |
mazgch | 3:c7cd4887560d | 360 | return sent; |
mazgch | 3:c7cd4887560d | 361 | } |
mazgch | 3:c7cd4887560d | 362 | |
mazgch | 2:b6012cd91657 | 363 | int GPSI2C::_get(char* buf, int len) |
mazgch | 2:b6012cd91657 | 364 | { |
mazgch | 3:c7cd4887560d | 365 | int read = 0; |
mazgch | 46:8ce9169e0747 | 366 | unsigned char sz[2] = {0,0}; |
mazgch | 19:2b5d097ca15d | 367 | if (!I2C::write(_i2cAdr,®LEN,sizeof(REGLEN),true) && |
mazgch | 46:8ce9169e0747 | 368 | !I2C::read(_i2cAdr,(char*)sz,sizeof(sz))) |
mazgch | 2:b6012cd91657 | 369 | { |
mazgch | 2:b6012cd91657 | 370 | int size = 256 * (int)sz[0] + sz[1]; |
mazgch | 2:b6012cd91657 | 371 | if (size > len) |
mazgch | 2:b6012cd91657 | 372 | size = len; |
mazgch | 46:8ce9169e0747 | 373 | if (size > 0) |
mazgch | 46:8ce9169e0747 | 374 | { |
mazgch | 46:8ce9169e0747 | 375 | if (!I2C::write(_i2cAdr,®STREAM,sizeof(REGSTREAM),true) && |
mazgch | 46:8ce9169e0747 | 376 | !I2C::read(_i2cAdr,buf,size)) { |
mazgch | 46:8ce9169e0747 | 377 | read = size; |
mazgch | 46:8ce9169e0747 | 378 | } |
mazgch | 46:8ce9169e0747 | 379 | } |
mazgch | 2:b6012cd91657 | 380 | } |
mazgch | 3:c7cd4887560d | 381 | return read; |
mazgch | 2:b6012cd91657 | 382 | } |
mazgch | 2:b6012cd91657 | 383 | |
mazgch | 4:c959dd4c5fe8 | 384 | int GPSI2C::_send(const void* buf, int len) |
mazgch | 3:c7cd4887560d | 385 | { |
mazgch | 19:2b5d097ca15d | 386 | return !I2C::write(_i2cAdr,(const char*)buf,len,true) ? len : 0; |
mazgch | 2:b6012cd91657 | 387 | } |
mazgch | 2:b6012cd91657 | 388 | |
mazgch | 2:b6012cd91657 | 389 | const char GPSI2C::REGLEN = 0xFD; |
mazgch | 2:b6012cd91657 | 390 | const char GPSI2C::REGSTREAM = 0xFF; |