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:
Thu Apr 30 17:01:46 2015 +0000
Revision:
6:3873db4a0164
Parent:
5:c8bb6b5d7fa0
Child:
7:3cb67634fa4e
added function to clear buffer;

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 5:c8bb6b5d7fa0 77 //transmit options 0xC0 to enable digimesh
ottaviano3 5:c8bb6b5d7fa0 78 _xbeespi.write(0xC0);
ottaviano3 5:c8bb6b5d7fa0 79 checksumsub += 0xC0;
ottaviano3 0:8c8a8244e590 80
ottaviano3 0:8c8a8244e590 81 // dat data
ottaviano3 0:8c8a8244e590 82 for (int i = 0; i < (length - 1); i++) {
ottaviano3 0:8c8a8244e590 83 _xbeespi.write(*data);
ottaviano3 0:8c8a8244e590 84 checksumsub += (*(data++));
ottaviano3 0:8c8a8244e590 85 }
ottaviano3 0:8c8a8244e590 86
ottaviano3 0:8c8a8244e590 87
ottaviano3 0:8c8a8244e590 88 // Calculate checksum
ottaviano3 0:8c8a8244e590 89 checksumsub = checksumsub & 0xFF;
ottaviano3 0:8c8a8244e590 90 checksum = 0xFF - checksumsub;
ottaviano3 0:8c8a8244e590 91
ottaviano3 0:8c8a8244e590 92 // finally write checksum
ottaviano3 0:8c8a8244e590 93 _xbeespi.write(checksum);
ottaviano3 1:b97d46c5d7ce 94 }
ottaviano3 1:b97d46c5d7ce 95
ottaviano3 2:7f4ddf710a44 96 int xbee900hp::readPacket(char* data) {
ottaviano3 2:7f4ddf710a44 97 unsigned int temp1;
ottaviano3 2:7f4ddf710a44 98 unsigned int temp2;
ottaviano3 2:7f4ddf710a44 99
ottaviano3 2:7f4ddf710a44 100 unsigned int checksumsub = 0;
ottaviano3 2:7f4ddf710a44 101 unsigned int checksum;
ottaviano3 2:7f4ddf710a44 102
ottaviano3 2:7f4ddf710a44 103 // get first vars.
ottaviano3 2:7f4ddf710a44 104 temp1 = _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 105 if (temp1 != 0x7E) {
ottaviano3 2:7f4ddf710a44 106 // drop packet
ottaviano3 2:7f4ddf710a44 107 return 1;
ottaviano3 2:7f4ddf710a44 108 }
ottaviano3 2:7f4ddf710a44 109 // Get length of message
ottaviano3 2:7f4ddf710a44 110 temp1 = _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 111 temp2 = _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 112
ottaviano3 2:7f4ddf710a44 113 // Get total length
ottaviano3 2:7f4ddf710a44 114 unsigned int length = (temp1<<8) | temp2;
ottaviano3 2:7f4ddf710a44 115
ottaviano3 2:7f4ddf710a44 116 // Next read frame type to ensure it is an RX packet.
ottaviano3 2:7f4ddf710a44 117 temp1 = _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 118 if (temp1 != 0x90) {
ottaviano3 2:7f4ddf710a44 119 // drop packet
ottaviano3 2:7f4ddf710a44 120 return 1;
ottaviano3 2:7f4ddf710a44 121 }
ottaviano3 2:7f4ddf710a44 122 checksumsub += temp1;
ottaviano3 2:7f4ddf710a44 123
ottaviano3 2:7f4ddf710a44 124 // in our case we dont care about source address this should be modified to extract source address if needed.
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 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 133 // reserved field, we dont care about except for checksum
ottaviano3 2:7f4ddf710a44 134 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 135 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 136
ottaviano3 2:7f4ddf710a44 137 // recive options we also dont care though
ottaviano3 2:7f4ddf710a44 138 checksumsub += _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 139
ottaviano3 2:7f4ddf710a44 140 // Now for the sweet sweet data.
ottaviano3 2:7f4ddf710a44 141 for (int i = 0; i<(length-12); i++) {
ottaviano3 2:7f4ddf710a44 142 *data = _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 143 checksumsub += *(data++);
ottaviano3 2:7f4ddf710a44 144 }
ottaviano3 2:7f4ddf710a44 145 // Null terminate char array.
ottaviano3 4:4083baa871fb 146 *data = '\0';
ottaviano3 2:7f4ddf710a44 147
ottaviano3 2:7f4ddf710a44 148 // Get that salty checksum
ottaviano3 2:7f4ddf710a44 149 temp1 = _xbeespi.write(0x00);
ottaviano3 2:7f4ddf710a44 150
ottaviano3 2:7f4ddf710a44 151 checksumsub = checksumsub & 0xFF;
ottaviano3 2:7f4ddf710a44 152 checksum = 0xFF - checksumsub;
ottaviano3 2:7f4ddf710a44 153
ottaviano3 2:7f4ddf710a44 154 // Check the checksum
ottaviano3 2:7f4ddf710a44 155 if (temp1 != checksum) {
ottaviano3 2:7f4ddf710a44 156 // Checksum failure, flag to discard packet
ottaviano3 2:7f4ddf710a44 157 return 1;
ottaviano3 2:7f4ddf710a44 158 }
ottaviano3 2:7f4ddf710a44 159
ottaviano3 2:7f4ddf710a44 160 return 0;
ottaviano3 2:7f4ddf710a44 161 }
ottaviano3 2:7f4ddf710a44 162
ottaviano3 2:7f4ddf710a44 163 int xbee900hp::attn() {
ottaviano3 2:7f4ddf710a44 164 return _pin_attn;
ottaviano3 2:7f4ddf710a44 165 }
ottaviano3 2:7f4ddf710a44 166
ottaviano3 1:b97d46c5d7ce 167 void xbee900hp::writeSetting(char command[2], unsigned int value) {
ottaviano3 1:b97d46c5d7ce 168 // checksum Variables
ottaviano3 1:b97d46c5d7ce 169 unsigned int checksum = 0;
ottaviano3 1:b97d46c5d7ce 170 unsigned int checksumsub = 0;
ottaviano3 1:b97d46c5d7ce 171
ottaviano3 1:b97d46c5d7ce 172 // Start config, send frames for SPI pins enable
ottaviano3 1:b97d46c5d7ce 173 _xbeespi.write(0x7E);
ottaviano3 1:b97d46c5d7ce 174 _xbeespi.write(0x00);
ottaviano3 1:b97d46c5d7ce 175 _xbeespi.write(0x05);
ottaviano3 1:b97d46c5d7ce 176
ottaviano3 1:b97d46c5d7ce 177 // Frame type (config)
ottaviano3 1:b97d46c5d7ce 178 _xbeespi.write(0x08);
ottaviano3 1:b97d46c5d7ce 179 checksumsub += 0x08;
ottaviano3 1:b97d46c5d7ce 180 _xbeespi.write(0x00);
ottaviano3 1:b97d46c5d7ce 181
ottaviano3 1:b97d46c5d7ce 182
ottaviano3 1:b97d46c5d7ce 183 // AT Command
ottaviano3 1:b97d46c5d7ce 184 for (int i = 0; i < 2; i++) {
ottaviano3 1:b97d46c5d7ce 185 _xbeespi.write(*command);
ottaviano3 1:b97d46c5d7ce 186 checksumsub += (*(command++));
ottaviano3 1:b97d46c5d7ce 187 }
ottaviano3 1:b97d46c5d7ce 188
ottaviano3 1:b97d46c5d7ce 189 // Value to set
ottaviano3 1:b97d46c5d7ce 190 _xbeespi.write(value);
ottaviano3 1:b97d46c5d7ce 191 checksumsub += value;
ottaviano3 1:b97d46c5d7ce 192
ottaviano3 1:b97d46c5d7ce 193 // Calculate checksum
ottaviano3 1:b97d46c5d7ce 194 checksumsub = checksumsub & 0xFF;
ottaviano3 1:b97d46c5d7ce 195 checksum = 0xFF - checksumsub;
ottaviano3 1:b97d46c5d7ce 196
ottaviano3 1:b97d46c5d7ce 197 // finally write checksum
ottaviano3 1:b97d46c5d7ce 198 _xbeespi.write(checksum);
ottaviano3 3:3c3707b0f5cd 199 }
ottaviano3 3:3c3707b0f5cd 200
ottaviano3 3:3c3707b0f5cd 201 int xbee900hp::getSerial(char* serialnumber) {
ottaviano3 3:3c3707b0f5cd 202 ///////////////////////////////////////////////////Send portion
ottaviano3 3:3c3707b0f5cd 203 // checksum Variables
ottaviano3 3:3c3707b0f5cd 204 unsigned int checksum = 0;
ottaviano3 3:3c3707b0f5cd 205 unsigned int checksumsub = 0;
ottaviano3 3:3c3707b0f5cd 206
ottaviano3 3:3c3707b0f5cd 207 // Write frame to return serial high and low.s
ottaviano3 3:3c3707b0f5cd 208
ottaviano3 3:3c3707b0f5cd 209 // Start config, send frames for SPI pins enable
ottaviano3 3:3c3707b0f5cd 210 _xbeespi.write(0x7E);
ottaviano3 3:3c3707b0f5cd 211 _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 212 _xbeespi.write(0x04);
ottaviano3 3:3c3707b0f5cd 213
ottaviano3 3:3c3707b0f5cd 214 // Frame type (config)
ottaviano3 3:3c3707b0f5cd 215 _xbeespi.write(0x08);
ottaviano3 3:3c3707b0f5cd 216 checksumsub += 0x08;
ottaviano3 3:3c3707b0f5cd 217 _xbeespi.write(0x52);
ottaviano3 3:3c3707b0f5cd 218 checksumsub += 0x52;
ottaviano3 3:3c3707b0f5cd 219
ottaviano3 3:3c3707b0f5cd 220 // Setting wanted
ottaviano3 3:3c3707b0f5cd 221 _xbeespi.write('S');
ottaviano3 3:3c3707b0f5cd 222 checksumsub += 'S';
ottaviano3 3:3c3707b0f5cd 223 _xbeespi.write('H');
ottaviano3 3:3c3707b0f5cd 224 checksumsub += 'H';
ottaviano3 3:3c3707b0f5cd 225
ottaviano3 3:3c3707b0f5cd 226 // Calculate checksum
ottaviano3 3:3c3707b0f5cd 227 checksumsub = checksumsub & 0xFF;
ottaviano3 3:3c3707b0f5cd 228 checksum = 0xFF - checksumsub;
ottaviano3 3:3c3707b0f5cd 229 // finally write checksum
ottaviano3 3:3c3707b0f5cd 230 _xbeespi.write(checksum);
ottaviano3 3:3c3707b0f5cd 231
ottaviano3 3:3c3707b0f5cd 232 //////////////////////////////////////RECIEVE PORTION
ottaviano3 3:3c3707b0f5cd 233 // Block until xbee replys
ottaviano3 3:3c3707b0f5cd 234 while (_pin_attn != 0) {}
ottaviano3 3:3c3707b0f5cd 235
ottaviano3 3:3c3707b0f5cd 236 // Containers for read values
ottaviano3 3:3c3707b0f5cd 237 char temp1 = 0;
ottaviano3 3:3c3707b0f5cd 238 char temp2 = 0;
ottaviano3 3:3c3707b0f5cd 239
ottaviano3 3:3c3707b0f5cd 240 // reset checksum to zero.
ottaviano3 3:3c3707b0f5cd 241 checksum = 0;
ottaviano3 3:3c3707b0f5cd 242 checksumsub = 0;
ottaviano3 3:3c3707b0f5cd 243
ottaviano3 3:3c3707b0f5cd 244 // get start byte
ottaviano3 3:3c3707b0f5cd 245 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 246 if (temp1 != 0x7E) {
ottaviano3 3:3c3707b0f5cd 247 // drop packet
ottaviano3 3:3c3707b0f5cd 248 return 1;
ottaviano3 3:3c3707b0f5cd 249 }
ottaviano3 3:3c3707b0f5cd 250 // Get length of message
ottaviano3 3:3c3707b0f5cd 251 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 252 temp2 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 253
ottaviano3 3:3c3707b0f5cd 254 // Get total length
ottaviano3 3:3c3707b0f5cd 255 unsigned int length = (temp1<<8) | temp2;
ottaviano3 3:3c3707b0f5cd 256
ottaviano3 3:3c3707b0f5cd 257 // Next read frame type to ensure it is an response packet.
ottaviano3 3:3c3707b0f5cd 258 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 259 if (temp1 != 0x88) {
ottaviano3 3:3c3707b0f5cd 260 // drop packet
ottaviano3 3:3c3707b0f5cd 261 return 1;
ottaviano3 3:3c3707b0f5cd 262 }
ottaviano3 3:3c3707b0f5cd 263 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 264
ottaviano3 3:3c3707b0f5cd 265 // get response frame id
ottaviano3 3:3c3707b0f5cd 266 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 267 if (temp1 != 0x52) {
ottaviano3 3:3c3707b0f5cd 268 // drop packet
ottaviano3 3:3c3707b0f5cd 269 return 1;
ottaviano3 3:3c3707b0f5cd 270 }
ottaviano3 3:3c3707b0f5cd 271 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 272
ottaviano3 3:3c3707b0f5cd 273 // get at response parameter
ottaviano3 3:3c3707b0f5cd 274 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 275 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 276 temp2 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 277 checksumsub += temp2;
ottaviano3 3:3c3707b0f5cd 278
ottaviano3 3:3c3707b0f5cd 279 if ((temp1 != 'S') || (temp2 != 'H')) {
ottaviano3 3:3c3707b0f5cd 280 return 1;
ottaviano3 3:3c3707b0f5cd 281 // drop
ottaviano3 3:3c3707b0f5cd 282 }
ottaviano3 3:3c3707b0f5cd 283
ottaviano3 3:3c3707b0f5cd 284 // Check OK flag
ottaviano3 3:3c3707b0f5cd 285 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 286 if (temp1 != 0x00) {
ottaviano3 3:3c3707b0f5cd 287 return 1;
ottaviano3 3:3c3707b0f5cd 288 // drop
ottaviano3 3:3c3707b0f5cd 289 }
ottaviano3 3:3c3707b0f5cd 290 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 291
ottaviano3 3:3c3707b0f5cd 292 // Now for the sweet sweet data.
ottaviano3 3:3c3707b0f5cd 293 for (int i = 0; i<(length-5); i++) {
ottaviano3 3:3c3707b0f5cd 294 *serialnumber = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 295 checksumsub += *(serialnumber++);
ottaviano3 3:3c3707b0f5cd 296 }
ottaviano3 3:3c3707b0f5cd 297
ottaviano3 3:3c3707b0f5cd 298 // Get that salty checksum
ottaviano3 3:3c3707b0f5cd 299 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 300
ottaviano3 3:3c3707b0f5cd 301 checksumsub = checksumsub & 0xFF;
ottaviano3 3:3c3707b0f5cd 302 checksum = 0xFF - checksumsub;
ottaviano3 3:3c3707b0f5cd 303
ottaviano3 3:3c3707b0f5cd 304 // Check the checksum
ottaviano3 3:3c3707b0f5cd 305 if (temp1 != checksum) {
ottaviano3 3:3c3707b0f5cd 306 // Checksum failure, flag to discard packet
ottaviano3 3:3c3707b0f5cd 307 return 1;
ottaviano3 3:3c3707b0f5cd 308 }
ottaviano3 3:3c3707b0f5cd 309
ottaviano3 3:3c3707b0f5cd 310 ////////////////////////////////////////////////////////////////////////
ottaviano3 3:3c3707b0f5cd 311 ///////////////////////////////////////////////////Send portion
ottaviano3 3:3c3707b0f5cd 312 // checksum Variables
ottaviano3 3:3c3707b0f5cd 313 checksum = 0;
ottaviano3 3:3c3707b0f5cd 314 checksumsub = 0;
ottaviano3 3:3c3707b0f5cd 315
ottaviano3 3:3c3707b0f5cd 316 // Write frame to return serial high and low.s
ottaviano3 3:3c3707b0f5cd 317
ottaviano3 3:3c3707b0f5cd 318 // Start config, send frames for SPI pins enable
ottaviano3 3:3c3707b0f5cd 319 _xbeespi.write(0x7E);
ottaviano3 3:3c3707b0f5cd 320 _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 321 _xbeespi.write(0x04);
ottaviano3 3:3c3707b0f5cd 322
ottaviano3 3:3c3707b0f5cd 323 // Frame type (config)
ottaviano3 3:3c3707b0f5cd 324 _xbeespi.write(0x08);
ottaviano3 3:3c3707b0f5cd 325 checksumsub += 0x08;
ottaviano3 3:3c3707b0f5cd 326 _xbeespi.write(0x52);
ottaviano3 3:3c3707b0f5cd 327 checksumsub += 0x52;
ottaviano3 3:3c3707b0f5cd 328
ottaviano3 3:3c3707b0f5cd 329 // Setting wanted
ottaviano3 3:3c3707b0f5cd 330 _xbeespi.write('S');
ottaviano3 3:3c3707b0f5cd 331 checksumsub += 'S';
ottaviano3 3:3c3707b0f5cd 332 _xbeespi.write('L');
ottaviano3 3:3c3707b0f5cd 333 checksumsub += 'L';
ottaviano3 3:3c3707b0f5cd 334
ottaviano3 3:3c3707b0f5cd 335 // Calculate checksum
ottaviano3 3:3c3707b0f5cd 336 checksumsub = checksumsub & 0xFF;
ottaviano3 3:3c3707b0f5cd 337 checksum = 0xFF - checksumsub;
ottaviano3 3:3c3707b0f5cd 338 // finally write checksum
ottaviano3 3:3c3707b0f5cd 339 _xbeespi.write(checksum);
ottaviano3 3:3c3707b0f5cd 340
ottaviano3 3:3c3707b0f5cd 341 //////////////////////////////////////RECIEVE PORTION
ottaviano3 3:3c3707b0f5cd 342 // Block until xbee replys
ottaviano3 3:3c3707b0f5cd 343 while (_pin_attn != 0) {}
ottaviano3 3:3c3707b0f5cd 344
ottaviano3 3:3c3707b0f5cd 345 // Containers for read values
ottaviano3 3:3c3707b0f5cd 346 temp1 = 0;
ottaviano3 3:3c3707b0f5cd 347 temp2 = 0;
ottaviano3 3:3c3707b0f5cd 348
ottaviano3 3:3c3707b0f5cd 349 // reset checksum to zero.
ottaviano3 3:3c3707b0f5cd 350 checksum = 0;
ottaviano3 3:3c3707b0f5cd 351 checksumsub = 0;
ottaviano3 3:3c3707b0f5cd 352
ottaviano3 3:3c3707b0f5cd 353 // get start byte
ottaviano3 3:3c3707b0f5cd 354 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 355 if (temp1 != 0x7E) {
ottaviano3 3:3c3707b0f5cd 356 // drop packet
ottaviano3 3:3c3707b0f5cd 357 return 1;
ottaviano3 3:3c3707b0f5cd 358 }
ottaviano3 3:3c3707b0f5cd 359 // Get length of message
ottaviano3 3:3c3707b0f5cd 360 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 361 temp2 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 362
ottaviano3 3:3c3707b0f5cd 363 // Get total length
ottaviano3 3:3c3707b0f5cd 364 length = (temp1<<8) | temp2;
ottaviano3 3:3c3707b0f5cd 365
ottaviano3 3:3c3707b0f5cd 366 // Next read frame type to ensure it is an response packet.
ottaviano3 3:3c3707b0f5cd 367 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 368 if (temp1 != 0x88) {
ottaviano3 3:3c3707b0f5cd 369 // drop packet
ottaviano3 3:3c3707b0f5cd 370 return 1;
ottaviano3 3:3c3707b0f5cd 371 }
ottaviano3 3:3c3707b0f5cd 372 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 373
ottaviano3 3:3c3707b0f5cd 374 // get response frame id
ottaviano3 3:3c3707b0f5cd 375 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 376 if (temp1 != 0x52) {
ottaviano3 3:3c3707b0f5cd 377 // drop packet
ottaviano3 3:3c3707b0f5cd 378 return 1;
ottaviano3 3:3c3707b0f5cd 379 }
ottaviano3 3:3c3707b0f5cd 380 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 381
ottaviano3 3:3c3707b0f5cd 382 // get at response parameter
ottaviano3 3:3c3707b0f5cd 383 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 384 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 385 temp2 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 386 checksumsub += temp2;
ottaviano3 3:3c3707b0f5cd 387
ottaviano3 3:3c3707b0f5cd 388 if ((temp1 != 'S') || (temp2 != 'L')) {
ottaviano3 3:3c3707b0f5cd 389 return 1;
ottaviano3 3:3c3707b0f5cd 390 // drop
ottaviano3 3:3c3707b0f5cd 391 }
ottaviano3 3:3c3707b0f5cd 392
ottaviano3 3:3c3707b0f5cd 393 // Check OK flag
ottaviano3 3:3c3707b0f5cd 394 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 395 if (temp1 != 0x00) {
ottaviano3 3:3c3707b0f5cd 396 return 1;
ottaviano3 3:3c3707b0f5cd 397 // drop
ottaviano3 3:3c3707b0f5cd 398 }
ottaviano3 3:3c3707b0f5cd 399 checksumsub += temp1;
ottaviano3 3:3c3707b0f5cd 400
ottaviano3 3:3c3707b0f5cd 401 // Now for the sweet sweet data.
ottaviano3 3:3c3707b0f5cd 402 for (int i = 0; i<(length-5); i++) {
ottaviano3 3:3c3707b0f5cd 403 *serialnumber = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 404 checksumsub += *(serialnumber++);
ottaviano3 3:3c3707b0f5cd 405 }
ottaviano3 3:3c3707b0f5cd 406 // Null terminate char array.
ottaviano3 4:4083baa871fb 407 *serialnumber = '\0';
ottaviano3 3:3c3707b0f5cd 408
ottaviano3 3:3c3707b0f5cd 409 // Get that salty checksum
ottaviano3 3:3c3707b0f5cd 410 temp1 = _xbeespi.write(0x00);
ottaviano3 3:3c3707b0f5cd 411
ottaviano3 3:3c3707b0f5cd 412 checksumsub = checksumsub & 0xFF;
ottaviano3 3:3c3707b0f5cd 413 checksum = 0xFF - checksumsub;
ottaviano3 3:3c3707b0f5cd 414
ottaviano3 3:3c3707b0f5cd 415 // Check the checksum
ottaviano3 3:3c3707b0f5cd 416 if (temp1 != checksum) {
ottaviano3 3:3c3707b0f5cd 417 // Checksum failure, flag to discard packet
ottaviano3 3:3c3707b0f5cd 418 return 1;
ottaviano3 3:3c3707b0f5cd 419 }
ottaviano3 3:3c3707b0f5cd 420
ottaviano3 3:3c3707b0f5cd 421 return 0;
ottaviano3 3:3c3707b0f5cd 422
ottaviano3 6:3873db4a0164 423 }
ottaviano3 6:3873db4a0164 424
ottaviano3 6:3873db4a0164 425 void xbee900hp::clearBuff()
ottaviano3 6:3873db4a0164 426 {
ottaviano3 6:3873db4a0164 427 while(_pin_attn == 0) {
ottaviano3 6:3873db4a0164 428 _xbeespi.write(0x00);
ottaviano3 6:3873db4a0164 429 }
ottaviano3 0:8c8a8244e590 430 }