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.
24 void setupPulsesPPM(uint8_t port
, PpmPulsesData
<T
> * ppmPulsesData
)
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 uint32_t firstCh
= g_model
.moduleData
[port
].channelsStart
;
33 uint32_t lastCh
= min
<unsigned int>(MAX_OUTPUT_CHANNELS
, firstCh
+ 8 + g_model
.moduleData
[port
].channelsCount
);
36 ppmPulsesData
->ptr
= ppmPulsesData
->pulses
;
38 uint16_t * ptr
= ppmPulsesData
->pulses
;
39 ppmPulsesData
->ptr
= ptr
;
42 int32_t rest
= 22500u * 2;
43 rest
+= (int32_t(g_model
.moduleData
[port
].ppm
.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
);
48 *ppmPulsesData
->ptr
++ = v
; /* as Pat MacKenzie suggests */
50 *ptr
++ = v
; /* as Pat MacKenzie suggests */
53 rest
= limit
<int32_t>(9000, rest
, 65535); /* avoids that CCR2 is bigger than ARR which would cause reboot */
55 *ppmPulsesData
->ptr
++ = rest
;
62 void setupPulsesPPMModule(uint8_t port
)
64 setupPulsesPPM
<pulse_duration_t
>(port
, &modulePulsesData
[port
].ppm
);
67 void setupPulsesPPMTrainer()
69 setupPulsesPPM
<trainer_pulse_duration_t
>(TRAINER_MODULE
, &trainerPulsesData
.ppm
);