Skip to content

Commit 5911445

Browse files
authored
Simplify sx127x driver (ExpressLRS#96)
1 parent 1051b51 commit 5911445

51 files changed

Lines changed: 2268 additions & 2672 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/lib/CRSF/CRSF.cpp

Lines changed: 360 additions & 336 deletions
Large diffs are not rendered by default.

src/lib/CRSF/CRSF.h

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,6 @@ static uint16_t ICACHE_RAM_ATTR fmap(uint16_t x, uint16_t in_min, uint16_t in_ma
285285
static inline uint16_t ICACHE_RAM_ATTR CRSF_to_US(uint16_t Val) { return round(fmap(Val, 172.0, 1811.0, 988.0, 2012.0)); };
286286
static inline uint16_t ICACHE_RAM_ATTR UINT10_to_CRSF(uint16_t Val) { return round(fmap(Val, 0.0, 1024.0, 172.0, 1811.0)); };
287287

288-
static inline uint16_t ICACHE_RAM_ATTR SWITCH3b_to_CRSF(uint16_t Val) { return round(map(Val, 0, 7, 188, 1795)); };
289-
290288
// 2b switches use 0, 1 and 2 as values to represent low, middle and high
291289
static inline uint16_t ICACHE_RAM_ATTR SWITCH2b_to_CRSF(uint16_t Val) { return round(map(Val, 0, 2, 188, 1795)); };
292290

@@ -342,40 +340,20 @@ class CRSF
342340
{
343341

344342
public:
345-
//CRSF(HardwareSerial& serial);
346-
347-
#if defined(PLATFORM_ESP8266) || defined(UNIT_TEST)
343+
#if defined(PLATFORM_ESP8266) || defined(TARGET_R9M_RX) || defined(UNIT_TEST)
348344

349345
CRSF(Stream *dev) : _dev(dev)
350346
{
351347
}
352-
CRSF(Stream &dev) : _dev(&dev) {}
353-
354-
void InitSerial()
355-
{
356-
_dev->println("CRSF Lib Ready!");
357-
}
358348

359-
#endif
360-
361-
#ifdef TARGET_R9M_RX
362-
363-
CRSF(Stream *dev) : _dev(dev)
364-
{
365-
}
366349
CRSF(Stream &dev) : _dev(&dev) {}
367350

368-
void InitSerial()
369-
{
370-
_dev->println("CRSF Lib Ready!");
371-
}
372-
#endif
351+
#endif
373352

374353
static HardwareSerial Port;
375-
//static Stream *Port;
376354

377355
static volatile uint16_t ChannelDataIn[16];
378-
static volatile uint16_t ChannelDataInPrev[16]; // Contains the previous RC channel data
356+
static volatile uint16_t ChannelDataInPrev[16]; // Contains the previous RC channel data RX side only
379357
static volatile uint16_t ChannelDataOut[16];
380358

381359
// current and sent switch values
@@ -426,26 +404,20 @@ class CRSF
426404
static uint32_t GoodPktsCount;
427405
static uint32_t BadPktsCount;
428406

429-
static void ICACHE_RAM_ATTR duplex_set_RX();
430-
static void ICACHE_RAM_ATTR duplex_set_TX();
431-
static void ICACHE_RAM_ATTR duplex_set_HIGHZ();
432-
static void ICACHE_RAM_ATTR FlushSerial();
433-
434407
#ifdef PLATFORM_ESP32
435408
static void ICACHE_RAM_ATTR ESP32uartTask(void *pvParameters);
436409
static void ICACHE_RAM_ATTR UARTwdt(void *pvParametersxHandleSerialOutFIFO);
410+
static void ICACHE_RAM_ATTR duplex_set_RX();
411+
static void ICACHE_RAM_ATTR duplex_set_TX();
437412
#endif
438-
#ifdef PLATFORM_ESP8266
439-
// static void ICACHE_RAM_ATTR ESP8266ReadUart();
440-
#endif
441-
#ifdef TARGET_R9M_TX
413+
414+
#if defined(TARGET_R9M_TX) || defined(TARGET_R9M_LITE_TX)
442415
static void ICACHE_RAM_ATTR STM32initUART();
443416
static void ICACHE_RAM_ATTR UARTwdt();
444417
static void ICACHE_RAM_ATTR STM32handleUARTin();
445418
static void ICACHE_RAM_ATTR STM32handleUARTout();
446419
#endif
447420

448-
449421
void ICACHE_RAM_ATTR sendRCFrameToFC();
450422
void ICACHE_RAM_ATTR sendMSPFrameToFC(mspPacket_t* packet);
451423
void ICACHE_RAM_ATTR sendLinkStatisticsToFC();
@@ -460,7 +432,7 @@ class CRSF
460432
void ICACHE_RAM_ATTR setSentSwitch(uint8_t index, uint8_t value);
461433

462434
///// Variables for OpenTX Syncing //////////////////////////
463-
#define OpenTXsyncPacketInterval 200 // in ms
435+
#define OpenTXsyncPacketInterval 200 // in ms
464436
static volatile uint32_t OpenTXsyncLastSent;
465437
static volatile uint32_t RequestedRCpacketInterval;
466438
static volatile uint32_t RCdataLastRecv;
@@ -474,6 +446,7 @@ class CRSF
474446
static bool ICACHE_RAM_ATTR STM32ProcessPacket();
475447
static void ICACHE_RAM_ATTR GetChannelDataIn();
476448
static void ICACHE_RAM_ATTR updateSwitchValues();
449+
bool RXhandleUARTout();
477450

478451
static void inline nullCallback(void);
479452

src/lib/DAC/DAC.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11

22
#ifdef TARGET_R9M_TX
33

4-
#include "../../src/targets.h"
54
#include "DAC.h"
6-
#include "LoRaRadioLib.h"
5+
#include "SX127xDriver.h"
76

87
extern SX127xDriver Radio;
98

@@ -27,13 +26,15 @@ uint8_t R9DAC::ADDR = 0;
2726

2827
DAC_STATE_ R9DAC::DAC_STATE = UNKNOWN;
2928

30-
void R9DAC::init(uint8_t SDA_, uint8_t SCL_, uint8_t ADDR_)
29+
void R9DAC::init()
3130
{
32-
R9DAC::SDA = SDA_;
33-
R9DAC::SCL = SCL_;
34-
R9DAC::ADDR = ADDR_;
31+
Serial.println("Wire.h begin()");
3532

36-
Wire.setSDA(SDA); // set is needed or it wont work :/
33+
R9DAC::SDA = GPIO_PIN_SDA;
34+
R9DAC::SCL = GPIO_PIN_SCL;
35+
R9DAC::ADDR = 0b0001100;
36+
37+
Wire.setSDA(GPIO_PIN_SDA); // set is needed or it wont work :/
3738
Wire.setSCL(SCL);
3839
Wire.begin();
3940
R9DAC::DAC_STATE = UNKNOWN;
@@ -57,7 +58,6 @@ void R9DAC::resume()
5758
{
5859
Radio.SetOutputPower(0b0000);
5960
R9DAC::setVoltageRegDirect(CurrVoltageRegVal);
60-
R9DAC::DAC_STATE = RUNNING;
6161
}
6262
}
6363

@@ -66,6 +66,7 @@ void R9DAC::setVoltageMV(uint32_t voltsMV)
6666
uint8_t ScaledVolts = map(voltsMV, 0, VCC, 0, 255);
6767
setVoltageRegDirect(ScaledVolts);
6868
CurrVoltageMV = voltsMV;
69+
Serial.println(CurrVoltageMV);
6970
}
7071

7172
void R9DAC::setVoltageRegDirect(uint8_t voltReg)

src/lib/DAC/DAC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class R9DAC
4141
static uint32_t CurrVoltageMV;
4242
static uint8_t CurrVoltageRegVal;
4343

44-
static void init(uint8_t SDA_, uint8_t SCL_, uint8_t ADDR_);
44+
static void init();
4545
static void standby();
4646
static void resume();
4747
static void setVoltageMV(uint32_t voltsMV);

src/lib/EEPROM/elrs_eeprom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include "../../src/targets.h"
4-
#include <Wire.h>
4+
//#include <Wire.h>
55
#ifdef PLATFORM_STM32
66
#include <extEEPROM.h>
77
#else

src/lib/FIFO/FIFO.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ void ICACHE_RAM_ATTR FIFO::push(uint8_t data)
4444
{
4545
if (numElements == FIFO_SIZE)
4646
{
47-
Serial.println(("CRITICAL ERROR: Buffer full, will flush"));
48-
//this->flush();
49-
this->popBytes(nullptr, numElements);
47+
Serial.println("CRITICAL ERROR: Buffer full, will flush");
48+
this->flush();
49+
//this->popBytes(nullptr, numElements);
5050
return;
5151
}
5252
else
@@ -133,8 +133,12 @@ int ICACHE_RAM_ATTR FIFO::size()
133133

134134
void ICACHE_RAM_ATTR FIFO::flush()
135135
{
136-
memset(buffer, 0x00, FIFO_SIZE);
137-
head = 0;
138-
tail = 0;
139-
numElements = 0;
136+
if (numElements > 0)
137+
{
138+
memset(buffer, 0x00, FIFO_SIZE);
139+
head = 0;
140+
tail = 0;
141+
numElements = 0;
142+
Serial.println("FIFO full err - it was flushed");
143+
}
140144
}

src/lib/FIFO/FIFO.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434
#include <Arduino.h>
3535
#include "../../src/targets.h"
3636

37-
#define FIFO_SIZE 512
37+
#define FIFO_SIZE 256
3838

3939
class FIFO
4040
{
4141
private:
42-
int head;
43-
int tail;
44-
int numElements;
42+
uint8_t head;
43+
uint8_t tail;
44+
uint8_t numElements;
4545
uint8_t buffer[FIFO_SIZE] = {0};
4646

4747
public:

src/lib/LoRaRadioLib/LoRaRadioLib.h

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)