Merge pull request #11494 from haslinghuis/dshot_gpio
[betaflight.git] / src / main / drivers / transponder_ir_erlt.c
blob7c562d96c4c79ba3245e18d1cf8043bdf3e1b006
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 <stdint.h>
23 #include <string.h>
24 #include "platform.h"
26 #ifdef USE_TRANSPONDER
28 #include "drivers/transponder_ir.h"
29 #include "drivers/transponder_ir_erlt.h"
31 #if defined(STM32F3) || defined(STM32F4) || defined(STM32F7) || defined(STM32H7) || defined(STM32G4) || defined(UNIT_TEST)
33 static uint16_t dmaBufferOffset;
34 extern const struct transponderVTable erltTansponderVTable;
36 void transponderIrInitERLT(transponder_t *transponder){
37 transponder->dma_buffer_size = TRANSPONDER_DMA_BUFFER_SIZE_ERLT;
38 transponder->vTable = &erltTansponderVTable;
39 transponder->timer_hz = TRANSPONDER_TIMER_MHZ_ERLT;
40 transponder->timer_carrier_hz = TRANSPONDER_CARRIER_HZ_ERLT;
41 memset(&(transponder->transponderIrDMABuffer.erlt), 0, sizeof(transponder->transponderIrDMABuffer.erlt));
44 void addBitToBuffer(transponder_t *transponder, uint8_t cycles, uint8_t pulsewidth)
46 for (int i = 0; i < cycles; i++) {
47 transponder->transponderIrDMABuffer.erlt[dmaBufferOffset++] = pulsewidth;
51 void updateTransponderDMABufferERLT(transponder_t *transponder, const uint8_t* transponderData)
53 uint8_t byteToSend = ~(*transponderData); //transponderData is stored inverted, so invert before using
54 uint8_t paritysum = 0; //sum of one bits
56 dmaBufferOffset = 0; //reset buffer count
58 //start bit 1, always pulsed, bit value = 0
59 addBitToBuffer(transponder, ERLTCyclesForZeroBit, transponder->bitToggleOne);
61 //start bit 2, always not pulsed, bit value = 0
62 addBitToBuffer(transponder, ERLTCyclesForZeroBit, ERLTBitQuiet);
64 //add data bits, only the 6 LSB
65 for (int i = 5; i >= 0; i--)
67 uint8_t bv = (byteToSend >> i) & 0x01;
68 paritysum += bv;
69 addBitToBuffer(transponder, (bv ? ERLTCyclesForOneBit : ERLTCyclesForZeroBit), ((i % 2) ? transponder->bitToggleOne : ERLTBitQuiet));
72 //parity bit, always pulsed, bit value is zero if sum is even, one if odd
73 addBitToBuffer(transponder, ((paritysum % 2) ? ERLTCyclesForOneBit : ERLTCyclesForZeroBit), transponder->bitToggleOne);
75 //add final zero after the pulsed parity bit to stop pulses until the next update
76 transponder->transponderIrDMABuffer.erlt[dmaBufferOffset++] = ERLTBitQuiet;
78 //reset buffer size to that required by this ERLT id
79 transponder->dma_buffer_size = dmaBufferOffset;
82 const struct transponderVTable erltTansponderVTable = {
83 updateTransponderDMABufferERLT,
86 #endif
87 #endif