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