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
Diff: xbee900hp.cpp
- Revision:
- 2:7f4ddf710a44
- Parent:
- 1:b97d46c5d7ce
- Child:
- 3:3c3707b0f5cd
--- a/xbee900hp.cpp Wed Apr 22 15:31:18 2015 +0000 +++ b/xbee900hp.cpp Thu Apr 23 17:09:28 2015 +0000 @@ -8,8 +8,8 @@ { _xbeespi.format(8,0); _xbeespi.frequency(1000000); - - _pin_attn.fall(this, &xbee900hp::readDataISR); + + reset(); } /** @@ -18,18 +18,10 @@ xbee900hp::~xbee900hp() {} /** -* Read Data ISR -*/ -void xbee900hp::readDataISR() { - // Nothing Yet -} - -/** * Reset xBee to SPI mode */ void xbee900hp::reset() { - // Set Xbee to SPI mode _pin_rst = 0; // Minimum pulse is 1ms wait_ms(1); @@ -100,6 +92,77 @@ _xbeespi.write(checksum); } +int xbee900hp::readPacket(char* data) { + unsigned int temp1; + unsigned int temp2; + + unsigned int checksumsub = 0; + unsigned int checksum; + + // get first vars. + temp1 = _xbeespi.write(0x00); + if (temp1 != 0x7E) { + // drop packet + return 1; + } + // Get length of message + temp1 = _xbeespi.write(0x00); + temp2 = _xbeespi.write(0x00); + + // Get total length + unsigned int length = (temp1<<8) | temp2; + + // Next read frame type to ensure it is an RX packet. + temp1 = _xbeespi.write(0x00); + if (temp1 != 0x90) { + // drop packet + return 1; + } + checksumsub += temp1; + + // in our case we dont care about source address this should be modified to extract source address if needed. + checksumsub += _xbeespi.write(0x00); + checksumsub += _xbeespi.write(0x00); + checksumsub += _xbeespi.write(0x00); + checksumsub += _xbeespi.write(0x00); + checksumsub += _xbeespi.write(0x00); + checksumsub += _xbeespi.write(0x00); + checksumsub += _xbeespi.write(0x00); + checksumsub += _xbeespi.write(0x00); + // reserved field, we dont care about except for checksum + checksumsub += _xbeespi.write(0x00); + checksumsub += _xbeespi.write(0x00); + + // recive options we also dont care though + checksumsub += _xbeespi.write(0x00); + + // Now for the sweet sweet data. + for (int i = 0; i<(length-12); i++) { + *data = _xbeespi.write(0x00); + checksumsub += *(data++); + } + // Null terminate char array. + *data = NULL; + + // Get that salty checksum + temp1 = _xbeespi.write(0x00); + + checksumsub = checksumsub & 0xFF; + checksum = 0xFF - checksumsub; + + // Check the checksum + if (temp1 != checksum) { + // Checksum failure, flag to discard packet + return 1; + } + + return 0; +} + +int xbee900hp::attn() { + return _pin_attn; +} + void xbee900hp::writeSetting(char command[2], unsigned int value) { // checksum Variables unsigned int checksum = 0;