Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: AlohaTransceiver RingBuffer SX1276Lib SerialInterfaceProtocol mbed L3PDU
main.cpp@6:19457108c899, 2016-07-27 (annotated)
- Committer:
- rba90
- Date:
- Wed Jul 27 03:21:37 2016 +0000
- Revision:
- 6:19457108c899
- Parent:
- 5:445f35444a6a
- Child:
- 8:332099ece6e9
fix error
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| rba90 | 0:1e42d79b42e1 | 1 | #include "mbed.h" |
| rba90 | 0:1e42d79b42e1 | 2 | #include "AlohaTransceiver.h" |
| rba90 | 0:1e42d79b42e1 | 3 | #include "buffer.h" |
| rba90 | 0:1e42d79b42e1 | 4 | #include "SerialInterfaceProtocol.h" |
| rba90 | 0:1e42d79b42e1 | 5 | #include "AlohaFrame.h" |
| rba90 | 0:1e42d79b42e1 | 6 | |
| rba90 | 0:1e42d79b42e1 | 7 | Serial pc(USBTX, USBRX); |
| rba90 | 0:1e42d79b42e1 | 8 | |
| rba90 | 0:1e42d79b42e1 | 9 | // sip uses two buffer queues |
| rba90 | 0:1e42d79b42e1 | 10 | CircularBuffer<uint8_t> SerialInputBuffer; |
| rba90 | 0:1e42d79b42e1 | 11 | CircularBuffer<uint8_t> SerialOutputBuffer; |
| rba90 | 0:1e42d79b42e1 | 12 | SerialInterfaceProtocol SIP(&SerialInputBuffer, &SerialOutputBuffer); |
| rba90 | 0:1e42d79b42e1 | 13 | |
| rba90 | 0:1e42d79b42e1 | 14 | // aloha transceiver |
| rba90 | 0:1e42d79b42e1 | 15 | AlohaTransceiver aloha; |
| rba90 | 0:1e42d79b42e1 | 16 | AlohaFrame txFrame; |
| rba90 | 0:1e42d79b42e1 | 17 | |
| rba90 | 0:1e42d79b42e1 | 18 | void serialInterruptHandler() { |
| rba90 | 0:1e42d79b42e1 | 19 | // Note: you need to actually read from the serial to clear the RX interrupt |
| rba90 | 0:1e42d79b42e1 | 20 | int c = pc.getc(); |
| rba90 | 0:1e42d79b42e1 | 21 | |
| rba90 | 0:1e42d79b42e1 | 22 | // add to buffer |
| rba90 | 0:1e42d79b42e1 | 23 | if (SerialInputBuffer.isLocked()) |
| rba90 | 0:1e42d79b42e1 | 24 | { |
| rba90 | 0:1e42d79b42e1 | 25 | printf("Mutex Locked\r\n"); |
| rba90 | 0:1e42d79b42e1 | 26 | } |
| rba90 | 0:1e42d79b42e1 | 27 | else |
| rba90 | 0:1e42d79b42e1 | 28 | { |
| rba90 | 0:1e42d79b42e1 | 29 | SerialInputBuffer.enqueue((uint8_t) c); |
| rba90 | 0:1e42d79b42e1 | 30 | } |
| rba90 | 0:1e42d79b42e1 | 31 | } |
| rba90 | 0:1e42d79b42e1 | 32 | |
| rba90 | 0:1e42d79b42e1 | 33 | int toggleChecksum(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
| rba90 | 0:1e42d79b42e1 | 34 | { |
| rba90 | 0:1e42d79b42e1 | 35 | // one payload |
| rba90 | 0:1e42d79b42e1 | 36 | if (payload_length != 1) |
| rba90 | 0:1e42d79b42e1 | 37 | { |
| rba90 | 0:1e42d79b42e1 | 38 | sprintf((char *) response, "Wrong Payload Length\r\n"); |
| rba90 | 0:1e42d79b42e1 | 39 | *response_length = 22; |
| rba90 | 0:1e42d79b42e1 | 40 | return 1; |
| rba90 | 0:1e42d79b42e1 | 41 | } |
| rba90 | 0:1e42d79b42e1 | 42 | |
| rba90 | 0:1e42d79b42e1 | 43 | if ((bool) payload[0]) |
| rba90 | 0:1e42d79b42e1 | 44 | { |
| rba90 | 0:1e42d79b42e1 | 45 | SIP.enableChecksum(); |
| rba90 | 0:1e42d79b42e1 | 46 | } |
| rba90 | 0:1e42d79b42e1 | 47 | else |
| rba90 | 0:1e42d79b42e1 | 48 | { |
| rba90 | 0:1e42d79b42e1 | 49 | SIP.disableChecksum(); |
| rba90 | 0:1e42d79b42e1 | 50 | } |
| rba90 | 0:1e42d79b42e1 | 51 | |
| rba90 | 0:1e42d79b42e1 | 52 | return 0; |
| rba90 | 0:1e42d79b42e1 | 53 | } |
| rba90 | 0:1e42d79b42e1 | 54 | |
| rba90 | 6:19457108c899 | 55 | int sendMessage(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
| rba90 | 6:19457108c899 | 56 | { |
| rba90 | 6:19457108c899 | 57 | static uint8_t seqid = 0; |
| rba90 | 6:19457108c899 | 58 | |
| rba90 | 6:19457108c899 | 59 | // prepare for the frame |
| rba90 | 6:19457108c899 | 60 | txFrame.setType(AlohaFrame::Aloha_Data); |
| rba90 | 6:19457108c899 | 61 | txFrame.setPayloadLength(0x0); |
| rba90 | 6:19457108c899 | 62 | txFrame.setSourceAddress(0x1); |
| rba90 | 6:19457108c899 | 63 | txFrame.setDestinationAddress(0x2); |
| rba90 | 6:19457108c899 | 64 | txFrame.setFullMessageFlag(0x1); |
| rba90 | 6:19457108c899 | 65 | txFrame.setSequenceID(seqid); |
| rba90 | 6:19457108c899 | 66 | txFrame.generateCrc(); |
| rba90 | 6:19457108c899 | 67 | |
| rba90 | 6:19457108c899 | 68 | |
| rba90 | 6:19457108c899 | 69 | uint8_t buffer[20]; |
| rba90 | 6:19457108c899 | 70 | memset(buffer, 0x0, sizeof(buffer)); |
| rba90 | 6:19457108c899 | 71 | txFrame.serialize(buffer); |
| rba90 | 6:19457108c899 | 72 | |
| rba90 | 6:19457108c899 | 73 | aloha.send(buffer, 20); |
| rba90 | 6:19457108c899 | 74 | |
| rba90 | 6:19457108c899 | 75 | seqid += 1; |
| rba90 | 6:19457108c899 | 76 | |
| rba90 | 6:19457108c899 | 77 | return 0; |
| rba90 | 6:19457108c899 | 78 | } |
| rba90 | 6:19457108c899 | 79 | |
| rba90 | 2:ae85337d4ca7 | 80 | void AlohaDataEcho(AlohaFrame *frame) |
| rba90 | 3:a4f6abad9378 | 81 | { |
| rba90 | 2:ae85337d4ca7 | 82 | // get rssi and snr |
| rba90 | 2:ae85337d4ca7 | 83 | uint16_t rssi = aloha.getRssi(); |
| rba90 | 2:ae85337d4ca7 | 84 | uint8_t snr = aloha.getSnr(); |
| rba90 | 2:ae85337d4ca7 | 85 | |
| rba90 | 2:ae85337d4ca7 | 86 | uint8_t rssi_h = (rssi & 0xff00) >> 8; |
| rba90 | 2:ae85337d4ca7 | 87 | uint8_t rssi_l = rssi & 0xff; |
| rba90 | 2:ae85337d4ca7 | 88 | |
| rba90 | 2:ae85337d4ca7 | 89 | // Transmit RSSI and SNR back to sender |
| rba90 | 2:ae85337d4ca7 | 90 | txFrame.setType(AlohaFrame::Aloha_Data); |
| rba90 | 2:ae85337d4ca7 | 91 | txFrame.setPayloadLength(3); |
| rba90 | 2:ae85337d4ca7 | 92 | txFrame.setSourceAddress(frame->getDestinationAddress()); |
| rba90 | 2:ae85337d4ca7 | 93 | txFrame.setDestinationAddress(frame->getSourceAddress()); |
| rba90 | 2:ae85337d4ca7 | 94 | txFrame.setFullMessageFlag(frame->getFullMessageFlag()); |
| rba90 | 2:ae85337d4ca7 | 95 | txFrame.setSequenceID(frame->getSequenceID() + 1); |
| rba90 | 2:ae85337d4ca7 | 96 | txFrame.setPayload(snr, 0); |
| rba90 | 2:ae85337d4ca7 | 97 | txFrame.setPayload(rssi_h, 1); |
| rba90 | 2:ae85337d4ca7 | 98 | txFrame.setPayload(rssi_l, 2); |
| rba90 | 2:ae85337d4ca7 | 99 | txFrame.generateCrc(); |
| rba90 | 2:ae85337d4ca7 | 100 | |
| rba90 | 2:ae85337d4ca7 | 101 | |
| rba90 | 2:ae85337d4ca7 | 102 | uint8_t buffer[20]; |
| rba90 | 2:ae85337d4ca7 | 103 | memset(buffer, 0x0, sizeof(buffer)); |
| rba90 | 2:ae85337d4ca7 | 104 | txFrame.serialize(buffer); |
| rba90 | 2:ae85337d4ca7 | 105 | |
| rba90 | 2:ae85337d4ca7 | 106 | aloha.send(buffer, 20); |
| rba90 | 2:ae85337d4ca7 | 107 | |
| rba90 | 3:a4f6abad9378 | 108 | // print received message |
| rba90 | 3:a4f6abad9378 | 109 | printf("-----------------------------------------\r\n"); |
| rba90 | 3:a4f6abad9378 | 110 | printf(">Received Frame\r\n"); |
| rba90 | 3:a4f6abad9378 | 111 | printf("> Type: 0x%x, PayloadLength: 0x%x\r\n", frame->getType(), frame->getPayloadLength()); |
| rba90 | 3:a4f6abad9378 | 112 | printf("> SrcAddr: 0x%x, DestAddr: 0x%x\r\n", frame->getSourceAddress(), frame->getDestinationAddress()); |
| rba90 | 3:a4f6abad9378 | 113 | printf("> FMF: 0x%x, SequenceID: 0x%x\r\n", frame->getFullMessageFlag(), frame->getSequenceID()); |
| rba90 | 3:a4f6abad9378 | 114 | for (uint8_t i = 0; i < frame->getPayloadLength(); i++) |
| rba90 | 3:a4f6abad9378 | 115 | { |
| rba90 | 3:a4f6abad9378 | 116 | printf("> Payload[%d]: 0x%x\r\n", i, frame->getPayload(i)); |
| rba90 | 3:a4f6abad9378 | 117 | } |
| rba90 | 3:a4f6abad9378 | 118 | printf("> CRC: 0x%x\r\n", frame->getCrc()); |
| rba90 | 3:a4f6abad9378 | 119 | printf("-----------------------------------------\r\n"); |
| rba90 | 0:1e42d79b42e1 | 120 | } |
| rba90 | 0:1e42d79b42e1 | 121 | |
| rba90 | 0:1e42d79b42e1 | 122 | int main() { |
| rba90 | 0:1e42d79b42e1 | 123 | // initialize radio module |
| rba90 | 5:445f35444a6a | 124 | aloha.boardInit(); |
| rba90 | 6:19457108c899 | 125 | aloha.updateSettings(); |
| rba90 | 6:19457108c899 | 126 | aloha.enable(); |
| rba90 | 0:1e42d79b42e1 | 127 | |
| rba90 | 0:1e42d79b42e1 | 128 | // attach serial interrupt handler |
| rba90 | 0:1e42d79b42e1 | 129 | pc.attach(&serialInterruptHandler); |
| rba90 | 0:1e42d79b42e1 | 130 | |
| rba90 | 0:1e42d79b42e1 | 131 | // register callback functions for SIP |
| rba90 | 0:1e42d79b42e1 | 132 | SIP.registerCommand(0x00, toggleChecksum); |
| rba90 | 6:19457108c899 | 133 | SIP.registerCommand(0x01, sendMessage); |
| rba90 | 0:1e42d79b42e1 | 134 | |
| rba90 | 0:1e42d79b42e1 | 135 | // register callback functions for aloha transceiver |
| rba90 | 2:ae85337d4ca7 | 136 | aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataEcho); |
| rba90 | 0:1e42d79b42e1 | 137 | |
| rba90 | 0:1e42d79b42e1 | 138 | while(1) { |
| rba90 | 0:1e42d79b42e1 | 139 | SIP.poll(); |
| rba90 | 0:1e42d79b42e1 | 140 | aloha.poll(); |
| rba90 | 0:1e42d79b42e1 | 141 | |
| rba90 | 0:1e42d79b42e1 | 142 | while (SerialOutputBuffer.getCounter() > 0) |
| rba90 | 0:1e42d79b42e1 | 143 | { |
| rba90 | 0:1e42d79b42e1 | 144 | uint8_t ch; |
| rba90 | 0:1e42d79b42e1 | 145 | ch = SerialOutputBuffer.dequeue(); |
| rba90 | 0:1e42d79b42e1 | 146 | pc.putc(ch); |
| rba90 | 0:1e42d79b42e1 | 147 | } |
| rba90 | 0:1e42d79b42e1 | 148 | } |
| rba90 | 0:1e42d79b42e1 | 149 | } |