* add C027_Support fork

Fork of C027_Support by u-blox

Committer:
mazgch
Date:
Thu Jun 12 07:17:38 2014 +0000
Revision:
93:2b5478693c20
Parent:
76:f7c3dd568dae
make destructor virtual (base class has changed)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mazgch 1:f41579f4e2ed 1 #pragma once
mazgch 1:f41579f4e2ed 2
mazgch 2:b6012cd91657 3 #include "mbed.h"
mazgch 2:b6012cd91657 4 #include "Pipe.h"
mazgch 1:f41579f4e2ed 5 #include "SerialPipe.h"
mazgch 2:b6012cd91657 6
mazgch 19:2b5d097ca15d 7 #ifdef TARGET_UBLOX_C027
mazgch 75:ce6e12067d0c 8 #define GPS_IF(onboard, shield) onboard
mazgch 19:2b5d097ca15d 9 #else
mazgch 75:ce6e12067d0c 10 #define GPS_IF(onboard, shield) shield
mazgch 19:2b5d097ca15d 11 #endif
mazgch 9:e7a5959ffae1 12
mazgch 38:e6cab4632d84 13 /** basic gps parser class
mazgch 38:e6cab4632d84 14 */
mazgch 2:b6012cd91657 15 class GPSParser
mazgch 2:b6012cd91657 16 {
mazgch 2:b6012cd91657 17 public:
mazgch 74:208e3e32d263 18 /** Power on / Wake up the gps
mazgch 74:208e3e32d263 19 */
mazgch 75:ce6e12067d0c 20 virtual bool init(PinName pn) = 0;
mazgch 74:208e3e32d263 21
mazgch 51:e7b81c31baec 22 enum {
mazgch 51:e7b81c31baec 23 // getLine Responses
mazgch 51:e7b81c31baec 24 WAIT = -1, //!< wait for more incoming data (the start of a message was found, or no data available)
mazgch 51:e7b81c31baec 25 NOT_FOUND = 0, //!< a parser concluded the the current offset of the pipe doe not contain a valid message
mazgch 39:9b4b9433e220 26
mazgch 51:e7b81c31baec 27 #define LENGTH(x) (x & 0x00FFFF) //!< extract/mask the length
mazgch 51:e7b81c31baec 28 #define PROTOCOL(x) (x & 0xFF0000) //!< extract/mask the type
mazgch 39:9b4b9433e220 29
mazgch 51:e7b81c31baec 30 UNKNOWN = 0x000000, //!< message type is unknown
mazgch 51:e7b81c31baec 31 UBX = 0x100000, //!< message if of protocol NMEA
mazgch 51:e7b81c31baec 32 NMEA = 0x200000 //!< message if of protocol UBX
mazgch 51:e7b81c31baec 33 };
mazgch 51:e7b81c31baec 34
mazgch 39:9b4b9433e220 35 /** Get a line from the physical interface. This fucntion
mazgch 39:9b4b9433e220 36 needs to be implemented in the inherited class.
mazgch 39:9b4b9433e220 37 \param buf the buffer to store it
mazgch 39:9b4b9433e220 38 \param len size of the buffer
mazgch 39:9b4b9433e220 39 \return type and length if something was found,
mazgch 39:9b4b9433e220 40 WAIT if not enough data is available
mazgch 39:9b4b9433e220 41 NOT_FOUND if nothing was found
mazgch 39:9b4b9433e220 42 */
mazgch 39:9b4b9433e220 43 virtual int getMessage(char* buf, int len) = 0;
mazgch 2:b6012cd91657 44
mazgch 39:9b4b9433e220 45 /** send a buffer
mazgch 39:9b4b9433e220 46 \param buf the buffer to write
mazgch 39:9b4b9433e220 47 \param len size of the buffer to write
mazgch 39:9b4b9433e220 48 \return bytes written
mazgch 39:9b4b9433e220 49 */
mazgch 4:c959dd4c5fe8 50 virtual int send(const char* buf, int len);
mazgch 39:9b4b9433e220 51
mazgch 39:9b4b9433e220 52 /** send a NMEA message, this function just takes the
mazgch 39:9b4b9433e220 53 payload and calculates and adds checksum. ($ and *XX\r\n will be added)
mazgch 39:9b4b9433e220 54 \param buf the message payload to write
mazgch 39:9b4b9433e220 55 \param len size of the message payload to write
mazgch 39:9b4b9433e220 56 \return total bytes written
mazgch 39:9b4b9433e220 57 */
mazgch 4:c959dd4c5fe8 58 virtual int sendNmea(const char* buf, int len);
mazgch 39:9b4b9433e220 59
mazgch 39:9b4b9433e220 60 /** send a UBX message, this function just takes the
mazgch 39:9b4b9433e220 61 payload and calculates and adds checksum.
mazgch 39:9b4b9433e220 62 \param cls the UBX class id
mazgch 39:9b4b9433e220 63 \param id the UBX message id
mazgch 39:9b4b9433e220 64 \param buf the message payload to write
mazgch 39:9b4b9433e220 65 \param len size of the message payload to write
mazgch 39:9b4b9433e220 66 \return total bytes written
mazgch 39:9b4b9433e220 67 */
mazgch 40:295099ff5338 68 virtual int sendUbx(unsigned char cls, unsigned char id,
mazgch 40:295099ff5338 69 const void* buf = NULL, int len = 0);
mazgch 74:208e3e32d263 70
mazgch 39:9b4b9433e220 71 /** Power off the gps, it can be again woken up by an
mazgch 39:9b4b9433e220 72 edge on the serial port on the external interrupt pin.
mazgch 39:9b4b9433e220 73 */
mazgch 31:a0bed6c1e05d 74 void powerOff(void);
mazgch 2:b6012cd91657 75
mazgch 39:9b4b9433e220 76 /** get the first character of a NMEA field
mazgch 39:9b4b9433e220 77 \param ix the index of the field to find
mazgch 39:9b4b9433e220 78 \param start the start of the buffer
mazgch 39:9b4b9433e220 79 \param end the end of the buffer
mazgch 39:9b4b9433e220 80 \return the pointer to the first character of the field.
mazgch 39:9b4b9433e220 81 */
mazgch 2:b6012cd91657 82 static const char* findNmeaItemPos(int ix, const char* start, const char* end);
mazgch 39:9b4b9433e220 83
mazgch 39:9b4b9433e220 84 /** extract a double value from a buffer containing a NMEA message
mazgch 39:9b4b9433e220 85 \param ix the index of the field to extract
mazgch 39:9b4b9433e220 86 \param buf the NMEA message
mazgch 39:9b4b9433e220 87 \param len the size of the NMEA message
mazgch 39:9b4b9433e220 88 \param val the extracted value
mazgch 39:9b4b9433e220 89 \return true if successful, false otherwise
mazgch 39:9b4b9433e220 90 */
mazgch 2:b6012cd91657 91 static bool getNmeaItem(int ix, char* buf, int len, double& val);
mazgch 39:9b4b9433e220 92
mazgch 39:9b4b9433e220 93 /** extract a interger value from a buffer containing a NMEA message
mazgch 39:9b4b9433e220 94 \param ix the index of the field to extract
mazgch 39:9b4b9433e220 95 \param buf the NMEA message
mazgch 39:9b4b9433e220 96 \param len the size of the NMEA message
mazgch 39:9b4b9433e220 97 \param val the extracted value
mazgch 39:9b4b9433e220 98 \param base the numeric base to be used (e.g. 8, 10 or 16)
mazgch 39:9b4b9433e220 99 \return true if successful, false otherwise
mazgch 39:9b4b9433e220 100 */
mazgch 2:b6012cd91657 101 static bool getNmeaItem(int ix, char* buf, int len, int& val, int base/*=10*/);
mazgch 39:9b4b9433e220 102
mazgch 39:9b4b9433e220 103 /** extract a char value from a buffer containing a NMEA message
mazgch 39:9b4b9433e220 104 \param ix the index of the field to extract
mazgch 39:9b4b9433e220 105 \param buf the NMEA message
mazgch 39:9b4b9433e220 106 \param len the size of the NMEA message
mazgch 39:9b4b9433e220 107 \param val the extracted value
mazgch 39:9b4b9433e220 108 \return true if successful, false otherwise
mazgch 39:9b4b9433e220 109 */
mazgch 2:b6012cd91657 110 static bool getNmeaItem(int ix, char* buf, int len, char& val);
mazgch 39:9b4b9433e220 111
mazgch 39:9b4b9433e220 112 /** extract a latitude/longitude value from a buffer containing a NMEA message
mazgch 39:9b4b9433e220 113 \param ix the index of the field to extract (will extract ix and ix + 1)
mazgch 39:9b4b9433e220 114 \param buf the NMEA message
mazgch 39:9b4b9433e220 115 \param len the size of the NMEA message
mazgch 39:9b4b9433e220 116 \param val the extracted latitude or longitude
mazgch 39:9b4b9433e220 117 \return true if successful, false otherwise
mazgch 39:9b4b9433e220 118 */
mazgch 20:535ef78655df 119 static bool getNmeaAngle(int ix, char* buf, int len, double& val);
mazgch 39:9b4b9433e220 120
mazgch 2:b6012cd91657 121 protected:
mazgch 39:9b4b9433e220 122 /** Get a line from the physical interface.
mazgch 39:9b4b9433e220 123 \param pipe the receiveing pipe to parse messages
mazgch 39:9b4b9433e220 124 \param buf the buffer to store it
mazgch 39:9b4b9433e220 125 \param len size of the buffer
mazgch 39:9b4b9433e220 126 \return type and length if something was found,
mazgch 39:9b4b9433e220 127 WAIT if not enough data is available
mazgch 39:9b4b9433e220 128 NOT_FOUND if nothing was found
mazgch 39:9b4b9433e220 129 */
mazgch 2:b6012cd91657 130 static int _getMessage(Pipe<char>* pipe, char* buf, int len);
mazgch 39:9b4b9433e220 131
mazgch 39:9b4b9433e220 132 /** Check if the current offset of the pipe contains a NMEA message.
mazgch 39:9b4b9433e220 133 \param pipe the receiveing pipe to parse messages
mazgch 39:9b4b9433e220 134 \param len numer of bytes to parse at maximum
mazgch 39:9b4b9433e220 135 \return length if something was found (including the NMEA frame)
mazgch 39:9b4b9433e220 136 WAIT if not enough data is available
mazgch 39:9b4b9433e220 137 NOT_FOUND if nothing was found
mazgch 39:9b4b9433e220 138 */
mazgch 2:b6012cd91657 139 static int _parseNmea(Pipe<char>* pipe, int len);
mazgch 39:9b4b9433e220 140
mazgch 39:9b4b9433e220 141 /** Check if the current offset of the pipe contains a UBX message.
mazgch 39:9b4b9433e220 142 \param pipe the receiveing pipe to parse messages
mazgch 39:9b4b9433e220 143 \param len numer of bytes to parse at maximum
mazgch 39:9b4b9433e220 144 \return length if something was found (including the UBX frame)
mazgch 39:9b4b9433e220 145 WAIT if not enough data is available
mazgch 39:9b4b9433e220 146 NOT_FOUND if nothing was found
mazgch 39:9b4b9433e220 147 */
mazgch 2:b6012cd91657 148 static int _parseUbx(Pipe<char>* pipe, int len);
mazgch 39:9b4b9433e220 149
mazgch 39:9b4b9433e220 150 /** Write bytes to the physical interface. This function
mazgch 39:9b4b9433e220 151 needs to be implemented by the inherited class.
mazgch 39:9b4b9433e220 152 \param buf the buffer to write
mazgch 39:9b4b9433e220 153 \param len size of the buffer to write
mazgch 39:9b4b9433e220 154 \return bytes written
mazgch 39:9b4b9433e220 155 */
mazgch 4:c959dd4c5fe8 156 virtual int _send(const void* buf, int len) = 0;
mazgch 39:9b4b9433e220 157
mazgch 39:9b4b9433e220 158 static const char toHex[16]; //!< num to hex conversion
mazgch 74:208e3e32d263 159 #ifdef TARGET_UBLOX_C027
mazgch 74:208e3e32d263 160 bool _onboard;
mazgch 74:208e3e32d263 161 #endif
mazgch 2:b6012cd91657 162 };
mazgch 2:b6012cd91657 163
mazgch 38:e6cab4632d84 164 /** gps class which uses a serial port
mazgch 38:e6cab4632d84 165 as physical interface.
mazgch 38:e6cab4632d84 166 */
mazgch 9:e7a5959ffae1 167 class GPSSerial : public SerialPipe, public GPSParser
mazgch 1:f41579f4e2ed 168 {
mazgch 1:f41579f4e2ed 169 public:
mazgch 38:e6cab4632d84 170 /** Constructor
mazgch 38:e6cab4632d84 171 \param tx is the serial ports transmit pin (gps to CPU)
mazgch 38:e6cab4632d84 172 \param rx is the serial ports receive pin (CPU to gps)
mazgch 38:e6cab4632d84 173 \param baudrate the baudrate of the gps use 9600
mazgch 38:e6cab4632d84 174 \param rxSize the size of the serial rx buffer
mazgch 38:e6cab4632d84 175 \param txSize the size of the serial tx buffer
mazgch 38:e6cab4632d84 176 */
mazgch 75:ce6e12067d0c 177 GPSSerial(PinName tx GPS_IF( = GPSTXD, /* = D8 */), // resistor on shield not populated
mazgch 75:ce6e12067d0c 178 PinName rx GPS_IF( = GPSRXD, /* = D9 */), // resistor on shield not populated
mazgch 75:ce6e12067d0c 179 int baudrate GPS_IF( = GPSBAUD, = 9600 ),
mazgch 19:2b5d097ca15d 180 int rxSize = 256 ,
mazgch 19:2b5d097ca15d 181 int txSize = 128 );
mazgch 39:9b4b9433e220 182
mazgch 76:f7c3dd568dae 183 //! Destructor
mazgch 93:2b5478693c20 184 virtual ~GPSSerial(void);
mazgch 76:f7c3dd568dae 185
mazgch 75:ce6e12067d0c 186 virtual bool init(PinName pn = NC);
mazgch 74:208e3e32d263 187
mazgch 38:e6cab4632d84 188 /** Get a line from the physical interface.
mazgch 38:e6cab4632d84 189 \param buf the buffer to store it
mazgch 38:e6cab4632d84 190 \param len size of the buffer
mazgch 38:e6cab4632d84 191 \return type and length if something was found,
mazgch 38:e6cab4632d84 192 WAIT if not enough data is available
mazgch 38:e6cab4632d84 193 NOT_FOUND if nothing was found
mazgch 38:e6cab4632d84 194 */
mazgch 2:b6012cd91657 195 virtual int getMessage(char* buf, int len);
mazgch 39:9b4b9433e220 196
mazgch 2:b6012cd91657 197 protected:
mazgch 38:e6cab4632d84 198 /** Write bytes to the physical interface.
mazgch 38:e6cab4632d84 199 \param buf the buffer to write
mazgch 38:e6cab4632d84 200 \param len size of the buffer to write
mazgch 38:e6cab4632d84 201 \return bytes written
mazgch 38:e6cab4632d84 202 */
mazgch 4:c959dd4c5fe8 203 virtual int _send(const void* buf, int len);
mazgch 1:f41579f4e2ed 204 };
mazgch 2:b6012cd91657 205
mazgch 38:e6cab4632d84 206 /** gps class which uses a i2c as physical interface.
mazgch 38:e6cab4632d84 207 */
mazgch 2:b6012cd91657 208 class GPSI2C : public I2C, public GPSParser
mazgch 2:b6012cd91657 209 {
mazgch 2:b6012cd91657 210 public:
mazgch 38:e6cab4632d84 211 /** Constructor
mazgch 38:e6cab4632d84 212 \param sda is the I2C SDA pin (between CPU and GPS)
mazgch 38:e6cab4632d84 213 \param scl is the I2C SCL pin (CPU to GPS)
mazgch 38:e6cab4632d84 214 \param adr the I2C address of the GPS set to (66<<1)
mazgch 38:e6cab4632d84 215 \param rxSize the size of the serial rx buffer
mazgch 38:e6cab4632d84 216 */
mazgch 75:ce6e12067d0c 217 GPSI2C(PinName sda GPS_IF( = GPSSDA, = D14 ),
mazgch 75:ce6e12067d0c 218 PinName scl GPS_IF( = GPSSCL, = D15 ),
mazgch 75:ce6e12067d0c 219 unsigned char i2cAdr GPS_IF( = GPSADR, = (66<<1) ),
mazgch 19:2b5d097ca15d 220 int rxSize = 256 );
mazgch 76:f7c3dd568dae 221 //! Destructor
mazgch 93:2b5478693c20 222 virtual ~GPSI2C(void);
mazgch 74:208e3e32d263 223
mazgch 38:e6cab4632d84 224 /** helper function to probe the i2c device
mazgch 38:e6cab4632d84 225 \return true if successfully detected the gps.
mazgch 38:e6cab4632d84 226 */
mazgch 75:ce6e12067d0c 227 virtual bool init(PinName pn = GPS_IF( GPSINT, NC /* D7 resistor R67 on shield not mounted */));
mazgch 2:b6012cd91657 228
mazgch 38:e6cab4632d84 229 /** Get a line from the physical interface.
mazgch 38:e6cab4632d84 230 \param buf the buffer to store it
mazgch 38:e6cab4632d84 231 \param len size of the buffer
mazgch 38:e6cab4632d84 232 \return type and length if something was found,
mazgch 38:e6cab4632d84 233 WAIT if not enough data is available
mazgch 38:e6cab4632d84 234 NOT_FOUND if nothing was found
mazgch 38:e6cab4632d84 235 */
mazgch 2:b6012cd91657 236 virtual int getMessage(char* buf, int len);
mazgch 38:e6cab4632d84 237
mazgch 38:e6cab4632d84 238 /** send a buffer
mazgch 38:e6cab4632d84 239 \param buf the buffer to write
mazgch 38:e6cab4632d84 240 \param len size of the buffer to write
mazgch 38:e6cab4632d84 241 \return bytes written
mazgch 38:e6cab4632d84 242 */
mazgch 4:c959dd4c5fe8 243 virtual int send(const char* buf, int len);
mazgch 38:e6cab4632d84 244
mazgch 38:e6cab4632d84 245 /** send a NMEA message, this function just takes the
mazgch 38:e6cab4632d84 246 payload and calculates and adds checksum. ($ and *XX\r\n will be added)
mazgch 38:e6cab4632d84 247 \param buf the message payload to write
mazgch 38:e6cab4632d84 248 \param len size of the message payload to write
mazgch 38:e6cab4632d84 249 \return total bytes written
mazgch 38:e6cab4632d84 250 */
mazgch 3:c7cd4887560d 251 virtual int sendNmea(const char* buf, int len);
mazgch 38:e6cab4632d84 252
mazgch 38:e6cab4632d84 253 /** send a UBX message, this function just takes the
mazgch 38:e6cab4632d84 254 payload and calculates and adds checksum.
mazgch 38:e6cab4632d84 255 \param cls the UBX class id
mazgch 38:e6cab4632d84 256 \param id the UBX message id
mazgch 38:e6cab4632d84 257 \param buf the message payload to write
mazgch 38:e6cab4632d84 258 \param len size of the message payload to write
mazgch 38:e6cab4632d84 259 \return total bytes written
mazgch 38:e6cab4632d84 260 */
mazgch 39:9b4b9433e220 261 virtual int sendUbx(unsigned char cls, unsigned char id,
mazgch 39:9b4b9433e220 262 const void* buf = NULL, int len = 0);
mazgch 39:9b4b9433e220 263
mazgch 2:b6012cd91657 264 protected:
mazgch 38:e6cab4632d84 265 /** check if the port is writeable (like SerialPipe)
mazgch 38:e6cab4632d84 266 \return true if writeable
mazgch 38:e6cab4632d84 267 */
mazgch 9:e7a5959ffae1 268 bool writeable(void) { return true; }
mazgch 39:9b4b9433e220 269
mazgch 38:e6cab4632d84 270 /** Write a character (like SerialPipe)
mazgch 38:e6cab4632d84 271 \param c the character to write
mazgch 38:e6cab4632d84 272 \return true if succesffully written
mazgch 38:e6cab4632d84 273 */
mazgch 9:e7a5959ffae1 274 bool putc(int c) { char ch = c; return send(&ch, 1); }
mazgch 39:9b4b9433e220 275
mazgch 38:e6cab4632d84 276 /** Write bytes to the physical interface.
mazgch 38:e6cab4632d84 277 \param buf the buffer to write
mazgch 38:e6cab4632d84 278 \param len size of the buffer to write
mazgch 38:e6cab4632d84 279 \return bytes written
mazgch 38:e6cab4632d84 280 */
mazgch 4:c959dd4c5fe8 281 virtual int _send(const void* buf, int len);
mazgch 39:9b4b9433e220 282
mazgch 38:e6cab4632d84 283 /** read bytes from the physical interface.
mazgch 38:e6cab4632d84 284 \param buf the buffer to read into
mazgch 38:e6cab4632d84 285 \param len size of the read buffer
mazgch 38:e6cab4632d84 286 \return bytes read
mazgch 38:e6cab4632d84 287 */
mazgch 38:e6cab4632d84 288 int _get(char* buf, int len);
mazgch 3:c7cd4887560d 289
mazgch 38:e6cab4632d84 290 Pipe<char> _pipe; //!< the rx pipe
mazgch 38:e6cab4632d84 291 unsigned char _i2cAdr; //!< the i2c address
mazgch 38:e6cab4632d84 292 static const char REGLEN; //!< the length i2c register address
mazgch 38:e6cab4632d84 293 static const char REGSTREAM;//!< the stream i2c register address
mazgch 2:b6012cd91657 294 };