chore: New module/serial API (#3055)

* WiP: new module interface

* WiP: softserial timer DMA

* WiP: setup() and send() in one step

and remove methods specific to internal or external module

* fixes and extra port config

* horus targets

* module ports have moved

* T8 support (only internal module)

* fix nv14

* stupid GCC....

* disable BT

* remove nv14 telem driver

* remove old aux serial driver

* Move module buffers out of pulses.h

* Soft serial fixes and PXX1 support

* Fixed PPM module output

* Fixed CLI

* compilation fixes

* Ported bluetooth and flysky gimbal to new serial driver

* Re-enable soft-serial RX

* fixes and cleanup

* nv14 fixes

* Fix flysky gimbals

* Ghost module

* 16 bytes of RAM

* Remove getByte from module API

* Ghost cleanup

* fix: _half_duplex_input() must be called after usart init

* WiP

* more cleanup

* Remove board specific settings from PXX1 / PXX2 drivers

* A bit of PXX2 header cleanup

* Add support for OVER8 and ONEBIT

* Add some support for inverters (NV14 / EL18)

* Support NV14 power fixes in generic board

* Fix polarity handling

* Fix RX DMA

* Fix mixer not running

* Add soft-serial polarity control

* Quick fix for telemetry polling while module port is changed

* Fix bootloader DEBUG

* Fix MPM telemetry

* Fix pulse start

* DSM2 soft-serial fallback

* DSMP async module restart

* Fix CRSF LUA upstream

* Remove global variable telemetryProtocol

* Use HAL/LL constants

* Fix various issue with AFHDS3 and NV14/EL18 internal module

* Fixed S.PORT unit tests

* fix: update widx after writing at current index

* fix: init error condition

* Fix: possible infinite loop in _find_port()

* Add support for bidir S.PORT init

* Add support for "getLastByte" in HW serial driver

* PXX1: Add support for S.PORT based on IDLE callback

* Fix rebase error

* Fix softserial _rx_fifo_push()

* Add softserial support for waitForTxCompleted()

* Fix MPM flashing (internal / external)

* Fixed external MPM

* Activate RX DMA on internal module

Seems to work with serial passthrough without issues, at least on the L3R PRO.

* Fix softserial DMA on stdlcd F4 targets

* Add softserial support for 32bit timers (mostly TIM2)

* More precise soft-serial

* Fix MPM update using inverters & HW uart

* Raise S.PORT DMA/USART IRQ prio to achieve faster switching to input

* Remove useless delays

* Fix flashing external ELRS module

* Added a retry mechanism for FrSky S.PORT update

* Remove useless enableRx()

* External FrSky modules are all flashed via S.PORT

* Remove power management unit update

This was only used on the first batch of X-lite.
This commit is contained in:
Raphael Coeffic
2023-02-09 19:44:32 +10:00
committed by GitHub
parent d80adc6c4d
commit 72193eda82
189 changed files with 7960 additions and 8364 deletions
+26 -38
View File
@@ -23,21 +23,18 @@
#include "afhds2.h"
#include "flysky.h"
#include "intmodule_serial_driver.h"
#include "mixer_scheduler.h"
#include "hal/module_port.h"
#include "pulses/pulses.h"
#include "telemetry/telemetry.h"
const etx_serial_init afhds2SerialInitParams = {
.baudrate = INTMODULE_USART_AFHDS2_BAUDRATE,
.parity = ETX_Parity_None,
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.encoding = ETX_Encoding_8N1,
.direction = ETX_Dir_TX_RX,
.polarity = ETX_Pol_Normal,
};
static void* afhds2Init(uint8_t module)
@@ -46,50 +43,43 @@ static void* afhds2Init(uint8_t module)
// serial port setup
resetPulsesAFHDS2();
void* ctx = IntmoduleSerialDriver.init(&afhds2SerialInitParams);
auto mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART,
&afhds2SerialInitParams);
// mixer setup
mixerSchedulerSetPeriod(INTERNAL_MODULE, AFHDS2_PERIOD);
INTERNAL_MODULE_ON();
mixerSchedulerSetPeriod(module, AFHDS2_PERIOD);
return ctx;
return (void*)mod_st;
}
static void afhds2DeInit(void* context)
static void afhds2DeInit(void* ctx)
{
INTERNAL_MODULE_OFF();
IntmoduleSerialDriver.deinit(context);
// mixer setup
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
auto mod_st = (etx_module_state_t*)ctx;
modulePortDeInit(mod_st);
}
static void afhds2SetupPulses(void* context, int16_t* channels, uint8_t nChannels)
static void afhds2SendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_t nChannels)
{
(void)context;
// TODO:
(void)channels;
(void)nChannels;
ModuleSyncStatus& status = getModuleSyncStatus(INTERNAL_MODULE);
mixerSchedulerSetPeriod(INTERNAL_MODULE, status.isValid()
? status.getAdjustedRefreshRate()
: AFHDS2_PERIOD);
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
ModuleSyncStatus& status = getModuleSyncStatus(module);
mixerSchedulerSetPeriod(module, status.isValid()
? status.getAdjustedRefreshRate()
: AFHDS2_PERIOD);
status.invalidate();
setupPulsesAFHDS2();
}
static void afhds2SendPulses(void* context)
{
uint8_t* data = (uint8_t*)intmodulePulsesData.flysky.pulses;
uint16_t size = intmodulePulsesData.flysky.ptr - data;
IntmoduleSerialDriver.sendBuffer(context, data, size);
}
auto p_data = buffer;
setupPulsesAFHDS2(p_data);
static int afhds2GetByte(void* context, uint8_t* data)
{
return IntmoduleSerialDriver.getByte(context, data);
auto drv = modulePortGetSerialDrv(mod_st->tx);
auto drv_ctx = modulePortGetCtx(mod_st->tx);
drv->sendBuffer(drv_ctx, buffer, p_data - buffer);
}
static void afhds2ProcessData(void*, uint8_t data, uint8_t* buffer, uint8_t* len)
@@ -97,12 +87,10 @@ static void afhds2ProcessData(void*, uint8_t data, uint8_t* buffer, uint8_t* len
processInternalFlySkyTelemetryData(data, buffer, len);
}
const etx_module_driver_t Afhds2InternalDriver = {
const etx_proto_driver_t Afhds2InternalDriver = {
.protocol = PROTOCOL_CHANNELS_AFHDS2A,
.init = afhds2Init,
.deinit = afhds2DeInit,
.setupPulses = afhds2SetupPulses,
.sendPulses = afhds2SendPulses,
.getByte = afhds2GetByte,
.processData = afhds2ProcessData,
};
+1 -12
View File
@@ -32,18 +32,7 @@
#define AFHDS2_PERIOD (2 * 1000) /* us */
struct FlySkySerialPulsesData {
uint8_t pulses[64];
uint8_t * ptr;
uint8_t frame_index;
uint8_t crc;
uint8_t state;
uint8_t timeout;
uint8_t esc_state;
} __attribute__((__packed__));
extern const etx_serial_init afhds2SerialInitParams;
extern const etx_module_driver_t Afhds2InternalDriver;
extern const etx_proto_driver_t Afhds2InternalDriver;
#endif /* PULSES_AFHDS2_H_ */
+46 -102
View File
@@ -31,16 +31,7 @@
#include "telemetry/telemetry.h"
#include "mixer_scheduler.h"
#include "hal/module_driver.h"
#if defined(INTMODULE_USART)
#include "intmodule_serial_driver.h"
#endif
#if defined(AFHDS3_EXT_UART)
#include "extmodule_serial_driver.h"
#else
#include "extmodule_driver.h"
#endif
#include "hal/module_port.h"
#define FAILSAFE_HOLD 1
#define FAILSAFE_CUSTOM 2
@@ -192,8 +183,7 @@ class ProtoState
* @param moduleIndex index of module one of INTERNAL_MODULE, EXTERNAL_MODULE
* @param resetFrameCount flag if current frame count should be reseted
*/
void init(uint8_t moduleIndex, void* buffer, const etx_serial_driver_t* drv);
void deinit() { trsp.deinit(); }
void init(uint8_t moduleIndex, void* buffer, etx_module_state_t* mod_st);
/**
* Fills DMA buffers with frame to be send depending on actual state
@@ -205,11 +195,6 @@ class ProtoState
*/
void sendFrame() { trsp.sendBuffer(); }
/**
* Fetch telemetry byte
*/
int getTelemetryByte(uint8_t* data) { return trsp.getTelemetryByte(data); }
/**
* Gets actual module status into provided buffer
* @param statusText target buffer for status
@@ -320,7 +305,7 @@ static const COMMAND periodicRequestCommands[] =
};
//Static collection of afhds3 object instances by module
static ProtoState protoState[NUM_MODULES];
static ProtoState protoState[MAX_MODULES];
void getStatusString(uint8_t module, char* buffer)
{
@@ -330,7 +315,8 @@ void getStatusString(uint8_t module, char* buffer)
//friends function that can access telemetry parsing method
void processTelemetryData(void* ctx, uint8_t data, uint8_t* buffer, uint8_t* len)
{
auto p_state = (ProtoState*)ctx;
auto mod_st = (etx_module_state_t*)ctx;
auto p_state = (ProtoState*)mod_st->user_data;
p_state->processTelemetryData(data, buffer, len);
}
@@ -482,16 +468,10 @@ void ProtoState::setupFrame()
}
}
void ProtoState::init(uint8_t moduleIndex, void* buffer, const etx_serial_driver_t* drv)
void ProtoState::init(uint8_t moduleIndex, void* buffer, etx_module_state_t* mod_st)
{
module_index = moduleIndex;
// TODO: move bit/byte transport to serial driver
if (drv) {
trsp.init(ByteTransport::Serial, buffer, drv);
} else {
trsp.init(ByteTransport::Pulses, buffer, nullptr);
}
trsp.init(buffer, mod_st);
//clear local vars because it is member of union
moduleData = &g_model.moduleData[module_index];
@@ -861,106 +841,70 @@ void applyModelConfig(uint8_t module)
p_state->applyConfigFromModel();
}
static void* initExternal(uint8_t module)
static const etx_serial_init _uartParams = {
.baudrate = 0, //AFHDS3_UART_BAUDRATE,
.encoding = ETX_Encoding_8N1,
.direction = ETX_Dir_TX_RX,
.polarity = ETX_Pol_Normal,
};
static void* initModule(uint8_t module)
{
#if defined(AFHDS3_EXT_UART)
auto drv = extmoduleGetSerialPort();
if (!drv) return nullptr;
etx_module_state_t* mod_st = nullptr;
etx_serial_init params(_uartParams);
uint16_t period = AFHDS3_UART_COMMAND_TIMEOUT * 1000;
uint16_t period = AFHDS3_UART_COMMAND_TIMEOUT * 1000 /* us */;
#else
const etx_serial_driver_t* drv = nullptr;
uint16_t period = AFHDS3_SOFTSERIAL_COMMAND_TIMEOUT * 1000 /* us */;
params.baudrate = AFHDS3_UART_BAUDRATE;
params.polarity =
module == INTERNAL_MODULE ? ETX_Pol_Normal : ETX_Pol_Inverted;
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &params);
telemetryPortInvertedInit(AFHDS3_SOFTSERIAL_BAUDRATE);
telemetryPortSetDirectionInput();
#endif
if (module == EXTERNAL_MODULE && !mod_st) {
// soft-serial fallback
params.baudrate = AFHDS3_SOFTSERIAL_BAUDRATE;
params.direction = ETX_Dir_RX;
period = AFHDS3_SOFTSERIAL_COMMAND_TIMEOUT * 1000 /* us */;
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, &params);
// TODO: telemetry RX ???
}
if (!mod_st) return nullptr;
auto p_state = &protoState[module];
p_state->init(module, &extmodulePulsesData.afhds3, drv);
p_state->init(module, pulsesGetModuleBuffer(module), mod_st);
mod_st->user_data = (void*)p_state;
mixerSchedulerSetPeriod(module, period);
EXTERNAL_MODULE_ON();
return p_state;
return mod_st;
}
static void deinitExternal(void* context)
static void deinitModule(void* ctx)
{
EXTERNAL_MODULE_OFF();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
auto p_state = (ProtoState*)context;
p_state->deinit();
auto mod_st = (etx_module_state_t*)ctx;
modulePortDeInit(mod_st);
}
static void sendPulses(void* context)
{
auto p_state = (ProtoState*)context;
p_state->sendFrame();
}
static void setupPulses(void* context, int16_t* channels, uint8_t nChannels)
static void sendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_t nChannels)
{
(void)buffer;
(void)channels;
(void)nChannels;
auto p_state = (ProtoState*)context;
auto mod_st = (etx_module_state_t*)ctx;
auto p_state = (ProtoState*)mod_st->user_data;
p_state->setupFrame();
p_state->sendFrame();
}
etx_module_driver_t externalDriver = {
etx_proto_driver_t ProtoDriver = {
.protocol = PROTOCOL_CHANNELS_AFHDS3,
.init = initExternal,
.deinit = deinitExternal,
.setupPulses = setupPulses,
.init = initModule,
.deinit = deinitModule,
.sendPulses = sendPulses,
.getByte = nullptr, // TODO
.processData = processTelemetryData,
};
#if defined(INTERNAL_MODULE_AFHDS3)
static void* initInternal(uint8_t module)
{
auto p_state = &protoState[module];
p_state->init(module, &intmodulePulsesData, &IntmoduleSerialDriver);
telemetryProtocol = PROTOCOL_TELEMETRY_AFHDS3;
mixerSchedulerSetPeriod(module, AFHDS3_UART_COMMAND_TIMEOUT * 1000 /* us */);
INTERNAL_MODULE_ON();
return p_state;
}
static void deinitInternal(void* context)
{
INTERNAL_MODULE_OFF();
telemetryProtocol = 0xFF;
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
auto p_state = (ProtoState*)context;
p_state->deinit();
}
static int getByteInternal(void* context, uint8_t* data)
{
auto p_state = (ProtoState*)context;
return p_state->getTelemetryByte(data);
}
etx_module_driver_t internalDriver = {
.protocol = PROTOCOL_CHANNELS_AFHDS3,
.init = initInternal,
.deinit = deinitInternal,
.setupPulses = setupPulses,
.sendPulses = sendPulses,
.getByte = getByteInternal,
.processData = processTelemetryData,
};
#endif
}
+1 -5
View File
@@ -70,11 +70,7 @@ typedef PulsesData ExtmoduleData;
typedef SerialData IntmoduleData;
#endif
extern etx_module_driver_t externalDriver;
#if defined(INTERNAL_MODULE_AFHDS3)
extern etx_module_driver_t internalDriver;
#endif
extern etx_proto_driver_t ProtoDriver;
void getStatusString(uint8_t module, char* buffer);
void processTelemetryData(uint8_t data, uint8_t module);
+19 -143
View File
@@ -23,8 +23,9 @@
#include "opentx_helpers.h"
#include "debug.h"
#include "extmodule_driver.h"
#include "board.h"
#include "dataconstants.h"
#include "mixer_scheduler.h"
// timer is 2 MHz
#if defined(AFHDS3_SLOW)
@@ -60,110 +61,16 @@ enum DeviceAddress
//Address used in transmitted frames - it constrains of target address and source address
const uint8_t FrameAddress = DeviceAddress::TRANSMITTER | (DeviceAddress::MODULE << 4);
static void _serial_reset(void* buffer)
void FrameTransport::init(void* buffer)
{
auto data = (SerialData*)buffer;
data->ptr = data->pulses;
}
static void _serial_sendByte(void* buffer, uint8_t b)
{
auto data = (SerialData*)buffer;
*(data->ptr++) = b;
}
static void _serial_flush(void*) {}
uint32_t _serial_getSize(void* buffer)
{
auto data = (SerialData*)buffer;
return data->ptr - data->pulses;
}
static void _pulses_reset(void* buffer)
{
auto data = (PulsesData*)buffer;
data->ptr = data->pulses;
}
static inline void _pulses_send_level(PulsesData* data, uint16_t v)
{
uint32_t size = data->ptr - data->pulses;
if (size >= AFHDS_MAX_PULSES_TRANSITIONS) return;
*(data->ptr)++ = v;
}
static void _pulses_sendByte(void* buffer, uint8_t b)
{
auto data = (PulsesData*)buffer;
uint32_t size = data->ptr - data->pulses;
if (size >= AFHDS_MAX_PULSES_TRANSITIONS) return;
// use 8n1
// parity: If the parity is enabled, then the MSB bit of the data to be
// transmitted is changed by the parity bit start is always 0
bool level = 0;
uint16_t length = BITLEN_AFHDS; // start bit
for (uint8_t i = 0; i <= 8; i++) { // 8 data bits + Stop=1
bool next_level = b & 1;
if (level == next_level) {
length += BITLEN_AFHDS;
} else {
_pulses_send_level(data, length);
length = BITLEN_AFHDS;
level = next_level;
}
b = (b >> 1) | 0x80; // shift left to get next bit, fill msb with stop
// bit - needed just once
}
_pulses_send_level(data, length); // last bit (stop)
}
static void _pulses_flush(void* buffer)
{
// TODO: use less, as the new driver can handle it
auto data = (PulsesData*)buffer;
*data->ptr = 60000;
}
uint32_t _pulses_getSize(void* buffer)
{
auto data = (PulsesData*)buffer;
return data->ptr - data->pulses;
}
void ByteTransport::init(Type t)
{
if (t == Serial) {
reset = _serial_reset;
sendByte = _serial_sendByte;
flush = _serial_flush;
getSize = _serial_getSize;
} else if (t == Pulses) {
reset = _pulses_reset;
sendByte = _pulses_sendByte;
flush = _pulses_flush;
getSize = _pulses_getSize;
} else {
reset = nullptr;
sendByte = nullptr;
flush = nullptr;
getSize = nullptr;
}
}
void FrameTransport::init(ByteTransport::Type t, void* buffer)
{
trsp.init(t);
trsp_buffer = buffer;
trsp_buffer = (uint8_t*)buffer;
clear();
}
void FrameTransport::clear()
{
// reset send buffer
trsp.reset(trsp_buffer);
data_ptr = trsp_buffer;
// reset parser
esc_state = 0;
@@ -171,7 +78,7 @@ void FrameTransport::clear()
void FrameTransport::sendByte(uint8_t b)
{
trsp.sendByte(trsp_buffer, b);
*(data_ptr++) = b;
}
void FrameTransport::putBytes(uint8_t* data, int length)
@@ -197,7 +104,8 @@ void FrameTransport::putFrame(COMMAND command, FRAME_TYPE frameType, uint8_t* da
uint8_t dataLength, uint8_t frameIndex)
{
//header
trsp.reset(trsp_buffer);
data_ptr = trsp_buffer;
crc = 0;
sendByte(START);
@@ -213,7 +121,11 @@ void FrameTransport::putFrame(COMMAND command, FRAME_TYPE frameType, uint8_t* da
uint8_t crcValue = crc ^ 0xff;
putBytes(&crcValue, 1);
sendByte(END);
trsp.flush(trsp_buffer);
}
uint32_t FrameTransport::getFrameSize()
{
return data_ptr - trsp_buffer;
}
static bool _checkCRC(const uint8_t* data, uint8_t size)
@@ -311,35 +223,10 @@ void CommandFifo::enqueue(COMMAND command, FRAME_TYPE frameType, bool useData,
}
}
static const etx_serial_init _uartParams = {
.baudrate = AFHDS3_UART_BAUDRATE,
.parity = ETX_Parity_None,
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
};
void Transport::init(ByteTransport::Type t, void* buffer, const etx_serial_driver_t* drv)
void Transport::init(void* buffer, etx_module_state_t* mod_st)
{
trsp.init(t, buffer);
if (drv) {
uart_drv = drv;
uart_ctx = drv->init(&_uartParams);
} else {
uart_drv = nullptr;
uart_ctx = nullptr;
extmoduleSerialStart();
}
}
void Transport::deinit()
{
if (uart_drv) {
uart_drv->deinit(uart_ctx);
} else {
extmoduleStop();
}
trsp.init(buffer);
this->mod_st = mod_st;
}
void Transport::clear()
@@ -385,11 +272,9 @@ void Transport::enqueue(COMMAND command, FRAME_TYPE frameType, bool useData,
void Transport::sendBuffer()
{
if (uart_drv) {
uart_drv->sendBuffer(uart_ctx, (uint8_t*)trsp.trsp_buffer, trsp.getFrameSize());
} else {
extmoduleSendNextFrameSoftSerial((uint8_t*)trsp.trsp_buffer, trsp.getFrameSize());
}
auto drv = modulePortGetSerialDrv(mod_st->tx);
auto ctx = modulePortGetCtx(mod_st->tx);
drv->sendBuffer(ctx, (uint8_t*)trsp.trsp_buffer, trsp.getFrameSize());
}
bool Transport::processQueue()
@@ -474,15 +359,6 @@ bool Transport::handleReply(uint8_t* buffer, uint8_t len)
return false;
}
int Transport::getTelemetryByte(uint8_t* data)
{
if (uart_drv) {
return uart_drv->getByte(uart_ctx, data);
} else {
return sportGetByte(data);
}
}
bool Transport::processTelemetryData(uint8_t byte, uint8_t* rxBuffer,
uint8_t& rxBufferCount, uint8_t maxSize)
{
+7 -22
View File
@@ -24,22 +24,11 @@
#include "afhds3_module.h"
#include "definitions.h"
#include "hal/serial_driver.h"
#include "hal/module_port.h"
namespace afhds3
{
struct ByteTransport {
enum Type { Serial, Pulses };
void (*reset)(void* buffer);
void (*sendByte)(void* buffer, uint8_t b);
void (*flush)(void* buffer);
uint32_t (*getSize)(void* buffer);
void init(Type t);
};
enum FRAME_TYPE: uint8_t
{
REQUEST_GET_DATA = 0x01, //Get data response: ACK + DATA
@@ -130,14 +119,14 @@ struct CommandFifo {
struct FrameTransport
{
ByteTransport trsp;
void* trsp_buffer;
uint8_t* trsp_buffer;
uint8_t* data_ptr;
uint8_t crc;
// uint8_t timeout;
uint8_t esc_state;
void init(ByteTransport::Type t, void* buffer);
void init(void* buffer);
void clear();
void sendByte(uint8_t b);
@@ -146,7 +135,7 @@ struct FrameTransport
void putFrame(COMMAND command, FRAME_TYPE frameType, uint8_t* data,
uint8_t dataLength, uint8_t frameIndex);
uint32_t getFrameSize() { return trsp.getSize(trsp_buffer); }
uint32_t getFrameSize();
bool processTelemetryData(uint8_t byte, uint8_t* rxBuffer,
uint8_t& rxBufferCount, uint8_t maxSize);
@@ -161,8 +150,7 @@ class Transport
IDLE
};
const etx_serial_driver_t* uart_drv;
void* uart_ctx;
etx_module_state_t* mod_st;
FrameTransport trsp;
CommandFifo fifo;
@@ -187,8 +175,7 @@ class Transport
bool handleReply(uint8_t* buffer, uint8_t len);
public:
void init(ByteTransport::Type t, void* buffer, const etx_serial_driver_t* drv);
void deinit();
void init(void* buffer, etx_module_state_t* mod_st);
void clear();
@@ -213,8 +200,6 @@ class Transport
*/
bool processQueue();
int getTelemetryByte(uint8_t* data);
bool processTelemetryData(uint8_t byte, uint8_t* rxBuffer,
uint8_t& rxBufferCount, uint8_t maxSize);
};
+57 -132
View File
@@ -22,10 +22,7 @@
#include "opentx.h"
#include "mixer_scheduler.h"
#include "hal/module_driver.h"
#if defined(INTMODULE_USART)
#include "intmodule_serial_driver.h"
#endif
#include "hal/module_port.h"
#include "crossfire.h"
#include "telemetry/crossfire.h"
@@ -79,26 +76,25 @@ uint8_t createCrossfireChannelsFrame(uint8_t * frame, int16_t * pulses)
return buf - frame;
}
static void setupPulsesCrossfire(uint8_t idx, CrossfirePulsesData* p_data,
static void setupPulsesCrossfire(uint8_t module, uint8_t*& p_buf,
uint8_t endpoint, int16_t* channels,
uint8_t nChannels)
{
#if defined(LUA)
if (outputTelemetryBuffer.destination == endpoint) {
memcpy(p_data->pulses, outputTelemetryBuffer.data,
outputTelemetryBuffer.size);
p_data->length = outputTelemetryBuffer.size;
auto len = outputTelemetryBuffer.size;
memcpy(p_buf, outputTelemetryBuffer.data, len);
outputTelemetryBuffer.reset();
p_buf += len;
} else
#endif
{
if (moduleState[idx].counter == CRSF_FRAME_MODELID) {
p_data->length = createCrossfireModelIDFrame(idx, p_data->pulses);
moduleState[idx].counter = CRSF_FRAME_MODELID_SENT;
if (moduleState[module].counter == CRSF_FRAME_MODELID) {
p_buf += createCrossfireModelIDFrame(module, p_buf);
moduleState[module].counter = CRSF_FRAME_MODELID_SENT;
} else {
p_data->length = createCrossfireChannelsFrame(
p_data->pulses,
channels /*TODO: nChannels*/);
/* TODO: nChannels */
p_buf += createCrossfireChannelsFrame(p_buf, channels);
}
}
}
@@ -109,7 +105,7 @@ static void crossfireSetupMixerScheduler(uint8_t module)
if (status.isValid()) {
mixerSchedulerSetPeriod(module, status.getAdjustedRefreshRate());
} else {
mixerSchedulerSetPeriod(module, CROSSFIRE_PERIOD);
mixerSchedulerSetPeriod(module, CROSSFIRE_PERIOD(module));
}
}
@@ -120,59 +116,22 @@ static bool _checkFrameCRC(uint8_t* rxBuffer)
return (crc == rxBuffer[len+1]);
}
struct CrossfireState {
uint8_t module;
CrossfirePulsesData* data;
const etx_serial_driver_t* uart_drv;
void* uart_ctx;
uint8_t endpoint;
void init(uint8_t _module, CrossfirePulsesData* _data,
const etx_serial_driver_t* _drv, void* _ctx, uint8_t _endpoint)
{
module = _module;
data = _data;
uart_drv = _drv;
uart_ctx = _ctx;
endpoint = _endpoint;
}
void deinit() { uart_drv->deinit(uart_ctx); }
};
static CrossfireState crossfireState[NUM_MODULES];
static void crossfireSetupPulses(void* context, int16_t* channels, uint8_t nChannels)
static void crossfireSendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_t nChannels)
{
auto state = (CrossfireState*)context;
crossfireSetupMixerScheduler(state->module);
setupPulsesCrossfire(state->module, state->data, state->endpoint, channels, nChannels);
}
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
crossfireSetupMixerScheduler(module);
static void crossfireSendPulses(void* context)
{
auto state = (CrossfireState*)context;
auto data = state->data;
uint8_t endpoint = 0;
#if defined(HARDWARE_EXTERNAL_MODULE)
if (module == EXTERNAL_MODULE) endpoint = TELEMETRY_ENDPOINT_SPORT;
#endif
auto p_buf = buffer;
setupPulsesCrossfire(module, p_buf, endpoint, channels, nChannels);
if (state->uart_drv) {
auto drv = state->uart_drv;
auto ctx = state->uart_ctx;
drv->sendBuffer(ctx, data->pulses, data->length);
} else {
sportSendBuffer(data->pulses, data->length);
}
}
static int crossfireGetByte(void* context, uint8_t* data)
{
auto state = (CrossfireState*)context;
if (state->uart_drv) {
auto drv = state->uart_drv;
auto ctx = state->uart_ctx;
return drv->getByte(ctx, data);
} else {
return sportGetByte(data);
}
auto drv = modulePortGetSerialDrv(mod_st->tx);
auto drv_ctx = modulePortGetCtx(mod_st->tx);
drv->sendBuffer(drv_ctx, buffer, p_buf - buffer);
}
static bool _lenIsSane(uint8_t len)
@@ -210,7 +169,7 @@ static void _seekStart(uint8_t* buffer, uint8_t* len)
*len = 0;
}
static void crossfireProcessData(void* context, uint8_t data, uint8_t* buffer, uint8_t* len)
static void crossfireProcessData(void* ctx, uint8_t data, uint8_t* buffer, uint8_t* len)
{
if (*len == 0 && data != RADIO_ADDRESS && data != UART_SYNC) {
TRACE("[XF] address 0x%02X error", data);
@@ -233,16 +192,15 @@ static void crossfireProcessData(void* context, uint8_t data, uint8_t* buffer, u
// rxBuffer[1] holds the packet length-2, check if the whole packet was received
while (*len > 4 && (buffer[1]+2) == *len) {
// TODO: module in context?
if (_checkFrameCRC(buffer)) {
#if defined(BLUETOOTH)
#if defined(BLUETOOTH) // TODO: generic telemetry mirror to BT
if (g_eeGeneral.bluetoothMode == BLUETOOTH_TELEMETRY &&
bluetooth.state == BLUETOOTH_STATE_CONNECTED) {
bluetooth.write(buffer, *len);
}
#endif
auto state = (CrossfireState*)context;
processCrossfireTelemetryFrame(state->module);
auto mod_st = (etx_module_state_t*)ctx;
processCrossfireTelemetryFrame(modulePortGetModule(mod_st));
*len = 0;
}
else {
@@ -252,84 +210,51 @@ static void crossfireProcessData(void* context, uint8_t data, uint8_t* buffer, u
}
}
#if defined(INTERNAL_MODULE_CRSF)
static const etx_serial_init intmoduleCrossfireInitParams = {
static const etx_serial_init crsfSerialParams = {
.baudrate = 0,
.parity = ETX_Parity_None,
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.encoding = ETX_Encoding_8N1,
.direction = ETX_Dir_TX_RX,
.polarity = ETX_Pol_Normal,
};
static void* crossfireInitInternal(uint8_t module)
static void* crossfireInit(uint8_t module)
{
// serial port setup
etx_serial_init params(intmoduleCrossfireInitParams);
params.baudrate = INT_CROSSFIRE_BAUDRATE;
INTERNAL_MODULE_ON();
etx_module_state_t* mod_st = nullptr;
etx_serial_init params(crsfSerialParams);
auto state = &crossfireState[module];
state->init(module, &intmodulePulsesData.crossfire,
&IntmoduleSerialDriver, IntmoduleSerialDriver.init(&params), 0);
#if defined(INTERNAL_MODULE_CRSF)
if (module == INTERNAL_MODULE) {
return state;
}
static void crossfireDeInitInternal(void* context)
{
INTERNAL_MODULE_OFF();
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
auto state = (CrossfireState*)context;
state->deinit();
}
const etx_module_driver_t CrossfireInternalDriver = {
.protocol = PROTOCOL_CHANNELS_CROSSFIRE,
.init = crossfireInitInternal,
.deinit = crossfireDeInitInternal,
.setupPulses = crossfireSetupPulses,
.sendPulses = crossfireSendPulses,
.getByte = crossfireGetByte,
.processData = crossfireProcessData,
};
params.baudrate = INT_CROSSFIRE_BAUDRATE;
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &params);
}
#endif
static void* crossfireInitExternal(uint8_t module)
{
// telemetryInit(PROTOCOL_TELEMETRY_CROSSFIRE);
telemetryProtocol = PROTOCOL_TELEMETRY_CROSSFIRE;
telemetryPortInit(EXT_CROSSFIRE_BAUDRATE, TELEMETRY_SERIAL_DEFAULT);
#if defined(LUA)
outputTelemetryBuffer.reset();
#if defined(HARDWARE_EXTERNAL_MODULE)
if (module == EXTERNAL_MODULE) {
params.baudrate = EXT_CROSSFIRE_BAUDRATE;
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &params);
}
#endif
telemetryPortSetDirectionOutput();
mixerSchedulerSetPeriod(module, CROSSFIRE_PERIOD);
EXTERNAL_MODULE_ON();
auto state = &crossfireState[module];
state->init(module, &extmodulePulsesData.crossfire, nullptr, nullptr,
TELEMETRY_ENDPOINT_SPORT);
return state;
if (mod_st) {
mixerSchedulerSetPeriod(module, CROSSFIRE_PERIOD(module));
}
return (void*)mod_st;
}
static void crossfireDeInitExternal(void* context)
static void crossfireDeInit(void* ctx)
{
(void)context;
telemetryProtocol = 0xFF;
EXTERNAL_MODULE_OFF();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
auto mod_st = (etx_module_state_t*)ctx;
modulePortDeInit(mod_st);
}
const etx_module_driver_t CrossfireExternalDriver = {
const etx_proto_driver_t CrossfireDriver = {
.protocol = PROTOCOL_CHANNELS_CROSSFIRE,
.init = crossfireInitExternal,
.deinit = crossfireDeInitExternal,
.setupPulses = crossfireSetupPulses,
.init = crossfireInit,
.deinit = crossfireDeInit,
.sendPulses = crossfireSendPulses,
.getByte = crossfireGetByte,
.processData = crossfireProcessData,
};
+1 -2
View File
@@ -23,5 +23,4 @@
#include "hal/module_driver.h"
extern const etx_module_driver_t CrossfireInternalDriver;
extern const etx_module_driver_t CrossfireExternalDriver;
extern const etx_proto_driver_t CrossfireDriver;
+141 -80
View File
@@ -19,7 +19,12 @@
* GNU General Public License for more details.
*/
#include "dsm2.h"
#include "hal/module_port.h"
#include "mixer_scheduler.h"
#include "opentx.h"
#include "telemetry/spektrum.h"
#define DSM2_SEND_BIND (1 << 7)
#define DSM2_SEND_RANGECHECK (1 << 5)
@@ -31,62 +36,18 @@
#define DSMX_BIT 0x08
#define BAD_DATA 0x47
#define BITLEN_DSM2 (8*2) //125000 Baud => 8uS per bit
#define DSM2_BITRATE 125000
static void reset_dsm2_buffer()
static inline void sendByte(uint8_t*& p_buf, uint8_t b)
{
extmodulePulsesData.dsm2.index = 0;
extmodulePulsesData.dsm2.ptr = extmodulePulsesData.dsm2.pulses;
*p_buf++ = b;
}
static void _send_1(uint8_t v)
{
*extmodulePulsesData.dsm2.ptr++ = v - 1;
extmodulePulsesData.dsm2.index += 1;
}
static void _sendByte(uint8_t b, uint8_t bit_len) // max 10 changes 0 10 10 10 10 1
{
bool lev = 0;
uint8_t len = bit_len; // max val: 9*16 < 256
for (uint8_t i=0; i<=8; i++) { // 8Bits + Stop=1
bool nlev = b & 1; // lsb first
if (lev == nlev) {
len += bit_len;
}
else {
_send_1(len);
len = bit_len;
lev = nlev;
}
b = (b>>1) | 0x80; // shift in stop bit
}
_send_1(len); // stop bit (len is already bit_len)
}
static void sendByteDsm2(uint8_t b)
{
_sendByte(b, BITLEN_DSM2);
}
void putDsm2Flush()
{
if (extmodulePulsesData.dsm2.index & 1)
*extmodulePulsesData.dsm2.ptr++ = 255;
else
*(extmodulePulsesData.dsm2.ptr - 1) = 255;
}
// This is the data stream to send, prepare after 19.5 mS
// Send after 22.5 mS
void setupPulsesDSM2()
static void setupPulsesDSM2(uint8_t module, uint8_t*& p_buf)
{
uint8_t dsmDat[14];
reset_dsm2_buffer();
switch (moduleState[EXTERNAL_MODULE].protocol) {
switch (moduleState[module].protocol) {
case PROTOCOL_CHANNELS_DSM2_LP45:
dsmDat[0] = 0x00;
break;
@@ -98,17 +59,17 @@ void setupPulsesDSM2()
break;
}
if (moduleState[EXTERNAL_MODULE].mode == MODULE_MODE_BIND) {
if (moduleState[module].mode == MODULE_MODE_BIND) {
dsmDat[0] |= DSM2_SEND_BIND;
}
else if (moduleState[EXTERNAL_MODULE].mode == MODULE_MODE_RANGECHECK) {
else if (moduleState[module].mode == MODULE_MODE_RANGECHECK) {
dsmDat[0] |= DSM2_SEND_RANGECHECK;
}
dsmDat[1] = g_model.header.modelId[EXTERNAL_MODULE]; // DSM2 Header second byte for model match
dsmDat[1] = g_model.header.modelId[module]; // DSM2 Header second byte for model match
for (int i=0; i<DSM2_CHANS; i++) {
int channel = g_model.moduleData[EXTERNAL_MODULE].channelsStart+i;
int channel = g_model.moduleData[module].channelsStart+i;
int value = channelOutputs[channel] + 2*PPM_CH_CENTER(channel) - 2*PPM_CENTER;
uint16_t pulse = limit(0, ((value*13)>>5)+512, 1023);
dsmDat[2+2*i] = (i<<2) | ((pulse>>8)&0x03);
@@ -116,36 +77,28 @@ void setupPulsesDSM2()
}
for (int i=0; i<14; i++) {
sendByteDsm2(dsmDat[i]);
sendByte(p_buf, dsmDat[i]);
}
putDsm2Flush();
}
#define BITLEN_DSMP (17)
static void sendByteDSMP(uint8_t b)
{
_sendByte(b, BITLEN_DSMP);
}
#define DSMP_BITRATE 115200
void setupPulsesLemonDSMP()
static void setupPulsesLemonDSMP(uint8_t module, uint8_t*& p_buf)
{
static uint8_t pass = 0;
reset_dsm2_buffer();
const auto& md = g_model.moduleData[EXTERNAL_MODULE];
const auto& md = g_model.moduleData[module];
uint8_t start_channel = md.channelsStart;
auto channels = md.getChannelsCount();
auto flags = md.dsmp.flags & 0x3F;
// Force setup packet in Bind mode.
auto module_mode = getModuleMode(EXTERNAL_MODULE);
auto module_mode = getModuleMode(module);
sendByteDSMP( 0xAA );
sendByteDSMP( pass );
sendByte(p_buf, 0xAA);
sendByte(p_buf, pass);
// Setup packet
if (pass == 0) {
@@ -154,17 +107,17 @@ void setupPulsesLemonDSMP()
flags = DSM2_SEND_BIND | (1 << 6 /* AUTO */);
channels = 12;
}
sendByteDSMP( flags );
sendByte(p_buf, flags);
uint8_t pwr = 7;
if (module_mode == MODULE_MODE_RANGECHECK) {
pwr = 4;
}
sendByteDSMP( pwr );
sendByteDSMP( channels );
sendByte(p_buf, pwr);
sendByte(p_buf, channels );
// Model number
sendByteDSMP( 1 );
sendByte(p_buf, 1);
// Send only 1 single Setup packet
pass = 1;
@@ -192,20 +145,18 @@ void setupPulsesLemonDSMP()
pulse = limit(0, ((value*13)>>5)+512, 1023) | (current_channel << 10);
}
sendByteDSMP( pulse >> 8 );
sendByteDSMP( pulse & 0xFF );
sendByte(p_buf, pulse >> 8);
sendByte(p_buf, pulse & 0xFF);
} else {
// Outside of announced number of channels:
// -> send invalid value
sendByteDSMP( 0xFF );
sendByteDSMP( 0xFF );
sendByte(p_buf, 0xFF);
sendByte(p_buf, 0xFF);
}
current_channel++;
}
}
putDsm2Flush();
if (++pass > 2) pass = 1;
if (channels < 8) pass = 1;
@@ -213,9 +164,119 @@ void setupPulsesLemonDSMP()
// bind packet is setup
pass = 0;
}
else if (--moduleState[EXTERNAL_MODULE].counter == 0) {
else if (--moduleState[module].counter == 0) {
// every 100th packet is setup
pass = 0;
moduleState[EXTERNAL_MODULE].counter = 100;
moduleState[module].counter = 100;
}
}
const etx_serial_init dsmUartParams = {
.baudrate = 0,
.encoding = ETX_Encoding_8N1,
.direction = ETX_Dir_TX,
.polarity = ETX_Pol_Inverted,
};
static void* dsmInit(uint8_t module, uint32_t baudrate, uint16_t period, bool telemetry)
{
// only external module supported
if (module == INTERNAL_MODULE) return nullptr;
etx_serial_init params(dsmUartParams);
params.baudrate = baudrate;
auto mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &params);
if (!mod_st) {
// inverted soft-serial fallback
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, &params);
if (!mod_st) return nullptr;
}
if (telemetry) {
params.direction = ETX_Dir_RX;
if (!modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &params)) {
// inverted soft-serial fallback
modulePortInitSerial(module, ETX_MOD_PORT_SPORT_INV, &params);
}
}
mixerSchedulerSetPeriod(module, period);
return (void*)mod_st;
}
static void* dsm2Init(uint8_t module)
{
return dsmInit(module, DSM2_BITRATE, DSM2_PERIOD, false);
}
static void* dsmpInit(uint8_t module)
{
return dsmInit(module, DSMP_BITRATE, 11 * 1000 /* 11ms in us */, true);
}
static void dsmDeInit(void* ctx)
{
auto mod_st = (etx_module_state_t*)ctx;
modulePortDeInit(mod_st);
}
static void _dsm_send(etx_module_state_t* st, uint8_t* buffer, uint32_t len)
{
auto drv = modulePortGetSerialDrv(st->tx);
auto ctx = modulePortGetCtx(st->tx);
drv->sendBuffer(ctx, buffer, len);
}
static void dsm2SendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_t nChannels)
{
// TODO:
(void)channels;
(void)nChannels;
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
auto p_data = buffer;
setupPulsesDSM2(module, p_data);
_dsm_send(mod_st, buffer, p_data - buffer);
}
static void dsmpSendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_t nChannels)
{
// TODO:
(void)channels;
(void)nChannels;
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
auto p_data = buffer;
setupPulsesLemonDSMP(module, p_data);
_dsm_send(mod_st, buffer, p_data - buffer);
}
static void dsmpProcessData(void* ctx, uint8_t data, uint8_t* buffer, uint8_t* len)
{
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
processSpektrumTelemetryData(module, data, buffer, *len);
}
// No telemetry
const etx_proto_driver_t DSM2Driver = {
.protocol = PROTOCOL_CHANNELS_DSM2_LP45,
.init = dsm2Init,
.deinit = dsmDeInit,
.sendPulses = dsm2SendPulses,
.processData = nullptr,
};
const etx_proto_driver_t DSMPDriver = {
.protocol = PROTOCOL_CHANNELS_DSMP,
.init = dsmpInit,
.deinit = dsmDeInit,
.sendPulses = dsmpSendPulses,
.processData = dsmpProcessData,
};
+30
View File
@@ -0,0 +1,30 @@
/*
* Copyright (C) EdgeTx
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* 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.
*/
#pragma once
#include "hal/module_driver.h"
// DSM2/DSMX
extern const etx_proto_driver_t DSM2Driver;
// Lemon RX DSMP
extern const etx_proto_driver_t DSMPDriver;
+115 -206
View File
@@ -116,11 +116,15 @@ struct rf_info_t {
uint8_t fw_state;
};
// static STRUCT_HALL rxBuffer = {0};
static uint32_t rfRxCount = 0;
static uint8_t lastState = STATE_IDLE;
static uint32_t set_loop_cnt = 0;
static uint8_t _flysky_state;
static uint8_t _flysky_frame_index;
static uint8_t _flysky_crc;
static uint8_t _flysky_timeout;
static uint8_t _esc_state;
uint32_t NV14internalModuleFwVersion = 0;
static rf_info_t rf_info = {
@@ -153,11 +157,7 @@ uint8_t emptyRxID[] = {0, 0, 0, 0};
void setFlyskyState(uint8_t state);
void onFlySkyBindReceiver();
void onFlySkyModuleSetPower(bool isPowerOn);
void afhds2Command(uint8_t type, uint8_t cmd);
void onFlySkyGetVersionInfoStart(uint8_t isRfTransfer);
// void usbDownloadTransmit(uint8_t *buffer, uint32_t size);
// extern uint32_t NV14internalModuleFwVersion;
void afhds2Command(uint8_t*& p_buf, uint8_t type, uint8_t cmd);
#define END 0xC0
#define ESC 0xDB
@@ -190,146 +190,85 @@ enum FlySkyModuleCommandID {
CMD_LAST
};
bool isFlySkyUsbDownload(void) { return rf_info.fw_state != 0; }
// void usbSetFrameTransmit(uint8_t packetID, uint8_t *dataBuf, uint32_t nBytes)
// {
// // send to host via usb
// uint8_t *pt = (uint8_t*)&rxBuffer;
// // rxBuffer.head = HALL_PROTOLO_HEAD;
// rxBuffer.hallID.hall_Id.packetID = packetID;//0x08;
// rxBuffer.hallID.hall_Id.senderID = 0x03;
// rxBuffer.hallID.hall_Id.receiverID = 0x02;
// if ( packetID == 0x08 ) {
// uint8_t fwVerision[40];
// for(uint32_t idx = 40; idx > 0; idx--)
// {
// if ( idx <= nBytes ) {
// fwVerision[idx-1] = dataBuf[idx-1];
// }
// else fwVerision[idx-1] = 0;
// }
// dataBuf = fwVerision;
// nBytes = 40;
// }
// rxBuffer.length = nBytes;
// TRACE_NOCRLF("\r\nToUSB: 55 %02X %02X ", rxBuffer.hallID.ID, nBytes);
// for ( uint32_t idx = 0; idx < nBytes; idx++ )
// {
// rxBuffer.data[idx] = dataBuf[idx];
// TRACE_NOCRLF("%02X ", rxBuffer.data[idx]);
// }
// #if !defined(SIMU)
// uint16_t checkSum = calc_crc16(pt, rxBuffer.length+3);
// TRACE(" CRC:%04X;", checkSum);
// pt[rxBuffer.length + 3] = checkSum & 0xFF;
// pt[rxBuffer.length + 4] = checkSum >> 8;
// usbDownloadTransmit(pt, rxBuffer.length + 5);
// #endif
// }
void onFlySkyModuleSetPower(bool isPowerOn)
void setFlyskyState(uint8_t state)
{
if (isPowerOn) {
INTERNAL_MODULE_ON();
resetPulsesAFHDS2();
} else {
moduleState[INTERNAL_MODULE].mode = MODULE_MODE_NORMAL;
INTERNAL_MODULE_OFF();
}
_flysky_state = state;
}
void setFlyskyState(uint8_t state) { intmodulePulsesData.flysky.state = state; }
void onFlySkyUsbDownloadStart(uint8_t fw_state) { rf_info.fw_state = fw_state; }
void onFlySkyGetVersionInfoStart(uint8_t isRfTransfer)
inline void initFlySkyCRC()
{
lastState = intmodulePulsesData.flysky.state;
setFlyskyState(isRfTransfer ? STATE_GET_RF_VERSION_INFO
: STATE_GET_RX_VERSION_INFO);
_flysky_crc = 0;
}
inline void initFlySkyArray()
{
intmodulePulsesData.flysky.ptr = intmodulePulsesData.flysky.pulses;
intmodulePulsesData.flysky.crc = 0;
}
inline void putFlySkyByte(uint8_t byte)
inline void putFlySkyByte(uint8_t*& p_buf, uint8_t byte)
{
if (END == byte) {
*intmodulePulsesData.flysky.ptr++ = ESC;
*intmodulePulsesData.flysky.ptr++ = ESC_END;
*p_buf++ = ESC;
*p_buf++ = ESC_END;
} else if (ESC == byte) {
*intmodulePulsesData.flysky.ptr++ = ESC;
*intmodulePulsesData.flysky.ptr++ = ESC_ESC;
*p_buf++ = ESC;
*p_buf++ = ESC_ESC;
} else {
*intmodulePulsesData.flysky.ptr++ = byte;
*p_buf++ = byte;
}
}
inline void putFlySkyFrameByte(uint8_t byte)
inline void putFlySkyFrameByte(uint8_t*& p_buf, uint8_t byte)
{
intmodulePulsesData.flysky.crc += byte;
putFlySkyByte(byte);
_flysky_crc += byte;
putFlySkyByte(p_buf, byte);
}
inline void putFlySkyFrameCmd(uint8_t type, uint8_t cmd)
inline void putFlySkyFrameCmd(uint8_t*& p_buf, uint8_t type, uint8_t cmd)
{
intmodulePulsesData.flysky.crc += type + cmd;
putFlySkyByte(type);
putFlySkyByte(cmd);
_flysky_crc += type + cmd;
putFlySkyByte(p_buf, type);
putFlySkyByte(p_buf, cmd);
}
inline void putFlySkyFrameBytes(uint8_t* data, int length)
inline void putFlySkyFrameBytes(uint8_t*& p_buf, uint8_t* data, int length)
{
for (int i = 0; i < length; i++) {
intmodulePulsesData.flysky.crc += data[i];
putFlySkyByte(data[i]);
_flysky_crc += data[i];
putFlySkyByte(p_buf, data[i]);
}
}
inline void putFlySkyFrameHeader()
inline void putFlySkyFrameHeader(uint8_t*& p_buf)
{
initFlySkyArray();
*intmodulePulsesData.flysky.ptr++ = END;
putFlySkyFrameByte(intmodulePulsesData.flysky.frame_index);
initFlySkyCRC();
*p_buf++ = END;
putFlySkyFrameByte(p_buf, _flysky_frame_index);
}
inline void putFlySkyFrameFooter()
inline void putFlySkyFrameFooter(uint8_t*& p_buf)
{
if (++intmodulePulsesData.flysky.frame_index == 0) {
intmodulePulsesData.flysky.frame_index = 1;
if (++_flysky_frame_index == 0) {
_flysky_frame_index = 1;
}
putFlySkyByte(intmodulePulsesData.flysky.crc ^ 0xff);
*intmodulePulsesData.flysky.ptr++ = END;
putFlySkyByte(p_buf, _flysky_crc ^ 0xff);
*p_buf++ = END;
}
void afhds2Command(uint8_t type, uint8_t cmd)
void afhds2Command(uint8_t*& p_buf, uint8_t type, uint8_t cmd)
{
putFlySkyFrameHeader();
putFlySkyFrameCmd(type, cmd);
putFlySkyFrameFooter();
putFlySkyFrameHeader(p_buf);
putFlySkyFrameCmd(p_buf, type, cmd);
putFlySkyFrameFooter(p_buf);
}
inline void putFlySkySendChannelData()
inline void putFlySkySendChannelData(uint8_t*& p_buf)
{
uint16_t pulseValue = 0;
uint8_t channels_start = g_model.moduleData[INTERNAL_MODULE].channelsStart;
uint8_t channels_last =
channels_start + 8 + g_model.moduleData[INTERNAL_MODULE].channelsCount;
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_NACK, CMD_SEND_CHANNEL_DATA);
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_NACK, CMD_SEND_CHANNEL_DATA);
if (moduleState[INTERNAL_MODULE].counter-- == 0) {
moduleState[INTERNAL_MODULE].counter = FAILSAVE_SEND_COUNTER_MAX;
putFlySkyFrameByte(FLYSKY_CHANNEL_DATA_FAILSAFE);
putFlySkyFrameByte(channels_last - channels_start);
putFlySkyFrameByte(p_buf, FLYSKY_CHANNEL_DATA_FAILSAFE);
putFlySkyFrameByte(p_buf, channels_last - channels_start);
for (uint8_t channel = channels_start; channel < channels_last; channel++) {
if (g_model.moduleData[INTERNAL_MODULE].failsafeMode == FAILSAFE_CUSTOM) {
int16_t failsafeValue = g_model.failsafeChannels[channel];
@@ -345,28 +284,28 @@ inline void putFlySkySendChannelData()
pulseValue =
limit<uint16_t>(0, 988 + ((failsafeValue + 1024) / 2), 0xfff);
}
putFlySkyFrameByte(pulseValue & 0xff);
putFlySkyFrameByte(pulseValue >> 8);
putFlySkyFrameByte(p_buf, pulseValue & 0xff);
putFlySkyFrameByte(p_buf, pulseValue >> 8);
}
if (DEBUG_RF_FRAME_PRINT & RF_FRAME_ONLY) {
TRACE("------FAILSAFE------");
}
} else {
putFlySkyFrameByte(FLYSKY_CHANNEL_DATA_NORMAL);
putFlySkyFrameByte(channels_last - channels_start);
putFlySkyFrameByte(p_buf, FLYSKY_CHANNEL_DATA_NORMAL);
putFlySkyFrameByte(p_buf, channels_last - channels_start);
for (uint8_t channel = channels_start; channel < channels_last; channel++) {
int channelValue =
channelOutputs[channel] + 2 * PPM_CH_CENTER(channel) - 2 * PPM_CENTER;
pulseValue = limit<uint16_t>(0, 988 + ((channelValue + 1024) / 2), 0xfff);
putFlySkyFrameByte(pulseValue & 0xff);
putFlySkyFrameByte(pulseValue >> 8);
putFlySkyFrameByte(p_buf, pulseValue & 0xff);
putFlySkyFrameByte(p_buf, pulseValue >> 8);
}
}
}
void putFlySkyUpdateFirmwareStart(uint8_t cmd)
void putFlySkyUpdateFirmwareStart(uint8_t*& p_buf, uint8_t cmd)
{
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, cmd);
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, cmd);
}
bool checkFlySkyFrameCrc(const uint8_t* ptr, uint8_t size)
@@ -419,17 +358,17 @@ inline void parseResponse(uint8_t* buffer, uint8_t dataLen)
if ((moduleState[INTERNAL_MODULE].mode != MODULE_MODE_BIND) &&
(resp->frame_type == FRAME_TYPE_ANSWER) &&
((intmodulePulsesData.flysky.frame_index - 1) != resp->frame_number)) {
((_flysky_frame_index - 1) != resp->frame_number)) {
return;
} else if (resp->frame_type == FRAME_TYPE_REQUEST_ACK) {
intmodulePulsesData.flysky.frame_index = resp->frame_number;
_flysky_frame_index = resp->frame_number;
}
debugFrame(&resp->command_id, dataLen - 2);
switch (resp->command_id) {
default:
if (moduleState[INTERNAL_MODULE].mode == MODULE_MODE_NORMAL &&
intmodulePulsesData.flysky.state >= STATE_IDLE) {
_flysky_state >= STATE_IDLE) {
setFlyskyState(STATE_SEND_CHANNELS);
if (DEBUG_RF_FRAME_PRINT & RF_FRAME_ONLY)
TRACE("State back to channel data");
@@ -467,12 +406,12 @@ inline void parseResponse(uint8_t* buffer, uint8_t dataLen)
}
case CMD_RF_GET_CONFIG:
setFlyskyState(STATE_GET_RECEIVER_CONFIG);
intmodulePulsesData.flysky.timeout = FLYSKY_MODULE_TIMEOUT;
_flysky_timeout = FLYSKY_MODULE_TIMEOUT;
break;
case CMD_RX_SENSOR_DATA:
flySkyNv14ProcessTelemetryPacket(&resp->value, dataLen - 3);
if (moduleState[INTERNAL_MODULE].mode == MODULE_MODE_NORMAL &&
intmodulePulsesData.flysky.state >= STATE_IDLE) {
_flysky_state >= STATE_IDLE) {
setFlyskyState(STATE_SEND_CHANNELS);
}
break;
@@ -510,7 +449,7 @@ inline void parseResponse(uint8_t* buffer, uint8_t dataLen)
break;
}
case CMD_GET_VERSION_INFO: {
if (intmodulePulsesData.flysky.state == STATE_GET_FW_VERSION_INIT) {
if (_flysky_state == STATE_GET_FW_VERSION_INIT) {
memcpy(&NV14internalModuleFwVersion, &resp->value + 1,
sizeof(NV14internalModuleFwVersion));
setFlyskyState(STATE_SET_RECEIVER_ID);
@@ -531,48 +470,17 @@ inline void parseResponse(uint8_t* buffer, uint8_t dataLen)
}
}
bool isrxBufferMsgOK(void)
{
bool isMsgOK = (0 != rfRxCount);
rfRxCount = 0;
return isMsgOK && isFlySkyUsbDownload();
}
void processInternalFlySkyTelemetryData(uint8_t byte, uint8_t* buffer, uint8_t* len)
{
// #if !defined(SIMU)
// parseFlyskyData(&rxBuffer, byte);
// #endif
// if (rxBuffer.valid) {
// rfRxCount++;
// rxBuffer.valid = 0;
// uint8_t *pt = (uint8_t*)&rxBuffer;
// //rxBuffer.head = HALL_PROTOLO_HEAD;
// pt[rxBuffer.length + 3] = rxBuffer.checkSum & 0xFF;
// pt[rxBuffer.length + 4] = rxBuffer.checkSum >> 8;
// if((DEBUG_RF_FRAME_PRINT & RF_FRAME_ONLY)) {
// TRACE("RF: %02X %02X %02X ...%04X; CRC:%04X", pt[0], pt[1], pt[2],
// rxBuffer.checkSum, calc_crc16(pt, rxBuffer.length+3));
// }
// //FW UPDATE done
// if (0x01 == rxBuffer.length && (0x05 == rxBuffer.data[0] || 0x06 ==
// rxBuffer.data[0])) {
// setFlyskyState(STATE_INIT);
// rf_info.fw_state = 0;
// }
// usbDownloadTransmit(pt, rxBuffer.length + 5);
// }
if (byte == END && *len > 0) {
parseResponse(buffer, *len);
*len = 0;
} else {
if (byte == ESC) {
intmodulePulsesData.flysky.esc_state = 1;
_esc_state = 1;
} else {
if (intmodulePulsesData.flysky.esc_state) {
intmodulePulsesData.flysky.esc_state = 0;
if (_esc_state) {
_esc_state = 0;
if (byte == ESC_END)
byte = END;
else if (byte == ESC_ESC)
@@ -590,10 +498,10 @@ void processInternalFlySkyTelemetryData(uint8_t byte, uint8_t* buffer, uint8_t*
void resetPulsesAFHDS2()
{
NV14internalModuleFwVersion = 0;
intmodulePulsesData.flysky.frame_index = 1;
_flysky_frame_index = 1;
setFlyskyState(STATE_SET_TX_POWER);
intmodulePulsesData.flysky.timeout = 0;
intmodulePulsesData.flysky.esc_state = 0;
_flysky_timeout = 0;
_esc_state = 0;
uint16_t rx_freq = gRomData.rx_freq[0];
rx_freq += (gRomData.rx_freq[1] * 256);
if (50 > rx_freq || 400 < rx_freq) {
@@ -601,41 +509,43 @@ void resetPulsesAFHDS2()
}
}
void setupPulsesAFHDS2()
void setupPulsesAFHDS2(uint8_t*& p_buf)
{
putFlySkyFrameHeader();
if (intmodulePulsesData.flysky.state == STATE_DISCONNECT) {
auto buffer_start = p_buf;
putFlySkyFrameHeader(p_buf);
if (_flysky_state == STATE_DISCONNECT) {
TRACE("STATE_DISCONNECT");
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, CMD_SET_RECEIVER_ID);
putFlySkyFrameBytes(emptyRxID, 4);
} else if (intmodulePulsesData.flysky.state < STATE_SEND_CHANNELS) {
if (++intmodulePulsesData.flysky.timeout >=
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, CMD_SET_RECEIVER_ID);
putFlySkyFrameBytes(p_buf, emptyRxID, 4);
} else if (_flysky_state < STATE_SEND_CHANNELS) {
if (++_flysky_timeout >=
FLYSKY_MODULE_TIMEOUT / FLYSKY_PERIOD) {
intmodulePulsesData.flysky.timeout = 0;
switch (intmodulePulsesData.flysky.state) {
_flysky_timeout = 0;
switch (_flysky_state) {
case STATE_INIT: {
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, CMD_RF_INIT);
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, CMD_RF_INIT);
} break;
case STATE_BIND: {
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, CMD_BIND);
putFlySkyFrameByte(rf_info.bind_power);
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, CMD_BIND);
putFlySkyFrameByte(p_buf, rf_info.bind_power);
uint32_t txID =
0; // internal module firmware does not allow changing TX id
putFlySkyFrameBytes((uint8_t*)(&txID), 4);
putFlySkyFrameBytes(p_buf, (uint8_t*)(&txID), 4);
} break;
case STATE_SET_RECEIVER_ID: {
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, CMD_SET_RECEIVER_ID);
putFlySkyFrameBytes(gRomData.rx_id, 4);
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, CMD_SET_RECEIVER_ID);
putFlySkyFrameBytes(p_buf, gRomData.rx_id, 4);
} break;
case STATE_GET_RECEIVER_CONFIG: {
putFlySkyFrameCmd(FRAME_TYPE_ANSWER, CMD_RF_GET_CONFIG);
putFlySkyFrameByte(AfhdsPwmMode); // 00:PWM, 01:PPM
putFlySkyFrameByte(AfhdsIbusMode); // 00:I-BUS, 01:S-BUS
putFlySkyFrameByte(
putFlySkyFrameCmd(p_buf, FRAME_TYPE_ANSWER, CMD_RF_GET_CONFIG);
putFlySkyFrameByte(p_buf, AfhdsPwmMode); // 00:PWM, 01:PPM
putFlySkyFrameByte(p_buf, AfhdsIbusMode); // 00:I-BUS, 01:S-BUS
putFlySkyFrameByte(p_buf,
gRomData.rx_freq[0] < 50
? 50
: gRomData.rx_freq[0]); // receiver servo freq bit[7:0]
putFlySkyFrameByte(
putFlySkyFrameByte(p_buf,
gRomData.rx_freq[1]); // receiver servo freq bit[15:8]
setFlyskyState(STATE_INIT);
} break;
@@ -644,12 +554,12 @@ void setupPulsesAFHDS2()
moduleState[INTERNAL_MODULE].mode == MODULE_MODE_RANGECHECK ? 0
: gRomData.rfPower ? 170
: 90;
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, CMD_SET_TX_POWER);
putFlySkyFrameByte(power);
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, CMD_SET_TX_POWER);
putFlySkyFrameByte(p_buf, power);
} break;
case STATE_SET_RANGE_TEST: {
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, CMD_TEST_RANGE);
putFlySkyFrameByte(moduleState[INTERNAL_MODULE].mode ==
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, CMD_TEST_RANGE);
putFlySkyFrameByte(p_buf, moduleState[INTERNAL_MODULE].mode ==
MODULE_MODE_RANGECHECK);
} break;
case STATE_RANGE_TEST_RUNNING: {
@@ -659,71 +569,70 @@ void setupPulsesAFHDS2()
}
} break;
case STATE_SET_RX_PWM_PPM: {
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, CMD_SET_RX_PWM_PPM);
putFlySkyFrameByte(AfhdsPwmMode); // 00:PWM, 01:PPM
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, CMD_SET_RX_PWM_PPM);
putFlySkyFrameByte(p_buf, AfhdsPwmMode); // 00:PWM, 01:PPM
} break;
case STATE_SET_RX_IBUS_SBUS: {
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, CMD_SET_RX_IBUS_SBUS);
putFlySkyFrameByte(AfhdsIbusMode); // 0x00:I-BUS, 0x01:S-BUS
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, CMD_SET_RX_IBUS_SBUS);
putFlySkyFrameByte(p_buf, AfhdsIbusMode); // 0x00:I-BUS, 0x01:S-BUS
} break;
case STATE_SET_RX_FREQUENCY: {
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, CMD_SET_RX_SERVO_FREQ);
putFlySkyFrameBytes(gRomData.rx_freq, 2);
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, CMD_SET_RX_SERVO_FREQ);
putFlySkyFrameBytes(p_buf, gRomData.rx_freq, 2);
} break;
case STATE_UPDATE_RF_PROTOCOL: {
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, CMD_SET_RF_PROTOCOL);
putFlySkyFrameByte(
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, CMD_SET_RF_PROTOCOL);
putFlySkyFrameByte(p_buf,
rf_info.protocol); // 0x00:AFHDS1 0x01:AFHDS2 0x02:AFHDS2A
} break;
case STATE_UPDATE_RX_FIRMWARE: {
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, CMD_UPDATE_RX_FIRMWARE);
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, CMD_UPDATE_RX_FIRMWARE);
} break;
case STATE_UPDATE_RF_FIRMWARE: {
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, CMD_UPDATE_RF_FIRMWARE);
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, CMD_UPDATE_RF_FIRMWARE);
} break;
case STATE_GET_RX_VERSION_INFO: {
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, CMD_GET_VERSION_INFO);
putFlySkyFrameByte(FLYSKY_RX_FIRMWARE);
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, CMD_GET_VERSION_INFO);
putFlySkyFrameByte(p_buf, FLYSKY_RX_FIRMWARE);
} break;
case STATE_GET_FW_VERSION_INIT:
case STATE_GET_RF_VERSION_INFO: {
putFlySkyFrameCmd(FRAME_TYPE_REQUEST_ACK, CMD_GET_VERSION_INFO);
putFlySkyFrameByte(FLYSKY_RF_FIRMWARE);
putFlySkyFrameCmd(p_buf, FRAME_TYPE_REQUEST_ACK, CMD_GET_VERSION_INFO);
putFlySkyFrameByte(p_buf, FLYSKY_RF_FIRMWARE);
} break;
case STATE_IDLE:
initFlySkyArray();
initFlySkyCRC();
break;
default:
setFlyskyState(STATE_INIT);
initFlySkyArray();
initFlySkyCRC();
if ((DEBUG_RF_FRAME_PRINT & TX_FRAME_ONLY)) {
TRACE("State back to INIT\r\n");
}
return;
}
} else {
initFlySkyArray();
initFlySkyCRC();
return;
}
} else {
if (moduleState[INTERNAL_MODULE].mode == MODULE_MODE_BIND)
moduleState[INTERNAL_MODULE].mode = MODULE_MODE_NORMAL;
putFlySkySendChannelData();
putFlySkySendChannelData(p_buf);
}
putFlySkyFrameFooter();
putFlySkyFrameFooter(p_buf);
if (intmodulePulsesData.flysky.state < STATE_SEND_CHANNELS) {
// uint8_t size = intmodulePulsesData.flysky.ptr -
// intmodulePulsesData.flysky.pulses;
// debugFrame(intmodulePulsesData.flysky.pulses, size);
if (_flysky_state < STATE_SEND_CHANNELS) {
// uint8_t size = p_buf - buffer_start;
// debugFrame(buffer_start, size);
}
if ((DEBUG_RF_FRAME_PRINT & TX_FRAME_ONLY)) {
/* print each command, except channel data by interval */
uint8_t* data = intmodulePulsesData.flysky.pulses;
uint8_t* data = buffer_start;
if (data[3] != CMD_SEND_CHANNEL_DATA || (set_loop_cnt++ % 100 == 0)) {
uint8_t size = intmodulePulsesData.flysky.ptr - data;
TRACE_NOCRLF("TX(State%0d)%0dB:", intmodulePulsesData.flysky.state, size);
uint8_t size = p_buf - data;
TRACE_NOCRLF("TX(State%0d)%0dB:", _flysky_state, size);
for (int idx = 0; idx < size; idx++) {
TRACE_NOCRLF(" %02X", data[idx]);
}
+1 -1
View File
@@ -23,7 +23,7 @@
#define INTMODULE_USART_AFHDS2_BAUDRATE 921600
void setupPulsesAFHDS2();
void setupPulsesAFHDS2(uint8_t*& p_buf);
void resetPulsesAFHDS2();
void processInternalFlySkyTelemetryData(uint8_t byte, uint8_t* buffer, uint8_t* len);
+135 -24
View File
@@ -20,10 +20,28 @@
*/
#include "opentx.h"
#include "ghost.h"
#include "telemetry/ghost.h"
#include "telemetry/ghost_menu.h"
#include "hal/module_port.h"
#include "mixer_scheduler.h"
uint8_t createGhostMenuControlFrame(uint8_t * frame, int16_t * pulses)
static uint8_t getGhostModuleAddr()
{
// TODO: check if asymmetric baudrate is really needed
// as it introduces a lot of clutter
//
// #if SPORT_MAX_BAUDRATE < 400000
// return g_model.moduleData[EXTERNAL_MODULE].ghost.telemetryBaudrate ==
// GHST_TELEMETRY_RATE_400K
// ? GHST_ADDR_MODULE_SYM
// : GHST_ADDR_MODULE_ASYM;
// #else
return GHST_ADDR_MODULE_SYM;
// #endif
}
static uint8_t createGhostMenuControlFrame(uint8_t * frame, int16_t * pulses)
{
uint8_t * buf = frame;
@@ -46,7 +64,7 @@ uint8_t createGhostMenuControlFrame(uint8_t * frame, int16_t * pulses)
}
// Range for pulses (channels output) is [-1024:+1024]
uint8_t createGhostChannelsFrame(uint8_t * frame, int16_t * pulses, bool raw12bits)
static uint8_t createGhostChannelsFrame(uint8_t * frame, int16_t * pulses, bool raw12bits)
{
static uint8_t lastGhostFrameId = 0;
uint8_t ghostUpper4Offset = 0;
@@ -88,10 +106,16 @@ uint8_t createGhostChannelsFrame(uint8_t * frame, int16_t * pulses, bool raw12bi
uint8_t bitsavailable = 0;
for (int i = 0; i < 4; i++) {
uint32_t value;
if(raw12bits) {
value = limit(0, (1024 + (pulses[i] + 2 * PPM_CH_CENTER(i) - 2 * PPM_CENTER)) << 1, 0xFFF);
if (raw12bits) {
value = limit(
0, (1024 + (pulses[i] + 2 * PPM_CH_CENTER(i) - 2 * PPM_CENTER)) << 1,
0xFFF);
} else {
value = limit(0, GHST_RC_CTR_VAL_12BIT + (((pulses[i] + 2 * PPM_CH_CENTER(i) - 2 * PPM_CENTER) << 3) / 5), 2 * GHST_RC_CTR_VAL_12BIT);
value = limit(
0,
GHST_RC_CTR_VAL_12BIT +
(((pulses[i] + 2 * PPM_CH_CENTER(i) - 2 * PPM_CENTER) << 3) / 5),
2 * GHST_RC_CTR_VAL_12BIT);
}
bits |= value << bitsavailable;
bitsavailable += GHST_CH_BITS_12;
@@ -106,10 +130,20 @@ uint8_t createGhostChannelsFrame(uint8_t * frame, int16_t * pulses, bool raw12bi
for (int i = 4; i < 8; ++i) {
uint8_t channelIndex = i + ghostUpper4Offset;
uint8_t value;
if(raw12bits) {
value = limit(0, 128 + ((pulses[channelIndex] + 2 * PPM_CH_CENTER(channelIndex) - 2 * PPM_CENTER) >> 3), 0xFF);
if (raw12bits) {
value = limit(0,
128 + ((pulses[channelIndex] +
2 * PPM_CH_CENTER(channelIndex) - 2 * PPM_CENTER) >>
3),
0xFF);
} else {
value = limit(0, GHST_RC_CTR_VAL_8BIT + (((pulses[channelIndex] + 2 * PPM_CH_CENTER(channelIndex) - 2 * PPM_CENTER) >> 1) / 5), 2 * GHST_RC_CTR_VAL_8BIT);
value = limit(0,
GHST_RC_CTR_VAL_8BIT +
(((pulses[channelIndex] +
2 * PPM_CH_CENTER(channelIndex) - 2 * PPM_CENTER) >>
1) /
5),
2 * GHST_RC_CTR_VAL_8BIT);
}
*buf++ = value;
}
@@ -120,25 +154,102 @@ uint8_t createGhostChannelsFrame(uint8_t * frame, int16_t * pulses, bool raw12bi
return buf - frame;
}
void setupPulsesGhost()
static const etx_serial_init ghostSerialParams = {
.baudrate = GHOST_BAUDRATE,
.encoding = ETX_Encoding_8N1,
.direction = ETX_Dir_TX_RX,
.polarity = ETX_Pol_Normal,
};
static void* ghostInit(uint8_t module)
{
if (telemetryProtocol == PROTOCOL_TELEMETRY_GHOST) {
auto mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &ghostSerialParams);
if (!mod_st) return nullptr;
auto &module = g_model.moduleData[EXTERNAL_MODULE];
auto *p_data = &extmodulePulsesData.ghost;
mixerSchedulerSetPeriod(module, GHOST_PERIOD);
return mod_st;
}
#if defined(LUA)
if (outputTelemetryBuffer.destination == TELEMETRY_ENDPOINT_SPORT) {
memcpy(p_data->pulses, outputTelemetryBuffer.data, outputTelemetryBuffer.size);
p_data->length = outputTelemetryBuffer.size;
outputTelemetryBuffer.reset();
} else
#endif
if (moduleState[EXTERNAL_MODULE].counter == GHST_MENU_CONTROL) {
p_data->length = createGhostMenuControlFrame(p_data->pulses, &channelOutputs[module.channelsStart]);
} else {
p_data->length = createGhostChannelsFrame(p_data->pulses, &channelOutputs[module.channelsStart], module.ghost.raw12bits);
static void ghostDeInit(void* ctx)
{
auto mod_st = (etx_module_state_t*)ctx;
modulePortDeInit(mod_st);
}
static void ghostSendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_t nChannels)
{
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
ModuleSyncStatus& status = getModuleSyncStatus(module);
if (status.isValid()) {
mixerSchedulerSetPeriod(module, status.getAdjustedRefreshRate());
} else {
mixerSchedulerSetPeriod(module, GHOST_PERIOD);
}
const auto& mod_cfg = g_model.moduleData[module].ghost;
auto p_data = buffer;
#if defined(LUA)
if (outputTelemetryBuffer.destination == TELEMETRY_ENDPOINT_SPORT) {
auto len = outputTelemetryBuffer.size;
auto f_data = outputTelemetryBuffer.data;
while (len >= 12) { // type(1B) + payload(10B) + crc(1B)
// insert address byte
*p_data++ = getGhostModuleAddr();
// and length
*p_data++ = 12;
memcpy(p_data, f_data, 12);
p_data += 12; f_data += 12;
len -= 12;
}
outputTelemetryBuffer.reset();
} else
#endif
if (moduleState[module].counter == GHST_MENU_CONTROL) {
p_data += createGhostMenuControlFrame(p_data, channels);
} else {
p_data += createGhostChannelsFrame(p_data, channels, mod_cfg.raw12bits);
}
moduleState[module].counter = GHST_FRAME_CHANNEL;
auto drv = modulePortGetSerialDrv(mod_st->tx);
auto drv_ctx = modulePortGetCtx(mod_st->tx);
drv->sendBuffer(drv_ctx, buffer, p_data - buffer);
}
static void ghostProcessData(void* ctx, uint8_t data, uint8_t* buffer, uint8_t* len)
{
if (*len == 0 && data != GHST_ADDR_RADIO) {
TRACE("[GH] address 0x%02X error", data);
return;
}
if (*len < TELEMETRY_RX_PACKET_SIZE) {
buffer[(*len)++] = data;
}
else {
TRACE("[GH] array size %d error", *len);
*len = 0;
}
if (*len > 4) {
uint8_t length = buffer[1];
if (length + 2 == *len) {
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
processGhostTelemetryFrame(module, buffer, *len);
*len = 0;
}
moduleState[EXTERNAL_MODULE].counter = GHST_FRAME_CHANNEL;
}
}
const etx_proto_driver_t GhostDriver = {
.protocol = PROTOCOL_CHANNELS_GHOST,
.init = ghostInit,
.deinit = ghostDeInit,
.sendPulses = ghostSendPulses,
.processData = ghostProcessData,
};
+26
View File
@@ -0,0 +1,26 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* 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.
*/
#pragma once
#include "hal/module_driver.h"
extern const etx_proto_driver_t GhostDriver;
+6 -17
View File
@@ -43,6 +43,10 @@ extern uint32_t NV14internalModuleFwVersion;
#define CROSSFIRE_CHANNELS_COUNT 16
#define GHOST_CHANNELS_COUNT 16
#define IS_NATIVE_FRSKY_PROTOCOL(module) \
((moduleState[module].protocol == PROTOCOL_CHANNELS_PXX1) || \
(moduleState[module].protocol == PROTOCOL_CHANNELS_PXX2))
#if defined (MULTIMODULE)
#define IS_D16_MULTI(module) \
((g_model.moduleData[module].multi.rfProtocol == MODULE_SUBTYPE_MULTI_FRSKYX) || \
@@ -71,32 +75,15 @@ extern uint32_t NV14internalModuleFwVersion;
(g_model.moduleData[module].multi.rfProtocol == \
MODULE_SUBTYPE_MULTI_DSM_RX))
#if defined(HARDWARE_INTERNAL_MODULE)
#define IS_FRSKY_SPORT_PROTOCOL() \
(telemetryProtocol == PROTOCOL_TELEMETRY_FRSKY_SPORT || \
(telemetryProtocol == PROTOCOL_TELEMETRY_MULTIMODULE && \
(IS_D16_MULTI(INTERNAL_MODULE) || IS_D16_MULTI(EXTERNAL_MODULE) || \
IS_R9_MULTI(INTERNAL_MODULE) || IS_R9_MULTI(EXTERNAL_MODULE))))
#else
#define IS_FRSKY_SPORT_PROTOCOL() \
(telemetryProtocol == PROTOCOL_TELEMETRY_FRSKY_SPORT || \
(telemetryProtocol == PROTOCOL_TELEMETRY_MULTIMODULE && \
(IS_D16_MULTI(EXTERNAL_MODULE) || IS_R9_MULTI(EXTERNAL_MODULE))))
#endif
#else
#define IS_D16_MULTI(module) false
#define IS_R9_MULTI(module) false
#define IS_HOTT_MULTI(module) false
#define IS_CONFIG_MULTI(module) false
#define IS_DSM_MULTI(module) false
#define IS_FRSKY_SPORT_PROTOCOL() (telemetryProtocol == PROTOCOL_TELEMETRY_FRSKY_SPORT)
#define IS_RX_MULTI(module) false
#endif
#define IS_SPEKTRUM_PROTOCOL() (telemetryProtocol == PROTOCOL_TELEMETRY_SPEKTRUM)
#if defined(MULTIMODULE)
// When using packed, the pointer in here end up not being aligned, which clang and gcc complain about
// Keep the order of the fields that the so that the size stays small
@@ -726,6 +713,7 @@ inline bool isTelemAllowedOnBind(uint8_t moduleIndex)
return false;
#endif
#if defined(HARDWARE_EXTERNAL_MODULE)
if (g_model.moduleData[EXTERNAL_MODULE].type == MODULE_TYPE_R9M_LITE_PXX1) {
if (isModuleR9M_LBT(EXTERNAL_MODULE))
return g_model.moduleData[EXTERNAL_MODULE].pxx.power < R9M_LITE_LBT_POWER_100_16CH_NOTELEM;
@@ -739,6 +727,7 @@ inline bool isTelemAllowedOnBind(uint8_t moduleIndex)
else
return true;
}
#endif
return true;
}
+156 -153
View File
@@ -41,37 +41,28 @@
#define MULTI_FAILSAFE 0x01
#define MULTI_DATA 0x02
static void sendFrameProtocolHeader(uint8_t moduleIdx, bool failsafe);
void sendChannels(uint8_t moduleIdx);
static void sendD16BindOption(uint8_t moduleIdx);
static void sendFrameProtocolHeader(uint8_t*& p_buf, uint8_t module, bool failsafe);
static void sendChannels(uint8_t*& p_buf, uint8_t module);
static void sendD16BindOption(uint8_t*& p_buf, uint8_t module);
#if defined(LUA)
static void sendSport(uint8_t moduleIdx);
static void sendHott(uint8_t moduleIdx);
static void sendConfig(uint8_t moduleIdx);
static void sendDSM(uint8_t moduleIdx);
static void sendSport(uint8_t*& p_buf, uint8_t module);
static void sendHott(uint8_t*& p_buf, uint8_t module);
static void sendConfig(uint8_t*& p_buf, uint8_t module);
static void sendDSM(uint8_t*& p_buf, uint8_t module);
#endif
#if defined(INTMODULE_USART) && defined(INTERNAL_MODULE_MULTI)
#include "intmodule_serial_driver.h"
#include "hal/module_port.h"
etx_serial_init multiSerialInitParams = {
.baudrate = MULTIMODULE_BAUDRATE,
.parity = ETX_Parity_Even,
.stop_bits = ETX_StopBits_Two,
.word_length = ETX_WordLength_9,
.rx_enable = true,
.encoding = ETX_Encoding_8E2,
.direction = ETX_Dir_TX,
.polarity = ETX_Pol_Normal,
};
#endif
static void sendMulti(uint8_t moduleIdx, uint8_t b)
static inline void sendMulti(uint8_t*& p_buf, uint8_t b)
{
#if defined(INTERNAL_MODULE_MULTI)
if (moduleIdx == INTERNAL_MODULE) {
intmodulePulsesData.multi.sendByte(b);
}
else
#endif
sendByteSbus(b);
*p_buf++ = b;
}
static void updateMultiSync(uint8_t module)
@@ -88,7 +79,7 @@ static void updateMultiSync(uint8_t module)
}
}
static void sendFailsafeChannels(uint8_t moduleIdx)
static void sendFailsafeChannels(uint8_t*& p_buf, uint8_t module)
{
uint32_t bits = 0;
uint8_t bitsavailable = 0;
@@ -97,16 +88,16 @@ static void sendFailsafeChannels(uint8_t moduleIdx)
int16_t failsafeValue = g_model.failsafeChannels[i];
int pulseValue;
if (g_model.moduleData[moduleIdx].failsafeMode == FAILSAFE_HOLD ||
if (g_model.moduleData[module].failsafeMode == FAILSAFE_HOLD ||
failsafeValue == FAILSAFE_CHANNEL_HOLD) {
pulseValue = 2047;
} else if (g_model.moduleData[moduleIdx].failsafeMode ==
} else if (g_model.moduleData[module].failsafeMode ==
FAILSAFE_NOPULSES ||
failsafeValue == FAILSAFE_CHANNEL_NOPULSE) {
pulseValue = 0;
} else {
failsafeValue +=
2 * PPM_CH_CENTER(g_model.moduleData[moduleIdx].channelsStart + i) -
2 * PPM_CH_CENTER(g_model.moduleData[module].channelsStart + i) -
2 * PPM_CENTER;
pulseValue = limit(1, (failsafeValue * 800 / 1000) + 1024, 2046);
}
@@ -114,14 +105,14 @@ static void sendFailsafeChannels(uint8_t moduleIdx)
bits |= pulseValue << bitsavailable;
bitsavailable += MULTI_CHAN_BITS;
while (bitsavailable >= 8) {
sendMulti(moduleIdx, (uint8_t) (bits & 0xff));
sendMulti(p_buf, (uint8_t) (bits & 0xff));
bits >>= 8;
bitsavailable -= 8;
}
}
}
void setupPulsesMulti(uint8_t moduleIdx)
static void setupPulsesMulti(uint8_t*& p_buf, uint8_t module)
{
static int counter[2] = {0,0}; //TODO
static uint8_t invert[2] = {0x00, //internal
@@ -133,165 +124,177 @@ void setupPulsesMulti(uint8_t moduleIdx)
};
uint8_t type=MULTI_NORMAL;
updateMultiSync(moduleIdx);
updateMultiSync(module);
// not scanning protos && not spectrum analyser
if (getModuleMode(moduleIdx) == MODULE_MODE_NORMAL) {
if (getModuleMode(module) == MODULE_MODE_NORMAL) {
// Failsafe packets
if (counter[moduleIdx] % 1000 == 0 &&
g_model.moduleData[moduleIdx].failsafeMode != FAILSAFE_NOT_SET &&
g_model.moduleData[moduleIdx].failsafeMode != FAILSAFE_RECEIVER) {
if (counter[module] % 1000 == 0 &&
g_model.moduleData[module].failsafeMode != FAILSAFE_NOT_SET &&
g_model.moduleData[module].failsafeMode != FAILSAFE_RECEIVER) {
type |= MULTI_FAILSAFE;
}
counter[moduleIdx]++;
counter[module]++;
}
// Invert telemetry if needed
if (invert[moduleIdx] & 0x80 &&
!g_model.moduleData[moduleIdx].multi.disableTelemetry) {
if (getMultiModuleStatus(moduleIdx).isValid()) {
invert[moduleIdx] &= 0x08; // Telemetry received, stop searching
} else if (counter[moduleIdx] % 100 == 0) {
invert[moduleIdx] ^= 0x08; // Try inverting telemetry
if (invert[module] & 0x80 &&
!g_model.moduleData[module].multi.disableTelemetry) {
if (getMultiModuleStatus(module).isValid()) {
invert[module] &= 0x08; // Telemetry received, stop searching
} else if (counter[module] % 100 == 0) {
invert[module] ^= 0x08; // Try inverting telemetry
}
}
// Send header
sendFrameProtocolHeader(moduleIdx, type & MULTI_FAILSAFE);
sendFrameProtocolHeader(p_buf, module, type & MULTI_FAILSAFE);
// Send channels
if (type & MULTI_FAILSAFE)
sendFailsafeChannels(moduleIdx);
sendFailsafeChannels(p_buf, module);
else
sendChannels(moduleIdx);
sendChannels(p_buf, module);
// Multi V1.3.X.X -> Send byte 26, Protocol (bits 7 & 6), RX_Num (bits 5 & 4), invert, not used, disable telemetry, disable mapping
if ((moduleState[moduleIdx].mode == MODULE_MODE_SPECTRUM_ANALYSER)
if (moduleState[module].mode == MODULE_MODE_SPECTRUM_ANALYSER
#if defined(MULTI_PROTOLIST)
|| (moduleState[moduleIdx].mode == MODULE_MODE_GET_HARDWARE_INFO)
|| moduleState[module].mode == MODULE_MODE_GET_HARDWARE_INFO
#endif
) {
sendMulti(moduleIdx, invert[moduleIdx] & 0x08);
sendMulti(p_buf, invert[module] & 0x08);
}
else {
sendMulti(moduleIdx, (uint8_t) (((g_model.moduleData[moduleIdx].multi.rfProtocol + 3) & 0xC0)
| (g_model.header.modelId[moduleIdx] & 0x30)
| (invert[moduleIdx] & 0x08)
//| 0x04 // Future use
| (g_model.moduleData[moduleIdx].multi.disableTelemetry << 1)
| g_model.moduleData[moduleIdx].multi.disableMapping));
sendMulti(p_buf, (uint8_t) (((g_model.moduleData[module].multi.rfProtocol + 3) & 0xC0)
| (g_model.header.modelId[module] & 0x30)
| (invert[module] & 0x08)
//| 0x04 // Future use
| (g_model.moduleData[module].multi.disableTelemetry << 1)
| g_model.moduleData[module].multi.disableMapping));
}
// Multi V1.3.X.X -> Send protocol additional data: max 9 bytes
if (getMultiModuleStatus(moduleIdx).isValid()) {
MultiModuleStatus &status = getMultiModuleStatus(moduleIdx);
if (getMultiModuleStatus(module).isValid()) {
MultiModuleStatus &status = getMultiModuleStatus(module);
if (status.minor >= 3 && !(status.flags & 0x80)) { //Version 1.3.x.x or more and Buffer not full
if ((IS_D16_MULTI(moduleIdx) || IS_R9_MULTI(moduleIdx)) && moduleState[moduleIdx].mode == MODULE_MODE_BIND) {
sendD16BindOption(moduleIdx);//1 byte of additional data
if ((IS_D16_MULTI(module) || IS_R9_MULTI(module)) && moduleState[module].mode == MODULE_MODE_BIND) {
sendD16BindOption(p_buf, module);//1 byte of additional data
}
#if defined(LUA)
// SPort send
if (IS_D16_MULTI(moduleIdx) && outputTelemetryBuffer.destination == TELEMETRY_ENDPOINT_SPORT && outputTelemetryBuffer.size) {
sendSport(moduleIdx); //8 bytes of additional data
if (IS_D16_MULTI(module) && outputTelemetryBuffer.destination == TELEMETRY_ENDPOINT_SPORT && outputTelemetryBuffer.size) {
sendSport(p_buf, module); //8 bytes of additional data
}
else if (IS_HOTT_MULTI(moduleIdx)) {
sendHott(moduleIdx); //1 byte of additional data
else if (IS_HOTT_MULTI(module)) {
sendHott(p_buf, module); //1 byte of additional data
}
else if (IS_CONFIG_MULTI(moduleIdx)) {
sendConfig(moduleIdx); //7 bytes of additional data
else if (IS_CONFIG_MULTI(module)) {
sendConfig(p_buf, module); //7 bytes of additional data
}
else if (IS_DSM_MULTI(moduleIdx)) {
sendDSM(moduleIdx); //7 bytes of additional data
else if (IS_DSM_MULTI(module)) {
sendDSM(p_buf, module); //7 bytes of additional data
}
#endif
}
}
}
void setupPulsesMultiExternalModule()
{
extmodulePulsesData.dsm2.index = 0;
extmodulePulsesData.dsm2.ptr = extmodulePulsesData.dsm2.pulses;
setupPulsesMulti(EXTERNAL_MODULE);
putDsm2Flush();
}
#if defined(INTERNAL_MODULE_MULTI)
static void* multiInit(uint8_t module)
{
(void)module;
// serial port setup
intmodulePulsesData.multi.initFrame();
void* uart_ctx = IntmoduleSerialDriver.init(&multiSerialInitParams);
etx_module_state_t* mod_st = nullptr;
etx_serial_init cfg(multiSerialInitParams);
#if defined(INTERNAL_MODULE_MULTI)
if (module == INTERNAL_MODULE) {
// serial port setup
// TODO: error handling
cfg.direction = ETX_Dir_TX_RX;
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &cfg);
}
#endif
#if defined(HARDWARE_EXTERNAL_MODULE)
if (module == EXTERNAL_MODULE) {
// serial port setup
cfg.direction = ETX_Dir_TX;
cfg.polarity = ETX_Pol_Inverted;
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &cfg);
if (!mod_st) {
cfg.polarity = ETX_Pol_Normal;
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, &cfg);
}
// no TX port: bail out
if (!mod_st) return nullptr;
// Init S.PORT RX channel
cfg.direction = ETX_Dir_RX;
cfg.polarity = ETX_Pol_Normal;
modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &cfg);
}
#endif
if (!mod_st) return nullptr;
// mixer setup
mixerSchedulerSetPeriod(INTERNAL_MODULE, MULTIMODULE_PERIOD);
INTERNAL_MODULE_ON();
mixerSchedulerSetPeriod(module, MULTIMODULE_PERIOD);
// reset status
getMultiModuleStatus(INTERNAL_MODULE).failsafeChecked = false;
getMultiModuleStatus(INTERNAL_MODULE).flags = 0;
getMultiModuleStatus(module).failsafeChecked = false;
getMultiModuleStatus(module).flags = 0;
#if defined(MULTI_PROTOLIST)
TRACE("enablePulsesInternalModule(): trigger scan");
MultiRfProtocols::instance(INTERNAL_MODULE)->triggerScan();
TRACE("counter = %d", moduleState[INTERNAL_MODULE].counter);
MultiRfProtocols::instance(module)->triggerScan();
#endif
return uart_ctx;
return mod_st;
}
static void multiDeInit(void* context)
static void multiDeInit(void* ctx)
{
INTERNAL_MODULE_OFF();
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
IntmoduleSerialDriver.deinit(context);
auto mod_st = (etx_module_state_t*)ctx;
modulePortDeInit(mod_st);
}
static void multiSetupPulses(void* context, int16_t* channels, uint8_t nChannels)
static void multiSendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_t nChannels)
{
(void)context;
// TODO:
(void)channels;
(void)nChannels;
intmodulePulsesData.multi.initFrame();
setupPulsesMulti(INTERNAL_MODULE);
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
auto data = buffer;
setupPulsesMulti(data, module);
auto drv = modulePortGetSerialDrv(mod_st->tx);
auto drv_ctx = modulePortGetCtx(mod_st->tx);
drv->sendBuffer(drv_ctx, buffer, data - buffer);
}
static void multiSendPulses(void* context)
static void multiProcessData(void* ctx, uint8_t data, uint8_t* buffer, uint8_t* len)
{
IntmoduleSerialDriver.sendBuffer(context, intmodulePulsesData.multi.getData(),
intmodulePulsesData.multi.getSize());
}
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
static int multiGetByte(void* context, uint8_t* data)
{
return IntmoduleSerialDriver.getByte(context, data);
}
static void multiProcessData(void* context, uint8_t data, uint8_t* buffer, uint8_t* len)
{
processMultiTelemetryData(data, INTERNAL_MODULE);
processMultiTelemetryData(data, module);
}
#include "hal/module_driver.h"
const etx_module_driver_t MultiInternalDriver = {
const etx_proto_driver_t MultiDriver = {
.protocol = PROTOCOL_CHANNELS_MULTIMODULE,
.init = multiInit,
.deinit = multiDeInit,
.setupPulses = multiSetupPulses,
.sendPulses = multiSendPulses,
.getByte = multiGetByte,
.processData = multiProcessData,
};
#endif
void sendChannels(uint8_t moduleIdx)
static void sendChannels(uint8_t*& p_buf, uint8_t module)
{
uint32_t bits = 0;
uint8_t bitsavailable = 0;
@@ -300,7 +303,7 @@ void sendChannels(uint8_t moduleIdx)
// Range for pulses (channelsOutputs) is [-1024:+1024] for [-100%;100%]
// Multi uses [204;1843] as [-100%;100%]
for (int i = 0; i < MULTI_CHANS; i++) {
int channel = g_model.moduleData[moduleIdx].channelsStart + i;
int channel = g_model.moduleData[module].channelsStart + i;
int value = channelOutputs[channel] + 2 * PPM_CH_CENTER(channel) - 2 * PPM_CENTER;
// Scale to 80%
@@ -310,42 +313,42 @@ void sendChannels(uint8_t moduleIdx)
bits |= value << bitsavailable;
bitsavailable += MULTI_CHAN_BITS;
while (bitsavailable >= 8) {
sendMulti(moduleIdx, (uint8_t) (bits & 0xff));
sendMulti(p_buf, (uint8_t) (bits & 0xff));
bits >>= 8;
bitsavailable -= 8;
}
}
}
void sendFrameProtocolHeader(uint8_t moduleIdx, bool failsafe)
void sendFrameProtocolHeader(uint8_t*& p_buf, uint8_t module, bool failsafe)
{// byte 1+2, protocol information
// Our enumeration starts at 0
int type = g_model.moduleData[moduleIdx].multi.rfProtocol + 1;
int subtype = g_model.moduleData[moduleIdx].subType;
int8_t optionValue = g_model.moduleData[moduleIdx].multi.optionValue;
int type = g_model.moduleData[module].multi.rfProtocol + 1;
int subtype = g_model.moduleData[module].subType;
int8_t optionValue = g_model.moduleData[module].multi.optionValue;
uint8_t protoByte = 0;
uint8_t moduleMode = getModuleMode(moduleIdx);
uint8_t moduleMode = getModuleMode(module);
if (moduleMode == MODULE_MODE_SPECTRUM_ANALYSER) {
sendMulti(moduleIdx, (uint8_t) 0x54); // Header byte
sendMulti(moduleIdx, (uint8_t) 54); // Spectrum custom protocol
sendMulti(moduleIdx, (uint8_t) 0);
sendMulti(moduleIdx, (uint8_t) 0);
sendMulti(p_buf, (uint8_t) 0x54); // Header byte
sendMulti(p_buf, (uint8_t) 54); // Spectrum custom protocol
sendMulti(p_buf, (uint8_t) 0);
sendMulti(p_buf, (uint8_t) 0);
return;
}
#if defined(MULTI_PROTOLIST)
if (moduleMode == MODULE_MODE_GET_HARDWARE_INFO) {
sendMulti(moduleIdx, (uint8_t) 0x55); // Header byte
sendMulti(moduleIdx, (uint8_t) 0); // PROTOLIST custom protocol
sendMulti(moduleIdx, (uint8_t) 0);
sendMulti(p_buf, (uint8_t) 0x55); // Header byte
sendMulti(p_buf, (uint8_t) 0); // PROTOLIST custom protocol
sendMulti(p_buf, (uint8_t) 0);
// proto array item
uint8_t protoIdx = MultiRfProtocols::instance(moduleIdx)->getScanProto();
uint8_t protoIdx = MultiRfProtocols::instance(module)->getScanProto();
TRACE("scan [%d]", protoIdx);
sendMulti(moduleIdx, protoIdx);
sendMulti(p_buf, protoIdx);
return;
}
#endif
@@ -362,19 +365,19 @@ void sendFrameProtocolHeader(uint8_t moduleIdx, bool failsafe)
optionValue = 0x80; // Max throw
else
optionValue = 0x00;
if (g_model.moduleData[moduleIdx].multi.optionValue & 0x02)
if (g_model.moduleData[module].multi.optionValue & 0x02)
optionValue |= 0x40; // 11ms servo refresh
optionValue |= sentModuleChannels(moduleIdx); //add number of channels
optionValue |= sentModuleChannels(module); //add number of channels
}
// Set the highest bit of option byte in AFHDS2A protocol to instruct MULTI to passthrough telemetry bytes instead
// of sending Frsky D telemetry
if (g_model.moduleData[moduleIdx].multi.rfProtocol == MODULE_SUBTYPE_MULTI_FS_AFHDS2A)
if (g_model.moduleData[module].multi.rfProtocol == MODULE_SUBTYPE_MULTI_FS_AFHDS2A)
optionValue = optionValue | 0x80;
// For custom protocol send unmodified type byte
if (g_model.moduleData[moduleIdx].multi.rfProtocol == MM_RF_CUSTOM_SELECTED)
type = g_model.moduleData[moduleIdx].multi.rfProtocol;
if (g_model.moduleData[module].multi.rfProtocol == MM_RF_CUSTOM_SELECTED)
type = g_model.moduleData[module].multi.rfProtocol;
uint8_t headerByte = 0x55;
// header, byte 0, 0x55 for proto 0-31, 0x54 for proto 32-63
@@ -384,34 +387,34 @@ void sendFrameProtocolHeader(uint8_t moduleIdx, bool failsafe)
if (failsafe)
headerByte |= 0x02;
sendMulti(moduleIdx, headerByte);
sendMulti(p_buf, headerByte);
// protocol byte
protoByte |= (type & 0x1f);
if (g_model.moduleData[moduleIdx].multi.rfProtocol != MODULE_SUBTYPE_MULTI_DSM2)
protoByte |= (g_model.moduleData[moduleIdx].multi.autoBindMode << 6);
if (g_model.moduleData[module].multi.rfProtocol != MODULE_SUBTYPE_MULTI_DSM2)
protoByte |= (g_model.moduleData[module].multi.autoBindMode << 6);
sendMulti(moduleIdx, protoByte);
sendMulti(p_buf, protoByte);
// byte 2, subtype, powermode, model id
sendMulti(moduleIdx, (uint8_t) ((g_model.header.modelId[moduleIdx] & 0x0f)
sendMulti(p_buf, (uint8_t) ((g_model.header.modelId[module] & 0x0f)
| ((subtype & 0x7) << 4)
| (g_model.moduleData[moduleIdx].multi.lowPowerMode << 7))
| (g_model.moduleData[module].multi.lowPowerMode << 7))
);
// byte 3
sendMulti(moduleIdx, (uint8_t) optionValue);
sendMulti(p_buf, (uint8_t) optionValue);
}
void sendD16BindOption(uint8_t moduleIdx)
void sendD16BindOption(uint8_t*& p_buf, uint8_t module)
{
uint8_t bind_opt = g_model.moduleData[moduleIdx].multi.receiverTelemetryOff ? 1 : 0;
bind_opt |= g_model.moduleData[moduleIdx].multi.receiverHigherChannels ? 2 : 0;
sendMulti(moduleIdx, bind_opt);
uint8_t bind_opt = g_model.moduleData[module].multi.receiverTelemetryOff ? 1 : 0;
bind_opt |= g_model.moduleData[module].multi.receiverHigherChannels ? 2 : 0;
sendMulti(p_buf, bind_opt);
}
#if defined(LUA)
void sendSport(uint8_t moduleIdx)
void sendSport(uint8_t*& p_buf, uint8_t module)
{
// example: B7 30 30 0C 80 00 00 00 13
uint8_t j=0;
@@ -420,25 +423,25 @@ void sendSport(uint8_t moduleIdx)
for (uint8_t i = 0; i < outputTelemetryBuffer.size - 1 && j < 8; i++, j++) {
if (outputTelemetryBuffer.data[i] == BYTE_STUFF) {
i++;
sendMulti(moduleIdx, outputTelemetryBuffer.data[i] ^ STUFF_MASK);
sendMulti(p_buf, outputTelemetryBuffer.data[i] ^ STUFF_MASK);
}
else {
sendMulti(moduleIdx, outputTelemetryBuffer.data[i]);
sendMulti(p_buf, outputTelemetryBuffer.data[i]);
}
}
outputTelemetryBuffer.reset(); // empty buffer
}
void sendHott(uint8_t moduleIdx)
void sendHott(uint8_t*& p_buf, uint8_t module)
{
if (Multi_Buffer && memcmp(Multi_Buffer, "HoTT", 4) == 0 && (Multi_Buffer[5] & 0x80) && (Multi_Buffer[5] & 0x0F) >= 0x07) {
// HoTT Lua script is running
sendMulti(moduleIdx, Multi_Buffer[5]);
sendMulti(p_buf, Multi_Buffer[5]);
}
}
void sendConfig(uint8_t moduleIdx)
void sendConfig(uint8_t*& p_buf, uint8_t module)
{
// Multi_Buffer[0..3]=="Conf" -> Lua script is running
// Multi_Buffer[4]==0x01 -> TX to Module data ready to be sent
@@ -449,13 +452,13 @@ void sendConfig(uint8_t moduleIdx)
if (Multi_Buffer && memcmp(Multi_Buffer, "Conf", 4) == 0 && Multi_Buffer[4] == 0x01) {
// Config Lua script is running and sending
for (uint8_t i = 0; i < 7; i++) {
sendMulti(moduleIdx, Multi_Buffer[5 + i]);
sendMulti(p_buf, Multi_Buffer[5 + i]);
}
Multi_Buffer[4] = 0x00; // Send data only once
}
}
void sendDSM(uint8_t moduleIdx)
void sendDSM(uint8_t*& p_buf, uint8_t module)
{
// Multi_Buffer[0..2]=="DSM" -> Lua script is running
// Multi_Buffer[3]==0x70 + len -> TX to RX data ready to be sent
@@ -463,7 +466,7 @@ void sendDSM(uint8_t moduleIdx)
// Multi_Buffer[10..25]=16 bytes of RX to TX data
if (Multi_Buffer && memcmp(Multi_Buffer, "DSM", 3) == 0 && (Multi_Buffer[3] & 0xF8) == 0x70) {
for(uint8_t i = 0; i < 7; i++) {
sendMulti(moduleIdx, Multi_Buffer[3+i]);
sendMulti(p_buf, Multi_Buffer[3+i]);
}
Multi_Buffer[3] = 0x00; // Data sent
}
+2 -6
View File
@@ -19,8 +19,7 @@
* GNU General Public License for more details.
*/
#ifndef _MULTI_PULSES_H_
#define _MULTI_PULSES_H_
#pragma once
#include "pulses_common.h"
#include "hal/serial_driver.h"
@@ -41,7 +40,4 @@ class UartMultiPulses: public DataBuffer<uint8_t, 64>
}
};
extern etx_serial_init multiSerialInitParams;
extern const etx_module_driver_t MultiInternalDriver;
#endif
extern const etx_proto_driver_t MultiDriver;
+77 -30
View File
@@ -21,14 +21,17 @@
#include <climits>
#include "ppm.h"
#include "hal/module_port.h"
#include "mixer_scheduler.h"
#include "opentx.h"
// Minimum space after the last PPM pulse in us
#define PPM_SAFE_MARGIN 3000 // 3ms
template <class T>
uint16_t setupPulsesPPM(PpmPulsesData<T>* ppmPulsesData, uint8_t channelsStart,
int8_t channelsCount)
uint16_t setupPulsesPPM(T*& data, uint8_t channelsStart, int8_t channelsCount)
{
uint16_t total = 0;
int16_t PPM_range = g_model.extendedLimits ?
@@ -40,16 +43,13 @@ uint16_t setupPulsesPPM(PpmPulsesData<T>* ppmPulsesData, uint8_t channelsStart,
// The pulse ISR is 2mhz that's why everything is multiplied by 2
uint8_t firstCh = channelsStart;
uint8_t lastCh =
min<uint8_t>(MAX_OUTPUT_CHANNELS, firstCh + 8 + channelsCount);
ppmPulsesData->ptr = ppmPulsesData->pulses;
uint8_t lastCh = min<uint8_t>(MAX_OUTPUT_CHANNELS, firstCh + 8 + channelsCount);
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);
*ppmPulsesData->ptr++ = v;
*data++ = v;
total += v;
}
@@ -58,8 +58,9 @@ uint16_t setupPulsesPPM(PpmPulsesData<T>* ppmPulsesData, uint8_t channelsStart,
void setupPulsesPPMTrainer()
{
auto p_data = trainerPulsesData.ppm.pulses;
uint16_t total = setupPulsesPPM<trainer_pulse_duration_t>(
&trainerPulsesData.ppm, g_model.trainerData.channelsStart,
p_data, g_model.trainerData.channelsStart,
g_model.trainerData.channelsCount);
uint32_t rest = PPM_TRAINER_PERIOD_HALF_US();
@@ -72,42 +73,88 @@ void setupPulsesPPMTrainer()
if (rest >= USHRT_MAX - 1)
rest = USHRT_MAX - 1;
*trainerPulsesData.ppm.ptr++ = (uint16_t)rest;
*p_data++ = (uint16_t)rest;
// stop mark so that IRQ-based sending
// knows when to stop
*trainerPulsesData.ppm.ptr = 0;
*p_data = 0;
trainerPulsesData.ppm.ptr = p_data;
}
static void setupPulsesPPMModule(uint8_t module)
static uint32_t setupPulsesPPMModule(uint8_t module, pulse_duration_t*& data)
{
PpmPulsesData<pulse_duration_t>* data = nullptr;
#if defined(PCBTARANIS) && defined(INTERNAL_MODULE_PPM)
if (module == INTERNAL_MODULE) {
data = &intmodulePulsesData.ppm;
} else
#endif
{
data = &extmodulePulsesData.ppm;
}
setupPulsesPPM(&extmodulePulsesData.ppm,
auto start = data;
setupPulsesPPM(data,
g_model.moduleData[module].channelsStart,
g_model.moduleData[module].channelsCount);
// Set the final period to 1ms after which the
// PPM will be switched OFF
*data->ptr++ = PPM_SAFE_MARGIN * 2;
*data++ = PPM_SAFE_MARGIN * 2;
return data - start;
}
#if defined(PCBTARANIS) && defined(INTERNAL_MODULE_PPM)
void setupPulsesPPMInternalModule()
static void* ppmInit(uint8_t module)
{
setupPulsesPPMModule(INTERNAL_MODULE);
}
#if defined(HARDWARE_INTERNAL_MODULE)
// only external module supported
if (module == INTERNAL_MODULE) return nullptr;
#endif
void setupPulsesPPMExternalModule()
{
setupPulsesPPMModule(EXTERNAL_MODULE);
auto delay = GET_MODULE_PPM_DELAY(module) * 2;
etx_timer_config_t cfg = {
.type = ETX_PWM,
.polarity = GET_MODULE_PPM_POLARITY(module),
.cmp_val = (uint16_t)delay,
};
auto mod_st = modulePortInitTimer(module, ETX_MOD_PORT_TIMER, &cfg);
if (!mod_st) return nullptr;
mixerSchedulerSetPeriod(module, PPM_PERIOD(module));
return (void*)mod_st;
}
static void ppmDeInit(void* ctx)
{
auto mod_st = (etx_module_state_t*)ctx;
modulePortDeInit(mod_st);
}
static void ppmSendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_t nChannels)
{
// TODO:
(void)channels;
(void)nChannels;
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
pulse_duration_t* pulses = (pulse_duration_t*)buffer;
auto length = setupPulsesPPMModule(module, pulses);
auto drv = modulePortGetTimerDrv(mod_st->tx);
auto drv_ctx = modulePortGetCtx(mod_st->tx);
auto delay = GET_MODULE_PPM_DELAY(module) * 2;
etx_timer_config_t cfg = {
.type = ETX_PWM,
.polarity = GET_MODULE_PPM_POLARITY(module),
.cmp_val = (uint16_t)delay,
};
drv->send(drv_ctx, &cfg, buffer, length);
// PPM_PERIOD is not a constant! It can be set from UI
mixerSchedulerSetPeriod(module, PPM_PERIOD(module));
}
const etx_proto_driver_t PpmDriver = {
.protocol = PROTOCOL_CHANNELS_PPM,
.init = ppmInit,
.deinit = ppmDeInit,
.sendPulses = ppmSendPulses,
.processData = nullptr,
};
+26
View File
@@ -0,0 +1,26 @@
/*
* Copyright (C) EdgeTx
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* 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.
*/
#pragma once
#include "hal/module_driver.h"
extern const etx_proto_driver_t PpmDriver;
+237 -474
View File
@@ -19,28 +19,36 @@
* GNU General Public License for more details.
*/
// #include "hal.h"
#include "opentx.h"
#include "mixer_scheduler.h"
#include "heartbeat_driver.h"
#include "hal/module_port.h"
#include "tasks/mixer_task.h"
#include "pulses/pxx2.h"
#include "pulses/flysky.h"
#include "pulses/dsm2.h"
#if defined(INTMODULE_USART)
#include "intmodule_serial_driver.h"
#else
#include "intmodule_pulses_driver.h"
#if defined(PPM)
#include "pulses/ppm.h"
#endif
#include "extmodule_serial_driver.h"
#include "extmodule_driver.h"
#if defined(PXX1)
#include "pulses/pxx1.h"
#endif
#if defined(SBUS)
#include "pulses/sbus.h"
#endif
#if defined(CROSSFIRE)
#include "pulses/crossfire.h"
#endif
#if defined(GHOST)
#include "telemetry/ghost.h"
#include "pulses/ghost.h"
#endif
#if defined(MULTIMODULE)
@@ -56,63 +64,91 @@
#include "pulses/afhds3.h"
#endif
uint8_t s_pulses_paused = 0;
static module_pulse_driver _module_drivers[MAX_MODULES];
static module_pulse_buffer _module_buffers[MAX_MODULES] __DMA;
void pulsesInit()
{
memset(_module_drivers, 0, sizeof(_module_drivers));
}
module_pulse_driver* pulsesGetModuleDriver(uint8_t module)
{
return &(_module_drivers[module]);
}
uint8_t* pulsesGetModuleBuffer(uint8_t module)
{
return _module_buffers[module]._buffer;
}
ModuleState moduleState[NUM_MODULES];
InternalModulePulsesData intmodulePulsesData __DMA;
ExternalModulePulsesData extmodulePulsesData __DMA;
TrainerPulsesData trainerPulsesData __DMA;
void startPulses()
void pulsesStart()
{
telemetryStart();
s_pulses_paused = false;
#if defined(HARDWARE_INTERNAL_MODULE)
setupPulsesInternalModule();
#endif
#if defined(HARDWARE_EXTERNAL_MODULE)
setupPulsesExternalModule();
#endif
#if defined(HARDWARE_EXTRA_MODULE)
extramodulePpmStart();
#endif
mixerTaskStart();
}
void stopPulses()
void pulsesStop()
{
telemetryStop();
s_pulses_paused = true;
mixerTaskStop();
for (uint8_t i = 0; i < NUM_MODULES; i++)
moduleState[i].protocol = PROTOCOL_CHANNELS_UNINITIALIZED;
for (uint8_t i = 0; i < MAX_MODULES; i++)
pulsesStopModule(i);
}
void restartModule(uint8_t idx)
void restartModule(uint8_t module)
{
if (idx == INTERNAL_MODULE) {
if (!IS_INTERNAL_MODULE_ON()) return;
} else if (idx == EXTERNAL_MODULE){
if (!IS_EXTERNAL_MODULE_ON()) return;
} else {
mixerTaskStop();
// wait for the power output to be drained
pulsesStopModule(module);
RTOS_WAIT_MS(200);
mixerTaskStart();
}
#if !defined(SIMU)
#include <FreeRTOS/include/FreeRTOS.h>
#include <FreeRTOS/include/timers.h>
static void _setup_async_module_restart(void* p1, uint32_t p2)
{
if (!mixerTaskTryLock()) {
// In case the mixer cannot be locked, try again later
// and make the same function pending again.
PendedFunction_t cb = _setup_async_module_restart;
xTimerPendFunctionCall(cb, p1, p2, 0/* do not wait */);
return;
}
pauseMixerCalculations();
pausePulses();
uint8_t module = (uint8_t)(uintptr_t)p1;
moduleState[module].forced_off = 1;
if (idx == INTERNAL_MODULE) stopPulsesInternalModule();
#if defined(HARDWARE_EXTERNAL_MODULE)
else stopPulsesExternalModule();
uint32_t timeout = p2;
moduleState[module].counter = timeout;
mixerTaskUnlock();
}
#endif
RTOS_WAIT_MS(200); // 20ms so that the pulses interrupt will reinit the frame rate
resumePulses();
resumeMixerCalculations();
// return true if the request could be posted to the timer queue
bool restartModuleAsync(uint8_t module, uint8_t cnt_delay)
{
#if !defined(SIMU)
PendedFunction_t cb = _setup_async_module_restart;
return xTimerPendFunctionCall(cb, (void*)(uintptr_t)module, cnt_delay,
0/* do not wait */) == pdPASS;
#else
return true;
#endif
}
// TODO: this should be moved to PXX2 territory!
#if defined(PXX2)
// use only for PXX
void ModuleState::startBind(BindInformation* destination,
ModuleCallback bindCallback)
@@ -160,6 +196,7 @@ void ModuleState::writeReceiverSettings(ReceiverSettings* source)
receiverSettings->timeout = 0;
mode = MODULE_MODE_RECEIVER_SETTINGS;
}
#endif
void getModuleStatusString(uint8_t moduleIdx, char * statusText)
{
@@ -170,6 +207,7 @@ void getModuleStatusString(uint8_t moduleIdx, char * statusText)
getMultiModuleStatus(moduleIdx).getStatusString(statusText);
}
#endif
#if defined(AFHDS3)
if (isModuleAFHDS3(moduleIdx)) {
afhds3::getStatusString(moduleIdx, statusText);
@@ -216,6 +254,9 @@ uint8_t getModuleType(uint8_t module)
return MODULE_TYPE_NONE;
}
// TODO: replace with some mapping between
// module type and protocol driver.
//
uint8_t getRequiredProtocol(uint8_t module)
{
uint8_t protocol = PROTOCOL_CHANNELS_UNINITIALIZED;
@@ -226,36 +267,17 @@ uint8_t getRequiredProtocol(uint8_t module)
break;
case MODULE_TYPE_XJT_PXX1:
#if defined(INTMODULE_USART)
if (module == INTERNAL_MODULE) {
protocol = PROTOCOL_CHANNELS_PXX1_SERIAL;
break;
}
#endif
protocol = PROTOCOL_CHANNELS_PXX1_PULSES;
break;
case MODULE_TYPE_R9M_PXX1:
protocol = PROTOCOL_CHANNELS_PXX1_PULSES;
break;
#if defined(HARDWARE_EXTERNAL_MODULE_SIZE_SML)
case MODULE_TYPE_R9M_LITE_PXX1:
protocol = PROTOCOL_CHANNELS_PXX1_SERIAL;
protocol = PROTOCOL_CHANNELS_PXX1;
break;
case MODULE_TYPE_R9M_LITE_PXX2:
protocol = PROTOCOL_CHANNELS_PXX2_LOWSPEED;
break;
#endif
case MODULE_TYPE_ISRM_PXX2:
case MODULE_TYPE_R9M_PXX2:
#if defined(HARDWARE_EXTERNAL_MODULE_SIZE_SML)
case MODULE_TYPE_R9M_LITE_PXX2:
case MODULE_TYPE_XJT_LITE_PXX2:
case MODULE_TYPE_R9M_LITE_PRO_PXX2:
#endif
protocol = PROTOCOL_CHANNELS_PXX2_HIGHSPEED;
protocol = PROTOCOL_CHANNELS_PXX2;
break;
case MODULE_TYPE_SBUS:
@@ -275,6 +297,7 @@ uint8_t getRequiredProtocol(uint8_t module)
PROTOCOL_CHANNELS_DSM2_LP45 + g_model.moduleData[module].subType,
PROTOCOL_CHANNELS_DSM2_DSMX);
// The module is set to OFF during one second before BIND start
// TODO: move this to DSM2 driver...
{
static tmr10ms_t bindStartTime = 0;
if (moduleState[module].mode == MODULE_MODE_BIND) {
@@ -322,239 +345,78 @@ uint8_t getRequiredProtocol(uint8_t module)
break;
}
if (s_pulses_paused) {
protocol = PROTOCOL_CHANNELS_NONE;
}
#if 0
// will need an EEPROM conversion
if (moduleState[module].mode == MODULE_OFF) {
protocol = PROTOCOL_CHANNELS_NONE;
}
#endif
return protocol;
}
#if defined(HARDWARE_INTERNAL_MODULE)
static void* internalModuleContext = nullptr;
static const etx_module_driver_t* internalModuleDriver = nullptr;
static module_init_cb_t _on_module_init = nullptr;
static module_deinit_cb_t _on_module_deinit = nullptr;
const etx_module_driver_t* getIntModuleDriver()
void pulsesSetModuleInitCb(module_init_cb_t cb)
{
return internalModuleDriver;
_on_module_init = cb;
}
void* getIntModuleCtx()
void pulsesSetModuleDeInitCb(module_deinit_cb_t cb)
{
return internalModuleContext;
_on_module_deinit = cb;
}
static void enablePulsesInternalModule(uint8_t protocol)
static void _init_module(uint8_t module, const etx_proto_driver_t* drv)
{
// start new protocol hardware here
if (internalModuleDriver) {
internalModuleDriver->deinit(internalModuleContext);
internalModuleContext = nullptr;
internalModuleDriver = nullptr;
} else {
intmoduleStop();
}
auto mod = &(_module_drivers[module]);
void* ctx = drv->init(module);
switch (protocol) {
#if defined(INTERNAL_MODULE_PXX1) && !defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_PULSES:
intmodulePxx1PulsesStart();
#if defined(INTMODULE_HEARTBEAT)
init_intmodule_heartbeat();
#endif
mixerSchedulerSetPeriod(INTERNAL_MODULE, INTMODULE_PXX1_SERIAL_PERIOD);
break;
#endif
#if defined(INTERNAL_MODULE_PXX1) && defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_SERIAL:
internalModuleContext = Pxx1InternalSerialDriver.init(INTERNAL_MODULE);
internalModuleDriver = &Pxx1InternalSerialDriver;
break;
#endif
#if defined(INTERNAL_MODULE_PXX2)
case PROTOCOL_CHANNELS_PXX2_HIGHSPEED:
internalModuleContext = Pxx2InternalDriver.init(INTERNAL_MODULE);
internalModuleDriver = &Pxx2InternalDriver;
break;
#endif
#if defined(INTERNAL_MODULE_MULTI) && defined(MULTIMODULE)
case PROTOCOL_CHANNELS_MULTIMODULE:
internalModuleContext = MultiInternalDriver.init(INTERNAL_MODULE);
internalModuleDriver = &MultiInternalDriver;
break;
#endif
#if defined(INTERNAL_MODULE_CRSF) && defined(CROSSFIRE)
case PROTOCOL_CHANNELS_CROSSFIRE:
internalModuleContext = CrossfireInternalDriver.init(INTERNAL_MODULE);
internalModuleDriver = &CrossfireInternalDriver;
break;
#endif
#if defined(INTERNAL_MODULE_PPM)
case PROTOCOL_CHANNELS_PPM:
intmodulePpmStart();
mixerSchedulerSetPeriod(INTERNAL_MODULE, PPM_PERIOD(INTERNAL_MODULE));
break;
#endif
#if defined(INTERNAL_MODULE_AFHDS2A) && defined(AFHDS2)
case PROTOCOL_CHANNELS_AFHDS2A:
internalModuleContext = Afhds2InternalDriver.init(INTERNAL_MODULE);
internalModuleDriver = &Afhds2InternalDriver;
break;
#endif
#if defined(INTERNAL_MODULE_AFHDS3)
case PROTOCOL_CHANNELS_AFHDS3:
internalModuleContext = afhds3::internalDriver.init(INTERNAL_MODULE);
internalModuleDriver = &afhds3::internalDriver;
break;
#endif
default:
// internal module stopped, use default mixer period
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
break;
}
}
bool setupPulsesInternalModule(uint8_t protocol)
{
uint8_t channelStart = g_model.moduleData[INTERNAL_MODULE].channelsStart;
int16_t* channels = &channelOutputs[channelStart];
uint8_t nChannels = 16; // TODO: MAX_CHANNELS - channelsStart
if (internalModuleDriver) {
internalModuleDriver->setupPulses(internalModuleContext,
channels, nChannels);
return true;
}
switch (protocol) {
#if defined(HARDWARE_INTERNAL_MODULE) && defined(PXX1) && !defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_PULSES:
intmodulePulsesData.pxx.setupFrame(INTERNAL_MODULE);
return true;
#endif
#if defined(PCBTARANIS) && defined(INTERNAL_MODULE_PPM)
case PROTOCOL_CHANNELS_PPM:
setupPulsesPPMInternalModule();
return true;
#endif
default:
return false;
}
}
void stopPulsesInternalModule()
{
auto& proto = moduleState[INTERNAL_MODULE].protocol;
if (proto != PROTOCOL_CHANNELS_UNINITIALIZED &&
proto != PROTOCOL_CHANNELS_NONE) {
if (internalModuleDriver) {
internalModuleDriver->deinit(internalModuleContext);
internalModuleDriver = nullptr;
internalModuleContext = nullptr;
} else {
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
intmoduleStop();
}
proto = PROTOCOL_CHANNELS_NONE;
}
}
void intmoduleSendNextFrame()
{
if (internalModuleDriver) {
internalModuleDriver->sendPulses(internalModuleContext);
// TODO: module init failed somehow, we should handle this better...
if (!ctx) {
TRACE("Module #%d init failed", module);
return;
}
switch (moduleState[INTERNAL_MODULE].protocol) {
mod->drv = drv;
mod->ctx = ctx;
#if defined(INTERNAL_MODULE_PPM)
case PROTOCOL_CHANNELS_PPM:
intmoduleSendNextFramePPM(
intmodulePulsesData.ppm.pulses,
intmodulePulsesData.ppm.ptr - intmodulePulsesData.ppm.pulses);
break;
#endif
#if defined(PXX1) && !defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_PULSES:
intmoduleSendNextFramePxx1(intmodulePulsesData.pxx.getData(),
intmodulePulsesData.pxx.getSize());
break;
#endif
}
// board specific hook
if (_on_module_init)
_on_module_init(module, drv);
// power ON
modulePortSetPower(module, true);
TRACE("Module #%d init succeeded", module);
}
bool setupPulsesInternalModule()
static void _deinit_module(uint8_t module)
{
uint8_t protocol = getRequiredProtocol(INTERNAL_MODULE);
auto mod = &(_module_drivers[module]);
if (!mod->drv) return;
heartbeat |= (HEART_TIMER_PULSES << INTERNAL_MODULE);
// scheduling OFF
mixerSchedulerSetPeriod(module, 0);
if (moduleState[INTERNAL_MODULE].protocol != protocol) {
enablePulsesInternalModule(protocol);
moduleState[INTERNAL_MODULE].protocol = protocol;
return false;
}
else {
return setupPulsesInternalModule(protocol);
}
}
#endif
// board specific hook
auto drv = mod->drv;
if (_on_module_deinit)
_on_module_deinit(module, drv);
// de-init
auto ctx = mod->ctx;
drv->deinit(ctx);
#if defined(HARDWARE_EXTERNAL_MODULE)
static void* externalModuleContext = nullptr;
static const etx_module_driver_t* externalModuleDriver = nullptr;
// power OFF
modulePortSetPower(module, false);
const etx_module_driver_t* getExtModuleDriver()
{
return externalModuleDriver;
// clear
memset(mod, 0, sizeof(module_pulse_driver));
TRACE("Module #%d de-init succeeded", module);
}
void* getExtModuleCtx()
static void pulsesEnableModule(uint8_t module, uint8_t protocol)
{
return externalModuleContext;
}
void enablePulsesExternalModule(uint8_t protocol)
{
// start new protocol hardware here
if (externalModuleDriver) {
externalModuleDriver->deinit(externalModuleContext);
externalModuleDriver = nullptr;
externalModuleContext = nullptr;
} else {
extmoduleStop();
}
_deinit_module(module);
switch (protocol) {
#if defined(PXX1)
case PROTOCOL_CHANNELS_PXX1_PULSES:
extmodulePxx1PulsesStart();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, PXX_PULSES_PERIOD);
break;
#endif
#if defined(PXX1) && defined(HARDWARE_EXTERNAL_MODULE_SIZE_SML)
case PROTOCOL_CHANNELS_PXX1_SERIAL:
externalModuleContext = Pxx1ExternalSerialDriver.init(EXTERNAL_MODULE);
externalModuleDriver = &Pxx1ExternalSerialDriver;
case PROTOCOL_CHANNELS_PXX1:
_init_module(module, &Pxx1Driver);
break;
#endif
@@ -562,254 +424,155 @@ void enablePulsesExternalModule(uint8_t protocol)
case PROTOCOL_CHANNELS_DSM2_LP45:
case PROTOCOL_CHANNELS_DSM2_DSM2:
case PROTOCOL_CHANNELS_DSM2_DSMX:
extmoduleSerialStart();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, DSM2_PERIOD);
_init_module(module, &DSM2Driver);
break;
#endif
#if defined(SBUS)
case PROTOCOL_CHANNELS_SBUS:
_init_module(module, &SBusDriver);
break;
#endif
#if defined(PXX2)
case PROTOCOL_CHANNELS_PXX2:
_init_module(module, &Pxx2Driver);
break;
#endif
#if defined(MULTIMODULE)
case PROTOCOL_CHANNELS_MULTIMODULE:
_init_module(module, &MultiDriver);
break;
#endif
#if defined(CROSSFIRE)
case PROTOCOL_CHANNELS_CROSSFIRE:
externalModuleContext = CrossfireExternalDriver.init(EXTERNAL_MODULE);
externalModuleDriver = &CrossfireExternalDriver;
_init_module(module, &CrossfireDriver);
break;
#endif
#if defined(GHOST)
case PROTOCOL_CHANNELS_GHOST:
EXTERNAL_MODULE_ON();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, GHOST_PERIOD);
break;
#endif
#if defined(PXX2)
case PROTOCOL_CHANNELS_PXX2_HIGHSPEED:
externalModuleContext = Pxx2ExternalDriver.init(EXTERNAL_MODULE);
externalModuleDriver = &Pxx2ExternalDriver;
break;
case PROTOCOL_CHANNELS_PXX2_LOWSPEED:
externalModuleContext = Pxx2LowSpeedExternalDriver.init(EXTERNAL_MODULE);
externalModuleDriver = &Pxx2LowSpeedExternalDriver;
break;
#endif
#if defined(MULTIMODULE)
case PROTOCOL_CHANNELS_MULTIMODULE:
extmoduleSerialStart();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, MULTIMODULE_PERIOD);
getMultiModuleStatus(EXTERNAL_MODULE).failsafeChecked = false;
getMultiModuleStatus(EXTERNAL_MODULE).flags = 0;
#if defined(MULTI_PROTOLIST)
MultiRfProtocols::instance(EXTERNAL_MODULE)->triggerScan();
#endif
break;
#endif
#if defined(SBUS)
case PROTOCOL_CHANNELS_SBUS:
extmoduleSerialStart();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, SBUS_PERIOD);
_init_module(module, &GhostDriver);
break;
#endif
#if defined(PPM)
case PROTOCOL_CHANNELS_PPM:
extmodulePpmStart(
GET_MODULE_PPM_DELAY(EXTERNAL_MODULE),
GET_MODULE_PPM_POLARITY(EXTERNAL_MODULE));
mixerSchedulerSetPeriod(EXTERNAL_MODULE, PPM_PERIOD(EXTERNAL_MODULE));
case PROTOCOL_CHANNELS_PPM:
_init_module(module, &PpmDriver);
break;
#endif
#if defined(AFHDS3)
#if defined(INTERNAL_MODULE_AFHDS2A) && defined(AFHDS2)
case PROTOCOL_CHANNELS_AFHDS2A:
_init_module(module, &Afhds2InternalDriver);
break;
#endif
#if defined(INTERNAL_MODULE_AFHDS3)
case PROTOCOL_CHANNELS_AFHDS3:
externalModuleContext = afhds3::externalDriver.init(EXTERNAL_MODULE);
externalModuleDriver = &afhds3::externalDriver;
_init_module(module, &afhds3::ProtoDriver);
break;
#endif
case PROTOCOL_CHANNELS_DSMP:
extmoduleSerialStart();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 11 * 1000 /* 11ms in us */);
break;
default:
// external module stopped, use default mixer period
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
break;
}
}
bool setupPulsesExternalModule(uint8_t protocol)
{
uint8_t channelStart = g_model.moduleData[EXTERNAL_MODULE].channelsStart;
int16_t* channels = &channelOutputs[channelStart];
uint8_t nChannels = 16; // TODO: MAX_CHANNELS - channelsStart
if (externalModuleDriver) {
externalModuleDriver->setupPulses(externalModuleContext,
channels, nChannels);
return true;
}
switch (protocol) {
#if defined(PXX1)
case PROTOCOL_CHANNELS_PXX1_PULSES:
extmodulePulsesData.pxx.setupFrame(EXTERNAL_MODULE);
return true;
#endif
#if defined(SBUS)
case PROTOCOL_CHANNELS_SBUS:
setupPulsesSbus();
// SBUS_PERIOD is not a constant! It can be set from UI
mixerSchedulerSetPeriod(EXTERNAL_MODULE, SBUS_PERIOD);
return true;
#endif
#if defined(DSM2)
case PROTOCOL_CHANNELS_DSM2_LP45:
case PROTOCOL_CHANNELS_DSM2_DSM2:
case PROTOCOL_CHANNELS_DSM2_DSMX:
setupPulsesDSM2();
return true;
#endif
#if defined(GHOST)
case PROTOCOL_CHANNELS_GHOST:
{
ModuleSyncStatus& status = getModuleSyncStatus(EXTERNAL_MODULE);
if (status.isValid())
mixerSchedulerSetPeriod(EXTERNAL_MODULE, status.getAdjustedRefreshRate());
else
mixerSchedulerSetPeriod(EXTERNAL_MODULE, GHOST_PERIOD);
setupPulsesGhost();
return true;
}
#endif
#if defined(MULTIMODULE)
case PROTOCOL_CHANNELS_MULTIMODULE:
setupPulsesMultiExternalModule();
return true;
#endif
#if defined(PPM)
case PROTOCOL_CHANNELS_PPM:
setupPulsesPPMExternalModule();
return true;
#endif
case PROTOCOL_CHANNELS_DSMP:
setupPulsesLemonDSMP();
return true;
_init_module(module, &DSMPDriver);
break;
#endif
default:
return false;
break;
}
}
void extmoduleSendNextFrame()
// TODO: declare a function in telemetry
extern volatile uint8_t _telemetryIsPolling;
void pulsesStopModule(uint8_t module)
{
if (externalModuleDriver) {
externalModuleDriver->sendPulses(externalModuleContext);
if (module >= MAX_MODULES) return;
while(_telemetryIsPolling) {
// In case the telemetry timer is currently polling the port,
// we give the timer task a chance to run and finish the polling.
RTOS_WAIT_MS(1);
}
_deinit_module(module);
auto& proto = moduleState[module].protocol;
proto = PROTOCOL_CHANNELS_NONE;
}
static bool _handle_async_restart(uint8_t module)
{
if (moduleState[module].forced_off) {
if (--moduleState[module].counter > 0) {
_deinit_module(module);
moduleState[module].protocol = PROTOCOL_CHANNELS_NONE;
return true;
} else {
moduleState[module].forced_off = 0;
}
}
return false;
}
void pulsesSendNextFrame(uint8_t module)
{
if (module >= MAX_MODULES) return;
uint8_t protocol = getRequiredProtocol(module);
if (moduleState[module].protocol != protocol ||
moduleState[module].forced_off) {
// TODO: error checking!
if (_telemetryIsPolling) {
// In case the telemetry timer is currently polling the port,
// we just yield in the hope it will be different next time.
return;
}
if (_handle_async_restart(module))
return;
pulsesEnableModule(module, protocol);
moduleState[module].protocol = protocol;
return;
}
switch (moduleState[EXTERNAL_MODULE].protocol) {
auto mod = &(_module_drivers[module]);
if (mod->drv) {
uint8_t channelStart = g_model.moduleData[module].channelsStart;
int16_t* channels = &channelOutputs[channelStart];
uint8_t nChannels = 16; // TODO: MAX_CHANNELS - channelsStart
case PROTOCOL_CHANNELS_PPM:
extmoduleSendNextFramePpm(
extmodulePulsesData.ppm.pulses,
extmodulePulsesData.ppm.ptr - extmodulePulsesData.ppm.pulses,
GET_MODULE_PPM_DELAY(EXTERNAL_MODULE),
GET_MODULE_PPM_POLARITY(EXTERNAL_MODULE));
mixerSchedulerSetPeriod(EXTERNAL_MODULE, PPM_PERIOD(EXTERNAL_MODULE));
break;
#if defined(PXX1)
case PROTOCOL_CHANNELS_PXX1_PULSES:
extmoduleSendNextFramePxx1(extmodulePulsesData.pxx.getData(),
extmodulePulsesData.pxx.getSize());
break;
#endif
#if defined(SBUS) || defined(DSM2) || defined(MULTIMODULE)
case PROTOCOL_CHANNELS_SBUS:
extmoduleSendNextFrameSoftSerial(
extmodulePulsesData.dsm2.pulses,
extmodulePulsesData.dsm2.ptr - extmodulePulsesData.dsm2.pulses,
GET_SBUS_POLARITY(EXTERNAL_MODULE));
break;
case PROTOCOL_CHANNELS_DSM2_LP45:
case PROTOCOL_CHANNELS_DSM2_DSM2:
case PROTOCOL_CHANNELS_DSM2_DSMX:
case PROTOCOL_CHANNELS_MULTIMODULE:
extmoduleSendNextFrameSoftSerial(
extmodulePulsesData.dsm2.pulses,
extmodulePulsesData.dsm2.ptr - extmodulePulsesData.dsm2.pulses);
break;
#endif
case PROTOCOL_CHANNELS_DSMP:
extmoduleSendNextFrameSoftSerial(
extmodulePulsesData.dsm2.pulses,
extmodulePulsesData.dsm2.ptr - extmodulePulsesData.dsm2.pulses,
true);
break;
#if defined(GHOST)
case PROTOCOL_CHANNELS_GHOST:
sportSendBuffer(extmodulePulsesData.ghost.pulses,
extmodulePulsesData.ghost.length);
break;
#endif
auto drv = mod->drv;
auto ctx = mod->ctx;
auto buffer = _module_buffers[module]._buffer;
drv->sendPulses(ctx, buffer, channels, nChannels);
}
}
void stopPulsesExternalModule()
void pulsesSendChannels()
{
auto& proto = moduleState[EXTERNAL_MODULE].protocol;
if (proto != PROTOCOL_CHANNELS_UNINITIALIZED &&
proto != PROTOCOL_CHANNELS_NONE) {
if (externalModuleDriver) {
externalModuleDriver->deinit(externalModuleContext);
externalModuleDriver = nullptr;
externalModuleContext = nullptr;
} else {
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
extmoduleStop();
}
proto = PROTOCOL_CHANNELS_NONE;
for (uint8_t i = 0; i < MAX_MODULES; i++) {
pulsesSendNextFrame(i);
}
}
bool setupPulsesExternalModule()
{
uint8_t protocol = getRequiredProtocol(EXTERNAL_MODULE);
heartbeat |= (HEART_TIMER_PULSES << EXTERNAL_MODULE);
if (moduleState[EXTERNAL_MODULE].protocol != protocol) {
enablePulsesExternalModule(protocol);
moduleState[EXTERNAL_MODULE].protocol = protocol;
return false;
}
else {
return setupPulsesExternalModule(protocol);
}
}
#endif
// set the failsafe channel values to the current output values
void setCustomFailsafe(uint8_t moduleIndex)
{
if (moduleIndex < NUM_MODULES) {
for (int ch=0; ch<MAX_OUTPUT_CHANNELS; ch++) {
if (ch < g_model.moduleData[moduleIndex].channelsStart || ch >= sentModuleChannels(moduleIndex) + g_model.moduleData[moduleIndex].channelsStart) {
for (int ch = 0; ch < MAX_OUTPUT_CHANNELS; ch++) {
if (ch < g_model.moduleData[moduleIndex].channelsStart ||
ch >= sentModuleChannels(moduleIndex) +
g_model.moduleData[moduleIndex].channelsStart) {
g_model.failsafeChannels[ch] = 0;
}
else if (g_model.failsafeChannels[ch] < FAILSAFE_CHANNEL_HOLD) {
} else if (g_model.failsafeChannels[ch] < FAILSAFE_CHANNEL_HOLD) {
g_model.failsafeChannels[ch] = channelOutputs[ch];
}
}
@@ -819,5 +582,5 @@ void setCustomFailsafe(uint8_t moduleIndex)
int32_t getChannelValue(uint8_t channel)
{
return channelOutputs[channel] + 2*PPM_CH_CENTER(channel) - 2*PPM_CENTER;
return channelOutputs[channel] + 2 * PPM_CH_CENTER(channel) - 2 * PPM_CENTER;
}
+45 -145
View File
@@ -25,15 +25,13 @@
#include "definitions.h"
#include "dataconstants.h"
#include "pulses_common.h"
#include "pxx1.h"
#include "pxx2.h"
#include "multi.h"
#include "afhds2.h"
#include "afhds3_module.h"
#include "modules_helpers.h"
#include "ff.h"
#include "hal/module_driver.h"
#if defined(PXX2)
#include "pxx2.h"
#include "pxx2_ota.h"
#endif
#if defined(DSM2)
#define IS_DSM2_PROTOCOL(protocol) (protocol>=PROTOCOL_CHANNELS_DSM2_LP45 && protocol<=PROTOCOL_CHANNELS_DSM2_DSMX)
@@ -62,17 +60,16 @@
#define IS_AFHDS3_PROTOCOL(protocol) (0)
#endif
extern uint8_t s_pulses_paused;
typedef void (* ModuleCallback)();
PACK(struct ModuleState {
uint8_t protocol;
uint8_t mode:4;
uint8_t paused:1;
uint8_t forced_off:1;
uint8_t spare:3;
uint16_t counter;
#if defined(PXX2)
// PXX specific items
union
{
@@ -90,6 +87,7 @@ PACK(struct ModuleState {
void writeModuleSettings(ModuleSettings * source);
void readReceiverSettings(ReceiverSettings * destination);
void writeReceiverSettings(ReceiverSettings * source);
#endif
});
extern ModuleState moduleState[NUM_MODULES];
@@ -130,98 +128,13 @@ typedef Dsm2TimerPulsesData Dsm2PulsesData;
#define SBUS_MAX_PERIOD 325 /*Overflows uint16_t if set higher*/
#define SBUS_DEF_PERIOD 225
#define SBUS_STEPSIZE 5 /* SBUS Step Size 0.5ms */
#define SBUS_PERIOD_HALF_US ((g_model.moduleData[EXTERNAL_MODULE].sbus.refreshRate * SBUS_STEPSIZE + SBUS_DEF_PERIOD) * 200) /*half us*/
#define SBUS_PERIOD (SBUS_PERIOD_HALF_US / 2) /*us*/
#define SBUS_PERIOD_HALF_US(m) ((g_model.moduleData[m].sbus.refreshRate * SBUS_STEPSIZE + SBUS_DEF_PERIOD) * 200) /*half us*/
#define SBUS_PERIOD(m) (SBUS_PERIOD_HALF_US(m) / 2) /*us*/
#define MULTIMODULE_BAUDRATE 100000
#define MULTIMODULE_PERIOD 7000 /*us*/
#define CROSSFIRE_FRAME_MAXLEN 64
PACK(struct CrossfirePulsesData {
uint8_t pulses[CROSSFIRE_FRAME_MAXLEN];
uint8_t length;
});
#define GHOST_FRAME_MAXLEN 16
PACK(struct GhostPulsesData {
uint8_t pulses[GHOST_FRAME_MAXLEN];
uint8_t length;
});
union InternalModulePulsesData {
#if defined(INTERNAL_MODULE_PXX1)
#if defined(INTMODULE_USART)
UartPxx1Pulses pxx_uart;
#else
PwmPxx1Pulses pxx;
#endif
#endif
#if defined(INTERNAL_MODULE_PXX2)
Pxx2Pulses pxx2;
#endif
#if defined(INTERNAL_MODULE_AFHDS2A)
FlySkySerialPulsesData flysky;
#endif
#if defined(INTERNAL_MODULE_MULTI)
UartMultiPulses multi;
#endif
#if defined(INTERNAL_MODULE_CRSF)
CrossfirePulsesData crossfire;
#endif
#if defined(INTERNAL_MODULE_AFHDS3)
afhds3::IntmoduleData afhds3;
#endif
#if defined(INTERNAL_MODULE_PPM)
PpmPulsesData<pulse_duration_t> ppm;
#endif
} __ALIGNED(4);
union ExternalModulePulsesData {
#if defined(PXX1)
#if defined(HARDWARE_EXTERNAL_MODULE_SIZE_SML)
UartPxx1Pulses pxx_uart;
#endif
PwmPxx1Pulses pxx;
#endif
#if defined(PXX2)
Pxx2Pulses pxx2;
#endif
#if defined(DSM2) || defined(MULTIMODULE) || defined(SBUS)
Dsm2PulsesData dsm2;
#endif
#if defined(AFHDS3)
afhds3::ExtmoduleData afhds3;
#endif
PpmPulsesData<pulse_duration_t> ppm;
#if defined(CROSSFIRE)
CrossfirePulsesData crossfire;
#endif
#if defined(GHOST)
GhostPulsesData ghost;
#endif
} __ALIGNED(4);
/* The __ALIGNED keyword is required to align the struct inside the modulePulsesData below,
* which is also defined to be __DMA (which includes __ALIGNED) aligned.
* Arrays in C/C++ are always defined to be *contiguously*. The first byte of the second element is therefore always
* sizeof(ModulePulsesData). __ALIGNED is required for sizeof(ModulePulsesData) to be a multiple of the alignment.
*/
extern InternalModulePulsesData intmodulePulsesData;
extern ExternalModulePulsesData extmodulePulsesData;
union TrainerPulsesData {
PpmPulsesData<trainer_pulse_duration_t> ppm;
@@ -229,63 +142,50 @@ union TrainerPulsesData {
extern TrainerPulsesData trainerPulsesData;
#if defined(HARDWARE_INTERNAL_MODULE)
bool setupPulsesInternalModule();
void stopPulsesInternalModule();
void intmoduleSendNextFrame();
const etx_module_driver_t* getIntModuleDriver();
void* getIntModuleCtx();
#if !defined(AFHDS3)
#define MODULE_BUFFER_SIZE 64
#else
#define MODULE_BUFFER_SIZE 128
#endif
#if defined(HARDWARE_EXTERNAL_MODULE)
bool setupPulsesExternalModule();
void stopPulsesExternalModule();
void extmoduleSendNextFrame();
const etx_module_driver_t* getExtModuleDriver();
void* getExtModuleCtx();
#endif
void restartModule(uint8_t idx);
void setupPulsesDSM2();
void setupPulsesLemonDSMP();
void setupPulsesCrossfire(uint8_t idx);
void setupPulsesGhost();
void setupPulsesMultiExternalModule();
void setupPulsesSbus();
void setupPulsesPPMInternalModule();
void setupPulsesPPMExternalModule();
struct module_pulse_buffer {
uint8_t _buffer[MODULE_BUFFER_SIZE];
};
struct module_pulse_driver {
module_pulse_buffer buffer;
const etx_proto_driver_t* drv;
void* ctx;
};
module_pulse_driver* pulsesGetModuleDriver(uint8_t module);
uint8_t* pulsesGetModuleBuffer(uint8_t module);
void pulsesStopModule(uint8_t module);
void pulsesSendNextFrame(uint8_t module);
void pulsesSendChannels();
typedef void (*module_init_cb_t)(uint8_t, const etx_proto_driver_t*);
typedef void (*module_deinit_cb_t)(uint8_t, const etx_proto_driver_t*);
void pulsesSetModuleInitCb(module_init_cb_t cb);
void pulsesSetModuleDeInitCb(module_deinit_cb_t cb);
void restartModule(uint8_t module);
bool restartModuleAsync(uint8_t module, uint8_t cnt_delay);
void setupPulsesPPMTrainer();
void putDsm2Flush();
void sendByteSbus(uint8_t b);
void intmodulePpmStart();
void intmodulePxx1PulsesStart();
void intmodulePxx1SerialStart();
void extmodulePxx1PulsesStart();
void extmodulePpmStart();
void intmoduleStop();
void extmoduleStop();
void getModuleStatusString(uint8_t moduleIdx, char * statusText);
void getModuleSyncStatusString(uint8_t moduleIdx, char * statusText);
#if defined(AFHDS3)
uint8_t actualAfhdsRunPower(int moduleIndex);
#endif
void extramodulePpmStart();
void startPulses();
void stopPulses();
inline bool pulsesStarted()
{
return moduleState[0].protocol != PROTOCOL_CHANNELS_UNINITIALIZED;
}
inline void pausePulses()
{
s_pulses_paused = true;
}
inline void resumePulses()
{
s_pulses_paused = false;
}
void pulsesInit();
void pulsesStart();
void pulsesStop();
inline void SEND_FAILSAFE_NOW(uint8_t idx)
{
+14 -96
View File
@@ -19,10 +19,7 @@
* GNU General Public License for more details.
*/
#ifndef _PULSES_PXX_H_
#define _PULSES_PXX_H_
#include "pulses_common.h"
#pragma once
#define PXX_SEND_BIND 0x01
#define PXX_SEND_FAILSAFE (1 << 4)
@@ -30,100 +27,21 @@
#define PXX2_LOWSPEED_BAUDRATE 230400
#define PXX2_HIGHSPEED_BAUDRATE 450000
#define PXX2_NO_HEARTBEAT_PERIOD 4000/*us*/
#define PXX2_MAX_HEARTBEAT_PERIOD (9000 + 1000)/*us longest period (isrm/accst) + 1ms heartbeat backup*/
#if defined(INTMODULE_HEARTBEAT)
#define PXX2_PERIOD PXX2_MAX_HEARTBEAT_PERIOD
#else
#define PXX2_PERIOD PXX2_NO_HEARTBEAT_PERIOD
#endif
/* microseconds */
#define PXX2_DEFAULT_PERIOD 4000
/* microseconds longest period (isrm/accst) + 1ms heartbeat backup*/
#define PXX2_MAX_HEARTBEAT_PERIOD (9000 + 1000)
#define PXX2_TOOLS_PERIOD 1000/*us*/
#define PXX2_FRAME_MAXLENGTH 64
#define PXX_PULSES_PERIOD 9000/*us*/
#define PXX1_DEFAULT_PERIOD 9000/*us*/
#define PXX1_DEFAULT_SERIAL_BAUDRATE 115200
#define PXX1_FAST_PERIOD 4000/*us*/
#define PXX1_FAST_SERIAL_BAUDRATE 450000
#define EXTMODULE_PXX1_SERIAL_PERIOD 4000/*us*/
#define EXTMODULE_PXX1_SERIAL_BAUDRATE 420000
#if defined(PXX_FREQUENCY_HIGH)
#define INTMODULE_PXX1_SERIAL_BAUDRATE 450000
#if defined(INTMODULE_HEARTBEAT)
// use backup trigger (1 ms later)
#define INTMODULE_PXX1_SERIAL_PERIOD (4000 + 1000)/*us*/
#else
#define INTMODULE_PXX1_SERIAL_PERIOD 4000/*us*/
#endif
#else
#define INTMODULE_PXX1_SERIAL_BAUDRATE 115200
#if defined(INTMODULE_HEARTBEAT)
// use backup trigger (1 ms later)
#define INTMODULE_PXX1_SERIAL_PERIOD (9000 + 1000)/*us*/
#else
#define INTMODULE_PXX1_SERIAL_PERIOD 9000/*us*/
#endif
#endif
// Used by the Sky9x family boards
class SerialPxxBitTransport: public DataBuffer<uint8_t, 64> {
protected:
uint8_t byte;
uint8_t bits_count;
void initFrame(uint32_t period)
{
initBuffer();
byte = 0;
bits_count = 0;
}
void addSerialBit(uint8_t bit)
{
byte >>= 1;
if (bit & 1) {
byte |= 0x80;
}
if (++bits_count >= 8) {
*ptr++ = byte;
bits_count = 0;
}
}
// 8uS/bit 01 = 0, 001 = 1
void addPart(uint8_t value)
{
addSerialBit(0);
if (value) {
addSerialBit(0);
}
addSerialBit(1);
}
void addTail()
{
while (bits_count != 0) {
addSerialBit(1);
}
}
};
class PwmPxxBitTransport: public PulsesBuffer<pulse_duration_t, 200> {
protected:
void initFrame(uint32_t period)
{
initBuffer();
}
void addPart(uint8_t value)
{
pulse_duration_t duration = value ? 47 : 31;
*ptr++ = duration;
}
void addTail()
{
// rest min value is 18000 - 200 * 48 = 8400 (4.2ms)
*(ptr - 1) = 255;
}
};
#endif
+172 -109
View File
@@ -19,10 +19,21 @@
* GNU General Public License for more details.
*/
#include "opentx.h"
#include "pulses/pxx1.h"
#include "mixer_scheduler.h"
#include "hal/module_port.h"
#include "heartbeat_driver.h"
#include "mixer_scheduler.h"
#include "pxx1_transport.h"
#include "pxx1.h"
#include "opentx.h"
static uint32_t _pxx1_internal_baudrate = PXX1_DEFAULT_SERIAL_BAUDRATE;
void pxx1SetInternalBaudrate(uint32_t baudrate)
{
_pxx1_internal_baudrate = baudrate;
}
template <class PxxTransport>
void Pxx1Pulses<PxxTransport>::addFlag1(uint8_t module, uint8_t sendFailsafe)
@@ -63,10 +74,12 @@ void Pxx1Pulses<PxxTransport>::addExtraFlags(uint8_t module)
extraFlags |= (1 << 6);
}
#if defined(HARDWARE_EXTERNAL_MODULE) && defined(HARDWARE_INTERNAL_MODULE)
// Disable S.PORT if internal module is active
if (module == EXTERNAL_MODULE && isSportLineUsedByInternalModule()) {
extraFlags |= (1 << 5);
}
#endif
PxxTransport::addByte(extraFlags);
}
@@ -175,17 +188,25 @@ void Pxx1Pulses<PxxTransport>::add8ChannelsFrame(uint8_t module, uint8_t sendUpp
}
template <class PxxTransport>
void Pxx1Pulses<PxxTransport>::setupFrame(uint8_t module)
Pxx1Pulses<PxxTransport>::Pxx1Pulses(uint8_t* buffer)
: PxxTransport(buffer)
{
}
template <class PxxTransport>
void Pxx1Pulses<PxxTransport>::setupFrame(uint8_t module, Pxx1Type type)
{
uint8_t sendUpperChannels = 0;
uint8_t sendFailsafe = 0;
PxxTransport::initFrame(PXX_PULSES_PERIOD);
#if defined(PXX_FREQUENCY_HIGH)
if (moduleState[module].protocol == PROTOCOL_CHANNELS_PXX1_SERIAL) {
// Fast serial PXX1 (either X-lite internal or R9M-lite):
// - up to 16 channels sent in the same frame
//
if (type == Pxx1Type::FAST_SERIAL) {
if (moduleState[module].counter-- == 0) {
sendFailsafe = (g_model.moduleData[module].failsafeMode != FAILSAFE_NOT_SET && g_model.moduleData[module].failsafeMode != FAILSAFE_RECEIVER);
sendFailsafe = (g_model.moduleData[module].failsafeMode != FAILSAFE_NOT_SET &&
g_model.moduleData[module].failsafeMode != FAILSAFE_RECEIVER);
moduleState[module].counter = 1000;
}
add8ChannelsFrame(module, 0, sendFailsafe);
@@ -194,8 +215,11 @@ void Pxx1Pulses<PxxTransport>::setupFrame(uint8_t module)
}
return;
}
#endif
// Slow PXX1:
// - if more than 8 channels shall be sent, it happens
// in alternating frames
//
if (moduleState[module].counter & 0x01) {
// channelsCount is shifted by 8
sendUpperChannels = g_model.moduleData[module].channelsCount;
@@ -207,7 +231,8 @@ void Pxx1Pulses<PxxTransport>::setupFrame(uint8_t module)
}
} else {
if (moduleState[module].counter == 0) {
sendFailsafe = (g_model.moduleData[module].failsafeMode != FAILSAFE_NOT_SET && g_model.moduleData[module].failsafeMode != FAILSAFE_RECEIVER);
sendFailsafe = (g_model.moduleData[module].failsafeMode != FAILSAFE_NOT_SET &&
g_model.moduleData[module].failsafeMode != FAILSAFE_RECEIVER);
}
}
@@ -218,129 +243,167 @@ void Pxx1Pulses<PxxTransport>::setupFrame(uint8_t module)
}
}
template class Pxx1Pulses<StandardPxx1Transport<PwmPxxBitTransport> >;
template class Pxx1Pulses<StandardPxx1Transport<SerialPxxBitTransport> >;
template class Pxx1Pulses<StandardPxx1Transport>;
template class Pxx1Pulses<UartPxx1Transport>;
#if defined(INTMODULE_USART) && defined(INTERNAL_MODULE_PXX1)
#include "intmodule_serial_driver.h"
static const etx_serial_init pxx1SerialInit = {
.baudrate = INTMODULE_PXX1_SERIAL_BAUDRATE,
.parity = ETX_Parity_None,
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = false,
static const etx_serial_init pxx1SerialCfg = {
.baudrate = 0,
.encoding = ETX_Encoding_8N1,
.direction = ETX_Dir_TX,
.polarity = ETX_Pol_Normal,
};
static void* pxx1InitInternal(uint8_t module)
static const etx_serial_driver_t* _sport_drv;
static void* _sport_ctx;
static void pxx1SportSensorPolling()
{
void* uart_ctx = IntmoduleSerialDriver.init(&pxx1SerialInit);
if (outputTelemetryBuffer.destination != TELEMETRY_ENDPOINT_SPORT)
return;
#if defined(INTMODULE_HEARTBEAT)
init_intmodule_heartbeat();
#endif
mixerSchedulerSetPeriod(INTERNAL_MODULE, INTMODULE_PXX1_SERIAL_PERIOD);
INTERNAL_MODULE_ON();
// Match sensor polling from the module
// -> detect <0x7E [Physical ID]> as the last sequence
uint8_t b;
if (_sport_drv->getLastByte(_sport_ctx, 2, &b) <= 0 || b != START_STOP)
return;
if (_sport_drv->getLastByte(_sport_ctx, 1, &b) <= 0 ||
b != outputTelemetryBuffer.sport.physicalId)
return;
return uart_ctx;
_sport_drv->sendBuffer(_sport_ctx,
outputTelemetryBuffer.data + 1,
outputTelemetryBuffer.size - 1);
outputTelemetryBuffer.reset();
}
static void pxx1DeInitInternal(void* context)
static void* pxx1Init(uint8_t module)
{
INTERNAL_MODULE_OFF();
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
#if defined(INTMODULE_HEARTBEAT)
stop_intmodule_heartbeat();
#endif
IntmoduleSerialDriver.deinit(context);
etx_module_state_t* mod_st = nullptr;
etx_serial_init txCfg(pxx1SerialCfg);
if (module == INTERNAL_MODULE) {
txCfg.baudrate = _pxx1_internal_baudrate;
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &txCfg);
if (!mod_st) {
// assume that radios that don't have an internal UART
// will have a module that uses legacy PXX1 (PWM)
txCfg.encoding = ETX_Encoding_PXX1_PWM;
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, &txCfg);
}
if (!mod_st) return nullptr;
}
if (module == EXTERNAL_MODULE) {
// Init driver (timer / serial) based on module type
uint8_t type = g_model.moduleData[module].type;
switch(type) {
case MODULE_TYPE_R9M_LITE_PXX1: {
txCfg.baudrate = EXTMODULE_PXX1_SERIAL_BAUDRATE;
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &txCfg);
if (!mod_st) return nullptr;
} break;
case MODULE_TYPE_XJT_PXX1:
case MODULE_TYPE_R9M_PXX1: {
txCfg.encoding = ETX_Encoding_PXX1_PWM;
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, &txCfg);
if (!mod_st) return nullptr;
} break;
default:
// unknown module: bail out!
return nullptr;
}
}
// Init telemetry RX
etx_serial_init rxCfg(pxx1SerialCfg);
rxCfg.baudrate = FRSKY_SPORT_BAUDRATE;
rxCfg.direction = ETX_Dir_TX_RX;
// TODO: handle init errors properly
if (modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &rxCfg) != nullptr) {
auto drv = modulePortGetSerialDrv(mod_st->rx);
auto ctx = modulePortGetCtx(mod_st->rx);
if (drv && ctx && drv->setIdleCb) {
_sport_drv = drv;
_sport_ctx = ctx;
drv->setIdleCb(ctx, pxx1SportSensorPolling);
}
}
// Store PXX1 type in 'user_data'
if (txCfg.encoding == ETX_Encoding_PXX1_PWM ||
txCfg.baudrate == PXX1_DEFAULT_SERIAL_BAUDRATE) {
// Legacy / slow PXX1
mixerSchedulerSetPeriod(module, PXX1_DEFAULT_PERIOD);
if (txCfg.encoding == ETX_Encoding_PXX1_PWM) {
mod_st->user_data = (void*)Pxx1Type::PWM;
} else {
mod_st->user_data = (void*)Pxx1Type::SLOW_SERIAL;
}
} else {
// Fast PXX1
mixerSchedulerSetPeriod(module, PXX1_FAST_PERIOD);
mod_st->user_data = (void*)Pxx1Type::FAST_SERIAL;
}
return mod_st;
}
static void pxx1SetupPulsesInternal(void* context, int16_t* channels, uint8_t nChannels)
static void pxx1DeInit(void* ctx)
{
(void)context;
// TODO:
(void)channels;
(void)nChannels;
intmodulePulsesData.pxx_uart.setupFrame(INTERNAL_MODULE);
auto mod_st = (etx_module_state_t*)ctx;
modulePortDeInit(mod_st);
}
static void pxx1SendPulsesInternal(void* context)
static void pxx1SendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_t nChannels)
{
IntmoduleSerialDriver.sendBuffer(context, intmodulePulsesData.pxx_uart.getData(),
intmodulePulsesData.pxx_uart.getSize());
}
const etx_module_driver_t Pxx1InternalSerialDriver = {
.protocol = PROTOCOL_CHANNELS_PXX1_SERIAL,
.init = pxx1InitInternal,
.deinit = pxx1DeInitInternal,
.setupPulses = pxx1SetupPulsesInternal,
.sendPulses = pxx1SendPulsesInternal,
};
#endif
#if defined(EXTMODULE_USART) && defined(HARDWARE_EXTERNAL_MODULE_SIZE_SML)
#include "extmodule_serial_driver.h"
static const etx_serial_init pxx1ExtSerialInit = {
.baudrate = EXTMODULE_PXX1_SERIAL_BAUDRATE,
.parity = ETX_Parity_None,
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = false,
};
static void* pxx1InitExternal(uint8_t module)
{
auto drv = extmoduleGetSerialPort();
if (!drv) return nullptr;
void* uart_ctx = drv->init(&pxx1ExtSerialInit);
mixerSchedulerSetPeriod(EXTERNAL_MODULE, EXTMODULE_PXX1_SERIAL_PERIOD);
EXTERNAL_MODULE_ON();
return uart_ctx;
}
static void pxx1DeInitExternal(void* context)
{
EXTERNAL_MODULE_OFF();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
auto drv = extmoduleGetSerialPort();
if (drv) drv->deinit(context);
}
static void pxx1SetupPulsesExternal(void* context, int16_t* channels, uint8_t nChannels)
{
(void)context;
// TODO:
(void)channels;
(void)nChannels;
extmodulePulsesData.pxx_uart.setupFrame(EXTERNAL_MODULE);
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
auto pxx1_type = (Pxx1Type)(uintptr_t)mod_st->user_data;
uint32_t frame_len = 0;
if (pxx1_type == Pxx1Type::PWM) {
PwmPxx1Pulses frame(buffer);
frame.setupFrame(module, pxx1_type);
frame_len = frame.getSize();
} else {
UartPxx1Pulses frame(buffer);
frame.setupFrame(module, pxx1_type);
frame_len = frame.getSize();
}
if (frame_len == 0) return;
auto drv = modulePortGetSerialDrv(mod_st->tx);
auto drv_ctx = modulePortGetCtx(mod_st->tx);
drv->sendBuffer(drv_ctx, buffer, frame_len);
}
static void pxx1SendPulsesExternal(void* context)
static void pxx1ProcessData(void* ctx, uint8_t data, uint8_t* buffer, uint8_t* len)
{
auto drv = extmoduleGetSerialPort();
if (!drv) return;
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
drv->sendBuffer(context, extmodulePulsesData.pxx_uart.getData(),
extmodulePulsesData.pxx_uart.getSize());
processFrskySportTelemetryData(module, data, buffer, *len);
}
const etx_module_driver_t Pxx1ExternalSerialDriver = {
.protocol = PROTOCOL_CHANNELS_PXX1_SERIAL,
.init = pxx1InitExternal,
.deinit = pxx1DeInitExternal,
.setupPulses = pxx1SetupPulsesExternal,
.sendPulses = pxx1SendPulsesExternal,
const etx_proto_driver_t Pxx1Driver = {
.protocol = PROTOCOL_CHANNELS_PXX1,
.init = pxx1Init,
.deinit = pxx1DeInit,
.sendPulses = pxx1SendPulses,
.processData = pxx1ProcessData,
};
#endif
+4 -153
View File
@@ -19,160 +19,11 @@
* GNU General Public License for more details.
*/
#ifndef _PULSES_PXX1_H_
#define _PULSES_PXX1_H_
#include "pxx.h"
#include "crc.h"
#pragma once
#include "hal/module_driver.h"
class Pxx1CrcMixin {
protected:
void initCrc()
{
crc = 0;
}
// Set the board specific PXX1 baudrate (X-lite only?)
void pxx1SetInternalBaudrate(uint32_t baudrate);
void addToCrc(uint8_t byte)
{
crc = (crc << 8) ^ (crc16tab[CRC_1189][((crc >> 8) ^ byte) & 0xFF]);
}
uint16_t crc;
};
template <class BitTransport>
class StandardPxx1Transport: public BitTransport, public Pxx1CrcMixin {
protected:
uint8_t ones_count;
void initFrame(uint32_t period)
{
BitTransport::initFrame(period);
ones_count = 0;
}
void addByte(uint8_t byte)
{
Pxx1CrcMixin::addToCrc(byte);
addByteWithoutCrc(byte);
};
void addRawByte(uint8_t byte)
{
for (uint8_t i = 0; i < 8; i++) {
if (byte & 0x80)
BitTransport::addPart(1);
else
BitTransport::addPart(0);
byte <<= 1;
}
}
void addByteWithoutCrc(uint8_t byte)
{
for (uint8_t i = 0; i < 8; i++) {
addBit(byte & 0x80);
byte <<= 1;
}
}
void addBit(uint8_t bit)
{
if (bit) {
BitTransport::addPart(1);
if (++ones_count == 5) {
ones_count = 0;
BitTransport::addPart(0); // Stuff a 0 bit in
}
}
else {
BitTransport::addPart(0);
ones_count = 0;
}
}
};
class UartPxx1Transport: public DataBuffer<uint8_t, 64>, public Pxx1CrcMixin {
protected:
void initFrame(uint32_t period)
{
initBuffer();
}
void addByte(uint8_t byte)
{
Pxx1CrcMixin::addToCrc(byte);
addWithByteStuffing(byte);
}
void addRawByte(uint8_t byte)
{
*ptr++ = byte;
}
void addByteWithoutCrc(uint8_t byte)
{
addWithByteStuffing(byte);
}
void addWithByteStuffing(uint8_t byte)
{
if (0x7E == byte) {
addRawByte(0x7D);
addRawByte(0x5E);
}
else if (0x7D == byte) {
addRawByte(0x7D);
addRawByte(0x5D);
}
else {
addRawByte(byte);
}
}
void addTail()
{
// nothing
}
};
template <class PxxTransport>
class Pxx1Pulses: public PxxTransport
{
public:
void setupFrame(uint8_t port);
protected:
void addHead()
{
// send 7E, do not CRC
PxxTransport::addRawByte(0x7E);
}
void addCrc()
{
PxxTransport::addByteWithoutCrc(Pxx1CrcMixin::crc >> 8);
PxxTransport::addByteWithoutCrc(Pxx1CrcMixin::crc);
}
void addFlag1(uint8_t port, uint8_t sendFailsafe);
void addExtraFlags(uint8_t port);
void addChannels(uint8_t port, uint8_t sendFailsafe, uint8_t sendUpperChannels);
void add8ChannelsFrame(uint8_t port, uint8_t sendUpperChannels, uint8_t sendFailsafe);
};
typedef Pxx1Pulses<UartPxx1Transport> UartPxx1Pulses;
typedef Pxx1Pulses<StandardPxx1Transport<PwmPxxBitTransport>> PwmPxx1Pulses;
typedef Pxx1Pulses<StandardPxx1Transport<SerialPxxBitTransport>> SerialPxx1Pulses;
#if defined(INTMODULE_USART)
extern const etx_module_driver_t Pxx1InternalSerialDriver;
#endif
#if defined(EXTMODULE_USART)
extern const etx_module_driver_t Pxx1ExternalSerialDriver;
#endif
#endif
extern const etx_proto_driver_t Pxx1Driver;
+212
View File
@@ -0,0 +1,212 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* 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.
*/
#pragma once
#include "pxx.h"
#include "crc.h"
class Pxx1CrcMixin {
protected:
void initCrc()
{
crc = 0;
}
void addToCrc(uint8_t byte)
{
crc = (crc << 8) ^ (crc16tab[CRC_1189][((crc >> 8) ^ byte) & 0xFF]);
}
uint16_t crc;
};
class StandardPxx1Transport: public Pxx1CrcMixin {
protected:
const uint8_t* buffer;
uint8_t* ptr;
uint8_t byte = 0;
uint8_t bits_count = 0;
uint8_t ones_count = 0;
StandardPxx1Transport(uint8_t* buffer)
: buffer(buffer), ptr(buffer)
{
}
void addByte(uint8_t b)
{
Pxx1CrcMixin::addToCrc(b);
addByteWithoutCrc(b);
};
void addRawByte(uint8_t b)
{
for (uint8_t i = 0; i < 8; i++) {
// MSB first
if (b & 0x80) addPart(1);
else addPart(0);
b <<= 1;
}
}
void addByteWithoutCrc(uint8_t b)
{
for (uint8_t i = 0; i < 8; i++) {
// MSB first
addBit(b & 0x80);
b <<= 1;
}
}
void addBit(uint8_t bit)
{
if (bit) {
addPart(1);
if (++ones_count == 5) {
ones_count = 0;
addPart(0); // Stuff a 0 bit in
}
}
else {
addPart(0);
ones_count = 0;
}
}
void addPart(uint8_t bit)
{
// MSB first
byte <<= 1;
if (bit & 1) {
byte |= 1;
}
if (++bits_count >= 8) {
*ptr++ = byte;
bits_count = 0;
}
}
void addTail()
{
if (bits_count > 0)
*ptr++ = byte << (8 - bits_count);
}
public:
uint32_t getSize()
{
uint32_t size = (ptr - buffer) * 8;
if (bits_count > 0)
size -= (8 - bits_count);
return size;
}
};
class UartPxx1Transport: public Pxx1CrcMixin {
protected:
const uint8_t* buffer;
uint8_t* ptr;
UartPxx1Transport(uint8_t* buffer)
: buffer(buffer), ptr(buffer)
{
}
void addByte(uint8_t byte)
{
Pxx1CrcMixin::addToCrc(byte);
addWithByteStuffing(byte);
}
void addRawByte(uint8_t byte)
{
*ptr++ = byte;
}
void addByteWithoutCrc(uint8_t byte)
{
addWithByteStuffing(byte);
}
void addWithByteStuffing(uint8_t byte)
{
if (0x7E == byte) {
addRawByte(0x7D);
addRawByte(0x5E);
}
else if (0x7D == byte) {
addRawByte(0x7D);
addRawByte(0x5D);
}
else {
addRawByte(byte);
}
}
void addTail()
{
// nothing
}
public:
uint32_t getSize()
{
return ptr - buffer;
}
};
enum class Pxx1Type : uintptr_t {
PWM,
SLOW_SERIAL,
FAST_SERIAL,
};
template <class PxxTransport>
class Pxx1Pulses: public PxxTransport
{
public:
Pxx1Pulses(uint8_t* buffer);
void setupFrame(uint8_t module, Pxx1Type type);
protected:
void addHead()
{
// send 7E, do not CRC
PxxTransport::addRawByte(0x7E);
}
void addCrc()
{
PxxTransport::addByteWithoutCrc(Pxx1CrcMixin::crc >> 8);
PxxTransport::addByteWithoutCrc(Pxx1CrcMixin::crc);
}
void addFlag1(uint8_t port, uint8_t sendFailsafe);
void addExtraFlags(uint8_t port);
void addChannels(uint8_t port, uint8_t sendFailsafe, uint8_t sendUpperChannels);
void add8ChannelsFrame(uint8_t port, uint8_t sendUpperChannels, uint8_t sendFailsafe);
};
typedef Pxx1Pulses<UartPxx1Transport> UartPxx1Pulses;
typedef Pxx1Pulses<StandardPxx1Transport> PwmPxx1Pulses;
+184 -298
View File
@@ -21,24 +21,117 @@
#include <stdio.h>
#include "opentx.h"
#include "pulses/pxx2.h"
#include "io/frsky_firmware_update.h"
#include "libopenui/src/libopenui_file.h"
#include "mixer_scheduler.h"
#include "heartbeat_driver.h"
#include "timers_driver.h"
#if defined(INTMODULE_USART)
#include "intmodule_serial_driver.h"
#include "pxx2.h"
#include "pxx2_transport.h"
const etx_serial_init pxx2SerialInitParams = {
static const etx_serial_init pxx2SerialInitParams = {
.baudrate = PXX2_HIGHSPEED_BAUDRATE,
.parity = ETX_Parity_None,
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.encoding = ETX_Encoding_8N1,
.direction = ETX_Dir_TX_RX,
.polarity = ETX_Pol_Normal,
};
#include "hal/module_port.h"
/* Module options order:
* - External antenna (0x01)
* - Power (0x02)
* - Spektrum analyser (0x04)
* - Power meter (0x08)
*/
static const uint8_t PXX2ModuleOptions[] = {
#if defined(SIMU)
0b11111111, // None = display all options on SIMU
#else
0b00000000, // None = no option available on unknown modules
#endif
0b00000001, // XJT
0b00000001, // ISRM
0b00001101, // ISRM-PRO
0b00000101, // ISRM-S
0b00000010, // R9M
0b00000010, // R9MLite
0b00000110, // R9MLite-PRO
0b00000100, // ISRM-N
0b00000100, // ISRM-S-X9
0b00000101, // ISRM-S-X10E
0b00000001, // XJT_LITE
0b00000101, // ISRM-S-X10S
0b00000100, // ISRM-X9LITES
};
uint8_t getPXX2ModuleOptions(uint8_t modelId)
{
if (modelId < DIM(PXX2ModuleOptions))
return PXX2ModuleOptions[modelId];
else
return PXX2ModuleOptions[0];
}
bool isPXX2ModuleOptionAvailable(uint8_t modelId, uint8_t option)
{
return getPXX2ModuleOptions(modelId) & (1 << option);
}
/* Receiver options order:
* - OTA (0x01)
*/
static const uint8_t PXX2ReceiverOptions[] = {
#if defined(SIMU)
0b11111111, // None = display all options on SIMU
#else
0b00000000, // None = display all options on SIMU
#endif
0b11111110, // X8R
0b11111110, // RX8R
0b11111110, // RX8R-PRO
0b11111111, // RX6R
0b11111111, // RX4R
0b11111111, // G-RX8
0b11111111, // G-RX6
0b11111110, // X6R
0b11111110, // X4R
0b11111110, // X4R-SB
0b11111110, // XSR
0b11111110, // XSR-M
0b11111111, // RXSR
0b11111110, // S6R
0b11111110, // S8R
0b11111110, // XM
0b11111110, // XM+
0b11111110, // XMR
0b11111110, // R9
0b11111110, // R9-SLIM
0b11111110, // R9-SLIM+
0b11111110, // R9-MINI
0b11111110, // R9-MM
0b11111111, // R9-STAB+OTA
0b11111111, // R9-MINI+OTA
0b11111111, // R9-MM+OTA
0b11111111, // R9-SLIM+OTA
0b11111111, // ARCHER-X
0b11111111, // R9MX
0b11111111, // R9SX
};
uint8_t getPXX2ReceiverOptions(uint8_t modelId)
{
if (modelId < DIM(PXX2ReceiverOptions))
return PXX2ReceiverOptions[modelId];
else
return PXX2ReceiverOptions[0];
}
bool isPXX2ReceiverOptionAvailable(uint8_t modelId, uint8_t option)
{
return getPXX2ReceiverOptions(modelId) & (1 << option);
}
bool isPXX2PowerAvailable(const PXX2HardwareInformation& info, int value)
{
@@ -435,10 +528,12 @@ void Pxx2Pulses::setupShareMode(uint8_t module)
Pxx2Transport::addByte(reusableBuffer.moduleSetup.pxx2.shareReceiverIndex);
}
void Pxx2Pulses::sendOtaUpdate(uint8_t module, const char * rxName, uint32_t address, const char * data)
{
initFrame();
// TODO: move up
static void _send_frame(etx_module_state_t* mod_st, uint8_t* buffer, uint32_t len);
void Pxx2Pulses::sendOtaUpdate(uint8_t module, const char* rxName,
uint32_t address, const char* data)
{
addFrameType(PXX2_TYPE_C_OTA, PXX2_TYPE_ID_OTA);
if (rxName) {
@@ -459,22 +554,11 @@ void Pxx2Pulses::sendOtaUpdate(uint8_t module, const char * rxName, uint32_t add
}
endFrame();
#if defined(HARDWARE_INTERNAL_MODULE)
if (module == INTERNAL_MODULE) {
intmoduleSendNextFrame();
}
#endif
#if defined(HARDWARE_EXTERNAL_MODULE)
if (module == EXTERNAL_MODULE) {
extmoduleSendNextFrame();
}
#endif
}
void Pxx2Pulses::setupAuthenticationFrame(uint8_t module, uint8_t mode, const uint8_t * outputMessage)
{
initFrame();
// initFrame();
addFrameType(PXX2_TYPE_C_MODULE, PXX2_TYPE_ID_AUTHENTICATION);
@@ -498,7 +582,7 @@ bool Pxx2Pulses::setupFrame(uint8_t module, int16_t* channels, uint8_t nChannels
return false;
}
initFrame();
// initFrame();
switch (moduleState[module].mode) {
case MODULE_MODE_GET_HARDWARE_INFO:
@@ -548,216 +632,96 @@ bool Pxx2Pulses::setupFrame(uint8_t module, int16_t* channels, uint8_t nChannels
return true;
}
bool Pxx2OtaUpdate::waitStep(uint8_t step, uint8_t timeout)
static void* pxx2Init(uint8_t module)
{
OtaUpdateInformation * destination = moduleState[module].otaUpdateInformation;
uint8_t elapsed = 0;
etx_module_state_t* mod_st = nullptr;
etx_serial_init params(pxx2SerialInitParams);
watchdogSuspend(100 /*1s*/);
if (module == INTERNAL_MODULE) {
while (step != destination->step) {
if (elapsed++ > timeout) {
return false;
}
RTOS_WAIT_MS(1);
telemetryWakeup();
params.baudrate = PXX2_HIGHSPEED_BAUDRATE;
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &params);
if (!mod_st) return nullptr;
resetAccessAuthenticationCount();
}
return true;
}
if (module == EXTERNAL_MODULE) {
const char * Pxx2OtaUpdate::nextStep(uint8_t step, const char * rxName, uint32_t address, const uint8_t * buffer)
{
OtaUpdateInformation * destination = moduleState[module].otaUpdateInformation;
uint8_t type = g_model.moduleData[module].type;
switch(type) {
destination->step = step;
destination->address = address;
case MODULE_TYPE_R9M_LITE_PXX2:
params.baudrate = PXX2_LOWSPEED_BAUDRATE;
break;
for (uint8_t retry = 0;; retry++) {
if (module == EXTERNAL_MODULE) {
extmodulePulsesData.pxx2.sendOtaUpdate(module, rxName, address, (const char *) buffer);
}
else if (module == INTERNAL_MODULE) {
intmodulePulsesData.pxx2.sendOtaUpdate(module, rxName, address, (const char *) buffer);
}
if (waitStep(step + 1, 20)) {
case MODULE_TYPE_ISRM_PXX2:
case MODULE_TYPE_R9M_PXX2:
case MODULE_TYPE_XJT_LITE_PXX2:
case MODULE_TYPE_R9M_LITE_PRO_PXX2:
params.baudrate = PXX2_HIGHSPEED_BAUDRATE;
break;
default:
return nullptr;
}
else if (retry == 100) {
return "Transfer failed";
}
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &params);
if (!mod_st) return nullptr;
}
mixerSchedulerSetPeriod(module, PXX2_DEFAULT_PERIOD);
return mod_st;
}
const char * Pxx2OtaUpdate::doFlashFirmware(const char * filename, ProgressHandler progressHandler)
static void pxx2DeInit(void* ctx)
{
FIL file;
uint8_t buffer[32];
UINT count;
const char * result;
auto mod_st = (etx_module_state_t*)ctx;
modulePortDeInit(mod_st);
}
result = nextStep(OTA_UPDATE_START, rxName, 0, nullptr);
if (result) {
return result;
}
static void _send_frame(etx_module_state_t* mod_st, uint8_t* buffer, uint32_t len)
{
auto drv = modulePortGetSerialDrv(mod_st->tx);
auto ctx = modulePortGetCtx(mod_st->tx);
drv->sendBuffer(ctx, buffer, len);
}
if (f_open(&file, filename, FA_READ) != FR_OK) {
return "Open file failed";
}
static void pxx2SendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_t nChannels)
{
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
uint32_t size;
const char * ext = getFileExtension(filename);
if (ext && !strcasecmp(ext, FRSKY_FIRMWARE_EXT)) {
FrSkyFirmwareInformation * information = (FrSkyFirmwareInformation *) buffer;
if (f_read(&file, buffer, sizeof(FrSkyFirmwareInformation), &count) != FR_OK || count != sizeof(FrSkyFirmwareInformation)) {
f_close(&file);
return "Format error";
}
size = information->size;
}
else {
size = f_size(&file);
}
if (module == INTERNAL_MODULE) {
Pxx2Pulses pxx2(buffer);
bool should_send = pxx2.setupFrame(module, channels, nChannels);
uint32_t done = 0;
while (1) {
progressHandler(getBasename(filename), STR_OTA_UPDATE, done, size);
if (f_read(&file, buffer, sizeof(buffer), &count) != FR_OK) {
f_close(&file);
return "Read file failed";
if (moduleState[module].mode == MODULE_MODE_SPECTRUM_ANALYSER ||
moduleState[module].mode == MODULE_MODE_POWER_METER) {
// backup normal period into 'user_data'
if (!mod_st->user_data) {
mod_st->user_data = (void*)(uintptr_t)mixerSchedulerGetPeriod(module);
}
mixerSchedulerSetPeriod(module, PXX2_TOOLS_PERIOD);
} else if (mod_st->user_data) {
// restore old scheduling period
uintptr_t period = (uintptr_t)mod_st->user_data;
mixerSchedulerSetPeriod(module, period);
mod_st->user_data = nullptr;
}
result = nextStep(OTA_UPDATE_TRANSFER, nullptr, done, buffer);
if (result) {
return result;
}
if (count < sizeof(buffer)) {
f_close(&file);
break;
}
done += count;
if (!should_send) return;
_send_frame(mod_st, buffer, pxx2.getSize());
}
return nextStep(OTA_UPDATE_EOF, nullptr, done, nullptr);
}
void Pxx2OtaUpdate::flashFirmware(const char * filename, ProgressHandler progressHandler)
{
pausePulses();
watchdogSuspend(100 /*1s*/);
RTOS_WAIT_MS(100);
moduleState[module].mode = MODULE_MODE_OTA_UPDATE;
const char * result = doFlashFirmware(filename, progressHandler);
moduleState[module].mode = MODULE_MODE_NORMAL;
AUDIO_PLAY(AU_SPECIAL_SOUND_BEEP1 );
BACKLIGHT_ENABLE();
if (result) {
POPUP_WARNING(STR_FIRMWARE_UPDATE_ERROR, result);
}
else {
POPUP_INFORMATION(STR_FIRMWARE_UPDATE_SUCCESS);
}
watchdogSuspend(100);
RTOS_WAIT_MS(100);
resumePulses();
}
struct PXX2State {
uint8_t module;
Pxx2Pulses* pulses;
const etx_serial_driver_t* uart_drv;
void* uart_ctx;
void init(uint8_t _module, Pxx2Pulses* _pulses, const etx_serial_driver_t* _drv, void* _ctx)
{
module = _module;
pulses = _pulses;
uart_drv = _drv;
uart_ctx = _ctx;
}
void deinit() { uart_drv->deinit(uart_ctx); }
};
static PXX2State pxx2State[NUM_MODULES];
static void* pxx2InitInternal(uint8_t module)
{
#if defined(INTMODULE_HEARTBEAT)
// use backup trigger (1 ms later)
init_intmodule_heartbeat();
#endif
mixerSchedulerSetPeriod(module, PXX2_PERIOD);
INTERNAL_MODULE_ON();
resetAccessAuthenticationCount();
auto state = &pxx2State[module];
state->init(module, &intmodulePulsesData.pxx2, &IntmoduleSerialDriver,
IntmoduleSerialDriver.init(&pxx2SerialInitParams));
return state;
}
static void pxx2DeInitInternal(void* context)
{
auto state = (PXX2State*)context;
state->deinit();
INTERNAL_MODULE_OFF();
#if defined(INTMODULE_HEARTBEAT)
stop_intmodule_heartbeat();
#endif
mixerSchedulerSetPeriod(state->module, 0);
}
// TODO: move this to Pxx2Pulses
static bool pxx2InternalSendNextFrame = true;
static void pxx2SetupPulsesInternal(void* context, int16_t* channels, uint8_t nChannels)
{
auto state = (PXX2State*)context;
auto pulses = state->pulses;
auto module = state->module;
pxx2InternalSendNextFrame = pulses->setupFrame(module, channels, nChannels);
if (moduleState[module].mode == MODULE_MODE_SPECTRUM_ANALYSER ||
moduleState[module].mode == MODULE_MODE_POWER_METER) {
mixerSchedulerSetPeriod(module, PXX2_TOOLS_PERIOD);
} else {
mixerSchedulerSetPeriod(module, PXX2_PERIOD);
if (module == EXTERNAL_MODULE) {
Pxx2Pulses pxx2(buffer);
pxx2.setupFrame(module, channels, nChannels);
_send_frame(mod_st, buffer, pxx2.getSize());
}
}
static void pxx2SendPulsesInternal(void* context)
{
if (pxx2InternalSendNextFrame) {
auto state = (PXX2State*)context;
auto drv = state->uart_drv;
auto ctx = state->uart_ctx;
auto pulses = state->pulses;
drv->sendBuffer(ctx, pulses->getData(), pulses->getSize());
}
}
static int pxx2GetByte(void* context, uint8_t* data)
{
auto state = (PXX2State*)context;
auto drv = state->uart_drv;
auto ctx = state->uart_ctx;
return drv->getByte(ctx, data);
}
static void pxx2ProcessData(void* context, uint8_t data, uint8_t* buffer, uint8_t* len)
static void pxx2ProcessData(void* ctx, uint8_t data, uint8_t* buffer, uint8_t* len)
{
if (*len == 0 && data != START_STOP) {
return;
@@ -802,99 +766,21 @@ static void pxx2ProcessData(void* context, uint8_t data, uint8_t* buffer, uint8_
return;
}
auto state = (PXX2State*)context;
auto module = state->module;
auto drv = state->uart_drv;
auto ctx = state->uart_ctx;
processPXX2Frame(module, frame, drv, ctx);
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
auto drv = modulePortGetSerialDrv(mod_st->rx);
auto drv_ctx = modulePortGetCtx(mod_st->rx);
processPXX2Frame(module, frame, drv, drv_ctx);
*len = 0;
}
#include "hal/module_driver.h"
#include "extmodule_serial_driver.h"
// #include "extmodule_serial_driver.h"
const etx_module_driver_t Pxx2InternalDriver = {
.protocol = PROTOCOL_CHANNELS_PXX2_HIGHSPEED,
.init = pxx2InitInternal,
.deinit = pxx2DeInitInternal,
.setupPulses = pxx2SetupPulsesInternal,
.sendPulses = pxx2SendPulsesInternal,
.getByte = pxx2GetByte,
.processData = pxx2ProcessData,
};
static void* pxx2InitExternal(uint8_t module, uint32_t baudrate)
{
etx_serial_init params(pxx2SerialInitParams);
params.baudrate = baudrate;
mixerSchedulerSetPeriod(module, PXX2_NO_HEARTBEAT_PERIOD);
EXTERNAL_MODULE_ON();
telemetryProtocol = PROTOCOL_TELEMETRY_FRSKY_SPORT;
auto state = &pxx2State[module];
auto drv = extmoduleGetSerialPort();
if (!drv) return nullptr;
state->init(module, &extmodulePulsesData.pxx2, drv, drv->init(&params));
return state;
}
static void* pxx2InitExtLowSpeed(uint8_t module)
{
return pxx2InitExternal(module, PXX2_LOWSPEED_BAUDRATE);
}
static void* pxx2InitExtHighSpeed(uint8_t module)
{
return pxx2InitExternal(module, PXX2_HIGHSPEED_BAUDRATE);
}
static void pxx2DeInitExternal(void* context)
{
auto state = (PXX2State*)context;
if (state) state->deinit();
EXTERNAL_MODULE_OFF();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
telemetryProtocol = 0xFF;
}
static void pxx2SetupPulsesExternal(void* context, int16_t* channels, uint8_t nChannels)
{
auto state = (PXX2State*)context;
auto pulses = state->pulses;
auto module = state->module;
pulses->setupFrame(module, channels, nChannels);
}
static void pxx2SendPulsesExternal(void* context)
{
auto state = (PXX2State*)context;
auto drv = state->uart_drv;
auto ctx = state->uart_ctx;
auto pulses = state->pulses;
drv->sendBuffer(ctx, pulses->getData(), pulses->getSize());
}
const etx_module_driver_t Pxx2ExternalDriver = {
.protocol = PROTOCOL_CHANNELS_PXX2_HIGHSPEED,
.init = pxx2InitExtHighSpeed,
.deinit = pxx2DeInitExternal,
.setupPulses = pxx2SetupPulsesExternal,
.sendPulses = pxx2SendPulsesExternal,
.getByte = pxx2GetByte,
.processData = pxx2ProcessData,
};
const etx_module_driver_t Pxx2LowSpeedExternalDriver = {
.protocol = PROTOCOL_CHANNELS_PXX2_LOWSPEED,
.init = pxx2InitExtLowSpeed,
.deinit = pxx2DeInitExternal,
.setupPulses = pxx2SetupPulsesExternal,
.sendPulses = pxx2SendPulsesExternal,
.getByte = pxx2GetByte,
const etx_proto_driver_t Pxx2Driver = {
.protocol = PROTOCOL_CHANNELS_PXX2,
.init = pxx2Init,
.deinit = pxx2DeInit,
.sendPulses = pxx2SendPulses,
.processData = pxx2ProcessData,
};
+12 -332
View File
@@ -22,13 +22,12 @@
#ifndef _PULSES_PXX2_H_
#define _PULSES_PXX2_H_
#include "fifo.h"
#include "popups.h"
#include "pxx.h"
#include "hal/serial_driver.h"
#include "hal/module_driver.h"
#include "dataconstants.h"
#include "opentx_types.h"
#define PXX2_TYPE_C_MODULE 0x01
#define PXX2_TYPE_ID_REGISTER 0x01
#define PXX2_TYPE_ID_BIND 0x02
@@ -67,11 +66,7 @@
#define PXX2_HW_INFO_TX_ID 0xFF
#if defined(COLORLCD)
#define SPECTRUM_ANALYSER_POWER_FLOOR -120 /*dBm*/
#else
#define SPECTRUM_ANALYSER_POWER_FLOOR -120 /*dBm*/
#endif
#define SPECTRUM_ANALYSER_POWER_FLOOR -120 /*dBm*/
#define PXX2_AUTH_REFUSED_FLAG 0xA5
@@ -92,30 +87,7 @@ enum PXX2ModuleModelID {
PXX2_MODULE_ISRM_X9LITES,
};
static const char * const PXX2ModulesNames[] = {
"---",
"XJT",
"ISRM",
"ISRM-PRO",
"ISRM-S",
"R9M",
"R9MLite",
"R9MLite-PRO",
"ISRM-N",
"ISRM-S-X9",
"ISRM-S-X10E",
"XJT Lite",
"ISRM-S-X10S",
"ISRM-X9LiteS"
};
inline const char * getPXX2ModuleName(uint8_t modelId)
{
if (modelId < DIM(PXX2ModulesNames))
return PXX2ModulesNames[modelId];
else
return PXX2ModulesNames[0];
}
const char * getPXX2ModuleName(uint8_t modelId);
enum {
MODULE_OPTION_EXTERNAL_ANTENNA,
@@ -124,149 +96,21 @@ enum {
MODULE_OPTION_POWER_METER,
};
/* Module options order:
* - External antenna (0x01)
* - Power (0x02)
* - Spektrum analyser (0x04)
* - Power meter (0x08)
*/
static const uint8_t PXX2ModuleOptions[] = {
#if defined(SIMU)
0b11111111, // None = display all options on SIMU
#else
0b00000000, // None = no option available on unknown modules
#endif
0b00000001, // XJT
0b00000001, // ISRM
0b00001101, // ISRM-PRO
0b00000101, // ISRM-S
0b00000010, // R9M
0b00000010, // R9MLite
0b00000110, // R9MLite-PRO
0b00000100, // ISRM-N
0b00000100, // ISRM-S-X9
0b00000101, // ISRM-S-X10E
0b00000001, // XJT_LITE
0b00000101, // ISRM-S-X10S
0b00000100, // ISRM-X9LITES
};
inline uint8_t getPXX2ModuleOptions(uint8_t modelId)
{
if (modelId < DIM(PXX2ModuleOptions))
return PXX2ModuleOptions[modelId];
else
return PXX2ModuleOptions[0];
}
inline bool isPXX2ModuleOptionAvailable(uint8_t modelId, uint8_t option)
{
return getPXX2ModuleOptions(modelId) & (1 << option);
}
uint8_t getPXX2ModuleOptions(uint8_t modelId);
bool isPXX2ModuleOptionAvailable(uint8_t modelId, uint8_t option);
enum ModuleCapabilities {
MODULE_CAPABILITY_COUNT
};
static const char * const PXX2ReceiversNames[] = {
"---",
"X8R",
"RX8R",
"RX8R-PRO",
"RX6R",
"RX4R",
"G-RX8",
"G-RX6",
"X6R",
"X4R",
"X4R-SB",
"XSR",
"XSR-M",
"RXSR",
"S6R",
"S8R",
"XM",
"XM+",
"XMR",
"R9",
"R9-SLIM",
"R9-SLIM+",
"R9-MINI",
"R9-MM",
"R9-STAB", // R9-STAB has OTA
"R9-MINI-OTA", // this one has OTA (different bootloader)
"R9-MM-OTA", // this one has OTA (different bootloader)
"R9-SLIM+-OTA", // this one has OTA (different bootloader)
"Archer-X", // this one has OTA (internal module)
"R9MX", // this one has OTA
"R9SX", // this one has OTA
};
inline const char * getPXX2ReceiverName(uint8_t modelId)
{
if (modelId < DIM(PXX2ReceiversNames))
return PXX2ReceiversNames[modelId];
else
return PXX2ReceiversNames[0];
}
const char * getPXX2ReceiverName(uint8_t modelId);
enum {
RECEIVER_OPTION_OTA,
};
/* Receiver options order:
* - OTA (0x01)
*/
static const uint8_t PXX2ReceiverOptions[] = {
#if defined(SIMU)
0b11111111, // None = display all options on SIMU
#else
0b00000000, // None = display all options on SIMU
#endif
0b11111110, // X8R
0b11111110, // RX8R
0b11111110, // RX8R-PRO
0b11111111, // RX6R
0b11111111, // RX4R
0b11111111, // G-RX8
0b11111111, // G-RX6
0b11111110, // X6R
0b11111110, // X4R
0b11111110, // X4R-SB
0b11111110, // XSR
0b11111110, // XSR-M
0b11111111, // RXSR
0b11111110, // S6R
0b11111110, // S8R
0b11111110, // XM
0b11111110, // XM+
0b11111110, // XMR
0b11111110, // R9
0b11111110, // R9-SLIM
0b11111110, // R9-SLIM+
0b11111110, // R9-MINI
0b11111110, // R9-MM
0b11111111, // R9-STAB+OTA
0b11111111, // R9-MINI+OTA
0b11111111, // R9-MM+OTA
0b11111111, // R9-SLIM+OTA
0b11111111, // ARCHER-X
0b11111111, // R9MX
0b11111111, // R9SX
};
inline uint8_t getPXX2ReceiverOptions(uint8_t modelId)
{
if (modelId < DIM(PXX2ReceiverOptions))
return PXX2ReceiverOptions[modelId];
else
return PXX2ReceiverOptions[0];
}
inline bool isPXX2ReceiverOptionAvailable(uint8_t modelId, uint8_t option)
{
return getPXX2ReceiverOptions(modelId) & (1 << option);
}
uint8_t getPXX2ReceiverOptions(uint8_t modelId);
bool isPXX2ReceiverOptionAvailable(uint8_t modelId, uint8_t option);
enum ReceiverCapabilities {
RECEIVER_CAPABILITY_FPORT,
@@ -403,15 +247,8 @@ class BindInformation {
PXX2HardwareInformation receiverInformation;
};
class OtaUpdateInformation: public BindInformation {
public:
char filename[FF_MAX_LFN + 1];
uint32_t address;
uint32_t module;
};
struct PXX2HardwareAndSettings {
ModuleInformation modules[NUM_MODULES];
ModuleInformation modules[MAX_MODULES];
uint32_t updateTime;
ModuleSettings moduleSettings;
ReceiverSettings receiverSettings; // when dealing with receiver settings, we
@@ -423,163 +260,6 @@ PXX2ModuleSetup& getPXX2ModuleSetupBuffer();
BindInformation& getPXX2BindInformationBuffer();
PXX2HardwareAndSettings& getPXX2HardwareAndSettingsBuffer();
class Pxx2CrcMixin {
protected:
void initCrc()
{
crc = 0xFFFF;
}
void addToCrc(uint8_t byte)
{
crc -= byte;
}
uint16_t crc;
};
class Pxx2Transport: public DataBuffer<uint8_t, 64>, public Pxx2CrcMixin {
protected:
void addWord(uint32_t word)
{
addByte(word);
addByte(word >> 8);
addByte(word >> 16);
addByte(word >> 24);
}
void addByte(uint8_t byte)
{
Pxx2CrcMixin::addToCrc(byte);
addByteWithoutCrc(byte);
};
void addByteWithoutCrc(uint8_t byte)
{
*ptr++ = byte;
}
};
class Pxx2Pulses: public Pxx2Transport {
friend class Pxx2OtaUpdate;
public:
bool setupFrame(uint8_t module, int16_t* channels, uint8_t nChannels);
void setupAuthenticationFrame(uint8_t module, uint8_t mode, const uint8_t * outputMessage);
protected:
void setupHardwareInfoFrame(uint8_t module, int16_t* channels, uint8_t nChannels);
void setupRegisterFrame(uint8_t module);
void setupBindFrame(uint8_t module);
void setupAccstBindFrame(uint8_t module);
void setupAccessBindFrame(uint8_t module);
void setupResetFrame(uint8_t module);
void setupShareMode(uint8_t module);
void setupModuleSettingsFrame(uint8_t module, int16_t* channels, uint8_t nChannels);
void setupReceiverSettingsFrame(uint8_t module, int16_t* channels, uint8_t nChannels);
void setupChannelsFrame(uint8_t module, int16_t* channels, uint8_t nChannels);
void setupTelemetryFrame(uint8_t module);
void setupSpectrumAnalyser(uint8_t module);
void setupPowerMeter(uint8_t module);
void sendOtaUpdate(uint8_t module, const char * rxName, uint32_t address, const char * data);
void addHead()
{
// send 7E, do not CRC
Pxx2Transport::addByteWithoutCrc(0x7E);
// reserve 1 byte for LEN
Pxx2Transport::addByteWithoutCrc(0x00);
}
void addFrameType(uint8_t type_c, uint8_t type_id)
{
// TYPE_C + TYPE_ID
Pxx2Transport::addByte(type_c);
Pxx2Transport::addByte(type_id);
}
uint8_t addFlag0(uint8_t module);
void addFlag1(uint8_t module);
void addPulsesValues(uint16_t low, uint16_t high);
void addChannels(uint8_t module, int16_t* channels, uint8_t nChannels);
void addFailsafe(uint8_t module);
void addCrc()
{
Pxx2Transport::addByteWithoutCrc(Pxx2CrcMixin::crc >> 8);
Pxx2Transport::addByteWithoutCrc(Pxx2CrcMixin::crc);
}
void initFrame()
{
// init the CRC counter
initCrc();
// reset the frame pointer
Pxx2Transport::initBuffer();
// add the frame head
addHead();
}
void endFrame()
{
uint8_t size = getSize() - 2;
if (size > 0) {
// update the frame LEN = frame length minus the 2 first bytes
data[1] = getSize() - 2;
// now add the CRC
addCrc();
}
else {
Pxx2Transport::initBuffer();
}
}
};
class Pxx2OtaUpdate {
public:
Pxx2OtaUpdate(uint8_t module, const char * rxName):
module(module),
rxName(rxName)
{
}
void flashFirmware(const char * filename, ProgressHandler progressHandler);
protected:
uint8_t module;
const char * rxName;
const char * doFlashFirmware(const char * filename, ProgressHandler progressHandler);
bool waitStep(uint8_t step, uint8_t timeout);
const char * nextStep(uint8_t step, const char * rxName, uint32_t address, const uint8_t * buffer);
};
extern const etx_serial_init pxx2SerialInitParams;
extern const etx_module_driver_t Pxx2InternalDriver;
extern const etx_module_driver_t Pxx2ExternalDriver;
extern const etx_module_driver_t Pxx2LowSpeedExternalDriver;
extern const etx_proto_driver_t Pxx2Driver;
#endif
+160
View File
@@ -0,0 +1,160 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* 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"
#include "io/frsky_firmware_update.h"
#include "tasks/mixer_task.h"
#include "pxx2_ota.h"
#include "pxx2_transport.h"
bool Pxx2OtaUpdate::waitStep(uint8_t step, uint8_t timeout)
{
OtaUpdateInformation * destination = moduleState[module].otaUpdateInformation;
uint8_t elapsed = 0;
watchdogSuspend(100 /*1s*/);
while (step != destination->step) {
if (elapsed++ > timeout) {
return false;
}
RTOS_WAIT_MS(1);
telemetryWakeup();
}
return true;
}
const char* Pxx2OtaUpdate::nextStep(uint8_t step, const char* rxName,
uint32_t address, const uint8_t* buffer)
{
OtaUpdateInformation * destination = moduleState[module].otaUpdateInformation;
destination->step = step;
destination->address = address;
for (uint8_t retry = 0;; retry++) {
uint8_t* module_buffer = pulsesGetModuleBuffer(module);
Pxx2Pulses pxx2(module_buffer);
pxx2.sendOtaUpdate(module, rxName, address, (const char *) buffer);
// send the frame immediately
auto mod = pulsesGetModuleDriver(module);
auto mod_st = (etx_module_state_t*)mod->ctx;
auto drv = modulePortGetSerialDrv(mod_st->tx);
auto ctx = modulePortGetCtx(mod_st->tx);
drv->sendBuffer(ctx, module_buffer, pxx2.getSize());
if (waitStep(step + 1, 20)) {
return nullptr;
}
else if (retry == 100) {
return "Transfer failed";
}
}
}
const char* Pxx2OtaUpdate::doFlashFirmware(const char* filename,
ProgressHandler progressHandler)
{
FIL file;
uint8_t buffer[32];
UINT count;
const char * result;
result = nextStep(OTA_UPDATE_START, rxName, 0, nullptr);
if (result) {
return result;
}
if (f_open(&file, filename, FA_READ) != FR_OK) {
return "Open file failed";
}
uint32_t size;
const char * ext = getFileExtension(filename);
if (ext && !strcasecmp(ext, FRSKY_FIRMWARE_EXT)) {
FrSkyFirmwareInformation * information = (FrSkyFirmwareInformation *) buffer;
if (f_read(&file, buffer, sizeof(FrSkyFirmwareInformation), &count) != FR_OK ||
count != sizeof(FrSkyFirmwareInformation)) {
f_close(&file);
return "Format error";
}
size = information->size;
}
else {
size = f_size(&file);
}
uint32_t done = 0;
while (1) {
progressHandler(getBasename(filename), STR_OTA_UPDATE, done, size);
if (f_read(&file, buffer, sizeof(buffer), &count) != FR_OK) {
f_close(&file);
return "Read file failed";
}
result = nextStep(OTA_UPDATE_TRANSFER, nullptr, done, buffer);
if (result) {
return result;
}
if (count < sizeof(buffer)) {
f_close(&file);
break;
}
done += count;
}
return nextStep(OTA_UPDATE_EOF, nullptr, done, nullptr);
}
void Pxx2OtaUpdate::flashFirmware(const char * filename, ProgressHandler progressHandler)
{
mixerTaskStop();
watchdogSuspend(100 /*1s*/);
RTOS_WAIT_MS(100);
moduleState[module].mode = MODULE_MODE_OTA_UPDATE;
const char * result = doFlashFirmware(filename, progressHandler);
moduleState[module].mode = MODULE_MODE_NORMAL;
AUDIO_PLAY(AU_SPECIAL_SOUND_BEEP1 );
BACKLIGHT_ENABLE();
if (result) {
POPUP_WARNING(STR_FIRMWARE_UPDATE_ERROR, result);
}
else {
POPUP_INFORMATION(STR_FIRMWARE_UPDATE_SUCCESS);
}
watchdogSuspend(100);
RTOS_WAIT_MS(100);
mixerTaskStart();
}
+53
View File
@@ -0,0 +1,53 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* 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.
*/
#pragma once
#include "pxx2.h"
#include "popups.h"
class OtaUpdateInformation: public BindInformation {
public:
char filename[FF_MAX_LFN + 1];
uint32_t address;
uint32_t module;
};
class Pxx2OtaUpdate {
public:
Pxx2OtaUpdate(uint8_t module, const char * rxName):
module(module),
rxName(rxName)
{
}
void flashFirmware(const char * filename, ProgressHandler progressHandler);
protected:
uint8_t module;
const char * rxName;
const char * doFlashFirmware(const char * filename, ProgressHandler progressHandler);
bool waitStep(uint8_t step, uint8_t timeout);
const char* nextStep(uint8_t step, const char* rxName, uint32_t address,
const uint8_t* buffer);
};
+156
View File
@@ -0,0 +1,156 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* 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.
*/
#pragma once
#include <stdint.h>
class Pxx2CrcMixin {
protected:
uint16_t crc = 0xFFFF;
void addToCrc(uint8_t byte)
{
crc -= byte;
}
};
class Pxx2Transport: public Pxx2CrcMixin {
protected:
uint8_t* buffer;
uint8_t* ptr;
Pxx2Transport(uint8_t* buffer)
: buffer(buffer), ptr(buffer)
{}
void addWord(uint32_t word)
{
addByte(word);
addByte(word >> 8);
addByte(word >> 16);
addByte(word >> 24);
}
void addByte(uint8_t byte)
{
Pxx2CrcMixin::addToCrc(byte);
addByteWithoutCrc(byte);
};
void addByteWithoutCrc(uint8_t byte)
{
*ptr++ = byte;
}
public:
uint32_t getSize()
{
return ptr - buffer;
}
};
class Pxx2Pulses: public Pxx2Transport {
friend class Pxx2OtaUpdate;
public:
Pxx2Pulses(uint8_t* buffer)
: Pxx2Transport(buffer)
{
addHead();
}
bool setupFrame(uint8_t module, int16_t* channels, uint8_t nChannels);
void setupAuthenticationFrame(uint8_t module, uint8_t mode, const uint8_t * outputMessage);
protected:
void setupHardwareInfoFrame(uint8_t module, int16_t* channels, uint8_t nChannels);
void setupRegisterFrame(uint8_t module);
void setupBindFrame(uint8_t module);
void setupAccstBindFrame(uint8_t module);
void setupAccessBindFrame(uint8_t module);
void setupResetFrame(uint8_t module);
void setupShareMode(uint8_t module);
void setupModuleSettingsFrame(uint8_t module, int16_t* channels, uint8_t nChannels);
void setupReceiverSettingsFrame(uint8_t module, int16_t* channels, uint8_t nChannels);
void setupChannelsFrame(uint8_t module, int16_t* channels, uint8_t nChannels);
void setupTelemetryFrame(uint8_t module);
void setupSpectrumAnalyser(uint8_t module);
void setupPowerMeter(uint8_t module);
void sendOtaUpdate(uint8_t module, const char * rxName, uint32_t address, const char * data);
void addHead()
{
// send 7E, do not CRC
Pxx2Transport::addByteWithoutCrc(0x7E);
// reserve 1 byte for LEN
Pxx2Transport::addByteWithoutCrc(0x00);
}
void addFrameType(uint8_t type_c, uint8_t type_id)
{
// TYPE_C + TYPE_ID
Pxx2Transport::addByte(type_c);
Pxx2Transport::addByte(type_id);
}
uint8_t addFlag0(uint8_t module);
void addFlag1(uint8_t module);
void addPulsesValues(uint16_t low, uint16_t high);
void addChannels(uint8_t module, int16_t* channels, uint8_t nChannels);
void addFailsafe(uint8_t module);
void addCrc()
{
Pxx2Transport::addByteWithoutCrc(Pxx2CrcMixin::crc >> 8);
Pxx2Transport::addByteWithoutCrc(Pxx2CrcMixin::crc);
}
void endFrame()
{
if (getSize() <= 2) return;
// update the frame LEN = frame length minus the 2 first bytes
buffer[1] = getSize() - 2;
// now add the CRC
addCrc();
}
};
+91 -74
View File
@@ -19,56 +19,14 @@
* GNU General Public License for more details.
*/
#include "sbus.h"
#include "hal/module_port.h"
#include "mixer_scheduler.h"
#include "opentx.h"
#define BITLEN_SBUS (10*2) // 100000 Baud => 10uS per bit
/* The protocol reuse some the DSM2 definitions where they are identical */
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 (extmodulePulsesData.dsm2.index & 1)
v += 2;
else
v -= 2;
*extmodulePulsesData.dsm2.ptr++ = v - 1;
extmodulePulsesData.dsm2.index+=1;
}
void sendByteSbus(uint8_t b) // max 11 changes 0 10 10 10 10 P 1
{
bool lev = 0;
uint8_t parity = 1;
uint8_t len = BITLEN_SBUS; //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 ^ (uint8_t)nlev;
if (lev == nlev) {
len += BITLEN_SBUS;
}
else {
_send_level(len);
len = BITLEN_SBUS;
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+ BITLEN_SBUS); // enlarge the last bit to be two stop bits long
}
#define SBUS_NORMAL_CHANS 16
#define SBUS_CHAN_BITS 11
#define SBUS_NORMAL_CHANS 16
#define SBUS_CHAN_BITS 11
/* Definitions from CleanFlight/BetaFlight */
@@ -80,44 +38,39 @@ void sendByteSbus(uint8_t b) // max 11 changes 0 10 10 10 10 P 1
#define SBUS_CHAN_CENTER 992
inline int getChannelValue(uint8_t port, int channel)
static inline void sendByte(uint8_t*& p_buf, uint8_t b)
{
int ch = g_model.moduleData[port].channelsStart+channel;
*p_buf++ = b;
}
static inline int getChannelValue(uint8_t port, int channel)
{
int ch = g_model.moduleData[port].channelsStart + channel;
// We will ignore 17 and 18th if that brings us over the limit
if (ch > 31)
return 0;
return channelOutputs[ch] + 2 * PPM_CH_CENTER(ch) - 2*PPM_CENTER;
if (ch > 31) return 0;
return channelOutputs[ch] + 2 * PPM_CH_CENTER(ch) - 2 * PPM_CENTER;
}
static void sbusFlush()
static void setupPulsesSbus(uint8_t module, uint8_t*& p_buf)
{
if (extmodulePulsesData.dsm2.index & 1)
*extmodulePulsesData.dsm2.ptr++ = 255;
else
*(extmodulePulsesData.dsm2.ptr - 1) = 255;
}
void setupPulsesSbus()
{
extmodulePulsesData.dsm2.index = 0;
extmodulePulsesData.dsm2.ptr = extmodulePulsesData.dsm2.pulses;
// extmodulePulsesData.dsm2.index = 0;
// extmodulePulsesData.dsm2.ptr = extmodulePulsesData.dsm2.pulses;
// Sync Byte
sendByteSbus(SBUS_FRAME_BEGIN_BYTE);
sendByte(p_buf, SBUS_FRAME_BEGIN_BYTE);
uint32_t bits = 0;
uint8_t bitsavailable = 0;
// byte 1-22, channels 0..2047, limits not really clear (B
for (int i=0; i<SBUS_NORMAL_CHANS; i++) {
int value = getChannelValue(EXTERNAL_MODULE, i);
int value = getChannelValue(module, i);
value = value*8/10 + SBUS_CHAN_CENTER;
bits |= limit(0, value, 2047) << bitsavailable;
bitsavailable += SBUS_CHAN_BITS;
while (bitsavailable >= 8) {
sendByteSbus((uint8_t) (bits & 0xff));
sendByte(p_buf, (uint8_t) (bits & 0xff));
bits >>= 8;
bitsavailable -= 8;
}
@@ -125,15 +78,79 @@ void setupPulsesSbus()
// flags
uint8_t flags=0;
if (getChannelValue(EXTERNAL_MODULE, 16) > 0)
if (getChannelValue(module, 16) > 0)
flags |= SBUS_FLAG_CHANNEL_17;
if (getChannelValue(EXTERNAL_MODULE, 17) > 0)
if (getChannelValue(module, 17) > 0)
flags |= SBUS_FLAG_CHANNEL_18;
sendByteSbus(flags);
sendByte(p_buf, flags);
// last byte, always 0x0
sendByteSbus(0x00);
sbusFlush();
sendByte(p_buf, 0x00);
}
#define SBUS_BAUDRATE 100000
const etx_serial_init sbusUartParams = {
.baudrate = SBUS_BAUDRATE,
.encoding = ETX_Encoding_8E2,
.direction = ETX_Dir_TX,
.polarity = ETX_Pol_Normal,
};
static void* sbusInit(uint8_t module)
{
#if defined(HARDWARE_INTERNAL_MODULE)
// only external module supported
if (module == INTERNAL_MODULE) return nullptr;
#endif
// auto mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &sbusUartParams);
// TODO: check if inverter is there, or mandate it somehow...
// if (!mod_st) {
// -> SOFT_INV has 'setPolarity'
auto mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, &sbusUartParams);
if (!mod_st) return nullptr;
// }
mixerSchedulerSetPeriod(module, SBUS_PERIOD(module));
return (void*)mod_st;
}
static void sbusDeInit(void* ctx)
{
auto mod_st = (etx_module_state_t*)ctx;
modulePortDeInit(mod_st);
}
static void sbusSendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_t nChannels)
{
// TODO:
(void)channels;
(void)nChannels;
auto mod_st = (etx_module_state_t*)ctx;
auto module = modulePortGetModule(mod_st);
auto p_data = buffer;
setupPulsesSbus(module, p_data);
auto drv = modulePortGetSerialDrv(mod_st->tx);
auto drv_ctx = modulePortGetCtx(mod_st->tx);
// TODO: check if the port has an inverter and use it instead
drv->setPolarity(drv_ctx, GET_SBUS_POLARITY(module));
drv->sendBuffer(drv_ctx, buffer, p_data - buffer);
// SBUS_PERIOD is not a constant! It can be set from UI
mixerSchedulerSetPeriod(module, SBUS_PERIOD(module));
}
const etx_proto_driver_t SBusDriver = {
.protocol = PROTOCOL_CHANNELS_SBUS,
.init = sbusInit,
.deinit = sbusDeInit,
.sendPulses = sbusSendPulses,
.processData = nullptr,
};
+26
View File
@@ -0,0 +1,26 @@
/*
* Copyright (C) EdgeTx
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* 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.
*/
#pragma once
#include "hal/module_driver.h"
extern const etx_proto_driver_t SBusDriver;