Basic RX/TX example, this is the firmware that LoDev S76S comes with initially.
Dependencies: mbed SX1276RonothLib
LoDev RX/TX Example
Transmit
By default LoDev comes on in receive mode. To switch to transmit mode, connect PC2 to 3.3v and press the reset switch. In TX mode the blue LED is normally off and blinks on briefly when transmitting.
Receive
When in RX mode the LED is normally on and turns off briefly when receiving a packet. This is the default mode.
main.cpp@3:80e4a46e676c, 2019-07-15 (annotated)
- Committer:
- steve918
- Date:
- Mon Jul 15 22:07:41 2019 +0000
- Revision:
- 3:80e4a46e676c
- Parent:
- 2:a93ffb29a2d2
update readme;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
steve918 | 0:5acc19265529 | 1 | #include "mbed.h" |
steve918 | 0:5acc19265529 | 2 | #include "config.h" |
steve918 | 0:5acc19265529 | 3 | #include "sx1276-mbed-hal.h" |
steve918 | 0:5acc19265529 | 4 | #include "main.h" |
steve918 | 0:5acc19265529 | 5 | |
steve918 | 0:5acc19265529 | 6 | // Override stdout to use our serial port |
steve918 | 0:5acc19265529 | 7 | FileHandle* mbed::mbed_override_console(int fd) { |
steve918 | 0:5acc19265529 | 8 | static UARTSerial pc(PA_9, PA_10, 115200); |
steve918 | 0:5acc19265529 | 9 | return &pc; |
steve918 | 0:5acc19265529 | 10 | } |
steve918 | 0:5acc19265529 | 11 | |
steve918 | 0:5acc19265529 | 12 | |
steve918 | 0:5acc19265529 | 13 | static RadioEvents_t RadioEvents; |
steve918 | 0:5acc19265529 | 14 | |
steve918 | 0:5acc19265529 | 15 | SX1276Generic *Radio; |
steve918 | 0:5acc19265529 | 16 | DigitalOut blueLed = DigitalOut(LED); |
steve918 | 0:5acc19265529 | 17 | |
steve918 | 0:5acc19265529 | 18 | // NOTE: By default the sample application is in recieve mode. |
steve918 | 0:5acc19265529 | 19 | // Connect PIN PC_2 to 3.3v to transmit |
steve918 | 2:a93ffb29a2d2 | 20 | DigitalIn txModeIndicator = DigitalIn(TX_IND, PullDown); |
steve918 | 0:5acc19265529 | 21 | |
steve918 | 0:5acc19265529 | 22 | int timeoutCount, errCount, recvCount = 0; |
steve918 | 0:5acc19265529 | 23 | |
steve918 | 0:5acc19265529 | 24 | int main() |
steve918 | 0:5acc19265529 | 25 | { |
steve918 | 0:5acc19265529 | 26 | |
steve918 | 0:5acc19265529 | 27 | blueLed = 1; |
steve918 | 0:5acc19265529 | 28 | |
steve918 | 0:5acc19265529 | 29 | Radio = new SX1276Generic(NULL, RFM95_SX1276, |
steve918 | 0:5acc19265529 | 30 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
steve918 | 0:5acc19265529 | 31 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5); |
steve918 | 0:5acc19265529 | 32 | |
steve918 | 0:5acc19265529 | 33 | printf("LoDev RX/TX Demo Application\n" ); |
steve918 | 0:5acc19265529 | 34 | printf("Freqency: %.1f\n", (double)RF_FREQUENCY/1000000.0); |
steve918 | 0:5acc19265529 | 35 | printf("TXPower: %d dBm\n", TX_OUTPUT_POWER); |
steve918 | 0:5acc19265529 | 36 | printf("Bandwidth: %d Hz\n", LORA_BANDWIDTH); |
steve918 | 0:5acc19265529 | 37 | printf("Spreading factor: SF%d\n", LORA_SPREADING_FACTOR); |
steve918 | 0:5acc19265529 | 38 | |
steve918 | 0:5acc19265529 | 39 | // Initialize Radio driver |
steve918 | 0:5acc19265529 | 40 | RadioEvents.TxDone = OnTxDone; |
steve918 | 0:5acc19265529 | 41 | RadioEvents.RxDone = OnRxDone; |
steve918 | 0:5acc19265529 | 42 | RadioEvents.RxError = OnRxError; |
steve918 | 0:5acc19265529 | 43 | RadioEvents.TxTimeout = OnTxTimeout; |
steve918 | 0:5acc19265529 | 44 | RadioEvents.RxTimeout = OnRxTimeout; |
steve918 | 0:5acc19265529 | 45 | if (Radio->Init( &RadioEvents ) == false) { |
steve918 | 0:5acc19265529 | 46 | while(1) { |
steve918 | 0:5acc19265529 | 47 | printf("Radio could not be detected!\n"); |
steve918 | 0:5acc19265529 | 48 | wait( 1 ); |
steve918 | 0:5acc19265529 | 49 | } |
steve918 | 0:5acc19265529 | 50 | } |
steve918 | 0:5acc19265529 | 51 | |
steve918 | 0:5acc19265529 | 52 | Radio->DetectBoardType(); |
steve918 | 0:5acc19265529 | 53 | Radio->SetChannel(RF_FREQUENCY ); |
steve918 | 0:5acc19265529 | 54 | |
steve918 | 0:5acc19265529 | 55 | Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
steve918 | 0:5acc19265529 | 56 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
steve918 | 0:5acc19265529 | 57 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
steve918 | 0:5acc19265529 | 58 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
steve918 | 0:5acc19265529 | 59 | LORA_IQ_INVERSION_ON, 2000 ); |
steve918 | 0:5acc19265529 | 60 | |
steve918 | 0:5acc19265529 | 61 | Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
steve918 | 0:5acc19265529 | 62 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
steve918 | 0:5acc19265529 | 63 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
steve918 | 0:5acc19265529 | 64 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
steve918 | 0:5acc19265529 | 65 | LORA_IQ_INVERSION_ON, true ); |
steve918 | 0:5acc19265529 | 66 | |
steve918 | 0:5acc19265529 | 67 | printf("Starting loop\n"); |
steve918 | 0:5acc19265529 | 68 | |
steve918 | 0:5acc19265529 | 69 | uint16_t frameCount = 0; |
steve918 | 0:5acc19265529 | 70 | char buffer[18]; |
steve918 | 0:5acc19265529 | 71 | |
steve918 | 0:5acc19265529 | 72 | bool txMode = false; |
steve918 | 0:5acc19265529 | 73 | if(txModeIndicator == 1) { |
steve918 | 0:5acc19265529 | 74 | // Transmit |
steve918 | 0:5acc19265529 | 75 | txMode = true; |
steve918 | 0:5acc19265529 | 76 | } else { |
steve918 | 0:5acc19265529 | 77 | Radio->Rx(0); |
steve918 | 0:5acc19265529 | 78 | } |
steve918 | 0:5acc19265529 | 79 | |
steve918 | 0:5acc19265529 | 80 | while( 1 ) |
steve918 | 0:5acc19265529 | 81 | { |
steve918 | 0:5acc19265529 | 82 | blueLed = 1; |
steve918 | 0:5acc19265529 | 83 | if(txMode) { |
steve918 | 0:5acc19265529 | 84 | wait_ms(5000); |
steve918 | 0:5acc19265529 | 85 | sprintf(buffer, "%u Hello World", frameCount); |
steve918 | 0:5acc19265529 | 86 | printf("Sending packet: %s\n", buffer); |
steve918 | 0:5acc19265529 | 87 | Radio->Send( buffer, strlen(buffer) ); |
steve918 | 0:5acc19265529 | 88 | frameCount++; |
steve918 | 0:5acc19265529 | 89 | } else { |
steve918 | 0:5acc19265529 | 90 | wait_ms(100); |
steve918 | 0:5acc19265529 | 91 | } |
steve918 | 0:5acc19265529 | 92 | |
steve918 | 0:5acc19265529 | 93 | } |
steve918 | 0:5acc19265529 | 94 | } |
steve918 | 0:5acc19265529 | 95 | |
steve918 | 0:5acc19265529 | 96 | void OnTxDone(void *radio, void *userThisPtr, void *userData) |
steve918 | 0:5acc19265529 | 97 | { |
steve918 | 0:5acc19265529 | 98 | Radio->Sleep( ); |
steve918 | 0:5acc19265529 | 99 | blueLed = 0; |
steve918 | 0:5acc19265529 | 100 | printf("> OnTxDone\n"); |
steve918 | 0:5acc19265529 | 101 | } |
steve918 | 0:5acc19265529 | 102 | |
steve918 | 0:5acc19265529 | 103 | void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
steve918 | 0:5acc19265529 | 104 | { |
steve918 | 0:5acc19265529 | 105 | Radio->Sleep( ); |
steve918 | 0:5acc19265529 | 106 | blueLed = 0; |
steve918 | 0:5acc19265529 | 107 | recvCount++; |
steve918 | 0:5acc19265529 | 108 | printf("Status: Recv=%d, Error=%d, Timeout=%d\n", recvCount, errCount, timeoutCount); |
steve918 | 0:5acc19265529 | 109 | printf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d\n", rssi, snr); |
steve918 | 0:5acc19265529 | 110 | printf("Data[%d]: %.*s\n", size, size, payload); |
steve918 | 0:5acc19265529 | 111 | Radio->Rx(0); |
steve918 | 0:5acc19265529 | 112 | } |
steve918 | 0:5acc19265529 | 113 | |
steve918 | 0:5acc19265529 | 114 | void OnTxTimeout(void *radio, void *userThisPtr, void *userData) |
steve918 | 0:5acc19265529 | 115 | { |
steve918 | 0:5acc19265529 | 116 | Radio->Sleep( ); |
steve918 | 0:5acc19265529 | 117 | blueLed = 0; |
steve918 | 0:5acc19265529 | 118 | printf("> OnTxTimeout\n"); |
steve918 | 0:5acc19265529 | 119 | } |
steve918 | 0:5acc19265529 | 120 | |
steve918 | 0:5acc19265529 | 121 | void OnRxTimeout(void *radio, void *userThisPtr, void *userData) |
steve918 | 0:5acc19265529 | 122 | { |
steve918 | 0:5acc19265529 | 123 | Radio->Sleep( ); |
steve918 | 0:5acc19265529 | 124 | blueLed = 0; |
steve918 | 0:5acc19265529 | 125 | printf("> OnRxTimeout\n"); |
steve918 | 0:5acc19265529 | 126 | timeoutCount++; |
steve918 | 0:5acc19265529 | 127 | blueLed = 0; |
steve918 | 0:5acc19265529 | 128 | Radio->Rx(0); |
steve918 | 0:5acc19265529 | 129 | } |
steve918 | 0:5acc19265529 | 130 | |
steve918 | 0:5acc19265529 | 131 | void OnRxError(void *radio, void *userThisPtr, void *userData) |
steve918 | 0:5acc19265529 | 132 | { |
steve918 | 0:5acc19265529 | 133 | Radio->Sleep( ); |
steve918 | 0:5acc19265529 | 134 | printf("> OnRxError\n"); |
steve918 | 0:5acc19265529 | 135 | errCount++; |
steve918 | 0:5acc19265529 | 136 | blueLed = 0; |
steve918 | 0:5acc19265529 | 137 | Radio->Rx(0); |
steve918 | 0:5acc19265529 | 138 | } |