Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Free RAM on IO #7165

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nuttx-configs/px4io-v2/nsh/defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ CONFIG_BOARD_LOOPSPERMSEC=2000
# Interrupt options
#
CONFIG_ARCH_HAVE_INTERRUPTSTACK=y
CONFIG_ARCH_INTERRUPTSTACK=500
CONFIG_ARCH_INTERRUPTSTACK=0
CONFIG_ARCH_HAVE_HIPRI_INTERRUPT=y
# CONFIG_ARCH_HIPRI_INTERRUPT is not set

Expand Down
9 changes: 5 additions & 4 deletions src/lib/rc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ px4_add_module(
COMPILE_FLAGS
-Wno-unused-result
SRCS
st24.c
sumd.c
sbus.c
dsm.c
st24.cpp
sumd.cpp
sbus.cpp
dsm.cpp
common_rc.cpp
DEPENDS
platforms__common
)
Expand Down
4 changes: 4 additions & 0 deletions src/lib/rc/common_rc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#include "common_rc.h"

__EXPORT uint8_t __rc_decode_buf[RC_LIB_MAX_BUFFER_SIZE];
8 changes: 8 additions & 0 deletions src/lib/rc/common_rc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

#pragma once

#include <stdint.h>

#define RC_LIB_MAX_BUFFER_SIZE 80

extern uint8_t __rc_decode_buf[RC_LIB_MAX_BUFFER_SIZE];
12 changes: 9 additions & 3 deletions src/lib/rc/dsm.c → src/lib/rc/dsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
****************************************************************************/

/**
* @file dsm.c
* @file dsm.cpp
*
* Serial protocol decoder for the Spektrum DSM* family of protocols.
*
Expand All @@ -49,6 +49,7 @@
#include <string.h>

#include "dsm.h"
#include "common_rc.h"
#include <drivers/drv_hrt.h>

#if defined (__PX4_LINUX) || defined (__PX4_DARWIN) || defined(__PX4_QURT)
Expand All @@ -68,8 +69,13 @@ static enum DSM_DECODE_STATE {
static int dsm_fd = -1; /**< File handle to the DSM UART */
static hrt_abstime dsm_last_rx_time; /**< Timestamp when we last received data */
static hrt_abstime dsm_last_frame_time; /**< Timestamp for start of last valid dsm frame */
static uint8_t dsm_frame[DSM_BUFFER_SIZE]; /**< DSM dsm frame receive buffer */
static uint8_t dsm_buf[DSM_FRAME_SIZE * 2];
static uint8_t *dsm_frame = &__rc_decode_buf[0]; /**< DSM_BUFFER_SIZE DSM dsm frame receive buffer */
static uint8_t *dsm_buf = &__rc_decode_buf[DSM_BUFFER_SIZE]; //[DSM_FRAME_SIZE * 2];

// Ensure there is enough space
static_assert(sizeof(__rc_decode_buf) > (DSM_BUFFER_SIZE + DSM_FRAME_SIZE * 2),
"__rc_decode_buf is too small for DSM protocol");

static uint16_t dsm_chan_buf[DSM_MAX_CHANNEL_COUNT];
static unsigned dsm_partial_frame_count; /**< Count of bytes received for current dsm frame */
static unsigned dsm_channel_shift = 0; /**< Channel resolution, 0=unknown, 1=10 bit, 2=11 bit */
Expand Down
7 changes: 6 additions & 1 deletion src/lib/rc/sbus.c → src/lib/rc/sbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#endif

#include "sbus.h"
#include "common_rc.h"
#include <drivers/drv_hrt.h>

#define SBUS_DEBUG_LEVEL 0 /* Set debug output level */
Expand Down Expand Up @@ -116,7 +117,11 @@ static enum SBUS2_DECODE_STATE {
SBUS2_DECODE_STATE_SBUS2_DATA2 = 0x34
} sbus_decode_state = SBUS2_DECODE_STATE_DESYNC;

static uint8_t sbus_frame[SBUS_FRAME_SIZE + (SBUS_FRAME_SIZE / 2)];
static uint8_t *sbus_frame = &__rc_decode_buf[0];

