Wireless interface using LoRa technology

Dependencies:   AlohaTransceiver RingBuffer SX1276Lib_inAir SerialInterfaceProtocol mbed L3PDU

Committer:
rba90
Date:
Wed Jul 27 04:42:36 2016 +0000
Revision:
9:86d95e095bb0
Parent:
8:e30610bf6f79
Child:
10:7dcb951ecabd
Add device id to AlohaTransceiver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rba90 0:7d8e24cb62c1 1 #include "mbed.h"
rba90 0:7d8e24cb62c1 2 #include "AlohaTransceiver.h"
rba90 0:7d8e24cb62c1 3 #include "buffer.h"
rba90 0:7d8e24cb62c1 4 #include "SerialInterfaceProtocol.h"
rba90 0:7d8e24cb62c1 5 #include "AlohaFrame.h"
rba90 0:7d8e24cb62c1 6
rba90 0:7d8e24cb62c1 7 Serial pc(USBTX, USBRX);
rba90 0:7d8e24cb62c1 8
rba90 0:7d8e24cb62c1 9 // sip uses two buffer queues
rba90 0:7d8e24cb62c1 10 CircularBuffer<uint8_t> SerialInputBuffer;
rba90 0:7d8e24cb62c1 11 CircularBuffer<uint8_t> SerialOutputBuffer;
rba90 0:7d8e24cb62c1 12 SerialInterfaceProtocol SIP(&SerialInputBuffer, &SerialOutputBuffer);
rba90 0:7d8e24cb62c1 13
rba90 0:7d8e24cb62c1 14 // aloha transceiver
rba90 9:86d95e095bb0 15 AlohaTransceiver aloha(DEVICE_ID);
rba90 0:7d8e24cb62c1 16 AlohaFrame txFrame;
rba90 1:101cf4fca4e5 17 Timer timer;
rba90 2:5a74ae8be594 18 InterruptIn button(USER_BUTTON);
rba90 0:7d8e24cb62c1 19
rba90 0:7d8e24cb62c1 20 void serialInterruptHandler() {
rba90 0:7d8e24cb62c1 21 // Note: you need to actually read from the serial to clear the RX interrupt
rba90 0:7d8e24cb62c1 22 int c = pc.getc();
rba90 0:7d8e24cb62c1 23
rba90 0:7d8e24cb62c1 24 // add to buffer
rba90 0:7d8e24cb62c1 25 if (SerialInputBuffer.isLocked())
rba90 0:7d8e24cb62c1 26 {
rba90 0:7d8e24cb62c1 27 printf("Mutex Locked\r\n");
rba90 0:7d8e24cb62c1 28 }
rba90 0:7d8e24cb62c1 29 else
rba90 0:7d8e24cb62c1 30 {
rba90 0:7d8e24cb62c1 31 SerialInputBuffer.enqueue((uint8_t) c);
rba90 0:7d8e24cb62c1 32 }
rba90 0:7d8e24cb62c1 33 }
rba90 0:7d8e24cb62c1 34
rba90 0:7d8e24cb62c1 35 int toggleChecksum(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
rba90 0:7d8e24cb62c1 36 {
rba90 0:7d8e24cb62c1 37 // one payload
rba90 0:7d8e24cb62c1 38 if (payload_length != 1)
rba90 0:7d8e24cb62c1 39 {
rba90 0:7d8e24cb62c1 40 sprintf((char *) response, "Wrong Payload Length\r\n");
rba90 0:7d8e24cb62c1 41 *response_length = 22;
rba90 0:7d8e24cb62c1 42 return 1;
rba90 0:7d8e24cb62c1 43 }
rba90 0:7d8e24cb62c1 44
rba90 0:7d8e24cb62c1 45 if ((bool) payload[0])
rba90 0:7d8e24cb62c1 46 {
rba90 0:7d8e24cb62c1 47 SIP.enableChecksum();
rba90 0:7d8e24cb62c1 48 }
rba90 0:7d8e24cb62c1 49 else
rba90 0:7d8e24cb62c1 50 {
rba90 0:7d8e24cb62c1 51 SIP.disableChecksum();
rba90 0:7d8e24cb62c1 52 }
rba90 0:7d8e24cb62c1 53
rba90 0:7d8e24cb62c1 54 return 0;
rba90 0:7d8e24cb62c1 55 }
rba90 0:7d8e24cb62c1 56
rba90 1:101cf4fca4e5 57 int sendMessage(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
rba90 1:101cf4fca4e5 58 {
rba90 1:101cf4fca4e5 59 static uint8_t seqid = 0;
rba90 1:101cf4fca4e5 60
rba90 1:101cf4fca4e5 61 // prepare for the frame
rba90 1:101cf4fca4e5 62 txFrame.setType(AlohaFrame::Aloha_Data);
rba90 1:101cf4fca4e5 63 txFrame.setPayloadLength(0x0);
rba90 9:86d95e095bb0 64 txFrame.setSourceAddress(aloha.getDeviceId());
rba90 9:86d95e095bb0 65 txFrame.setDestinationAddress(0x0);
rba90 1:101cf4fca4e5 66 txFrame.setFullMessageFlag(0x1);
rba90 1:101cf4fca4e5 67 txFrame.setSequenceID(seqid);
rba90 1:101cf4fca4e5 68 txFrame.generateCrc();
rba90 1:101cf4fca4e5 69
rba90 1:101cf4fca4e5 70
rba90 1:101cf4fca4e5 71 uint8_t buffer[20];
rba90 1:101cf4fca4e5 72 memset(buffer, 0x0, sizeof(buffer));
rba90 1:101cf4fca4e5 73 txFrame.serialize(buffer);
rba90 1:101cf4fca4e5 74
rba90 3:7bb50ee42cba 75 aloha.send(buffer, 20);
rba90 1:101cf4fca4e5 76
rba90 3:7bb50ee42cba 77 seqid += 1;
rba90 1:101cf4fca4e5 78
rba90 1:101cf4fca4e5 79 // start the timer to measure round trip time
rba90 1:101cf4fca4e5 80 timer.reset();
rba90 1:101cf4fca4e5 81 timer.start();
rba90 1:101cf4fca4e5 82
rba90 3:7bb50ee42cba 83 return 0;
rba90 3:7bb50ee42cba 84 }
rba90 3:7bb50ee42cba 85
rba90 4:9151697dfa70 86 /*
rba90 4:9151697dfa70 87 * Format:
rba90 4:9151697dfa70 88 * < :start flag
rba90 4:9151697dfa70 89 * 02 :command
rba90 4:9151697dfa70 90 * xx :length
rba90 4:9151697dfa70 91 * xx: :00: get, 01: set
rba90 4:9151697dfa70 92 * xx :index for parameters
rba90 4:9151697dfa70 93 * ...
rba90 4:9151697dfa70 94 * ff :checksum
rba90 4:9151697dfa70 95 * > :end flag
rba90 4:9151697dfa70 96 */
rba90 3:7bb50ee42cba 97 int configureRadio(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
rba90 3:7bb50ee42cba 98 {
rba90 3:7bb50ee42cba 99 // read settings from radio
rba90 3:7bb50ee42cba 100 #if USE_MODEM_LORA == 1
rba90 3:7bb50ee42cba 101 AlohaTransceiver::LoRaSettings_t *settings = aloha.getSettings();
rba90 3:7bb50ee42cba 102 #elif USE_MODEM_FSK == 1
rba90 3:7bb50ee42cba 103 AlohaTransceiver::FskSettings_t *settings = aloha.getSettings();
rba90 3:7bb50ee42cba 104 #else
rba90 3:7bb50ee42cba 105 #error "Please define a modem in the compiler options."
rba90 3:7bb50ee42cba 106 #endif
rba90 3:7bb50ee42cba 107
rba90 3:7bb50ee42cba 108 if (payload_length < 2)
rba90 3:7bb50ee42cba 109 {
rba90 3:7bb50ee42cba 110 sprintf((char *) response, "Wrong Payload Length\r\n");
rba90 3:7bb50ee42cba 111 *response_length = 22;
rba90 3:7bb50ee42cba 112 return 1;
rba90 3:7bb50ee42cba 113 }
rba90 3:7bb50ee42cba 114
rba90 3:7bb50ee42cba 115 // true is set, false is get
rba90 3:7bb50ee42cba 116 bool isSet = (bool) payload[0];
rba90 3:7bb50ee42cba 117 uint8_t idx = payload[1];
rba90 3:7bb50ee42cba 118
rba90 3:7bb50ee42cba 119 switch(idx)
rba90 3:7bb50ee42cba 120 {
rba90 3:7bb50ee42cba 121 case 0x00: // Power
rba90 3:7bb50ee42cba 122 {
rba90 3:7bb50ee42cba 123 if (isSet)
rba90 3:7bb50ee42cba 124 {
rba90 3:7bb50ee42cba 125 int8_t Power = (int8_t) payload[2];
rba90 3:7bb50ee42cba 126 settings->Power = Power;
rba90 3:7bb50ee42cba 127
rba90 3:7bb50ee42cba 128 return 0;
rba90 3:7bb50ee42cba 129 }
rba90 3:7bb50ee42cba 130 else
rba90 3:7bb50ee42cba 131 {
rba90 3:7bb50ee42cba 132 response[0] = (uint8_t) settings->Power;
rba90 3:7bb50ee42cba 133 *response_length = 1;
rba90 1:101cf4fca4e5 134
rba90 3:7bb50ee42cba 135 return 0;
rba90 3:7bb50ee42cba 136 }
rba90 3:7bb50ee42cba 137 }
rba90 3:7bb50ee42cba 138
rba90 3:7bb50ee42cba 139 case 0x01: // Bandwidth
rba90 3:7bb50ee42cba 140 {
rba90 3:7bb50ee42cba 141 if (isSet)
rba90 3:7bb50ee42cba 142 {
Razorfoot 8:e30610bf6f79 143 uint32_t Bandwidth = (payload[5]) |
Razorfoot 8:e30610bf6f79 144 (payload[4] << 8) |
Razorfoot 8:e30610bf6f79 145 (payload[3] << 16) |
Razorfoot 8:e30610bf6f79 146 (payload[2] << 24);
rba90 3:7bb50ee42cba 147 settings->Bandwidth = Bandwidth;
rba90 3:7bb50ee42cba 148
rba90 3:7bb50ee42cba 149 return 0;
rba90 3:7bb50ee42cba 150 }
rba90 3:7bb50ee42cba 151 else
rba90 3:7bb50ee42cba 152 {
rba90 3:7bb50ee42cba 153 response[3] = (uint8_t) (settings->Bandwidth);
rba90 3:7bb50ee42cba 154 response[2] = (uint8_t) (settings->Bandwidth >> 8);
rba90 3:7bb50ee42cba 155 response[1] = (uint8_t) (settings->Bandwidth >> 16);
rba90 3:7bb50ee42cba 156 response[0] = (uint8_t) (settings->Bandwidth >> 24);
rba90 3:7bb50ee42cba 157 *response_length = 4;
rba90 3:7bb50ee42cba 158
rba90 3:7bb50ee42cba 159 return 0;
rba90 3:7bb50ee42cba 160 }
rba90 3:7bb50ee42cba 161
rba90 3:7bb50ee42cba 162 }
rba90 3:7bb50ee42cba 163
Razorfoot 8:e30610bf6f79 164 case 0x02: // Datarate, AKA Spreading Factor
rba90 3:7bb50ee42cba 165 {
rba90 3:7bb50ee42cba 166 if (isSet)
rba90 3:7bb50ee42cba 167 {
Razorfoot 8:e30610bf6f79 168 uint32_t Datarate = (payload[5]) |
Razorfoot 8:e30610bf6f79 169 (payload[4] << 8) |
Razorfoot 8:e30610bf6f79 170 (payload[3] << 16) |
Razorfoot 8:e30610bf6f79 171 (payload[2] << 24);
rba90 3:7bb50ee42cba 172 settings->Datarate = Datarate;
rba90 3:7bb50ee42cba 173
rba90 3:7bb50ee42cba 174 return 0;
rba90 3:7bb50ee42cba 175 }
rba90 3:7bb50ee42cba 176 else
rba90 3:7bb50ee42cba 177 {
rba90 3:7bb50ee42cba 178 response[3] = (uint8_t) (settings->Datarate);
rba90 3:7bb50ee42cba 179 response[2] = (uint8_t) (settings->Datarate >> 8);
rba90 3:7bb50ee42cba 180 response[1] = (uint8_t) (settings->Datarate >> 16);
rba90 3:7bb50ee42cba 181 response[0] = (uint8_t) (settings->Datarate >> 24);
rba90 3:7bb50ee42cba 182 *response_length = 4;
rba90 3:7bb50ee42cba 183
rba90 3:7bb50ee42cba 184 return 0;
rba90 3:7bb50ee42cba 185 }
rba90 3:7bb50ee42cba 186 }
rba90 3:7bb50ee42cba 187
rba90 3:7bb50ee42cba 188 case 0x03: // Coderate
rba90 3:7bb50ee42cba 189 {
rba90 3:7bb50ee42cba 190 if (isSet)
rba90 3:7bb50ee42cba 191 {
Razorfoot 8:e30610bf6f79 192 uint8_t Coderate = payload[2];
rba90 3:7bb50ee42cba 193 settings->Coderate = Coderate;
rba90 3:7bb50ee42cba 194
rba90 3:7bb50ee42cba 195 return 0;
rba90 3:7bb50ee42cba 196 }
rba90 3:7bb50ee42cba 197 else
rba90 3:7bb50ee42cba 198 {
rba90 3:7bb50ee42cba 199 response[0] = (uint8_t) settings->Coderate;
rba90 3:7bb50ee42cba 200 *response_length = 1;
rba90 3:7bb50ee42cba 201
rba90 3:7bb50ee42cba 202 return 0;
rba90 3:7bb50ee42cba 203 }
rba90 3:7bb50ee42cba 204 }
rba90 3:7bb50ee42cba 205
Razorfoot 8:e30610bf6f79 206 case 0x04: //Preamble Length
Razorfoot 8:e30610bf6f79 207 {
Razorfoot 8:e30610bf6f79 208 if (isSet)
Razorfoot 8:e30610bf6f79 209 {
Razorfoot 8:e30610bf6f79 210 uint16_t PreambleLen = payload[3] | (payload[2] << 8);
Razorfoot 8:e30610bf6f79 211 settings->PreambleLen = PreambleLen;
Razorfoot 8:e30610bf6f79 212 return 0;
Razorfoot 8:e30610bf6f79 213 }
Razorfoot 8:e30610bf6f79 214 else
Razorfoot 8:e30610bf6f79 215 {
Razorfoot 8:e30610bf6f79 216 response[1] = (uint8_t) (settings->PreambleLen);
Razorfoot 8:e30610bf6f79 217 response[0] = (uint8_t)(settings->PreambleLen >> 8);
Razorfoot 8:e30610bf6f79 218 *response_length = 2;
Razorfoot 8:e30610bf6f79 219
Razorfoot 8:e30610bf6f79 220 return 0;
Razorfoot 8:e30610bf6f79 221 }
Razorfoot 8:e30610bf6f79 222 }
Razorfoot 8:e30610bf6f79 223
Razorfoot 8:e30610bf6f79 224 case 0x05: //Symbol Timeout
Razorfoot 8:e30610bf6f79 225 {
Razorfoot 8:e30610bf6f79 226 if (isSet)
Razorfoot 8:e30610bf6f79 227 {
Razorfoot 8:e30610bf6f79 228 uint16_t SymbolTimeout = payload[3] | (payload[2] << 8);
Razorfoot 8:e30610bf6f79 229 settings->SymbolTimeout = SymbolTimeout;
Razorfoot 8:e30610bf6f79 230 return 0;
Razorfoot 8:e30610bf6f79 231 }
Razorfoot 8:e30610bf6f79 232 else
Razorfoot 8:e30610bf6f79 233 {
Razorfoot 8:e30610bf6f79 234 response[1] = (uint8_t) (settings->SymbolTimeout);
Razorfoot 8:e30610bf6f79 235 response[0] = (uint8_t) (settings->SymbolTimeout >> 8);
Razorfoot 8:e30610bf6f79 236 *response_length = 2;
Razorfoot 8:e30610bf6f79 237
Razorfoot 8:e30610bf6f79 238 return 0;
Razorfoot 8:e30610bf6f79 239 }
Razorfoot 8:e30610bf6f79 240 }
Razorfoot 8:e30610bf6f79 241
Razorfoot 8:e30610bf6f79 242 case 0x06: //FixLen
Razorfoot 8:e30610bf6f79 243 {
Razorfoot 8:e30610bf6f79 244 if (isSet)
Razorfoot 8:e30610bf6f79 245 {
Razorfoot 8:e30610bf6f79 246 bool FixLen = payload[2];
Razorfoot 8:e30610bf6f79 247 settings->FixLen = FixLen;
Razorfoot 8:e30610bf6f79 248 return 0;
Razorfoot 8:e30610bf6f79 249 }
Razorfoot 8:e30610bf6f79 250 else
Razorfoot 8:e30610bf6f79 251 {
Razorfoot 8:e30610bf6f79 252 response[0] = (bool) (settings->SymbolTimeout);
Razorfoot 8:e30610bf6f79 253 *response_length = 1;
Razorfoot 8:e30610bf6f79 254
Razorfoot 8:e30610bf6f79 255 return 0;
Razorfoot 8:e30610bf6f79 256 }
Razorfoot 8:e30610bf6f79 257 }
Razorfoot 8:e30610bf6f79 258
Razorfoot 8:e30610bf6f79 259 case 0x07: //PayloadLen
Razorfoot 8:e30610bf6f79 260 {
Razorfoot 8:e30610bf6f79 261 if (isSet)
Razorfoot 8:e30610bf6f79 262 {
Razorfoot 8:e30610bf6f79 263 uint8_t PayloadLen = payload[2];
Razorfoot 8:e30610bf6f79 264 settings->PayloadLen = PayloadLen;
Razorfoot 8:e30610bf6f79 265 return 0;
Razorfoot 8:e30610bf6f79 266 }
Razorfoot 8:e30610bf6f79 267 else
Razorfoot 8:e30610bf6f79 268 {
Razorfoot 8:e30610bf6f79 269 response[0] = (uint8_t) (settings->PayloadLen);
Razorfoot 8:e30610bf6f79 270
Razorfoot 8:e30610bf6f79 271 return 0;
Razorfoot 8:e30610bf6f79 272 }
Razorfoot 8:e30610bf6f79 273 }
Razorfoot 8:e30610bf6f79 274
Razorfoot 8:e30610bf6f79 275 case 0x08: //CrcOn
Razorfoot 8:e30610bf6f79 276 {
Razorfoot 8:e30610bf6f79 277 if (isSet) {
Razorfoot 8:e30610bf6f79 278 bool CrcOn = payload[2];
Razorfoot 8:e30610bf6f79 279 settings->CrcOn = CrcOn;
Razorfoot 8:e30610bf6f79 280 return 0;
Razorfoot 8:e30610bf6f79 281 }
Razorfoot 8:e30610bf6f79 282 else
Razorfoot 8:e30610bf6f79 283 {
Razorfoot 8:e30610bf6f79 284 response[0] = (bool) (settings->CrcOn);
Razorfoot 8:e30610bf6f79 285
Razorfoot 8:e30610bf6f79 286 return 0;
Razorfoot 8:e30610bf6f79 287 }
Razorfoot 8:e30610bf6f79 288 }
Razorfoot 8:e30610bf6f79 289
Razorfoot 8:e30610bf6f79 290 case 0x09: //FreqHopOn
Razorfoot 8:e30610bf6f79 291 {
Razorfoot 8:e30610bf6f79 292 if (isSet) {
Razorfoot 8:e30610bf6f79 293 bool FreqHopOn = payload[2];
Razorfoot 8:e30610bf6f79 294 settings->FreqHopOn = FreqHopOn;
Razorfoot 8:e30610bf6f79 295 return 0;
Razorfoot 8:e30610bf6f79 296 }
Razorfoot 8:e30610bf6f79 297 else
Razorfoot 8:e30610bf6f79 298 {
Razorfoot 8:e30610bf6f79 299 response[0] = (bool) (settings->FreqHopOn);
Razorfoot 8:e30610bf6f79 300
Razorfoot 8:e30610bf6f79 301 return 0;
Razorfoot 8:e30610bf6f79 302 }
Razorfoot 8:e30610bf6f79 303 }
Razorfoot 8:e30610bf6f79 304
Razorfoot 8:e30610bf6f79 305 case 0x0A: //HopPeriod
Razorfoot 8:e30610bf6f79 306 {
Razorfoot 8:e30610bf6f79 307 if (isSet) {
Razorfoot 8:e30610bf6f79 308 uint8_t HopPeriod = payload[2];
Razorfoot 8:e30610bf6f79 309 settings->HopPeriod = HopPeriod;
Razorfoot 8:e30610bf6f79 310 return 0;
Razorfoot 8:e30610bf6f79 311 }
Razorfoot 8:e30610bf6f79 312 else
Razorfoot 8:e30610bf6f79 313 {
Razorfoot 8:e30610bf6f79 314 response[0] = (uint8_t) (settings->HopPeriod);
Razorfoot 8:e30610bf6f79 315 return 0;
Razorfoot 8:e30610bf6f79 316 }
Razorfoot 8:e30610bf6f79 317 }
Razorfoot 8:e30610bf6f79 318
Razorfoot 8:e30610bf6f79 319 case 0x0B: //IqInverted
Razorfoot 8:e30610bf6f79 320 {
Razorfoot 8:e30610bf6f79 321 if (isSet) {
Razorfoot 8:e30610bf6f79 322 bool IqInverted = payload[2];
Razorfoot 8:e30610bf6f79 323 settings->IqInverted = IqInverted;
Razorfoot 8:e30610bf6f79 324 return 0;
Razorfoot 8:e30610bf6f79 325 }
Razorfoot 8:e30610bf6f79 326 else
Razorfoot 8:e30610bf6f79 327 {
Razorfoot 8:e30610bf6f79 328 response[0] = (bool) (settings->IqInverted);
Razorfoot 8:e30610bf6f79 329 return 0;
Razorfoot 8:e30610bf6f79 330 }
Razorfoot 8:e30610bf6f79 331 }
Razorfoot 8:e30610bf6f79 332
Razorfoot 8:e30610bf6f79 333 case 0x0C: //RxContinuous
Razorfoot 8:e30610bf6f79 334 {
Razorfoot 8:e30610bf6f79 335 if(isSet)
Razorfoot 8:e30610bf6f79 336 {
Razorfoot 8:e30610bf6f79 337 bool RxContinuous = payload[2];
Razorfoot 8:e30610bf6f79 338 settings->RxContinuous = RxContinuous;
Razorfoot 8:e30610bf6f79 339 return 0;
Razorfoot 8:e30610bf6f79 340 }
Razorfoot 8:e30610bf6f79 341 else
Razorfoot 8:e30610bf6f79 342 {
Razorfoot 8:e30610bf6f79 343 response[0] = (bool) (settings->RxContinuous);
Razorfoot 8:e30610bf6f79 344 return 0;
Razorfoot 8:e30610bf6f79 345 }
Razorfoot 8:e30610bf6f79 346 }
Razorfoot 8:e30610bf6f79 347
Razorfoot 8:e30610bf6f79 348 case 0x0D: //TxTimeout
Razorfoot 8:e30610bf6f79 349 {
Razorfoot 8:e30610bf6f79 350 if (isSet)
Razorfoot 8:e30610bf6f79 351 {
Razorfoot 8:e30610bf6f79 352 uint32_t TxTimeout = (payload[5]) |
Razorfoot 8:e30610bf6f79 353 (payload[4] << 8) |
Razorfoot 8:e30610bf6f79 354 (payload[3] << 16) |
Razorfoot 8:e30610bf6f79 355 (payload[2] << 24);
Razorfoot 8:e30610bf6f79 356 settings->TxTimeout = TxTimeout;
Razorfoot 8:e30610bf6f79 357 return 0;
Razorfoot 8:e30610bf6f79 358 }
Razorfoot 8:e30610bf6f79 359 else
Razorfoot 8:e30610bf6f79 360 {
Razorfoot 8:e30610bf6f79 361 response[3] = (uint8_t) (settings->TxTimeout);
Razorfoot 8:e30610bf6f79 362 response[2] = (uint8_t) (settings->TxTimeout >> 8);
Razorfoot 8:e30610bf6f79 363 response[1] = (uint8_t) (settings->TxTimeout >> 16);
Razorfoot 8:e30610bf6f79 364 response[0] = (uint8_t) (settings->TxTimeout >> 24);
Razorfoot 8:e30610bf6f79 365 *response_length = 4;
Razorfoot 8:e30610bf6f79 366
Razorfoot 8:e30610bf6f79 367 return 0;
Razorfoot 8:e30610bf6f79 368 }
Razorfoot 8:e30610bf6f79 369 }
Razorfoot 8:e30610bf6f79 370
Razorfoot 8:e30610bf6f79 371
rba90 3:7bb50ee42cba 372 default:
rba90 3:7bb50ee42cba 373 {
rba90 3:7bb50ee42cba 374 break;
rba90 3:7bb50ee42cba 375 }
Razorfoot 8:e30610bf6f79 376
Razorfoot 8:e30610bf6f79 377 //case
rba90 3:7bb50ee42cba 378 }
rba90 3:7bb50ee42cba 379
rba90 3:7bb50ee42cba 380
rba90 3:7bb50ee42cba 381 return 0;
rba90 1:101cf4fca4e5 382 }
rba90 1:101cf4fca4e5 383
rba90 1:101cf4fca4e5 384 void AlohaDataHandler(AlohaFrame *frame)
rba90 1:101cf4fca4e5 385 {
rba90 1:101cf4fca4e5 386 // stop the timer to measure round trip time
rba90 1:101cf4fca4e5 387 timer.stop();
rba90 1:101cf4fca4e5 388
rba90 2:5a74ae8be594 389 printf("-----------------------------------------\r\n");
rba90 2:5a74ae8be594 390 printf(">Received Frame\r\n");
rba90 2:5a74ae8be594 391 printf("> Type: 0x%x, PayloadLength: 0x%x\r\n", frame->getType(), frame->getPayloadLength());
rba90 2:5a74ae8be594 392 printf("> SrcAddr: 0x%x, DestAddr: 0x%x\r\n", frame->getSourceAddress(), frame->getDestinationAddress());
rba90 2:5a74ae8be594 393 printf("> FMF: 0x%x, SequenceID: 0x%x\r\n", frame->getFullMessageFlag(), frame->getSequenceID());
rba90 0:7d8e24cb62c1 394 for (uint8_t i = 0; i < frame->getPayloadLength(); i++)
rba90 0:7d8e24cb62c1 395 {
rba90 2:5a74ae8be594 396 printf("> Payload[%d]: 0x%x\r\n", i, frame->getPayload(i));
rba90 0:7d8e24cb62c1 397 }
rba90 2:5a74ae8be594 398 printf("> CRC: 0x%x\r\n", frame->getCrc());
rba90 0:7d8e24cb62c1 399
rba90 1:101cf4fca4e5 400 // decode payload (range test only)
rba90 1:101cf4fca4e5 401 int8_t snr = (int8_t) frame->getPayload(0);
rba90 1:101cf4fca4e5 402
rba90 1:101cf4fca4e5 403 uint16_t rssi_h = frame->getPayload(1);
rba90 1:101cf4fca4e5 404 uint8_t rssi_l = frame->getPayload(2);
rba90 1:101cf4fca4e5 405 int16_t rssi = (int16_t) (rssi_h << 8 | rssi_l);
rba90 0:7d8e24cb62c1 406
rba90 0:7d8e24cb62c1 407
rba90 3:7bb50ee42cba 408 printf(">About this transmission:\r\n");
rba90 3:7bb50ee42cba 409 printf("> Base Station:: Rssi: %d, Snr: %d\r\n", rssi, snr);
rba90 3:7bb50ee42cba 410 printf("> Terminal:: Rssi: %d, Snr: %d\r\n", aloha.getRssi(), aloha.getSnr());
rba90 2:5a74ae8be594 411 printf("> Round trip time: %d ms\r\n", timer.read_ms());
rba90 2:5a74ae8be594 412 printf("-----------------------------------------\r\n");
rba90 0:7d8e24cb62c1 413 }
rba90 0:7d8e24cb62c1 414
rba90 2:5a74ae8be594 415 void automaticPacketTransmit()
rba90 2:5a74ae8be594 416 {
rba90 2:5a74ae8be594 417 SerialInputBuffer.enqueue((uint8_t) '<');
rba90 2:5a74ae8be594 418 SerialInputBuffer.enqueue((uint8_t) '0');
rba90 2:5a74ae8be594 419 SerialInputBuffer.enqueue((uint8_t) '1');
rba90 2:5a74ae8be594 420 SerialInputBuffer.enqueue((uint8_t) '0');
rba90 2:5a74ae8be594 421 SerialInputBuffer.enqueue((uint8_t) '0');
rba90 2:5a74ae8be594 422 SerialInputBuffer.enqueue((uint8_t) 'f');
rba90 2:5a74ae8be594 423 SerialInputBuffer.enqueue((uint8_t) 'f');
rba90 2:5a74ae8be594 424 SerialInputBuffer.enqueue((uint8_t) '>');
rba90 2:5a74ae8be594 425 }
rba90 2:5a74ae8be594 426
rba90 2:5a74ae8be594 427
rba90 0:7d8e24cb62c1 428 int main() {
rba90 0:7d8e24cb62c1 429 // initialize radio module
rba90 3:7bb50ee42cba 430 aloha.boardInit();
rba90 3:7bb50ee42cba 431 aloha.updateSettings();
rba90 3:7bb50ee42cba 432 aloha.enable();
rba90 0:7d8e24cb62c1 433
rba90 0:7d8e24cb62c1 434 // attach serial interrupt handler
rba90 0:7d8e24cb62c1 435 pc.attach(&serialInterruptHandler);
rba90 0:7d8e24cb62c1 436
rba90 0:7d8e24cb62c1 437 // register callback functions for SIP
rba90 0:7d8e24cb62c1 438 SIP.registerCommand(0x00, toggleChecksum);
rba90 1:101cf4fca4e5 439 SIP.registerCommand(0x01, sendMessage);
rba90 3:7bb50ee42cba 440 SIP.registerCommand(0x02, configureRadio);
rba90 0:7d8e24cb62c1 441
rba90 0:7d8e24cb62c1 442 // register callback functions for aloha transceiver
rba90 1:101cf4fca4e5 443 aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataHandler);
rba90 2:5a74ae8be594 444
rba90 2:5a74ae8be594 445 // configure button interrupt
rba90 2:5a74ae8be594 446 button.fall(automaticPacketTransmit);
rba90 0:7d8e24cb62c1 447
rba90 0:7d8e24cb62c1 448 while(1) {
rba90 0:7d8e24cb62c1 449 SIP.poll();
rba90 0:7d8e24cb62c1 450 aloha.poll();
rba90 0:7d8e24cb62c1 451
rba90 0:7d8e24cb62c1 452 while (SerialOutputBuffer.getCounter() > 0)
rba90 0:7d8e24cb62c1 453 {
rba90 0:7d8e24cb62c1 454 uint8_t ch;
rba90 0:7d8e24cb62c1 455 ch = SerialOutputBuffer.dequeue();
rba90 0:7d8e24cb62c1 456 pc.putc(ch);
rba90 0:7d8e24cb62c1 457 }
rba90 0:7d8e24cb62c1 458 }
rba90 0:7d8e24cb62c1 459 }