xbee communication for UWB quadcopter project Originally by Greg Abdo Forking to reduce impact of interrupt by moving packetbuilder out of the interrupt and letting be handled in the main loop

Fork of com by Prosper Van

Committer:
oprospero
Date:
Thu Oct 23 04:46:10 2014 +0000
Revision:
21:117e01fc0373
Parent:
20:95244d4f47f6
Child:
22:0e8e22f161ff
reverted back to single Serial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oprospero 0:26a151d2c6db 1
oprospero 0:26a151d2c6db 2 #include "com.h"
oprospero 17:acef0fb07510 3
oprospero 18:19bcb2dbf3c8 4 //#define DEBUG_COM
oprospero 13:2fb7b19dcd70 5 #ifdef DEBUG_COM
oprospero 13:2fb7b19dcd70 6 #define NL "\n\r"
oprospero 17:acef0fb07510 7 #define PRINT(x) xbee.printf(x) //Serial.print(x)
oprospero 17:acef0fb07510 8 #define PRINTF xbee.printf //Serial.print(x, y)
oprospero 16:89695823d407 9 #define PRINTLN(x) PRINT(x);PRINT(NL)
oprospero 16:89695823d407 10 #define START timerCom.start();
oprospero 16:89695823d407 11 #define STOP timerCom.stop()
oprospero 16:89695823d407 12 #define RESET timerCom.reset()
oprospero 16:89695823d407 13 #define READ timerCom.read_us()
oprospero 16:89695823d407 14 #define GET(x) x = READ
oprospero 13:2fb7b19dcd70 15 #else
oprospero 13:2fb7b19dcd70 16 #define PRINT(x)
oprospero 13:2fb7b19dcd70 17 #define PRINTF(x, y)
oprospero 13:2fb7b19dcd70 18 #define PRINTLN(x)
oprospero 13:2fb7b19dcd70 19 #define PRINTLNF(x, y)
oprospero 13:2fb7b19dcd70 20 #define START
oprospero 13:2fb7b19dcd70 21 #define STOP
oprospero 13:2fb7b19dcd70 22 #define RESET
oprospero 13:2fb7b19dcd70 23 #define READ
oprospero 13:2fb7b19dcd70 24 #define GET(x)
oprospero 13:2fb7b19dcd70 25 #endif
oprospero 0:26a151d2c6db 26
oprospero 21:117e01fc0373 27 //com::com(PinName tx, PinName rx): xbee(NC,rx), xbtx(tx,NC)
oprospero 21:117e01fc0373 28 com::com(PinName tx, PinName rx): xbee(tx,rx)
oprospero 0:26a151d2c6db 29 {
oprospero 21:117e01fc0373 30 // xbtx.baud(BAUDRATE);
oprospero 17:acef0fb07510 31 xbee.baud(BAUDRATE);
oprospero 17:acef0fb07510 32 xbee.attach(this,&com::callback);
oprospero 0:26a151d2c6db 33 }
oprospero 0:26a151d2c6db 34
oprospero 17:acef0fb07510 35 com::~com()
oprospero 17:acef0fb07510 36 {
oprospero 0:26a151d2c6db 37 }
oprospero 0:26a151d2c6db 38
oprospero 17:acef0fb07510 39 void com::callback()
oprospero 4:f8e953201251 40 {
oprospero 19:dc9d18037565 41 __disable_irq();
oprospero 17:acef0fb07510 42 while(xbee.readable())
oprospero 4:f8e953201251 43 {
oprospero 17:acef0fb07510 44 char data = xbee.getc();
oprospero 21:117e01fc0373 45 xbee.putc(data);
oprospero 17:acef0fb07510 46 rxBuffer.add(data);
oprospero 4:f8e953201251 47 }
oprospero 19:dc9d18037565 48 __enable_irq();
oprospero 4:f8e953201251 49 }
oprospero 4:f8e953201251 50
oprospero 16:89695823d407 51 bool com::isData()
oprospero 16:89695823d407 52 {
oprospero 17:acef0fb07510 53 static char packetIndex = 0;
oprospero 17:acef0fb07510 54 static char pack_cmd = 0;
oprospero 17:acef0fb07510 55 static char pack_value[2] = {0,0};
oprospero 17:acef0fb07510 56 static char pack_seq = 0;
oprospero 17:acef0fb07510 57 static char pack_checksum = 0;
oprospero 16:89695823d407 58
oprospero 16:89695823d407 59 __disable_irq();
oprospero 17:acef0fb07510 60 while ( !rxBuffer.isEmpty() )
oprospero 16:89695823d407 61 {
oprospero 17:acef0fb07510 62 char data = rxBuffer.pop();
oprospero 20:95244d4f47f6 63 // __enable_irq();
oprospero 17:acef0fb07510 64 // PRINTF("d: %d \n\r", data);
oprospero 16:89695823d407 65 switch (packetIndex)
oprospero 16:89695823d407 66 {
oprospero 16:89695823d407 67 case 0:
oprospero 16:89695823d407 68 {
oprospero 17:acef0fb07510 69 if ( data == 255 )
oprospero 16:89695823d407 70 packetIndex++;
oprospero 16:89695823d407 71 break;
oprospero 16:89695823d407 72 }
oprospero 16:89695823d407 73 case 1:
oprospero 16:89695823d407 74 {
oprospero 17:acef0fb07510 75 if ( data < 13 )
oprospero 16:89695823d407 76 {
oprospero 17:acef0fb07510 77 pack_cmd = data;
oprospero 16:89695823d407 78 packetIndex++;
oprospero 16:89695823d407 79 }
oprospero 16:89695823d407 80 else
oprospero 16:89695823d407 81 packetIndex = 0;
oprospero 16:89695823d407 82 break;
oprospero 16:89695823d407 83 }
oprospero 16:89695823d407 84 case 2:
oprospero 16:89695823d407 85 {
oprospero 17:acef0fb07510 86 pack_value[1] = data;
oprospero 16:89695823d407 87 packetIndex++;
oprospero 16:89695823d407 88 break;
oprospero 16:89695823d407 89 }
oprospero 16:89695823d407 90 case 3:
oprospero 16:89695823d407 91 {
oprospero 17:acef0fb07510 92 if ( data < 128 )
oprospero 16:89695823d407 93 {
oprospero 17:acef0fb07510 94 pack_value[0] = data;
oprospero 16:89695823d407 95 packetIndex++;
oprospero 16:89695823d407 96 }
oprospero 16:89695823d407 97 else
oprospero 16:89695823d407 98 packetIndex = 0;
oprospero 16:89695823d407 99 break;
oprospero 16:89695823d407 100 }
oprospero 16:89695823d407 101 case 4:
oprospero 16:89695823d407 102 {
oprospero 17:acef0fb07510 103 pack_seq = data;
oprospero 16:89695823d407 104 packetIndex++;
oprospero 16:89695823d407 105 break;
oprospero 16:89695823d407 106 }
oprospero 16:89695823d407 107 case 5:
oprospero 16:89695823d407 108 {
oprospero 16:89695823d407 109 short temp = pack_value[0] + pack_cmd;
oprospero 17:acef0fb07510 110 pack_checksum = data;
oprospero 16:89695823d407 111 if ( temp == pack_checksum )
oprospero 16:89695823d407 112 {
oprospero 20:95244d4f47f6 113 // __disable_irq();
oprospero 16:89695823d407 114 short * ackPacket = new short[2];
oprospero 16:89695823d407 115 ackPacket[0] = pack_cmd;
oprospero 16:89695823d407 116 ackPacket[1] = pack_seq;
oprospero 17:acef0fb07510 117 txBuffer.add( ackPacket ); // Ack the packet with sequence nuber.
oprospero 16:89695823d407 118
oprospero 16:89695823d407 119 short * array = new short[2];
oprospero 16:89695823d407 120 array[0] = pack_cmd;
oprospero 16:89695823d407 121 array[1] = pack_value[1] * 128 + pack_value[0];
oprospero 17:acef0fb07510 122 cmdBuffer.add( array );
oprospero 20:95244d4f47f6 123 // __enable_irq();
oprospero 16:89695823d407 124 }
oprospero 16:89695823d407 125
oprospero 16:89695823d407 126 packetIndex = 0;
oprospero 16:89695823d407 127 break;
oprospero 16:89695823d407 128 }
oprospero 16:89695823d407 129 default:
oprospero 16:89695823d407 130 packetIndex = 0;
oprospero 16:89695823d407 131 break;
oprospero 16:89695823d407 132 }
oprospero 16:89695823d407 133
oprospero 20:95244d4f47f6 134 // __disable_irq();
oprospero 16:89695823d407 135
oprospero 16:89695823d407 136 }
oprospero 16:89695823d407 137 __enable_irq();
oprospero 17:acef0fb07510 138 return !cmdBuffer.isEmpty();
oprospero 17:acef0fb07510 139 }
oprospero 17:acef0fb07510 140
oprospero 17:acef0fb07510 141 short * com::read()
oprospero 17:acef0fb07510 142 {
oprospero 17:acef0fb07510 143 if ( !cmdBuffer.isEmpty() )
oprospero 17:acef0fb07510 144 return cmdBuffer.pop();
oprospero 17:acef0fb07510 145 else
oprospero 17:acef0fb07510 146 return NULL;
oprospero 16:89695823d407 147 }
oprospero 16:89695823d407 148
oprospero 16:89695823d407 149
oprospero 17:acef0fb07510 150 bool com::rdy2ack()
oprospero 17:acef0fb07510 151 {
oprospero 17:acef0fb07510 152 return !txBuffer.isEmpty();
oprospero 17:acef0fb07510 153 }
oprospero 17:acef0fb07510 154
oprospero 17:acef0fb07510 155 void com::sendACK()
oprospero 0:26a151d2c6db 156 {
oprospero 19:dc9d18037565 157 __disable_irq();
oprospero 21:117e01fc0373 158 if( !txBuffer.isEmpty() && xbee.writeable() )
oprospero 17:acef0fb07510 159 {
oprospero 17:acef0fb07510 160 short * pkt = txBuffer.pop();
oprospero 17:acef0fb07510 161 write(pkt[0],pkt[1]); //may need to disable interrupt
oprospero 17:acef0fb07510 162 delete[] pkt;
oprospero 17:acef0fb07510 163
oprospero 17:acef0fb07510 164 }
oprospero 19:dc9d18037565 165 __enable_irq();
oprospero 0:26a151d2c6db 166 }
oprospero 17:acef0fb07510 167
oprospero 17:acef0fb07510 168 void com::write( short command, short seq )
oprospero 17:acef0fb07510 169 {
oprospero 21:117e01fc0373 170 xbee.putc( 255 ); // End of message.
oprospero 21:117e01fc0373 171 xbee.putc( (char)command ); // Command
oprospero 21:117e01fc0373 172 xbee.putc( (char) seq); // Second 8 bits in array.
oprospero 21:117e01fc0373 173 xbee.putc( (char) command + seq ); // Checksum array[0] + array[1].
oprospero 17:acef0fb07510 174 }
oprospero 17:acef0fb07510 175
oprospero 19:dc9d18037565 176 void com::print(char* str)
oprospero 19:dc9d18037565 177 {
oprospero 19:dc9d18037565 178 __disable_irq();
oprospero 19:dc9d18037565 179 xbee.printf(str);
oprospero 19:dc9d18037565 180 __enable_irq();
oprospero 19:dc9d18037565 181 }