This is an experimental driver for the XBee 900 HP pro module's SPI connection. This driver is unfinished and stability is not guaranteed. Use with caution.

Dependents:   Sentinel_BASE Sentinel_NODE

Committer:
ottaviano3
Date:
Wed Apr 29 23:58:46 2015 +0000
Revision:
4:4083baa871fb
Parent:
3:3c3707b0f5cd
Child:
5:c8bb6b5d7fa0
null terminated arrays;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ottaviano3 0:8c8a8244e590 1 #include "xbee900hp.h"
ottaviano3 0:8c8a8244e590 2
ottaviano3 0:8c8a8244e590 3 /**
ottaviano3 0:8c8a8244e590 4 * Initialize the xBee Module
ottaviano3 0:8c8a8244e590 5 */
ottaviano3 1:b97d46c5d7ce 6 xbee900hp::xbee900hp(PinName pin_mosi,PinName pin_miso,PinName pin_sck,PinName pin_attn, PinName pin_rst)
ottaviano3 1:b97d46c5d7ce 7 : _pin_rst(pin_rst), _pin_attn(pin_attn), _xbeespi(pin_mosi,pin_miso,pin_sck)
ottaviano3 0:8c8a8244e590 8 {
ottaviano3 0:8c8a8244e590 9 _xbeespi.format(8,0);
ottaviano3 0:8c8a8244e590 10 _xbeespi.frequency(1000000);
ottaviano3 2:7f4ddf710a44 11
ottaviano3 2:7f4ddf710a44 12 reset();
ottaviano3 0:8c8a8244e590 13 }
ottaviano3 0:8c8a8244e590 14
ottaviano3 0:8c8a8244e590 15 /**
ottaviano3 0:8c8a8244e590 16 * Destructor
ottaviano3 0:8c8a8244e590 17 */
ottaviano3 0:8c8a8244e590 18 xbee900hp::~xbee900hp() {}
ottaviano3 0:8c8a8244e590 19
ottaviano3 1:b97d46c5d7ce 20 /**
ottaviano3 0:8c8a8244e590 21 * Reset xBee to SPI mode
ottaviano3 0:8c8a8244e590 22 */
ottaviano3 0:8c8a8244e590 23 void xbee900hp::reset()
ottaviano3 0:8c8a8244e590 24 {
ottaviano3 0:8c8a8244e590 25 _pin_rst = 0;
ottaviano3 0:8c8a8244e590 26 // Minimum pulse is 1ms
ottaviano3 0:8c8a8244e590 27 wait_ms(1);
ottaviano3 0:8c8a8244e590 28 _pin_rst = 1;
ottaviano3 0:8c8a8244e590 29 // wait for module to come back online
ottaviano3 0:8c8a8244e590 30 wait_ms(500);
ottaviano3 0:8c8a8244e590 31 }
ottaviano3 0:8c8a8244e590 32
ottaviano3 0:8c8a8244e590 33 /**
ottaviano3 0:8c8a8244e590 34 * Send packet out on RF
ottaviano3 0:8c8a8244e590 35 */
ottaviano3 0:8c8a8244e590 36 void xbee900hp::sendPacket(char* data, unsigned int length)
ottaviano3 0:8c8a8244e590 37 {
ottaviano3 0:8c8a8244e590 38 // checksum
ottaviano3 0:8c8a8244e590 39 unsigned int checksum;
ottaviano3 0:8c8a8244e590 40 unsigned int checksumsub = 0;
ottaviano3 0:8c8a8244e590 41
ottaviano3 0:8c8a8244e590 42 // start char
ottaviano3 0:8c8a8244e590 43 _xbeespi.write(0x7E);
ottaviano3 0:8c8a8244e590 44 // lenght
ottaviano3 0:8c8a8244e590 45 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 46
ottaviano3 0:8c8a8244e590 47 unsigned int totallength = 14 + length - 1;
ottaviano3 0:8c8a8244e590 48 _xbeespi.write(totallength);
ottaviano3 0:8c8a8244e590 49
ottaviano3 0:8c8a8244e590 50 // frame delimter
ottaviano3 0:8c8a8244e590 51 _xbeespi.write(0x10);
ottaviano3 0:8c8a8244e590 52 checksumsub += 0x10;
ottaviano3 0:8c8a8244e590 53 // id for later reference 0 = no id
ottaviano3 0:8c8a8244e590 54 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 55
ottaviano3 0:8c8a8244e590 56 // destination address
ottaviano3 0:8c8a8244e590 57 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 58 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 59 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 60 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 61 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 62 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 63 _xbeespi.write(0xFF);
ottaviano3 0:8c8a8244e590 64 checksumsub += 0xFF;
ottaviano3 0:8c8a8244e590 65 _xbeespi.write(0xFF);
ottaviano3 0:8c8a8244e590 66 checksumsub += 0xFF;
ottaviano3 0:8c8a8244e590 67
ottaviano3 0:8c8a8244e590 68 // reserved field, dont change
ottaviano3 0:8c8a8244e590 69 _xbeespi.write(0xFF);
ottaviano3 0:8c8a8244e590 70 checksumsub += 0xFF;
ottaviano3 0:8c8a8244e590 71 _xbeespi.write(0xFE);
ottaviano3 0:8c8a8244e590 72 checksumsub += 0xFE;
ottaviano3 0:8c8a8244e590 73
ottaviano3 0:8c8a8244e590 74 //bcast radius
ottaviano3 0:8c8a8244e590 75 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 76
ottaviano3 0:8c8a8244e590 77 //transmit options 0x00 to disable ACK
ottaviano3 0:8c8a8244e590 78 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 79
ottaviano3 0:8c8a8244e590 80 // dat data
ottaviano3 0:8c8a8244e590 81 for (int i = 0; i < (length - 1); i++) {
ottaviano3 0:8c8a8244e590 82 _xbeespi.write(*data);
ottaviano3 0:8c8a8244e590 83 checksumsub += (*(data++));
ottaviano3 0:8c8a8244e590 84 }
ottaviano3 0:8c8a8244e590 85
ottaviano3 0:8c8a8244e590 86
ottaviano3 0:8c8a8244e590 87 // Calculate checksum
ottaviano3 0:8c8a8244e590 88 checksumsub = checksumsub & 0xFF;
ottaviano3 0:8c8a8244e590 89 checksum = 0xFF - checksumsub;
ottaviano3 0:8c8a8244e590 90
ottaviano3 0:8c8a8244e590 91 // finally write checksum
ottaviano3 0:8c8a8244e590 92 _xbeespi.write(checksum);
ottaviano3 1:b97d46c5d7ce 93 }
ottaviano3 1:b97d46c5d7ce 94
ottaviano3 2:7f4ddf710a44 95 int xbee900hp::readPacket(char* data) {
ottaviano3 2:7f4ddf710a44 96 unsigned int temp1;
ottaviano3 2:7f4ddf710a44 97 unsigned int temp2;
ottaviano3 2:7f4ddf710a44 98
ottaviano3 2:7f4ddf710a44 99 unsigned int checksumsub = 0;
ottaviano3 2:7f4ddf710a44 100 unsigned int checksum;
ottaviano3 2:7f4ddf710a44 101
ottaviano3 2:7f4ddf710a44 102 // get first vars.
ottaviano3 2:7f4ddf710a44 103 temp1 = _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 104 if (temp1 != 0x7E) {
ottaviano3 2:7f4ddf710a44 105 // drop packet
ottaviano3 2:7f4ddf710a44 106 return 1;
ottaviano3 2:7f4ddf710a44 107 }
ottaviano3 2:7f4ddf710a44 108 // Get length of message
ottaviano3 2:7f4ddf710a44 109 temp1 = _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 110 temp2 = _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 111
ottaviano3 2:7f4ddf710a44 112 // Get total length
ottaviano3 2:7f4ddf710a44 113 unsigned int length = (temp1<<8) | temp2;
ottaviano3 2:7f4ddf710a44 114
ottaviano3 2:7f4ddf710a44 115 // Next read frame type to ensure it is an RX packet.
ottaviano3 2:7f4ddf710a44 116 temp1 = _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 117 if (temp1 != 0x90) {
ottaviano3 2:7f4ddf710a44 118 // drop packet
ottaviano3 2:7f4ddf710a44 119 return 1;
ottaviano3 2:7f4ddf710a44 120 }
ottaviano3 2:7f4ddf710a44 121 checksumsub += temp1;
ottaviano3 2:7f4ddf710a44 122
ottaviano3 2:7f4ddf710a44 123 // in our case we dont care about source address this should be modified to extract source address if needed.
ottaviano3 2:7f4ddf710a44 124 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 125 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 126 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 127 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 128 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 129 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 130 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 131 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 132 // reserved field, we dont care about except for checksum
ottaviano3 2:7f4ddf710a44 133 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 134 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 135
ottaviano3 2:7f4ddf710a44 136 // recive options we also dont care though
ottaviano3 2:7f4ddf710a44 137 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 138
ottaviano3 2:7f4ddf710a44 139 // Now for the sweet sweet data.
ottaviano3 2:7f4ddf710a44 140 for (int i = 0; i<(length-12); i++) {
ottaviano3 2:7f4ddf710a44 141 *data = _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 142 checksumsub += *(data++);
ottaviano3 2:7f4ddf710a44 143 }
ottaviano3 2:7f4ddf710a44 144 // Null terminate char array.
ottaviano3 4:4083baa871fb 145 *data = '\0';
ottaviano3 2:7f4ddf710a44 146
ottaviano3 2:7f4ddf710a44 147 // Get that salty checksum
ottaviano3 2:7f4ddf710a44 148 temp1 = _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 149
ottaviano3 2:7f4ddf710a44 150 checksumsub = checksumsub & 0xFF;
ottaviano3 2:7f4ddf710a44 151 checksum = 0xFF - checksumsub;
ottaviano3 2:7f4ddf710a44 152
ottaviano3 2:7f4ddf710a44 153 // Check the checksum
ottaviano3 2:7f4ddf710a44 154 if (temp1 != checksum) {
ottaviano3 2:7f4ddf710a44 155 // Checksum failure, flag to discard packet
ottaviano3 2:7f4ddf710a44 156 return 1;
ottaviano3 2:7f4ddf710a44 157 }
ottaviano3 2:7f4ddf710a44 158
ottaviano3 2:7f4ddf710a44 159 return 0;
ottaviano3 2:7f4ddf710a44 160 }
ottaviano3 2:7f4ddf710a44 161
ottaviano3 2:7f4ddf710a44 162 int xbee900hp::attn() {
ottaviano3 2:7f4ddf710a44 163 return _pin_attn;
ottaviano3 2:7f4ddf710a44 164 }
ottaviano3 2:7f4ddf710a44 165
ottaviano3 1:b97d46c5d7ce 166 void xbee900hp::writeSetting(char command[2], unsigned int value) {
ottaviano3 1:b97d46c5d7ce 167 // checksum Variables
ottaviano3 1:b97d46c5d7ce 168 unsigned int checksum = 0;
ottaviano3 1:b97d46c5d7ce 169 unsigned int checksumsub = 0;
ottaviano3 1:b97d46c5d7ce 170
ottaviano3 1:b97d46c5d7ce 171 // Start config, send frames for SPI pins enable
ottaviano3 1:b97d46c5d7ce 172 _xbeespi.write(0x7E);
ottaviano3 1:b97d46c5d7ce 173 _xbeespi.write(0x00);
ottaviano3 1:b97d46c5d7ce 174 _xbeespi.write(0x05);
ottaviano3 1:b97d46c5d7ce 175
ottaviano3 1:b97d46c5d7ce 176 // Frame type (config)
ottaviano3 1:b97d46c5d7ce 177 _xbeespi.write(0x08);
ottaviano3 1:b97d46c5d7ce 178 checksumsub += 0x08;
ottaviano3 1:b97d46c5d7ce 179 _xbeespi.write(0x00);
ottaviano3 1:b97d46c5d7ce 180
ottaviano3 1:b97d46c5d7ce 181
ottaviano3 1:b97d46c5d7ce 182 // AT Command
ottaviano3 1:b97d46c5d7ce 183 for (int i = 0; i < 2; i++) {
ottaviano3 1:b97d46c5d7ce 184 _xbeespi.write(*command);
ottaviano3 1:b97d46c5d7ce 185 checksumsub += (*(command++));
ottaviano3 1:b97d46c5d7ce 186 }
ottaviano3 1:b97d46c5d7ce 187
ottaviano3 1:b97d46c5d7ce 188 // Value to set
ottaviano3 1:b97d46c5d7ce 189 _xbeespi.write(value);
ottaviano3 1:b97d46c5d7ce 190 checksumsub += value;
ottaviano3 1:b97d46c5d7ce 191
ottaviano3 1:b97d46c5d7ce 192 // Calculate checksum
ottaviano3 1:b97d46c5d7ce 193 checksumsub = checksumsub & 0xFF;
ottaviano3 1:b97d46c5d7ce 194 checksum = 0xFF - checksumsub;
ottaviano3 1:b97d46c5d7ce 195
ottaviano3 1:b97d46c5d7ce 196 // finally write checksum
ottaviano3 1:b97d46c5d7ce 197 _xbeespi.write(checksum);
ottaviano3 3:3c3707b0f5cd 198 }
ottaviano3 3:3c3707b0f5cd 199
ottaviano3 3:3c3707b0f5cd 200 int xbee900hp::getSerial(char* serialnumber) {
ottaviano3 3:3c3707b0f5cd 201 ///////////////////////////////////////////////////Send portion
ottaviano3 3:3c3707b0f5cd 202 // checksum Variables
ottaviano3 3:3c3707b0f5cd 203 unsigned int checksum = 0;
ottaviano3 3:3c3707b0f5cd 204 unsigned int checksumsub = 0;
ottaviano3 3:3c3707b0f5cd 205
ottaviano3 3:3c3707b0f5cd 206 // Write frame to return serial high and low.s
ottaviano3 3:3c3707b0f5cd 207
ottaviano3 3:3c3707b0f5cd 208 // Start config, send frames for SPI pins enable
ottaviano3 3:3c3707b0f5cd 209 _xbeespi.write(0x7E);
ottaviano3 3:3c3707b0f5cd 210 _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 211 _xbeespi.write(0x04);
ottaviano3 3:3c3707b0f5cd 212
ottaviano3 3:3c3707b0f5cd 213 // Frame type (config)
ottaviano3 3:3c3707b0f5cd 214 _xbeespi.write(0x08);
ottaviano3 3:3c3707b0f5cd 215 checksumsub += 0x08;
ottaviano3 3:3c3707b0f5cd 216 _xbeespi.write(0x52);
ottaviano3 3:3c3707b0f5cd 217 checksumsub += 0x52;
ottaviano3 3:3c3707b0f5cd 218
ottaviano3 3:3c3707b0f5cd 219 // Setting wanted
ottaviano3 3:3c3707b0f5cd 220 _xbeespi.write('S');
ottaviano3 3:3c3707b0f5cd 221 checksumsub += 'S';
ottaviano3 3:3c3707b0f5cd 222 _xbeespi.write('H');
ottaviano3 3:3c3707b0f5cd 223 checksumsub += 'H';
ottaviano3 3:3c3707b0f5cd 224
ottaviano3 3:3c3707b0f5cd 225 // Calculate checksum
ottaviano3 3:3c3707b0f5cd 226 checksumsub = checksumsub & 0xFF;
ottaviano3 3:3c3707b0f5cd 227 checksum = 0xFF - checksumsub;
ottaviano3 3:3c3707b0f5cd 228 // finally write checksum
ottaviano3 3:3c3707b0f5cd 229 _xbeespi.write(checksum);
ottaviano3 3:3c3707b0f5cd 230
ottaviano3 3:3c3707b0f5cd 231 //////////////////////////////////////RECIEVE PORTION
ottaviano3 3:3c3707b0f5cd 232 // Block until xbee replys
ottaviano3 3:3c3707b0f5cd 233 while (_pin_attn != 0) {}
ottaviano3 3:3c3707b0f5cd 234
ottaviano3 3:3c3707b0f5cd 235 // Containers for read values
ottaviano3 3:3c3707b0f5cd 236 char temp1 = 0;
ottaviano3 3:3c3707b0f5cd 237 char temp2 = 0;
ottaviano3 3:3c3707b0f5cd 238
ottaviano3 3:3c3707b0f5cd 239 // reset checksum to zero.
ottaviano3 3:3c3707b0f5cd 240 checksum = 0;
ottaviano3 3:3c3707b0f5cd 241 checksumsub = 0;
ottaviano3 3:3c3707b0f5cd 242
ottaviano3 3:3c3707b0f5cd 243 // get start byte
ottaviano3 3:3c3707b0f5cd 244 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 245 if (temp1 != 0x7E) {
ottaviano3 3:3c3707b0f5cd 246 // drop packet
ottaviano3 3:3c3707b0f5cd 247 return 1;
ottaviano3 3:3c3707b0f5cd 248 }
ottaviano3 3:3c3707b0f5cd 249 // Get length of message
ottaviano3 3:3c3707b0f5cd 250 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 251 temp2 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 252
ottaviano3 3:3c3707b0f5cd 253 // Get total length
ottaviano3 3:3c3707b0f5cd 254 unsigned int length = (temp1<<8) | temp2;
ottaviano3 3:3c3707b0f5cd 255
ottaviano3 3:3c3707b0f5cd 256 // Next read frame type to ensure it is an response packet.
ottaviano3 3:3c3707b0f5cd 257 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 258 if (temp1 != 0x88) {
ottaviano3 3:3c3707b0f5cd 259 // drop packet
ottaviano3 3:3c3707b0f5cd 260 return 1;
ottaviano3 3:3c3707b0f5cd 261 }
ottaviano3 3:3c3707b0f5cd 262 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 263
ottaviano3 3:3c3707b0f5cd 264 // get response frame id
ottaviano3 3:3c3707b0f5cd 265 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 266 if (temp1 != 0x52) {
ottaviano3 3:3c3707b0f5cd 267 // drop packet
ottaviano3 3:3c3707b0f5cd 268 return 1;
ottaviano3 3:3c3707b0f5cd 269 }
ottaviano3 3:3c3707b0f5cd 270 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 271
ottaviano3 3:3c3707b0f5cd 272 // get at response parameter
ottaviano3 3:3c3707b0f5cd 273 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 274 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 275 temp2 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 276 checksumsub += temp2;
ottaviano3 3:3c3707b0f5cd 277
ottaviano3 3:3c3707b0f5cd 278 if ((temp1 != 'S') || (temp2 != 'H')) {
ottaviano3 3:3c3707b0f5cd 279 return 1;
ottaviano3 3:3c3707b0f5cd 280 // drop
ottaviano3 3:3c3707b0f5cd 281 }
ottaviano3 3:3c3707b0f5cd 282
ottaviano3 3:3c3707b0f5cd 283 // Check OK flag
ottaviano3 3:3c3707b0f5cd 284 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 285 if (temp1 != 0x00) {
ottaviano3 3:3c3707b0f5cd 286 return 1;
ottaviano3 3:3c3707b0f5cd 287 // drop
ottaviano3 3:3c3707b0f5cd 288 }
ottaviano3 3:3c3707b0f5cd 289 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 290
ottaviano3 3:3c3707b0f5cd 291 // Now for the sweet sweet data.
ottaviano3 3:3c3707b0f5cd 292 for (int i = 0; i<(length-5); i++) {
ottaviano3 3:3c3707b0f5cd 293 *serialnumber = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 294 checksumsub += *(serialnumber++);
ottaviano3 3:3c3707b0f5cd 295 }
ottaviano3 3:3c3707b0f5cd 296
ottaviano3 3:3c3707b0f5cd 297 // Get that salty checksum
ottaviano3 3:3c3707b0f5cd 298 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 299
ottaviano3 3:3c3707b0f5cd 300 checksumsub = checksumsub & 0xFF;
ottaviano3 3:3c3707b0f5cd 301 checksum = 0xFF - checksumsub;
ottaviano3 3:3c3707b0f5cd 302
ottaviano3 3:3c3707b0f5cd 303 // Check the checksum
ottaviano3 3:3c3707b0f5cd 304 if (temp1 != checksum) {
ottaviano3 3:3c3707b0f5cd 305 // Checksum failure, flag to discard packet
ottaviano3 3:3c3707b0f5cd 306 return 1;
ottaviano3 3:3c3707b0f5cd 307 }
ottaviano3 3:3c3707b0f5cd 308
ottaviano3 3:3c3707b0f5cd 309 ////////////////////////////////////////////////////////////////////////
ottaviano3 3:3c3707b0f5cd 310 ///////////////////////////////////////////////////Send portion
ottaviano3 3:3c3707b0f5cd 311 // checksum Variables
ottaviano3 3:3c3707b0f5cd 312 checksum = 0;
ottaviano3 3:3c3707b0f5cd 313 checksumsub = 0;
ottaviano3 3:3c3707b0f5cd 314
ottaviano3 3:3c3707b0f5cd 315 // Write frame to return serial high and low.s
ottaviano3 3:3c3707b0f5cd 316
ottaviano3 3:3c3707b0f5cd 317 // Start config, send frames for SPI pins enable
ottaviano3 3:3c3707b0f5cd 318 _xbeespi.write(0x7E);
ottaviano3 3:3c3707b0f5cd 319 _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 320 _xbeespi.write(0x04);
ottaviano3 3:3c3707b0f5cd 321
ottaviano3 3:3c3707b0f5cd 322 // Frame type (config)
ottaviano3 3:3c3707b0f5cd 323 _xbeespi.write(0x08);
ottaviano3 3:3c3707b0f5cd 324 checksumsub += 0x08;
ottaviano3 3:3c3707b0f5cd 325 _xbeespi.write(0x52);
ottaviano3 3:3c3707b0f5cd 326 checksumsub += 0x52;
ottaviano3 3:3c3707b0f5cd 327
ottaviano3 3:3c3707b0f5cd 328 // Setting wanted
ottaviano3 3:3c3707b0f5cd 329 _xbeespi.write('S');
ottaviano3 3:3c3707b0f5cd 330 checksumsub += 'S';
ottaviano3 3:3c3707b0f5cd 331 _xbeespi.write('L');
ottaviano3 3:3c3707b0f5cd 332 checksumsub += 'L';
ottaviano3 3:3c3707b0f5cd 333
ottaviano3 3:3c3707b0f5cd 334 // Calculate checksum
ottaviano3 3:3c3707b0f5cd 335 checksumsub = checksumsub & 0xFF;
ottaviano3 3:3c3707b0f5cd 336 checksum = 0xFF - checksumsub;
ottaviano3 3:3c3707b0f5cd 337 // finally write checksum
ottaviano3 3:3c3707b0f5cd 338 _xbeespi.write(checksum);
ottaviano3 3:3c3707b0f5cd 339
ottaviano3 3:3c3707b0f5cd 340 //////////////////////////////////////RECIEVE PORTION
ottaviano3 3:3c3707b0f5cd 341 // Block until xbee replys
ottaviano3 3:3c3707b0f5cd 342 while (_pin_attn != 0) {}
ottaviano3 3:3c3707b0f5cd 343
ottaviano3 3:3c3707b0f5cd 344 // Containers for read values
ottaviano3 3:3c3707b0f5cd 345 temp1 = 0;
ottaviano3 3:3c3707b0f5cd 346 temp2 = 0;
ottaviano3 3:3c3707b0f5cd 347
ottaviano3 3:3c3707b0f5cd 348 // reset checksum to zero.
ottaviano3 3:3c3707b0f5cd 349 checksum = 0;
ottaviano3 3:3c3707b0f5cd 350 checksumsub = 0;
ottaviano3 3:3c3707b0f5cd 351
ottaviano3 3:3c3707b0f5cd 352 // get start byte
ottaviano3 3:3c3707b0f5cd 353 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 354 if (temp1 != 0x7E) {
ottaviano3 3:3c3707b0f5cd 355 // drop packet
ottaviano3 3:3c3707b0f5cd 356 return 1;
ottaviano3 3:3c3707b0f5cd 357 }
ottaviano3 3:3c3707b0f5cd 358 // Get length of message
ottaviano3 3:3c3707b0f5cd 359 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 360 temp2 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 361
ottaviano3 3:3c3707b0f5cd 362 // Get total length
ottaviano3 3:3c3707b0f5cd 363 length = (temp1<<8) | temp2;
ottaviano3 3:3c3707b0f5cd 364
ottaviano3 3:3c3707b0f5cd 365 // Next read frame type to ensure it is an response packet.
ottaviano3 3:3c3707b0f5cd 366 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 367 if (temp1 != 0x88) {
ottaviano3 3:3c3707b0f5cd 368 // drop packet
ottaviano3 3:3c3707b0f5cd 369 return 1;
ottaviano3 3:3c3707b0f5cd 370 }
ottaviano3 3:3c3707b0f5cd 371 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 372
ottaviano3 3:3c3707b0f5cd 373 // get response frame id
ottaviano3 3:3c3707b0f5cd 374 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 375 if (temp1 != 0x52) {
ottaviano3 3:3c3707b0f5cd 376 // drop packet
ottaviano3 3:3c3707b0f5cd 377 return 1;
ottaviano3 3:3c3707b0f5cd 378 }
ottaviano3 3:3c3707b0f5cd 379 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 380
ottaviano3 3:3c3707b0f5cd 381 // get at response parameter
ottaviano3 3:3c3707b0f5cd 382 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 383 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 384 temp2 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 385 checksumsub += temp2;
ottaviano3 3:3c3707b0f5cd 386
ottaviano3 3:3c3707b0f5cd 387 if ((temp1 != 'S') || (temp2 != 'L')) {
ottaviano3 3:3c3707b0f5cd 388 return 1;
ottaviano3 3:3c3707b0f5cd 389 // drop
ottaviano3 3:3c3707b0f5cd 390 }
ottaviano3 3:3c3707b0f5cd 391
ottaviano3 3:3c3707b0f5cd 392 // Check OK flag
ottaviano3 3:3c3707b0f5cd 393 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 394 if (temp1 != 0x00) {
ottaviano3 3:3c3707b0f5cd 395 return 1;
ottaviano3 3:3c3707b0f5cd 396 // drop
ottaviano3 3:3c3707b0f5cd 397 }
ottaviano3 3:3c3707b0f5cd 398 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 399
ottaviano3 3:3c3707b0f5cd 400 // Now for the sweet sweet data.
ottaviano3 3:3c3707b0f5cd 401 for (int i = 0; i<(length-5); i++) {
ottaviano3 3:3c3707b0f5cd 402 *serialnumber = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 403 checksumsub += *(serialnumber++);
ottaviano3 3:3c3707b0f5cd 404 }
ottaviano3 3:3c3707b0f5cd 405 // Null terminate char array.
ottaviano3 4:4083baa871fb 406 *serialnumber = '\0';
ottaviano3 3:3c3707b0f5cd 407
ottaviano3 3:3c3707b0f5cd 408 // Get that salty checksum
ottaviano3 3:3c3707b0f5cd 409 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 410
ottaviano3 3:3c3707b0f5cd 411 checksumsub = checksumsub & 0xFF;
ottaviano3 3:3c3707b0f5cd 412 checksum = 0xFF - checksumsub;
ottaviano3 3:3c3707b0f5cd 413
ottaviano3 3:3c3707b0f5cd 414 // Check the checksum
ottaviano3 3:3c3707b0f5cd 415 if (temp1 != checksum) {
ottaviano3 3:3c3707b0f5cd 416 // Checksum failure, flag to discard packet
ottaviano3 3:3c3707b0f5cd 417 return 1;
ottaviano3 3:3c3707b0f5cd 418 }
ottaviano3 3:3c3707b0f5cd 419
ottaviano3 3:3c3707b0f5cd 420 return 0;
ottaviano3 3:3c3707b0f5cd 421
ottaviano3 0:8c8a8244e590 422 }