ACwing FC firmware
STM32WB55: attitude IEKF, altitude Kalman filter, flight core, protocol v2
Loading...
Searching...
No Matches
log_queue.c
Go to the documentation of this file.
1
5#include "log_queue.h"
6#include "spsc.h"
7#include <string.h>
8
9typedef struct {
10 uint8_t *buf;
11 uint32_t size; /* power of two */
12 volatile uint32_t head, tail; /* free-running byte counters */
13 volatile uint16_t drops; /* producer increments ... */
14 uint16_t drops_seen; /* ... consumer reads */
15} ring_t;
16
19 { s_b_imu, LOG_Q_IMU_SIZE, 0, 0, 0, 0 },
20 { s_b_batt, LOG_Q_BATT_SIZE, 0, 0, 0, 0 },
21};
22static volatile uint8_t s_active;
23
24uint8_t log_q_active(void) { return s_active; }
25void log_q_set_active(uint8_t on) { s_active = on; }
26
27void log_q_reset(void)
28{
29 for (int i = 0; i < LOG_SRC_COUNT; ++i) { s_r[i].head = s_r[i].tail = 0; s_r[i].drops = s_r[i].drops_seen = 0; }
30 s_active = 0;
31}
32
33int log_q_put(uint8_t src, uint8_t type, const void *p, uint8_t len)
34{
35 if (src >= LOG_SRC_COUNT) return -1;
36 ring_t *r = &s_r[src];
37 const uint32_t h = r->head;
39 const uint32_t used = h - r->tail;
40 const uint32_t n = 2u + len;
41 if (r->size - used < n) { r->drops = (uint16_t)(r->drops + 1u); return -1; }
42 const uint32_t m = r->size - 1u;
43 r->buf[h & m] = type;
44 r->buf[(h + 1u) & m] = len;
45 const uint8_t *b = (const uint8_t *)p;
46 for (uint32_t k = 0; k < len; ++k) r->buf[(h + 2u + k) & m] = b[k];
48 r->head = h + n;
49 return 0;
50}
51
52uint32_t log_q_get(uint8_t src, uint8_t *type, void *p, uint8_t cap)
53{
54 if (src >= LOG_SRC_COUNT) return 0;
55 ring_t *r = &s_r[src];
56 const uint32_t t = r->tail;
57 const uint32_t h = r->head;
59 if (h == t) return 0;
60 const uint32_t m = r->size - 1u;
61 *type = r->buf[t & m];
62 const uint8_t len = r->buf[(t + 1u) & m];
63 uint8_t *b = (uint8_t *)p;
64 for (uint32_t k = 0; k < len; ++k) {
65 const uint8_t v = r->buf[(t + 2u + k) & m];
66 if (k < cap) b[k] = v;
67 }
69 r->tail = t + 2u + len;
70 return (uint32_t)(len > cap ? cap : len) + 1u;
71}
72
73uint16_t log_q_take_drops(uint8_t src)
74{
75 if (src >= LOG_SRC_COUNT) return 0;
76 ring_t *r = &s_r[src];
77 const uint16_t d = r->drops;
78 const uint16_t n = (uint16_t)(d - r->drops_seen);
79 r->drops_seen = d;
80 return n;
81}
82
83_Static_assert((LOG_Q_IMU_SIZE & (LOG_Q_IMU_SIZE - 1u)) == 0u, "power of two");
84_Static_assert((LOG_Q_BATT_SIZE & (LOG_Q_BATT_SIZE - 1u)) == 0u, "power of two");
double b[6]
Definition calib_mgr.c:53
uint32_t n
Definition calib_mgr.c:35
float used
Definition calib_mgr.c:50
float cap[6][3]
Definition calib_mgr.c:36
static volatile uint8_t s_active
Definition log_queue.c:22
int log_q_put(uint8_t src, uint8_t type, const void *p, uint8_t len)
0 = queued
Definition log_queue.c:33
static uint8_t s_b_batt[LOG_Q_BATT_SIZE]
Definition log_queue.c:17
static uint8_t s_b_imu[LOG_Q_IMU_SIZE]
Definition log_queue.c:17
static ring_t s_r[LOG_SRC_COUNT]
Definition log_queue.c:18
uint32_t log_q_get(uint8_t src, uint8_t *type, void *p, uint8_t cap)
next record of 'src': returns payload length + 1, 0 if empty
Definition log_queue.c:52
void log_q_reset(void)
Definition log_queue.c:27
uint8_t log_q_active(void)
Definition log_queue.c:24
void log_q_set_active(uint8_t on)
Definition log_queue.c:25
uint16_t log_q_take_drops(uint8_t src)
Definition log_queue.c:73
Record queues from the producing tasks to the LogTask.
@ LOG_SRC_COUNT
Definition log_queue.h:18
#define LOG_Q_BATT_SIZE
Definition log_queue.h:21
#define LOG_Q_IMU_SIZE
~1.2 s of ImuTask records: covers a sector erase
Definition log_queue.h:20
uint8_t on
Definition log_task.c:48
Lock-free single-producer single-consumer queue of fixed-size items.
#define SPSC_BARRIER()
Definition spsc.h:18
volatile uint32_t head
Definition tlm_ble.c:29
uint16_t drops_seen
Definition log_queue.c:14
volatile uint16_t drops
Definition log_queue.c:13
uint8_t buf[TLM_BLE_RING_SIZE]
Definition tlm_ble.c:28
uint32_t size
Definition log_queue.c:11
volatile uint32_t tail
Definition tlm_ble.c:30
uint8_t * buf
Definition log_queue.c:10