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
cdc.c
Go to the documentation of this file.
1
24#include "bsp/cdc.h"
25
26#include <string.h>
27
28#ifndef HOST_TEST
29
30#include "usbd_cdc_if.h"
31#include "icarus/icarus.h" /* task_active_sleep */
32
33bool CDC_Write(const uint8_t *data, uint16_t len) {
34 if (len == 0u) {
35 return true;
36 }
37 if (data == NULL) {
38 return false;
39 }
40
41 /* CDC_Transmit_FS takes a non-const buffer; it does not modify the
42 * payload but its prototype is loose. Casting away const here is
43 * safe — we own the call site contract. */
44 uint8_t *buf = (uint8_t *)(uintptr_t)data;
45
46 uint8_t result = CDC_Transmit_FS(buf, len);
47 uint16_t retries = 0u;
48 while (result == USBD_BUSY && retries < CDC_WRITE_MAX_RETRIES) {
50 result = CDC_Transmit_FS(buf, len);
51 retries++;
52 }
53 return (result == USBD_OK);
54}
55
56#else /* HOST_TEST — in-memory sink */
57
58#define CDC_HOST_SINK_SIZE 256u
59
60static uint8_t g_cdc_sink[CDC_HOST_SINK_SIZE];
61static uint16_t g_cdc_sink_len;
62static uint32_t g_cdc_call_count;
63static bool g_cdc_force_fail;
64
65bool CDC_Write(const uint8_t *data, uint16_t len) {
66 g_cdc_call_count++;
67 if (g_cdc_force_fail) {
68 return false;
69 }
70 if (len == 0u) {
71 return true;
72 }
73 if (data == NULL) {
74 return false;
75 }
76 uint16_t write_len = len;
77 if (write_len > (uint16_t)CDC_HOST_SINK_SIZE) {
78 write_len = (uint16_t)CDC_HOST_SINK_SIZE;
79 }
80 (void)memcpy(g_cdc_sink, data, write_len);
81 g_cdc_sink_len = write_len;
82 return true;
83}
84
85/* ---- Test-only host hooks ---------------------------------------------- */
86
87const uint8_t *__cdc_host_sink(void) {
88 return g_cdc_sink;
89}
90
91uint16_t __cdc_host_sink_len(void) {
92 return g_cdc_sink_len;
93}
94
95uint32_t __cdc_host_call_count(void) {
96 return g_cdc_call_count;
97}
98
99void __cdc_host_set_fail(bool fail) {
100 g_cdc_force_fail = fail;
101}
102
103void __cdc_host_reset_state(void) {
104 (void)memset(g_cdc_sink, 0, sizeof(g_cdc_sink));
105 g_cdc_sink_len = 0u;
106 g_cdc_call_count = 0u;
107 g_cdc_force_fail = false;
108}
109
110#endif /* HOST_TEST */
111
112bool CDC_WriteString(const char *s) {
113 if (s == NULL) {
114 return true;
115 }
116 return CDC_Write((const uint8_t *)s, (uint16_t)strlen(s));
117}
bool CDC_WriteString(const char *s)
Convenience wrapper that pushes a NUL-terminated C string.
Definition cdc.c:112
bool CDC_Write(const uint8_t *data, uint16_t len)
Push len bytes to the USB CDC IN endpoint.
Definition cdc.c:33
#define CDC_WRITE_MAX_RETRIES
Maximum number of busy-retry attempts inside CDC_Write.
Definition cdc.h:47
ICARUS OS - Main API Header.
uint32_t task_active_sleep(uint32_t ticks)
Sleep for specified ticks (cooperative, via SVC)
Definition svc.c:522
uint8_t CDC_Transmit_FS(uint8_t *buf, uint16_t len)
Transmit data over USB CDC.