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@2:7f4ddf710a44, 2015-04-23 (annotated)
- Committer:
- ottaviano3
- Date:
- Thu Apr 23 17:09:28 2015 +0000
- Revision:
- 2:7f4ddf710a44
- Parent:
- 1:b97d46c5d7ce
- Child:
- 3:3c3707b0f5cd
added read capability
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 | 1:b97d46c5d7ce | 6 | xbee900hp::xbee900hp(PinName pin_mosi,PinName pin_miso,PinName pin_sck,PinName pin_attn, PinName pin_rst) |
ottaviano3 | 1:b97d46c5d7ce | 7 | : _pin_rst(pin_rst), _pin_attn(pin_attn), _xbeespi(pin_mosi,pin_miso,pin_sck) |
ottaviano3 | 0:8c8a8244e590 | 8 | { |
ottaviano3 | 0:8c8a8244e590 | 9 | _xbeespi.format(8,0); |
ottaviano3 | 0:8c8a8244e590 | 10 | _xbeespi.frequency(1000000); |
ottaviano3 | 2:7f4ddf710a44 | 11 | |
ottaviano3 | 2:7f4ddf710a44 | 12 | reset(); |
ottaviano3 | 0:8c8a8244e590 | 13 | } |
ottaviano3 | 0:8c8a8244e590 | 14 | |
ottaviano3 | 0:8c8a8244e590 | 15 | /** |
ottaviano3 | 0:8c8a8244e590 | 16 | * Destructor |
ottaviano3 | 0:8c8a8244e590 | 17 | */ |
ottaviano3 | 0:8c8a8244e590 | 18 | xbee900hp::~xbee900hp() {} |
ottaviano3 | 0:8c8a8244e590 | 19 | |
ottaviano3 | 1:b97d46c5d7ce | 20 | /** |
ottaviano3 | 0:8c8a8244e590 | 21 | * Reset xBee to SPI mode |
ottaviano3 | 0:8c8a8244e590 | 22 | */ |
ottaviano3 | 0:8c8a8244e590 | 23 | void xbee900hp::reset() |
ottaviano3 | 0:8c8a8244e590 | 24 | { |
ottaviano3 | 0:8c8a8244e590 | 25 | _pin_rst = 0; |
ottaviano3 | 0:8c8a8244e590 | 26 | // Minimum pulse is 1ms |
ottaviano3 | 0:8c8a8244e590 | 27 | wait_ms(1); |
ottaviano3 | 0:8c8a8244e590 | 28 | _pin_rst = 1; |
ottaviano3 | 0:8c8a8244e590 | 29 | // wait for module to come back online |
ottaviano3 | 0:8c8a8244e590 | 30 | wait_ms(500); |
ottaviano3 | 0:8c8a8244e590 | 31 | } |
ottaviano3 | 0:8c8a8244e590 | 32 | |
ottaviano3 | 0:8c8a8244e590 | 33 | /** |
ottaviano3 | 0:8c8a8244e590 | 34 | * Send packet out on RF |
ottaviano3 | 0:8c8a8244e590 | 35 | */ |
ottaviano3 | 0:8c8a8244e590 | 36 | void xbee900hp::sendPacket(char* data, unsigned int length) |
ottaviano3 | 0:8c8a8244e590 | 37 | { |
ottaviano3 | 0:8c8a8244e590 | 38 | // checksum |
ottaviano3 | 0:8c8a8244e590 | 39 | unsigned int checksum; |
ottaviano3 | 0:8c8a8244e590 | 40 | unsigned int checksumsub = 0; |
ottaviano3 | 0:8c8a8244e590 | 41 | |
ottaviano3 | 0:8c8a8244e590 | 42 | // start char |
ottaviano3 | 0:8c8a8244e590 | 43 | _xbeespi.write(0x7E); |
ottaviano3 | 0:8c8a8244e590 | 44 | // lenght |
ottaviano3 | 0:8c8a8244e590 | 45 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 46 | |
ottaviano3 | 0:8c8a8244e590 | 47 | unsigned int totallength = 14 + length - 1; |
ottaviano3 | 0:8c8a8244e590 | 48 | _xbeespi.write(totallength); |
ottaviano3 | 0:8c8a8244e590 | 49 | |
ottaviano3 | 0:8c8a8244e590 | 50 | // frame delimter |
ottaviano3 | 0:8c8a8244e590 | 51 | _xbeespi.write(0x10); |
ottaviano3 | 0:8c8a8244e590 | 52 | checksumsub += 0x10; |
ottaviano3 | 0:8c8a8244e590 | 53 | // id for later reference 0 = no id |
ottaviano3 | 0:8c8a8244e590 | 54 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 55 | |
ottaviano3 | 0:8c8a8244e590 | 56 | // destination address |
ottaviano3 | 0:8c8a8244e590 | 57 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 58 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 59 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 60 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 61 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 62 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 63 | _xbeespi.write(0xFF); |
ottaviano3 | 0:8c8a8244e590 | 64 | checksumsub += 0xFF; |
ottaviano3 | 0:8c8a8244e590 | 65 | _xbeespi.write(0xFF); |
ottaviano3 | 0:8c8a8244e590 | 66 | checksumsub += 0xFF; |
ottaviano3 | 0:8c8a8244e590 | 67 | |
ottaviano3 | 0:8c8a8244e590 | 68 | // reserved field, dont change |
ottaviano3 | 0:8c8a8244e590 | 69 | _xbeespi.write(0xFF); |
ottaviano3 | 0:8c8a8244e590 | 70 | checksumsub += 0xFF; |
ottaviano3 | 0:8c8a8244e590 | 71 | _xbeespi.write(0xFE); |
ottaviano3 | 0:8c8a8244e590 | 72 | checksumsub += 0xFE; |
ottaviano3 | 0:8c8a8244e590 | 73 | |
ottaviano3 | 0:8c8a8244e590 | 74 | //bcast radius |
ottaviano3 | 0:8c8a8244e590 | 75 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 76 | |
ottaviano3 | 0:8c8a8244e590 | 77 | //transmit options 0x00 to disable ACK |
ottaviano3 | 0:8c8a8244e590 | 78 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 79 | |
ottaviano3 | 0:8c8a8244e590 | 80 | // dat data |
ottaviano3 | 0:8c8a8244e590 | 81 | for (int i = 0; i < (length - 1); i++) { |
ottaviano3 | 0:8c8a8244e590 | 82 | _xbeespi.write(*data); |
ottaviano3 | 0:8c8a8244e590 | 83 | checksumsub += (*(data++)); |
ottaviano3 | 0:8c8a8244e590 | 84 | } |
ottaviano3 | 0:8c8a8244e590 | 85 | |
ottaviano3 | 0:8c8a8244e590 | 86 | |
ottaviano3 | 0:8c8a8244e590 | 87 | // Calculate checksum |
ottaviano3 | 0:8c8a8244e590 | 88 | checksumsub = checksumsub & 0xFF; |
ottaviano3 | 0:8c8a8244e590 | 89 | checksum = 0xFF - checksumsub; |
ottaviano3 | 0:8c8a8244e590 | 90 | |
ottaviano3 | 0:8c8a8244e590 | 91 | // finally write checksum |
ottaviano3 | 0:8c8a8244e590 | 92 | _xbeespi.write(checksum); |
ottaviano3 | 1:b97d46c5d7ce | 93 | } |
ottaviano3 | 1:b97d46c5d7ce | 94 | |
ottaviano3 | 2:7f4ddf710a44 | 95 | int xbee900hp::readPacket(char* data) { |
ottaviano3 | 2:7f4ddf710a44 | 96 | unsigned int temp1; |
ottaviano3 | 2:7f4ddf710a44 | 97 | unsigned int temp2; |
ottaviano3 | 2:7f4ddf710a44 | 98 | |
ottaviano3 | 2:7f4ddf710a44 | 99 | unsigned int checksumsub = 0; |
ottaviano3 | 2:7f4ddf710a44 | 100 | unsigned int checksum; |
ottaviano3 | 2:7f4ddf710a44 | 101 | |
ottaviano3 | 2:7f4ddf710a44 | 102 | // get first vars. |
ottaviano3 | 2:7f4ddf710a44 | 103 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 104 | if (temp1 != 0x7E) { |
ottaviano3 | 2:7f4ddf710a44 | 105 | // drop packet |
ottaviano3 | 2:7f4ddf710a44 | 106 | return 1; |
ottaviano3 | 2:7f4ddf710a44 | 107 | } |
ottaviano3 | 2:7f4ddf710a44 | 108 | // Get length of message |
ottaviano3 | 2:7f4ddf710a44 | 109 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 110 | temp2 = _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 111 | |
ottaviano3 | 2:7f4ddf710a44 | 112 | // Get total length |
ottaviano3 | 2:7f4ddf710a44 | 113 | unsigned int length = (temp1<<8) | temp2; |
ottaviano3 | 2:7f4ddf710a44 | 114 | |
ottaviano3 | 2:7f4ddf710a44 | 115 | // Next read frame type to ensure it is an RX packet. |
ottaviano3 | 2:7f4ddf710a44 | 116 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 117 | if (temp1 != 0x90) { |
ottaviano3 | 2:7f4ddf710a44 | 118 | // drop packet |
ottaviano3 | 2:7f4ddf710a44 | 119 | return 1; |
ottaviano3 | 2:7f4ddf710a44 | 120 | } |
ottaviano3 | 2:7f4ddf710a44 | 121 | checksumsub += temp1; |
ottaviano3 | 2:7f4ddf710a44 | 122 | |
ottaviano3 | 2:7f4ddf710a44 | 123 | // in our case we dont care about source address this should be modified to extract source address if needed. |
ottaviano3 | 2:7f4ddf710a44 | 124 | checksumsub += _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 125 | checksumsub += _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 126 | checksumsub += _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 127 | checksumsub += _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 128 | checksumsub += _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 129 | checksumsub += _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 130 | checksumsub += _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 131 | checksumsub += _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 132 | // reserved field, we dont care about except for checksum |
ottaviano3 | 2:7f4ddf710a44 | 133 | checksumsub += _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 134 | checksumsub += _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 135 | |
ottaviano3 | 2:7f4ddf710a44 | 136 | // recive options we also dont care though |
ottaviano3 | 2:7f4ddf710a44 | 137 | checksumsub += _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 138 | |
ottaviano3 | 2:7f4ddf710a44 | 139 | // Now for the sweet sweet data. |
ottaviano3 | 2:7f4ddf710a44 | 140 | for (int i = 0; i<(length-12); i++) { |
ottaviano3 | 2:7f4ddf710a44 | 141 | *data = _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 142 | checksumsub += *(data++); |
ottaviano3 | 2:7f4ddf710a44 | 143 | } |
ottaviano3 | 2:7f4ddf710a44 | 144 | // Null terminate char array. |
ottaviano3 | 2:7f4ddf710a44 | 145 | *data = NULL; |
ottaviano3 | 2:7f4ddf710a44 | 146 | |
ottaviano3 | 2:7f4ddf710a44 | 147 | // Get that salty checksum |
ottaviano3 | 2:7f4ddf710a44 | 148 | temp1 = _xbeespi.write(0x00); |
ottaviano3 | 2:7f4ddf710a44 | 149 | |
ottaviano3 | 2:7f4ddf710a44 | 150 | checksumsub = checksumsub & 0xFF; |
ottaviano3 | 2:7f4ddf710a44 | 151 | checksum = 0xFF - checksumsub; |
ottaviano3 | 2:7f4ddf710a44 | 152 | |
ottaviano3 | 2:7f4ddf710a44 | 153 | // Check the checksum |
ottaviano3 | 2:7f4ddf710a44 | 154 | if (temp1 != checksum) { |
ottaviano3 | 2:7f4ddf710a44 | 155 | // Checksum failure, flag to discard packet |
ottaviano3 | 2:7f4ddf710a44 | 156 | return 1; |
ottaviano3 | 2:7f4ddf710a44 | 157 | } |
ottaviano3 | 2:7f4ddf710a44 | 158 | |
ottaviano3 | 2:7f4ddf710a44 | 159 | return 0; |
ottaviano3 | 2:7f4ddf710a44 | 160 | } |
ottaviano3 | 2:7f4ddf710a44 | 161 | |
ottaviano3 | 2:7f4ddf710a44 | 162 | int xbee900hp::attn() { |
ottaviano3 | 2:7f4ddf710a44 | 163 | return _pin_attn; |
ottaviano3 | 2:7f4ddf710a44 | 164 | } |
ottaviano3 | 2:7f4ddf710a44 | 165 | |
ottaviano3 | 1:b97d46c5d7ce | 166 | void xbee900hp::writeSetting(char command[2], unsigned int value) { |
ottaviano3 | 1:b97d46c5d7ce | 167 | // checksum Variables |
ottaviano3 | 1:b97d46c5d7ce | 168 | unsigned int checksum = 0; |
ottaviano3 | 1:b97d46c5d7ce | 169 | unsigned int checksumsub = 0; |
ottaviano3 | 1:b97d46c5d7ce | 170 | |
ottaviano3 | 1:b97d46c5d7ce | 171 | // Start config, send frames for SPI pins enable |
ottaviano3 | 1:b97d46c5d7ce | 172 | _xbeespi.write(0x7E); |
ottaviano3 | 1:b97d46c5d7ce | 173 | _xbeespi.write(0x00); |
ottaviano3 | 1:b97d46c5d7ce | 174 | _xbeespi.write(0x05); |
ottaviano3 | 1:b97d46c5d7ce | 175 | |
ottaviano3 | 1:b97d46c5d7ce | 176 | // Frame type (config) |
ottaviano3 | 1:b97d46c5d7ce | 177 | _xbeespi.write(0x08); |
ottaviano3 | 1:b97d46c5d7ce | 178 | checksumsub += 0x08; |
ottaviano3 | 1:b97d46c5d7ce | 179 | _xbeespi.write(0x00); |
ottaviano3 | 1:b97d46c5d7ce | 180 | |
ottaviano3 | 1:b97d46c5d7ce | 181 | |
ottaviano3 | 1:b97d46c5d7ce | 182 | // AT Command |
ottaviano3 | 1:b97d46c5d7ce | 183 | for (int i = 0; i < 2; i++) { |
ottaviano3 | 1:b97d46c5d7ce | 184 | _xbeespi.write(*command); |
ottaviano3 | 1:b97d46c5d7ce | 185 | checksumsub += (*(command++)); |
ottaviano3 | 1:b97d46c5d7ce | 186 | } |
ottaviano3 | 1:b97d46c5d7ce | 187 | |
ottaviano3 | 1:b97d46c5d7ce | 188 | // Value to set |
ottaviano3 | 1:b97d46c5d7ce | 189 | _xbeespi.write(value); |
ottaviano3 | 1:b97d46c5d7ce | 190 | checksumsub += value; |
ottaviano3 | 1:b97d46c5d7ce | 191 | |
ottaviano3 | 1:b97d46c5d7ce | 192 | // Calculate checksum |
ottaviano3 | 1:b97d46c5d7ce | 193 | checksumsub = checksumsub & 0xFF; |
ottaviano3 | 1:b97d46c5d7ce | 194 | checksum = 0xFF - checksumsub; |
ottaviano3 | 1:b97d46c5d7ce | 195 | |
ottaviano3 | 1:b97d46c5d7ce | 196 | // finally write checksum |
ottaviano3 | 1:b97d46c5d7ce | 197 | _xbeespi.write(checksum); |
ottaviano3 | 0:8c8a8244e590 | 198 | } |