2 * The Clear BSD License
3 * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
7 * Redistribution and use in source and binary forms, with or without modification,
8 * are permitted (subject to the limitations in the disclaimer below) provided
9 * that the following conditions are met:
11 * o Redistributions of source code must retain the above copyright notice, this list
12 * of conditions and the following disclaimer.
14 * o Redistributions in binary form must reproduce the above copyright notice, this
15 * list of conditions and the following disclaimer in the documentation and/or
16 * other materials provided with the distribution.
18 * o Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
22 * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "fsl_device_registers.h"
39 #include "fsl_common.h"
41 /*******************************************************************************
43 ******************************************************************************/
44 #define USB_OSA_BM_EVENT_COUNT (4U)
45 #define USB_OSA_BM_SEM_COUNT (1U)
46 #define USB_OSA_BM_MSGQ_COUNT (1U)
47 #define USB_OSA_BM_MSG_COUNT (8U)
48 #define USB_OSA_BM_MSG_SIZE (4U)
50 /* BM Event status structure */
51 typedef struct _usb_osa_event_struct
53 uint32_t value; /* Event mask */
54 uint32_t flag; /* Event flags, includes auto clear flag */
55 uint8_t isUsed; /* Is used */
56 } usb_osa_event_struct_t;
58 /* BM semaphore status structure */
59 typedef struct _usb_osa_sem_struct
61 uint32_t value; /* Semaphore count */
62 uint8_t isUsed; /* Is used */
63 } usb_osa_sem_struct_t;
65 /* BM msg status structure */
66 typedef struct _usb_osa_msg_struct
68 uint32_t msg[USB_OSA_BM_MSG_SIZE]; /* Message entity pointer */
69 } usb_osa_msg_struct_t;
71 /* BM msgq status structure */
72 typedef struct _usb_osa_msgq_struct
74 usb_osa_msg_struct_t msgs[USB_OSA_BM_MSG_COUNT]; /* Message entity list */
75 uint32_t count; /* Max message entity count */
76 uint32_t msgSize; /* Size of each message */
77 uint32_t msgCount; /* Valid messages */
78 uint32_t index; /* The first empty message entity index */
79 uint32_t current; /* The vaild message index */
80 uint8_t isUsed; /* Is used */
81 } usb_osa_msgq_struct_t;
83 /*******************************************************************************
85 ******************************************************************************/
87 /*******************************************************************************
89 ******************************************************************************/
91 USB_GLOBAL USB_RAM_ADDRESS_ALIGNMENT(USB_DATA_ALIGN_SIZE) static usb_osa_sem_struct_t
92 s_UsbBmSemStruct[USB_OSA_BM_SEM_COUNT];
93 USB_GLOBAL USB_RAM_ADDRESS_ALIGNMENT(USB_DATA_ALIGN_SIZE) static usb_osa_event_struct_t
94 s_UsbBmEventStruct[USB_OSA_BM_EVENT_COUNT];
95 USB_GLOBAL USB_RAM_ADDRESS_ALIGNMENT(USB_DATA_ALIGN_SIZE) static usb_osa_msgq_struct_t
96 s_UsbBmMsgqStruct[USB_OSA_BM_MSGQ_COUNT];
98 /*******************************************************************************
100 ******************************************************************************/
102 void *USB_OsaMemoryAllocate(uint32_t length)
104 void *p = (void *)malloc(length);
105 uint8_t *temp = (uint8_t *)p;
108 for (uint32_t count = 0U; count < length; count++)
116 void USB_OsaMemoryFree(void *p)
121 void USB_OsaEnterCritical(uint32_t *sr)
123 *sr = DisableGlobalIRQ();
127 void USB_OsaExitCritical(uint32_t sr)
132 usb_osa_status_t USB_OsaEventCreate(usb_osa_event_handle *handle, uint32_t flag)
134 usb_osa_event_struct_t *event = NULL;
139 return kStatus_USB_OSA_Error;
142 USB_OSA_ENTER_CRITICAL();
143 for (uint32_t i = 0; i < USB_OSA_BM_EVENT_COUNT; i++)
145 if (0 == s_UsbBmEventStruct[i].isUsed)
147 event = &s_UsbBmEventStruct[i];
154 USB_OSA_EXIT_CRITICAL();
155 return kStatus_USB_OSA_Error;
162 USB_OSA_EXIT_CRITICAL();
163 return kStatus_USB_OSA_Success;
166 usb_osa_status_t USB_OsaEventDestroy(usb_osa_event_handle handle)
168 usb_osa_event_struct_t *event = (usb_osa_event_struct_t *)handle;
173 USB_OSA_ENTER_CRITICAL();
175 USB_OSA_EXIT_CRITICAL();
176 return kStatus_USB_OSA_Success;
178 return kStatus_USB_OSA_Error;
181 usb_osa_status_t USB_OsaEventSet(usb_osa_event_handle handle, uint32_t bitMask)
183 usb_osa_event_struct_t *event = (usb_osa_event_struct_t *)handle;
188 USB_OSA_ENTER_CRITICAL();
189 event->value |= bitMask;
190 USB_OSA_EXIT_CRITICAL();
191 return kStatus_USB_OSA_Success;
193 return kStatus_USB_OSA_Error;
196 usb_osa_status_t USB_OsaEventWait(
197 usb_osa_event_handle handle, uint32_t bitMask, uint32_t flag, uint32_t timeout, uint32_t *bitSet)
199 usb_osa_event_struct_t *event = (usb_osa_event_struct_t *)handle;
205 USB_OSA_ENTER_CRITICAL();
206 bits = event->value & bitMask;
211 USB_OSA_EXIT_CRITICAL();
212 return kStatus_USB_OSA_TimeOut;
219 USB_OSA_EXIT_CRITICAL();
220 return kStatus_USB_OSA_TimeOut;
229 event->value &= ~bits;
231 USB_OSA_EXIT_CRITICAL();
232 return kStatus_USB_OSA_Success;
234 return kStatus_USB_OSA_Error;
237 usb_osa_status_t USB_OsaEventCheck(usb_osa_event_handle handle, uint32_t bitMask, uint32_t *bitSet)
239 usb_osa_event_struct_t *event = (usb_osa_event_struct_t *)handle;
245 USB_OSA_ENTER_CRITICAL();
246 bits = event->value & bitMask;
250 USB_OSA_EXIT_CRITICAL();
251 return kStatus_USB_OSA_Error;
258 USB_OSA_EXIT_CRITICAL();
259 return kStatus_USB_OSA_Success;
261 return kStatus_USB_OSA_Error;
264 usb_osa_status_t USB_OsaEventClear(usb_osa_event_handle handle, uint32_t bitMask)
266 usb_osa_event_struct_t *event = (usb_osa_event_struct_t *)handle;
272 USB_OSA_ENTER_CRITICAL();
273 bits = event->value & bitMask;
274 event->value &= ~bits;
275 USB_OSA_EXIT_CRITICAL();
276 return kStatus_USB_OSA_Success;
278 return kStatus_USB_OSA_Error;
281 usb_osa_status_t USB_OsaSemCreate(usb_osa_sem_handle *handle, uint32_t count)
283 usb_osa_sem_struct_t *sem = NULL;
288 return kStatus_USB_OSA_Error;
291 USB_OSA_ENTER_CRITICAL();
292 for (uint32_t i = 0; i < USB_OSA_BM_SEM_COUNT; i++)
294 if (0 == s_UsbBmSemStruct[i].isUsed)
296 sem = &s_UsbBmSemStruct[i];
302 USB_OSA_EXIT_CRITICAL();
303 return kStatus_USB_OSA_Error;
309 USB_OSA_EXIT_CRITICAL();
310 return kStatus_USB_OSA_Success;
313 usb_osa_status_t USB_OsaSemDestroy(usb_osa_sem_handle handle)
315 usb_osa_sem_struct_t *sem = (usb_osa_sem_struct_t *)handle;
320 USB_OSA_ENTER_CRITICAL();
322 USB_OSA_EXIT_CRITICAL();
323 return kStatus_USB_OSA_Success;
325 return kStatus_USB_OSA_Error;
328 usb_osa_status_t USB_OsaSemPost(usb_osa_sem_handle handle)
330 usb_osa_sem_struct_t *sem = (usb_osa_sem_struct_t *)handle;
335 return kStatus_USB_OSA_Error;
338 USB_OSA_ENTER_CRITICAL();
340 USB_OSA_EXIT_CRITICAL();
341 return kStatus_USB_OSA_Success;
344 usb_osa_status_t USB_OsaSemWait(usb_osa_sem_handle handle, uint32_t timeout)
346 usb_osa_sem_struct_t *sem = (usb_osa_sem_struct_t *)handle;
351 return kStatus_USB_OSA_Error;
354 USB_OSA_ENTER_CRITICAL();
361 USB_OSA_EXIT_CRITICAL();
362 return kStatus_USB_OSA_TimeOut;
364 USB_OSA_EXIT_CRITICAL();
365 return kStatus_USB_OSA_Success;
368 usb_osa_status_t USB_OsaMutexCreate(usb_osa_mutex_handle *handle)
372 return kStatus_USB_OSA_Error;
374 *handle = (usb_osa_mutex_handle)0xFFFF0000U;
375 return kStatus_USB_OSA_Success;
378 usb_osa_status_t USB_OsaMutexDestroy(usb_osa_mutex_handle handle)
380 return kStatus_USB_OSA_Success;
382 usb_osa_status_t USB_OsaMutexLock(usb_osa_mutex_handle handle)
384 return kStatus_USB_OSA_Success;
386 usb_osa_status_t USB_OsaMutexUnlock(usb_osa_mutex_handle handle)
388 return kStatus_USB_OSA_Success;
391 usb_osa_status_t USB_OsaMsgqCreate(usb_osa_msgq_handle *handle, uint32_t count, uint32_t size)
393 usb_osa_msgq_struct_t *msgq = NULL;
398 return kStatus_USB_OSA_Error;
400 USB_OSA_ENTER_CRITICAL();
402 for (uint32_t i = 0; i < USB_OSA_BM_MSGQ_COUNT; i++)
404 if (0 == s_UsbBmMsgqStruct[i].isUsed)
406 msgq = &s_UsbBmMsgqStruct[i];
410 if ((NULL == msgq) || (count > USB_OSA_BM_MSG_COUNT) || (size > USB_OSA_BM_MSG_SIZE))
412 USB_OSA_EXIT_CRITICAL();
413 return kStatus_USB_OSA_Error;
416 msgq->msgSize = size;
422 USB_OSA_EXIT_CRITICAL();
423 return kStatus_USB_OSA_Success;
426 usb_osa_status_t USB_OsaMsgqDestroy(usb_osa_msgq_handle handle)
428 usb_osa_msgq_struct_t *msgq = (usb_osa_msgq_struct_t *)handle;
433 return kStatus_USB_OSA_Error;
435 USB_OSA_ENTER_CRITICAL();
437 USB_OSA_EXIT_CRITICAL();
438 return kStatus_USB_OSA_Success;
441 usb_osa_status_t USB_OsaMsgqSend(usb_osa_msgq_handle handle, void *msg)
443 usb_osa_msgq_struct_t *msgq = (usb_osa_msgq_struct_t *)handle;
444 usb_osa_msg_struct_t *msgEntity;
452 return kStatus_USB_OSA_Error;
454 USB_OSA_ENTER_CRITICAL();
455 if (msgq->msgCount >= msgq->count)
457 USB_OSA_EXIT_CRITICAL();
458 return kStatus_USB_OSA_Error;
461 msgEntity = &msgq->msgs[msgq->index];
462 p = (uint32_t *)&msgEntity->msg[0];
465 for (count = 0U; count < msgq->msgSize; count++)
470 if (0U == msgq->msgCount)
472 msgq->current = msgq->index;
477 msgq->index = msgq->index % msgq->count;
479 USB_OSA_EXIT_CRITICAL();
481 return kStatus_USB_OSA_Success;
484 usb_osa_status_t USB_OsaMsgqRecv(usb_osa_msgq_handle handle, void *msg, uint32_t timeout)
486 usb_osa_msgq_struct_t *msgq = (usb_osa_msgq_struct_t *)handle;
487 usb_osa_msg_struct_t *msgEntity;
495 return kStatus_USB_OSA_Error;
497 USB_OSA_ENTER_CRITICAL();
498 if (msgq->msgCount < 1U)
500 USB_OSA_EXIT_CRITICAL();
501 return kStatus_USB_OSA_TimeOut;
504 msgEntity = &msgq->msgs[msgq->current];
505 q = (uint32_t *)&msgEntity->msg[0];
508 for (count = 0U; count < msgq->msgSize; count++)
515 msgq->current = msgq->current % msgq->count;
517 USB_OSA_EXIT_CRITICAL();
519 return kStatus_USB_OSA_Success;
522 usb_osa_status_t USB_OsaMsgqCheck(usb_osa_msgq_handle handle, void *msg)
524 usb_osa_msgq_struct_t *msgq = (usb_osa_msgq_struct_t *)handle;
530 return kStatus_USB_OSA_Error;
533 USB_OSA_ENTER_CRITICAL();
534 msgCount = msgq->msgCount;
535 USB_OSA_EXIT_CRITICAL();
539 if (kStatus_USB_OSA_Success == USB_OsaMsgqRecv(msgq, msg, 0U))
541 return kStatus_USB_OSA_Success;
545 return kStatus_USB_OSA_Error;