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

Committer:
ottaviano3
Date:
Wed Apr 22 15:31:18 2015 +0000
Revision:
1:b97d46c5d7ce
Parent:
0:8c8a8244e590
Child:
2:7f4ddf710a44
fixed dout settings

Who changed what in which revision?

UserRevisionLine numberNew 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 1:b97d46c5d7ce 11
ottaviano3 1:b97d46c5d7ce 12 _pin_attn.fall(this, &xbee900hp::readDataISR);
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 1:b97d46c5d7ce 21 * Read Data ISR
ottaviano3 1:b97d46c5d7ce 22 */
ottaviano3 1:b97d46c5d7ce 23 void xbee900hp::readDataISR() {
ottaviano3 1:b97d46c5d7ce 24 // Nothing Yet
ottaviano3 1:b97d46c5d7ce 25 }
ottaviano3 0:8c8a8244e590 26
ottaviano3 0:8c8a8244e590 27 /**
ottaviano3 0:8c8a8244e590 28 * Reset xBee to SPI mode
ottaviano3 0:8c8a8244e590 29 */
ottaviano3 0:8c8a8244e590 30 void xbee900hp::reset()
ottaviano3 0:8c8a8244e590 31 {
ottaviano3 0:8c8a8244e590 32 // Set Xbee to SPI mode
ottaviano3 0:8c8a8244e590 33 _pin_rst = 0;
ottaviano3 0:8c8a8244e590 34 // Minimum pulse is 1ms
ottaviano3 0:8c8a8244e590 35 wait_ms(1);
ottaviano3 0:8c8a8244e590 36 _pin_rst = 1;
ottaviano3 0:8c8a8244e590 37 // wait for module to come back online
ottaviano3 0:8c8a8244e590 38 wait_ms(500);
ottaviano3 0:8c8a8244e590 39 }
ottaviano3 0:8c8a8244e590 40
ottaviano3 0:8c8a8244e590 41 /**
ottaviano3 0:8c8a8244e590 42 * Send packet out on RF
ottaviano3 0:8c8a8244e590 43 */
ottaviano3 0:8c8a8244e590 44 void xbee900hp::sendPacket(char* data, unsigned int length)
ottaviano3 0:8c8a8244e590 45 {
ottaviano3 0:8c8a8244e590 46 // checksum
ottaviano3 0:8c8a8244e590 47 unsigned int checksum;
ottaviano3 0:8c8a8244e590 48 unsigned int checksumsub = 0;
ottaviano3 0:8c8a8244e590 49
ottaviano3 0:8c8a8244e590 50 // start char
ottaviano3 0:8c8a8244e590 51 _xbeespi.write(0x7E);
ottaviano3 0:8c8a8244e590 52 // lenght
ottaviano3 0:8c8a8244e590 53 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 54
ottaviano3 0:8c8a8244e590 55 unsigned int totallength = 14 + length - 1;
ottaviano3 0:8c8a8244e590 56 _xbeespi.write(totallength);
ottaviano3 0:8c8a8244e590 57
ottaviano3 0:8c8a8244e590 58 // frame delimter
ottaviano3 0:8c8a8244e590 59 _xbeespi.write(0x10);
ottaviano3 0:8c8a8244e590 60 checksumsub += 0x10;
ottaviano3 0:8c8a8244e590 61 // id for later reference 0 = no id
ottaviano3 0:8c8a8244e590 62 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 63
ottaviano3 0:8c8a8244e590 64 // destination address
ottaviano3 0:8c8a8244e590 65 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 66 _xbeespi.write(0x00);
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(0xFF);
ottaviano3 0:8c8a8244e590 72 checksumsub += 0xFF;
ottaviano3 0:8c8a8244e590 73 _xbeespi.write(0xFF);
ottaviano3 0:8c8a8244e590 74 checksumsub += 0xFF;
ottaviano3 0:8c8a8244e590 75
ottaviano3 0:8c8a8244e590 76 // reserved field, dont change
ottaviano3 0:8c8a8244e590 77 _xbeespi.write(0xFF);
ottaviano3 0:8c8a8244e590 78 checksumsub += 0xFF;
ottaviano3 0:8c8a8244e590 79 _xbeespi.write(0xFE);
ottaviano3 0:8c8a8244e590 80 checksumsub += 0xFE;
ottaviano3 0:8c8a8244e590 81
ottaviano3 0:8c8a8244e590 82 //bcast radius
ottaviano3 0:8c8a8244e590 83 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 84
ottaviano3 0:8c8a8244e590 85 //transmit options 0x00 to disable ACK
ottaviano3 0:8c8a8244e590 86 _xbeespi.write(0x00);
ottaviano3 0:8c8a8244e590 87
ottaviano3 0:8c8a8244e590 88 // dat data
ottaviano3 0:8c8a8244e590 89 for (int i = 0; i < (length - 1); i++) {
ottaviano3 0:8c8a8244e590 90 _xbeespi.write(*data);
ottaviano3 0:8c8a8244e590 91 checksumsub += (*(data++));
ottaviano3 0:8c8a8244e590 92 }
ottaviano3 0:8c8a8244e590 93
ottaviano3 0:8c8a8244e590 94
ottaviano3 0:8c8a8244e590 95 // Calculate checksum
ottaviano3 0:8c8a8244e590 96 checksumsub = checksumsub & 0xFF;
ottaviano3 0:8c8a8244e590 97 checksum = 0xFF - checksumsub;
ottaviano3 0:8c8a8244e590 98
ottaviano3 0:8c8a8244e590 99 // finally write checksum
ottaviano3 0:8c8a8244e590 100 _xbeespi.write(checksum);
ottaviano3 1:b97d46c5d7ce 101 }
ottaviano3 1:b97d46c5d7ce 102
ottaviano3 1:b97d46c5d7ce 103 void xbee900hp::writeSetting(char command[2], unsigned int value) {
ottaviano3 1:b97d46c5d7ce 104 // checksum Variables
ottaviano3 1:b97d46c5d7ce 105 unsigned int checksum = 0;
ottaviano3 1:b97d46c5d7ce 106 unsigned int checksumsub = 0;
ottaviano3 1:b97d46c5d7ce 107
ottaviano3 1:b97d46c5d7ce 108 // Start config, send frames for SPI pins enable
ottaviano3 1:b97d46c5d7ce 109 _xbeespi.write(0x7E);
ottaviano3 1:b97d46c5d7ce 110 _xbeespi.write(0x00);
ottaviano3 1:b97d46c5d7ce 111 _xbeespi.write(0x05);
ottaviano3 1:b97d46c5d7ce 112
ottaviano3 1:b97d46c5d7ce 113 // Frame type (config)
ottaviano3 1:b97d46c5d7ce 114 _xbeespi.write(0x08);
ottaviano3 1:b97d46c5d7ce 115 checksumsub += 0x08;
ottaviano3 1:b97d46c5d7ce 116 _xbeespi.write(0x00);
ottaviano3 1:b97d46c5d7ce 117
ottaviano3 1:b97d46c5d7ce 118
ottaviano3 1:b97d46c5d7ce 119 // AT Command
ottaviano3 1:b97d46c5d7ce 120 for (int i = 0; i < 2; i++) {
ottaviano3 1:b97d46c5d7ce 121 _xbeespi.write(*command);
ottaviano3 1:b97d46c5d7ce 122 checksumsub += (*(command++));
ottaviano3 1:b97d46c5d7ce 123 }
ottaviano3 1:b97d46c5d7ce 124
ottaviano3 1:b97d46c5d7ce 125 // Value to set
ottaviano3 1:b97d46c5d7ce 126 _xbeespi.write(value);
ottaviano3 1:b97d46c5d7ce 127 checksumsub += value;
ottaviano3 1:b97d46c5d7ce 128
ottaviano3 1:b97d46c5d7ce 129 // Calculate checksum
ottaviano3 1:b97d46c5d7ce 130 checksumsub = checksumsub & 0xFF;
ottaviano3 1:b97d46c5d7ce 131 checksum = 0xFF - checksumsub;
ottaviano3 1:b97d46c5d7ce 132
ottaviano3 1:b97d46c5d7ce 133 // finally write checksum
ottaviano3 1:b97d46c5d7ce 134 _xbeespi.write(checksum);
ottaviano3 0:8c8a8244e590 135 }