CF/BF - Update RTC6705 via SOFT SPI to use VTX API.
[betaflight.git] / src / main / drivers / vtx_rtc6705_soft_spi.c
blob5c027735ab4eb92b1dd681c95d352f9addad015e
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 #if defined(VTX_RTC6705) && defined(VTX_RTC6705SOFTSPI)
25 #include "bus_spi.h"
26 #include "io.h"
27 #include "system.h"
28 #include "light_led.h"
30 #include "vtx_rtc6705.h"
32 #define DP_5G_MASK 0x7000
33 #define PA5G_BS_MASK 0x0E00
34 #define PA5G_PW_MASK 0x0180
35 #define PD_Q5G_MASK 0x0040
36 #define QI_5G_MASK 0x0038
37 #define PA_BS_MASK 0x0007
39 #define PA_CONTROL_DEFAULT 0x4FBD
41 #define RTC6705_SPICLK_ON IOHi(rtc6705ClkPin)
42 #define RTC6705_SPICLK_OFF IOLo(rtc6705ClkPin)
44 #define RTC6705_SPIDATA_ON IOHi(rtc6705DataPin)
45 #define RTC6705_SPIDATA_OFF IOLo(rtc6705DataPin)
47 #define RTC6705_SPILE_ON IOHi(rtc6705LePin)
48 #define RTC6705_SPILE_OFF IOLo(rtc6705LePin)
50 const uint16_t vtx_freq[] =
52 5865, 5845, 5825, 5805, 5785, 5765, 5745, 5725, // Boacam A
53 5733, 5752, 5771, 5790, 5809, 5828, 5847, 5866, // Boscam B
54 5705, 5685, 5665, 5645, 5885, 5905, 5925, 5945, // Boscam E
55 5740, 5760, 5780, 5800, 5820, 5840, 5860, 5880, // FatShark
56 5658, 5695, 5732, 5769, 5806, 5843, 5880, 5917, // RaceBand
59 static IO_t rtc6705DataPin = IO_NONE;
60 static IO_t rtc6705LePin = IO_NONE;
61 static IO_t rtc6705ClkPin = IO_NONE;
63 void rtc6705IOInit(void)
65 rtc6705DataPin = IOGetByTag(IO_TAG(RTC6705_SPIDATA_PIN));
66 rtc6705LePin = IOGetByTag(IO_TAG(RTC6705_SPILE_PIN));
67 rtc6705ClkPin = IOGetByTag(IO_TAG(RTC6705_SPICLK_PIN));
69 IOInit(rtc6705DataPin, OWNER_SPI_MOSI, RESOURCE_SOFT_OFFSET);
70 IOConfigGPIO(rtc6705DataPin, IOCFG_OUT_PP);
72 IOInit(rtc6705LePin, OWNER_SPI_CS, RESOURCE_SOFT_OFFSET);
73 IOConfigGPIO(rtc6705LePin, IOCFG_OUT_PP);
75 IOInit(rtc6705ClkPin, OWNER_SPI_SCK, RESOURCE_SOFT_OFFSET);
76 IOConfigGPIO(rtc6705ClkPin, IOCFG_OUT_PP);
79 static void rtc6705_write_register(uint8_t addr, uint32_t data)
81 uint8_t i;
83 RTC6705_SPILE_OFF;
84 delay(1);
85 // send address
86 for (i=0; i<4; i++) {
87 if ((addr >> i) & 1)
88 RTC6705_SPIDATA_ON;
89 else
90 RTC6705_SPIDATA_OFF;
92 RTC6705_SPICLK_ON;
93 delay(1);
94 RTC6705_SPICLK_OFF;
95 delay(1);
97 // Write bit
99 RTC6705_SPIDATA_ON;
100 RTC6705_SPICLK_ON;
101 delay(1);
102 RTC6705_SPICLK_OFF;
103 delay(1);
104 for (i=0; i<20; i++) {
105 if ((data >> i) & 1)
106 RTC6705_SPIDATA_ON;
107 else
108 RTC6705_SPIDATA_OFF;
109 RTC6705_SPICLK_ON;
110 delay(1);
111 RTC6705_SPICLK_OFF;
112 delay(1);
114 RTC6705_SPILE_ON;
117 void rtc6705SetFreq(uint16_t channel_freq)
119 uint32_t freq = (uint32_t)channel_freq * 1000;
120 uint32_t N, A;
122 freq /= 40;
123 N = freq / 64;
124 A = freq % 64;
125 rtc6705_write_register(0, 400);
126 rtc6705_write_register(1, (N << 7) | A);
129 void rtc6705SetChannel(const uint8_t band, const uint8_t channel)
131 // band and channel are 1-based, not 0-based
133 // example for raceband/ch8:
134 // (5 - 1) * 8 + (8 - 1)
135 // 4 * 8 + 7
136 // 32 + 7 = 39
137 uint8_t freqIndex = ((band - 1) * RTC6705_BAND_COUNT) + (channel - 1);
139 uint16_t freq = vtx_freq[freqIndex];
140 rtc6705SetFreq(freq);
143 void rtc6705SetRFPower(const uint8_t rf_power)
145 rtc6705_write_register(7, (rf_power ? PA_CONTROL_DEFAULT : (PA_CONTROL_DEFAULT | PD_Q5G_MASK) & (~(PA5G_PW_MASK | PA5G_BS_MASK))));
148 void rtc6705Disable(void)
152 void rtc6705Enable(void)
156 #endif