1+ /*
2+ * _______ _ _ _____ ____
3+ * |__ __| | | | |/ ____| _ \
4+ * | | ___ ___ _ __ _ _| | | | (___ | |_) |
5+ * | |/ _ \/ _ \ '_ \| | | | | | |\___ \| _ <
6+ * | | __/ __/ | | | |_| | |__| |____) | |_) |
7+ * |_|\___|\___|_| |_|\__, |\____/|_____/|____/
8+ * __/ |
9+ * |___/
10+ *
11+ * TeenyUSB - light weight usb stack for STM32 micro controllers
12+ *
13+ * Copyright (c) 2019 XToolBox - admin@xtoolbox.org
14+ * www.tusb.org
15+ *
16+ * Permission is hereby granted, free of charge, to any person obtaining a copy
17+ * of this software and associated documentation files (the "Software"), to deal
18+ * in the Software without restriction, including without limitation the rights
19+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20+ * copies of the Software, and to permit persons to whom the Software is
21+ * furnished to do so, subject to the following conditions:
22+ *
23+ * The above copyright notice and this permission notice shall be included in all
24+ * copies or substantial portions of the Software.
25+ *
26+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32+ * SOFTWARE.
33+ */
34+
35+ extern " C" {
36+ #include " tusbh.h"
37+ }
38+ #include < stdarg.h>
39+ #include " string.h"
40+ #include < stdlib.h>
41+
42+ #include " Arduino.h"
43+
44+ #ifdef TUSB_HAS_OS
45+
46+ #define TUSBH_MSG_Q_LENGTH 16
47+
48+ static rtos::Mail<tusbh_message_t , TUSBH_MSG_Q_LENGTH> mail_box;
49+
50+ tusbh_msg_q_t * tusbh_mq_create ()
51+ {
52+ return NULL ;
53+ }
54+
55+ void tusbh_mq_free (tusbh_msg_q_t * mq)
56+ {
57+ }
58+
59+
60+ int tusbh_mq_init (tusbh_msg_q_t * mq)
61+ {
62+ return 0 ;
63+ }
64+
65+ int tusbh_mq_post (tusbh_msg_q_t * mq, const tusbh_message_t * msg)
66+ {
67+ tusbh_message_t *mail = mail_box.alloc ();
68+ memcpy (mail, msg, sizeof (tusbh_message_t ));
69+ mail_box.put (mail);
70+ return 1 ;
71+ }
72+
73+ int tusbh_mq_get (tusbh_msg_q_t * mq, tusbh_message_t * msg)
74+ {
75+ osEvent evt = mail_box.get ();
76+ if (evt.status == osEventMail) {
77+ tusbh_message_t *mail = (tusbh_message_t *)evt.value .p ;
78+ memcpy (msg, mail, sizeof (tusbh_message_t ));
79+ mail_box.free (mail);
80+ return 1 ;
81+ }
82+ return 0 ;
83+ }
84+
85+ #define MAX_DEVICE_COUNT 16
86+ static tusbh_device_t * device_pool[MAX_DEVICE_COUNT] = { NULL };
87+
88+ tusbh_device_t * tusbh_new_device ()
89+ {
90+ for (int i=0 ;i<MAX_DEVICE_COUNT;i++){
91+ if (device_pool[i] == NULL ){
92+ device_pool[i] = (tusbh_device_t *)calloc (sizeof (tusbh_device_t ), 1 );
93+ return device_pool[i];
94+ }
95+ }
96+ TUSB_OS_INFO (" Error: no free device space\n " );
97+ return 0 ;
98+ }
99+
100+ void tusbh_free_device (tusbh_device_t * device)
101+ {
102+ for (int i=0 ;i<MAX_DEVICE_COUNT;i++){
103+ if (device == device_pool[i] && device != NULL ){
104+ delete device;
105+ device_pool[i] = NULL ;
106+ return ;
107+ }
108+ }
109+ TUSB_OS_INFO (" Error: device memory out bound\n " );
110+ }
111+
112+ struct _tusbh_evt
113+ {
114+ rtos::EventFlags* event;
115+ };
116+
117+ #define MAX_EVENTS_COUNT 16
118+ static tusbh_evt_t * event_pool[MAX_EVENTS_COUNT] = { NULL };
119+
120+ tusbh_evt_t * tusbh_evt_create ()
121+ {
122+ for (int i=0 ;i<MAX_EVENTS_COUNT;i++){
123+ if (event_pool[i] == NULL ){
124+ event_pool[i] = (tusbh_evt_t *)calloc (sizeof (tusbh_evt_t ), 1 );
125+ event_pool[i]->event = new rtos::EventFlags ();
126+ return event_pool[i];
127+ }
128+ }
129+ TUSB_OS_INFO (" Error: no free event space\n " );
130+ return 0 ;
131+ }
132+
133+ void tusbh_evt_free (tusbh_evt_t * evt)
134+ {
135+ for (int i=0 ;i<MAX_EVENTS_COUNT;i++){
136+ if (evt == event_pool[i] && evt != NULL ){
137+ delete evt->event ;
138+ delete evt;
139+ event_pool[i] = NULL ;
140+ return ;
141+ }
142+ }
143+ TUSB_OS_INFO (" Error: Event memory out bound\n " );
144+ }
145+
146+ int tusbh_evt_init (tusbh_evt_t * evt)
147+ {
148+ return 0 ;
149+ }
150+
151+ int tusbh_evt_set (tusbh_evt_t * evt)
152+ {
153+ evt->event ->set (1 );
154+ return 0 ;
155+ }
156+
157+
158+ int tusbh_evt_clear (tusbh_evt_t * evt)
159+ {
160+ evt->event ->set (0 );
161+ return 0 ;
162+ }
163+
164+ int tusbh_evt_wait (tusbh_evt_t * evt, uint32_t timeout_ms)
165+ {
166+ evt->event ->wait_any (0xFF , timeout_ms);
167+ }
168+
169+ static int mem_used;
170+ static int mem_max;
171+ void * tusbh_malloc (uint32_t size)
172+ {
173+ size = (size + 3 ) & (~3 );
174+ mem_used+=size;
175+ if (mem_max < mem_used){
176+ mem_max = mem_used;
177+ }
178+ void * r = malloc (size+8 );
179+ TUSB_ASSERT ( (r != 0 ) && (((uint32_t )r) & 3 ) == 0 );
180+ uint32_t * p = (uint32_t *)r;
181+ *p = size;
182+ *(p + (size/4 ) + 1 ) = 0xdeadbeef ;
183+ // TUSB_OS_INFO("Allocate %p %d\n", p, size);
184+ return (void *)(p+1 );
185+ }
186+
187+ void tusbh_free (void * ptr)
188+ {
189+ TUSB_ASSERT (ptr != 0 );
190+ uint32_t * p = (uint32_t *)ptr;
191+ p = p - 1 ;
192+ uint32_t size = *p;
193+ mem_used -= size;
194+ TUSB_ASSERT (*(p+(size/4 )+1 ) == 0xdeadbeef );
195+ // TUSB_OS_INFO("Free %p %d\n", p, size);
196+ free (p);
197+ }
198+
199+ void show_memory (void )
200+ {
201+ }
202+
203+ #endif // TUSB_HAS_OS
0 commit comments