Logging of the S -term values in blackbox for Fixed wings. (#14012)
[betaflight.git] / src / main / io / vtx_rtc6705.c
blob8c3ad7333e00d69d60cb772d5aa49001b25b374b
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"
43 #if (defined(USE_CMS) || defined(USE_VTX_COMMON)) && !defined(USE_VTX_TABLE)
44 const char *rtc6705PowerNames[VTX_RTC6705_POWER_COUNT + 1] = {
45 "---", "MIN", "MAX"
47 #endif
49 #ifdef USE_VTX_COMMON
50 static vtxVTable_t rtc6705VTable; // Forward
51 static vtxDevice_t vtxRTC6705 = {
52 .vTable = &rtc6705VTable,
54 #endif
56 static uint16_t rtc6705Frequency;
57 static int8_t rtc6705PowerIndex;
58 static bool rtc6705PitModeActive;
60 static void vtxRTC6705SetBandAndChannel(vtxDevice_t *vtxDevice, uint8_t band, uint8_t channel);
61 static void vtxRTC6705SetFrequency(vtxDevice_t *vtxDevice, uint16_t frequency);
63 bool vtxRTC6705Init(void)
65 vtxCommonSetDevice(&vtxRTC6705);
66 #ifndef USE_VTX_TABLE
67 //without USE_VTX_TABLE, fill vtxTable variables with default settings (instead of loading them from PG)
68 vtxTablePowerLevels = VTX_RTC6705_POWER_COUNT;
69 for (int i = 0; i < VTX_RTC6705_POWER_COUNT + 1; i++) {
70 vtxTablePowerLabels[i] = rtc6705PowerNames[i];
72 for (int i = 0; i < VTX_RTC6705_POWER_COUNT; i++) {
73 vtxTablePowerValues[i] = i;
76 #endif
78 vtxInit();
80 return true;
83 bool vtxRTC6705CanUpdate(void)
85 #if defined(MAX7456_SPI_INSTANCE) && defined(RTC6705_SPI_INSTANCE) && defined(SPI_SHARED_MAX7456_AND_RTC6705)
86 if (featureIsEnabled(FEATURE_OSD)) {
87 return !max7456DmaInProgress();
89 #endif
90 return true;
93 #ifdef RTC6705_POWER_PIN
94 static void vtxRTC6705Configure(vtxDevice_t *vtxDevice)
96 uint16_t newPowerValue = 0;
97 vtxCommonLookupPowerValue(vtxDevice, rtc6705PowerIndex, &newPowerValue);
98 rtc6705SetRFPower(newPowerValue);
99 vtxRTC6705SetFrequency(vtxDevice, rtc6705Frequency);
102 static void vtxRTC6705EnableAndConfigure(vtxDevice_t *vtxDevice)
104 while (!vtxRTC6705CanUpdate());
106 rtc6705Enable();
108 delay(VTX_RTC6705_BOOT_DELAY);
110 vtxRTC6705Configure(vtxDevice);
112 #endif
114 static void vtxRTC6705Process(vtxDevice_t *vtxDevice, timeUs_t now)
116 UNUSED(vtxDevice);
117 UNUSED(now);
120 #ifdef USE_VTX_COMMON
121 // Interface to common VTX API
123 static vtxDevType_e vtxRTC6705GetDeviceType(const vtxDevice_t *vtxDevice)
125 UNUSED(vtxDevice);
126 return VTXDEV_RTC6705;
129 static bool vtxRTC6705IsReady(const vtxDevice_t *vtxDevice)
131 return vtxDevice != NULL;
134 static void vtxRTC6705SetBandAndChannel(vtxDevice_t *vtxDevice, uint8_t band, uint8_t channel)
136 UNUSED(vtxDevice);
137 UNUSED(band);
138 UNUSED(channel);
139 //rtc6705 does not support bands and channels, only frequencies
142 static void vtxRTC6705SetPowerByIndex(vtxDevice_t *vtxDevice, uint8_t index)
144 while (!vtxRTC6705CanUpdate());
146 uint16_t newPowerValue = 0;
147 if (!vtxCommonLookupPowerValue(vtxDevice, index, &newPowerValue)) {
148 return;
150 rtc6705PowerIndex = index;
151 rtc6705SetRFPower(newPowerValue);
154 static void vtxRTC6705SetPitMode(vtxDevice_t *vtxDevice, uint8_t onoff)
156 UNUSED(vtxDevice);
157 #ifdef RTC6705_POWER_PIN
158 if (onoff) {
159 // power device off
160 if (!rtc6705PitModeActive) {
161 // on, power it off
162 rtc6705PitModeActive = onoff;
163 rtc6705Disable();
164 return;
165 } else {
166 // already off
168 } else {
169 // change rf power and maybe turn the device on first
170 if (rtc6705PitModeActive) {
171 // if it's powered down, power it up, wait and configure channel, band and power.
172 rtc6705PitModeActive = onoff;
173 vtxRTC6705EnableAndConfigure(vtxDevice);
174 return;
175 } else {
176 //already on
179 #else
180 UNUSED(onoff);
181 #endif
184 static void vtxRTC6705SetFrequency(vtxDevice_t *vtxDevice, uint16_t frequency)
186 UNUSED(vtxDevice);
187 if (frequency >= VTX_RTC6705_FREQ_MIN && frequency <= VTX_RTC6705_FREQ_MAX) {
188 frequency = constrain(frequency, VTX_RTC6705_FREQ_MIN, VTX_RTC6705_FREQ_MAX);
189 rtc6705Frequency = frequency;
190 rtc6705SetFrequency(frequency);
194 static bool vtxRTC6705GetBandAndChannel(const vtxDevice_t *vtxDevice, uint8_t *pBand, uint8_t *pChannel)
196 UNUSED(vtxDevice);
197 *pBand = 0;
198 *pChannel = 0;
199 //rtc6705 does not support bands and channels, only frequencies.
200 //therefore always return 0
201 return true;
204 static bool vtxRTC6705GetPowerIndex(const vtxDevice_t *vtxDevice, uint8_t *pIndex)
206 UNUSED(vtxDevice);
207 *pIndex = rtc6705PowerIndex;
208 return true;
211 static bool vtxRTC6705GetFreq(const vtxDevice_t *vtxDevice, uint16_t *pFrequency)
213 UNUSED(vtxDevice);
214 *pFrequency = rtc6705Frequency;
215 return true;
218 static bool vtxRTC6705GetStatus(const vtxDevice_t *vtxDevice, unsigned *status)
220 UNUSED(vtxDevice);
221 *status = rtc6705PitModeActive ? VTX_STATUS_PIT_MODE : 0;
222 return true;
225 static uint8_t vtxRTC6705GetPowerLevels(const vtxDevice_t *vtxDevice, uint16_t *levels, uint16_t *powers)
227 UNUSED(vtxDevice);
228 UNUSED(levels);
229 UNUSED(powers);
231 return 0;
234 static vtxVTable_t rtc6705VTable = {
235 .process = vtxRTC6705Process,
236 .getDeviceType = vtxRTC6705GetDeviceType,
237 .isReady = vtxRTC6705IsReady,
238 .setBandAndChannel = vtxRTC6705SetBandAndChannel,
239 .setPowerByIndex = vtxRTC6705SetPowerByIndex,
240 .setPitMode = vtxRTC6705SetPitMode,
241 .setFrequency = vtxRTC6705SetFrequency,
242 .getBandAndChannel = vtxRTC6705GetBandAndChannel,
243 .getPowerIndex = vtxRTC6705GetPowerIndex,
244 .getFrequency = vtxRTC6705GetFreq,
245 .getStatus = vtxRTC6705GetStatus,
246 .getPowerLevels = vtxRTC6705GetPowerLevels,
247 .serializeCustomDeviceStatus = NULL,
250 #endif // VTX_COMMON
252 #endif // VTX_RTC6705