Skip to content

Commit bef20ac

Browse files
cruwallerRogero-
andauthored
Add R9SLIM+ target (ExpressLRS#209)
* Start working on R9SLIM+ variant * R9SLIM dual uart support * R9SLIM+ antenna switch init added Co-authored-by: Roger <[email protected]>
1 parent 0302e9a commit bef20ac

8 files changed

Lines changed: 149 additions & 62 deletions

File tree

12 KB
Binary file not shown.

src/lib/CRSF/CRSF.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#define PACKED __attribute__((packed))
2121

22+
//#define CRSF_RX_BAUDRATE 230400 // for linux debugging
2223
#define CRSF_RX_BAUDRATE 420000
2324
#define CRSF_OPENTX_FAST_BAUDRATE 400000
2425
#define CRSF_OPENTX_SLOW_BAUDRATE 115200 // Used for QX7 not supporting 400kbps
@@ -355,7 +356,7 @@ class CRSF
355356
static HardwareSerial Port;
356357

357358
static volatile uint16_t ChannelDataIn[16];
358-
static volatile uint16_t ChannelDataInPrev[16]; // Contains the previous RC channel data RX side only
359+
static volatile uint16_t ChannelDataInPrev[16]; // Contains the previous RC channel data RX side only
359360
static volatile uint16_t ChannelDataOut[16];
360361

361362
// current and sent switch values
@@ -472,4 +473,4 @@ class CRSF
472473
static volatile bool CRSFframeActive; //since we get a copy of the serial data use this flag to know when to ignore it
473474
};
474475

475-
#endif
476+
#endif

src/lib/EEPROM/elrs_eeprom.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33

44
#if defined(TARGET_R9M_TX) || defined(TARGET_R9M_LITE_TX) || defined(TARGET_R9M_LITE_PRO_TX)
55
extEEPROM EEPROM(kbits_2, 1, 1, 0x51);
6-
#endif
7-
8-
#ifdef TARGET_R9M_RX
6+
#elif defined(TARGET_R9M_RX)
97
extEEPROM EEPROM(kbits_2, 1, 1, 0x50);
8+
#else
9+
#define NO_EEPROM 1
1010
#endif
1111

1212
void
1313
ELRS_EEPROM::Begin()
1414
{
15+
#if !NO_EEPROM
1516
#ifdef PLATFORM_STM32
1617
Wire.setSDA(GPIO_PIN_SDA); // set is needed or it wont work :/
1718
Wire.setSCL(GPIO_PIN_SCL);
@@ -20,6 +21,7 @@ ELRS_EEPROM::Begin()
2021
#else
2122
EEPROM.begin(RESERVED_EEPROM_SIZE);
2223
#endif
24+
#endif /* NO_EEPROM */
2325
}
2426

2527
uint8_t
@@ -31,7 +33,11 @@ ELRS_EEPROM::ReadByte(const unsigned long address)
3133
Serial.println("ERROR! EEPROM address is out of bounds");
3234
return 0;
3335
}
36+
#if !NO_EEPROM
3437
return EEPROM.read(address);
38+
#else /* NO_EEPROM */
39+
return 0;
40+
#endif /* NO_EEPROM */
3541
}
3642

3743
void
@@ -43,7 +49,9 @@ ELRS_EEPROM::WriteByte(const unsigned long address, const uint8_t value)
4349
Serial.println("ERROR! EEPROM address is out of bounds");
4450
return;
4551
}
52+
#if !NO_EEPROM
4653
EEPROM.write(address, value);
54+
#endif /* NO_EEPROM */
4755
}
4856

4957
void

src/lib/SX127xDriver/SX127xHal.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ void SX127xHal::init()
4747
digitalWrite(GPIO_PIN_TX_ENABLE, LOW);
4848
#endif
4949

50+
#if defined(GPIO_PIN_ANTENNA_SELECT)
51+
pinMode(GPIO_PIN_ANTENNA_SELECT, OUTPUT);
52+
digitalWrite(GPIO_PIN_ANTENNA_SELECT, LOW);
53+
#endif
54+
5055
#ifdef PLATFORM_ESP32
5156
SPI.begin(GPIO_PIN_SCK, GPIO_PIN_MISO, GPIO_PIN_MOSI); // sck, miso, mosi, ss (ss can be any GPIO)
5257
SPI.setBitOrder(MSBFIRST);
@@ -248,4 +253,4 @@ void ICACHE_RAM_ATTR SX127xHal::dioISR()
248253
{
249254
RXdoneCallback();
250255
}
251-
}
256+
}

