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)
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 */
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"
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] = {
51 static vtxVTable_t rtc6705VTable
; // Forward
52 static vtxDevice_t vtxRTC6705
= {
53 .vTable
= &rtc6705VTable
,
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
);
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
;
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();
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());
109 delay(VTX_RTC6705_BOOT_DELAY
);
111 vtxRTC6705Configure(vtxDevice
);
115 static void vtxRTC6705Process(vtxDevice_t
*vtxDevice
, timeUs_t now
)
121 #ifdef USE_VTX_COMMON
122 // Interface to common VTX API
124 static vtxDevType_e
vtxRTC6705GetDeviceType(const vtxDevice_t
*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
)
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
)) {
151 rtc6705PowerIndex
= index
;
152 rtc6705SetRFPower(newPowerValue
);
155 static void vtxRTC6705SetPitMode(vtxDevice_t
*vtxDevice
, uint8_t onoff
)
158 #ifdef RTC6705_POWER_PIN
161 if (!rtc6705PitModeActive
) {
163 rtc6705PitModeActive
= onoff
;
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
);
185 static void vtxRTC6705SetFrequency(vtxDevice_t
*vtxDevice
, uint16_t frequency
)
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
)
200 //rtc6705 does not support bands and channels, only frequencies.
201 //therefore always return 0
205 static bool vtxRTC6705GetPowerIndex(const vtxDevice_t
*vtxDevice
, uint8_t *pIndex
)
208 *pIndex
= rtc6705PowerIndex
;
212 static bool vtxRTC6705GetFreq(const vtxDevice_t
*vtxDevice
, uint16_t *pFrequency
)
215 *pFrequency
= rtc6705Frequency
;
219 static bool vtxRTC6705GetStatus(const vtxDevice_t
*vtxDevice
, unsigned *status
)
222 *status
= rtc6705PitModeActive
? VTX_STATUS_PIT_MODE
: 0;
226 static uint8_t vtxRTC6705GetPowerLevels(const vtxDevice_t
*vtxDevice
, uint16_t *levels
, uint16_t *powers
)
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
,
253 #endif // VTX_RTC6705