Skip to content

Commit

Permalink
Add critical section for sbus read() operation
Browse files Browse the repository at this point in the history
  • Loading branch information
shengwen-tw committed May 19, 2024
1 parent 261d697 commit 95bd6e0
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions drivers/devices/sbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string.h>

#include <fs/fs.h>
#include <kernel/preempt.h>
#include <kernel/printk.h>
#include <kernel/time.h>

Expand Down Expand Up @@ -45,16 +46,19 @@ void sbus_interrupt_handler(uint8_t new_byte)
}

if (sbus.index < 24) {
/* Append new byte */
sbus.buf[sbus.index] = new_byte;

/* Append new byte until full */
sbus.index++;
} else {
/* Remove the oldest byte */
for (int i = 0; i < 23; i++)
sbus.buf[i] = sbus.buf[i + 1];
}

/* Append new byte at the last position */
sbus.buf[sbus.index] = new_byte;
/* Append new byte */
sbus.buf[sbus.index] = new_byte;
}

/* Decode new frame */
if ((sbus.index == 24) && (sbus.buf[0] == 0x0f) && (sbus.buf[24] == 0x00))
Expand All @@ -76,13 +80,16 @@ static ssize_t sbus_read(struct file *filp,
if (size != sizeof(sbus_t))
return -EINVAL;

preempt_disable();

/* RC signal mapping */
float throttle_raw = (float) sbus.rc_val[2]; // channel 3
float roll_raw = (float) sbus.rc_val[0]; // channel 1
float pitch_raw = (float) sbus.rc_val[1]; // channel 2
float yaw_raw = (float) sbus.rc_val[3]; // channel 4
float dual_sw1 = (float) sbus.rc_val[4]; // channel 5

/* "sbus" should be accessed atomically in critical section */
sbus.roll = (float) (roll_raw - RC_ROLL_MIN) / (RC_ROLL_MAX - RC_ROLL_MIN) *
(RC_ROLL_RANGE_MAX - RC_ROLL_RANGE_MIN) +
RC_ROLL_RANGE_MIN;
Expand All @@ -105,6 +112,8 @@ static ssize_t sbus_read(struct file *filp,
/* Return raw data and mapped signal */
memcpy(buf, &sbus, sizeof(sbus_t));

preempt_enable();

return size;
}

Expand Down

0 comments on commit 95bd6e0

Please sign in to comment.