Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / io / vtx_rtc6705.c
blob9789d4c7a5fc690421efc2cca3c5d6ece6035438
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 /* Created by jflyper */
23 #include <stdbool.h>
24 #include <stdint.h>
26 #include "platform.h"
28 #if defined(USE_VTX_RTC6705) && defined(USE_VTX_CONTROL)
30 #include "common/maths.h"
31 #include "common/utils.h"
33 #include "config/feature.h"
35 #include "drivers/max7456.h"
36 #include "drivers/time.h"
37 #include "drivers/vtx_table.h"
38 #include "drivers/vtx_rtc6705.h"
40 #include "io/vtx.h"
41 #include "io/vtx_rtc6705.h"
44 #if (defined(USE_CMS) || defined(USE_VTX_COMMON)) && !defined(USE_VTX_TABLE)
45 const char *rtc6705PowerNames[VTX_RTC6705_POWER_COUNT + 1] = {
46 "---", "MIN", "MAX"
48 #endif
50 #ifdef USE_VTX_COMMON
51 static vtxVTable_t rtc6705VTable; // Forward
52 static vtxDevice_t vtxRTC6705 = {
53 .vTable = &rtc6705VTable,
55 #endif
57 static uint16_t rtc6705Frequency;
58 static int8_t rtc6705PowerIndex;
59 static bool rtc6705PitModeActive;
61 static void vtxRTC6705SetBandAndChannel(vtxDevice_t *vtxDevice, uint8_t band, uint8_t channel);
62 static void vtxRTC6705SetFrequency(vtxDevice_t *vtxDevice, uint16_t frequency);
64 bool vtxRTC6705Init(void)
66 vtxCommonSetDevice(&vtxRTC6705);
67 #ifndef USE_VTX_TABLE
68 //without USE_VTX_TABLE, fill vtxTable variables with default settings (instead of loading them from PG)
69 vtxTablePowerLevels = VTX_RTC6705_POWER_COUNT;
70 for (int i = 0; i < VTX_RTC6705_POWER_COUNT + 1; i++) {
71 vtxTablePowerLabels[i] = rtc6705PowerNames[i];
73 for (int i = 0; i < VTX_RTC6705_POWER_COUNT; i++) {
74 vtxTablePowerValues[i] = i;
77 #endif
79 vtxInit();
81 return true;
84 bool vtxRTC6705CanUpdate(void)
86 #if defined(MAX7456_SPI_INSTANCE) && defined(RTC6705_SPI_INSTANCE) && defined(SPI_SHARED_MAX7456_AND_RTC6705)
87 if (featureIsEnabled(FEATURE_OSD)) {
88 return !max7456DmaInProgress();
90 #endif
91 return true;
94 #ifdef RTC6705_POWER_PIN
95 static void vtxRTC6705Configure(vtxDevice_t *vtxDevice)
97 uint16_t newPowerValue = 0;
98 vtxCommonLookupPowerValue(vtxDevice, rtc6705PowerIndex, &newPowerValue);
99 rtc6705SetRFPower(newPowerValue);
100 vtxRTC6705SetFrequency(vtxDevice, rtc6705Frequency);
103 static void vtxRTC6705EnableAndConfigure(vtxDevice_t *vtxDevice)
105 while (!vtxRTC6705CanUpdate());
107 rtc6705Enable();
109 delay(VTX_RTC6705_BOOT_DELAY);
111 vtxRTC6705Configure(vtxDevice);
113 #endif
115 static void vtxRTC6705Process(vtxDevice_t *vtxDevice, timeUs_t now)
117 UNUSED(vtxDevice);
118 UNUSED(now);
121 #ifdef USE_VTX_COMMON
122 // Interface to common VTX API
124 static vtxDevType_e vtxRTC6705GetDeviceType(const vtxDevice_t *vtxDevice)
126 UNUSED(vtxDevice);
127 return VTXDEV_RTC6705;
130 static bool vtxRTC6705IsReady(const vtxDevice_t *vtxDevice)
132 return vtxDevice != NULL;
135 static void vtxRTC6705SetBandAndChannel(vtxDevice_t *vtxDevice, uint8_t band, uint8_t channel)
137 UNUSED(vtxDevice);
138 UNUSED(band);
139 UNUSED(channel);
140 //rtc6705 does not support bands and channels, only frequencies
143 static void vtxRTC6705SetPowerByIndex(vtxDevice_t *vtxDevice, uint8_t index)
145 while (!vtxRTC6705CanUpdate());
147 uint16_t newPowerValue = 0;
148 if (!vtxCommonLookupPowerValue(vtxDevice, index, &newPowerValue)) {
149 return;
151 rtc6705PowerIndex = index;
152 rtc6705SetRFPower(newPowerValue);
155 static void vtxRTC6705SetPitMode(vtxDevice_t *vtxDevice, uint8_t onoff)
157 UNUSED(vtxDevice);
158 #ifdef RTC6705_POWER_PIN
159 if (onoff) {
160 // power device off
161 if (!rtc6705PitModeActive) {
162 // on, power it off
163 rtc6705PitModeActive = onoff;
164 rtc6705Disable();
165 return;
166 } else {
167 // already off
169 } else {
170 // change rf power and maybe turn the device on first
171 if (rtc6705PitModeActive) {
172 // if it's powered down, power it up, wait and configure channel, band and power.
173 rtc6705PitModeActive = onoff;
174 vtxRTC6705EnableAndConfigure(vtxDevice);
175 return;
176 } else {
177 //already on
180 #else
181 UNUSED(onoff);
182 #endif
185 static void vtxRTC6705SetFrequency(vtxDevice_t *vtxDevice, uint16_t frequency)
187 UNUSED(vtxDevice);
188 if (frequency >= VTX_RTC6705_FREQ_MIN && frequency <= VTX_RTC6705_FREQ_MAX) {
189 frequency = constrain(frequency, VTX_RTC6705_FREQ_MIN, VTX_RTC6705_FREQ_MAX);
190 rtc6705Frequency = frequency;
191 rtc6705SetFrequency(frequency);
195 static bool vtxRTC6705GetBandAndChannel(const vtxDevice_t *vtxDevice, uint8_t *pBand, uint8_t *pChannel)
197 UNUSED(vtxDevice);
198 *pBand = 0;
199 *pChannel = 0;
200 //rtc6705 does not support bands and channels, only frequencies.
201 //therefore always return 0
202 return true;
205 static bool vtxRTC6705GetPowerIndex(const vtxDevice_t *vtxDevice, uint8_t *pIndex)
207 UNUSED(vtxDevice);
208 *pIndex = rtc6705PowerIndex;
209 return true;
212 static bool vtxRTC6705GetFreq(const vtxDevice_t *vtxDevice, uint16_t *pFrequency)
214 UNUSED(vtxDevice);
215 *pFrequency = rtc6705Frequency;
216 return true;
219 static bool vtxRTC6705GetStatus(const vtxDevice_t *vtxDevice, unsigned *status)
221 UNUSED(vtxDevice);
222 *status = rtc6705PitModeActive ? VTX_STATUS_PIT_MODE : 0;
223 return true;
226 static uint8_t vtxRTC6705GetPowerLevels(const vtxDevice_t *vtxDevice, uint16_t *levels, uint16_t *powers)
228 UNUSED(vtxDevice);
229 UNUSED(levels);
230 UNUSED(powers);
232 return 0;
235 static vtxVTable_t rtc6705VTable = {
236 .process = vtxRTC6705Process,
237 .getDeviceType = vtxRTC6705GetDeviceType,
238 .isReady = vtxRTC6705IsReady,
239 .setBandAndChannel = vtxRTC6705SetBandAndChannel,
240 .setPowerByIndex = vtxRTC6705SetPowerByIndex,
241 .setPitMode = vtxRTC6705SetPitMode,
242 .setFrequency = vtxRTC6705SetFrequency,
243 .getBandAndChannel = vtxRTC6705GetBandAndChannel,
244 .getPowerIndex = vtxRTC6705GetPowerIndex,
245 .getFrequency = vtxRTC6705GetFreq,
246 .getStatus = vtxRTC6705GetStatus,
247 .getPowerLevels = vtxRTC6705GetPowerLevels,
248 .serializeCustomDeviceStatus = NULL,
251 #endif // VTX_COMMON
253 #endif // VTX_RTC6705