Multimodule merge
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Copyright (C) OpenTX
|
||||
*
|
||||
* Based on code named
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "opentx.h"
|
||||
|
||||
// for the MULTI protocol definition
|
||||
// see https://github.com/pascallanger/DIY-Multiprotocol-TX-Module
|
||||
// file Multiprotocol/multiprotocol.h
|
||||
|
||||
|
||||
|
||||
/* The protocol is heavily inspired by the DSM2 protocol, so reuse some the definitions where they are identical */
|
||||
|
||||
#define MULTI_SEND_BIND (1 << 7)
|
||||
#define MULTI_SEND_RANGECHECK (1 << 5)
|
||||
#define MULTI_SEND_AUTOBIND (1 << 6)
|
||||
|
||||
|
||||
#define BITLEN_MULTI (10*2) //100000 Baud => 10uS per bit
|
||||
|
||||
#if defined(PPM_PIN_HW_SERIAL)
|
||||
static void sendByteMulti(uint8_t b)
|
||||
{
|
||||
uint8_t parity = 1;
|
||||
|
||||
putDsm2SerialBit(0); // Start bit
|
||||
for (uint8_t i=0; i<8; i++) { // 8 data Bits
|
||||
putDsm2SerialBit(b & 1);
|
||||
parity = parity ^ (b & 1);
|
||||
b >>= 1;
|
||||
}
|
||||
putDsm2SerialBit(parity); // Even Parity bit
|
||||
|
||||
putDsm2SerialBit(1); // Stop bit
|
||||
putDsm2SerialBit(1); // Stop bit
|
||||
}
|
||||
#else
|
||||
static void _send_level(uint8_t v)
|
||||
{
|
||||
/* Copied over from DSM, this looks doubious and in my logic analyzer
|
||||
output the low->high is about 2 ns late */
|
||||
if (modulePulsesData[EXTERNAL_MODULE].dsm2.index == 0)
|
||||
v -= 2;
|
||||
else
|
||||
v += 2;
|
||||
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.value += v;
|
||||
*modulePulsesData[EXTERNAL_MODULE].dsm2.ptr++ = modulePulsesData[EXTERNAL_MODULE].dsm2.value;
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.index = (modulePulsesData[EXTERNAL_MODULE].dsm2.index+1) % 2;
|
||||
}
|
||||
|
||||
static void sendByteMulti(uint8_t b) //max 11 changes 0 10 10 10 10 P 1
|
||||
{
|
||||
bool lev = 0;
|
||||
uint8_t parity = 1;
|
||||
|
||||
uint8_t len = BITLEN_MULTI; //max val: 10*20 < 256
|
||||
for (uint8_t i=0; i<=9; i++) { //8Bits + 1Parity + Stop=1
|
||||
bool nlev = b & 1; //lsb first
|
||||
parity = parity ^ nlev;
|
||||
if (lev == nlev) {
|
||||
len += BITLEN_MULTI;
|
||||
}
|
||||
else {
|
||||
_send_level(len);
|
||||
len = BITLEN_MULTI;
|
||||
lev = nlev;
|
||||
}
|
||||
b = (b>>1) | 0x80; //shift in ones for stop bit and parity
|
||||
if (i==7)
|
||||
b = b ^ parity; // lowest bit is one from previous line
|
||||
}
|
||||
_send_level(len+2*BITLEN_MULTI); // 2Stop bits
|
||||
}
|
||||
#endif
|
||||
|
||||
// This is the data stream to send, prepare after 19.5 mS
|
||||
// Send after 22.5 mS
|
||||
|
||||
//static uint8_t *Dsm2_pulsePtr = pulses2MHz.pbyte ;
|
||||
|
||||
|
||||
#define MULTI_CHANS 16
|
||||
#define MULTI_CHAN_BITS 11
|
||||
void setupPulsesMultimodule(unsigned int port)
|
||||
{
|
||||
|
||||
#if defined(PPM_PIN_HW_SERIAL)
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.serialByte = 0 ;
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.serialBitCount = 0 ;
|
||||
#else
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.value = 0;
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.index = 1;
|
||||
#endif
|
||||
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.ptr = modulePulsesData[EXTERNAL_MODULE].dsm2.pulses;
|
||||
|
||||
#if defined(PCBTARANIS)
|
||||
modulePulsesData[EXTERNAL_MODULE].dsm2.value = 100;
|
||||
*modulePulsesData[EXTERNAL_MODULE].dsm2.ptr++ = modulePulsesData[EXTERNAL_MODULE].dsm2.value;
|
||||
#endif
|
||||
|
||||
// header, byte 0, always 0x55
|
||||
sendByteMulti(0x55);
|
||||
|
||||
// byte 1+2, protocol information
|
||||
|
||||
int type = g_model.moduleData[port].multi.rfProtocol +1;
|
||||
int subtype = g_model.moduleData[port].subType;
|
||||
|
||||
uint8_t protoByte=0;
|
||||
if (moduleFlag[port] == MODULE_BIND)
|
||||
protoByte |= MULTI_SEND_BIND;
|
||||
else if (moduleFlag[port] == MODULE_RANGECHECK)
|
||||
protoByte |= MULTI_SEND_RANGECHECK;
|
||||
|
||||
// rfProtocol
|
||||
// 15 for Multimodule is FrskyX or D16 which we map as a subprotocol of 3 (FrSky)
|
||||
// all protos > frskyx are therefore also off by one
|
||||
if (type >=15)
|
||||
type= type +1;
|
||||
|
||||
if (g_model.moduleData[port].multi.rfProtocol == MM_RF_PROTO_FRSKY) {
|
||||
if(subtype == 1)
|
||||
type = 3;
|
||||
else
|
||||
type = 15;
|
||||
subtype = 0;
|
||||
}
|
||||
|
||||
if (g_model.moduleData[port].multi.rfProtocol >= MM_RF_PROTO_CUSTOM)
|
||||
type = g_model.moduleData[port].multi.rfProtocol & 0x1f;
|
||||
|
||||
protoByte |= (type & 0x1f) | (g_model.moduleData[port].multi.autoBindMode << 6);
|
||||
sendByteMulti(protoByte);
|
||||
|
||||
|
||||
// power always set to high (0 << 7)
|
||||
sendByteMulti((g_model.header.modelId[port] & 0x0f)
|
||||
| ((subtype & 0x7) << 4)
|
||||
| (g_model.moduleData[port].multi.lowPowerMode << 7)
|
||||
);
|
||||
|
||||
|
||||
|
||||
//TODO: option_protocol, use same default as multi module
|
||||
// byte 3
|
||||
sendByteMulti(g_model.moduleData[port].multi.optionValue);
|
||||
|
||||
uint32_t bits = 0;
|
||||
uint8_t bitsavailable = 0;
|
||||
|
||||
// byte 4-25, channels 0..2047
|
||||
// ?? Range for pulses (channelsOutputs) is [-1024:+1024]
|
||||
for (int i=0; i<MULTI_CHANS; i++) {
|
||||
int channel = g_model.moduleData[port].channelsStart+i;
|
||||
int value = channelOutputs[channel] + 2*PPM_CH_CENTER(channel) - 2*PPM_CENTER;
|
||||
|
||||
bits |= limit(0, 1024 + value, 2047) << bitsavailable;
|
||||
bitsavailable += MULTI_CHAN_BITS;
|
||||
while (bitsavailable >= 8) {
|
||||
sendByteMulti((uint8_t) (bits & 0xff));
|
||||
bits >>= 8;
|
||||
bitsavailable -= 8;
|
||||
}
|
||||
}
|
||||
|
||||
putDsm2Flush();
|
||||
}
|
||||
@@ -35,8 +35,8 @@ void setupPulsesPPM(unsigned int port) // Don't enable interru
|
||||
// TODO move register stuff to driver
|
||||
register Pwm *pwmptr = PWM;
|
||||
uint32_t pwmCh = (port == EXTERNAL_MODULE ? 3 : 1);
|
||||
pwmptr->PWM_CH_NUM[pwmCh].PWM_CDTYUPD = (g_model.moduleData[port].ppmDelay * 50 + 300) * 2; //Stoplen *2
|
||||
if (g_model.moduleData[port].ppmPulsePol)
|
||||
pwmptr->PWM_CH_NUM[pwmCh].PWM_CDTYUPD = (g_model.moduleData[port].ppm.delay * 50 + 300) * 2; //Stoplen *2
|
||||
if (g_model.moduleData[port].ppm.pulsePol)
|
||||
pwmptr->PWM_CH_NUM[pwmCh].PWM_CMR &= ~0x00000200 ; // CPOL
|
||||
else
|
||||
pwmptr->PWM_CH_NUM[pwmCh].PWM_CMR |= 0x00000200 ; // CPOL
|
||||
@@ -48,7 +48,7 @@ void setupPulsesPPM(unsigned int port) // Don't enable interru
|
||||
ppmPulsesData->ptr = ptr;
|
||||
|
||||
int32_t rest = 22500u * 2;
|
||||
rest += (int32_t(g_model.moduleData[port].ppmFrameLength)) * 1000;
|
||||
rest += (int32_t(g_model.moduleData[port].ppm.frameLength)) * 1000;
|
||||
for (uint32_t i=firstCh; i<lastCh; i++) {
|
||||
int16_t v = limit((int16_t)-PPM_range, channelOutputs[i], (int16_t)PPM_range) + 2*PPM_CH_CENTER(i);
|
||||
rest -= v;
|
||||
@@ -61,19 +61,19 @@ void setupPulsesPPM(unsigned int port) // Don't enable interru
|
||||
|
||||
#if !defined(PCBSKY9X)
|
||||
rest -= 1000;
|
||||
uint32_t ppmDelay = (g_model.moduleData[port].ppmDelay * 50 + 300) * 2;
|
||||
uint32_t ppmDelay = (g_model.moduleData[port].ppm.delay * 50 + 300) * 2;
|
||||
// set idle time, ppm delay and ppm polarity
|
||||
if (port == TRAINER_MODULE) {
|
||||
set_trainer_ppm_parameters(rest, ppmDelay, !g_model.moduleData[TRAINER_MODULE].ppmPulsePol); // ppmPulsePol: 0 - positive, 1 - negative
|
||||
set_trainer_ppm_parameters(rest, ppmDelay, !g_model.moduleData[TRAINER_MODULE].ppm.pulsePol); // ppmPulsePol: 0 - positive, 1 - negative
|
||||
}
|
||||
else if (port == EXTERNAL_MODULE) {
|
||||
set_external_ppm_parameters(rest, ppmDelay, !g_model.moduleData[EXTERNAL_MODULE].ppmPulsePol);
|
||||
set_external_ppm_parameters(rest, ppmDelay, !g_model.moduleData[EXTERNAL_MODULE].ppm.pulsePol);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(TARANIS_INTERNAL_PPM)
|
||||
else if (port == INTERNAL_MODULE) {
|
||||
set_internal_ppm_parameters(rest, ppmDelay, !g_model.moduleData[INTERNAL_MODULE].ppmPulsePol);
|
||||
set_internal_ppm_parameters(rest, ppmDelay, !g_model.moduleData[INTERNAL_MODULE].ppm.pulsePol);
|
||||
}
|
||||
#endif // #if defined(TARANIS_INTERNAL_PPM)
|
||||
}
|
||||
|
||||
@@ -68,6 +68,16 @@ enum ModuleFlag
|
||||
#define IS_DSM2_SERIAL_PROTOCOL(protocol) (0)
|
||||
#endif
|
||||
|
||||
#if defined(MULTIMODULE)
|
||||
#define IS_MULTIMODULE_PROTOCOL(protocol) (protocol==PROTO_MULTIMODULE)
|
||||
#if !defined(DSM2)
|
||||
#error You need to enable DSM2 = PPM for MULTIMODULE support
|
||||
#endif
|
||||
#else
|
||||
#define IS_MULTIMODULE_PROTOCOL(protocol) (0)
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(CPUARM)
|
||||
#include "pulses_arm.h"
|
||||
#else
|
||||
|
||||
@@ -64,6 +64,11 @@ void setupPulses(unsigned int port)
|
||||
case MODULE_TYPE_XJT:
|
||||
required_protocol = PROTO_PXX;
|
||||
break;
|
||||
#if defined(MULTIMODULE)
|
||||
case MODULE_TYPE_MULTIMODULE:
|
||||
required_protocol = PROTO_MULTIMODULE;
|
||||
break;
|
||||
#endif
|
||||
#if defined(PCBTARANIS) && defined(DSM2)
|
||||
case MODULE_TYPE_DSM2:
|
||||
required_protocol = limit<uint8_t>(PROTO_DSM2_LP45, PROTO_DSM2_LP45+g_model.moduleData[EXTERNAL_MODULE].rfProtocol, PROTO_DSM2_DSMX);
|
||||
@@ -123,6 +128,11 @@ void setupPulses(unsigned int port)
|
||||
case PROTO_CROSSFIRE:
|
||||
disable_crossfire(port);
|
||||
break;
|
||||
#endif
|
||||
#if defined(MULTIMODULE)
|
||||
case PROTO_MULTIMODULE:
|
||||
disable_dsm2(port);
|
||||
break;
|
||||
#endif
|
||||
case PROTO_PPM:
|
||||
disable_ppm(port);
|
||||
@@ -149,6 +159,11 @@ void setupPulses(unsigned int port)
|
||||
case PROTO_CROSSFIRE:
|
||||
init_crossfire(port);
|
||||
break;
|
||||
#endif
|
||||
#if defined(MULTIMODULE)
|
||||
case PROTO_MULTIMODULE:
|
||||
init_dsm2(port);
|
||||
break;
|
||||
#endif
|
||||
case PROTO_PPM:
|
||||
init_ppm(port);
|
||||
@@ -182,6 +197,11 @@ void setupPulses(unsigned int port)
|
||||
}
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
#if defined(MULTIMODULE)
|
||||
case PROTO_MULTIMODULE:
|
||||
setupPulsesMultimodule(port);
|
||||
break;
|
||||
#endif
|
||||
case PROTO_PPM:
|
||||
setupPulsesPPM(port);
|
||||
|
||||
@@ -88,11 +88,16 @@ union TrainerPulsesData {
|
||||
|
||||
extern ModulePulsesData modulePulsesData[NUM_MODULES];
|
||||
extern TrainerPulsesData trainerPulsesData;
|
||||
extern const uint16_t CRCTable[];
|
||||
|
||||
void setupPulses(unsigned int port);
|
||||
void setupPulsesDSM2(unsigned int port);
|
||||
void setupPulsesMultimodule(unsigned int port);
|
||||
void setupPulsesPXX(unsigned int port);
|
||||
void setupPulsesPPM(unsigned int port);
|
||||
void sendByteDsm2(uint8_t b);
|
||||
void putDsm2Flush();
|
||||
void putDsm2SerialBit(uint8_t bit);
|
||||
|
||||
void createCrossfireFrame(uint8_t * frame, int16_t * pulses);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user