CF/BF - Move CMS for RTC6705 into vtx_rtc6705.c for consistency with
[betaflight.git] / src / main / drivers / vtx_soft_spi_rtc6705.c
blobb6b9a2b06d19276f1048fc663bef587567401e7e
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdbool.h>
19 #include <stdint.h>
21 #include "platform.h"
23 #ifdef VTX_RTC6705SOFTSPI
25 #include "bus_spi.h"
26 #include "io.h"
27 #include "system.h"
28 #include "light_led.h"
30 #include "vtx_soft_spi_rtc6705.h"
32 #define RTC6705_SPICLK_ON IOHi(rtc6705ClkPin)
33 #define RTC6705_SPICLK_OFF IOLo(rtc6705ClkPin)
35 #define RTC6705_SPIDATA_ON IOHi(rtc6705DataPin)
36 #define RTC6705_SPIDATA_OFF IOLo(rtc6705DataPin)
38 #define RTC6705_SPILE_ON IOHi(rtc6705LePin)
39 #define RTC6705_SPILE_OFF IOLo(rtc6705LePin)
41 const uint16_t vtx_freq[] =
43 5865, 5845, 5825, 5805, 5785, 5765, 5745, 5725, // Boacam A
44 5733, 5752, 5771, 5790, 5809, 5828, 5847, 5866, // Boscam B
45 5705, 5685, 5665, 5645, 5885, 5905, 5925, 5945, // Boscam E
46 5740, 5760, 5780, 5800, 5820, 5840, 5860, 5880, // FatShark
47 5658, 5695, 5732, 5769, 5806, 5843, 5880, 5917, // RaceBand
50 static IO_t rtc6705DataPin = IO_NONE;
51 static IO_t rtc6705LePin = IO_NONE;
52 static IO_t rtc6705ClkPin = IO_NONE;
54 void rtc6705_soft_spi_init(void)
56 rtc6705DataPin = IOGetByTag(IO_TAG(RTC6705_SPIDATA_PIN));
57 rtc6705LePin = IOGetByTag(IO_TAG(RTC6705_SPILE_PIN));
58 rtc6705ClkPin = IOGetByTag(IO_TAG(RTC6705_SPICLK_PIN));
60 IOInit(rtc6705DataPin, OWNER_SPI_MOSI, RESOURCE_SOFT_OFFSET);
61 IOConfigGPIO(rtc6705DataPin, IOCFG_OUT_PP);
63 IOInit(rtc6705LePin, OWNER_SPI_CS, RESOURCE_SOFT_OFFSET);
64 IOConfigGPIO(rtc6705LePin, IOCFG_OUT_PP);
66 IOInit(rtc6705ClkPin, OWNER_SPI_SCK, RESOURCE_SOFT_OFFSET);
67 IOConfigGPIO(rtc6705ClkPin, IOCFG_OUT_PP);
70 static void rtc6705_write_register(uint8_t addr, uint32_t data)
72 uint8_t i;
74 RTC6705_SPILE_OFF;
75 delay(1);
76 // send address
77 for (i=0; i<4; i++) {
78 if ((addr >> i) & 1)
79 RTC6705_SPIDATA_ON;
80 else
81 RTC6705_SPIDATA_OFF;
83 RTC6705_SPICLK_ON;
84 delay(1);
85 RTC6705_SPICLK_OFF;
86 delay(1);
88 // Write bit
90 RTC6705_SPIDATA_ON;
91 RTC6705_SPICLK_ON;
92 delay(1);
93 RTC6705_SPICLK_OFF;
94 delay(1);
95 for (i=0; i<20; i++) {
96 if ((data >> i) & 1)
97 RTC6705_SPIDATA_ON;
98 else
99 RTC6705_SPIDATA_OFF;
100 RTC6705_SPICLK_ON;
101 delay(1);
102 RTC6705_SPICLK_OFF;
103 delay(1);
105 RTC6705_SPILE_ON;
108 void rtc6705_soft_spi_set_freq(uint16_t channel_freq)
111 uint32_t freq = (uint32_t)channel_freq * 1000;
112 uint32_t N, A;
114 freq /= 40;
115 N = freq / 64;
116 A = freq % 64;
117 rtc6705_write_register(0, 400);
118 rtc6705_write_register(1, (N << 7) | A);
121 void rtc6705_soft_spi_set_band_and_channel(const uint8_t band, const uint8_t channel)
123 // band and channel are 1-based, not 0-based
125 // example for raceband/ch8:
126 // (5 - 1) * 8 + (8 - 1)
127 // 4 * 8 + 7
128 // 32 + 7 = 39
129 uint8_t freqIndex = ((band - 1) * CHANNELS_PER_BAND) + (channel - 1);
131 uint16_t freq = vtx_freq[freqIndex];
132 rtc6705_soft_spi_set_freq(freq);
135 void rtc6705_soft_spi_set_rf_power(uint8_t reduce_power)
137 rtc6705_write_register(7, (reduce_power ? (PA_CONTROL_DEFAULT | PD_Q5G_MASK) & (~(PA5G_PW_MASK | PA5G_BS_MASK)) : PA_CONTROL_DEFAULT));
140 #endif