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