tinyusb
osal.h
Go to the documentation of this file.
1 /**************************************************************************/
37 /**************************************************************************/
38 
39 #ifndef _TUSB_OSAL_H_
40 #define _TUSB_OSAL_H_
41 
42 #ifdef __cplusplus
43  extern "C" {
44 #endif
45 
52 #define TUSB_OS_NONE 1
53 #define TUSB_OS_FREERTOS 2
54 #define TUSB_OS_CMSIS_RTX 3
55 #define TUSB_OS_UCOS3 4
56 
58 #include "tusb_option.h"
59 
60 #ifndef _TEST_
61 
62 #if TUSB_CFG_OS == TUSB_OS_NONE
63  #include "osal_none.h"
64 
65 #else
66  #if TUSB_CFG_OS == TUSB_OS_FREERTOS
67  #include "osal_freeRTOS.h"
68  #elif TUSB_CFG_OS == TUSB_OS_CMSIS_RTX
69  #include "osal_cmsis_rtx.h"
70  #else
71  #error TUSB_CFG_OS is not defined or OS is not supported yet
72  #endif
73 
74  #define OSAL_VAR
75  #define OSAL_TASK_LOOP_BEGIN \
76  while(1) {
77 
78  #define OSAL_TASK_LOOP_END \
79  }
80 
81  //------------- Sub Task -------------//
82  #define OSAL_SUBTASK_BEGIN // TODO refractor move
83  #define OSAL_SUBTASK_END \
84  return TUSB_ERROR_NONE;
85 
86  #define SUBTASK_EXIT(error) return error;
87 
88  #define OSAL_SUBTASK_INVOKED_AND_WAIT(subtask, status) \
89  status = subtask
90 
91  //------------- Sub Task Assert -------------//
92  #define SUBTASK_ASSERT_STATUS(sts) ASSERT_STATUS(sts)
93  #define SUBTASK_ASSERT(condition) ASSERT(condition, TUSB_ERROR_OSAL_TASK_FAILED)
94 
95  #define _SUBTASK_ASSERT_ERROR_HANDLER(error, func_call)\
96  func_call; return error
97 
98  #define SUBTASK_ASSERT_STATUS_WITH_HANDLER(sts, func_call) \
99  ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, func_call, tusb_error_t status = (tusb_error_t)(sts),\
100  TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status])
101 
102  #define SUBTASK_ASSERT_WITH_HANDLER(condition, func_call) \
103  ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, func_call, ,\
104  condition, TUSB_ERROR_OSAL_TASK_FAILED, "%s", "evaluated to false")
105 #endif
106 
107 //------------- OSAL API for cmock -------------//
108 #else
109 
110 #define OSAL_VAR
111 #include "osal_common.h"
112 
113 //------------- Tick -------------//
114 uint32_t osal_tick_get(void);
115 
116 //--------------------------------------------------------------------+
117 // TASK API
118 //--------------------------------------------------------------------+
119 typedef uint32_t osal_task_t;
120 tusb_error_t osal_task_create(osal_task_t *task);
121 
122 #define OSAL_TASK_DEF(code, stack_depth, prio) \
123  osal_task_t variable
124 
125 #define OSAL_TASK_REF(name) (&name)
126 
127 #define OSAL_TASK_FUNCTION(task_name) \
128  void task_name
129 
130 void osal_task_delay(uint32_t msec);
131 
132 #define OSAL_TASK_LOOP_BEGIN
133 #define OSAL_TASK_LOOP_END
134 
135 #define SUBTASK_EXIT(error) return error;
136 
137 //------------- Sub Task -------------//
138 #define OSAL_SUBTASK_INVOKED_AND_WAIT(subtask, status) \
139  status = subtask
140 
141 #define OSAL_SUBTASK_BEGIN
142 #define OSAL_SUBTASK_END \
143  return TUSB_ERROR_NONE;
144 
145 //------------- Sub Task Assert -------------//
146 #define _SUBTASK_ASSERT_ERROR_HANDLER(error, func_call)\
147  func_call; return error
148 
149 #define SUBTASK_ASSERT_STATUS(sts) ASSERT_STATUS(sts)
150 
151 #define SUBTASK_ASSERT_STATUS_WITH_HANDLER(sts, func_call) \
152  ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, func_call, tusb_error_t status = (tusb_error_t)(sts),\
153  TUSB_ERROR_NONE == status, status, "%s", TUSB_ErrorStr[status])
154 
155 #define SUBTASK_ASSERT(condition) ASSERT(condition, TUSB_ERROR_OSAL_TASK_FAILED)
156 
157 #define SUBTASK_ASSERT_WITH_HANDLER(condition, func_call) \
158  ASSERT_DEFINE_WITH_HANDLER(_SUBTASK_ASSERT_ERROR_HANDLER, func_call, ,\
159  condition, TUSB_ERROR_OSAL_TASK_FAILED, "%s", "evaluated to false")
160 
161 
162 //--------------------------------------------------------------------+
163 // Semaphore API
164 //--------------------------------------------------------------------+
165 typedef volatile uint8_t osal_semaphore_t;
166 typedef osal_semaphore_t * osal_semaphore_handle_t;
167 
168 #define OSAL_SEM_DEF(name)\
169  osal_semaphore_t name
170 
171 #define OSAL_SEM_REF(name)\
172  &name
173 
174 osal_semaphore_handle_t osal_semaphore_create(osal_semaphore_t * p_sem);
175 void osal_semaphore_wait(osal_semaphore_handle_t sem_hdl, uint32_t msec, tusb_error_t *p_error);
176 tusb_error_t osal_semaphore_post(osal_semaphore_handle_t sem_hdl);
177 void osal_semaphore_reset(osal_semaphore_handle_t sem_hdl);
178 
179 //--------------------------------------------------------------------+
180 // MUTEX API (priority inheritance)
181 //--------------------------------------------------------------------+
182 #define OSAL_MUTEX_DEF(name)\
183  osal_mutex_t name
184 
185 #define OSAL_MUTEX_REF(name)\
186  &name
187 
188 typedef osal_semaphore_t osal_mutex_t;
189 typedef osal_semaphore_handle_t osal_mutex_handle_t;
190 
191 osal_mutex_handle_t osal_mutex_create(osal_mutex_t * p_mutex);
192 void osal_mutex_wait(osal_mutex_handle_t mutex_hdl, uint32_t msec, tusb_error_t *p_error);
193 tusb_error_t osal_mutex_release(osal_mutex_handle_t mutex_hdl);
194 void osal_mutex_reset(osal_mutex_handle_t mutex_hdl);
195 //--------------------------------------------------------------------+
196 // QUEUE API
197 //--------------------------------------------------------------------+
198 typedef struct{
199  uint32_t * const buffer ;
200  uint8_t const depth ;
201  volatile uint8_t count ;
202  volatile uint8_t wr_idx ;
203  volatile uint8_t rd_idx ;
204 } osal_queue_t;
205 
207 
208 #define OSAL_QUEUE_DEF(name, queue_depth, type) \
209  osal_queue_t name
210 
211 #define OSAL_QUEUE_REF(name) (&name)
212 
213 osal_queue_handle_t osal_queue_create (osal_queue_t *p_queue);
214 void osal_queue_receive (osal_queue_handle_t const queue_hdl, void *p_data, uint32_t msec, tusb_error_t *p_error);
215 tusb_error_t osal_queue_send (osal_queue_handle_t const queue_hdl, const void * data);
216 void osal_queue_flush(osal_queue_handle_t const queue_hdl);
217 
218 //--------------------------------------------------------------------+
219 // TICK API
220 //--------------------------------------------------------------------+
221 uint32_t osal_tick_get(void);
222 #endif
223 
224 #ifdef __cplusplus
225  }
226 #endif
227 
230 #endif /* _TUSB_OSAL_H_ */
Definition: osal_cmsis_rtx.h:119
Definition: osal_freeRTOS.h:160
tusb_error_t
Error Code returned.
Definition: tusb_errors.h:100
Definition: osal_freeRTOS.h:69
Definition: osal_cmsis_rtx.h:82