Solutions for the XBee experiments for LPC812 MAX

Dependencies:   mbed

Committer:
embeddedartists
Date:
Wed Nov 20 08:26:28 2013 +0000
Revision:
0:54828b08e71d
First Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:54828b08e71d 1
embeddedartists 0:54828b08e71d 2 #ifndef XBEE_H
embeddedartists 0:54828b08e71d 3 #define XBEE_H
embeddedartists 0:54828b08e71d 4
embeddedartists 0:54828b08e71d 5 #define RX_BUF_SIZE (512)
embeddedartists 0:54828b08e71d 6 #define XBEE_BUF_SZ (200)
embeddedartists 0:54828b08e71d 7
embeddedartists 0:54828b08e71d 8 #define XBEE_ADDRLO_BROADCAST (0x0000FFFF)
embeddedartists 0:54828b08e71d 9 #define XBEE_ADDRHI_BROADCAST (0x00000000)
embeddedartists 0:54828b08e71d 10
embeddedartists 0:54828b08e71d 11 /**
embeddedartists 0:54828b08e71d 12 * Interface to Digi International's XBee module. The XBee S1 module has
embeddedartists 0:54828b08e71d 13 * been used during testing of this interface.
embeddedartists 0:54828b08e71d 14 */
embeddedartists 0:54828b08e71d 15 class XBee {
embeddedartists 0:54828b08e71d 16 public:
embeddedartists 0:54828b08e71d 17
embeddedartists 0:54828b08e71d 18 /** Error codes returned from public methods */
embeddedartists 0:54828b08e71d 19 enum XBeeError {
embeddedartists 0:54828b08e71d 20 Ok = 0,
embeddedartists 0:54828b08e71d 21 ReadError = -1,
embeddedartists 0:54828b08e71d 22 CmdError = -2,
embeddedartists 0:54828b08e71d 23 BufTooSmallError = -3,
embeddedartists 0:54828b08e71d 24 TimeOutError = -4,
embeddedartists 0:54828b08e71d 25 NotInitializedError = -5,
embeddedartists 0:54828b08e71d 26 ArgumentError = -6
embeddedartists 0:54828b08e71d 27
embeddedartists 0:54828b08e71d 28 };
embeddedartists 0:54828b08e71d 29
embeddedartists 0:54828b08e71d 30 /** Callback function/method types. See registerCallback() */
embeddedartists 0:54828b08e71d 31 enum CallbackType {
embeddedartists 0:54828b08e71d 32 /** Device is up and ready */
embeddedartists 0:54828b08e71d 33 CbDeviceUp = 0,
embeddedartists 0:54828b08e71d 34 /** Device is down (disconnected) */
embeddedartists 0:54828b08e71d 35 CbDeviceDown,
embeddedartists 0:54828b08e71d 36 /** A node has been found */
embeddedartists 0:54828b08e71d 37 CbNodeFound,
embeddedartists 0:54828b08e71d 38 /** Transmit status */
embeddedartists 0:54828b08e71d 39 CbTxStat,
embeddedartists 0:54828b08e71d 40 /** Data is available */
embeddedartists 0:54828b08e71d 41 CbDataAvailable,
embeddedartists 0:54828b08e71d 42 CbNum // must be last
embeddedartists 0:54828b08e71d 43
embeddedartists 0:54828b08e71d 44 };
embeddedartists 0:54828b08e71d 45
embeddedartists 0:54828b08e71d 46 /** Xbee types */
embeddedartists 0:54828b08e71d 47 enum XBeeType {
embeddedartists 0:54828b08e71d 48 EndDevice = 0,
embeddedartists 0:54828b08e71d 49 Coordinator
embeddedartists 0:54828b08e71d 50 };
embeddedartists 0:54828b08e71d 51
embeddedartists 0:54828b08e71d 52 /** Transmit status */
embeddedartists 0:54828b08e71d 53 enum XBeeTxStatus {
embeddedartists 0:54828b08e71d 54 TxStatusOk = 0,
embeddedartists 0:54828b08e71d 55 TxStatusNoAck,
embeddedartists 0:54828b08e71d 56 TxStatusCCA,
embeddedartists 0:54828b08e71d 57 TxStatusPurged
embeddedartists 0:54828b08e71d 58 };
embeddedartists 0:54828b08e71d 59
embeddedartists 0:54828b08e71d 60 /**
embeddedartists 0:54828b08e71d 61 * Create an interface to an XBee module.
embeddedartists 0:54828b08e71d 62 *
embeddedartists 0:54828b08e71d 63 * @param tx UART TX line
embeddedartists 0:54828b08e71d 64 * @param tx UART rx line
embeddedartists 0:54828b08e71d 65 * @param reset reset pin
embeddedartists 0:54828b08e71d 66 * @param sleep sleep request pin
embeddedartists 0:54828b08e71d 67 */
embeddedartists 0:54828b08e71d 68 XBee(PinName tx, PinName rx, PinName reset, PinName sleep);
embeddedartists 0:54828b08e71d 69
embeddedartists 0:54828b08e71d 70 /**
embeddedartists 0:54828b08e71d 71 * Initialize the XBee module and configure it to be of a
embeddedartists 0:54828b08e71d 72 * specific type.
embeddedartists 0:54828b08e71d 73 *
embeddedartists 0:54828b08e71d 74 * Note: This implementation will always configure the XBee module to
embeddedartists 0:54828b08e71d 75 * work in API mode.
embeddedartists 0:54828b08e71d 76 *
embeddedartists 0:54828b08e71d 77 * @param type the type of this XBee node
embeddedartists 0:54828b08e71d 78 * @param panId the PAN ID to use for the XBee network. This argument
embeddedartists 0:54828b08e71d 79 * must contain 4 characters representing hexadecimal values
embeddedartists 0:54828b08e71d 80 * (e.g. "A1E0" means the hexadecimal value 0xA1E0);
embeddedartists 0:54828b08e71d 81 */
embeddedartists 0:54828b08e71d 82 XBeeError init(XBeeType type, const char* panId);
embeddedartists 0:54828b08e71d 83
embeddedartists 0:54828b08e71d 84 /**
embeddedartists 0:54828b08e71d 85 * Register a callback function
embeddedartists 0:54828b08e71d 86 *
embeddedartists 0:54828b08e71d 87 * @param fptr Callback function to register
embeddedartists 0:54828b08e71d 88 * @param type The type of event that will trigger a call to the function.
embeddedartists 0:54828b08e71d 89 */
embeddedartists 0:54828b08e71d 90 void registerCallback(void (*fptr)(void), CallbackType type) {
embeddedartists 0:54828b08e71d 91 if (fptr) {
embeddedartists 0:54828b08e71d 92 _callbacks[type].attach(fptr);
embeddedartists 0:54828b08e71d 93 }
embeddedartists 0:54828b08e71d 94 }
embeddedartists 0:54828b08e71d 95
embeddedartists 0:54828b08e71d 96 /**
embeddedartists 0:54828b08e71d 97 * Register a callback method
embeddedartists 0:54828b08e71d 98 *
embeddedartists 0:54828b08e71d 99 * @param tptr pointer to the object to call the member function on
embeddedartists 0:54828b08e71d 100 * @param mptr pointer to the member function to be called
embeddedartists 0:54828b08e71d 101 * @param type The type of event that will trigger a call to the method.
embeddedartists 0:54828b08e71d 102 */
embeddedartists 0:54828b08e71d 103 template<typename T>
embeddedartists 0:54828b08e71d 104 void registerCallback(T* tptr, void (T::*mptr)(void), CallbackType type) {
embeddedartists 0:54828b08e71d 105 if((mptr != NULL) && (tptr != NULL)) {
embeddedartists 0:54828b08e71d 106 _callbacks[type].attach(tptr, mptr);
embeddedartists 0:54828b08e71d 107 }
embeddedartists 0:54828b08e71d 108 }
embeddedartists 0:54828b08e71d 109
embeddedartists 0:54828b08e71d 110 /**
embeddedartists 0:54828b08e71d 111 * Call this method repeatedly to process incoming data.
embeddedartists 0:54828b08e71d 112 */
embeddedartists 0:54828b08e71d 113 void process();
embeddedartists 0:54828b08e71d 114
embeddedartists 0:54828b08e71d 115 /**
embeddedartists 0:54828b08e71d 116 * Get address of remote node. This method will only return valid data
embeddedartists 0:54828b08e71d 117 * when called in the context of the CbDataAvailable and CbNodeFound
embeddedartists 0:54828b08e71d 118 * callbacks
embeddedartists 0:54828b08e71d 119 *
embeddedartists 0:54828b08e71d 120 * @param addrHi Top 32 bits of address will be written to this argument
embeddedartists 0:54828b08e71d 121 * @param addrLo Bottom 32 bits of address will be written to this argument
embeddedartists 0:54828b08e71d 122 */
embeddedartists 0:54828b08e71d 123 XBeeError getRemoteAddress(uint32_t* addrHi, uint32_t* addrLo);
embeddedartists 0:54828b08e71d 124
embeddedartists 0:54828b08e71d 125 /**
embeddedartists 0:54828b08e71d 126 * Get signal strength indicator value (RSSI). This method will only
embeddedartists 0:54828b08e71d 127 * return valid data when called in the context of the
embeddedartists 0:54828b08e71d 128 * CbDataAvailable and CbNodeFound callbacks
embeddedartists 0:54828b08e71d 129 *
embeddedartists 0:54828b08e71d 130 * @param rssi RSSI value will be written to this argument
embeddedartists 0:54828b08e71d 131 */
embeddedartists 0:54828b08e71d 132 XBeeError getRssi(uint8_t* rssi );
embeddedartists 0:54828b08e71d 133
embeddedartists 0:54828b08e71d 134 /**
embeddedartists 0:54828b08e71d 135 * Get the transmit status. This method will only return valid data when
embeddedartists 0:54828b08e71d 136 * called in the context of the CbTxStat callback.
embeddedartists 0:54828b08e71d 137 *
embeddedartists 0:54828b08e71d 138 * @param frameId the frame ID will be written to this argument
embeddedartists 0:54828b08e71d 139 * @param status the status will be written to this argument
embeddedartists 0:54828b08e71d 140 */
embeddedartists 0:54828b08e71d 141 XBeeError getTxStatus(uint8_t* frameId, XBeeTxStatus* status);
embeddedartists 0:54828b08e71d 142
embeddedartists 0:54828b08e71d 143 /**
embeddedartists 0:54828b08e71d 144 * Get received data. This method will only return valid data when called
embeddedartists 0:54828b08e71d 145 * in the context of the CbDataAvailable callback
embeddedartists 0:54828b08e71d 146 *
embeddedartists 0:54828b08e71d 147 * @param data will point to a buffer with received data
embeddedartists 0:54828b08e71d 148 * @param len will contain the length of the received data
embeddedartists 0:54828b08e71d 149 */
embeddedartists 0:54828b08e71d 150 XBeeError getData(char** data, uint8_t* len);
embeddedartists 0:54828b08e71d 151
embeddedartists 0:54828b08e71d 152 /**
embeddedartists 0:54828b08e71d 153 * Send data to a node with specified address. It is also possible to
embeddedartists 0:54828b08e71d 154 * broadcast a message using broadcast address (XBEE_ADDRHI_BROADCAST,
embeddedartists 0:54828b08e71d 155 * XBEE_ADDRLO_BROADCAST).
embeddedartists 0:54828b08e71d 156 *
embeddedartists 0:54828b08e71d 157 * @param addrHi Top 32 bits of address
embeddedartists 0:54828b08e71d 158 * @param addrLo Bottom 32 bits of address
embeddedartists 0:54828b08e71d 159 * @param data buffer containing data to send
embeddedartists 0:54828b08e71d 160 * @param len number of bytes to send
embeddedartists 0:54828b08e71d 161 * @param frameId the ID of the frame will be written to this argument.
embeddedartists 0:54828b08e71d 162 * The ID can then be used to match this request with the status returned
embeddedartists 0:54828b08e71d 163 * in the CbTxStat callback.
embeddedartists 0:54828b08e71d 164 */
embeddedartists 0:54828b08e71d 165 XBeeError send(uint32_t addrHi, uint32_t addrLo, char* data,
embeddedartists 0:54828b08e71d 166 uint8_t len, uint8_t* frameId);
embeddedartists 0:54828b08e71d 167
embeddedartists 0:54828b08e71d 168 /**
embeddedartists 0:54828b08e71d 169 * Send a Node Discover request. All modules on the operating channel and
embeddedartists 0:54828b08e71d 170 * PAN ID should respond. The responses will be reported in the CbNodeFound
embeddedartists 0:54828b08e71d 171 * callback.
embeddedartists 0:54828b08e71d 172 */
embeddedartists 0:54828b08e71d 173 XBeeError discoverNodes();
embeddedartists 0:54828b08e71d 174
embeddedartists 0:54828b08e71d 175 /**
embeddedartists 0:54828b08e71d 176 * Request the module to enter sleep mode.
embeddedartists 0:54828b08e71d 177 */
embeddedartists 0:54828b08e71d 178 XBeeError enterSleep();
embeddedartists 0:54828b08e71d 179
embeddedartists 0:54828b08e71d 180 /**
embeddedartists 0:54828b08e71d 181 * Request the module to exit sleep mode.
embeddedartists 0:54828b08e71d 182 */
embeddedartists 0:54828b08e71d 183 XBeeError exitSleep();
embeddedartists 0:54828b08e71d 184
embeddedartists 0:54828b08e71d 185
embeddedartists 0:54828b08e71d 186 protected:
embeddedartists 0:54828b08e71d 187
embeddedartists 0:54828b08e71d 188
embeddedartists 0:54828b08e71d 189 private:
embeddedartists 0:54828b08e71d 190
embeddedartists 0:54828b08e71d 191 enum RfState {
embeddedartists 0:54828b08e71d 192 RfStateFrame = 0,
embeddedartists 0:54828b08e71d 193 RfStateLength,
embeddedartists 0:54828b08e71d 194 RfStateData
embeddedartists 0:54828b08e71d 195 };
embeddedartists 0:54828b08e71d 196
embeddedartists 0:54828b08e71d 197
embeddedartists 0:54828b08e71d 198 bool _initialized;
embeddedartists 0:54828b08e71d 199 Serial _serial;
embeddedartists 0:54828b08e71d 200 XBeeType _type;
embeddedartists 0:54828b08e71d 201 DigitalOut _reset;
embeddedartists 0:54828b08e71d 202 DigitalOut _sleep;
embeddedartists 0:54828b08e71d 203
embeddedartists 0:54828b08e71d 204 uint8_t rxqIn;
embeddedartists 0:54828b08e71d 205 uint8_t rxqOut;
embeddedartists 0:54828b08e71d 206 uint8_t rxq[RX_BUF_SIZE];
embeddedartists 0:54828b08e71d 207
embeddedartists 0:54828b08e71d 208 uint32_t _rfFrameTimeout;
embeddedartists 0:54828b08e71d 209 Timer _rfFrameTimer;
embeddedartists 0:54828b08e71d 210
embeddedartists 0:54828b08e71d 211 RfState _rfState;
embeddedartists 0:54828b08e71d 212 uint32_t _rfPos;
embeddedartists 0:54828b08e71d 213 uint32_t _rfFrameLen;
embeddedartists 0:54828b08e71d 214 char _rfBuf[XBEE_BUF_SZ];
embeddedartists 0:54828b08e71d 215 uint8_t _rfFrameId;
embeddedartists 0:54828b08e71d 216
embeddedartists 0:54828b08e71d 217 uint32_t _addrHi;
embeddedartists 0:54828b08e71d 218 uint32_t _addrLo;
embeddedartists 0:54828b08e71d 219 uint8_t _rssi;
embeddedartists 0:54828b08e71d 220 uint8_t _frameId;
embeddedartists 0:54828b08e71d 221 XBeeTxStatus _txStatus;
embeddedartists 0:54828b08e71d 222 char* _recvData;
embeddedartists 0:54828b08e71d 223 uint8_t _recvLen;
embeddedartists 0:54828b08e71d 224
embeddedartists 0:54828b08e71d 225
embeddedartists 0:54828b08e71d 226 FunctionPointer _callbacks[CbNum];
embeddedartists 0:54828b08e71d 227
embeddedartists 0:54828b08e71d 228
embeddedartists 0:54828b08e71d 229 void uartRxIrq();
embeddedartists 0:54828b08e71d 230 void uartRxQPut(uint8_t data);
embeddedartists 0:54828b08e71d 231 uint8_t uartRxQGet();
embeddedartists 0:54828b08e71d 232 bool uartRxQIsEmpty();
embeddedartists 0:54828b08e71d 233 uint32_t uartReceive(char *buf, uint32_t buflen);
embeddedartists 0:54828b08e71d 234 int32_t uartReadLine(char* buf, uint32_t bufLen, uint32_t timeout);
embeddedartists 0:54828b08e71d 235
embeddedartists 0:54828b08e71d 236 void resetModule();
embeddedartists 0:54828b08e71d 237 XBeeError commandMode();
embeddedartists 0:54828b08e71d 238 XBeeError atGet(const char* atCmd, char* resp, uint32_t respLen);
embeddedartists 0:54828b08e71d 239 XBeeError atSet(const char* atCmd);
embeddedartists 0:54828b08e71d 240
embeddedartists 0:54828b08e71d 241 void processByte(char data);
embeddedartists 0:54828b08e71d 242 void processFrame(char* buf, uint32_t len);
embeddedartists 0:54828b08e71d 243
embeddedartists 0:54828b08e71d 244 void handleAtResponse(uint8_t frameId, char* atBuf, uint8_t status,
embeddedartists 0:54828b08e71d 245 char* valueBuf, uint32_t valueLen);
embeddedartists 0:54828b08e71d 246
embeddedartists 0:54828b08e71d 247 void handleDiscovery(uint8_t status, char* buf, uint32_t len);
embeddedartists 0:54828b08e71d 248 void handleTxStatus(uint8_t frameId, uint8_t status);
embeddedartists 0:54828b08e71d 249 void processData(uint32_t addrHi, uint32_t addrLo, uint8_t rssi,
embeddedartists 0:54828b08e71d 250 uint8_t opt, char* buf, uint32_t len);
embeddedartists 0:54828b08e71d 251 void handleModemStatus(uint8_t status);
embeddedartists 0:54828b08e71d 252
embeddedartists 0:54828b08e71d 253 char checksum(char* buf, uint32_t len);
embeddedartists 0:54828b08e71d 254 uint32_t bufTo32bitInt(const char* buf);
embeddedartists 0:54828b08e71d 255 void int32bitToBuf(uint32_t v, char* buf);
embeddedartists 0:54828b08e71d 256
embeddedartists 0:54828b08e71d 257 XBeeError apiTx64(uint32_t addrHi, uint32_t addrLo, char* data,
embeddedartists 0:54828b08e71d 258 uint32_t len, uint8_t* frameId);
embeddedartists 0:54828b08e71d 259 XBeeError apiAtCmd(const char* atCmd, uint32_t param, bool useParameter);
embeddedartists 0:54828b08e71d 260
embeddedartists 0:54828b08e71d 261 uint8_t getFrameId();
embeddedartists 0:54828b08e71d 262
embeddedartists 0:54828b08e71d 263
embeddedartists 0:54828b08e71d 264 };
embeddedartists 0:54828b08e71d 265
embeddedartists 0:54828b08e71d 266 #endif