ICARUS OS (Intelligent Cooperative Architecture for Real-time Unified Systems) 0.1.0
Preemptive Real-Time Operating System for ARM Cortex-M7
Loading...
Searching...
No Matches
pipe.c
Go to the documentation of this file.
1
16#include "icarus/pipe.h"
17#include "icarus/kernel.h"
18#include "icarus/scheduler.h"
19
20/* ============================================================================
21 * SECTION PLACEMENT MACROS
22 * ========================================================================= */
23
24/* Defined centrally in icarus/config.h — included via icarus/kernel.h */
25
26/* ============================================================================
27 * MESSAGE PIPE IMPLEMENTATION
28 * ========================================================================= */
29
35ITCM_FUNC bool __pipe_init(uint8_t pipe_idx, uint16_t pipe_capacity_bytes) {
36 if ((pipe_idx < (uint8_t)ICARUS_MAX_MESSAGE_QUEUES) &&
37 (pipe_capacity_bytes > 0u) &&
38 (pipe_capacity_bytes <= (uint16_t)ICARUS_MAX_MESSAGE_BYTES)) {
39
40 if (!message_pipe_list[pipe_idx]->engaged) {
41 message_pipe_list[pipe_idx]->count = 0;
42 message_pipe_list[pipe_idx]->max_count = pipe_capacity_bytes;
43 message_pipe_list[pipe_idx]->enqueue_idx = 0;
44 message_pipe_list[pipe_idx]->dequeue_idx = 0;
46 message_pipe_list[pipe_idx]->engaged = true;
47
48 for (uint16_t i = 0u; i < (uint16_t)ICARUS_MAX_MESSAGE_BYTES; i++) {
49 message_pipe_list[pipe_idx]->buffer[i] = 0;
50 }
51
52 return true;
53 }
54 }
55
56 return false;
57}
58
64ITCM_FUNC bool __pipe_enqueue(uint8_t pipe_idx, uint8_t *message,
65 uint8_t message_bytes) {
66#ifdef HOST_TEST
67 /* In host tests, check validity directly (no MPU) */
68 if ((pipe_idx >= (uint8_t)ICARUS_MAX_MESSAGE_QUEUES) ||
69 (!message_pipe_list[pipe_idx]->engaged) ||
70 (message_bytes > message_pipe_list[pipe_idx]->max_count)) {
71 return false;
72 }
73#else
74 /* On target, index check only — gate checks engaged+size via SVC */
75 if (pipe_idx >= ICARUS_MAX_MESSAGE_QUEUES) {
76 return false;
77 }
78#endif
79
80 /* pipe_can_enqueue checks engaged + free space via SVC (priv-safe) */
81 while (!pipe_can_enqueue(pipe_idx, message_bytes)) {
82 (void)task_active_sleep(1);
83 }
84
85 pipe_write_bytes(pipe_idx, message, message_bytes);
86 return true;
87}
88
94ITCM_FUNC bool __pipe_dequeue(uint8_t pipe_idx, uint8_t *message,
95 uint8_t message_bytes) {
96#ifdef HOST_TEST
97 /* In host tests, check validity directly (no MPU) */
98 if ((pipe_idx >= (uint8_t)ICARUS_MAX_MESSAGE_QUEUES) ||
99 (!message_pipe_list[pipe_idx]->engaged) ||
100 (message_bytes > message_pipe_list[pipe_idx]->max_count)) {
101 return false;
102 }
103#else
104 /* On target, index check only — gate checks engaged+size via SVC */
105 if (pipe_idx >= ICARUS_MAX_MESSAGE_QUEUES) {
106 return false;
107 }
108#endif
109
110 /* pipe_can_dequeue checks engaged + available bytes via SVC (priv-safe) */
111 while (!pipe_can_dequeue(pipe_idx, message_bytes)) {
112 (void)task_active_sleep(1);
113 }
114
115 pipe_read_bytes(pipe_idx, message, message_bytes);
116 return true;
117}
118
123ITCM_FUNC uint16_t __pipe_get_count(uint8_t pipe_idx) {
124 if ((pipe_idx >= (uint8_t)ICARUS_MAX_MESSAGE_QUEUES) ||
125 (!message_pipe_list[pipe_idx]->engaged)) {
126 return 0;
127 }
128 return message_pipe_list[pipe_idx]->count;
129}
130
135ITCM_FUNC uint16_t __pipe_get_max_count(uint8_t pipe_idx) {
136 if ((pipe_idx >= (uint8_t)ICARUS_MAX_MESSAGE_QUEUES) ||
137 (!message_pipe_list[pipe_idx]->engaged)) {
138 return 0;
139 }
140 return message_pipe_list[pipe_idx]->max_count;
141}
142
149ITCM_FUNC bool __pipe_can_enqueue(uint8_t pipe_idx, uint8_t message_bytes) {
150 if ((pipe_idx >= (uint8_t)ICARUS_MAX_MESSAGE_QUEUES) ||
151 (!message_pipe_list[pipe_idx]->engaged)) {
152 return false;
153 }
154 return (message_pipe_list[pipe_idx]->max_count -
155 message_pipe_list[pipe_idx]->count) >= message_bytes;
156}
157
163ITCM_FUNC bool __pipe_can_dequeue(uint8_t pipe_idx, uint8_t message_bytes) {
164 if ((pipe_idx >= (uint8_t)ICARUS_MAX_MESSAGE_QUEUES) ||
165 (!message_pipe_list[pipe_idx]->engaged)) {
166 return false;
167 }
168 return message_pipe_list[pipe_idx]->count >= message_bytes;
169}
170
176ITCM_FUNC void __pipe_write_bytes(uint8_t pipe_idx, uint8_t *message, uint8_t message_bytes) {
177 if ((pipe_idx >= (uint8_t)ICARUS_MAX_MESSAGE_QUEUES) ||
178 (!message_pipe_list[pipe_idx]->engaged)) {
179 return;
180 }
181
182 for (uint8_t i = 0; i < message_bytes; i++) {
183 message_pipe_list[pipe_idx]->buffer[
184 message_pipe_list[pipe_idx]->enqueue_idx] = message[i];
185 message_pipe_list[pipe_idx]->enqueue_idx =
186 (uint16_t)(((uint16_t)(message_pipe_list[pipe_idx]->enqueue_idx + 1u)) %
187 message_pipe_list[pipe_idx]->max_count);
188 message_pipe_list[pipe_idx]->count++;
189 }
190
192}
193
199ITCM_FUNC void __pipe_read_bytes(uint8_t pipe_idx, uint8_t *message, uint8_t message_bytes) {
200 if ((pipe_idx >= (uint8_t)ICARUS_MAX_MESSAGE_QUEUES) ||
201 (!message_pipe_list[pipe_idx]->engaged)) {
202 return;
203 }
204
205 for (uint8_t i = 0; i < message_bytes; i++) {
206 message[i] = message_pipe_list[pipe_idx]->buffer[
207 message_pipe_list[pipe_idx]->dequeue_idx];
208 message_pipe_list[pipe_idx]->dequeue_idx =
209 (uint16_t)(((uint16_t)(message_pipe_list[pipe_idx]->dequeue_idx + 1u)) %
210 message_pipe_list[pipe_idx]->max_count);
211 message_pipe_list[pipe_idx]->count--;
212 }
213
215}
#define ICARUS_MAX_MESSAGE_BYTES
Maximum bytes per message pipe buffer.
Definition config.h:76
#define ICARUS_MAX_MESSAGE_QUEUES
Maximum number of message queues (pipes)
Definition config.h:68
#define ITCM_FUNC
Definition config.h:155
ICARUS Kernel Core - State and Initialization.
icarus_pipe_t * message_pipe_list[64]
Array of pointers to all message pipes.
Definition kernel.c:52
volatile uint32_t os_tick_count
System tick counter.
Definition kernel.c:69
uint32_t task_active_sleep(uint32_t ticks)
Sleep for specified ticks (cooperative, via SVC)
Definition svc.c:522
uint16_t __pipe_get_count(uint8_t pipe_idx)
Privileged implementation of pipe_get_count.
Definition pipe.c:123
bool __pipe_init(uint8_t pipe_idx, uint16_t pipe_capacity_bytes)
Privileged implementation of pipe_init.
Definition pipe.c:35
uint16_t __pipe_get_max_count(uint8_t pipe_idx)
Privileged implementation of pipe_get_max_count.
Definition pipe.c:135
bool __pipe_enqueue(uint8_t pipe_idx, uint8_t *message, uint8_t message_bytes)
Privileged implementation of pipe_enqueue.
Definition pipe.c:64
bool __pipe_dequeue(uint8_t pipe_idx, uint8_t *message, uint8_t message_bytes)
Privileged implementation of pipe_dequeue.
Definition pipe.c:94
void __pipe_write_bytes(uint8_t pipe_idx, uint8_t *message, uint8_t message_bytes)
Privileged write gate: write bytes to pipe buffer.
Definition pipe.c:176
bool __pipe_can_dequeue(uint8_t pipe_idx, uint8_t message_bytes)
Privileged call gate: can pipe deliver message_bytes bytes?
Definition pipe.c:163
void __pipe_read_bytes(uint8_t pipe_idx, uint8_t *message, uint8_t message_bytes)
Privileged write gate: read bytes from pipe buffer.
Definition pipe.c:199
bool __pipe_can_enqueue(uint8_t pipe_idx, uint8_t message_bytes)
Privileged call gate: can pipe accept message_bytes bytes?
Definition pipe.c:149
ICARUS Pipe - Message Pipes (Circular Buffer Queues)
void pipe_read_bytes(uint8_t pipe_idx, uint8_t *message, uint8_t message_bytes)
Read bytes from pipe (SVC call gate)
Definition svc.c:1095
bool pipe_can_dequeue(uint8_t pipe_idx, uint8_t message_bytes)
Check if pipe has data available (SVC call gate)
Definition svc.c:1011
bool pipe_can_enqueue(uint8_t pipe_idx, uint8_t message_bytes)
Check if pipe can accept data (SVC call gate)
Definition svc.c:988
void pipe_write_bytes(uint8_t pipe_idx, uint8_t *message, uint8_t message_bytes)
Write bytes to pipe (SVC call gate)
Definition svc.c:1074
ICARUS Scheduler - Task Scheduling and Timing.
uint16_t count
Current number of bytes in buffer.
Definition types.h:171
uint16_t dequeue_idx
Read index (circular)
Definition types.h:174
uint16_t max_count
Maximum buffer capacity in bytes.
Definition types.h:172
uint16_t enqueue_idx
Write index (circular)
Definition types.h:173
uint8_t buffer[512]
Circular message buffer.
Definition types.h:176
uint32_t tick_updated_at
Last modification timestamp.
Definition types.h:175
bool engaged
true if pipe is initialized and active
Definition types.h:170