Skip to content

Commit 8f64f7c

Browse files
davids5dagar
authored andcommitted
uavcan_1:Fixed hardfault on stop
1 parent 6aa4e12 commit 8f64f7c

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/drivers/uavcan_v1/Uavcan.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,28 @@ UavcanNode::~UavcanNode()
9898
_task_should_exit.store(true);
9999
ScheduleNow();
100100

101-
unsigned i = 10;
101+
unsigned i = 1000;
102102

103103
do {
104-
/* wait 5ms - it should wake every 10ms or so worst-case */
104+
/* Wait for it to exit or timeout */
105105
usleep(5000);
106106

107107
if (--i == 0) {
108+
PX4_ERR("Failed to Stop Task - reboot needed");
108109
break;
109110
}
110111

111112
} while (_instance);
112113
}
113114

114115
delete _can_interface;
116+
_can_interface = nullptr;
115117

116118
perf_free(_cycle_perf);
117119
perf_free(_interval_perf);
118120

119-
//delete _uavcan_heap;
121+
delete static_cast<uint8_t *>(_uavcan_heap);
122+
_uavcan_heap = nullptr;
120123
}
121124

122125
int UavcanNode::start(uint32_t node_id, uint32_t bitrate)
@@ -163,7 +166,7 @@ void UavcanNode::Run()
163166
{
164167
pthread_mutex_lock(&_node_mutex);
165168

166-
if (!_initialized) {
169+
if (_instance != nullptr && !_initialized) {
167170
init();
168171

169172
// return early if still not initialized
@@ -216,7 +219,7 @@ void UavcanNode::Run()
216219
CanardFrame received_frame{};
217220
received_frame.payload = &data;
218221

219-
while (_can_interface->receive(&received_frame) > 0) {
222+
while (!_task_should_exit.load() && _can_interface->receive(&received_frame) > 0) {
220223
CanardTransfer receive{};
221224
CanardRxSubscription *subscription = NULL;
222225
int32_t result = canardRxAccept2(&_canard_instance, &received_frame, 0, &receive, &subscription);
@@ -253,10 +256,14 @@ void UavcanNode::Run()
253256

254257
perf_end(_cycle_perf);
255258

256-
if (_task_should_exit.load()) {
257-
_can_interface->close();
258-
259+
if (_instance && _task_should_exit.load()) {
259260
ScheduleClear();
261+
262+
if (_initialized && _can_interface != nullptr) {
263+
_can_interface->close();
264+
_initialized = false;
265+
}
266+
260267
_instance = nullptr;
261268
}
262269

src/drivers/uavcan_v1/Uavcan.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class UavcanNode : public ModuleParams, public px4::ScheduledWorkItem
148148

149149
void *_uavcan_heap{nullptr};
150150

151-
CanardInterface *const _can_interface;
151+
CanardInterface *_can_interface;
152152

153153
CanardInstance _canard_instance;
154154

0 commit comments

Comments
 (0)