From 4bc7668ae98ff8041a7fa7b49325915f4f4cf865 Mon Sep 17 00:00:00 2001 From: "Pawel Spychalski (DzikuVx)" Date: Sat, 30 Sep 2017 18:43:03 +0200 Subject: [PATCH] Support for SPI connected SX1278 --- .vscode/arduino.json | 3 +- README.md | 2 +- build/.gitkeep | 0 crossbow.ino | 82 +++++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 77 insertions(+), 10 deletions(-) delete mode 100644 build/.gitkeep diff --git a/.vscode/arduino.json b/.vscode/arduino.json index 265a87f..56ce281 100644 --- a/.vscode/arduino.json +++ b/.vscode/arduino.json @@ -1,6 +1,5 @@ { - "board": "arduino:avr:pro", - "configuration": "cpu=16MHzatmega328", + "board": "bsfrance:avr:lora32u4", "sketch": "crossbow.ino", "port": "COM23", "output": "./build" diff --git a/README.md b/README.md index 1bb774b..ca64cc7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # QuadMeUp Crossbow -_QuadMeUp Crossbow_ is a DIY project that gives 5km (at least) of RC link for UAV (airplanes and drones) for a price below $40. I uses SX1276 (LoRa 868MHz/915MHz) compatible (like HopeRF RFM95W) radio modules connected to Arduino compatible boards. It can be regular Arduino connected via SPI to SX1276 or dedicated board like _Adafruit Feather 32u4 RFM LoRa_ or _LoRa32u4 II_ +_QuadMeUp Crossbow_ is a DIY project that gives 5km (at least) of RC link for UAV (airplanes and drones) for a price below $40. I uses SX1278 (LoRa 868MHz/915MHz) compatible (like HopeRF RFM95W) radio modules connected to Arduino compatible boards. It can be regular Arduino connected via SPI to SX1278 or dedicated board like _Adafruit Feather 32u4 RFM LoRa_ or _LoRa32u4 II_ # Current state diff --git a/build/.gitkeep b/build/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/crossbow.ino b/crossbow.ino index f5e7b44..e0c604b 100644 --- a/crossbow.ino +++ b/crossbow.ino @@ -1,7 +1,11 @@ +#include #include "variables.h" -// #define DEVICE_MODE_TX -#define DEVICE_MODE_RX +// #define LORA_HARDWARE_SERIAL +#define LORA_HARDWARE_SPI + +#define DEVICE_MODE_TX +// #define DEVICE_MODE_RX /* * Main defines for device working in TX mode @@ -199,14 +203,55 @@ uint8_t get10bitLowShift(uint8_t channel) { return 8 - get10bitHighShift(channel); } +void computeCrc(uint8_t dataByte) { + qspCrc ^= dataByte; +} + +/* + * Serial port used to send data + */ +#ifdef LORA_HARDWARE_SERIAL + +void radioPacketStart(void) { + +} + +void radioPacketEnd(void) { + Serial.end(); + delay(E45_TTL_100_UART_DOWNTIME); + Serial.begin(UART_SPEED); +} + void writeToRadio(uint8_t dataByte) { //Compute CRC - qspCrc ^= dataByte; + computeCrc(dataByte); //Write to radio Serial.write(dataByte); } +#endif + +#ifdef LORA_HARDWARE_SPI + +void radioPacketStart(void) { + LoRa.beginPacket(); +} + +void radioPacketEnd(void) { + LoRa.endPacket(); +} + +void writeToRadio(uint8_t dataByte) { + //Compute CRC + computeCrc(dataByte); + + //Write to radio + LoRa.write(dataByte); +} + +#endif + /* display.clearDisplay(); display.setCursor(0,0); @@ -216,7 +261,19 @@ display.display(); */ void setup(void) { + +#ifdef LORA_HARDWARE_SERIAL Serial.begin(UART_SPEED); +#endif + +#ifdef LORA_HARDWARE_SPI + if (!LoRa.begin(868E6)) { + Serial.println("LoRa init failed. Check your connections."); + while (true); + } + LoRa.onReceive(onReceive); + LoRa.receive(); +#endif #ifdef DEVICE_MODE_RX pinMode(PIN_LED, OUTPUT); @@ -299,17 +356,28 @@ void loop(void) { #endif +#ifdef LORA_HARDWARE_SERIAL if (Serial.available()) { qspDecodeIncomingFrame(Serial.read()); } +#endif if (transmitPayload) { transmitPayload = false; + radioPacketStart(); qspEncodeFrame(qspFrameToSend, qspPayloadLength, qspPayload); - Serial.end(); - delay(E45_TTL_100_UART_DOWNTIME); - Serial.begin(UART_SPEED); + radioPacketEnd(); } -} \ No newline at end of file +} + +#ifdef LORA_HARDWARE_SPI +void onReceive(int packetSize) { + if (packetSize == 0) return; + + while (LoRa.available()) { + qspDecodeIncomingFrame(LoRa.read()); + } +} +#endif \ No newline at end of file