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:
- 3:3c3707b0f5cd
- Parent:
- 2:7f4ddf710a44
- Child:
- 4:4083baa871fb
--- a/xbee900hp.cpp Thu Apr 23 17:09:28 2015 +0000 +++ b/xbee900hp.cpp Wed Apr 29 23:25:43 2015 +0000 @@ -195,4 +195,228 @@ // finally write checksum _xbeespi.write(checksum); +} + +int xbee900hp::getSerial(char* serialnumber) { + ///////////////////////////////////////////////////Send portion + // checksum Variables + unsigned int checksum = 0; + unsigned int checksumsub = 0; + + // Write frame to return serial high and low.s + + // Start config, send frames for SPI pins enable + _xbeespi.write(0x7E); + _xbeespi.write(0x00); + _xbeespi.write(0x04); + + // Frame type (config) + _xbeespi.write(0x08); + checksumsub += 0x08; + _xbeespi.write(0x52); + checksumsub += 0x52; + + // Setting wanted + _xbeespi.write('S'); + checksumsub += 'S'; + _xbeespi.write('H'); + checksumsub += 'H'; + + // Calculate checksum + checksumsub = checksumsub & 0xFF; + checksum = 0xFF - checksumsub; + // finally write checksum + _xbeespi.write(checksum); + + //////////////////////////////////////RECIEVE PORTION + // Block until xbee replys + while (_pin_attn != 0) {} + + // Containers for read values + char temp1 = 0; + char temp2 = 0; + + // reset checksum to zero. + checksum = 0; + checksumsub = 0; + + // get start byte + 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 response packet. + temp1 = _xbeespi.write(0x00); + if (temp1 != 0x88) { + // drop packet + return 1; + } + checksumsub += temp1; + + // get response frame id + temp1 = _xbeespi.write(0x00); + if (temp1 != 0x52) { + // drop packet + return 1; + } + checksumsub += temp1; + + // get at response parameter + temp1 = _xbeespi.write(0x00); + checksumsub += temp1; + temp2 = _xbeespi.write(0x00); + checksumsub += temp2; + + if ((temp1 != 'S') || (temp2 != 'H')) { + return 1; + // drop + } + + // Check OK flag + temp1 = _xbeespi.write(0x00); + if (temp1 != 0x00) { + return 1; + // drop + } + checksumsub += temp1; + + // Now for the sweet sweet data. + for (int i = 0; i<(length-5); i++) { + *serialnumber = _xbeespi.write(0x00); + checksumsub += *(serialnumber++); + } + + // 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; + } + +//////////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////Send portion + // checksum Variables + checksum = 0; + checksumsub = 0; + + // Write frame to return serial high and low.s + + // Start config, send frames for SPI pins enable + _xbeespi.write(0x7E); + _xbeespi.write(0x00); + _xbeespi.write(0x04); + + // Frame type (config) + _xbeespi.write(0x08); + checksumsub += 0x08; + _xbeespi.write(0x52); + checksumsub += 0x52; + + // Setting wanted + _xbeespi.write('S'); + checksumsub += 'S'; + _xbeespi.write('L'); + checksumsub += 'L'; + + // Calculate checksum + checksumsub = checksumsub & 0xFF; + checksum = 0xFF - checksumsub; + // finally write checksum + _xbeespi.write(checksum); + + //////////////////////////////////////RECIEVE PORTION + // Block until xbee replys + while (_pin_attn != 0) {} + + // Containers for read values + temp1 = 0; + temp2 = 0; + + // reset checksum to zero. + checksum = 0; + checksumsub = 0; + + // get start byte + 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 + length = (temp1<<8) | temp2; + + // Next read frame type to ensure it is an response packet. + temp1 = _xbeespi.write(0x00); + if (temp1 != 0x88) { + // drop packet + return 1; + } + checksumsub += temp1; + + // get response frame id + temp1 = _xbeespi.write(0x00); + if (temp1 != 0x52) { + // drop packet + return 1; + } + checksumsub += temp1; + + // get at response parameter + temp1 = _xbeespi.write(0x00); + checksumsub += temp1; + temp2 = _xbeespi.write(0x00); + checksumsub += temp2; + + if ((temp1 != 'S') || (temp2 != 'L')) { + return 1; + // drop + } + + // Check OK flag + temp1 = _xbeespi.write(0x00); + if (temp1 != 0x00) { + return 1; + // drop + } + checksumsub += temp1; + + // Now for the sweet sweet data. + for (int i = 0; i<(length-5); i++) { + *serialnumber = _xbeespi.write(0x00); + checksumsub += *(serialnumber++); + } + // Null terminate char array. + *serialnumber = 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; + } \ No newline at end of file