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"
43 #if (defined(USE_CMS) || defined(USE_VTX_COMMON)) && !defined(USE_VTX_TABLE)
44 const char *rtc6705PowerNames
[VTX_RTC6705_POWER_COUNT
+ 1] = {
50 static vtxVTable_t rtc6705VTable
; // Forward
51 static vtxDevice_t vtxRTC6705
= {
52 .vTable
= &rtc6705VTable
,
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
);
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
;
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();
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());
108 delay(VTX_RTC6705_BOOT_DELAY
);
110 vtxRTC6705Configure(vtxDevice
);
114 static void vtxRTC6705Process(vtxDevice_t
*vtxDevice
, timeUs_t now
)
120 #ifdef USE_VTX_COMMON
121 // Interface to common VTX API
123 static vtxDevType_e
vtxRTC6705GetDeviceType(const vtxDevice_t
*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
)
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
)) {
150 rtc6705PowerIndex
= index
;
151 rtc6705SetRFPower(newPowerValue
);
154 static void vtxRTC6705SetPitMode(vtxDevice_t
*vtxDevice
, uint8_t onoff
)
157 #ifdef RTC6705_POWER_PIN
160 if (!rtc6705PitModeActive
) {
162 rtc6705PitModeActive
= onoff
;
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
);
184 static void vtxRTC6705SetFrequency(vtxDevice_t
*vtxDevice
, uint16_t frequency
)
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
)
199 //rtc6705 does not support bands and channels, only frequencies.
200 //therefore always return 0
204 static bool vtxRTC6705GetPowerIndex(const vtxDevice_t
*vtxDevice
, uint8_t *pIndex
)
207 *pIndex
= rtc6705PowerIndex
;
211 static bool vtxRTC6705GetFreq(const vtxDevice_t
*vtxDevice
, uint16_t *pFrequency
)
214 *pFrequency
= rtc6705Frequency
;
218 static bool vtxRTC6705GetStatus(const vtxDevice_t
*vtxDevice
, unsigned *status
)
221 *status
= rtc6705PitModeActive
? VTX_STATUS_PIT_MODE
: 0;
225 static uint8_t vtxRTC6705GetPowerLevels(const vtxDevice_t
*vtxDevice
, uint16_t *levels
, uint16_t *powers
)
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
,
252 #endif // VTX_RTC6705