16 _mqx_uint
_lwmsgq_deinit(pointer location
);
26 typedef void * HANDLE
;
29 #define __compiler_offsetof(a,b) __builtin_offsetof(a,b)
30 #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
32 //-#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
33 #define container_of(ptr, type, member) ({ \
34 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
35 (type *)( (char *)__mptr - offsetof(type,member) );})
38 #define INT_DISABLE() _int_disable()
39 #define INT_ENABLE() _int_enable()
41 /* building ***************************************************/
44 #if MQX_USE_UNCACHED_MEM && PSP_HAS_DATA_CACHE
45 #define os_mem_alloc_uncached(n) _mem_alloc_system_uncached(n)
46 #define os_mem_alloc_uncached_zero(n) _mem_alloc_system_zero_uncached(n)
47 #define os_mem_alloc_uncached_align(n,a) _mem_alloc_align_uncached(n,a)
49 #define os_mem_alloc_uncached(n) _mem_alloc_system(n)
50 #define os_mem_alloc_uncached_zero(n) _mem_alloc_system_zero(n)
51 #define os_mem_alloc_uncached_align(n,a) _mem_alloc_align(n,a)
52 #endif /* PSP_HAS_DATA_CACHE */
54 #define os_mem_alloc(n) _mem_alloc_system(n)
55 #define os_mem_alloc_zero(n) _mem_alloc_system_zero(n)
56 #define os_mem_free(ptr) _mem_free(ptr)
57 #define os_mem_zero(ptr,n) _mem_zero(ptr,n)
58 #define os_mem_copy(src,dst,n) _mem_copy(src,dst,n)
60 #define os_memcpy memcpy
61 #define os_memset memset
63 //#define os_malloc _mem_alloc_system
64 static inline void * os_malloc(unsigned int y
)
66 int align_len
= (y
& 3) ? y
+ (4 - (y
& 3)) : y
;
68 printf("os_malloc size 0\n");
69 // ??? dbgPrintf("SDP: Failed to allocate flattened result array (%ub)\n", sg_length(resultSoFar));
70 //align_len = 4; // align_len
73 return /*_mem_alloc_system*/_mem_alloc_system_zero(align_len
);
76 //#define os_free _mem_free
77 static inline void os_free(void * x
)
80 printf("os_free NULL pointer\n");
85 #define os_zalloc os_mem_alloc_zero
86 static inline void * os_realloc(void * x
, unsigned int y
)
88 int align_len
= (y
& 3) ? y
+ (4 - (y
& 3)) : y
;
90 printf("os_realloc NULL pointer\n");
93 void * addr
= _mem_alloc_system(align_len
);
106 #define realloc(x,y) \
114 #define __stringify_1(x...) #x
115 #define __stringify(x...) __stringify_1(x)
117 #define halt() do { for(;;); } while (0)
118 #define ASSERT(x) do { if (!(x)) { /*dbgPrintf*/printf("%s:%d ASSERT failed: %s\n", __FILE__, __LINE__, #x); halt(); } } while (0)
120 #if defined(OS_DEBUG)
121 #define pr_debug(fmt, ...) \
122 printf(pr_fmt(fmt), ##__VA_ARGS__)
123 #define pr_warning(fmt, ...) \
124 printf(pr_fmt(fmt), ##__VA_ARGS__)
125 #define pr_warn pr_warning
126 #define os_printf(fmt, ...) \
127 printf(pr_fmt(fmt), ##__VA_ARGS__)
129 #define pr_debug(fmt, ...)
130 #define pr_warning(fmt, ...)
132 #define os_printf(fmt, ...)
136 #define pr_fmt(fmt) fmt
139 #define pr_err(fmt, ...) \
140 printf(pr_fmt(fmt), ##__VA_ARGS__)
142 #define USHRT_MAX ((u16)(~0U))
143 #define SHRT_MAX ((s16)(USHRT_MAX>>1))
144 #define SHRT_MIN ((s16)(-SHRT_MAX - 1))
145 #define INT_MAX ((int)(~0U>>1))
146 #define INT_MIN (-INT_MAX - 1)
147 #define UINT_MAX (~0U)
148 #define LONG_MAX ((long)(~0UL>>1))
149 #define LONG_MIN (-LONG_MAX - 1)
150 #define ULONG_MAX (~0UL)
151 #define LLONG_MAX ((long long)(~0ULL>>1))
152 #define LLONG_MIN (-LLONG_MAX - 1)
153 #define ULLONG_MAX (~0ULL)
156 #define min(x, y) (x < y ? x : y)
159 #define max(x, y) (x > y ? x : y)
162 #define ULONG_MAX (~0UL)
163 #define LONG_MAX ((long)(~0UL>>1))
165 /* building ***************************************************/
167 static inline uint8_t ffs(unsigned int val
)
170 for(i
= 0; i
< 32; i
++) {
183 test_bit(unsigned int bit
, volatile unsigned long *p
)
185 //unsigned long flags;
187 unsigned long mask
= 1UL << (bit
& 31);
193 return (res
& mask
) != 0;
196 static inline void set_bit(unsigned int bit
, volatile unsigned long *p
)
198 //unsigned long flags;
199 unsigned long mask
= 1UL << (bit
& 31);
206 static inline void clear_bit(unsigned int bit
, volatile unsigned long *p
)
208 //unsigned long flags;
209 unsigned long mask
= 1UL << (bit
& 31);
216 static inline void change_bit(unsigned int bit
, volatile unsigned long *p
)
218 //unsigned long flags;
219 unsigned long mask
= 1UL << (bit
& 31);
227 test_and_set_bit(unsigned int bit
, volatile unsigned long *p
)
229 //unsigned long flags;
231 unsigned long mask
= 1UL << (bit
& 31);
238 return (res
& mask
) != 0;
242 test_and_clear_bit(unsigned int bit
, volatile unsigned long *p
)
244 //unsigned long flags;
246 unsigned long mask
= 1UL << (bit
& 31);
253 return (res
& mask
) != 0;
257 test_and_change_bit(unsigned int bit
, volatile unsigned long *p
)
259 //unsigned long flags;
261 unsigned long mask
= 1UL << (bit
& 31);
268 return (res
& mask
) != 0;
272 //#define FIXED_USE_DYNAMIC
273 //#define DEBUG_FIXED_MEM
275 #define MAX_FIXED_MEM_BLOCKS 32 /* only support 32 blocks */
277 typedef struct _fixed_mem
{
278 unsigned short mem_blocks
;
279 unsigned short mem_bsize
; /* size of blocks for fixed mem , little than 64K */
281 #ifdef DEBUG_FIXED_MEM
282 unsigned int allocated_blocks
;
285 unsigned long mem_map
[MAX_FIXED_MEM_BLOCKS
/sizeof(unsigned long)];
286 unsigned char mem_ptr
[1];
290 typedef void * fixed_mem_id_t
;
293 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) /* + __must_be_array(arr) */)
297 static inline fixed_mem_id_t
os_alloc_fixed_mem(unsigned int blocksize
, unsigned int count
)
299 fixed_mem_id_t fmem_id
;
300 int align_len
= blocksize
+ (4 - (blocksize
& 3));
302 ASSERT((count
> 0) && (count
<= MAX_FIXED_MEM_BLOCKS
));
304 #ifdef FIXED_USE_DYNAMIC
305 fmem_id
= (fixed_mem_id_t
)os_mem_alloc_zero(offsetof(fixed_mem_t
, mem_ptr
) + 0);
307 fmem_id
= (fixed_mem_id_t
)os_mem_alloc_zero(offsetof(fixed_mem_t
, mem_ptr
) + (align_len
* count
));
309 ((fixed_mem_t
*)fmem_id
)->mem_blocks
= count
;
310 ((fixed_mem_t
*)fmem_id
)->mem_bsize
= align_len
;
312 #ifdef DEBUG_FIXED_MEM
313 ((fixed_mem_t
*)fmem_id
)->allocated_blocks
= 0;
319 static inline void * os_get_fixed(fixed_mem_id_t fid
)
321 void * result_fixed
= NULL
;
322 #ifdef FIXED_USE_DYNAMIC
323 result_fixed
== os_mem_alloc( ((fixed_mem_t
*)fid
)->mem_bsize
);
327 j
= ((fixed_mem_t
*)fid
)->mem_blocks
;
328 for(i
= 0; i
< j
; i
++) {
329 if(!test_bit(i
, ((fixed_mem_t
*)fid
)->mem_map
)) {
334 if(i
< ((fixed_mem_t
*)fid
)->mem_blocks
) {
335 set_bit(i
, ((fixed_mem_t
*)fid
)->mem_map
);
336 result_fixed
= (void *) ((int) (((fixed_mem_t
*)fid
)->mem_ptr
) + i
* ((fixed_mem_t
*)fid
)->mem_bsize
);
340 // if i >= mem_blocks then realloc the fixmem and alloc it.
344 #ifdef DEBUG_FIXED_MEM
345 printf("get fixed 0x%x, num %d, %d\n",result_fixed
, ++(((fixed_mem_t
*)fid
)->allocated_blocks
),((fixed_mem_t
*)fid
)->mem_bsize
);
351 static inline void os_put_fixed (void * data
, fixed_mem_id_t fid
)
353 #ifdef DEBUG_FIXED_MEM
354 printf("put fixed 0x%x num %d\n",(int)data
,--(((fixed_mem_t
*)fid
)->allocated_blocks
));
356 #ifdef FIXED_USE_DYNAMIC
361 i
= ((int) data
- (int) (((fixed_mem_t
*)fid
)->mem_ptr
)) / ((fixed_mem_t
*)fid
)->mem_bsize
;
363 ASSERT ((i
>= 0) && (i
< MAX_FIXED_MEM_BLOCKS
) && test_bit(i
, ((fixed_mem_t
*)fid
)->mem_map
));
364 clear_bit(i
, ((fixed_mem_t
*)fid
)->mem_map
);
368 static inline void os_free_fixed_mem (fixed_mem_id_t fid
)
375 /* os ,task, sem,mutex, event */
376 /**HEADER********************************************************************
378 * Copyright (c) 2013 Freescale Semiconductor;
379 * All Rights Reserved
382 ***************************************************************************
384 * THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR
385 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
386 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
387 * IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
388 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
389 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
390 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
391 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
392 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
393 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
394 * THE POSSIBILITY OF SUCH DAMAGE.
396 **************************************************************************
398 * $FileName: osadapter_mqx.c$
404 * @brief The file includes the implementation of MQX of OS adapter.
405 *****************************************************************************/
406 typedef void (* TASK_START
)( void *);
407 typedef void (* OSA_INT_ISR_FPTR
)(void *);
408 typedef void* OS_Event_handle
;
409 typedef void* OS_MsgQ_handle
;
410 typedef void* OS_GPIO_handle
;
411 typedef void* OS_Mutex_handle
;
412 typedef void* OS_Sem_handle
;
415 #define OS_TASK_OK (0)
416 #define OS_TASK_ERROR (-1)
417 #define OS_EVENT_OK (0)
418 #define OS_EVENT_ERROR (-1)
419 #define OS_EVENT_TIMEOUT (-2)
420 #define OS_MSGQ_OK (0)
421 #define OS_MSGQ_ERROR (-1)
422 #define OS_GPIO_OK (0)
423 #define OS_GPIO_ERROR (-1)
424 #define OS_MUTEX_OK (0)
425 #define OS_MUTEX_ERROR (-1)
426 #define OS_SEM_OK (0)
427 #define OS_SEM_ERROR (-1)
428 #define OS_SEM_TIMEOUT (-2)
430 /* Block the reading task if msgq is empty */
431 #define OS_MSGQ_RECEIVE_BLOCK_ON_EMPTY (0x04)
432 static inline uint32_t OS_Task_create(TASK_START pstart
, void* param
, uint32_t pri
, uint32_t stack_size
, char* task_name
, void* opt
)
435 TASK_TEMPLATE_STRUCT task_template
;
437 task_template
.TASK_TEMPLATE_INDEX
= 0;
438 task_template
.TASK_ADDRESS
= (TASK_FPTR
)pstart
;
439 task_template
.TASK_STACKSIZE
= stack_size
;
440 task_template
.TASK_PRIORITY
= pri
;
441 task_template
.TASK_NAME
= task_name
;
444 task_template
.TASK_ATTRIBUTES
= *((uint32_t*)opt
);
448 task_template
.TASK_ATTRIBUTES
= 0;
451 task_template
.CREATION_PARAMETER
= (uint32_t)param
;
452 task_template
.DEFAULT_TIME_SLICE
= 0;
454 task_id
= _task_create_blocked(0, 0, (uint32_t)&task_template
);
456 if (task_id
== MQX_NULL_TASK_ID
) {
457 return (uint32_t)OS_TASK_ERROR
;
460 _task_ready(_task_get_td(task_id
));
461 return (uint32_t)task_id
;
464 static inline uint32_t OS_Task_delete(uint32_t task_id
)
467 ret
= _task_destroy((_task_id
)task_id
);
469 return (uint32_t)OS_TASK_ERROR
;
471 return (uint32_t)OS_TASK_OK
;
474 static inline uint32_t OS_Task_suspend(uint32_t task_id
)
478 return (uint32_t)OS_TASK_OK
;
481 static inline uint32_t OS_Task_resume(uint32_t task_id
)
483 _task_ready(_task_get_td(task_id
));
484 return (uint32_t)OS_TASK_OK
;
487 static inline void OS_Task_yield()
490 //_time_delay_ticks(2);
494 static inline OS_Event_handle
OS_Event_create(uint32_t flag
)
496 LWEVENT_STRUCT
*event
;
497 event
= (LWEVENT_STRUCT
*)_mem_alloc_system_zero(sizeof(LWEVENT_STRUCT
));
503 if (_lwevent_create(event
, flag
) != MQX_OK
)
505 _mem_free((void*)event
);
508 return (OS_Event_handle
)event
;
511 static inline uint32_t OS_Event_destroy(OS_Event_handle handle
)
513 LWEVENT_STRUCT
*event
= (LWEVENT_STRUCT
*)handle
;
514 if(_lwevent_destroy(event
) != MQX_OK
)
516 _mem_free((void*)event
);
517 return (uint32_t)OS_EVENT_ERROR
;
519 _mem_free((void*)event
);
523 static inline uint32_t OS_Event_check_bit(OS_Event_handle handle
, uint32_t bitmask
)
525 LWEVENT_STRUCT
*event
= (LWEVENT_STRUCT
*)handle
;
526 if((event
->VALUE
& bitmask
) != 0)
532 static inline uint32_t OS_Event_clear(OS_Event_handle handle
, uint32_t bitmask
)
534 LWEVENT_STRUCT
*event
= (LWEVENT_STRUCT
*)handle
;
535 if(_lwevent_clear(event
, bitmask
) != MQX_OK
)
537 return (uint32_t)OS_EVENT_ERROR
;
539 return (uint32_t)OS_EVENT_OK
;
542 static inline uint32_t OS_Event_set(OS_Event_handle handle
, uint32_t bitmask
)
544 LWEVENT_STRUCT
*event
= (LWEVENT_STRUCT
*)handle
;
545 if(_lwevent_set(event
, bitmask
) != MQX_OK
)
547 return (uint32_t)OS_EVENT_ERROR
;
549 return (uint32_t)OS_EVENT_OK
;
552 static inline uint32_t OS_Event_wait(OS_Event_handle handle
, uint32_t bitmask
, uint32_t flag
, uint32_t timeout
)
554 LWEVENT_STRUCT
*event
= (LWEVENT_STRUCT
*)handle
;
557 ret
= _lwevent_wait_ticks(event
, bitmask
, flag
, timeout
* _time_get_ticks_per_sec() / 1000);
558 //printf("os 0x%x\n", ret);
559 if(LWEVENT_WAIT_TIMEOUT
== ret
)
561 return (uint32_t)OS_EVENT_TIMEOUT
;
563 else if(MQX_OK
== ret
)
565 return (uint32_t)OS_EVENT_OK
;
567 return (uint32_t)OS_EVENT_ERROR
;
570 static inline uint32_t OS_Event_status(OS_Event_handle handle
)
572 LWEVENT_STRUCT
*event
= (LWEVENT_STRUCT
*)handle
;
576 static inline OS_MsgQ_handle
OS_MsgQ_create(uint32_t max_msg_number
, uint32_t msg_size
)
579 uint32_t size
= sizeof(LWMSGQ_STRUCT
) + max_msg_number
* msg_size
* 4;
581 msgq
= _mem_alloc_system_zero(size
);
587 if (_lwmsgq_init(msgq
, max_msg_number
, msg_size
) != MQX_OK
)
593 return (OS_MsgQ_handle
)msgq
;
596 static inline uint32_t OS_MsgQ_send(OS_MsgQ_handle msgq
, void* msg
, uint32_t flag
)
598 if (MQX_OK
!= _lwmsgq_send(msgq
, (uint32_t *) msg
, flag
))
600 return (uint32_t)OS_MSGQ_ERROR
;
602 return (uint32_t)OS_MSGQ_OK
;
605 static inline uint32_t OS_MsgQ_recv(OS_MsgQ_handle msgq
, void* msg
, uint32_t flag
, uint32_t timeout
)
607 if (MQX_OK
!= _lwmsgq_receive(msgq
, (uint32_t *) msg
, flag
, timeout
, NULL
))
609 return (uint32_t)OS_MSGQ_ERROR
;
611 return (uint32_t)OS_MSGQ_OK
;
614 static inline uint32_t OS_MsgQ_Is_Empty(OS_MsgQ_handle msgq
, void* msg
)
617 ret
= LWMSGQ_IS_EMPTY(msgq
);
620 if (MQX_OK
!= _lwmsgq_receive(msgq
, (uint32_t *) msg
, OS_MSGQ_RECEIVE_BLOCK_ON_EMPTY
, 1, NULL
))
622 return (uint32_t)OS_MSGQ_ERROR
;
628 static inline uint32_t OS_MsgQ_destroy(OS_MsgQ_handle msgq
)
630 _lwmsgq_deinit(msgq
);
632 return (uint32_t)OS_MSGQ_OK
;
635 static inline OS_Mutex_handle
OS_Mutex_create()
637 MUTEX_STRUCT_PTR mutex
= NULL
;
638 mutex
= _mem_alloc_system_zero(sizeof(MUTEX_STRUCT
));
643 if (_mutex_init(mutex
, NULL
) != MQX_OK
)
648 return (OS_Mutex_handle
)mutex
;
651 static inline uint32_t OS_Mutex_lock(OS_Mutex_handle mutex
)
653 if (_mutex_lock((MUTEX_STRUCT_PTR
)mutex
) != MQX_OK
)
654 return (uint32_t)OS_MUTEX_ERROR
;
656 return (uint32_t)OS_MUTEX_OK
;
659 static inline uint32_t OS_Mutex_unlock(OS_Mutex_handle mutex
)
661 if (_mutex_unlock((MUTEX_STRUCT_PTR
)mutex
) != MQX_OK
)
662 return (uint32_t)OS_MUTEX_ERROR
;
664 return (uint32_t)OS_MUTEX_OK
;
667 static inline uint32_t OS_Mutex_destroy(OS_Mutex_handle mutex
)
669 _mutex_destroy((MUTEX_STRUCT_PTR
)mutex
);
674 static inline OS_Sem_handle
OS_Sem_create(uint32_t initial_number
)
676 LWSEM_STRUCT_PTR sem
= NULL
;
677 sem
= (LWSEM_STRUCT_PTR
)_mem_alloc_system_zero(sizeof(LWSEM_STRUCT
));
682 if (_lwsem_create(sem
, initial_number
) != MQX_OK
)
687 return (OS_Sem_handle
)sem
;
690 static inline uint32_t OS_Sem_wait(OS_Sem_handle sem
, uint32_t timeout
)
692 uint32_t result
= _lwsem_wait_ticks((LWSEM_STRUCT_PTR
)sem
, timeout
);
693 if (result
== MQX_LWSEM_WAIT_TIMEOUT
)
695 return (uint32_t)OS_SEM_TIMEOUT
;
697 else if (result
== MQX_OK
)
699 return (uint32_t)OS_SEM_OK
;
703 return (uint32_t)OS_SEM_ERROR
;
707 static inline uint32_t OS_Sem_post(OS_Sem_handle sem
)
709 uint32_t result
= _lwsem_post((LWSEM_STRUCT_PTR
)sem
);
710 if (result
== MQX_OK
)
712 return (uint32_t)OS_SEM_OK
;
716 return (uint32_t)OS_SEM_ERROR
;
720 static inline uint32_t OS_Sem_destroy(OS_Sem_handle sem
)
722 uint32_t result
= _lwsem_destroy((LWSEM_STRUCT_PTR
)sem
);
724 if (result
== MQX_OK
)
726 return (uint32_t)OS_SEM_OK
;
730 return (uint32_t)OS_SEM_ERROR
;
734 void OS_Time_delay(register uint32_t milliseconds
)
736 _time_delay(milliseconds
);
740 #endif /* OS_UTILS_H */