Skip to content

Commit 68f0175

Browse files
added thread names
1 parent bdf04ef commit 68f0175

File tree

7 files changed

+68
-39
lines changed

7 files changed

+68
-39
lines changed

cfg/chconf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ extern volatile uint32_t last_idle_tick;
658658
#if !defined(CH_DBG_ENABLE_STACK_CHECK)
659659
#ifdef DEBUG_BUILD
660660
#define CH_DBG_ENABLE_STACK_CHECK TRUE
661+
#define PORT_ENABLE_GUARD_PAGES TRUE
661662
#else
662663
#ifdef RELEASE_BUILD
663664
#define CH_DBG_ENABLE_STACK_CHECK FALSE

ext/xbot_framework

portable/Io.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ XBOT_THREAD_TYPEDEF io_thread_{};
3030

3131
fd_set socket_set;
3232

33+
static const char* IO_THD_NAME = "xbot-io";
34+
3335
using namespace xbot::service;
3436

3537
void runIo(void* arg) {
@@ -106,7 +108,7 @@ bool Io::start() {
106108
return false;
107109
}
108110
return thread::initialize(&io_thread_, runIo, nullptr, &waIoThread,
109-
sizeof(waIoThread));
111+
sizeof(waIoThread), IO_THD_NAME);
110112
}
111113

112114
} // namespace xbot::service

portable/thread.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ using namespace xbot::service::thread;
55

66
bool xbot::service::thread::initialize(ThreadPtr thread,
77
void (*threadfunc)(void*), void* arg,
8-
void* stackbuf, size_t buflen) {
8+
void* stackbuf, size_t buflen, const char* name) {
99
// Create a multicast sender thread
1010
*thread = chThdCreateStatic(stackbuf, buflen, NORMALPRIO, threadfunc, arg);
11+
(*thread)->name = name;
1112
return true;
1213
}
1314

src/boot_service_discovery.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
#include "chprintf.h"
1515
#include "lwip/sockets.h"
16-
static char boardAdvertisementBuffer[1000];
17-
static char boardAdvertisementRequestBuffer[1000];
18-
static THD_WORKING_AREA(waServiceDiscovery, 500);
16+
static char boardAdvertisementBuffer[100];
17+
static char boardAdvertisementRequestBuffer[100];
18+
static THD_WORKING_AREA(waServiceDiscovery, 512);
1919

