Simple transmitter application for SX127x radio.
Dependencies: MbedJSONValue SX127x sx12xx_hal
main.cpp
- Committer:
- corymast
- Date:
- 2019-09-06
- Revision:
- 3:5e25ec621190
- Parent:
- 2:efc0c1bbdffc
- Child:
- 4:b2fc780be0b3
File content as of revision 3:5e25ec621190:
#include "radio.h" #include "mbed.h" #include "platform/CircularBuffer.h" #include "MbedJSONValue/MbedJSONValue.h" // Semtech radio definitions for SX127x, SX126x and SX128x #if defined(SX127x_H) #define BW_KHZ 500 #define SPREADING_FACTOR 11 #define CF_HZ 912000000 #define TX_DBM 20 #elif defined(SX126x_H) #define BW_KHZ 500 #define SPREADING_FACTOR 10 #define CF_HZ 915000000 #define TX_DBM (Radio::chipType == CHIP_TYPE_SX1262 ? 20 : 14) #elif defined(SX128x_H) #define BW_KHZ 200 #define SPREADING_FACTOR 7 #define CF_HZ 2487000000 #define TX_DBM 6 #endif // The buffer size should match the tx_buf length in sx12xx.h #define BUF_SIZE 256 /******************** Setup radio transmitter ****************************/ volatile bool txDone; CircularBuffer<uint8_t, BUF_SIZE> msg; void txDoneCB() { txDone = true; } void rxDoneCB(uint8_t size, float rssi, float snr) { } // Define radio events for transmitter const RadioEvents_t rev = { /* Dio0_top_half */ NULL, /* TxDone_topHalf */ NULL, /* TxDone_botHalf */ txDoneCB, /* TxTimeout */ NULL, /* RxDone */ rxDoneCB, /* RxTimeout */ NULL, /* RxError */ NULL, /* FhssChangeChannel */NULL, /* CadDone */ NULL }; void print_radio_tx_buffer(uint8_t len) { printf("Tx Buffer = "); for(int i=0; i<len; i++) { printf("%d ", Radio::radio.tx_buf[i]); } printf("\r\n\n"); } void msg_append_bytes(uint8_t data[], uint8_t len) { for(int i=0; i < len; i++) { if(!msg.full()) { msg.push(data[i]); } else{ // If buffer is full, contents will be over-written // TODO: Handle this error case printf("buffer is full\n"); } } } void msg_append_string(string str, uint8_t len) { msg_append_bytes(reinterpret_cast<uint8_t*>(&str[0]), len); } void msg_send() { uint8_t len = 0; while(!msg.empty()) { // Pop data from the circular buffer to Radio tx_buf msg.pop(Radio::radio.tx_buf[len]); // Track the size of the tx_buf len++; } print_radio_tx_buffer(len); txDone = false; Radio::Send(len, 0, 0, 0); // begin transmission of payload while (!txDone) { Radio::service(); } printf("Tx Done.\r\n"); } void radio_init() { // Start radio transmitter after POR or reset Radio::Init(&rev); //Set radio properties for transmitter Radio::Standby(); Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1); Radio::SetChannel(CF_HZ); // Set transmitter output power Radio::set_tx_dbm(TX_DBM); // Setup transmit packet payload -> preambleLen, fixLen, crcOn, invIQ Radio::LoRaPacketConfig(8, false, true, false); } int main() { printf("\r\nreset-tx \n"); radio_init(); uint8_t seq = 0; // Set initial transmit sequence to 0 for (;;) { // Create an array to hold byte data uint8_t data[] = {seq}; // Append the byte array to the message msg_append_bytes(data, sizeof(data)/sizeof(data[0])); // Append a new value to the message data[0] = seq+1; msg_append_bytes(data, sizeof(data)/sizeof(data[0])); // Append a string to the message string myString = "Hello World!"; msg_append_string(myString, myString.length()); msg_send(); // Transmit payload every 500mS wait(0.5); // throttle sending rate seq+=2; // change payload (increment sequence number) } }