src/platformio.ini

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,30 @@ upload_command =
162162
python python/runpython.py $PYTHONEXE python/UARTupload.py $SOURCE
163163
lib_deps = ${env:Frsky_RX_R9MM_R9MINI_via_STLINK.lib_deps}
164164

165+
[env:Frsky_RX_R9SLIMPLUS_via_BetaflightPassthrough]
166+
167+
framework = arduino
168+
board = bluepill_f103c8
169+
build_unflags = -Os
170+
build_flags =
171+
-D TARGET_R9M_RX
172+
-D TARGET_R9SLIMPLUS_RX
173+
-D TARGET_100mW_MODULE
174+
-D PLATFORM_STM32
175+
-D HSE_VALUE=12000000U
176+
-O2
177+
-DVECT_TAB_OFFSET=0x8000U
178+
board_build.ldscript = variants/R9MM/R9MM_ldscript.ld
179+
src_filter = ${common_env_data.src_filter} -<ESP32*.*> -<ESP8266*.*> -<WS281B*.*> -<tx_*.cpp>
180+
extra_scripts = ${common_env_data.extra_scripts}
181+
upload_protocol = custom
182+
upload_command =
183+
python python/runpython.py $PYTHONEXE python/BFinitPassthrough.py 420000
184+
python python/runpython.py $PYTHONEXE python/UARTupload.py $SOURCE
185+
lib_deps =
186+
https://github.com/PaoloP74/extEEPROM.git
187+
Wire
188+
165189
[env:Frsky_RX_R9MX_via_STLINK]
166190
167191
framework = arduino

src/src/STM32_UARTinHandler.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#include "CRSF.h"
22
#include "targets.h"
33

4+
#if defined(TARGET_R9SLIMPLUS_RX)
5+
#define CRSF_RX_SERIAL CrsfRxSerial
6+
HardwareSerial CrsfRxSerial(USART3);
7+
#else /* !TARGET_R9SLIMPLUS_RX */
8+
#define CRSF_RX_SERIAL Serial
9+
#endif /* TARGET_R9SLIMPLUS_RX */
10+
411
extern CRSF crsf;
512

613
uint8_t UARTinPacketPtr;
@@ -35,10 +42,10 @@ void STM32_RX_UARTprocessPacket()
3542

3643
void STM32_RX_HandleUARTin()
3744
{
38-
while (Serial.available())
45+
while (CRSF_RX_SERIAL.available())
3946
{
4047
UARTLastDataTime = millis();
41-
char inChar = Serial.read();
48+
char inChar = CRSF_RX_SERIAL.read();
4249

4350
if ((inChar == CRSF_ADDRESS_CRSF_RECEIVER || inChar == CRSF_SYNC_BYTE) && UARTframeActive == false) // we got sync, reset write pointer
4451
{
@@ -58,11 +65,10 @@ void STM32_RX_HandleUARTin()
5865
{
5966
UARTinPacketPtr = 0;
6067
UARTframeActive = false;
61-
while (Serial.available())
68+
while (CRSF_RX_SERIAL.available())
6269
{
63-
Serial.read();
70+
(void)CRSF_RX_SERIAL.read();
6471
}
65-
Serial.flush();
6672
}
6773
}
6874

@@ -102,11 +108,12 @@ void STM32_RX_HandleUARTin()
102108
//Serial.println();
103109
UARTframeActive = false;
104110
UARTinPacketPtr = 0;
105-
while (Serial.available())
111+
while (CRSF_RX_SERIAL.available())
106112
{
107-
Serial.read(); // dunno why but the flush() method wasn't working
113+
// dunno why but the flush() method wasn't working
114+
// A: because flush() waits until all data is SENT aka TX buffer is empty
115+
(void)CRSF_RX_SERIAL.read();
108116
}
109-
Serial.flush();
110117
}
111118
}
112119
}

