Skip to content

Commit 25d434a

Browse files
authored
Merge pull request #946 from lukash/imu-high-prio
imu: Raise the IMU thread priority to NORMALPRIO+2 on HW transports
2 parents 5b7f277 + 16bc128 commit 25d434a

5 files changed

Lines changed: 11 additions & 1 deletion

File tree

imu/imu_thread.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ void imu_thread_start(void (*cb)(float *accel, float *gyro, float *mag, float dt
136136
m_dev->interface->enable_drdy_output(m_dev, true);
137137
}
138138

139-
m_thd = chThdCreateStatic(m_wa, sizeof(m_wa), NORMALPRIO, thread_func, NULL);
139+
// Run above NORMALPRIO threads and the CAN read thread (NORMALPRIO + 1) when the
140+
// transport allows it (is not CPU-heavy).
141+
tprio_t prio = m_dev->transport->interface->cpu_bound ? NORMALPRIO : NORMALPRIO + 2;
142+
m_thd = chThdCreateStatic(m_wa, sizeof(m_wa), prio, thread_func, NULL);
140143
}
141144

142145
void imu_thread_stop(void) {

imu/transport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ typedef struct transport transport_t;
4444
typedef struct {
4545
// Human-readable transport name for diagnostics, e.g. "i2c-bb".
4646
const char *name;
47+
// True when a transfer occupies the CPU for its whole duration (bit-banged
48+
// buses) and takes long to complete. Decides whether the IMU thread can
49+
// run above NORMALPRIO.
50+
bool cpu_bound;
4751
// Highest sample rate (Hz) this bus may be driven at. Setting too high
4852
// sample rate has caused rare unexplained MCU resets, probably due to CPU
4953
// saturation. Each bus should set a safe maximum.

imu/transport_i2c_bb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static uint16_t max_sample_rate(transport_t *t) {
5050

5151
static const transport_interface_t i2c_bb_interface = {
5252
.name = "i2c-bb",
53+
.cpu_bound = true,
5354
.max_sample_rate = max_sample_rate,
5455
.read_reg = read_reg,
5556
.write_reg = write_reg,

imu/transport_spi_bb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ static uint16_t max_sample_rate(transport_t *t) {
6868

6969
static const transport_interface_t spi_bb_interface = {
7070
.name = "spi-bb",
71+
.cpu_bound = true,
7172
.max_sample_rate = max_sample_rate,
7273
.read_reg = read_reg,
7374
.write_reg = write_reg,

imu/transport_spi_hw.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ static void deinit(transport_t *t) {
107107

108108
static const transport_interface_t spi_hw_interface = {
109109
.name = "spi-hw",
110+
.cpu_bound = false,
110111
.max_sample_rate = max_sample_rate,
111112
.read_reg = read_reg,
112113
.write_reg = write_reg,

0 commit comments

Comments
 (0)