00001 /*! \file WaspXBee.h 00002 \brief Library for managing the UART related with the XBee 00003 00004 Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L. 00005 http://www.libelium.com 00006 00007 This program is free software: you can redistribute it and/or modify 00008 it under the terms of the GNU Lesser General Public License as published by 00009 the Free Software Foundation, either version 2.1 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. 00019 00020 Version: 0.11 00021 00022 Design: David Gascón 00023 00024 Implementation: Alberto Bielsa 00025 00026 */ 00027 00028 /*! \def WaspXBeeXSC_h 00029 \brief The library flag 00030 00031 */ 00032 #ifndef WaspXBeeXSC_h 00033 #define WaspXBeeXSC_h 00034 00035 /****************************************************************************** 00036 * Includes 00037 ******************************************************************************/ 00038 00039 #include <string.h> 00040 #include <stdint.h> 00041 #include <stdlib.h> 00042 #include "WConstants.h" 00043 #include "WaspXBee.h" 00044 #include "WaspUSB.h" 00045 #include <inttypes.h> 00046 00047 #ifndef __WASPXBEECONSTANTS_H__ 00048 #include "WaspXBeeConstants.h" 00049 #endif 00050 00051 /****************************************************************************** 00052 * Definitions & Declarations 00053 ******************************************************************************/ 00054 00055 //! Structure : used for storing the information and data to send/receive a packet using XBee XSC module 00056 /*! 00057 */ 00058 typedef struct packetXSC 00059 { 00060 //! Structure Variable : Source MAC higher bytes 00061 /*! 00062 */ 00063 uint8_t sourceMacHigh[2]; 00064 00065 //! Structure Variable : Source MAC lower bytes 00066 /*! 00067 */ 00068 uint8_t sourceMacLow[2]; 00069 00070 //! Structure Variable : Data to send or received 00071 /*! 00072 */ 00073 char data[MAX_DATA]; 00074 00075 //! Structure Variable : Data sent/received length 00076 /*! 00077 */ 00078 uint8_t data_length; 00079 00080 //! Structure Variable : Last received packet RSSI 00081 /*! 00082 */ 00083 uint8_t RSSI; 00084 }; 00085 00086 /****************************************************************************** 00087 * Class 00088 ******************************************************************************/ 00089 00090 //! WaspXBeeXSC Class 00091 /*! 00092 WaspXBee Class defines all the variables and functions used for managing the XBee XSC modules 00093 */ 00094 class WaspXBeeXSC 00095 { 00096 public: 00097 00098 //! class constructor 00099 /*! 00100 It does nothing 00101 \param void 00102 \return void 00103 */ 00104 WaspXBeeXSC(){}; 00105 00106 //! It initializes the necessary variables 00107 /*! 00108 \param uint8_t protocol_used : specifies the protocol used in the XBee module (XBEE_XSC in this case) 00109 \param uint8_t frequency : specifies the frequency used in the XBee module (FREQ900M in this case) 00110 \param uint8_t model_used : specifies the XBee model used (PRO) 00111 \return void 00112 */ 00113 void init(uint8_t protocol_used, uint8_t frequency, uint8_t model_used); 00114 00115 //! It sets the module into Command Mode 00116 /*! 00117 \param void 00118 \return '0' on success, '1' otherwise 00119 */ 00120 uint8_t setCommandMode(); 00121 00122 //! It exits the module from Command Mode 00123 /*! 00124 \param void 00125 \return '0' on success, '1' otherwise 00126 */ 00127 uint8_t exitCommandMode(); 00128 00129 //! It sets the Baud Rate used in the module 00130 /*! 00131 \param uint8_t brate : the baud rate (range [0-5]) 00132 \return '0' on success, '1' otherwise 00133 */ 00134 uint8_t setBaudRate(uint8_t brate); 00135 00136 //! It gets the Baud Rate used in the module 00137 /*! 00138 It stores in global 'baudRate' variable the Baud Rate used in the module 00139 \return '0' on success, '1' otherwise 00140 */ 00141 uint8_t getBaudRate(); 00142 00143 //! It sets the module Vendor ID 00144 /*! 00145 \param uint8_t VID_H : higher module Vendor ID byte (range [0x00-0x7F]) 00146 \param uint8_t VID_L : lower module Vendor ID byte (range [0x00-0xFF]) 00147 \return '0' on success, '1' otherwise 00148 */ 00149 uint8_t setVendorID(uint8_t VID_H, uint8_t VID_L); 00150 00151 //! It gets the module Vendor ID 00152 /*! 00153 It stores in global 'vendorID' variable the module Vendor ID 00154 \return '0' on success, '1' otherwise 00155 */ 00156 uint8_t getVendorID(); 00157 00158 //! It gets the dBm level in the most recent packet 00159 /*! 00160 It stores in global 'valueRSSI' variable the dBm level in the most recent packet 00161 \return '0' on success, '1' otherwise 00162 */ 00163 uint8_t getRSSI(); 00164 00165 //! It sets the networking address of a module 00166 /*! 00167 \param uint8_t destAD_H : higher destination address byte (range [0x00-0xFF]) 00168 \param uint8_t destAD_L : lower destination address byte (range [0x00-0xFF]) 00169 \return '0' on success, '1' otherwise 00170 */ 00171 uint8_t setDestAddress(uint8_t destAD_H, uint8_t destAD_L); 00172 00173 //! It gets the networking address of a module 00174 /*! 00175 It stores in global 'destAddress' variable the networking address of a module 00176 \return '0' on success, '1' otherwise 00177 */ 00178 uint8_t getDestAddress(); 00179 00180 //! It sets the receive error count 00181 /*! 00182 \param uint8_t recerror_H : higher receiver error count byte (range [0x00-0xFF]) 00183 \param uint8_t recerror_L : lower receiver error count byte (range [0x00-0xFF]) 00184 \return '0' on success, '1' otherwise 00185 */ 00186 uint8_t setReceiveErrorCount(uint8_t recerror_H, uint8_t recerror_L); 00187 00188 //! It gets the receive error count 00189 /*! 00190 It stores in global 'receiveErrorCount' variable the receive error count 00191 \return '0' on success, '1' otherwise 00192 */ 00193 uint8_t getReceiveErrorCount(); 00194 00195 //! It forces a Wake-up initializer to be sent on the next transmit 00196 /*! 00197 \return '0' on success, '1' otherwise 00198 */ 00199 uint8_t forceWakeUP(); 00200 00201 //! It resets the module through the UART 00202 /*! 00203 \return '0' on success, '1' otherwise 00204 */ 00205 uint8_t forceReset(); 00206 00207 //! It sets the receive good count 00208 /*! 00209 \param uint8_t recgood_H : higher receive good count byte (range [0x00-0xFF]) 00210 \param uint8_t recgood_L : lower receive good count byte (range [0x00-0xFF]) 00211 \return '0' on success, '1' otherwise 00212 */ 00213 uint8_t setReceiveGoodCount(uint8_t recgood_H, uint8_t recgood_L); 00214 00215 //! It gets the receive good count 00216 /*! 00217 It stores in global 'receiveGoodCount' variable the receive good count 00218 \return '0' on success, '1' otherwise 00219 */ 00220 uint8_t getReceiveGoodCount(); 00221 00222 //! It sets the Hopping Channel used in the transmission 00223 /*! 00224 \param uint8_t hchannel : hopping channel used in the transmission (range [0-6]) 00225 \return '0' on success, '1' otherwise 00226 */ 00227 uint8_t setHoppingChannel(uint8_t hchannel); 00228 00229 //! It gets the Hopping Channel used in the transmission 00230 /*! 00231 It stores in global 'channel' variable the Hopping Channel used in the transmission 00232 \return '0' on success, '1' otherwise 00233 */ 00234 uint8_t getHoppingChannel(); 00235 00236 //! It sets the time before send a Wake-up initializer 00237 /*! 00238 \param uint8_t timeHT_H : higher timer byte (range [0x00-0xFF]) 00239 \param uint8_t timeHT_L : lower timer byte (range [0x00-0xFF]) 00240 \return '0' on success, '1' otherwise 00241 */ 00242 uint8_t setTimeBeforeWakeUP(uint8_t timeHT_H, uint8_t timeHT_L); 00243 00244 //! It gets the time before send a Wake-up initializer 00245 /*! 00246 It stores in global 'timeBeforeWakeUP' variable the time before sending a Wake-up initializer 00247 \return '0' on success, '1' otherwise 00248 */ 00249 uint8_t getTimeBeforeWakeUP(); 00250 00251 //! It sets the amount of time the Wake-Up Init is sent 00252 /*! 00253 \param uint8_t timeLH : the amount of time the Wake-Up Init is sent (range [0x00-0xFF]) 00254 \return '0' on success, '1' otherwise 00255 */ 00256 uint8_t setTimeWakeUpInit(uint8_t timeLH); 00257 00258 //! It gets the amount of time the Wake-Up Init is sent 00259 /*! 00260 It stores in global 'timeWakeUpInit' variable the time before send a Wake-up initializer 00261 \return '0' on success, '1' otherwise 00262 */ 00263 uint8_t getTimeWakeUpInit(); 00264 00265 //! It sets the address mask 00266 /*! 00267 \param uint8_t mask_H : higher address mask byte (range [0x00-0xFF]) 00268 \param uint8_t mask_L : lower address mask byte (range [0x00-0xFF]) 00269 \return '0' on success, '1' otherwise 00270 */ 00271 uint8_t setAddressMask(uint8_t mask_H, uint8_t mask_L); 00272 00273 //! It gets the address mask 00274 /*! 00275 It stores in global 'addressMask' variable the address mask 00276 \return '0' on success, '1' otherwise 00277 */ 00278 uint8_t getAddressMask(); 00279 00280 //! It sets the pin Wake-UP ON/OFF 00281 /*! 00282 \param uint8_t pin : the pin to wake-up (range [0-1]) 00283 \return '0' on success, '1' otherwise 00284 */ 00285 uint8_t setPinWakeUP(uint8_t pin); 00286 00287 //! It gets the pin Wake-UP ON/OFF 00288 /*! 00289 It stores in global 'pin' variable the pin Wake-UP ON/OFF 00290 \return '0' on success, '1' otherwise 00291 */ 00292 uint8_t getPinWakeUP(); 00293 00294 //! It restores all configurable parameters to defaults values 00295 /*! 00296 \return '0' on success, '1' otherwise 00297 */ 00298 uint8_t restoreDefaults(); 00299 00300 //! It sets the random delay slots 00301 /*! 00302 \param uint8_t slot : the random delay slots (range [0x00-0xFF]) 00303 \return '0' on success, '1' otherwise 00304 */ 00305 uint8_t setDelaySlots(uint8_t slot); 00306 00307 //! It gets the random delay slots 00308 /*! 00309 It stores in global 'delaySlots' variable the random delay slots 00310 \return '0' on success, '1' otherwise 00311 */ 00312 uint8_t getDelaySlots(); 00313 00314 //! It sets the packetization timeout 00315 /*! 00316 \param uint8_t pack_H : higher timeout byte (range [0x00-0xFF]) 00317 \param uint8_t pack_L : lower timeout byte (range [0x00-0xFF]) 00318 \return '0' on success, '1' otherwise 00319 */ 00320 uint8_t setPacketTimeout(uint8_t pack_H, uint8_t pack_L); 00321 00322 //! It gets the packetization timeout 00323 /*! 00324 It stores in global 'packetTimeout' variable the random delay slots 00325 \return '0' on success, '1' otherwise 00326 */ 00327 uint8_t getPacketTimeout(); 00328 00329 //! It sets the time the output pin is active with the RSSI value 00330 /*! 00331 \param uint8_t rssiTime : the time the output pin is active with the RSSI value (range [0x00-0x7F]) 00332 \return '0' on success, '1' otherwise 00333 */ 00334 uint8_t setRSSItime(uint8_t rssiTime); 00335 00336 //! It gets the time the output pin is active with the RSSI value 00337 /*! 00338 It stores in global 'timeRSSI' variable the time the output pin is active with the RSSI value 00339 \return '0' on success, '1' otherwise 00340 */ 00341 uint8_t getRSSItime(); 00342 00343 //! It sets the number of retries that can be sent for a given packet RF 00344 /*! 00345 \param uint8_t retry : the number of retries that can be sent for a given packet RF (range [0x00-0xFF]) 00346 \return '0' on success, '1' otherwise 00347 */ 00348 uint8_t setRetries(uint8_t retry); 00349 00350 //! It gets the number of retries that can be sent for a given packet RF 00351 /*! 00352 It stores in global 'retries' variable the number of retries that can be sent for a given packet RF 00353 \return '0' on success, '1' otherwise 00354 */ 00355 uint8_t getRetries(); 00356 00357 //! It gets the last received packet RSSI 00358 /*! 00359 It stores in global 'RSSI' variable the last received packet RSSI 00360 \return '0' on success, '1' otherwise 00361 */ 00362 uint8_t getRSSIvalue(); 00363 00364 //! It sets the number of stop bits in the data packets 00365 /*! 00366 \param uint8_t stop : the number of stop bits in the data packets (range [0-1]) 00367 \return '0' on success, '1' otherwise 00368 */ 00369 uint8_t setStopBits(uint8_t stop); 00370 00371 //! It gets the number of stop bits in the data packets 00372 /*! 00373 It stores in global 'stopBits' variable the number of stop bits in the data packets 00374 \return '0' on success, '1' otherwise 00375 */ 00376 uint8_t getStopBits(); 00377 00378 //! It gets the 32b Higher Source Mac 00379 /*! 00380 It stores in global 'sourceMacHigh' variable the 32b Higher Source Mac 00381 \return '0' on success, '1' otherwise 00382 */ 00383 uint8_t getSourceMacHigh(); 00384 00385 //! It gets the 32b Lower Source Mac 00386 /*! 00387 It stores in global 'sourceMacLow' variable the 32b Lower Source Mac 00388 \return '0' on success, '1' otherwise 00389 */ 00390 uint8_t getSourceMacLow(); 00391 00392 //! It sets the Sleep Mode 00393 /*! 00394 \param uint8_t smode : the Sleep Mode (range [0-8]) 00395 \return '0' on success, '1' otherwise 00396 */ 00397 uint8_t setSleepMode(uint8_t smode); 00398 00399 //! It gets the Sleep Mode 00400 /*! 00401 It stores in global 'sleepMode' variable the Sleep Mode 00402 \return '0' on success, '1' otherwise 00403 */ 00404 uint8_t getSleepMode(); 00405 00406 //! It sets the amount of time the module is awake 00407 /*! 00408 \param uint8_t awake_H : higher awake time byte (range [0x00-0xFF]) 00409 \param uint8_t awake_L : lower awake time byte (range [0x10-0xFF]) 00410 \return '0' on success, '1' otherwise 00411 */ 00412 uint8_t setAwakeTime(uint8_t awake_H, uint8_t awake_L); 00413 00414 //! It gets the amount of time the module is awake 00415 /*! 00416 It stores in global 'awakeTime' variable the amount of time the module is awake 00417 \return '0' on success, '1' otherwise 00418 */ 00419 uint8_t getAwakeTime(); 00420 00421 //! It sets the time before sending again the initializer 00422 /*! 00423 \param uint8_t timeInit : the time before sending again the initializer (range [0x00-0xFF]) 00424 \return '0' on success, '1' otherwise 00425 */ 00426 uint8_t setTimeBeforeInit(uint8_t timeInit); 00427 00428 //! It gets the time before sending again the initializer 00429 /*! 00430 It stores in global 'timeBeforeInit' variable the time before sending again the initializer 00431 \return '0' on success, '1' otherwise 00432 */ 00433 uint8_t getTimeBeforeInit(); 00434 00435 //! It sets the transmit error count 00436 /*! 00437 \param uint8_t txerror_H : higher transmit error count byte (range [0x00-0xFF]) 00438 \param uint8_t txerror_L : lower transmit error count byte (range [0x00-0xFF]) 00439 \return '0' on success, '1' otherwise 00440 */ 00441 uint8_t setTransmitErrorCount(uint8_t txerror_H, uint8_t txerror_L); 00442 00443 //! It gets the transmit error count 00444 /*! 00445 It stores in global 'transmitErrorCount' variable the transmit error count 00446 \return '0' on success, '1' otherwise 00447 */ 00448 uint8_t getTransmitErrorCount(); 00449 00450 //! It sets the max number of bytes a module can send before waiting 00451 /*! 00452 \param uint8_t txlim_H : higher transmit limit byte (range [0x00-0xFF]) 00453 \param uint8_t txlim_L : lower transmit limit byte (range [0x00-0xFF]) 00454 \return '0' on success, '1' otherwise 00455 */ 00456 uint8_t setTransmitLimit(uint8_t txlim_H, uint8_t txlim_L); 00457 00458 //! It gets the max number of bytes a module can send before waiting 00459 /*! 00460 It stores in global 'transmitLimit' variable the max number of bytes a module can send before waiting 00461 \return '0' on success, '1' otherwise 00462 */ 00463 uint8_t getTransmitLimit(); 00464 00465 //! It gets the Firmware version 00466 /*! 00467 It stores in global 'firmwareVersion' variable the Firmware version 00468 \return '0' on success, '1' otherwise 00469 */ 00470 uint8_t getSoftVersion(); 00471 00472 //! It writes the parameters changed into non-volatil memory 00473 /*! 00474 \return '0' on success, '1' otherwise 00475 */ 00476 uint8_t writeValues(); 00477 00478 //! It sends data to other modules 00479 /*! 00480 It sends over the air its own MAC, data length and the data, so the receiver can identify the sender and store the data 00481 \param struct packetXSC* packet : a structure where the data to send is stored 00482 \return '0' on success, '1' otherwise 00483 */ 00484 uint8_t sendData(struct packetXSC* packet); 00485 00486 //! It reads data from other modules 00487 /*! 00488 It stores the sender MAC and data into the 'packet' structure 00489 \param struct packetXSC* packet : a structure where the received data and node information is stored 00490 \return '0' on success, '1' otherwise 00491 */ 00492 uint8_t readData(struct packetXSC* packet); 00493 00494 //! It is a transparent function. The user introduces an AT command within a string. 00495 /*! 00496 It stores in the global 'commandAT' variable the XBee answer to the command 00497 \param char *atcommand : the AT command to send to the XBee module 00498 \return '0' on success, '1' otherwise 00499 */ 00500 uint8_t sendCommandAT(char *atcommand); 00501 00502 //! It connects XBee, activating switch and opens the UART 00503 /*! 00504 \return '0' on success, '1' otherwise 00505 */ 00506 uint8_t ON(); 00507 00508 //! It sets the XBee to sleep, asserting PIN 9 00509 /*! 00510 \return '0' on success, '1' otherwise 00511 */ 00512 uint8_t sleep(); 00513 00514 //! It wakes up the XBee, de-asserting PIN 9 00515 /*! 00516 \return '0' on success, '1' otherwise 00517 */ 00518 uint8_t wake(); 00519 00520 //! Variable : the module Vendor ID 00521 /*! 00522 */ 00523 uint8_t vendorID[2]; 00524 00525 //! Variable : the Baud Rate used in the module 00526 /*! 00527 */ 00528 uint8_t baudRate; 00529 00530 //! Variable : the dBm level in the most recent packet 00531 /*! 00532 */ 00533 uint8_t valueRSSI; 00534 00535 //! Variable : the networking address of a module 00536 /*! 00537 */ 00538 uint8_t destAddress[2]; 00539 00540 //! Variable : the receive error count 00541 /*! 00542 */ 00543 uint8_t receiveErrorCount[2]; 00544 00545 //! Variable : the receive good count 00546 /*! 00547 */ 00548 uint8_t receiveGoodCount[2]; 00549 00550 //! Variable : the Hopping Channel used in the transmission 00551 /*! 00552 */ 00553 uint8_t channel; 00554 00555 //! Variable : the time before send a Wake-up initializer 00556 /*! 00557 */ 00558 uint8_t timeBeforeWakeUP[2]; 00559 00560 //! Variable : the amount of time the Wake-Up Init is sent 00561 /*! 00562 */ 00563 uint8_t timeWakeUpInit; 00564 00565 //! Variable : the address mask 00566 /*! 00567 */ 00568 uint8_t addressMask[2]; 00569 00570 //! Variable : the pin Wake-UP ON/OFF 00571 /*! 00572 */ 00573 uint8_t pinWakeUP; 00574 00575 //! Variable : the random delay slots 00576 /*! 00577 */ 00578 uint8_t delaySlots; 00579 00580 //! Variable : the packetization timeout 00581 /*! 00582 */ 00583 uint8_t packetTimeout[2]; 00584 00585 //! Variable : the time the output pin is active with the RSSI value 00586 /*! 00587 */ 00588 uint8_t timeRSSI; 00589 00590 //! Variable : the number of retries that can be sent for a given packet RF 00591 /*! 00592 */ 00593 uint8_t retries; 00594 00595 //! Variable : the last received packet RSSI 00596 /*! 00597 */ 00598 uint8_t RSSI; 00599 00600 //! Variable : the number of stop bits in the data packets 00601 /*! 00602 */ 00603 uint8_t stopBits; 00604 00605 //! Variable : the 32b Higher Source Mac 00606 /*! 00607 */ 00608 uint8_t sourceMacHigh[2]; 00609 00610 //! Variable : the 32b Lower Source Mac 00611 /*! 00612 */ 00613 uint8_t sourceMacLow[2]; 00614 00615 //! Variable : the Sleep Mode 00616 /*! 00617 */ 00618 uint8_t sleepMode; 00619 00620 //! Variable : the amount of time the module is awake 00621 /*! 00622 */ 00623 uint8_t awakeTime[2]; 00624 00625 //! Variable : the time before sending again the initializer 00626 /*! 00627 */ 00628 uint8_t timeBeforeInit; 00629 00630 //! Variable : the transmit error count 00631 /*! 00632 */ 00633 uint8_t transmitErrorCount[2]; 00634 00635 //! Variable : the max number of bytes a module can send before waiting 00636 /*! 00637 */ 00638 uint8_t transmitLimit[2]; 00639 00640 //! Variable : the Firmware version 00641 /*! 00642 */ 00643 uint8_t firmwareVersion[2]; 00644 00645 //! Variable : the XBee answer to the command 00646 /*! 00647 */ 00648 uint8_t commandAT[20]; 00649 00650 00651 private: 00652 00653 //! It generates a decimal number from an ASCII character which was a number 00654 /*! 00655 \param uint8_t conv1 : the ASCII number to convert 00656 \return the converted number 00657 */ 00658 uint8_t converter(uint8_t conv1); 00659 00660 //! It generates a decimal number from two ASCII characters which were numbers 00661 /*! 00662 \param uint8_t conv1 : the ASCII number first digit to convert 00663 \param uint8_t conv2 : the ASCII number second digit to convert 00664 \return the converted number 00665 */ 00666 uint8_t converter(uint8_t conv1, uint8_t conv2); 00667 00668 //! It generates a decimal number from three ASCII characters which were numbers 00669 /*! 00670 \param uint8_t conv1 : the ASCII number first digit to convert 00671 \param uint8_t conv2 : the ASCII number second digit to convert 00672 \param uint8_t conv3 : the ASCII number third digit to convert 00673 \return the converted number 00674 */ 00675 uint8_t converter(uint8_t conv1, uint8_t conv2, uint8_t conv3); 00676 00677 //! It checks if the XBee module answer a 'OK' message 00678 /*! 00679 \return '0' if OK returned, '1' otherwise 00680 */ 00681 void clean(); 00682 00683 //! It cleans the unwanted bytes returned by XBee module 00684 /*! 00685 \return '0' on success, '1' otherwise 00686 */ 00687 uint8_t check(); 00688 00689 //! Variable : counter used in the library 00690 /*! 00691 */ 00692 uint8_t it; 00693 00694 //! Variable : protocol used (XBEE_XSC in this case) 00695 /*! 00696 */ 00697 uint8_t protocol; 00698 00699 //! Variable : frequency used (FREQ900M in this case) 00700 /*! 00701 */ 00702 uint8_t freq; 00703 00704 //! Variable : model used (PRO in this case) 00705 /*! 00706 */ 00707 uint8_t model; 00708 }; 00709 00710 extern WaspXBeeXSC xbeeXSC; 00711 00712 #endif
1.5.6