src/src/rx_main.cpp

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void ICACHE_RAM_ATTR getRFlinkInfo()
112112

113113
crsf.PackedRCdataOut.ch15 = UINT10_to_CRSF(map(rssiDBM, -100, -50, 0, 1023));
114114
crsf.PackedRCdataOut.ch14 = UINT10_to_CRSF(fmap(uplinkLQ, 0, 100, 0, 1023));
115-
115+
116116
// our rssiDBM is currently in the range -128 to 98, but BF wants a value in the range
117117
// 0 to 255 that maps to -1 * the negative part of the rssiDBM, so cap at 0.
118118
if (rssiDBM > 0)
@@ -278,14 +278,17 @@ void LostConnection()
278278
prevOffset = 0;
279279
LPF_Offset.init(0);
280280

281-
digitalWrite(GPIO_PIN_LED, 0); // turn off led
282281
Radio.SetFrequency(GetInitialFreq()); // in conn lost state we always want to listen on freq index 0
283282
hwTimer.stop();
284283
Serial.println("lost conn");
285284

286-
#ifdef PLATFORM_STM32
285+
#ifdef GPIO_PIN_LED_GREEN
287286
digitalWrite(GPIO_PIN_LED_GREEN, LOW);
288287
#endif
288+
289+
#ifdef GPIO_PIN_LED
290+
digitalWrite(GPIO_PIN_LED, 0); // turn off led
291+
#endif
289292
}
290293

291294
void ICACHE_RAM_ATTR TentativeConnection()
@@ -319,13 +322,16 @@ void GotConnection()
319322
RXtimerState = tim_tentative;
320323
GotConnectionMillis = millis();
321324

322-
RFmodeLastCycled = millis(); // give another 3 sec for loc to occur.
323-
digitalWrite(GPIO_PIN_LED, 1); // turn on led
325+
RFmodeLastCycled = millis(); // give another 3 sec for loc to occur.
324326
Serial.println("got conn");
325327

326-
#ifdef PLATFORM_STM32
328+
#ifdef GPIO_PIN_LED_GREEN
327329
digitalWrite(GPIO_PIN_LED_GREEN, HIGH);
328330
#endif
331+
332+
#ifdef GPIO_PIN_LED
333+
digitalWrite(GPIO_PIN_LED, HIGH); // turn on led
334+
#endif
329335
}
330336

331337
void ICACHE_RAM_ATTR UnpackChannelData_11bit()
@@ -492,7 +498,7 @@ void ICACHE_RAM_ATTR ProcessRFPacket()
492498
Radio.SetPPMoffsetReg(FreqCorrection); //as above but corrects a different PPM offset based on freq error
493499
#endif
494500
}
495-
501+
496502
doneProcessing = micros();
497503

498504
#ifndef DEBUG_SUPPRESS
@@ -518,6 +524,7 @@ void beginWebsever()
518524

519525
void sampleButton()
520526
{
527+
#ifdef GPIO_PIN_BUTTON
521528
bool buttonValue = digitalRead(GPIO_PIN_BUTTON);
522529

523530
if (buttonValue == false && buttonPrevValue == true)
@@ -550,6 +557,7 @@ void sampleButton()
550557
}
551558

552559
buttonPrevValue = buttonValue;
560+
#endif
553561
}
554562

