XBee API mode library
Revision 16:cdfcb63b2c4b, committed 2013-03-20
- Comitter:
- yamaguch
- Date:
- Wed Mar 20 00:28:27 2013 +0000
- Parent:
- 15:10224db0fc2b
- Child:
- 17:2f728fd13bc0
- Commit message:
- non rtos version
Changed in this revision
--- a/Receive.cpp Thu Mar 14 09:45:41 2013 +0000 +++ b/Receive.cpp Wed Mar 20 00:28:27 2013 +0000 @@ -22,14 +22,9 @@ #include "XBee.h" -#ifndef XBEE_RTOS #define LOCK() NVIC_DisableIRQ(UARTx_IRQn[_serial.index]) #define UNLOCK() NVIC_EnableIRQ(UARTx_IRQn[_serial.index]) const IRQn_Type UARTx_IRQn[] = {UART0_IRQn, UART1_IRQn, UART2_IRQn, UART3_IRQn}; -#else -#define LOCK() mutex.lock() -#define UNLOCK() mutex.unlock(); -#endif XBee::FrameType XBee::receive(float timeout) { flush(); @@ -53,14 +48,8 @@ return None; } UNLOCK(); - -#ifndef XBEE_RTOS wait(0.001); timeout -= 0.001; -#else - Thread::wait(50); - timeout -= 0.05; -#endif } } @@ -71,30 +60,40 @@ * if the frame is found before timeout, returns the index of the packet, otherwise -1. * */ -int XBee::seekFor(FrameType type, char id, float timeout) { - int index = out; +int XBee::seekFor(FrameType type, char id, float timeout) +{ + LOCK(); - while (timeout > 0) { - if (index != in) { - if (getFrameType(buf[INDEX(index + 2)]) == type && (id == 0 || buf[INDEX(index + 3)] == id)) + while (out != in && getFrameType(buf[INDEX(out + 2)]) == None) { + int size = SIZE(buf, out); + out = INDEX(out + 2 + size); + free += 2 + size; + } + + int index = out; + while (true) { + if (index != in) { + if (getFrameType(buf[INDEX(index + 2)]) == type && (id == 0 || buf[INDEX(index + 3)] == id)) { + UNLOCK(); return index; + } + //::printf("seekFor: index = %02X\n", index); int size = SIZE(buf, index); index = INDEX(index + 2 + size); - } else { -#ifndef XBEE_RTOS + continue; + } else if (timeout <= 0) { + UNLOCK(); + return -1; + } + UNLOCK(); wait(0.001); timeout -= 0.001; -#else - Thread::wait(10); - timeout -= 0.01; -#endif - } + LOCK(); } - - return -1; } -XBee::FrameType XBee::getFrameType(char c) { +XBee::FrameType XBee::getFrameType(char c) +{ switch (c) { case 0x00: return None; @@ -121,7 +120,8 @@ } } -void XBee::flush() { +void XBee::flush() +{ LOCK(); if (received == out) { do { @@ -129,27 +129,46 @@ out = INDEX(out + 2 + size); free += 2 + size; } while (out != in && getFrameType(buf[INDEX(out + 2)]) == None); - if (debug) leds = leds & 12; //**LEDS=xx00 } UNLOCK(); } -#ifndef XBEE_RTOS -void XBee::rxInterruptHandler() { +void XBee::rxISR() { + static enum {UNKNOWN, LENGTH1, LENGTH2, DATA, SUMCHECK} state = UNKNOWN; static bool escaped = false; + int c = -1; + + switch (_serial.index) { + case 0: {// USBTX, USBRX + uint32_t UART_0_IIR = LPC_UART0->IIR; + if (readable()) c = LPC_UART0->RBR; + } + break; + case 1: {// p13/14 + uint32_t UART_1_IIR = LPC_UART1->IIR; + if (readable()) c = LPC_UART1->RBR; + } + break; + case 2: {// p28/27 + uint32_t UART_2_IIR = LPC_UART2->IIR; + if (readable()) c = LPC_UART2->RBR; + } + break; + case 3: {// p9/10 + uint32_t UART_3_IIR = LPC_UART3->IIR; + if (readable()) c = LPC_UART3->RBR; + } + break; + } - if (debug) leds = leds ^ 4; //**LEDS=x@xx - - while (readable()) { - char c = getc() & 255; - + if (c != -1) { if (apiMode == 2) { if (escaped) { c ^= 0x20; escaped = false; } else if (c == ESCAPE) { escaped = true; - continue; + return; } } @@ -159,142 +178,42 @@ buf[cur] = c; state = LENGTH2; break; + case LENGTH2: if ((buf[cur] << 8 | c) + 2 < BUFSIZE) { state = DATA; while (free < (buf[cur] << 8 | c) + 2) { int size = SIZE(buf, out); - out = INDEX(out + 2 + size); - free += size + 2; + out = INDEX(out + size + 2); + free += (size + 2); } buf[INDEX(cur + 1)] = c; cur = INDEX(cur + 2); - if (debug) leds = (leds & 12) | 1; //**LEDS=xx01 - } else { + } else state = UNKNOWN; - if (debug) leds = leds & 12; //**LEDS=xx00 - } break; + case DATA: buf[cur] = c; cur = INDEX(cur + 1); - if (debug) leds = (leds & 12) | (cur & 3); //**LEDS=xx@@ - if (cur == INDEX(in + 2 + SIZE(buf, in))) state = SUMCHECK; + if (cur == INDEX(in + 2 + SIZE(buf, in))) + state = SUMCHECK; break; + case SUMCHECK: for (int i = INDEX(in + 2); i != cur; i = INDEX(i + 1)) { c += buf[i]; } - if (c == 255) { - if (debug) leds = (leds & 12) | 3; //**LEDS=xx11 + if ((c & 255) == 255) { in = cur; - free = in < out ? out - in : BUFSIZE + out - in; + free = in <= out ? out - in : BUFSIZE + out - in; // maybe in == out, then free == 0, but != BUFSIZE } state = UNKNOWN; break; + default: - if (c == PREAMBLE) state = LENGTH1; - if (debug) leds = (leds & 12) | 1; //**LEDS=xx01 + if (c == PREAMBLE) + state = LENGTH1; } } -} -#else -void XBee::rxISR() { - int c = -1; - switch (_serial.index) { - case 0: { - uint32_t UART_0_IIR = LPC_UART0->IIR; - if (readable()) c = LPC_UART0->RBR; - } - break; - case 1: { - uint32_t UART_1_IIR = LPC_UART1->IIR; - if (readable()) c = LPC_UART1->RBR; - } - break; - case 2: { - uint32_t UART_2_IIR = LPC_UART2->IIR; - if (readable()) c = LPC_UART2->RBR; - } - break; - case 3: { - uint32_t UART_3_IIR = LPC_UART3->IIR; - if (readable()) c = LPC_UART3->RBR; - } - break; - } - - if (c != -1) { - rxBuf = c; - notify(); - } -} - -void XBee::run() { - bool escaped = false; - - while (true) { - wait(); - mutex.lock(); - if (debug && rxBuf.readable()) leds = leds ^ 4; // x@xx - - while (rxBuf.readable()) { - char c = rxBuf.getc() & 255; - if (apiMode == 2) { - if (escaped) { - c ^= 0x20; - escaped = false; - } else if (c == ESCAPE) { - escaped = true; - continue; - } - } - - switch (state) { - case LENGTH1: - cur = in; - buf[cur] = c; - state = LENGTH2; - break; - case LENGTH2: - if ((buf[cur] << 8 | c) + 2 < BUFSIZE) { - state = DATA; - while (free < (buf[cur] << 8 | c) + 2) { - int size = SIZE(buf, out); - out = INDEX(out + size + 2); - free += (size + 2); - } - buf[INDEX(cur + 1)] = c; - cur = INDEX(cur + 2); - if (debug) leds = (leds & 12) | 2; // xx10 - } else { - state = UNKNOWN; - if (debug) leds = leds & 12; //** update leds - } - break; - case DATA: - buf[cur] = c; - cur = INDEX(cur + 1); - if (debug) leds = (leds & 12) | (cur & 3); //**LEDS=xx@@ - if (cur == INDEX(in + 2 + SIZE(buf, in))) state = SUMCHECK; - break; - case SUMCHECK: - for (int i = INDEX(in + 2); i != cur; i = INDEX(i + 1)) { - c += buf[i]; - } - if (c == 255) { - if (debug) leds = (leds & 12) | 3; //**LEDS=xx11 - in = cur; - free = in <= out ? out - in : BUFSIZE + out - in; // maybe in == out, then free == 0, but != BUFSIZE - } - state = UNKNOWN; - break; - default: - if (c == PREAMBLE) state = LENGTH1; - if (debug) leds = (leds & 12) | 1; //**LEDS=xx01 - } - } - mutex.unlock(); - } -} -#endif \ No newline at end of file +} \ No newline at end of file
--- a/Scan.cpp Thu Mar 14 09:45:41 2013 +0000 +++ b/Scan.cpp Wed Mar 20 00:28:27 2013 +0000 @@ -80,7 +80,7 @@ case Address16: switch (frameType) { case ZigBeeTransmitStatus: - from = 4; + from = 2; break; case ZigBeeReceivePacket: case ZigBeeIODataSampleRxIndicator:
--- a/Send.cpp Thu Mar 14 09:45:41 2013 +0000 +++ b/Send.cpp Wed Mar 20 00:28:27 2013 +0000 @@ -23,6 +23,10 @@ #include "XBee.h" #include <stdarg.h> +#define LOCK() NVIC_DisableIRQ(UARTx_IRQn[_serial.index]) +#define UNLOCK() NVIC_EnableIRQ(UARTx_IRQn[_serial.index]) +const IRQn_Type UARTx_IRQn[] = {UART0_IRQn, UART1_IRQn, UART2_IRQn, UART3_IRQn}; + void XBee::sendCommand(const char *command, int8_t param, bool queue) { sendCommand(command, (uint64_t) param, queue); } @@ -143,19 +147,29 @@ createTxRequest(frame_id, data, length, buf, sizeof(buf)); sendFrame(buf, length + 14); - frame_id = frame_id % 255 + 1; + bool ret = true; if (timeout > 0) { int index = seekFor(ZigBeeTransmitStatus, frame_id, timeout); if (index != -1) { char delivery; scan(index, DeliveryStatus, &delivery); - buf[INDEX(index + 2)] = 0x00; - return delivery == 0; + this->buf[INDEX(index + 2)] = None; + //::printf("index = %02X, out = %02X, in = %02X\n", index, out, in); + if (index == out) { + LOCK(); + int size = SIZE(this->buf, out); + out = INDEX(out + 2 + size); + free += (2 + size); + UNLOCK(); + } + ret = (delivery == 0); } else - return false; + ret = false; } - return true; + + frame_id = frame_id % 255 + 1; + return ret; } void XBee::setSendConfirmation(float sendConfirmation) { @@ -191,7 +205,6 @@ void XBee::sendFrame(const char *frame, int length) { char checksum = 255; - if (debug) leds = leds | 8; //**LEDS=1xxx send(PREAMBLE); @@ -212,7 +225,6 @@ } send(checksum); } - if (debug) leds = leds & 7; //**LEDS=0xxx } int XBee::createTxRequest(char frame_id, const char *data, int data_length, char *buf, int buf_size) {
--- a/XBee.cpp Thu Mar 14 09:45:41 2013 +0000 +++ b/XBee.cpp Wed Mar 20 00:28:27 2013 +0000 @@ -22,67 +22,37 @@ #include "XBee.h" -#ifndef XBEE_RTOS -XBee::XBee(Serial& ser, int apiMode, bool debug) - : Serial(ser), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode), - state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug) { - memset(buf, 0, BUFSIZE); - attach(this, &XBee::rxInterruptHandler, RxIrq); -} - -XBee::XBee(PinName tx, PinName rx, int apiMode, bool debug) - : Serial(tx, rx), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode), - state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug) { - memset(buf, 0, BUFSIZE); - attach(this, &XBee::rxInterruptHandler, RxIrq); -} +#define LOCK() NVIC_DisableIRQ(UARTx_IRQn[_serial.index]) +#define UNLOCK() NVIC_EnableIRQ(UARTx_IRQn[_serial.index]) +const IRQn_Type UARTx_IRQn[] = {UART0_IRQn, UART1_IRQn, UART2_IRQn, UART3_IRQn}; -XBee::XBee(Serial& ser, Serial& mon, int apiMode, bool debug) - : Serial(ser), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode), - state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug) { +XBee::XBee(Serial& ser, int apiMode) + : Serial(ser), mon(USBTX, USBRX), apiMode(apiMode), + in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0) { memset(buf, 0, BUFSIZE); - attach(this, &XBee::rxInterruptHandler, RxIrq); -} - -XBee::XBee(PinName tx, PinName rx, Serial& mon, int apiMode, bool debug) - : Serial(tx, rx), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode), - state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug) { - memset(buf, 0, BUFSIZE); - attach(this, &XBee::rxInterruptHandler, RxIrq); -} -#else -XBee::XBee(Serial& ser, int apiMode, bool debug) - : Serial(ser), AbstractThread(osPriorityAboveNormal), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode), - state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug) { - memset(buf, 0, BUFSIZE); - start(); attach(this, &XBee::rxISR, RxIrq); } -XBee::XBee(PinName tx, PinName rx, int apiMode, bool debug) - : Serial(tx, rx), AbstractThread(osPriorityAboveNormal), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode), - state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug) { +XBee::XBee(PinName tx, PinName rx, int apiMode) + : Serial(tx, rx), mon(USBTX, USBRX), apiMode(apiMode), + in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0) { memset(buf, 0, BUFSIZE); - start(); attach(this, &XBee::rxISR, RxIrq); } -XBee::XBee(Serial& ser, Serial& mon, int apiMode, bool debug) - : Serial(ser), AbstractThread(osPriorityAboveNormal), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode), - state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug) { +XBee::XBee(Serial& ser, Serial& mon, int apiMode) + : Serial(ser), mon(mon), apiMode(apiMode), + in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0) { memset(buf, 0, BUFSIZE); - start(); attach(this, &XBee::rxISR, RxIrq); } -XBee::XBee(PinName tx, PinName rx, Serial& mon, int apiMode, bool debug) - : Serial(tx, rx), AbstractThread(osPriorityAboveNormal), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode), - state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug) { +XBee::XBee(PinName tx, PinName rx, Serial& mon, int apiMode) + : Serial(tx, rx), mon(mon), apiMode(apiMode), + in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0) { memset(buf, 0, BUFSIZE); - start(); attach(this, &XBee::rxISR, RxIrq); } -#endif bool XBee::init(float timeout) { while (readable()) getc(); @@ -202,7 +172,14 @@ } } } - buf[INDEX(index + 2)] = 0x00; + buf[INDEX(index + 2)] = None; + if (index == out) { + LOCK(); + int size = SIZE(buf, out); + out = INDEX(out + 2 + size); + free += (2 + size); + UNLOCK(); + } } return succeeded ? data : 0;
--- a/XBee.h Thu Mar 14 09:45:41 2013 +0000 +++ b/XBee.h Wed Mar 20 00:28:27 2013 +0000 @@ -23,13 +23,6 @@ #ifndef XBEE_H #define XBEE_H -#define XBEE_RTOS - -#ifdef XBEE_RTOS -#include "rtos.h" -#include "AbstractThread.h" -#include "RingBuffer.h" -#endif #include "mbed.h" #include "XBeeDataTypes.h" @@ -44,11 +37,7 @@ /** * class for XBee module API mode interface */ -class XBee : public Serial -#ifdef XBEE_RTOS -, AbstractThread -#endif -{ +class XBee : public Serial { public: /** @@ -213,7 +202,7 @@ * @param apiMode API mode either 1 or 2 (use escape; default) * @param debug display debugging (I/O state) information through LEDs */ - XBee(Serial& ser, int apiMode = 2, bool debug = false); + XBee(Serial& ser, int apiMode = 2); /** * creates an XBee interface object. @@ -223,7 +212,7 @@ * @param apiMode API mode either 1 or 2 (use escape; default) * @param debug display debugging (I/O state) information through LEDs */ - XBee(PinName tx, PinName rx, int apiMode = 2, bool debug = false); + XBee(PinName tx, PinName rx, int apiMode = 2); /** * creates an XBee interface object. @@ -233,7 +222,7 @@ * @param apiMode API mode either 1 or 2 (use escape; default) * @param debug display debugging (I/O state) information through LEDs */ - XBee(Serial& ser, Serial& mon, int apiMode = 2, bool debug = false); + XBee(Serial& ser, Serial& mon, int apiMode = 2); /** * creates an XBee interface object. @@ -244,7 +233,7 @@ * @param apiMode API mode either 1 or 2 (use escape; default) * @param debug display debugging (I/O state) information through LEDs */ - XBee(PinName tx, PinName rx, Serial& mon, int apiMode = 2, bool debug = false); + XBee(PinName tx, PinName rx, Serial& mon, int apiMode = 2); /** * initializes XBee module. @@ -479,22 +468,15 @@ private: Serial mon; - BusOut leds; int apiMode; - volatile enum {UNKNOWN, LENGTH1, LENGTH2, DATA, SUMCHECK} state; volatile int cur, in, out, received, free; char frame_id; float sendConfirmation; char destination64[8]; char destination16[2]; char buf[BUFSIZE]; -#ifdef XBEE_RTOS - RingBuffer<char, 128> rxBuf; -#endif bool debug; -#ifdef XBEE_RTOS - Mutex mutex; -#endif + void send(char c); void send2(char c); void sendFrame(const char *frame, int length); @@ -506,12 +488,7 @@ FrameType getFrameType(char c); bool scan(int i, ValueType type, char *value, int maxlength = 1, int *length = 0); void flush(); -#ifndef XBEE_RTOS - void rxInterruptHandler(); -#else void rxISR(); - virtual void run(); -#endif void dump(const char *data, int length); void dumpIOSample(const char *data, int length); void copy(char *toBuf, int fromIndex, int length);