Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / src / main / io / transponder_ir.c
blobadb723fa2aa2bd6d0e93ef6710b123f2721fef70
1 /*
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)
8 * any later version.
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 #include <stdbool.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <stdarg.h>
27 #include "platform.h"
29 #ifdef USE_TRANSPONDER
30 #include "build/build_config.h"
32 #include "config/config_reset.h"
33 #include "pg/pg.h"
34 #include "pg/pg_ids.h"
36 #include "drivers/timer.h"
37 #include "drivers/transponder_ir.h"
38 #include "drivers/system.h"
39 #include "drivers/usb_io.h"
41 #include "config/config.h"
43 #include "io/transponder_ir.h"
45 PG_REGISTER_WITH_RESET_FN(transponderConfig_t, transponderConfig, PG_TRANSPONDER_CONFIG, 0);
47 void pgResetFn_transponderConfig(transponderConfig_t *transponderConfig)
49 RESET_CONFIG_2(transponderConfig_t, transponderConfig,
50 .provider = TRANSPONDER_ILAP,
51 .reserved = 0,
52 .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
53 .ioTag = IO_TAG_NONE
55 transponderConfig->ioTag = timerioTagGetByUsage(TIM_USE_TRANSPONDER, 0);
58 static bool transponderInitialised = false;
59 static bool transponderRepeat = false;
61 // timers
62 static timeUs_t nextUpdateAtUs = 0;
64 #define JITTER_DURATION_COUNT (sizeof(jitterDurations) / sizeof(uint8_t))
65 static uint8_t jitterDurations[] = {0,9,4,8,3,9,6,7,1,6,9,7,8,2,6};
67 const transponderRequirement_t transponderRequirements[TRANSPONDER_PROVIDER_COUNT] = {
68 {TRANSPONDER_ILAP, TRANSPONDER_DATA_LENGTH_ILAP, TRANSPONDER_TRANSMIT_DELAY_ILAP, TRANSPONDER_TRANSMIT_JITTER_ILAP},
69 {TRANSPONDER_ARCITIMER, TRANSPONDER_DATA_LENGTH_ARCITIMER, TRANSPONDER_TRANSMIT_DELAY_ARCITIMER, TRANSPONDER_TRANSMIT_JITTER_ARCITIMER},
70 {TRANSPONDER_ERLT, TRANSPONDER_DATA_LENGTH_ERLT, TRANSPONDER_TRANSMIT_DELAY_ERLT, TRANSPONDER_TRANSMIT_JITTER_ERLT}
73 void transponderUpdate(timeUs_t currentTimeUs)
75 static uint32_t jitterIndex = 0;
77 if (!(transponderInitialised && transponderRepeat && isTransponderIrReady())) {
78 return;
81 const bool updateNow = (timeDelta_t)(currentTimeUs - nextUpdateAtUs) >= 0L;
82 if (!updateNow) {
83 return;
86 uint8_t provider = transponderConfig()->provider;
88 // TODO use a random number generator for random jitter? The idea here is to avoid multiple transmitters transmitting at the same time.
89 uint32_t jitter = (transponderRequirements[provider - 1].transmitJitter / 10 * jitterDurations[jitterIndex++]);
90 if (jitterIndex >= JITTER_DURATION_COUNT) {
91 jitterIndex = 0;
94 nextUpdateAtUs = currentTimeUs + transponderRequirements[provider - 1].transmitDelay + jitter;
96 #ifdef REDUCE_TRANSPONDER_CURRENT_DRAW_WHEN_USB_CABLE_PRESENT
97 // reduce current draw when USB cable is plugged in by decreasing the transponder transmit rate.
98 if (usbCableIsInserted()) {
99 nextUpdateAtUs = currentTimeUs + (1000 * 1000) / 10; // 10 hz.
101 #endif
103 transponderIrTransmit();
106 void transponderInit(void)
108 transponderInitialised = transponderIrInit(transponderConfig()->ioTag, transponderConfig()->provider);
109 if (!transponderInitialised) {
110 return;
113 transponderIrUpdateData(transponderConfig()->data);
116 void transponderStopRepeating(void)
118 transponderRepeat = false;
121 void transponderStartRepeating(void)
123 if (!transponderInitialised) {
124 return;
127 transponderRepeat = true;
130 void transponderUpdateData(void)
132 if (!transponderInitialised) {
133 return;
136 transponderIrUpdateData(transponderConfig()->data);
139 void transponderTransmitOnce(void) {
141 if (!transponderInitialised) {
142 return;
144 transponderIrTransmit();
146 #endif