Cosmetics
[opentx.git] / radio / src / pulses / pxx1.h
blobd2f581580f8d14bb02b62ead530ee58be7e50c72
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 #ifndef _PULSES_PXX1_H_
22 #define _PULSES_PXX1_H_
24 #include "pxx.h"
25 #include "crc.h"
27 struct HeartbeatCapture {
28 uint32_t timestamp;
29 uint32_t count;
30 uint8_t valid;
33 extern volatile HeartbeatCapture heartbeatCapture;
35 class Pxx1CrcMixin {
36 protected:
37 void initCrc()
39 crc = 0;
42 void addToCrc(uint8_t byte)
44 crc = (crc << 8) ^ (crc16tab_1189[((crc >> 8) ^ byte) & 0xFF]);
47 uint16_t crc;
48 static const uint16_t CRCTable[];
51 template <class BitTransport>
52 class StandardPxx1Transport: public BitTransport, public Pxx1CrcMixin {
53 protected:
54 uint8_t ones_count;
56 void initFrame(uint32_t period)
58 BitTransport::initFrame(period);
59 ones_count = 0;
62 void addByte(uint8_t byte)
64 Pxx1CrcMixin::addToCrc(byte);
65 addByteWithoutCrc(byte);
68 void addRawByte(uint8_t byte)
70 for (uint8_t i = 0; i < 8; i++) {
71 if (byte & 0x80)
72 BitTransport::addPart(1);
73 else
74 BitTransport::addPart(0);
75 byte <<= 1;
79 void addByteWithoutCrc(uint8_t byte)
81 for (uint8_t i = 0; i < 8; i++) {
82 addBit(byte & 0x80);
83 byte <<= 1;
87 void addBit(uint8_t bit)
89 if (bit) {
90 BitTransport::addPart(1);
91 if (++ones_count == 5) {
92 ones_count = 0;
93 BitTransport::addPart(0); // Stuff a 0 bit in
96 else {
97 BitTransport::addPart(0);
98 ones_count = 0;
103 class UartPxx1Transport: public DataBuffer<uint8_t, 64>, public Pxx1CrcMixin {
104 protected:
105 void initFrame(uint32_t period)
107 initBuffer();
110 void addByte(uint8_t byte)
112 Pxx1CrcMixin::addToCrc(byte);
113 addWithByteStuffing(byte);
116 void addRawByte(uint8_t byte)
118 *ptr++ = byte;
121 void addByteWithoutCrc(uint8_t byte)
123 addWithByteStuffing(byte);
126 void addWithByteStuffing(uint8_t byte)
128 if (0x7E == byte) {
129 addRawByte(0x7D);
130 addRawByte(0x5E);
132 else if (0x7D == byte) {
133 addRawByte(0x7D);
134 addRawByte(0x5D);
136 else {
137 addRawByte(byte);
141 void addTail()
143 // nothing
147 template <class PxxTransport>
148 class Pxx1Pulses: public PxxTransport
150 public:
151 void setupFrame(uint8_t port);
153 protected:
154 void addHead()
156 // send 7E, do not CRC
157 PxxTransport::addRawByte(0x7E);
160 void addCrc()
162 PxxTransport::addByteWithoutCrc(Pxx1CrcMixin::crc >> 8);
163 PxxTransport::addByteWithoutCrc(Pxx1CrcMixin::crc);
166 void addFlag1(uint8_t port, uint8_t sendFailsafe);
167 void addExtraFlags(uint8_t port);
168 void addChannels(uint8_t port, uint8_t sendFailsafe, uint8_t sendUpperChannels);
169 void add8ChannelsFrame(uint8_t port, uint8_t sendUpperChannels, uint8_t sendFailsafe);
172 typedef Pxx1Pulses<UartPxx1Transport> UartPxx1Pulses;
173 typedef Pxx1Pulses<StandardPxx1Transport<PwmPxxBitTransport>> PwmPxx1Pulses;
174 typedef Pxx1Pulses<StandardPxx1Transport<SerialPxxBitTransport>> SerialPxx1Pulses;
176 #endif