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