Add tlmConfirm to tlm_dl ota packet-structure (#2991)
[ExpressLRS.git] / src / lib / RFAMP / RFAMP_hal.cpp
blobada9f4bb7e9097921eb9e12d6bed54053cd4ecde
1 #ifndef UNIT_TEST
3 #include "RFAMP_hal.h"
4 #include "logging.h"
6 RFAMP_hal *RFAMP_hal::instance = NULL;
8 RFAMP_hal::RFAMP_hal()
10 instance = this;
13 void RFAMP_hal::init()
15 DBGLN("RFAMP_hal Init");
17 #if defined(PLATFORM_ESP32)
18 #define SET_BIT(n) ((n != UNDEF_PIN) ? 1ULL << n : 0)
20 txrx_disable_clr_bits = 0;
21 txrx_disable_clr_bits |= SET_BIT(GPIO_PIN_PA_ENABLE);
22 txrx_disable_clr_bits |= SET_BIT(GPIO_PIN_RX_ENABLE);
23 txrx_disable_clr_bits |= SET_BIT(GPIO_PIN_TX_ENABLE);
24 txrx_disable_clr_bits |= SET_BIT(GPIO_PIN_RX_ENABLE_2);
25 txrx_disable_clr_bits |= SET_BIT(GPIO_PIN_TX_ENABLE_2);
27 tx1_enable_set_bits = 0;
28 tx1_enable_clr_bits = 0;
29 tx1_enable_set_bits |= SET_BIT(GPIO_PIN_PA_ENABLE);
30 tx1_enable_set_bits |= SET_BIT(GPIO_PIN_TX_ENABLE);
31 tx1_enable_clr_bits |= SET_BIT(GPIO_PIN_RX_ENABLE);
32 tx1_enable_clr_bits |= SET_BIT(GPIO_PIN_RX_ENABLE_2);
34 tx2_enable_set_bits = 0;
35 tx2_enable_clr_bits = 0;
36 tx2_enable_set_bits |= SET_BIT(GPIO_PIN_PA_ENABLE);
37 tx2_enable_set_bits |= SET_BIT(GPIO_PIN_TX_ENABLE_2);
38 tx2_enable_clr_bits |= SET_BIT(GPIO_PIN_RX_ENABLE_2);
39 tx2_enable_clr_bits |= SET_BIT(GPIO_PIN_RX_ENABLE);
41 tx_all_enable_set_bits = 0;
42 tx_all_enable_clr_bits = 0;
43 tx_all_enable_set_bits = tx1_enable_set_bits | tx2_enable_set_bits;
44 tx_all_enable_clr_bits = tx1_enable_clr_bits | tx2_enable_clr_bits;
46 rx_enable_set_bits = 0;
47 rx_enable_clr_bits = 0;
48 rx_enable_set_bits |= SET_BIT(GPIO_PIN_PA_ENABLE);
49 rx_enable_set_bits |= SET_BIT(GPIO_PIN_RX_ENABLE);
50 rx_enable_set_bits |= SET_BIT(GPIO_PIN_RX_ENABLE_2);
51 rx_enable_clr_bits |= SET_BIT(GPIO_PIN_TX_ENABLE);
52 rx_enable_clr_bits |= SET_BIT(GPIO_PIN_TX_ENABLE_2);
53 #else
54 rx_enabled = false;
55 tx1_enabled = false;
56 tx2_enabled = false;
57 #endif
59 if (GPIO_PIN_PA_ENABLE != UNDEF_PIN)
61 DBGLN("Use PA enable pin: %d", GPIO_PIN_PA_ENABLE);
62 pinMode(GPIO_PIN_PA_ENABLE, OUTPUT);
63 digitalWrite(GPIO_PIN_PA_ENABLE, LOW);
66 if (GPIO_PIN_TX_ENABLE != UNDEF_PIN)
68 DBGLN("Use TX pin: %d", GPIO_PIN_TX_ENABLE);
69 pinMode(GPIO_PIN_TX_ENABLE, OUTPUT);
70 digitalWrite(GPIO_PIN_TX_ENABLE, LOW);
73 if (GPIO_PIN_RX_ENABLE != UNDEF_PIN)
75 DBGLN("Use RX pin: %d", GPIO_PIN_RX_ENABLE);
76 pinMode(GPIO_PIN_RX_ENABLE, OUTPUT);
77 digitalWrite(GPIO_PIN_RX_ENABLE, LOW);
80 if (GPIO_PIN_TX_ENABLE_2 != UNDEF_PIN)
82 DBGLN("Use TX_2 pin: %d", GPIO_PIN_TX_ENABLE_2);
83 pinMode(GPIO_PIN_TX_ENABLE_2, OUTPUT);
84 digitalWrite(GPIO_PIN_TX_ENABLE_2, LOW);
87 if (GPIO_PIN_RX_ENABLE_2 != UNDEF_PIN)
89 DBGLN("Use RX_2 pin: %d", GPIO_PIN_RX_ENABLE_2);
90 pinMode(GPIO_PIN_RX_ENABLE_2, OUTPUT);
91 digitalWrite(GPIO_PIN_RX_ENABLE_2, LOW);
95 void ICACHE_RAM_ATTR RFAMP_hal::TXenable(SX12XX_Radio_Number_t radioNumber)
97 #if defined(PLATFORM_ESP32_C3)
98 if (radioNumber == SX12XX_Radio_All)
100 GPIO.out_w1ts.out_w1ts = tx_all_enable_set_bits;
101 GPIO.out_w1tc.out_w1tc = tx_all_enable_clr_bits;
103 else if (radioNumber == SX12XX_Radio_2)
105 GPIO.out_w1ts.out_w1ts = tx2_enable_set_bits;
106 GPIO.out_w1tc.out_w1tc = tx2_enable_clr_bits;
108 else
110 GPIO.out_w1ts.out_w1ts = tx1_enable_set_bits;
111 GPIO.out_w1tc.out_w1tc = tx1_enable_clr_bits;
113 #elif defined(PLATFORM_ESP32)
114 if (radioNumber == SX12XX_Radio_All)
116 GPIO.out_w1ts = (uint32_t)tx_all_enable_set_bits;
117 GPIO.out_w1tc = tx_all_enable_clr_bits;
119 GPIO.out1_w1ts.data = tx_all_enable_set_bits >> 32;
120 GPIO.out1_w1tc.data = tx_all_enable_clr_bits >> 32;
122 else if (radioNumber == SX12XX_Radio_2)
124 GPIO.out_w1ts = tx2_enable_set_bits;
125 GPIO.out_w1tc = tx2_enable_clr_bits;
127 GPIO.out1_w1ts.data = tx2_enable_set_bits >> 32;
128 GPIO.out1_w1tc.data = tx2_enable_clr_bits >> 32;
130 else
132 GPIO.out_w1ts = tx1_enable_set_bits;
133 GPIO.out_w1tc = tx1_enable_clr_bits;
135 GPIO.out1_w1ts.data = tx1_enable_set_bits >> 32;
136 GPIO.out1_w1tc.data = tx1_enable_clr_bits >> 32;
138 #else
139 if (!tx1_enabled && !tx2_enabled && !rx_enabled)
141 if (GPIO_PIN_PA_ENABLE != UNDEF_PIN)
143 digitalWrite(GPIO_PIN_PA_ENABLE, HIGH);
146 if (rx_enabled)
148 if (GPIO_PIN_RX_ENABLE != UNDEF_PIN)
150 digitalWrite(GPIO_PIN_RX_ENABLE, LOW);
152 if (GPIO_PIN_RX_ENABLE_2 != UNDEF_PIN)
154 digitalWrite(GPIO_PIN_RX_ENABLE_2, LOW);
156 rx_enabled = false;
158 if (radioNumber == SX12XX_Radio_1 && !tx1_enabled)
160 if (GPIO_PIN_TX_ENABLE != UNDEF_PIN)
162 digitalWrite(GPIO_PIN_TX_ENABLE, HIGH);
164 if (GPIO_PIN_TX_ENABLE_2 != UNDEF_PIN)
166 digitalWrite(GPIO_PIN_TX_ENABLE_2, LOW);
168 tx1_enabled = true;
169 tx2_enabled = false;
171 if (radioNumber == SX12XX_Radio_2 && !tx2_enabled)
173 if (GPIO_PIN_TX_ENABLE != UNDEF_PIN)
175 digitalWrite(GPIO_PIN_TX_ENABLE, LOW);
177 if (GPIO_PIN_TX_ENABLE_2 != UNDEF_PIN)
179 digitalWrite(GPIO_PIN_TX_ENABLE_2, HIGH);
181 tx1_enabled = false;
182 tx2_enabled = true;
184 #endif
187 void ICACHE_RAM_ATTR RFAMP_hal::RXenable()
189 #if defined(PLATFORM_ESP32_C3)
190 GPIO.out_w1ts.out_w1ts = rx_enable_set_bits;
191 GPIO.out_w1tc.out_w1tc = rx_enable_clr_bits;
192 #elif defined(PLATFORM_ESP32)
193 GPIO.out_w1ts = rx_enable_set_bits;
194 GPIO.out_w1tc = rx_enable_clr_bits;
196 GPIO.out1_w1ts.data = rx_enable_set_bits >> 32;
197 GPIO.out1_w1tc.data = rx_enable_clr_bits >> 32;
198 #else
199 if (!rx_enabled)
201 if (!tx1_enabled && !tx2_enabled && GPIO_PIN_PA_ENABLE != UNDEF_PIN)
202 digitalWrite(GPIO_PIN_PA_ENABLE, HIGH);
204 if (tx1_enabled && GPIO_PIN_TX_ENABLE != UNDEF_PIN)
206 digitalWrite(GPIO_PIN_TX_ENABLE, LOW);
207 tx1_enabled = false;
210 if (tx2_enabled && GPIO_PIN_TX_ENABLE_2 != UNDEF_PIN)
212 digitalWrite(GPIO_PIN_TX_ENABLE_2, LOW);
213 tx2_enabled = false;
216 if (GPIO_PIN_RX_ENABLE != UNDEF_PIN)
218 digitalWrite(GPIO_PIN_RX_ENABLE, HIGH);
220 if (GPIO_PIN_RX_ENABLE_2 != UNDEF_PIN)
222 digitalWrite(GPIO_PIN_RX_ENABLE_2, HIGH);
225 rx_enabled = true;
227 #endif
230 void ICACHE_RAM_ATTR RFAMP_hal::TXRXdisable()
232 #if defined(PLATFORM_ESP32_C3)
233 GPIO.out_w1tc.out_w1tc = txrx_disable_clr_bits;
234 #elif defined(PLATFORM_ESP32)
235 GPIO.out_w1tc = txrx_disable_clr_bits;
236 GPIO.out1_w1tc.data = txrx_disable_clr_bits >> 32;
237 #else
238 if (rx_enabled)
240 if (GPIO_PIN_RX_ENABLE != UNDEF_PIN)
242 digitalWrite(GPIO_PIN_RX_ENABLE, LOW);
244 if (GPIO_PIN_RX_ENABLE_2 != UNDEF_PIN)
246 digitalWrite(GPIO_PIN_RX_ENABLE_2, LOW);
248 rx_enabled = false;
250 if (tx1_enabled)
252 if (GPIO_PIN_PA_ENABLE != UNDEF_PIN)
254 digitalWrite(GPIO_PIN_PA_ENABLE, LOW);
256 if (GPIO_PIN_TX_ENABLE != UNDEF_PIN)
258 digitalWrite(GPIO_PIN_TX_ENABLE, LOW);
260 tx1_enabled = false;
262 if (tx2_enabled)
264 if (GPIO_PIN_PA_ENABLE != UNDEF_PIN)
266 digitalWrite(GPIO_PIN_PA_ENABLE, LOW);
268 if (GPIO_PIN_TX_ENABLE_2 != UNDEF_PIN)
270 digitalWrite(GPIO_PIN_TX_ENABLE_2, LOW);
272 tx2_enabled = false;
274 #endif
277 #endif // UNIT_TEST