Companion: Russian UI (#7180)
[opentx.git] / radio / src / pulses / ppm.cpp
blob1c068f7e60b140ee262879b9729795434e151a22
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "opentx.h"
23 template<class T>
24 void setupPulsesPPM(PpmPulsesData<T> * ppmPulsesData, uint8_t channelsStart, int8_t channelsCount, int8_t frameLength)
26 int16_t PPM_range = g_model.extendedLimits ? (512*LIMIT_EXT_PERCENT/100) * 2 : 512 * 2; // range of 0.7 .. 1.7msec
28 // Total frame length = 22.5msec
29 // each pulse is 0.7..1.7ms long with a 0.3ms stop tail
30 // The pulse ISR is 2mhz that's why everything is multiplied by 2
32 uint8_t firstCh = channelsStart;
33 uint8_t lastCh = min<uint8_t>(MAX_OUTPUT_CHANNELS, firstCh + 8 + channelsCount);
35 #if defined(STM32)
36 ppmPulsesData->ptr = ppmPulsesData->pulses;
37 #else
38 uint16_t * ptr = ppmPulsesData->pulses;
39 ppmPulsesData->ptr = ptr;
40 #endif
42 int32_t rest = 22500u * 2;
43 rest += int32_t(frameLength) * 1000;
44 for (uint32_t i=firstCh; i<lastCh; i++) {
45 int16_t v = limit((int16_t)-PPM_range, channelOutputs[i], (int16_t)PPM_range) + 2*PPM_CH_CENTER(i);
46 rest -= v;
47 #if defined(STM32)
48 *ppmPulsesData->ptr++ = v; /* as Pat MacKenzie suggests */
49 #else
50 *ptr++ = v; /* as Pat MacKenzie suggests */
51 #endif
53 rest = limit<int32_t>(9000, rest, 65535); /* avoids that CCR2 is bigger than ARR which would cause reboot */
54 #if defined(STM32)
55 *ppmPulsesData->ptr++ = rest;
56 *ppmPulsesData->ptr = 0; // it's needed in case PPM is sent without DMA (we stop when we reach this 0)
57 #else
58 *ptr = rest;
59 *(ptr + 1) = 0;
60 #endif
63 void setupPulsesPPMTrainer()
65 setupPulsesPPM<trainer_pulse_duration_t>(&trainerPulsesData.ppm, g_model.trainerData.channelsStart, g_model.trainerData.channelsCount, g_model.trainerData.frameLength);
68 #if defined(PCBTARANIS) && defined(INTERNAL_MODULE_PPM)
69 void setupPulsesPPMInternalModule()
71 setupPulsesPPM(&intmodulePulsesData.ppm, g_model.moduleData[INTERNAL_MODULE].channelsStart, g_model.moduleData[INTERNAL_MODULE].channelsCount, g_model.moduleData[INTERNAL_MODULE].ppm.frameLength);
73 #endif
75 void setupPulsesPPMExternalModule()
77 setupPulsesPPM(&extmodulePulsesData.ppm, g_model.moduleData[EXTERNAL_MODULE].channelsStart, g_model.moduleData[EXTERNAL_MODULE].channelsCount, g_model.moduleData[EXTERNAL_MODULE].ppm.frameLength);