Library used to receive data through a wireless channel using XBEE.

Dependencies:   mbed

Dependents:   nxp_can

Fork of frdm_xbee by Jose Pesado

Committer:
josepesado
Date:
Sat Oct 18 13:21:18 2014 +0000
Revision:
0:06bd131cf762
Child:
1:11f87e6215f7
Additional updates to the XBee services.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
josepesado 0:06bd131cf762 1 #include "mbed.h"
josepesado 0:06bd131cf762 2
josepesado 0:06bd131cf762 3 #define XBEE_SERVER 1
josepesado 0:06bd131cf762 4 #define XBEE_CLIENT 1
josepesado 0:06bd131cf762 5 #define SERIAL_BAUDRATE 115200
josepesado 0:06bd131cf762 6 #define BUFFER_SIZE 9
josepesado 0:06bd131cf762 7 #define BUFFER_FORMAT "%X %X %X %X %X %X %X %X %X"
josepesado 0:06bd131cf762 8 #define BUFFER_FORMAT_RX "%c%c%c%c%c%c%c%c%c"
josepesado 0:06bd131cf762 9 #define BUFFER_RX_DATA_WR &CommandRx[0],&CommandRx[1],&CommandRx[2],&CommandRx[3],\
josepesado 0:06bd131cf762 10 &CommandRx[4],&CommandRx[5],&CommandRx[6],&CommandRx[7],\
josepesado 0:06bd131cf762 11 &CommandRx[8]
josepesado 0:06bd131cf762 12 #define BUFFER_RX_DATA_RD CommandRx[0],CommandRx[1],CommandRx[2],CommandRx[3],\
josepesado 0:06bd131cf762 13 CommandRx[4],CommandRx[5],CommandRx[6],CommandRx[7],\
josepesado 0:06bd131cf762 14 CommandRx[8]
josepesado 0:06bd131cf762 15
josepesado 0:06bd131cf762 16 Serial PC(USBTX, USBRX);
josepesado 0:06bd131cf762 17 DigitalOut Indicator(LED_GREEN);
josepesado 0:06bd131cf762 18 int XBee_u16Loop;
josepesado 0:06bd131cf762 19
josepesado 0:06bd131cf762 20 #if XBEE_SERVER == 1
josepesado 0:06bd131cf762 21 Serial XBeeServer(PTC17, PTC16); /* Tx, Rx*/
josepesado 0:06bd131cf762 22 char CommandTx[BUFFER_SIZE+1]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, '\0'};
josepesado 0:06bd131cf762 23 DigitalOut RSTServer(PTB9);
josepesado 0:06bd131cf762 24 int XBee_u16TxResult;
josepesado 0:06bd131cf762 25 int XBee_u8TxRq;
josepesado 0:06bd131cf762 26 #endif
josepesado 0:06bd131cf762 27
josepesado 0:06bd131cf762 28 #if XBEE_CLIENT == 1
josepesado 0:06bd131cf762 29 Serial XBeeClient(PTE24, PTE25); /* Tx, Rx*/
josepesado 0:06bd131cf762 30 DigitalOut RSTClient(PTA1);
josepesado 0:06bd131cf762 31 char CommandRx[BUFFER_SIZE+1];
josepesado 0:06bd131cf762 32 volatile int XBee_u8RxResult;
josepesado 0:06bd131cf762 33 int XBee_boRxIndication = 0;
josepesado 0:06bd131cf762 34 int XBee_boRxAllowed = 1;
josepesado 0:06bd131cf762 35
josepesado 0:06bd131cf762 36 /* This function is called when a character goes into the RX buffer.*/
josepesado 0:06bd131cf762 37 void XBeeClientRx() {
josepesado 0:06bd131cf762 38 if(XBee_boRxAllowed == 1)
josepesado 0:06bd131cf762 39 {
josepesado 0:06bd131cf762 40 PC.printf("\r\nRx... ");
josepesado 0:06bd131cf762 41 XBee_u8RxResult = 0;
josepesado 0:06bd131cf762 42 XBee_u8RxResult = XBeeClient.scanf( BUFFER_FORMAT_RX, BUFFER_RX_DATA_WR );
josepesado 0:06bd131cf762 43 if(XBee_u8RxResult>0)
josepesado 0:06bd131cf762 44 {
josepesado 0:06bd131cf762 45 XBee_boRxIndication = 1;
josepesado 0:06bd131cf762 46 }
josepesado 0:06bd131cf762 47 PC.printf("\r\nRx done [%i]. ",XBee_u8RxResult);
josepesado 0:06bd131cf762 48 PC.printf( BUFFER_FORMAT, BUFFER_RX_DATA_RD );
josepesado 0:06bd131cf762 49 }
josepesado 0:06bd131cf762 50 return;
josepesado 0:06bd131cf762 51 }
josepesado 0:06bd131cf762 52 #endif
josepesado 0:06bd131cf762 53
josepesado 0:06bd131cf762 54 void XBee_vInit(void)
josepesado 0:06bd131cf762 55 {
josepesado 0:06bd131cf762 56 XBee_u16Loop = 0;
josepesado 0:06bd131cf762 57
josepesado 0:06bd131cf762 58 PC.baud(SERIAL_BAUDRATE);
josepesado 0:06bd131cf762 59
josepesado 0:06bd131cf762 60 #if XBEE_SERVER == 1
josepesado 0:06bd131cf762 61 PC.printf("\r\nSetting server XBee...");
josepesado 0:06bd131cf762 62 XBeeServer.baud(SERIAL_BAUDRATE);
josepesado 0:06bd131cf762 63 XBee_u16TxResult = 0;
josepesado 0:06bd131cf762 64 XBee_u8TxRq = 0;
josepesado 0:06bd131cf762 65 #endif
josepesado 0:06bd131cf762 66
josepesado 0:06bd131cf762 67 #if XBEE_CLIENT == 1
josepesado 0:06bd131cf762 68 PC.printf("\r\nSetting client XBee...");
josepesado 0:06bd131cf762 69 XBeeClient.baud(SERIAL_BAUDRATE);
josepesado 0:06bd131cf762 70 XBeeClient.attach(&XBeeClientRx, Serial::RxIrq);
josepesado 0:06bd131cf762 71 XBee_u8RxResult = 0;
josepesado 0:06bd131cf762 72 XBee_boRxAllowed = 1;
josepesado 0:06bd131cf762 73 #endif
josepesado 0:06bd131cf762 74
josepesado 0:06bd131cf762 75 /* XBee reset*/
josepesado 0:06bd131cf762 76 #if XBEE_SERVER == 1
josepesado 0:06bd131cf762 77 RSTServer = 0;
josepesado 0:06bd131cf762 78 #endif
josepesado 0:06bd131cf762 79 #if XBEE_CLIENT == 1
josepesado 0:06bd131cf762 80 RSTClient = 0;
josepesado 0:06bd131cf762 81 #endif
josepesado 0:06bd131cf762 82 wait_ms(1);
josepesado 0:06bd131cf762 83 #if XBEE_SERVER == 1
josepesado 0:06bd131cf762 84 RSTServer = 1;
josepesado 0:06bd131cf762 85 #endif
josepesado 0:06bd131cf762 86 #if XBEE_CLIENT == 1
josepesado 0:06bd131cf762 87 RSTClient = 1;
josepesado 0:06bd131cf762 88 #endif
josepesado 0:06bd131cf762 89 wait_ms(1);
josepesado 0:06bd131cf762 90 }
josepesado 0:06bd131cf762 91
josepesado 0:06bd131cf762 92 void XBee_vMain(void)
josepesado 0:06bd131cf762 93 {
josepesado 0:06bd131cf762 94 XBee_u16Loop++;
josepesado 0:06bd131cf762 95 PC.printf("\r\n[%i]:",XBee_u16Loop);
josepesado 0:06bd131cf762 96
josepesado 0:06bd131cf762 97 if(XBee_u8TxRq == 1)
josepesado 0:06bd131cf762 98 {
josepesado 0:06bd131cf762 99 /* a Tx job request is active */
josepesado 0:06bd131cf762 100 PC.printf("\r\nTx... ");
josepesado 0:06bd131cf762 101 /* disable rx */
josepesado 0:06bd131cf762 102 XBee_boRxAllowed = 0;
josepesado 0:06bd131cf762 103 for (int i=0; i<BUFFER_SIZE; i++)
josepesado 0:06bd131cf762 104 {
josepesado 0:06bd131cf762 105 while (true)
josepesado 0:06bd131cf762 106 {
josepesado 0:06bd131cf762 107 if (XBeeServer.writeable())
josepesado 0:06bd131cf762 108 {
josepesado 0:06bd131cf762 109 XBee_u16TxResult= XBeeServer.putc(CommandTx[i]);
josepesado 0:06bd131cf762 110 PC.printf("%X ", XBee_u16TxResult);
josepesado 0:06bd131cf762 111 break;
josepesado 0:06bd131cf762 112 }
josepesado 0:06bd131cf762 113 }
josepesado 0:06bd131cf762 114 }
josepesado 0:06bd131cf762 115 /* tx done */
josepesado 0:06bd131cf762 116 PC.printf("\r\nTx done. ");
josepesado 0:06bd131cf762 117 XBee_u8TxRq = 0;
josepesado 0:06bd131cf762 118 /* enable rx */
josepesado 0:06bd131cf762 119 XBee_boRxAllowed = 1;
josepesado 0:06bd131cf762 120 }
josepesado 0:06bd131cf762 121 }
josepesado 0:06bd131cf762 122
josepesado 0:06bd131cf762 123 #if 1
josepesado 0:06bd131cf762 124
josepesado 0:06bd131cf762 125 int main()
josepesado 0:06bd131cf762 126 {
josepesado 0:06bd131cf762 127 char TxJob[BUFFER_SIZE+1] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, '\0'};
josepesado 0:06bd131cf762 128
josepesado 0:06bd131cf762 129 XBee_vInit();
josepesado 0:06bd131cf762 130
josepesado 0:06bd131cf762 131 while (true) {
josepesado 0:06bd131cf762 132 wait(0.2f); // wait a small period of time
josepesado 0:06bd131cf762 133 Indicator = !Indicator; // toggle a led
josepesado 0:06bd131cf762 134
josepesado 0:06bd131cf762 135 XBee_vMain();
josepesado 0:06bd131cf762 136
josepesado 0:06bd131cf762 137 memcpy(CommandTx,TxJob,9);
josepesado 0:06bd131cf762 138 //XBee_u8TxRq = 1;
josepesado 0:06bd131cf762 139
josepesado 0:06bd131cf762 140 for(int i=0;i<BUFFER_SIZE;i++)
josepesado 0:06bd131cf762 141 {
josepesado 0:06bd131cf762 142 TxJob[i]++;
josepesado 0:06bd131cf762 143 }
josepesado 0:06bd131cf762 144
josepesado 0:06bd131cf762 145 }
josepesado 0:06bd131cf762 146 }
josepesado 0:06bd131cf762 147 #endif
josepesado 0:06bd131cf762 148