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@0:8c8a8244e590, 2015-04-20 (annotated)
- Committer:
- ottaviano3
- Date:
- Mon Apr 20 21:04:26 2015 +0000
- Revision:
- 0:8c8a8244e590
- Child:
- 1:b97d46c5d7ce
First revision XBee 900 HP SPI driver
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 | 0:8c8a8244e590 | 6 | xbee900hp::xbee900hp(PinName pin_mosi,PinName pin_miso,PinName pin_sck,PinName pin_attn, PinName pin_rst, PinName pin_dout) |
ottaviano3 | 0:8c8a8244e590 | 7 | : _pin_rst(pin_rst), _pin_attn(pin_attn), _pin_dout(pin_dout), _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 | 0:8c8a8244e590 | 11 | |
ottaviano3 | 0:8c8a8244e590 | 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 | 0:8c8a8244e590 | 20 | |
ottaviano3 | 0:8c8a8244e590 | 21 | |
ottaviano3 | 0:8c8a8244e590 | 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 | // Set Xbee to SPI mode |
ottaviano3 | 0:8c8a8244e590 | 28 | _pin_dout = 0; |
ottaviano3 | 0:8c8a8244e590 | 29 | _pin_rst = 0; |
ottaviano3 | 0:8c8a8244e590 | 30 | // Minimum pulse is 1ms |
ottaviano3 | 0:8c8a8244e590 | 31 | wait_ms(1); |
ottaviano3 | 0:8c8a8244e590 | 32 | _pin_rst = 1; |
ottaviano3 | 0:8c8a8244e590 | 33 | |
ottaviano3 | 0:8c8a8244e590 | 34 | // Wait for asst pin to go low to indicate SPI mode |
ottaviano3 | 0:8c8a8244e590 | 35 | while(_pin_attn != 0) { } |
ottaviano3 | 0:8c8a8244e590 | 36 | |
ottaviano3 | 0:8c8a8244e590 | 37 | _pin_dout = 1; |
ottaviano3 | 0:8c8a8244e590 | 38 | |
ottaviano3 | 0:8c8a8244e590 | 39 | // wait for module to come back online |
ottaviano3 | 0:8c8a8244e590 | 40 | wait_ms(500); |
ottaviano3 | 0:8c8a8244e590 | 41 | } |
ottaviano3 | 0:8c8a8244e590 | 42 | |
ottaviano3 | 0:8c8a8244e590 | 43 | /** |
ottaviano3 | 0:8c8a8244e590 | 44 | * Send packet out on RF |
ottaviano3 | 0:8c8a8244e590 | 45 | */ |
ottaviano3 | 0:8c8a8244e590 | 46 | void xbee900hp::sendPacket(char* data, unsigned int length) |
ottaviano3 | 0:8c8a8244e590 | 47 | { |
ottaviano3 | 0:8c8a8244e590 | 48 | // checksum |
ottaviano3 | 0:8c8a8244e590 | 49 | unsigned int checksum; |
ottaviano3 | 0:8c8a8244e590 | 50 | unsigned int checksumsub = 0; |
ottaviano3 | 0:8c8a8244e590 | 51 | |
ottaviano3 | 0:8c8a8244e590 | 52 | // start char |
ottaviano3 | 0:8c8a8244e590 | 53 | _xbeespi.write(0x7E); |
ottaviano3 | 0:8c8a8244e590 | 54 | // lenght |
ottaviano3 | 0:8c8a8244e590 | 55 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 56 | |
ottaviano3 | 0:8c8a8244e590 | 57 | unsigned int totallength = 14 + length - 1; |
ottaviano3 | 0:8c8a8244e590 | 58 | _xbeespi.write(totallength); |
ottaviano3 | 0:8c8a8244e590 | 59 | |
ottaviano3 | 0:8c8a8244e590 | 60 | // frame delimter |
ottaviano3 | 0:8c8a8244e590 | 61 | _xbeespi.write(0x10); |
ottaviano3 | 0:8c8a8244e590 | 62 | checksumsub += 0x10; |
ottaviano3 | 0:8c8a8244e590 | 63 | // id for later reference 0 = no id |
ottaviano3 | 0:8c8a8244e590 | 64 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 65 | |
ottaviano3 | 0:8c8a8244e590 | 66 | // destination address |
ottaviano3 | 0:8c8a8244e590 | 67 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 68 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 69 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 70 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 71 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 72 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 73 | _xbeespi.write(0xFF); |
ottaviano3 | 0:8c8a8244e590 | 74 | checksumsub += 0xFF; |
ottaviano3 | 0:8c8a8244e590 | 75 | _xbeespi.write(0xFF); |
ottaviano3 | 0:8c8a8244e590 | 76 | checksumsub += 0xFF; |
ottaviano3 | 0:8c8a8244e590 | 77 | |
ottaviano3 | 0:8c8a8244e590 | 78 | // reserved field, dont change |
ottaviano3 | 0:8c8a8244e590 | 79 | _xbeespi.write(0xFF); |
ottaviano3 | 0:8c8a8244e590 | 80 | checksumsub += 0xFF; |
ottaviano3 | 0:8c8a8244e590 | 81 | _xbeespi.write(0xFE); |
ottaviano3 | 0:8c8a8244e590 | 82 | checksumsub += 0xFE; |
ottaviano3 | 0:8c8a8244e590 | 83 | |
ottaviano3 | 0:8c8a8244e590 | 84 | //bcast radius |
ottaviano3 | 0:8c8a8244e590 | 85 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 86 | |
ottaviano3 | 0:8c8a8244e590 | 87 | //transmit options 0x00 to disable ACK |
ottaviano3 | 0:8c8a8244e590 | 88 | _xbeespi.write(0x00); |
ottaviano3 | 0:8c8a8244e590 | 89 | |
ottaviano3 | 0:8c8a8244e590 | 90 | // dat data |
ottaviano3 | 0:8c8a8244e590 | 91 | for (int i = 0; i < (length - 1); i++) { |
ottaviano3 | 0:8c8a8244e590 | 92 | _xbeespi.write(*data); |
ottaviano3 | 0:8c8a8244e590 | 93 | checksumsub += (*(data++)); |
ottaviano3 | 0:8c8a8244e590 | 94 | } |
ottaviano3 | 0:8c8a8244e590 | 95 | |
ottaviano3 | 0:8c8a8244e590 | 96 | |
ottaviano3 | 0:8c8a8244e590 | 97 | // Calculate checksum |
ottaviano3 | 0:8c8a8244e590 | 98 | checksumsub = checksumsub & 0xFF; |
ottaviano3 | 0:8c8a8244e590 | 99 | checksum = 0xFF - checksumsub; |
ottaviano3 | 0:8c8a8244e590 | 100 | |
ottaviano3 | 0:8c8a8244e590 | 101 | // finally write checksum |
ottaviano3 | 0:8c8a8244e590 | 102 | _xbeespi.write(checksum); |
ottaviano3 | 0:8c8a8244e590 | 103 | } |