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:
Tue Oct 21 21:22:21 2014 +0000
Revision:
19:dc9d18037565
Parent:
18:19bcb2dbf3c8
Child:
20:95244d4f47f6
Added tx serial object

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