Fix unused param, add PERIPH_DRIVER flag for F4, tidied up F1 and F3 in prep.
[betaflight.git] / src / main / io / transponder_ir.c
blob0559dbf75dd328518209989173b980fba83d131e
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 <stdlib.h>
20 #include <stdint.h>
21 #include <string.h>
22 #include <stdarg.h>
24 #include <platform.h>
26 #ifdef TRANSPONDER
27 #include "build/build_config.h"
29 #include "config/config_reset.h"
30 #include "config/parameter_group.h"
31 #include "config/parameter_group_ids.h"
33 #include "drivers/timer.h"
34 #include "drivers/transponder_ir.h"
35 #include "drivers/system.h"
36 #include "drivers/usb_io.h"
38 #include "fc/config.h"
40 #include "io/transponder_ir.h"
42 PG_REGISTER_WITH_RESET_FN(transponderConfig_t, transponderConfig, PG_TRANSPONDER_CONFIG, 0);
44 void pgResetFn_transponderConfig(transponderConfig_t *transponderConfig)
46 RESET_CONFIG_2(transponderConfig_t, transponderConfig,
47 .provider = TRANSPONDER_ILAP,
48 .reserved = 0,
49 .data = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0x0, 0x0, 0x0 }, // Note, this is NOT a valid transponder code, it's just for testing production hardware
50 .ioTag = IO_TAG_NONE
53 for (int i = 0; i < USABLE_TIMER_CHANNEL_COUNT; i++) {
54 if (timerHardware[i].usageFlags & TIM_USE_TRANSPONDER) {
55 transponderConfig->ioTag = timerHardware[i].tag;
56 break;
61 static bool transponderInitialised = false;
62 static bool transponderRepeat = false;
64 // timers
65 static timeUs_t nextUpdateAtUs = 0;
67 #define JITTER_DURATION_COUNT (sizeof(jitterDurations) / sizeof(uint8_t))
68 static uint8_t jitterDurations[] = {0,9,4,8,3,9,6,7,1,6,9,7,8,2,6};
70 const transponderRequirement_t transponderRequirements[TRANSPONDER_PROVIDER_COUNT] = {
71 {TRANSPONDER_ILAP, TRANSPONDER_DATA_LENGTH_ILAP, TRANSPONDER_TRANSMIT_DELAY_ILAP, TRANSPONDER_TRANSMIT_JITTER_ILAP},
72 {TRANSPONDER_ARCITIMER, TRANSPONDER_DATA_LENGTH_ARCITIMER, TRANSPONDER_TRANSMIT_DELAY_ARCITIMER, TRANSPONDER_TRANSMIT_JITTER_ARCITIMER},
73 {TRANSPONDER_ERLT, TRANSPONDER_DATA_LENGTH_ERLT, TRANSPONDER_TRANSMIT_DELAY_ERLT, TRANSPONDER_TRANSMIT_JITTER_ERLT}
76 void transponderUpdate(timeUs_t currentTimeUs)
78 static uint32_t jitterIndex = 0;
80 if (!(transponderInitialised && transponderRepeat && isTransponderIrReady())) {
81 return;
84 const bool updateNow = (timeDelta_t)(currentTimeUs - nextUpdateAtUs) >= 0L;
85 if (!updateNow) {
86 return;
89 uint8_t provider = transponderConfig()->provider;
91 // TODO use a random number generator for random jitter? The idea here is to avoid multiple transmitters transmitting at the same time.
92 uint32_t jitter = (transponderRequirements[provider - 1].transmitJitter / 10 * jitterDurations[jitterIndex++]);
93 if (jitterIndex >= JITTER_DURATION_COUNT) {
94 jitterIndex = 0;
97 nextUpdateAtUs = currentTimeUs + transponderRequirements[provider - 1].transmitDelay + jitter;
99 #ifdef REDUCE_TRANSPONDER_CURRENT_DRAW_WHEN_USB_CABLE_PRESENT
100 // reduce current draw when USB cable is plugged in by decreasing the transponder transmit rate.
101 if (usbCableIsInserted()) {
102 nextUpdateAtUs = currentTimeUs + (1000 * 1000) / 10; // 10 hz.
104 #endif
106 transponderIrTransmit();
109 void transponderInit(void)
111 transponderInitialised = transponderIrInit(transponderConfig()->ioTag, transponderConfig()->provider);
112 if (!transponderInitialised) {
113 return;
116 transponderIrUpdateData(transponderConfig()->data);
119 void transponderStopRepeating(void)
121 transponderRepeat = false;
124 void transponderStartRepeating(void)
126 if (!transponderInitialised) {
127 return;
130 transponderRepeat = true;
133 void transponderUpdateData(void)
135 if (!transponderInitialised) {
136 return;
139 transponderIrUpdateData(transponderConfig()->data);
142 void transponderTransmitOnce(void) {
144 if (!transponderInitialised) {
145 return;
147 transponderIrTransmit();
149 #endif