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.
39 * @addtogroup usb_os_abstraction
43 /*******************************************************************************
45 ******************************************************************************/
47 /*! @brief Define big endian */
48 #define USB_BIG_ENDIAN (0U)
49 /*! @brief Define little endian */
50 #define USB_LITTLE_ENDIAN (1U)
52 /*! @brief Define current endian */
53 #define ENDIANNESS USB_LITTLE_ENDIAN
55 /*! @brief Define USB OSA event handle */
56 typedef void *usb_osa_event_handle;
58 /*! @brief Define USB OSA semaphore handle */
59 typedef void *usb_osa_sem_handle;
61 /*! @brief Define USB OSA mutex handle */
62 typedef void *usb_osa_mutex_handle;
64 /*! @brief Define USB OSA message queue handle */
65 typedef void *usb_osa_msgq_handle;
67 /*! @brief USB OSA error code */
68 typedef enum _usb_osa_status
70 kStatus_USB_OSA_Success = 0x00U, /*!< Success */
71 kStatus_USB_OSA_Error, /*!< Failed */
72 kStatus_USB_OSA_TimeOut, /*!< Timeout occurs while waiting */
75 /*! @brief The event flags are cleared automatically or manually.*/
76 typedef enum _usb_osa_event_mode
78 kUSB_OsaEventManualClear = 0U, /*!< The flags of the event is cleared manually. */
79 kUSB_OsaEventAutoClear = 1U, /*!< The flags of the event is cleared automatically. */
80 } usb_osa_event_mode_t;
82 #include "usb_osa_bm.h"
84 /*******************************************************************************
86 ******************************************************************************/
88 #if defined(__cplusplus)
93 * @name USB OSA Memory Management
98 * @brief Reserves the requested amount of memory in bytes.
100 * The function is used to reserve the requested amount of memory in bytes and initializes it to 0.
102 * @param length Amount of bytes to reserve.
104 * @return Pointer to the reserved memory. NULL if memory can't be allocated.
106 void *USB_OsaMemoryAllocate(uint32_t length);
109 * @brief Frees the memory previously reserved.
111 * The function is used to free the memory block previously reserved.
113 * @param p Pointer to the start of the memory block previously reserved.
116 extern void USB_OsaMemoryFree(void *p);
121 * @name USB OSA Event
126 * @brief Creates an event object with all flags cleared.
128 * This function creates an event object and sets its clear mode. If the clear mode
129 * is kUSB_OsaEventAutoClear, when a task gets the event flags, these flags are
130 * cleared automatically. If the clear mode is kUSB_OsaEventManualClear, the flags must
131 * be cleared manually.
133 * @param handle It is an out parameter, which is used to return the pointer of the event object.
134 * @param flag The event is auto-clear or manual-clear. See the enumeration #usb_osa_event_mode_t.
136 * @return A USB OSA error code or kStatus_OSA_Success.
140 usb_osa_event_handle eventHandle;
141 usb_osa_status_t usbOsaStatus;
142 usbOsaStatus = USB_OsaEventCreate(&eventHandle, kUSB_OsaEventManualClear);
146 extern usb_osa_status_t USB_OsaEventCreate(usb_osa_event_handle *handle, uint32_t flag);
149 * @brief Destroys a created event object.
151 * @param handle Pointer to the event object.
153 * @return A USB OSA error code or kStatus_OSA_Success.
157 usb_osa_status_t usbOsaStatus;
159 usbOsaStatus = USB_OsaEventDestroy(eventHandle);
163 extern usb_osa_status_t USB_OsaEventDestroy(usb_osa_event_handle handle);
166 * @brief Sets an event flag.
168 * Sets specified flags for an event object.
170 * @param handle Pointer to the event object.
171 * @param bitMask Event flags to be set.
173 * @return A USB OSA error code or kStatus_OSA_Success.
177 usb_osa_status_t usbOsaStatus;
179 usbOsaStatus = USB_OsaEventSet(eventHandle, 0x01U);
183 extern usb_osa_status_t USB_OsaEventSet(usb_osa_event_handle handle, uint32_t bitMask);
186 * @brief Waits for an event flag.
188 * This function waits for a combination of flags to be set in an event object.
189 * An applications can wait for any/all bits to be set. This function can
190 * get the flags that wake up the waiting task.
192 * @param handle Pointer to the event object.
193 * @param bitMask Event flags to wait.
194 * @param flag Wait all flags or any flag to be set. 0U - wait any flag, others, wait all flags.
195 * @param timeout The maximum number of milliseconds to wait for the event.
196 * If the wait condition is not met, passing 0U
197 * waits indefinitely when the environment is an RTOS and returns the kStatus_OSA_Timeout
198 * immediately. Pass any value for the bare metal.
199 * @param bitSet Flags that wake up the waiting task are obtained by this parameter.
201 * @return An USB OSA error code or kStatus_OSA_Success.
205 usb_osa_status_t usbOsaStatus;
208 usbOsaStatus = USB_OsaEventWait(eventHandle, 0x01U, 0U, 0U, &bitSet);
212 extern usb_osa_status_t USB_OsaEventWait(
213 usb_osa_event_handle handle, uint32_t bitMask, uint32_t flag, uint32_t timeout, uint32_t *bitSet);
216 * @brief Checks an event flag.
218 * This function checks for a combination of flags to be set in an event object.
220 * @param handle Pointer to the event object.
221 * @param bitMask Event flags to check.
222 * @param bitSet Flags have been set.
224 * @return An USB OSA error code or kStatus_OSA_Success.
228 usb_osa_status_t usbOsaStatus;
231 usbOsaStatus = USB_OsaEventCheck(eventHandle, 0x01U, &bitSet);
235 extern usb_osa_status_t USB_OsaEventCheck(usb_osa_event_handle handle, uint32_t bitMask, uint32_t *bitSet);
238 * @brief Clears an event flag.
240 * This function clears flags of an event object.
242 * @param handle Pointer to the event object
243 * @param bitMask Event flags to be cleared.
245 * @return An USB OSA error code or kStatus_OSA_Success.
249 usb_osa_status_t usbOsaStatus;
251 usbOsaStatus = USB_OsaEventClear(eventHandle, 0x01U);
254 extern usb_osa_status_t USB_OsaEventClear(usb_osa_event_handle handle, uint32_t bitMask);
258 * @name USB OSA Semaphore
263 * @brief Creates a semaphore with a given value.
265 * This function creates a semaphore and sets the default count.
267 * @param handle It is an out parameter, which is used to return pointer of the semaphore object.
268 * @param count Initializes a value of the semaphore.
270 * @return An USB OSA error code or kStatus_OSA_Success.
274 usb_osa_sem_handle semHandle;
275 usb_osa_status_t usbOsaStatus;
276 usbOsaStatus = USB_OsaSemCreate(&semHandle, 1U);
280 extern usb_osa_status_t USB_OsaSemCreate(usb_osa_sem_handle *handle, uint32_t count);
283 * @brief Destroys a semaphore object.
285 * This function destroys a semaphore object.
287 * @param handle Pointer to the semaphore.
289 * @return An USB OSA error code or kStatus_OSA_Success.
293 usb_osa_sem_handle semHandle;
294 usb_osa_status_t usbOsaStatus;
296 usbOsaStatus = USB_OsaSemDestroy(semHandle);
300 extern usb_osa_status_t USB_OsaSemDestroy(usb_osa_sem_handle handle);
303 * @brief Posts a semaphore.
305 * This function wakes up a task waiting on the semaphore. If a task is not pending, increases the semaphore's
308 * @param handle Pointer to the semaphore.
310 * @return A USB OSA error code or kStatus_OSA_Success.
314 usb_osa_sem_handle semHandle;
315 usb_osa_status_t usbOsaStatus;
317 usbOsaStatus = USB_OsaSemPost(semHandle);
321 extern usb_osa_status_t USB_OsaSemPost(usb_osa_sem_handle handle);
324 * @brief Waits on a semaphore.
326 * This function checks the semaphore's value. If it is positive, it decreases the semaphore's value and return
329 * @param handle Pointer to the semaphore.
330 * @param timeout The maximum number of milliseconds to wait for the semaphore.
331 * If the wait condition is not met, passing 0U
332 * waits indefinitely when environment is RTOS. And return kStatus_OSA_Timeout
333 * immediately for bare metal no matter what value has been passed.
335 * @return A USB OSA error code or kStatus_OSA_Success.
339 usb_osa_sem_handle semHandle;
340 usb_osa_status_t usbOsaStatus;
342 usbOsaStatus = USB_OsaSemWait(semHandle, 0U);
346 extern usb_osa_status_t USB_OsaSemWait(usb_osa_sem_handle handle, uint32_t timeout);
350 * @name USB OSA Mutex
355 * @brief Creates a mutex.
357 * This function creates a mutex and sets it to an unlocked status.
359 * @param handle It is out parameter, which is used to return the pointer of the mutex object.
361 * @return A USB OSA error code or kStatus_OSA_Success.
365 usb_osa_mutex_handle mutexHandle;
366 usb_osa_status_t usbOsaStatus;
367 usbOsaStatus = USB_OsaMutexCreate(&mutexHandle);
371 extern usb_osa_status_t USB_OsaMutexCreate(usb_osa_mutex_handle *handle);
374 * @brief Destroys a mutex.
376 * This function destroys a mutex and sets it to an unlocked status.
378 * @param handle Pointer to the mutex.
380 * @return A USB OSA error code or kStatus_OSA_Success.
384 usb_osa_mutex_handle mutexHandle;
385 usb_osa_status_t usbOsaStatus;
387 usbOsaStatus = USB_OsaMutexDestroy(mutexHandle);
391 extern usb_osa_status_t USB_OsaMutexDestroy(usb_osa_mutex_handle handle);
394 * @brief Waits for a mutex and locks it.
396 * This function checks the mutex status. If it is unlocked, it locks it and returns the
397 * kStatus_OSA_Success. Otherwise, it waits forever to lock in RTOS and returns the
398 * kStatus_OSA_Success immediately for bare metal.
400 * @param handle Pointer to the mutex.
402 * @return A USB OSA error code or kStatus_OSA_Success.
406 usb_osa_mutex_handle mutexHandle;
407 usb_osa_status_t usbOsaStatus;
409 usbOsaStatus = USB_OsaMutexLock(mutexHandle);
413 extern usb_osa_status_t USB_OsaMutexLock(usb_osa_mutex_handle handle);
416 * @brief Unlocks a mutex.
418 * This function unlocks a mutex.
420 * @param handle Pointer to the mutex.
422 * @return A USB OSA error code or kStatus_OSA_Success.
426 usb_osa_mutex_handle mutexHandle;
427 usb_osa_status_t usbOsaStatus;
429 usbOsaStatus = USB_OsaMutexUnlock(mutexHandle);
433 extern usb_osa_status_t USB_OsaMutexUnlock(usb_osa_mutex_handle handle);
437 * @name USB OSA Message Queue
442 * @brief Creates a message queue.
444 * This function creates a message queue.
446 * @param handle It is an out parameter, which is used to return a pointer of the message queue object.
447 * @param count The count of elements in the queue.
448 * @param size Size of every elements in words.
450 * @return A USB OSA error code or kStatus_OSA_Success.
454 usb_osa_msgq_handle msgqHandle;
455 usb_osa_status_t usbOsaStatus;
456 usbOsaStatus = USB_OsaMsgqCreate(msgqHandle, 8U, 4U);
460 extern usb_osa_status_t USB_OsaMsgqCreate(usb_osa_msgq_handle *handle, uint32_t count, uint32_t size);
463 * @brief Destroys a message queue.
465 * This function destroys a message queue.
467 * @param handle Pointer to a message queue.
469 * @return A USB OSA error code or kStatus_OSA_Success.
473 usb_osa_msgq_handle msgqHandle;
474 usb_osa_status_t usbOsaStatus;
476 usbOsaStatus = USB_OsaMsgqDestroy(msgqHandle);
480 extern usb_osa_status_t USB_OsaMsgqDestroy(usb_osa_msgq_handle handle);
483 * @brief Sends a message.
485 * This function sends a message to the tail of the message queue.
487 * @param handle Pointer to a message queue.
488 * @param msg The pointer to a message to be put into the queue.
490 * @return A USB OSA error code or kStatus_OSA_Success.
494 usb_osa_msgq_handle msgqHandle;
495 message_struct_t message;
496 usb_osa_status_t usbOsaStatus;
498 usbOsaStatus = USB_OsaMsgqSend(msgqHandle, &message);
502 extern usb_osa_status_t USB_OsaMsgqSend(usb_osa_msgq_handle handle, void *msg);
505 * @brief Receives a message.
507 * This function receives a message from the head of the message queue.
509 * @param handle Pointer to a message queue.
510 * @param msg The pointer to save a received message.
511 * @param timeout The maximum number of milliseconds to wait for a message.
512 * If the wait condition is not met, passing 0U
513 * waits indefinitely when an environment is RTOS and returns the kStatus_OSA_Timeout
514 * immediately for bare metal.
516 * @return A USB OSA error code or kStatus_OSA_Success.
520 usb_osa_msgq_handle msgqHandle;
521 message_struct_t message;
522 usb_osa_status_t usbOsaStatus;
524 usbOsaStatus = USB_OsaMsgqRecv(msgqHandle, &message, 0U);
528 extern usb_osa_status_t USB_OsaMsgqRecv(usb_osa_msgq_handle handle, void *msg, uint32_t timeout);
531 * @brief Checks a message queue and receives a message if the queue is not empty.
533 * This function checks a message queue and receives a message if the queue is not empty.
535 * @param handle Pointer to a message queue.
536 * @param msg The pointer to save a received message.
538 * @return A USB OSA error code or kStatus_OSA_Success.
542 usb_osa_msgq_handle msgqHandle;
543 message_struct_t message;
544 usb_osa_status_t usbOsaStatus;
546 usbOsaStatus = USB_OsaMsgqCheck(msgqHandle, &message);
550 extern usb_osa_status_t USB_OsaMsgqCheck(usb_osa_msgq_handle handle, void *msg);
554 #if defined(__cplusplus)
560 #endif /* __USB_OSA_H__ */