STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
sensors_task.c
Go to the documentation of this file.
1#include "sensors_task.h"
2
3#include "board_devbus.h"
4#include "board_exti.h"
5#include "board_time.h"
6#include "cmsis_os2.h"
7#include "emmc_stm32.h"
8#include "main.h"
9
10#define EV_MODE (1U << 4) /* sensors_task_set_active() */
11
15static volatile bool want_active; /* written by the mode, read here */
16
17static void to_emmc(void* ctx, uint8_t type, const void* data, uint8_t len) {
18 (void)ctx;
19 /* Non-blocking; false outside ARMED/LAUNCHED/RECOVERY or when every
20 * chunk is full (the log counts and reports drops itself). */
22 len);
23}
24
25static void sensors_task(void* argument) {
26 (void)argument;
30
34 core.sink.record = to_emmc;
35
36 bool active = false; /* chips sit in power-down until the first start */
37 for (;;) {
38 if (!want_active) {
39 if (active) {
41 sensors_core_stop(&core); /* Idle: power down */
43 active = false;
44 }
46 continue;
47 }
48 if (!active) {
52 active = true;
53 }
54 uint32_t wait_us = sensors_core_wait_us(&core, board_time_us());
55 uint32_t ticks = (wait_us + 999U) / 1000U; /* 1 kHz tick */
56 uint32_t flags = osThreadFlagsWait(
58 if (flags & osFlagsError) {
59 flags = 0; /* timeout: deadlines only */
60 }
61 flags &= ~EV_MODE; /* re-checked at the top of the loop */
66 }
67}
68
74
76 static const osMutexAttr_t lock_attr = {"sensors", osMutexPrioInherit, NULL,
77 0};
78 static const osThreadAttr_t attr = {
79 .name = "sensors",
80 .stack_size = 2048,
81 .priority = osPriorityRealtime,
82 };
83 snapshot_lock = osMutexNew(&lock_attr);
85 return thread != NULL;
86}
87
89 want_active = on;
90 if (thread) {
92 }
93}
#define ACCEL_INT_1_Pin
Definition main.h:170
#define IMU_INT1_Pin
Definition main.h:163
dev_bus_t board_devbus(board_spi_device_t device)
bool board_exti_route(uint16_t pin, osThreadId_t thread, uint32_t flags)
Definition board_exti.c:29
uint32_t board_exti_last_us(uint16_t pin)
Definition board_exti.c:39
@ BOARD_SPI_BARO
Definition board_spi.h:29
@ BOARD_SPI_IMU
Definition board_spi.h:27
@ BOARD_SPI_HIGH_G
Definition board_spi.h:28
uint32_t board_time_us(void)
Definition board_time.c:34
#define osFlagsError
Error indicator.
Definition cmsis_os2.h:181
#define osFlagsWaitAny
Wait for any flag (default).
Definition cmsis_os2.h:176
void * osMutexId_t
Definition cmsis_os2.h:220
#define osMutexPrioInherit
Priority inherit protocol.
Definition cmsis_os2.h:194
osThreadId_t osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)
Create a thread and add it to Active Threads.
Definition cmsis_os2.c:577
void * osThreadId_t
Definition cmsis_os2.h:211
uint32_t osThreadFlagsSet(osThreadId_t thread_id, uint32_t flags)
Set the specified Thread Flags of a thread.
uint32_t osKernelGetTickCount(void)
Get the RTOS kernel tick count.
Definition cmsis_os2.c:491
osThreadId_t osThreadGetId(void)
Return the thread ID of the current running thread.
Definition cmsis_os2.c:669
#define osWaitForever
Wait forever timeout value.
Definition cmsis_os2.h:173
osMutexId_t osMutexNew(const osMutexAttr_t *attr)
Create and Initialize a Mutex object.
osStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout)
Acquire a Mutex or timeout if it is locked.
uint32_t osThreadFlagsWait(uint32_t flags, uint32_t options, uint32_t timeout)
Wait for one or more Thread Flags of the current running thread to become signaled.
@ osPriorityRealtime
Priority: realtime.
Definition cmsis_os2.h:147
osStatus_t osMutexRelease(osMutexId_t mutex_id)
Release a Mutex that was acquired by osMutexAcquire.
bool emmc_log_append(emmc_log_t *log, uint8_t type, uint32_t t_ms, const void *data, uint8_t len)
Definition emmc_log.c:235
emmc_log_t * emmc_stm32_log(void)
Definition emmc_stm32.c:246
static void to_emmc(void *ctx, uint8_t type, const void *data, uint8_t len)
Definition gnss_task.c:37
#define EV_MODE
Definition gnss_task.c:10
static volatile bool want_active
Definition gnss_task.c:14
static osThreadId_t thread
Definition gnss_task.c:13
static gnss_core_t core
Definition gnss_task.c:12
#define NULL
Definition usbd_def.h:44
uint32_t sensors_core_wait_us(const sensors_core_t *c, uint32_t now_us)
void sensors_core_init(sensors_core_t *c, uint32_t now_us)
void sensors_core_stop(sensors_core_t *c)
void sensors_core_step(sensors_core_t *c, uint32_t events, uint32_t imu_edge_us, uint32_t highg_edge_us, uint32_t now_us)
#define SENSORS_EV_HIGHG
#define SENSORS_EV_IMU
void sensors_task_snapshot(sensors_core_t *out)
static void to_emmc(void *ctx, uint8_t type, const void *data, uint8_t len)
bool sensors_task_start(void)
static osMutexId_t snapshot_lock
static void sensors_task(void *argument)
void sensors_task_set_active(bool on)
Attributes structure for mutex.
Definition cmsis_os2.h:269
Attributes structure for thread.
Definition cmsis_os2.h:240