2020
static void multicast_sender_thread(void *p) {
2121
(void)p;
@@ -92,6 +92,9 @@ void InitBootloaderServiceDiscovery() {
9292
"BOARD_ADVERTISEMENT:xcore-boot;application-mode");
9393

9494
// Create a multicast sender thread
95-
chThdCreateStatic(waServiceDiscovery, sizeof(waServiceDiscovery), NORMALPRIO,
95+
thread_t* threadPointer = chThdCreateStatic(waServiceDiscovery, sizeof(waServiceDiscovery), NORMALPRIO,
9696
multicast_sender_thread, NULL);
97+
#ifdef USE_SEGGER_SYSTEMVIEW
98+
threadPointer->name = "Boot SD";
99+
#endif
97100
}

src/drivers/gps/gps_interface.cpp

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
#include <cmath>
1111

12-
13-
1412
namespace xbot::driver::gps {
1513

1614
GpsDriver::GpsDriver() {
@@ -19,54 +17,49 @@ GpsDriver::GpsDriver() {
1917
bool GpsDriver::StartDriver(UARTDriver *uart, uint32_t baudrate) {
2018
chDbgAssert(stopped_, "don't start the driver twice");
2119
chDbgAssert(uart != nullptr, "need to provide a driver");
22-
if(!stopped_) {
20+
if (!stopped_) {
2321
return false;
2422
}
2523
this->uart_ = uart;
2624
uart_config_.speed = baudrate;
2725
uart_config_.context = this;
2826
bool uartStarted = uartStart(uart, &uart_config_) == MSG_OK;
29-
if(!uartStarted) {
27+
if (!uartStarted) {
3028
return false;
3129
}
3230

33-
34-
uart_config_.rxchar_cb = [](UARTDriver* uartp, uint16_t c) {
35-
(void)uartp;
36-
(void)c;
37-
static int missing_bytes=0;
38-
missing_bytes++;
39-
};
40-
41-
uart_config_.rxend_cb = [](UARTDriver* uartp) {
31+
uart_config_.rxend_cb = [](UARTDriver *uartp) {
4232
(void)uartp;
4333
static int i = 0;
4434
i++;
45-
GpsDriver* instance = reinterpret_cast<const UARTConfigEx*>(uartp->config)->context;
46-
if(!instance->processing_done_) {
35+
GpsDriver *instance = reinterpret_cast<const UARTConfigEx *>(uartp->config)->context;
36+
if (!instance->processing_done_) {
4737
// This is bad, processing is too slow to keep up with updates!
4838
// We just read into the same buffer again
49-
uint8_t* next_recv_buffer = (instance->processing_buffer_ == instance->recv_buffer1_) ? instance->recv_buffer2_ : instance->recv_buffer1_;
39+
uint8_t *next_recv_buffer =
40+
(instance->processing_buffer_ == instance->recv_buffer1_) ? instance->recv_buffer2_ : instance->recv_buffer1_;
5041
uartStartReceiveI(uartp, RECV_BUFFER_SIZE, next_recv_buffer);
5142
} else {
5243
// Swap buffers and read into the next one
5344
// Get the pointer to the receiving buffer (it's not the processing buffer)
54-
uint8_t* next_recv_buffer = instance->processing_buffer_;
45+
uint8_t *next_recv_buffer = instance->processing_buffer_;
5546
uartStartReceiveI(uartp, RECV_BUFFER_SIZE, next_recv_buffer);
56-
instance->processing_buffer_ = (instance->processing_buffer_ == instance->recv_buffer1_) ? instance->recv_buffer2_ : instance->recv_buffer1_;
47+
instance->processing_buffer_ =
48+
(instance->processing_buffer_ == instance->recv_buffer1_) ? instance->recv_buffer2_ : instance->recv_buffer1_;
5749
instance->processing_buffer_len_ = RECV_BUFFER_SIZE;
5850
instance->processing_done_ = false;
5951
// Signal the processing thread
60-
if(instance->processing_thread_) {
52+
if (instance->processing_thread_) {
6153
chEvtSignalI(instance->processing_thread_, 1);
6254
}
6355
}
6456
};
6557

66-
67-
6858
stopped_ = false;
6959
processing_thread_ = chThdCreateStatic(&thd_wa_, sizeof(thd_wa_), NORMALPRIO, threadHelper, this);
60+
#ifdef USE_SEGGER_SYSTEMVIEW
61+
processing_thread_->name = "GpsDriver";
62+
#endif
7063

7164
uartStartReceive(uart, RECV_BUFFER_SIZE, recv_buffer1_);
7265
return true;
@@ -75,8 +68,7 @@ bool GpsDriver::StartDriver(UARTDriver *uart, uint32_t baudrate) {
7568
void GpsDriver::SetStateCallback(const GpsDriver::StateCallback &function) {
7669
state_callback_ = function;
7770
}
78-
void GpsDriver::SetDatum(double datum_lat, double datum_long,
79-
double datum_height) {
71+
void GpsDriver::SetDatum(double datum_lat, double datum_long, double datum_height) {
8072
datum_lat_ = datum_lat;
8173
datum_long_ = datum_long;
8274
datum_u_ = datum_height;
@@ -89,10 +81,39 @@ bool GpsDriver::send_raw(const void *data, size_t size) {
8981
}
9082

9183
void GpsDriver::threadFunc() {
92-
while(!stopped_) {
93-
// Wait for data to arrive, process and mark as done
94-
chEvtWaitAny(ALL_EVENTS);
95-
ProcessBytes(processing_buffer_, processing_buffer_len_);
84+
while (!stopped_) {
85+
// Wait for data to arrive
86+
bool timeout = chEvtWaitAnyTimeout(ALL_EVENTS, TIME_MS2I(RECV_TIMEOUT_MILLIS)) == 0;
87+
if (timeout) {
88+
// If timeout, we take the buffer and restart the reception
89+
// Else, the ISR has already prepared the buffer for us
90+
// Lock the core to prevent the RX interrupt from firing
91+
chSysLock();
92+
// Check, that processing_done_ is still true
93+
// (in case ISR happened between the timeout and now, this will be set to false by ISR)
94+
if (processing_done_) {
95+
// Stop reception and get the partial received length
96+
size_t not_received_len = uartStopReceiveI(uart_);
97+
if (not_received_len != UART_ERR_NOT_ACTIVE) {
98+
// Uart was still receiving, so the buffer length is not complete
99+
processing_buffer_len_ = RECV_BUFFER_SIZE - not_received_len;
100+
} else {
101+
// Uart was not active.
102+
// This should not happen, but could during debug when pausing the chip
103+
// we ignore this buffer, but carry on as usual
104+
processing_buffer_len_ = 0;
105+
}
106+
uint8_t *next_recv_buffer = processing_buffer_;
107+
uartStartReceiveI(uart_, RECV_BUFFER_SIZE, next_recv_buffer);
108+
processing_buffer_ = (processing_buffer_ == recv_buffer1_) ? recv_buffer2_ : recv_buffer1_;
109+
processing_done_ = false;
110+
}
111+
// allow ISR again
112+
chSysUnlock();
113+
}
114+
if (processing_buffer_len_ > 0) {
115+
ProcessBytes(processing_buffer_, processing_buffer_len_);
116+
}
96117
processing_done_ = true;
97118
}
98119
}
@@ -105,4 +126,4 @@ void GpsDriver::threadHelper(void *instance) {
105126
void GpsDriver::SendRTCM(const uint8_t *data, size_t size) {
106127
send_raw(data, size);
107128
}
108-
} // namespace xbot::driver::gps
129+
} // namespace xbot::driver::gps

src/drivers/gps/gps_interface.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ class GpsDriver {
9090
GpsDriver *context;
9191
};
9292

93-
static constexpr size_t RECV_BUFFER_SIZE = 100;
93+
static constexpr size_t RECV_BUFFER_SIZE = 512;
94+
// 10Hz timeout for reception
95+
static constexpr uint32_t RECV_TIMEOUT_MILLIS = 100;
9496
// Keep two buffers for streaming data while doing processing
9597
uint8_t recv_buffer1_[RECV_BUFFER_SIZE]{};
9698
uint8_t recv_buffer2_[RECV_BUFFER_SIZE]{};
@@ -101,7 +103,7 @@ class GpsDriver {
101103
UARTDriver *uart_{};
102104
UARTConfigEx uart_config_{};
103105

104-
THD_WORKING_AREA(thd_wa_, 512){};
106+
THD_WORKING_AREA(thd_wa_, 1024){};
105107
thread_t *processing_thread_ = nullptr;
106108
// This is reset by the receiving ISR and set by the thread to signal if it's safe to process more data.
107109
volatile bool processing_done_ = true;
@@ -111,7 +113,6 @@ class GpsDriver {
111113

112114
static void threadHelper(void *instance);
113115
};
114-
} // namespace xbot::driver::gps
115-
116+
} // namespace xbot::driver::gps
116117

117118
#endif // XBOT_DRIVER_GPS_GPS_INTERFACE_H

0 commit comments

Comments
 (0)