Update version of EALib.

Dependencies:   FATFileSystem

Fork of EALib by IONX

Committer:
embeddedartists
Date:
Mon Nov 11 09:39:58 2013 +0000
Revision:
7:e431d9d47db6
Child:
12:15597e45eea0
Added XBee class. Tested with XBee S1

Who changed what in which revision?

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