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
xbee900hp.cpp@8:fa84dacc45b9, 2016-04-27 (annotated)
- Committer:
- ottaviano3
- Date:
- Wed Apr 27 01:19:54 2016 +0000
- Revision:
- 8:fa84dacc45b9
- Parent:
- 7:3cb67634fa4e
- Child:
- 9:d4542525b218
Comments Change
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ottaviano3 | 0:8c8a8244e590 | 1 | #include "xbee900hp.h" |
ottaviano3 | 0:8c8a8244e590 | 2 | |
ottaviano3 | 7:3cb67634fa4e | 3 | xbee900hp::xbee900hp(PinName pin_mosi,PinName pin_miso,PinName pin_sck,PinName pin_attn, PinName pin_rst, unsigned int freq) |
ottaviano3 | 7:3cb67634fa4e | 4 | : _pin_rst(pin_rst), _pin_attn(pin_attn), _xbeespi(pin_mosi,pin_miso,pin_sck) { |
ottaviano3 | 7:3cb67634fa4e | 5 | // Set proper spi transfer mode format. |
ottaviano3 | 0:8c8a8244e590 | 6 | _xbeespi.format(8,0); |
ottaviano3 | 7:3cb67634fa4e | 7 | // Set SPI frequency |
ottaviano3 | 7:3cb67634fa4e | 8 | _xbeespi.frequency(freq); |
ottaviano3 | 7:3cb67634fa4e | 9 | |
ottaviano3 | 7:3cb67634fa4e | 10 | // Reset for good measure. |
ottaviano3 | 2:7f4ddf710a44 | 11 | reset(); |
ottaviano3 | 7:3cb67634fa4e | 12 | |
ottaviano3 | 7:3cb67634fa4e | 13 | // Xbee is up and running! |
ottaviano3 | 0:8c8a8244e590 | 14 | } |
ottaviano3 | 0:8c8a8244e590 | 15 | |
ottaviano3 | 7:3cb67634fa4e | 16 | |
ottaviano3 | 0:8c8a8244e590 | 17 | /** |
ottaviano3 | 0:8c8a8244e590 | 18 | * Destructor |
ottaviano3 | 0:8c8a8244e590 | 19 | */ |
ottaviano3 | 0:8c8a8244e590 | 20 | xbee900hp::~xbee900hp() {} |
ottaviano3 | 0:8c8a8244e590 | 21 | |
ottaviano3 | 1:b97d46c5d7ce | 22 | /** |
ottaviano3 | 0:8c8a8244e590 | 23 | * Reset xBee to SPI mode |
ottaviano3 | 0:8c8a8244e590 | 24 | */ |
ottaviano3 | 0:8c8a8244e590 | 25 | void xbee900hp::reset() |
ottaviano3 | 0:8c8a8244e590 | 26 | { |
ottaviano3 | 0:8c8a8244e590 | 27 | _pin_rst = 0; |
ottaviano3 | 0:8c8a8244e590 | 28 | // Minimum pulse is 1ms |
ottaviano3 | 7:3cb67634fa4e | 29 | wait_ms(2); |
ottaviano3 | 0:8c8a8244e590 | 30 | _pin_rst = 1; |
ottaviano3 | 7:3cb67634fa4e | 31 | |
ottaviano3 | 7:3cb67634fa4e | 32 | // Wait for device to have time to start the reboot process. |
ottaviano3 | 7:3cb67634fa4e | 33 | wait_ms(80); |
ottaviano3 | 7:3cb67634fa4e | 34 | |
ottaviano3 | 7:3cb67634fa4e | 35 | // Now wait for the startup message. |
ottaviano3 | 7:3cb67634fa4e | 36 | while (attn() == 1) {} |
ottaviano3 | 7:3cb67634fa4e | 37 | |
ottaviano3 | 7:3cb67634fa4e | 38 | // This is the message the XBEE should send out on startup after a reset. |
ottaviano3 | 7:3cb67634fa4e | 39 | unsigned int modemMsg[6] = { 0x7E,0x00,0x02,0x8A,0x00,0x75 }; |
ottaviano3 | 7:3cb67634fa4e | 40 | // Container for or new message. |
ottaviano3 | 7:3cb67634fa4e | 41 | unsigned int delim[6]; |
ottaviano3 | 7:3cb67634fa4e | 42 | |
ottaviano3 | 7:3cb67634fa4e | 43 | // Read in the new message from the xbee. |
ottaviano3 | 7:3cb67634fa4e | 44 | for (int i = 0; i < 6; i++) { |
ottaviano3 | 7:3cb67634fa4e | 45 | delim[i] = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 46 | } |
ottaviano3 | 7:3cb67634fa4e | 47 | |
ottaviano3 | 7:3cb67634fa4e | 48 | // Compare recieved msg with msg we want. |
ottaviano3 | 7:3cb67634fa4e | 49 | if (memcmp(delim, modemMsg, sizeof(modemMsg))) { |
ottaviano3 | 7:3cb67634fa4e | 50 | // If msg mismatch. |
ottaviano3 | 7:3cb67634fa4e | 51 | error("XBEE FAILURE"); |
ottaviano3 | 7:3cb67634fa4e | 52 | } |
ottaviano3 | 0:8c8a8244e590 | 53 | } |
ottaviano3 | 0:8c8a8244e590 | 54 | |
ottaviano3 | 7:3cb67634fa4e | 55 | |
ottaviano3 | 0:8c8a8244e590 | 56 | /** |
ottaviano3 | 0:8c8a8244e590 | 57 | * Send packet out on RF |
ottaviano3 | 0:8c8a8244e590 | 58 | */ |
ottaviano3 | 7:3cb67634fa4e | 59 | int xbee900hp::sendPacket(char* data, unsigned int length, bool enMesh) { |
ottaviano3 | 0:8c8a8244e590 | 60 | // checksum |
ottaviano3 | 0:8c8a8244e590 | 61 | unsigned int checksum; |
ottaviano3 | 0:8c8a8244e590 | 62 | unsigned int checksumsub = 0; |
ottaviano3 | 7:3cb67634fa4e | 63 | |
ottaviano3 | 0:8c8a8244e590 | 64 | // start char |
ottaviano3 | 0:8c8a8244e590 | 65 | _xbeespi.write(0x7E); |
ottaviano3 | 0:8c8a8244e590 | 66 | // lenght |
ottaviano3 | 0:8c8a8244e590 | 67 | _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 68 | |
ottaviano3 | 0:8c8a8244e590 | 69 | unsigned int totallength = 14 + length - 1; |
ottaviano3 | 0:8c8a8244e590 | 70 | _xbeespi.write(totallength); |
ottaviano3 | 0:8c8a8244e590 | 71 | |
ottaviano3 | 0:8c8a8244e590 | 72 | // frame delimter |
ottaviano3 | 0:8c8a8244e590 | 73 | _xbeespi.write(0x10); |
ottaviano3 | 0:8c8a8244e590 | 74 | checksumsub += 0x10; |
ottaviano3 | 7:3cb67634fa4e | 75 | // id for later reference |
ottaviano3 | 0:8c8a8244e590 | 76 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 77 | |
ottaviano3 | 0:8c8a8244e590 | 78 | // destination address |
ottaviano3 | 0:8c8a8244e590 | 79 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 80 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 81 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 82 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 83 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 84 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 85 | _xbeespi.write(0xFF); |
ottaviano3 | 0:8c8a8244e590 | 86 | checksumsub += 0xFF; |
ottaviano3 | 0:8c8a8244e590 | 87 | _xbeespi.write(0xFF); |
ottaviano3 | 0:8c8a8244e590 | 88 | checksumsub += 0xFF; |
ottaviano3 | 0:8c8a8244e590 | 89 | |
ottaviano3 | 0:8c8a8244e590 | 90 | // reserved field, dont change |
ottaviano3 | 0:8c8a8244e590 | 91 | _xbeespi.write(0xFF); |
ottaviano3 | 0:8c8a8244e590 | 92 | checksumsub += 0xFF; |
ottaviano3 | 0:8c8a8244e590 | 93 | _xbeespi.write(0xFE); |
ottaviano3 | 0:8c8a8244e590 | 94 | checksumsub += 0xFE; |
ottaviano3 | 0:8c8a8244e590 | 95 | |
ottaviano3 | 0:8c8a8244e590 | 96 | //bcast radius |
ottaviano3 | 0:8c8a8244e590 | 97 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 98 | |
ottaviano3 | 7:3cb67634fa4e | 99 | // This is a bitmasked field |
ottaviano3 | 7:3cb67634fa4e | 100 | // bit 0: Disable ACK |
ottaviano3 | 7:3cb67634fa4e | 101 | // bit 1: Disable Route Discovery |
ottaviano3 | 7:3cb67634fa4e | 102 | // bit 2: Enable Unicast NACK messages. |
ottaviano3 | 7:3cb67634fa4e | 103 | // bit 3: Enable Unicast Trace Route messages. |
ottaviano3 | 7:3cb67634fa4e | 104 | // bits 6,7: b’01 - Point-Multipoint |
ottaviano3 | 7:3cb67634fa4e | 105 | // b’10 - Repeater mode (directed broadcast) |
ottaviano3 | 7:3cb67634fa4e | 106 | // b’11 - DigiMesh |
ottaviano3 | 7:3cb67634fa4e | 107 | // All other bits must be set to 0. |
ottaviano3 | 7:3cb67634fa4e | 108 | if (enMesh == true) { |
ottaviano3 | 7:3cb67634fa4e | 109 | // Transmit using digimesh |
ottaviano3 | 7:3cb67634fa4e | 110 | _xbeespi.write(0xC0); |
ottaviano3 | 7:3cb67634fa4e | 111 | checksumsub += 0xC0; |
ottaviano3 | 7:3cb67634fa4e | 112 | } else { |
ottaviano3 | 7:3cb67634fa4e | 113 | // Transmit point to point (without ACK) |
ottaviano3 | 7:3cb67634fa4e | 114 | _xbeespi.write(0x41); |
ottaviano3 | 7:3cb67634fa4e | 115 | checksumsub += 0x41; |
ottaviano3 | 7:3cb67634fa4e | 116 | } |
ottaviano3 | 0:8c8a8244e590 | 117 | |
ottaviano3 | 0:8c8a8244e590 | 118 | // dat data |
ottaviano3 | 0:8c8a8244e590 | 119 | for (int i = 0; i < (length - 1); i++) { |
ottaviano3 | 0:8c8a8244e590 | 120 | _xbeespi.write(*data); |
ottaviano3 | 0:8c8a8244e590 | 121 | checksumsub += (*(data++)); |
ottaviano3 | 0:8c8a8244e590 | 122 | } |
ottaviano3 | 0:8c8a8244e590 | 123 | |
ottaviano3 | 0:8c8a8244e590 | 124 | // Calculate checksum |
ottaviano3 | 0:8c8a8244e590 | 125 | checksumsub = checksumsub & 0xFF; |
ottaviano3 | 0:8c8a8244e590 | 126 | checksum = 0xFF - checksumsub; |
ottaviano3 | 0:8c8a8244e590 | 127 | |
ottaviano3 | 0:8c8a8244e590 | 128 | // finally write checksum |
ottaviano3 | 0:8c8a8244e590 | 129 | _xbeespi.write(checksum); |
ottaviano3 | 7:3cb67634fa4e | 130 | |
ottaviano3 | 7:3cb67634fa4e | 131 | return 0; |
ottaviano3 | 1:b97d46c5d7ce | 132 | } |
ottaviano3 | 1:b97d46c5d7ce | 133 | |
ottaviano3 | 7:3cb67634fa4e | 134 | |
ottaviano3 | 7:3cb67634fa4e | 135 | /** |
ottaviano3 | 7:3cb67634fa4e | 136 | * Wait for and read incoming data packet |
ottaviano3 | 7:3cb67634fa4e | 137 | */ |
ottaviano3 | 7:3cb67634fa4e | 138 | unsigned long long xbee900hp::readPacket(char* data) { |
ottaviano3 | 7:3cb67634fa4e | 139 | unsigned long long temp1; |
ottaviano3 | 2:7f4ddf710a44 | 140 | unsigned int temp2; |
ottaviano3 | 2:7f4ddf710a44 | 141 | |
ottaviano3 | 7:3cb67634fa4e | 142 | unsigned long long srcAddr = 0; |
ottaviano3 | 7:3cb67634fa4e | 143 | |
ottaviano3 | 2:7f4ddf710a44 | 144 | unsigned int checksumsub = 0; |
ottaviano3 | 2:7f4ddf710a44 | 145 | unsigned int checksum; |
ottaviano3 | 7:3cb67634fa4e | 146 | |
ottaviano3 | 2:7f4ddf710a44 | 147 | // get first vars. |
ottaviano3 | 2:7f4ddf710a44 | 148 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 149 | if (temp1 != 0x7E) { |
ottaviano3 | 7:3cb67634fa4e | 150 | return 0; |
ottaviano3 | 2:7f4ddf710a44 | 151 | } |
ottaviano3 | 2:7f4ddf710a44 | 152 | // Get length of message |
ottaviano3 | 2:7f4ddf710a44 | 153 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 154 | temp2 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 155 | |
ottaviano3 | 2:7f4ddf710a44 | 156 | // Get total length |
ottaviano3 | 2:7f4ddf710a44 | 157 | unsigned int length = (temp1<<8) | temp2; |
ottaviano3 | 7:3cb67634fa4e | 158 | |
ottaviano3 | 2:7f4ddf710a44 | 159 | // Next read frame type to ensure it is an RX packet. |
ottaviano3 | 2:7f4ddf710a44 | 160 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 161 | if (temp1 != 0x90) { |
ottaviano3 | 7:3cb67634fa4e | 162 | return 0; |
ottaviano3 | 2:7f4ddf710a44 | 163 | } |
ottaviano3 | 2:7f4ddf710a44 | 164 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 165 | |
ottaviano3 | 2:7f4ddf710a44 | 166 | // in our case we dont care about source address this should be modified to extract source address if needed. |
ottaviano3 | 7:3cb67634fa4e | 167 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 168 | srcAddr = (temp1 << 56); |
ottaviano3 | 7:3cb67634fa4e | 169 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 170 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 171 | srcAddr ^= (temp1 << 48); |
ottaviano3 | 7:3cb67634fa4e | 172 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 173 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 174 | srcAddr ^= (temp1 << 40); |
ottaviano3 | 7:3cb67634fa4e | 175 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 176 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 177 | srcAddr ^= (temp1 << 32); |
ottaviano3 | 7:3cb67634fa4e | 178 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 179 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 180 | srcAddr ^= (temp1 << 24); |
ottaviano3 | 7:3cb67634fa4e | 181 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 182 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 183 | srcAddr ^= (temp1 << 16); |
ottaviano3 | 7:3cb67634fa4e | 184 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 185 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 186 | srcAddr ^= (temp1 << 8); |
ottaviano3 | 7:3cb67634fa4e | 187 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 188 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 189 | srcAddr ^= (temp1); |
ottaviano3 | 7:3cb67634fa4e | 190 | checksumsub += temp1; |
ottaviano3 | 2:7f4ddf710a44 | 191 | // reserved field, we dont care about except for checksum |
ottaviano3 | 2:7f4ddf710a44 | 192 | checksumsub += _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 193 | checksumsub += _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 194 | |
ottaviano3 | 2:7f4ddf710a44 | 195 | // recive options we also dont care though |
ottaviano3 | 2:7f4ddf710a44 | 196 | checksumsub += _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 197 | |
ottaviano3 | 2:7f4ddf710a44 | 198 | // Now for the sweet sweet data. |
ottaviano3 | 2:7f4ddf710a44 | 199 | for (int i = 0; i<(length-12); i++) { |
ottaviano3 | 2:7f4ddf710a44 | 200 | *data = _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 201 | checksumsub += *(data++); |
ottaviano3 | 2:7f4ddf710a44 | 202 | } |
ottaviano3 | 2:7f4ddf710a44 | 203 | // Null terminate char array. |
ottaviano3 | 4:4083baa871fb | 204 | *data = '\0'; |
ottaviano3 | 7:3cb67634fa4e | 205 | |
ottaviano3 | 2:7f4ddf710a44 | 206 | // Get that salty checksum |
ottaviano3 | 2:7f4ddf710a44 | 207 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 208 | |
ottaviano3 | 2:7f4ddf710a44 | 209 | checksumsub = checksumsub & 0xFF; |
ottaviano3 | 2:7f4ddf710a44 | 210 | checksum = 0xFF - checksumsub; |
ottaviano3 | 7:3cb67634fa4e | 211 | |
ottaviano3 | 2:7f4ddf710a44 | 212 | // Check the checksum |
ottaviano3 | 2:7f4ddf710a44 | 213 | if (temp1 != checksum) { |
ottaviano3 | 2:7f4ddf710a44 | 214 | // Checksum failure, flag to discard packet |
ottaviano3 | 7:3cb67634fa4e | 215 | return 0; |
ottaviano3 | 2:7f4ddf710a44 | 216 | } |
ottaviano3 | 7:3cb67634fa4e | 217 | |
ottaviano3 | 7:3cb67634fa4e | 218 | return srcAddr; |
ottaviano3 | 2:7f4ddf710a44 | 219 | } |
ottaviano3 | 2:7f4ddf710a44 | 220 | |
ottaviano3 | 7:3cb67634fa4e | 221 | |
ottaviano3 | 7:3cb67634fa4e | 222 | /** |
ottaviano3 | 7:3cb67634fa4e | 223 | * Check ATTN signal |
ottaviano3 | 7:3cb67634fa4e | 224 | */ |
ottaviano3 | 2:7f4ddf710a44 | 225 | int xbee900hp::attn() { |
ottaviano3 | 2:7f4ddf710a44 | 226 | return _pin_attn; |
ottaviano3 | 2:7f4ddf710a44 | 227 | } |
ottaviano3 | 2:7f4ddf710a44 | 228 | |
ottaviano3 | 7:3cb67634fa4e | 229 | /** |
ottaviano3 | 7:3cb67634fa4e | 230 | * Get signal strength of last recieved packet |
ottaviano3 | 7:3cb67634fa4e | 231 | */ |
ottaviano3 | 7:3cb67634fa4e | 232 | unsigned int xbee900hp::getRSSI() { |
ottaviano3 | 1:b97d46c5d7ce | 233 | // checksum Variables |
ottaviano3 | 1:b97d46c5d7ce | 234 | unsigned int checksum = 0; |
ottaviano3 | 1:b97d46c5d7ce | 235 | unsigned int checksumsub = 0; |
ottaviano3 | 7:3cb67634fa4e | 236 | |
ottaviano3 | 7:3cb67634fa4e | 237 | // RSSI storage container |
ottaviano3 | 7:3cb67634fa4e | 238 | unsigned int rssi; |
ottaviano3 | 7:3cb67634fa4e | 239 | |
ottaviano3 | 7:3cb67634fa4e | 240 | // Frame initiator |
ottaviano3 | 1:b97d46c5d7ce | 241 | _xbeespi.write(0x7E); |
ottaviano3 | 7:3cb67634fa4e | 242 | // Length |
ottaviano3 | 1:b97d46c5d7ce | 243 | _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 244 | _xbeespi.write(0x04); |
ottaviano3 | 7:3cb67634fa4e | 245 | |
ottaviano3 | 1:b97d46c5d7ce | 246 | // Frame type (config) |
ottaviano3 | 1:b97d46c5d7ce | 247 | _xbeespi.write(0x08); |
ottaviano3 | 1:b97d46c5d7ce | 248 | checksumsub += 0x08; |
ottaviano3 | 7:3cb67634fa4e | 249 | // Response frame ID |
ottaviano3 | 7:3cb67634fa4e | 250 | _xbeespi.write(0x44); |
ottaviano3 | 7:3cb67634fa4e | 251 | checksumsub += 0x44; |
ottaviano3 | 7:3cb67634fa4e | 252 | |
ottaviano3 | 1:b97d46c5d7ce | 253 | // AT Command |
ottaviano3 | 7:3cb67634fa4e | 254 | _xbeespi.write(0x44); // Hex for ASCII D |
ottaviano3 | 7:3cb67634fa4e | 255 | checksumsub += 0x44; |
ottaviano3 | 7:3cb67634fa4e | 256 | _xbeespi.write(0x42); // Hex for ASCII B |
ottaviano3 | 7:3cb67634fa4e | 257 | checksumsub += 0x42; |
ottaviano3 | 7:3cb67634fa4e | 258 | |
ottaviano3 | 1:b97d46c5d7ce | 259 | // Calculate checksum |
ottaviano3 | 1:b97d46c5d7ce | 260 | checksumsub = checksumsub & 0xFF; |
ottaviano3 | 1:b97d46c5d7ce | 261 | checksum = 0xFF - checksumsub; |
ottaviano3 | 1:b97d46c5d7ce | 262 | |
ottaviano3 | 1:b97d46c5d7ce | 263 | // finally write checksum |
ottaviano3 | 1:b97d46c5d7ce | 264 | _xbeespi.write(checksum); |
ottaviano3 | 3:3c3707b0f5cd | 265 | |
ottaviano3 | 3:3c3707b0f5cd | 266 | // Block until xbee replys |
ottaviano3 | 7:3cb67634fa4e | 267 | while (_pin_attn != 0) { } |
ottaviano3 | 7:3cb67634fa4e | 268 | |
ottaviano3 | 3:3c3707b0f5cd | 269 | // reset checksum to zero. |
ottaviano3 | 3:3c3707b0f5cd | 270 | checksum = 0; |
ottaviano3 | 3:3c3707b0f5cd | 271 | checksumsub = 0; |
ottaviano3 | 7:3cb67634fa4e | 272 | |
ottaviano3 | 7:3cb67634fa4e | 273 | unsigned int temp1; |
ottaviano3 | 7:3cb67634fa4e | 274 | unsigned int temp2; |
ottaviano3 | 7:3cb67634fa4e | 275 | |
ottaviano3 | 3:3c3707b0f5cd | 276 | // get start byte |
ottaviano3 | 3:3c3707b0f5cd | 277 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 3:3c3707b0f5cd | 278 | if (temp1 != 0x7E) { |
ottaviano3 | 3:3c3707b0f5cd | 279 | // drop packet |
ottaviano3 | 3:3c3707b0f5cd | 280 | return 1; |
ottaviano3 | 3:3c3707b0f5cd | 281 | } |
ottaviano3 | 7:3cb67634fa4e | 282 | // Get length of message (we dont care, we know what it is) |
ottaviano3 | 7:3cb67634fa4e | 283 | _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 284 | _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 285 | |
ottaviano3 | 3:3c3707b0f5cd | 286 | // Next read frame type to ensure it is an response packet. |
ottaviano3 | 3:3c3707b0f5cd | 287 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 3:3c3707b0f5cd | 288 | if (temp1 != 0x88) { |
ottaviano3 | 3:3c3707b0f5cd | 289 | // drop packet |
ottaviano3 | 7:3cb67634fa4e | 290 | return 0; |
ottaviano3 | 3:3c3707b0f5cd | 291 | } |
ottaviano3 | 3:3c3707b0f5cd | 292 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 293 | |
ottaviano3 | 3:3c3707b0f5cd | 294 | // get response frame id |
ottaviano3 | 7:3cb67634fa4e | 295 | |
ottaviano3 | 7:3cb67634fa4e | 296 | while (temp1 != 0x44) { |
ottaviano3 | 7:3cb67634fa4e | 297 | // Dredge through data until the response frame we want arrives. |
ottaviano3 | 7:3cb67634fa4e | 298 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 3:3c3707b0f5cd | 299 | } |
ottaviano3 | 3:3c3707b0f5cd | 300 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 301 | |
ottaviano3 | 3:3c3707b0f5cd | 302 | // get at response parameter |
ottaviano3 | 3:3c3707b0f5cd | 303 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 3:3c3707b0f5cd | 304 | checksumsub += temp1; |
ottaviano3 | 3:3c3707b0f5cd | 305 | temp2 = _xbeespi.write(0x00); |
ottaviano3 | 3:3c3707b0f5cd | 306 | checksumsub += temp2; |
ottaviano3 | 7:3cb67634fa4e | 307 | |
ottaviano3 | 7:3cb67634fa4e | 308 | if ((temp1 != 0x44) || (temp2 != 0x42)) { |
ottaviano3 | 7:3cb67634fa4e | 309 | return 1; |
ottaviano3 | 7:3cb67634fa4e | 310 | // drop |
ottaviano3 | 7:3cb67634fa4e | 311 | } |
ottaviano3 | 7:3cb67634fa4e | 312 | |
ottaviano3 | 7:3cb67634fa4e | 313 | // Check OK flag |
ottaviano3 | 7:3cb67634fa4e | 314 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 315 | if (temp1 != 0x00) { |
ottaviano3 | 3:3c3707b0f5cd | 316 | return 1; |
ottaviano3 | 3:3c3707b0f5cd | 317 | // drop |
ottaviano3 | 3:3c3707b0f5cd | 318 | } |
ottaviano3 | 7:3cb67634fa4e | 319 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 320 | |
ottaviano3 | 7:3cb67634fa4e | 321 | // Get RSSI Data |
ottaviano3 | 7:3cb67634fa4e | 322 | rssi = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 323 | checksumsub += rssi; |
ottaviano3 | 7:3cb67634fa4e | 324 | |
ottaviano3 | 7:3cb67634fa4e | 325 | // Get that salty checksum |
ottaviano3 | 7:3cb67634fa4e | 326 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 327 | |
ottaviano3 | 7:3cb67634fa4e | 328 | checksumsub = checksumsub & 0xFF; |
ottaviano3 | 7:3cb67634fa4e | 329 | checksum = 0xFF - checksumsub; |
ottaviano3 | 7:3cb67634fa4e | 330 | |
ottaviano3 | 7:3cb67634fa4e | 331 | // Check the checksum |
ottaviano3 | 7:3cb67634fa4e | 332 | if (temp1 != checksum) { |
ottaviano3 | 7:3cb67634fa4e | 333 | // Checksum failure, flag to discard packet |
ottaviano3 | 7:3cb67634fa4e | 334 | return 1; |
ottaviano3 | 7:3cb67634fa4e | 335 | } |
ottaviano3 | 7:3cb67634fa4e | 336 | |
ottaviano3 | 7:3cb67634fa4e | 337 | return rssi; |
ottaviano3 | 7:3cb67634fa4e | 338 | } |
ottaviano3 | 7:3cb67634fa4e | 339 | |
ottaviano3 | 7:3cb67634fa4e | 340 | /** |
ottaviano3 | 7:3cb67634fa4e | 341 | * Set the transmission power level. |
ottaviano3 | 7:3cb67634fa4e | 342 | */ |
ottaviano3 | 7:3cb67634fa4e | 343 | int xbee900hp::setPower(int value) { |
ottaviano3 | 7:3cb67634fa4e | 344 | if ((value > 4) || (value < 0)) { |
ottaviano3 | 7:3cb67634fa4e | 345 | return 1; |
ottaviano3 | 7:3cb67634fa4e | 346 | } |
ottaviano3 | 7:3cb67634fa4e | 347 | |
ottaviano3 | 7:3cb67634fa4e | 348 | // checksum Variables |
ottaviano3 | 7:3cb67634fa4e | 349 | unsigned int checksum = 0; |
ottaviano3 | 7:3cb67634fa4e | 350 | unsigned int checksumsub = 0; |
ottaviano3 | 7:3cb67634fa4e | 351 | |
ottaviano3 | 7:3cb67634fa4e | 352 | // Frame initiator |
ottaviano3 | 7:3cb67634fa4e | 353 | _xbeespi.write(0x7E); |
ottaviano3 | 7:3cb67634fa4e | 354 | // Length |
ottaviano3 | 7:3cb67634fa4e | 355 | _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 356 | _xbeespi.write(0x05); |
ottaviano3 | 7:3cb67634fa4e | 357 | |
ottaviano3 | 7:3cb67634fa4e | 358 | // Frame type (config) |
ottaviano3 | 7:3cb67634fa4e | 359 | _xbeespi.write(0x08); |
ottaviano3 | 7:3cb67634fa4e | 360 | checksumsub += 0x08; |
ottaviano3 | 7:3cb67634fa4e | 361 | // Response frame ID |
ottaviano3 | 7:3cb67634fa4e | 362 | _xbeespi.write(0x50); |
ottaviano3 | 7:3cb67634fa4e | 363 | checksumsub += 0x50; |
ottaviano3 | 7:3cb67634fa4e | 364 | |
ottaviano3 | 7:3cb67634fa4e | 365 | // AT Command |
ottaviano3 | 7:3cb67634fa4e | 366 | _xbeespi.write(0x50); // Hex for ASCII P |
ottaviano3 | 7:3cb67634fa4e | 367 | checksumsub += 0x50; |
ottaviano3 | 7:3cb67634fa4e | 368 | _xbeespi.write(0x4C); // Hex for ASCII L |
ottaviano3 | 7:3cb67634fa4e | 369 | checksumsub += 0x4C; |
ottaviano3 | 7:3cb67634fa4e | 370 | |
ottaviano3 | 7:3cb67634fa4e | 371 | // Value to set |
ottaviano3 | 7:3cb67634fa4e | 372 | _xbeespi.write(value); |
ottaviano3 | 7:3cb67634fa4e | 373 | checksumsub += value; |
ottaviano3 | 7:3cb67634fa4e | 374 | |
ottaviano3 | 7:3cb67634fa4e | 375 | // Calculate checksum |
ottaviano3 | 7:3cb67634fa4e | 376 | checksumsub = checksumsub & 0xFF; |
ottaviano3 | 7:3cb67634fa4e | 377 | checksum = 0xFF - checksumsub; |
ottaviano3 | 7:3cb67634fa4e | 378 | |
ottaviano3 | 7:3cb67634fa4e | 379 | // finally write checksum |
ottaviano3 | 7:3cb67634fa4e | 380 | _xbeespi.write(checksum); |
ottaviano3 | 7:3cb67634fa4e | 381 | |
ottaviano3 | 7:3cb67634fa4e | 382 | // Block until xbee replys |
ottaviano3 | 7:3cb67634fa4e | 383 | while (_pin_attn != 0) { } |
ottaviano3 | 7:3cb67634fa4e | 384 | |
ottaviano3 | 7:3cb67634fa4e | 385 | // reset checksum to zero. |
ottaviano3 | 7:3cb67634fa4e | 386 | checksum = 0; |
ottaviano3 | 7:3cb67634fa4e | 387 | checksumsub = 0; |
ottaviano3 | 7:3cb67634fa4e | 388 | |
ottaviano3 | 7:3cb67634fa4e | 389 | unsigned int temp1; |
ottaviano3 | 7:3cb67634fa4e | 390 | unsigned int temp2; |
ottaviano3 | 7:3cb67634fa4e | 391 | |
ottaviano3 | 7:3cb67634fa4e | 392 | // get start byte |
ottaviano3 | 7:3cb67634fa4e | 393 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 394 | if (temp1 != 0x7E) { |
ottaviano3 | 7:3cb67634fa4e | 395 | // drop packet |
ottaviano3 | 7:3cb67634fa4e | 396 | return 1; |
ottaviano3 | 7:3cb67634fa4e | 397 | } |
ottaviano3 | 7:3cb67634fa4e | 398 | // Get length of message (we dont care, we know what it is) |
ottaviano3 | 7:3cb67634fa4e | 399 | _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 400 | _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 401 | |
ottaviano3 | 7:3cb67634fa4e | 402 | // Next read frame type to ensure it is an response packet. |
ottaviano3 | 7:3cb67634fa4e | 403 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 404 | if (temp1 != 0x88) { |
ottaviano3 | 7:3cb67634fa4e | 405 | // drop packet |
ottaviano3 | 7:3cb67634fa4e | 406 | return 0; |
ottaviano3 | 7:3cb67634fa4e | 407 | } |
ottaviano3 | 7:3cb67634fa4e | 408 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 409 | |
ottaviano3 | 7:3cb67634fa4e | 410 | while (temp1 != 0x50) { |
ottaviano3 | 7:3cb67634fa4e | 411 | // Keep going through data until response frame is found. |
ottaviano3 | 7:3cb67634fa4e | 412 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 413 | } |
ottaviano3 | 7:3cb67634fa4e | 414 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 415 | |
ottaviano3 | 7:3cb67634fa4e | 416 | // get at response parameter |
ottaviano3 | 7:3cb67634fa4e | 417 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 418 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 419 | temp2 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 420 | checksumsub += temp2; |
ottaviano3 | 7:3cb67634fa4e | 421 | |
ottaviano3 | 7:3cb67634fa4e | 422 | if ((temp1 != 0x50) || (temp2 != 0x4C)) { |
ottaviano3 | 7:3cb67634fa4e | 423 | return 1; |
ottaviano3 | 7:3cb67634fa4e | 424 | // drop |
ottaviano3 | 7:3cb67634fa4e | 425 | } |
ottaviano3 | 7:3cb67634fa4e | 426 | |
ottaviano3 | 3:3c3707b0f5cd | 427 | // Check OK flag |
ottaviano3 | 3:3c3707b0f5cd | 428 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 3:3c3707b0f5cd | 429 | if (temp1 != 0x00) { |
ottaviano3 | 3:3c3707b0f5cd | 430 | return 1; |
ottaviano3 | 3:3c3707b0f5cd | 431 | // drop |
ottaviano3 | 3:3c3707b0f5cd | 432 | } |
ottaviano3 | 3:3c3707b0f5cd | 433 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 434 | |
ottaviano3 | 3:3c3707b0f5cd | 435 | // Get that salty checksum |
ottaviano3 | 3:3c3707b0f5cd | 436 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 437 | |
ottaviano3 | 3:3c3707b0f5cd | 438 | checksumsub = checksumsub & 0xFF; |
ottaviano3 | 3:3c3707b0f5cd | 439 | checksum = 0xFF - checksumsub; |
ottaviano3 | 7:3cb67634fa4e | 440 | |
ottaviano3 | 3:3c3707b0f5cd | 441 | // Check the checksum |
ottaviano3 | 3:3c3707b0f5cd | 442 | if (temp1 != checksum) { |
ottaviano3 | 3:3c3707b0f5cd | 443 | // Checksum failure, flag to discard packet |
ottaviano3 | 3:3c3707b0f5cd | 444 | return 1; |
ottaviano3 | 3:3c3707b0f5cd | 445 | } |
ottaviano3 | 7:3cb67634fa4e | 446 | |
ottaviano3 | 7:3cb67634fa4e | 447 | return 0; |
ottaviano3 | 7:3cb67634fa4e | 448 | } |
ottaviano3 | 7:3cb67634fa4e | 449 | |
ottaviano3 | 7:3cb67634fa4e | 450 | /** |
ottaviano3 | 7:3cb67634fa4e | 451 | * Get serial number of xbee module |
ottaviano3 | 7:3cb67634fa4e | 452 | */ |
ottaviano3 | 7:3cb67634fa4e | 453 | unsigned long long xbee900hp::getSerial() { |
ottaviano3 | 3:3c3707b0f5cd | 454 | // checksum Variables |
ottaviano3 | 7:3cb67634fa4e | 455 | unsigned int checksum = 0; |
ottaviano3 | 7:3cb67634fa4e | 456 | unsigned int checksumsub = 0; |
ottaviano3 | 7:3cb67634fa4e | 457 | |
ottaviano3 | 7:3cb67634fa4e | 458 | // Container for serial as we calculate it. |
ottaviano3 | 7:3cb67634fa4e | 459 | unsigned long long serialNumber = 0; |
ottaviano3 | 7:3cb67634fa4e | 460 | |
ottaviano3 | 7:3cb67634fa4e | 461 | // Containers for read values |
ottaviano3 | 7:3cb67634fa4e | 462 | unsigned long long temp1 = 0; |
ottaviano3 | 7:3cb67634fa4e | 463 | unsigned long long temp2 = 0; |
ottaviano3 | 7:3cb67634fa4e | 464 | unsigned long long temp3 = 0; |
ottaviano3 | 7:3cb67634fa4e | 465 | unsigned long long temp4 = 0; |
ottaviano3 | 7:3cb67634fa4e | 466 | |
ottaviano3 | 7:3cb67634fa4e | 467 | // Write frame to return serial high. |
ottaviano3 | 7:3cb67634fa4e | 468 | // Total frame looks like this: |
ottaviano3 | 7:3cb67634fa4e | 469 | // 7E 00 04 08 52 53(S) 48(H) CHECKSUM |
ottaviano3 | 7:3cb67634fa4e | 470 | // Wait for response to get high part of serial |
ottaviano3 | 7:3cb67634fa4e | 471 | // Profit???? |
ottaviano3 | 7:3cb67634fa4e | 472 | |
ottaviano3 | 7:3cb67634fa4e | 473 | // Frame Header |
ottaviano3 | 3:3c3707b0f5cd | 474 | _xbeespi.write(0x7E); |
ottaviano3 | 7:3cb67634fa4e | 475 | // Frame Length |
ottaviano3 | 3:3c3707b0f5cd | 476 | _xbeespi.write(0x00); |
ottaviano3 | 3:3c3707b0f5cd | 477 | _xbeespi.write(0x04); |
ottaviano3 | 7:3cb67634fa4e | 478 | |
ottaviano3 | 3:3c3707b0f5cd | 479 | // Frame type (config) |
ottaviano3 | 3:3c3707b0f5cd | 480 | _xbeespi.write(0x08); |
ottaviano3 | 3:3c3707b0f5cd | 481 | checksumsub += 0x08; |
ottaviano3 | 3:3c3707b0f5cd | 482 | _xbeespi.write(0x52); |
ottaviano3 | 3:3c3707b0f5cd | 483 | checksumsub += 0x52; |
ottaviano3 | 7:3cb67634fa4e | 484 | |
ottaviano3 | 7:3cb67634fa4e | 485 | // Setting wanted SerialHigh |
ottaviano3 | 7:3cb67634fa4e | 486 | _xbeespi.write(0x53); |
ottaviano3 | 7:3cb67634fa4e | 487 | checksumsub += 0x53; |
ottaviano3 | 7:3cb67634fa4e | 488 | _xbeespi.write(0x48); |
ottaviano3 | 7:3cb67634fa4e | 489 | checksumsub += 0x48; |
ottaviano3 | 7:3cb67634fa4e | 490 | |
ottaviano3 | 3:3c3707b0f5cd | 491 | // Calculate checksum |
ottaviano3 | 3:3c3707b0f5cd | 492 | checksumsub = checksumsub & 0xFF; |
ottaviano3 | 3:3c3707b0f5cd | 493 | checksum = 0xFF - checksumsub; |
ottaviano3 | 3:3c3707b0f5cd | 494 | // finally write checksum |
ottaviano3 | 3:3c3707b0f5cd | 495 | _xbeespi.write(checksum); |
ottaviano3 | 7:3cb67634fa4e | 496 | |
ottaviano3 | 7:3cb67634fa4e | 497 | // RECIEVE PORTION |
ottaviano3 | 3:3c3707b0f5cd | 498 | // Block until xbee replys |
ottaviano3 | 7:3cb67634fa4e | 499 | while (_pin_attn != 0) { } |
ottaviano3 | 7:3cb67634fa4e | 500 | |
ottaviano3 | 3:3c3707b0f5cd | 501 | // reset checksum to zero. |
ottaviano3 | 3:3c3707b0f5cd | 502 | checksum = 0; |
ottaviano3 | 3:3c3707b0f5cd | 503 | checksumsub = 0; |
ottaviano3 | 7:3cb67634fa4e | 504 | |
ottaviano3 | 3:3c3707b0f5cd | 505 | // get start byte |
ottaviano3 | 3:3c3707b0f5cd | 506 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 3:3c3707b0f5cd | 507 | if (temp1 != 0x7E) { |
ottaviano3 | 3:3c3707b0f5cd | 508 | // drop packet |
ottaviano3 | 7:3cb67634fa4e | 509 | return 0; |
ottaviano3 | 3:3c3707b0f5cd | 510 | } |
ottaviano3 | 7:3cb67634fa4e | 511 | // Get length of message (we don't care we know how long) |
ottaviano3 | 7:3cb67634fa4e | 512 | _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 513 | _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 514 | |
ottaviano3 | 3:3c3707b0f5cd | 515 | // Next read frame type to ensure it is an response packet. |
ottaviano3 | 3:3c3707b0f5cd | 516 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 3:3c3707b0f5cd | 517 | if (temp1 != 0x88) { |
ottaviano3 | 3:3c3707b0f5cd | 518 | // drop packet |
ottaviano3 | 7:3cb67634fa4e | 519 | return 0; |
ottaviano3 | 3:3c3707b0f5cd | 520 | } |
ottaviano3 | 3:3c3707b0f5cd | 521 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 522 | |
ottaviano3 | 3:3c3707b0f5cd | 523 | // get response frame id |
ottaviano3 | 3:3c3707b0f5cd | 524 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 3:3c3707b0f5cd | 525 | if (temp1 != 0x52) { |
ottaviano3 | 3:3c3707b0f5cd | 526 | // drop packet |
ottaviano3 | 7:3cb67634fa4e | 527 | return 0; |
ottaviano3 | 3:3c3707b0f5cd | 528 | } |
ottaviano3 | 3:3c3707b0f5cd | 529 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 530 | |
ottaviano3 | 3:3c3707b0f5cd | 531 | // get at response parameter |
ottaviano3 | 3:3c3707b0f5cd | 532 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 3:3c3707b0f5cd | 533 | checksumsub += temp1; |
ottaviano3 | 3:3c3707b0f5cd | 534 | temp2 = _xbeespi.write(0x00); |
ottaviano3 | 3:3c3707b0f5cd | 535 | checksumsub += temp2; |
ottaviano3 | 7:3cb67634fa4e | 536 | |
ottaviano3 | 7:3cb67634fa4e | 537 | if ((temp1 != 0x53) || (temp2 != 0x48)) { |
ottaviano3 | 7:3cb67634fa4e | 538 | return 0; |
ottaviano3 | 3:3c3707b0f5cd | 539 | // drop |
ottaviano3 | 3:3c3707b0f5cd | 540 | } |
ottaviano3 | 7:3cb67634fa4e | 541 | |
ottaviano3 | 3:3c3707b0f5cd | 542 | // Check OK flag |
ottaviano3 | 3:3c3707b0f5cd | 543 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 3:3c3707b0f5cd | 544 | if (temp1 != 0x00) { |
ottaviano3 | 7:3cb67634fa4e | 545 | return 0; |
ottaviano3 | 3:3c3707b0f5cd | 546 | // drop |
ottaviano3 | 3:3c3707b0f5cd | 547 | } |
ottaviano3 | 3:3c3707b0f5cd | 548 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 549 | |
ottaviano3 | 7:3cb67634fa4e | 550 | // Now for the high portion of the serial |
ottaviano3 | 7:3cb67634fa4e | 551 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 552 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 553 | temp2 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 554 | checksumsub += temp2; |
ottaviano3 | 7:3cb67634fa4e | 555 | temp3 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 556 | checksumsub += temp3; |
ottaviano3 | 7:3cb67634fa4e | 557 | temp4 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 558 | checksumsub += temp4; |
ottaviano3 | 7:3cb67634fa4e | 559 | |
ottaviano3 | 7:3cb67634fa4e | 560 | // Store new items in serial number |
ottaviano3 | 7:3cb67634fa4e | 561 | serialNumber += (temp1 << 56) ^ (temp2 << 48) ^ (temp3 << 40) ^ (temp4 << 32); |
ottaviano3 | 7:3cb67634fa4e | 562 | |
ottaviano3 | 3:3c3707b0f5cd | 563 | // Get that salty checksum |
ottaviano3 | 3:3c3707b0f5cd | 564 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 565 | |
ottaviano3 | 3:3c3707b0f5cd | 566 | checksumsub = checksumsub & 0xFF; |
ottaviano3 | 3:3c3707b0f5cd | 567 | checksum = 0xFF - checksumsub; |
ottaviano3 | 7:3cb67634fa4e | 568 | |
ottaviano3 | 3:3c3707b0f5cd | 569 | // Check the checksum |
ottaviano3 | 3:3c3707b0f5cd | 570 | if (temp1 != checksum) { |
ottaviano3 | 3:3c3707b0f5cd | 571 | // Checksum failure, flag to discard packet |
ottaviano3 | 7:3cb67634fa4e | 572 | return 0; |
ottaviano3 | 7:3cb67634fa4e | 573 | } |
ottaviano3 | 7:3cb67634fa4e | 574 | |
ottaviano3 | 7:3cb67634fa4e | 575 | ///////////////////////////////////////////////////Send portion |
ottaviano3 | 7:3cb67634fa4e | 576 | // checksum Variables |
ottaviano3 | 7:3cb67634fa4e | 577 | checksum = 0; |
ottaviano3 | 7:3cb67634fa4e | 578 | checksumsub = 0; |
ottaviano3 | 7:3cb67634fa4e | 579 | |
ottaviano3 | 7:3cb67634fa4e | 580 | // Containers for read values |
ottaviano3 | 7:3cb67634fa4e | 581 | temp1 = 0; |
ottaviano3 | 7:3cb67634fa4e | 582 | temp2 = 0; |
ottaviano3 | 7:3cb67634fa4e | 583 | temp3 = 0; |
ottaviano3 | 7:3cb67634fa4e | 584 | temp4 = 0; |
ottaviano3 | 7:3cb67634fa4e | 585 | |
ottaviano3 | 7:3cb67634fa4e | 586 | // Write frame to return serial low. |
ottaviano3 | 7:3cb67634fa4e | 587 | // Total frame looks like this: |
ottaviano3 | 7:3cb67634fa4e | 588 | // 7E 00 04 08 52 53(S) 4C(L) CHECKSUM |
ottaviano3 | 7:3cb67634fa4e | 589 | // Wait for response to get lower part of serial |
ottaviano3 | 7:3cb67634fa4e | 590 | // Profit???? |
ottaviano3 | 7:3cb67634fa4e | 591 | |
ottaviano3 | 7:3cb67634fa4e | 592 | // Frame Header |
ottaviano3 | 7:3cb67634fa4e | 593 | _xbeespi.write(0x7E); |
ottaviano3 | 7:3cb67634fa4e | 594 | // Frame Length |
ottaviano3 | 7:3cb67634fa4e | 595 | _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 596 | _xbeespi.write(0x04); |
ottaviano3 | 7:3cb67634fa4e | 597 | |
ottaviano3 | 7:3cb67634fa4e | 598 | // Frame type (config) |
ottaviano3 | 7:3cb67634fa4e | 599 | _xbeespi.write(0x08); |
ottaviano3 | 7:3cb67634fa4e | 600 | checksumsub += 0x08; |
ottaviano3 | 7:3cb67634fa4e | 601 | _xbeespi.write(0x52); |
ottaviano3 | 7:3cb67634fa4e | 602 | checksumsub += 0x52; |
ottaviano3 | 7:3cb67634fa4e | 603 | |
ottaviano3 | 7:3cb67634fa4e | 604 | // Setting wanted SerialLow |
ottaviano3 | 7:3cb67634fa4e | 605 | _xbeespi.write(0x53); |
ottaviano3 | 7:3cb67634fa4e | 606 | checksumsub += 0x53; |
ottaviano3 | 7:3cb67634fa4e | 607 | _xbeespi.write(0x4C); |
ottaviano3 | 7:3cb67634fa4e | 608 | checksumsub += 0x4C; |
ottaviano3 | 7:3cb67634fa4e | 609 | |
ottaviano3 | 7:3cb67634fa4e | 610 | // Calculate checksum |
ottaviano3 | 7:3cb67634fa4e | 611 | checksumsub = checksumsub & 0xFF; |
ottaviano3 | 7:3cb67634fa4e | 612 | checksum = 0xFF - checksumsub; |
ottaviano3 | 7:3cb67634fa4e | 613 | // finally write checksum |
ottaviano3 | 7:3cb67634fa4e | 614 | _xbeespi.write(checksum); |
ottaviano3 | 7:3cb67634fa4e | 615 | |
ottaviano3 | 7:3cb67634fa4e | 616 | // RECIEVE PORTION |
ottaviano3 | 7:3cb67634fa4e | 617 | // Block until xbee replys |
ottaviano3 | 7:3cb67634fa4e | 618 | while (_pin_attn != 0) { } |
ottaviano3 | 7:3cb67634fa4e | 619 | |
ottaviano3 | 7:3cb67634fa4e | 620 | // reset checksum to zero. |
ottaviano3 | 7:3cb67634fa4e | 621 | checksum = 0; |
ottaviano3 | 7:3cb67634fa4e | 622 | checksumsub = 0; |
ottaviano3 | 7:3cb67634fa4e | 623 | |
ottaviano3 | 7:3cb67634fa4e | 624 | // get start byte |
ottaviano3 | 7:3cb67634fa4e | 625 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 626 | if (temp1 != 0x7E) { |
ottaviano3 | 7:3cb67634fa4e | 627 | // drop packet |
ottaviano3 | 7:3cb67634fa4e | 628 | return 0; |
ottaviano3 | 3:3c3707b0f5cd | 629 | } |
ottaviano3 | 7:3cb67634fa4e | 630 | // Get length of message (dont care again) |
ottaviano3 | 7:3cb67634fa4e | 631 | _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 632 | _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 633 | |
ottaviano3 | 7:3cb67634fa4e | 634 | // Next read frame type to ensure it is an response packet. |
ottaviano3 | 7:3cb67634fa4e | 635 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 636 | if (temp1 != 0x88) { |
ottaviano3 | 7:3cb67634fa4e | 637 | // drop packet |
ottaviano3 | 7:3cb67634fa4e | 638 | return 0; |
ottaviano3 | 7:3cb67634fa4e | 639 | } |
ottaviano3 | 7:3cb67634fa4e | 640 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 641 | |
ottaviano3 | 7:3cb67634fa4e | 642 | // get response frame id |
ottaviano3 | 7:3cb67634fa4e | 643 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 644 | if (temp1 != 0x52) { |
ottaviano3 | 7:3cb67634fa4e | 645 | // drop packet |
ottaviano3 | 7:3cb67634fa4e | 646 | return 0; |
ottaviano3 | 7:3cb67634fa4e | 647 | } |
ottaviano3 | 7:3cb67634fa4e | 648 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 649 | |
ottaviano3 | 7:3cb67634fa4e | 650 | // get at response parameter |
ottaviano3 | 7:3cb67634fa4e | 651 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 652 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 653 | temp2 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 654 | checksumsub += temp2; |
ottaviano3 | 7:3cb67634fa4e | 655 | |
ottaviano3 | 7:3cb67634fa4e | 656 | if ((temp1 != 0x53) || (temp2 != 0x4C)) { |
ottaviano3 | 7:3cb67634fa4e | 657 | return 0; |
ottaviano3 | 7:3cb67634fa4e | 658 | // drop |
ottaviano3 | 7:3cb67634fa4e | 659 | } |
ottaviano3 | 3:3c3707b0f5cd | 660 | |
ottaviano3 | 7:3cb67634fa4e | 661 | // Check OK flag |
ottaviano3 | 7:3cb67634fa4e | 662 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 663 | if (temp1 != 0x00) { |
ottaviano3 | 7:3cb67634fa4e | 664 | return 0; |
ottaviano3 | 7:3cb67634fa4e | 665 | // drop |
ottaviano3 | 7:3cb67634fa4e | 666 | } |
ottaviano3 | 7:3cb67634fa4e | 667 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 668 | |
ottaviano3 | 7:3cb67634fa4e | 669 | // Now for the high portion of the serial |
ottaviano3 | 7:3cb67634fa4e | 670 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 671 | checksumsub += temp1; |
ottaviano3 | 7:3cb67634fa4e | 672 | temp2 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 673 | checksumsub += temp2; |
ottaviano3 | 7:3cb67634fa4e | 674 | temp3 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 675 | checksumsub += temp3; |
ottaviano3 | 7:3cb67634fa4e | 676 | temp4 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 677 | checksumsub += temp4; |
ottaviano3 | 7:3cb67634fa4e | 678 | |
ottaviano3 | 7:3cb67634fa4e | 679 | serialNumber += (temp1 << 24) ^ (temp2 << 16) ^ (temp3 << 8) ^ (temp4); |
ottaviano3 | 7:3cb67634fa4e | 680 | |
ottaviano3 | 7:3cb67634fa4e | 681 | // Get that salty checksum |
ottaviano3 | 7:3cb67634fa4e | 682 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 683 | |
ottaviano3 | 7:3cb67634fa4e | 684 | checksumsub = checksumsub & 0xFF; |
ottaviano3 | 7:3cb67634fa4e | 685 | checksum = 0xFF - checksumsub; |
ottaviano3 | 7:3cb67634fa4e | 686 | |
ottaviano3 | 7:3cb67634fa4e | 687 | // Check the checksum |
ottaviano3 | 7:3cb67634fa4e | 688 | if (temp1 != checksum) { |
ottaviano3 | 7:3cb67634fa4e | 689 | // Checksum failure, flag to discard packet |
ottaviano3 | 7:3cb67634fa4e | 690 | return 0; |
ottaviano3 | 7:3cb67634fa4e | 691 | } |
ottaviano3 | 7:3cb67634fa4e | 692 | |
ottaviano3 | 7:3cb67634fa4e | 693 | return serialNumber; |
ottaviano3 | 6:3873db4a0164 | 694 | } |
ottaviano3 | 6:3873db4a0164 | 695 | |
ottaviano3 | 7:3cb67634fa4e | 696 | |
ottaviano3 | 7:3cb67634fa4e | 697 | /** |
ottaviano3 | 7:3cb67634fa4e | 698 | * Clear output buffer |
ottaviano3 | 7:3cb67634fa4e | 699 | */ |
ottaviano3 | 7:3cb67634fa4e | 700 | void xbee900hp::clearBuff() { |
ottaviano3 | 6:3873db4a0164 | 701 | while(_pin_attn == 0) { |
ottaviano3 | 6:3873db4a0164 | 702 | _xbeespi.write(0x00); |
ottaviano3 | 6:3873db4a0164 | 703 | } |
ottaviano3 | 7:3cb67634fa4e | 704 | } |
ottaviano3 | 7:3cb67634fa4e | 705 | |
ottaviano3 | 7:3cb67634fa4e | 706 | |
ottaviano3 | 7:3cb67634fa4e | 707 | /** |
ottaviano3 | 7:3cb67634fa4e | 708 | * Read raw data in from module |
ottaviano3 | 7:3cb67634fa4e | 709 | */ |
ottaviano3 | 7:3cb67634fa4e | 710 | char xbee900hp::debug() { |
ottaviano3 | 7:3cb67634fa4e | 711 | return _xbeespi.write(0x00); |
ottaviano3 | 7:3cb67634fa4e | 712 | } |