// Ensure there is enough space
static_assert(sizeof(__rc_decode_buf) > SBUS_BUFFER_SIZE,
"__rc_decode_buf is too small for SBUS protocol");

static unsigned partial_frame_count;
static unsigned sbus1_frame_delay = (1000U * 1000U) / SBUS1_DEFAULT_RATE_HZ;
Expand Down
29 changes: 17 additions & 12 deletions src/lib/rc/st24.c → src/lib/rc/st24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <stdbool.h>
#include <stdio.h>
#include "st24.h"
#include "common_rc.h"

enum ST24_DECODE_STATE {
ST24_DECODE_STATE_UNSYNCED = 0,
Expand Down Expand Up @@ -74,7 +75,11 @@ const char *decode_states[] = {"UNSYNCED",
static enum ST24_DECODE_STATE _decode_state = ST24_DECODE_STATE_UNSYNCED;
static uint8_t _rxlen;

static ReceiverFcPacket _rxpacket;
static ReceiverFcPacket *_rxpacket = (ReceiverFcPacket *) &__rc_decode_buf[0];

// Ensure there is enough space
static_assert(sizeof(__rc_decode_buf) > sizeof(ReceiverFcPacket),
"__rc_decode_buf is too small for ST protocol");

uint8_t st24_common_crc8(uint8_t *ptr, uint8_t len)
{
Expand Down Expand Up @@ -132,8 +137,8 @@ int st24_decode(uint8_t byte, uint8_t *rssi, uint8_t *lost_count, uint16_t *chan
case ST24_DECODE_STATE_GOT_STX2:

/* ensure no data overflow failure or hack is possible */
if ((unsigned)byte <= sizeof(_rxpacket.length) + sizeof(_rxpacket.type) + sizeof(_rxpacket.st24_data)) {
_rxpacket.length = byte;
if ((unsigned)byte <= sizeof(_rxpacket->length) + sizeof(_rxpacket->type) + sizeof(_rxpacket->st24_data)) {
_rxpacket->length = byte;
_rxlen = 0;
_decode_state = ST24_DECODE_STATE_GOT_LEN;

Expand All @@ -144,35 +149,35 @@ int st24_decode(uint8_t byte, uint8_t *rssi, uint8_t *lost_count, uint16_t *chan
break;

case ST24_DECODE_STATE_GOT_LEN:
_rxpacket.type = byte;
_rxpacket->type = byte;
_rxlen++;
_decode_state = ST24_DECODE_STATE_GOT_TYPE;
break;

case ST24_DECODE_STATE_GOT_TYPE:
_rxpacket.st24_data[_rxlen - 1] = byte;
_rxpacket->st24_data[_rxlen - 1] = byte;
_rxlen++;

if (_rxlen == (_rxpacket.length - 1)) {
if (_rxlen == (_rxpacket->length - 1)) {
_decode_state = ST24_DECODE_STATE_GOT_DATA;
}

break;

case ST24_DECODE_STATE_GOT_DATA:
_rxpacket.crc8 = byte;
_rxpacket->crc8 = byte;
_rxlen++;

if (st24_common_crc8((uint8_t *) & (_rxpacket.length), _rxlen) == _rxpacket.crc8) {
if (st24_common_crc8((uint8_t *) & (_rxpacket->length), _rxlen) == _rxpacket->crc8) {

ret = 0;

/* decode the actual packet */

switch (_rxpacket.type) {
switch (_rxpacket->type) {

case ST24_PACKET_TYPE_CHANNELDATA12: {
ChannelData12 *d = (ChannelData12 *)_rxpacket.st24_data;
ChannelData12 *d = (ChannelData12 *)_rxpacket->st24_data;

// Scale from 0..255 to 100%.
*rssi = d->rssi * (100.0f / 255.0f);
Expand Down Expand Up @@ -201,7 +206,7 @@ int st24_decode(uint8_t byte, uint8_t *rssi, uint8_t *lost_count, uint16_t *chan
break;

case ST24_PACKET_TYPE_CHANNELDATA24: {
ChannelData24 *d = (ChannelData24 *)&_rxpacket.st24_data;
ChannelData24 *d = (ChannelData24 *)&_rxpacket->st24_data;

// Scale from 0..255 to 100%.
*rssi = d->rssi * (100.0f / 255.0f);
Expand Down Expand Up @@ -231,7 +236,7 @@ int st24_decode(uint8_t byte, uint8_t *rssi, uint8_t *lost_count, uint16_t *chan

case ST24_PACKET_TYPE_TRANSMITTERGPSDATA: {

// ReceiverFcPacket* d = (ReceiverFcPacket*)&_rxpacket.st24_data;
// ReceiverFcPacket* d = (ReceiverFcPacket*)&_rxpacket->st24_data;
/* we silently ignore this data for now, as it is unused */
ret = 5;
}
Expand Down
57 changes: 30 additions & 27 deletions src/lib/rc/sumd.c → src/lib/rc/sumd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#include <stdbool.h>
#include <stdio.h>
#include "sumd.h"

#include "common_rc.h"

enum SUMD_DECODE_STATE {
SUMD_DECODE_STATE_UNSYNCED = 0,
Expand Down Expand Up @@ -88,8 +88,11 @@ bool _debug = false;
static enum SUMD_DECODE_STATE _decode_state = SUMD_DECODE_STATE_UNSYNCED;
static uint8_t _rxlen;

static ReceiverFcPacketHoTT _rxpacket;
static ReceiverFcPacketHoTT *_rxpacket = (ReceiverFcPacketHoTT *) &__rc_decode_buf[0];

// Ensure there is enough space
static_assert(sizeof(__rc_decode_buf) > sizeof(ReceiverFcPacketHoTT),
"__rc_decode_buf is too small for SUMD protocol");

uint16_t sumd_crc16(uint16_t crc, uint8_t value)
{
Expand Down Expand Up @@ -122,7 +125,7 @@ int sumd_decode(uint8_t byte, uint8_t *rssi, uint8_t *rx_count, uint16_t *channe
}

if (byte == SUMD_HEADER_ID) {
_rxpacket.header = byte;
_rxpacket->header = byte;
_sumd = true;
_rxlen = 0;
_crc16 = 0x0000;
Expand All @@ -144,7 +147,7 @@ int sumd_decode(uint8_t byte, uint8_t *rssi, uint8_t *rx_count, uint16_t *channe

case SUMD_DECODE_STATE_GOT_HEADER:
if (byte == SUMD_ID_SUMD || byte == SUMD_ID_FAILSAFE || byte == SUMD_ID_SUMH) {
_rxpacket.status = byte;
_rxpacket->status = byte;

if (byte == SUMD_ID_SUMH) {
_sumd = false;
Expand All @@ -171,7 +174,7 @@ int sumd_decode(uint8_t byte, uint8_t *rssi, uint8_t *rx_count, uint16_t *channe

case SUMD_DECODE_STATE_GOT_STATE:
if (byte >= 2 && byte <= SUMD_MAX_CHANNELS) {
_rxpacket.length = byte;
_rxpacket->length = byte;

if (_sumd) {
_crc16 = sumd_crc16(_crc16, byte);
Expand All @@ -194,7 +197,7 @@ int sumd_decode(uint8_t byte, uint8_t *rssi, uint8_t *rx_count, uint16_t *channe
break;

case SUMD_DECODE_STATE_GOT_LEN:
_rxpacket.sumd_data[_rxlen] = byte;
_rxpacket->sumd_data[_rxlen] = byte;

if (_sumd) {
_crc16 = sumd_crc16(_crc16, byte);
Expand All @@ -205,7 +208,7 @@ int sumd_decode(uint8_t byte, uint8_t *rssi, uint8_t *rx_count, uint16_t *channe

_rxlen++;

if (_rxlen <= ((_rxpacket.length * 2))) {
if (_rxlen <= ((_rxpacket->length * 2))) {
if (_debug) {
printf(" SUMD_DECODE_STATE_GOT_DATA[%d]: %x\n", _rxlen - 2, byte) ;
}
Expand All @@ -222,7 +225,7 @@ int sumd_decode(uint8_t byte, uint8_t *rssi, uint8_t *rx_count, uint16_t *channe
break;

case SUMD_DECODE_STATE_GOT_DATA:
_rxpacket.crc16_high = byte;
_rxpacket->crc16_high = byte;

if (_debug) {
printf(" SUMD_DECODE_STATE_GOT_CRC16[1]: %x [%x]\n", byte, ((_crc16 >> 8) & 0xff)) ;
Expand All @@ -238,7 +241,7 @@ int sumd_decode(uint8_t byte, uint8_t *rssi, uint8_t *rx_count, uint16_t *channe
break;

case SUMD_DECODE_STATE_GOT_CRC16_BYTE_1:
_rxpacket.crc16_low = byte;
_rxpacket->crc16_low = byte;

if (_debug) {
printf(" SUMD_DECODE_STATE_GOT_CRC16[2]: %x [%x]\n", byte, (_crc16 & 0xff)) ;
Expand All @@ -249,7 +252,7 @@ int sumd_decode(uint8_t byte, uint8_t *rssi, uint8_t *rx_count, uint16_t *channe
break;

case SUMD_DECODE_STATE_GOT_CRC16_BYTE_2:
_rxpacket.telemetry = byte;
_rxpacket->telemetry = byte;

if (_debug) {
printf(" SUMD_DECODE_STATE_GOT_SUMH_TELEMETRY: %x\n", byte) ;
Expand All @@ -261,24 +264,24 @@ int sumd_decode(uint8_t byte, uint8_t *rssi, uint8_t *rx_count, uint16_t *channe

case SUMD_DECODE_STATE_GOT_CRC:
if (_sumd) {
_rxpacket.crc16_low = byte;
_rxpacket->crc16_low = byte;

if (_debug) {
printf(" SUMD_DECODE_STATE_GOT_CRC[2]: %x [%x]\n\n", byte, (_crc16 & 0xff)) ;
}

if (_crc16 == (uint16_t)(_rxpacket.crc16_high << 8) + _rxpacket.crc16_low) {
if (_crc16 == (uint16_t)(_rxpacket->crc16_high << 8) + _rxpacket->crc16_low) {
_crcOK = true;
}

} else {
_rxpacket.crc8 = byte;
_rxpacket->crc8 = byte;

if (_debug) {
printf(" SUMD_DECODE_STATE_GOT_CRC8_SUMH: %x [%x]\n\n", byte, _crc8) ;
}

if (_crc8 == _rxpacket.crc8) {
if (_crc8 == _rxpacket->crc8) {
_crcOK = true;
}
}
Expand Down Expand Up @@ -312,38 +315,38 @@ int sumd_decode(uint8_t byte, uint8_t *rssi, uint8_t *rx_count, uint16_t *channe
*rssi = 100;

/* failsafe flag */
*failsafe = (_rxpacket.status == SUMD_ID_FAILSAFE);
*failsafe = (_rxpacket->status == SUMD_ID_FAILSAFE);

/* received Channels */
if ((uint16_t)_rxpacket.length > max_chan_count) {
_rxpacket.length = (uint8_t) max_chan_count;
if ((uint16_t)_rxpacket->length > max_chan_count) {
_rxpacket->length = (uint8_t) max_chan_count;
}

*channel_count = (uint16_t)_rxpacket.length;
*channel_count = (uint16_t)_rxpacket->length;

/* decode the actual packet */
/* reorder first 4 channels */

/* ch1 = roll -> sumd = ch2 */
channels[0] = (uint16_t)((_rxpacket.sumd_data[1 * 2 + 1] << 8) | _rxpacket.sumd_data[1 * 2 + 2]) >> 3;
channels[0] = (uint16_t)((_rxpacket->sumd_data[1 * 2 + 1] << 8) | _rxpacket->sumd_data[1 * 2 + 2]) >> 3;
/* ch2 = pitch -> sumd = ch2 */
channels[1] = (uint16_t)((_rxpacket.sumd_data[2 * 2 + 1] << 8) | _rxpacket.sumd_data[2 * 2 + 2]) >> 3;
channels[1] = (uint16_t)((_rxpacket->sumd_data[2 * 2 + 1] << 8) | _rxpacket->sumd_data[2 * 2 + 2]) >> 3;
/* ch3 = throttle -> sumd = ch2 */
channels[2] = (uint16_t)((_rxpacket.sumd_data[0 * 2 + 1] << 8) | _rxpacket.sumd_data[0 * 2 + 2]) >> 3;
channels[2] = (uint16_t)((_rxpacket->sumd_data[0 * 2 + 1] << 8) | _rxpacket->sumd_data[0 * 2 + 2]) >> 3;
/* ch4 = yaw -> sumd = ch2 */
channels[3] = (uint16_t)((_rxpacket.sumd_data[3 * 2 + 1] << 8) | _rxpacket.sumd_data[3 * 2 + 2]) >> 3;
channels[3] = (uint16_t)((_rxpacket->sumd_data[3 * 2 + 1] << 8) | _rxpacket->sumd_data[3 * 2 + 2]) >> 3;

/* we start at channel 5(index 4) */
unsigned chan_index = 4;

for (i = 4; i < _rxpacket.length; i++) {
for (i = 4; i < _rxpacket->length; i++) {
if (_debug) {
printf("ch[%d] : %x %x [ %x %d ]\n", i + 1, _rxpacket.sumd_data[i * 2 + 1], _rxpacket.sumd_data[i * 2 + 2],
((_rxpacket.sumd_data[i * 2 + 1] << 8) | _rxpacket.sumd_data[i * 2 + 2]) >> 3,
((_rxpacket.sumd_data[i * 2 + 1] << 8) | _rxpacket.sumd_data[i * 2 + 2]) >> 3);
printf("ch[%d] : %x %x [ %x %d ]\n", i + 1, _rxpacket->sumd_data[i * 2 + 1], _rxpacket->sumd_data[i * 2 + 2],
((_rxpacket->sumd_data[i * 2 + 1] << 8) | _rxpacket->sumd_data[i * 2 + 2]) >> 3,
((_rxpacket->sumd_data[i * 2 + 1] << 8) | _rxpacket->sumd_data[i * 2 + 2]) >> 3);
}

channels[chan_index] = (uint16_t)((_rxpacket.sumd_data[i * 2 + 1] << 8) | _rxpacket.sumd_data[i * 2 + 2]) >> 3;
channels[chan_index] = (uint16_t)((_rxpacket->sumd_data[i * 2 + 1] << 8) | _rxpacket->sumd_data[i * 2 + 2]) >> 3;
/* convert values to 1000-2000 ppm encoding in a not too sloppy fashion */
//channels[chan_index] = (uint16_t)(channels[chan_index] * SUMD_SCALE_FACTOR + .5f) + SUMD_SCALE_OFFSET;

Expand Down
9 changes: 5 additions & 4 deletions src/modules/px4iofirmware/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ set(srcs
${PX4_SOURCE_DIR}/src/drivers/stm32/drv_hrt.c
${PX4_SOURCE_DIR}/src/drivers/stm32/drv_io_timer.c
${PX4_SOURCE_DIR}/src/drivers/stm32/drv_pwm_servo.c
${PX4_SOURCE_DIR}/src/lib/rc/dsm.c
${PX4_SOURCE_DIR}/src/lib/rc/sbus.c
${PX4_SOURCE_DIR}/src/lib/rc/st24.c
${PX4_SOURCE_DIR}/src/lib/rc/sumd.c
${PX4_SOURCE_DIR}/src/lib/rc/dsm.cpp
${PX4_SOURCE_DIR}/src/lib/rc/sbus.cpp
${PX4_SOURCE_DIR}/src/lib/rc/st24.cpp
${PX4_SOURCE_DIR}/src/lib/rc/sumd.cpp
${PX4_SOURCE_DIR}/src/lib/rc/common_rc.cpp
${PX4_SOURCE_DIR}/src/modules/systemlib/mixer/mixer.cpp
${PX4_SOURCE_DIR}/src/modules/systemlib/mixer/mixer_group.cpp
${PX4_SOURCE_DIR}/src/modules/systemlib/mixer/mixer_helicopter.cpp
Expand Down
1 change: 1 addition & 0 deletions src/modules/px4iofirmware/mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ enum mixer_source {
MIX_FAILSAFE,
MIX_OVERRIDE_FMU_OK
};

static volatile mixer_source source;

static int mixer_callback(uintptr_t handle,
Expand Down