555563
void ICACHE_RAM_ATTR RXdoneISR()
@@ -568,30 +576,43 @@ void ICACHE_RAM_ATTR TXdoneISR()
568576
void setup()
569577
{
570578
delay(100);
571-
Serial.println("ExpressLRS Module Booting...");
572579

573580
#ifdef PLATFORM_STM32
581+
#if defined(TARGET_R9SLIMPLUS_RX)
582+
CRSF_RX_SERIAL.setRx(GPIO_PIN_RCSIGNAL_RX);
583+
CRSF_RX_SERIAL.begin(CRSF_RX_BAUDRATE);
584+
585+
Serial.setTx(GPIO_PIN_RCSIGNAL_TX);
586+
#else /* !TARGET_R9SLIMPLUS_RX */
574587
#ifdef USE_R9MM_R9MINI_SBUS
575-
HardwareSerial(USART2);
588+
//HardwareSerial(USART2); // This is useless call
576589
#endif
577590
Serial.setTx(GPIO_PIN_RCSIGNAL_TX);
578591
Serial.setRx(GPIO_PIN_RCSIGNAL_RX);
579-
pinMode(GPIO_PIN_LED_GREEN, OUTPUT);
580-
pinMode(GPIO_PIN_LED_RED, OUTPUT);
581-
pinMode(GPIO_PIN_LED, OUTPUT);
582-
pinMode(GPIO_PIN_BUTTON, INPUT);
583-
#endif
592+
#endif /* TARGET_R9SLIMPLUS_RX */
593+
#endif /* PLATFORM_STM32 */
594+
595+
Serial.begin(CRSF_RX_BAUDRATE);
596+
597+
Serial.println("ExpressLRS Module Booting...");
584598

585599
#ifdef PLATFORM_ESP8266
586600
WiFi.mode(WIFI_OFF);
587601
WiFi.forceSleepBegin();
588-
pinMode(GPIO_PIN_LED, OUTPUT);
589-
#endif
602+
#endif /* PLATFORM_ESP8266 */
590603

591-
#ifdef PLATFORM_STM32
604+
#ifdef GPIO_PIN_LED_GREEN
592605
pinMode(GPIO_PIN_LED_GREEN, OUTPUT);
593-
#endif
606+
#endif /* GPIO_PIN_LED_GREEN */
607+
#ifdef GPIO_PIN_LED_RED
608+
pinMode(GPIO_PIN_LED_RED, OUTPUT);
609+
#endif /* GPIO_PIN_LED_RED */
610+
#if defined(GPIO_PIN_LED)
611+
pinMode(GPIO_PIN_LED, OUTPUT);
612+
#endif /* GPIO_PIN_LED */
613+
#ifdef GPIO_PIN_BUTTON
594614
pinMode(GPIO_PIN_BUTTON, INPUT);
615+
#endif /* GPIO_PIN_BUTTON */
595616

596617
#ifdef Regulatory_Domain_AU_915
597618
Serial.println("Setting 915MHz Mode");
@@ -603,9 +624,6 @@ void setup()
603624
Serial.println("Setting 433MHz Mode");
604625
#endif
605626

606-
// Serial.begin(230400); // for linux debugging
607-
Serial.begin(420000);
608-
609627
FHSSrandomiseFHSSsequence();
610628

611629
Radio.currFreq = GetInitialFreq();
@@ -615,7 +633,9 @@ void setup()
615633
bool init_success = Radio.Begin();
616634
while (!init_success)
617635
{
636+
#ifdef GPIO_PIN_LED
618637
digitalWrite(GPIO_PIN_LED, LED);
638+
#endif
619639
LED = !LED;
620640
delay(200);
621641
Serial.println("Failed to detect RF chipset!!!");
@@ -682,7 +702,9 @@ void loop()
682702
HandleWebUpdate();
683703
if (millis() > WEB_UPDATE_LED_FLASH_INTERVAL + webUpdateLedFlashIntervalLast)
684704
{
705+
#ifdef GPIO_PIN_LED
685706
digitalWrite(GPIO_PIN_LED, LED);
707+
#endif
686708
LED = !LED;
687709
webUpdateLedFlashIntervalLast = millis();
688710
}
@@ -722,7 +744,9 @@ void loop()
722744
SetRFLinkRate(scanIndex % CURR_RATE_MAX); //switch between rates
723745
SendLinkStatstoFCintervalLastSent = millis();
724746
LQCALC.reset();
747+
#ifdef GPIO_PIN_LED
725748
digitalWrite(GPIO_PIN_LED, LED);
749+
#endif
726750
LED = !LED;
727751
Serial.println(ExpressLRS_currAirRate_Modparams->interval);
728752
scanIndex++;
@@ -769,7 +793,7 @@ void loop()
769793
#endif
770794
}
771795

772-
#ifdef PLATFORM_STM32
796+
#ifdef PLATFORM_STM32
773797
STM32_RX_HandleUARTin();
774-
#endif
798+
#endif
775799
}

0 commit comments

Comments
 (0)