Skip to content
This repository has been archived by the owner on Oct 1, 2021. It is now read-only.

UAVCAN: fixed stack overflow, reduced memory usage, cleaned up IOCTL #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 16 additions & 8 deletions src/modules/uavcan/uavcan_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,25 +550,33 @@ int UavcanNode::start(uavcan::NodeID node_id, uint32_t bitrate)

/*
* CAN driver init
* Note that we instantiate and initialize CanInitHelper only once, because the STM32's bxCAN driver
* shipped with libuavcan does not support deinitialization.
*/
static CanInitHelper can;
static bool can_initialized = false;
static CanInitHelper* can = nullptr;

if (!can_initialized) {
const int can_init_res = can.init(bitrate);
if (can == nullptr) {
warnx("CAN driver init...");

can = new CanInitHelper();

if (can == nullptr) { // We don't have exceptions so bad_alloc cannot be thrown
warnx("Out of memory");
return -1;
}

const int can_init_res = can->init(bitrate);

if (can_init_res < 0) {
warnx("CAN driver init failed %i", can_init_res);
return can_init_res;
}

can_initialized = true;
}

/*
* Node init
*/
_instance = new UavcanNode(can.driver, uavcan_stm32::SystemClock::instance());
_instance = new UavcanNode(can->driver, uavcan_stm32::SystemClock::instance());

if (_instance == nullptr) {
warnx("Out of memory");
Expand Down Expand Up @@ -1115,7 +1123,7 @@ UavcanNode::ioctl(file *filp, int cmd, unsigned long arg)
break;


case UAVCANIOC_HARDPOINT_SET: {
case UAVCAN_IOCS_HARDPOINT_SET: {
const auto &hp_cmd = *reinterpret_cast<uavcan::equipment::hardpoint::Command *>(arg);
_hardpoint_controller.set_command(hp_cmd.hardpoint_id, hp_cmd.command);
}
Expand Down
6 changes: 1 addition & 5 deletions src/modules/uavcan/uavcan_main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@
// we add two to allow for actuator_direct and busevent
#define UAVCAN_NUM_POLL_FDS (NUM_ACTUATOR_CONTROL_GROUPS_UAVCAN+2)

// IOCTL control codes
static constexpr unsigned UAVCANIOCBASE = 0x7800;
static constexpr unsigned UAVCANIOC_HARDPOINT_SET = _PX4_IOC(UAVCANIOCBASE, 0x10);

/**
* A UAVCAN node.
*/
Expand All @@ -103,7 +99,7 @@ class UavcanNode : public device::CDev
*/

static constexpr unsigned RxQueueLenPerIface = FramePerMSecond * PollTimeoutMs; // At
static constexpr unsigned StackSize = 1800;
static constexpr unsigned StackSize = 2400;

public:
typedef uavcan_stm32::CanInitHelper<RxQueueLenPerIface> CanInitHelper;
Expand Down
13 changes: 12 additions & 1 deletion src/modules/uavcan/uavcan_module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@
// ioctl interface
#define _UAVCAN_IOC(_n) (_IOC(_UAVCAN_IOCBASE, _n))
#define _UAVCAN_IOCBASE (0x4000) // IOCTL base for module UAVCAN
#define UAVCAN_IOCG_NODEID_INPROGRESS _UAVCAN_IOC(1) // query if node identification is in progress
/*
* Query if node identification is in progress. Returns:
* EINVAL - not applicable in the current operating mode
* ETIME - network discovery complete
* OK (0) - network discovery in progress
*/
#define UAVCAN_IOCG_NODEID_INPROGRESS _UAVCAN_IOC(1)
/*
* Set hardpoint command. Accepts a pointer to uavcan::equipment::hardpoint::Command; returns nothing.
* The pointer may be invalidated once the call returns.
*/
#define UAVCAN_IOCS_HARDPOINT_SET _UAVCAN_IOC(10)

// public prototypes
extern "C" __EXPORT int uavcan_main(int argc, char *argv[]);
1 change: 0 additions & 1 deletion src/modules/uavcan/uavcan_servers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,6 @@ void UavcanServers::unpackFwFromROMFS(const char* sd_path, const char* romfs_pat

DIR* const romfs_dir = opendir(romfs_path);
if (!romfs_dir) {
warnx("base: couldn't open %s", romfs_path);
return;
}

Expand Down