ACwing FC firmware
STM32WB55: attitude IEKF, altitude Kalman filter, flight core, protocol v2
Loading...
Searching...
No Matches
app_link.c
Go to the documentation of this file.
1
5#include "app_link.h"
6#include "proto_v2.h"
7#include <string.h>
8
9#if defined(__arm__)
10#define APPL_BARRIER() __asm volatile ("dmb" ::: "memory")
11#else
12#define APPL_BARRIER() __sync_synchronize()
13#endif
14
15#define QMASK (APPL_QUEUE_LEN - 1u)
16#if (APPL_QUEUE_LEN & QMASK) != 0
17#error "APPL_QUEUE_LEN must be a power of 2"
18#endif
19
21static volatile uint32_t s_head, s_tail, s_qdrop;
22
23static volatile uint32_t s_last_ms;
24static volatile uint8_t s_ever;
25static volatile uint8_t s_down;
26
27static volatile uint32_t s_rc_seq; /* odd while the writer is in the middle */
28static volatile uint32_t s_rc_ms;
29static volatile uint8_t s_rc_ever;
31
32void appl_reset(void)
33{
34 s_head = s_tail = s_qdrop = 0u;
35 s_last_ms = 0u; s_ever = 0u; s_down = 0u;
36 s_rc_seq = 0u; s_rc_ms = 0u; s_rc_ever = 0u;
37 memset(&s_rc, 0, sizeof s_rc);
38}
39
40/* ------------------------------------------------------------ BLE context */
41void appl_on_cmd_write(const uint8_t *d, uint16_t len, uint32_t now_ms)
42{
43 if (!d || len == 0u) return;
44
45 /* every CMD frame refreshes the app-link time: one atomic 32-bit store */
46 s_last_ms = now_ms;
48 s_ever = 1u;
49 s_down = 0u;
50
51 const uint8_t op = d[0];
52 if (op < PV2_OP_HEARTBEAT) return; /* legacy 0x01/0x02: handled inline */
53 if (op == PV2_OP_HEARTBEAT) return; /* time only */
54
55 if (op == PV2_OP_RC) {
56 if (len < 11u) return; /* op seq thr yaw pitch roll buttons */
57 s_rc_seq++; /* odd: write in progress */
59 s_rc.thr = pv2_rd_i16(&d[2]);
60 s_rc.yaw = pv2_rd_i16(&d[4]);
61 s_rc.pitch = pv2_rd_i16(&d[6]);
62 s_rc.roll = pv2_rd_i16(&d[8]);
63 s_rc.buttons = d[10];
64 s_rc_ms = now_ms;
66 s_rc_seq++; /* even: consistent */
67 s_rc_ever = 1u;
68 return;
69 }
70
71 /* any other command: copy into the queue, never block */
72 const uint32_t head = s_head;
73 if (head - s_tail >= APPL_QUEUE_LEN) { s_qdrop++; return; }
74 appl_cmd_t *c = &s_q[head & QMASK];
75 c->len = (uint8_t)((len > APPL_CMD_MAX) ? APPL_CMD_MAX : len);
76 memcpy(c->b, d, c->len);
78 s_head = head + 1u;
79}
80
81void appl_on_connect(void) { }
82void appl_on_disconnect(void) { s_down = 1u; }
83
84/* ------------------------------------------------------------ AppTask */
86{
87 const uint32_t tail = s_tail;
88 if (s_head == tail) return 0;
90 *out = s_q[tail & QMASK];
92 s_tail = tail + 1u;
93 return 1;
94}
95
96uint32_t appl_queue_dropped(void) { return s_qdrop; }
97
98/* ------------------------------------------------------------ readers */
99uint8_t appl_ever(void) { return s_ever; }
100uint32_t appl_last_ms(void) { return s_last_ms; }
101uint8_t appl_link_down(void) { return s_down; }
102
103uint32_t appl_silence_ms(uint32_t now_ms, uint32_t down_floor_ms)
104{
105 if (!s_ever) return UINT32_MAX;
106 uint32_t s = now_ms - s_last_ms;
107 if (s > 0x80000000u) s = 0u; /* stamp newer than 'now': just arrived */
108 if (s_down && s < down_floor_ms) s = down_floor_ms;
109 return s;
110}
111
112int appl_rc_read(appl_rc_t *out, uint32_t *t_ms)
113{
114 if (!s_rc_ever) return 0;
115 for (int tries = 0; tries < 4; ++tries) {
116 const uint32_t s0 = s_rc_seq;
117 if (s0 & 1u) continue; /* writer busy: retry */
118 APPL_BARRIER();
119 const appl_rc_t v = s_rc;
120 const uint32_t t = s_rc_ms;
121 APPL_BARRIER();
122 if (s_rc_seq == s0) { *out = v; if (t_ms) *t_ms = t; return 1; }
123 }
124 return 0; /* keep the caller's previous copy */
125}
static batt_state_t s
Definition battery.c:21
ACwing Pilot app protocol v2: identifiers and frame layouts.
#define PV2_OP_HEARTBEAT
Definition proto_v2.h:49
static int16_t pv2_rd_i16(const uint8_t *p)
Definition proto_v2.h:330
#define PV2_OP_RC
Definition proto_v2.h:50
uint8_t b[APPL_CMD_MAX]
Definition app_link.h:33
uint8_t len
Definition app_link.h:32
int16_t roll
-1000..1000: thr -1000 = stick bottom
Definition app_link.h:37
uint8_t buttons
Definition app_link.h:38
int16_t yaw
Definition app_link.h:37
int16_t thr
Definition app_link.h:37
int16_t pitch
Definition app_link.h:37