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_PXX2_H_
22 #define _PULSES_PXX2_H_
25 #include "io/frsky_pxx2.h"
28 #define PXX2_TYPE_C_MODULE 0x01
29 #define PXX2_TYPE_ID_REGISTER 0x01
30 #define PXX2_TYPE_ID_BIND 0x02
31 #define PXX2_TYPE_ID_CHANNELS 0x03
32 #define PXX2_TYPE_ID_TX_SETTINGS 0x04
33 #define PXX2_TYPE_ID_RX_SETTINGS 0x05
34 #define PXX2_TYPE_ID_HW_INFO 0x06
35 #define PXX2_TYPE_ID_SHARE 0x07
36 #define PXX2_TYPE_ID_RESET 0x08
37 #define PXX2_TYPE_ID_AUTHENTICATION 0x09
38 #define PXX2_TYPE_ID_TELEMETRY 0xFE
40 #define PXX2_TYPE_C_POWER_METER 0x02
41 #define PXX2_TYPE_ID_POWER_METER 0x01
42 #define PXX2_TYPE_ID_SPECTRUM 0x02
44 #define PXX2_TYPE_C_OTA 0xFE
45 #define PXX2_TYPE_ID_OTA 0x02
47 #define PXX2_CHANNELS_FLAG0_FAILSAFE (1 << 6)
48 #define PXX2_CHANNELS_FLAG0_RANGECHECK (1 << 7)
50 #define PXX2_RX_SETTINGS_FLAG0_WRITE (1 << 6)
52 #define PXX2_RX_SETTINGS_FLAG1_TELEMETRY_DISABLED (1 << 7)
53 #define PXX2_RX_SETTINGS_FLAG1_READONLY (1 << 6)
54 #define PXX2_RX_SETTINGS_FLAG1_FASTPWM (1 << 4)
55 #define PXX2_RX_SETTINGS_FLAG1_FPORT (1 << 3)
57 #define PXX2_TX_SETTINGS_FLAG0_WRITE (1 << 6)
58 #define PXX2_TX_SETTINGS_FLAG1_EXTERNAL_ANTENNA (1 << 3)
60 #define PXX2_HW_INFO_TX_ID 0xFF
63 #define SPECTRUM_ANALYSER_POWER_FLOOR -120 /*dBm*/
65 #define SPECTRUM_ANALYSER_POWER_FLOOR -120 /*dBm*/
68 #define PXX2_AUTH_REFUSED_FLAG 0xA5
70 enum PXX2ModuleModelID
{
78 PXX2_MODULE_R9M_LITE_PRO
,
80 PXX2_MODULE_ISRM_S_X9
,
81 PXX2_MODULE_ISRM_S_X10E
,
83 PXX2_MODULE_ISRM_S_X10S
,
84 PXX2_MODULE_ISRM_S_X9LITE
,
87 static const char * const PXX2ModulesNames
[] = {
104 inline const char * getPXX2ModuleName(uint8_t modelId
)
106 if (modelId
< DIM(PXX2ModulesNames
))
107 return PXX2ModulesNames
[modelId
];
109 return PXX2ModulesNames
[0];
113 MODULE_OPTION_EXTERNAL_ANTENNA
,
115 MODULE_OPTION_SPECTRUM_ANALYSER
,
116 MODULE_OPTION_POWER_METER
,
119 /* Module options order:
120 * - External antenna (0x01)
122 * - Spektrum analyser (0x04)
123 * - Power meter (0x08)
125 static const uint8_t PXX2ModuleOptions
[] = {
127 0b11111111, // None = display all options on SIMU
129 0b00000000, // None = no option available on unknown modules
133 0b00001101, // ISRM-PRO
134 0b00000101, // ISRM-S
136 0b00000010, // R9MLite
137 0b00000110, // R9MLite-PRO
138 0b00000100, // ISRM-N
139 0b00000100, // ISRM-S-X9
140 0b00000101, // ISRM-S-X10E
141 0b00000001, // XJT_LITE
142 0b00000101, // ISRM-S-X10S
143 0b00000101, // ISRM-S-X9LITE
146 inline uint8_t getPXX2ModuleOptions(uint8_t modelId
)
148 if (modelId
< DIM(PXX2ModuleOptions
))
149 return PXX2ModuleOptions
[modelId
];
151 return PXX2ModuleOptions
[0];
154 inline bool isPXX2ModuleOptionAvailable(uint8_t modelId
, uint8_t option
)
156 return getPXX2ModuleOptions(modelId
) & (1 << option
);
159 enum ModuleCapabilities
{
160 MODULE_CAPABILITY_COUNT
163 static const char * const PXX2ReceiversNames
[] = {
189 "R9-MINI-OTA", // this one has OTA (different bootloader)
190 "R9-MM-OTA", // this one has OTA (different bootloader)
191 "R9-SLIM+-OTA", // this one has OTA (different bootloader)
194 inline const char * getPXX2ReceiverName(uint8_t modelId
)
196 if (modelId
< DIM(PXX2ReceiversNames
))
197 return PXX2ReceiversNames
[modelId
];
199 return PXX2ReceiversNames
[0];
206 /* Receiver options order:
209 static const uint8_t PXX2ReceiverOptions
[] = {
211 0b11111111, // None = display all options on SIMU
213 0b00000000, // None = display all options on SIMU
217 0b11111110, // RX8R-PRO
224 0b11111110, // X4R-SB
234 0b11111110, // R9-SLIM
235 0b11111110, // R9-SLIM+
236 0b11111110, // R9-MINI
238 0b11111110, // R9-STAB
239 0b11111111, // R9-MINI+OTA
240 0b11111111, // R9-MM+OTA
241 0b11111111, // R9-SLIM+OTA
244 inline uint8_t getPXX2ReceiverOptions(uint8_t modelId
)
246 if (modelId
< DIM(PXX2ReceiverOptions
))
247 return PXX2ReceiverOptions
[modelId
];
249 return PXX2ReceiverOptions
[0];
252 inline bool isPXX2ReceiverOptionAvailable(uint8_t modelId
, uint8_t option
)
254 return getPXX2ReceiverOptions(modelId
) & (1 << option
);
257 enum ReceiverCapabilities
{
258 RECEIVER_CAPABILITY_FPORT
,
259 RECEIVER_CAPABILITY_COUNT
269 enum PXX2RegisterSteps
{
271 REGISTER_RX_NAME_RECEIVED
,
272 REGISTER_RX_NAME_SELECTED
,
277 BIND_MODULE_TX_INFORMATION_REQUEST
= -2,
278 BIND_MODULE_TX_SETTINGS_REQUEST
= -1,
280 BIND_RX_NAME_SELECTED
,
287 enum PXX2OtaUpdateSteps
{
288 OTA_UPDATE_START
= BIND_OK
+ 1,
289 OTA_UPDATE_START_ACK
,
291 OTA_UPDATE_TRANSFER_ACK
,
296 enum PXX2ReceiverStatus
{
303 extern ModuleFifo intmoduleFifo
;
304 extern ModuleFifo extmoduleFifo
;
313 void addToCrc(uint8_t byte
)
321 class Pxx2Transport
: public DataBuffer
<uint8_t, 64>, public Pxx2CrcMixin
{
323 void addWord(uint32_t word
)
331 void addByte(uint8_t byte
)
333 Pxx2CrcMixin::addToCrc(byte
);
334 addByteWithoutCrc(byte
);
337 void addByteWithoutCrc(uint8_t byte
)
343 class Pxx2Pulses
: public Pxx2Transport
{
344 friend class Pxx2OtaUpdate
;
347 bool setupFrame(uint8_t module
);
348 void setupAuthenticationFrame(uint8_t module
, uint8_t mode
, const uint8_t * outputMessage
);
351 void setupHardwareInfoFrame(uint8_t module
);
353 void setupRegisterFrame(uint8_t module
);
355 void setupAccstBindFrame(uint8_t module
);
357 void setupAccessBindFrame(uint8_t module
);
359 void setupResetFrame(uint8_t module
);
361 void setupShareMode(uint8_t module
);
363 void setupModuleSettingsFrame(uint8_t module
);
365 void setupReceiverSettingsFrame(uint8_t module
);
367 void setupChannelsFrame(uint8_t module
);
369 void setupTelemetryFrame(uint8_t module
);
371 void setupSpectrumAnalyser(uint8_t module
);
373 void setupPowerMeter(uint8_t module
);
375 void sendOtaUpdate(uint8_t module
, const char * rxName
, uint32_t address
, const char * data
);
379 // send 7E, do not CRC
380 Pxx2Transport::addByteWithoutCrc(0x7E);
382 // reserve 1 byte for LEN
383 Pxx2Transport::addByteWithoutCrc(0x00);
386 void addFrameType(uint8_t type_c
, uint8_t type_id
)
389 Pxx2Transport::addByte(type_c
);
390 Pxx2Transport::addByte(type_id
);
393 uint8_t addFlag0(uint8_t module
);
395 void addFlag1(uint8_t module
);
397 void addPulsesValues(uint16_t low
, uint16_t high
);
399 void addChannels(uint8_t module
);
401 void addFailsafe(uint8_t module
);
405 Pxx2Transport::addByteWithoutCrc(Pxx2CrcMixin::crc
>> 8);
406 Pxx2Transport::addByteWithoutCrc(Pxx2CrcMixin::crc
);
411 // init the CRC counter
414 // reset the frame pointer
415 Pxx2Transport::initBuffer();
417 // add the frame head
423 uint8_t size
= getSize() - 2;
426 // update the frame LEN = frame length minus the 2 first bytes
427 data
[1] = getSize() - 2;
433 Pxx2Transport::initBuffer();
438 class Pxx2OtaUpdate
{
440 Pxx2OtaUpdate(uint8_t module
, const char * rxName
):
446 void flashFirmware(const char * filename
);
452 const char * doFlashFirmware(const char * filename
);
453 bool waitStep(uint8_t step
, uint8_t timeout
);
454 const char * nextStep(uint8_t step
, const char * rxName
, uint32_t address
, const uint8_t * buffer
);