OSAL.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /******************************************************************************
  29. Filename: OSAL.h
  30. Revised:
  31. Revision:
  32. Description: This API allows the software components in the Z-Stack to be
  33. written independently of the specifics of the operating system,
  34. kernel, or tasking environment (including control loops or
  35. connect-to-interrupt systems).
  36. ******************************************************************************/
  37. #ifndef OSAL_H
  38. #define OSAL_H
  39. #ifdef __cplusplus
  40. extern "C"
  41. {
  42. #endif
  43. /*********************************************************************
  44. * INCLUDES
  45. */
  46. //#include <limits.h>
  47. #include "comdef.h"
  48. #include "OSAL_Memory.h"
  49. #include "OSAL_Timers.h"
  50. /*********************************************************************
  51. * MACROS
  52. */
  53. #if ( UINT_MAX == 65535 ) /* 8-bit and 16-bit devices */
  54. #define osal_offsetof(type, member) ((uint16) &(((type *) 0)->member))
  55. #else /* 32-bit devices */
  56. #define osal_offsetof(type, member) ((uint32) &(((type *) 0)->member))
  57. #endif
  58. #define OSAL_MSG_NEXT(msg_ptr) ((osal_msg_hdr_t *) (msg_ptr) - 1)->next
  59. #define OSAL_MSG_Q_INIT(q_ptr) *(q_ptr) = NULL
  60. #define OSAL_MSG_Q_EMPTY(q_ptr) (*(q_ptr) == NULL)
  61. #define OSAL_MSG_Q_HEAD(q_ptr) (*(q_ptr))
  62. #define OSAL_MSG_LEN(msg_ptr) ((osal_msg_hdr_t *) (msg_ptr) - 1)->len
  63. #define OSAL_MSG_ID(msg_ptr) ((osal_msg_hdr_t *) (msg_ptr) - 1)->dest_id
  64. /*********************************************************************
  65. * CONSTANTS
  66. */
  67. /*** Interrupts ***/
  68. #define INTS_ALL 0xFF
  69. /*********************************************************************
  70. * TYPEDEFS
  71. */
  72. typedef struct
  73. {
  74. void *next;
  75. uint16 len;
  76. uint8 dest_id;
  77. } osal_msg_hdr_t;
  78. typedef struct
  79. {
  80. uint8 event;
  81. uint8 status;
  82. } osal_event_hdr_t;
  83. typedef void * osal_msg_q_t;
  84. /*********************************************************************
  85. * GLOBAL VARIABLES
  86. */
  87. /*********************************************************************
  88. * FUNCTIONS
  89. */
  90. /*** Message Management ***/
  91. /*
  92. * Task Message Allocation
  93. */
  94. extern uint8 * osal_msg_allocate(uint16 len );
  95. /*
  96. * Task Message Deallocation
  97. */
  98. extern uint8 osal_msg_deallocate( uint8 *msg_ptr );
  99. /*
  100. * Send a Task Message
  101. */
  102. extern uint8 osal_msg_send( uint8 destination_task, uint8 *msg_ptr );
  103. /*
  104. * Push a Task Message to head of queue
  105. */
  106. extern uint8 osal_msg_push_front( uint8 destination_task, uint8 *msg_ptr );
  107. /*
  108. * Receive a Task Message
  109. */
  110. extern uint8 *osal_msg_receive( uint8 task_id );
  111. /*
  112. * Find in place a matching Task Message / Event.
  113. */
  114. extern osal_event_hdr_t *osal_msg_find(uint8 task_id, uint8 event);
  115. /*
  116. * Enqueue a Task Message
  117. */
  118. extern void osal_msg_enqueue( osal_msg_q_t *q_ptr, void *msg_ptr );
  119. /*
  120. * Enqueue a Task Message Up to Max
  121. */
  122. extern uint8 osal_msg_enqueue_max( osal_msg_q_t *q_ptr, void *msg_ptr, uint8 max );
  123. /*
  124. * Dequeue a Task Message
  125. */
  126. extern void *osal_msg_dequeue( osal_msg_q_t *q_ptr );
  127. /*
  128. * Push a Task Message to head of queue
  129. */
  130. extern void osal_msg_push( osal_msg_q_t *q_ptr, void *msg_ptr );
  131. /*
  132. * Extract and remove a Task Message from queue
  133. */
  134. extern void osal_msg_extract( osal_msg_q_t *q_ptr, void *msg_ptr, void *prev_ptr );
  135. /*** Task Synchronization ***/
  136. /*
  137. * Set a Task Event
  138. */
  139. extern uint8 osal_set_event( uint8 task_id, uint16 event_flag );
  140. /*
  141. * Clear a Task Event
  142. */
  143. extern uint8 osal_clear_event( uint8 task_id, uint16 event_flag );
  144. /*** Interrupt Management ***/
  145. /*
  146. * Register Interrupt Service Routine (ISR)
  147. */
  148. extern uint8 osal_isr_register( uint8 interrupt_id, void (*isr_ptr)( uint8* ) );
  149. /*
  150. * Enable Interrupt
  151. */
  152. extern uint8 osal_int_enable( uint8 interrupt_id );
  153. /*
  154. * Disable Interrupt
  155. */
  156. extern uint8 osal_int_disable( uint8 interrupt_id );
  157. /*** Task Management ***/
  158. /*
  159. * Initialize the Task System
  160. */
  161. extern uint8 osal_init_system( void );
  162. /*
  163. * System Processing Loop
  164. */
  165. #if defined (ZBIT)
  166. extern __declspec(dllexport) void osal_start_system( void );
  167. #else
  168. extern void osal_start_system( void );
  169. #endif
  170. /*
  171. * One Pass Throu the OSAL Processing Loop
  172. */
  173. extern void osal_run_system( void );
  174. /*
  175. * Get the active task ID
  176. */
  177. extern uint8 osal_self( void );
  178. /*** Helper Functions ***/
  179. /*
  180. * String Length
  181. */
  182. extern int osal_strlen( char *pString );
  183. /*
  184. * Memory copy
  185. */
  186. extern void *osal_memcpy( void*, const void GENERIC *, unsigned int );
  187. /*
  188. * Memory Duplicate - allocates and copies
  189. */
  190. extern void *osal_memdup( const void GENERIC *src, unsigned int len );
  191. /*
  192. * Reverse Memory copy
  193. */
  194. extern void *osal_revmemcpy( void*, const void GENERIC *, unsigned int );
  195. /*
  196. * Memory compare
  197. */
  198. extern uint8 osal_memcmp( const void GENERIC *src1, const void GENERIC *src2, unsigned int len );
  199. /*
  200. * Memory set
  201. */
  202. extern void *osal_memset( void *dest, uint8 value, int len );
  203. /*
  204. * Build a uint16 out of 2 bytes (0 then 1).
  205. */
  206. extern uint16 osal_build_uint16( uint8 *swapped );
  207. /*
  208. * Build a uint32 out of sequential bytes.
  209. */
  210. extern uint32 osal_build_uint32( uint8 *swapped, uint8 len );
  211. /*
  212. * Convert long to ascii string
  213. */
  214. #if !defined ( ZBIT ) && !defined ( ZBIT2 ) && !defined (UBIT)
  215. extern uint8 *_ltoa( uint32 l, uint8 * buf, uint8 radix );
  216. #endif
  217. /*
  218. * Random number generator
  219. */
  220. extern uint16 osal_rand( void );
  221. /*
  222. * Buffer an uint32 value - LSB first.
  223. */
  224. extern uint8* osal_buffer_uint32( uint8 *buf, uint32 val );
  225. /*
  226. * Buffer an uint24 value - LSB first
  227. */
  228. extern uint8* osal_buffer_uint24( uint8 *buf, uint24 val );
  229. /*
  230. * Is all of the array elements set to a value?
  231. */
  232. extern uint8 osal_isbufset( uint8 *buf, uint8 val, uint8 len );
  233. /*********************************************************************
  234. *********************************************************************/
  235. #ifdef __cplusplus
  236. }
  237. #endif
  238. #endif /* OSAL_H */