osi.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. //*****************************************************************************
  2. // osi.h
  3. //
  4. // MACRO and Function prototypes for TI-RTOS and Free-RTOS API calls
  5. //
  6. // Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
  7. //
  8. //
  9. // Redistribution and use in source and binary forms, with or without
  10. // modification, are permitted provided that the following conditions
  11. // are met:
  12. //
  13. // Redistributions of source code must retain the above copyright
  14. // notice, this list zof conditions and the following disclaimer.
  15. //
  16. // Redistributions in binary form must reproduce the above copyright
  17. // notice, this list of conditions and the following disclaimer in the
  18. // documentation and/or other materials provided with the
  19. // distribution.
  20. //
  21. // Neither the name of Texas Instruments Incorporated nor the names of
  22. // its contributors may be used to endorse or promote products derived
  23. // from this software without specific prior written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. //
  37. //*****************************************************************************
  38. #ifndef __OSI_H__
  39. #define __OSI_H__
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. #define OSI_WAIT_FOREVER (0xFFFFFFFF)
  44. #define OSI_NO_WAIT (0)
  45. typedef enum
  46. {
  47. OSI_OK = 0,
  48. OSI_FAILURE = -1,
  49. OSI_OPERATION_FAILED = -2,
  50. OSI_ABORTED = -3,
  51. OSI_INVALID_PARAMS = -4,
  52. OSI_MEMORY_ALLOCATION_FAILURE = -5,
  53. OSI_TIMEOUT = -6,
  54. OSI_EVENTS_IN_USE = -7,
  55. OSI_EVENT_OPEARTION_FAILURE = -8
  56. }OsiReturnVal_e;
  57. //#define ENTER_CRITICAL_SECTION osi_EnterCritical()
  58. //#define EXIT_CRITICAL_SECTION osi_ExitCritical()
  59. typedef void* OsiMsgQ_t;
  60. /*!
  61. \brief type definition for a time value
  62. \note On each porting or platform the type could be whatever is needed - integer, pointer to structure etc.
  63. */
  64. //typedef unsigned int OsiTime_t;
  65. typedef unsigned int OsiTime_t;
  66. /*!
  67. \brief type definition for a sync object container
  68. Sync object is object used to synchronize between two threads or thread and interrupt handler.
  69. One thread is waiting on the object and the other thread send a signal, which then
  70. release the waiting thread.
  71. The signal must be able to be sent from interrupt context.
  72. This object is generally implemented by binary semaphore or events.
  73. \note On each porting or platform the type could be whatever is needed - integer, structure etc.
  74. */
  75. //typedef unsigned int OsiSyncObj_t;
  76. typedef void * OsiSyncObj_t;
  77. /*!
  78. \brief type definition for a locking object container
  79. Locking object are used to protect a resource from mutual accesses of two or more threads.
  80. The locking object should support re-entrant locks by a signal thread.
  81. This object is generally implemented by mutex semaphore
  82. \note On each porting or platform the type could be whatever is needed - integer, structure etc.
  83. */
  84. //typedef unsigned int OsiLockObj_t;
  85. typedef void * OsiLockObj_t;
  86. /*!
  87. \brief type definition for a spawn entry callback
  88. the spawn mechanism enable to run a function on different context.
  89. This mechanism allow to transfer the execution context from interrupt context to thread context
  90. or changing the context from an unknown user thread to general context.
  91. The implementation of the spawn mechanism depends on the user's system requirements and could varies
  92. from implementation of serialized execution using single thread to creating thread per call
  93. \note The stack size of the execution thread must be at least of TBD bytes!
  94. */
  95. typedef void (*P_OSI_SPAWN_ENTRY)(void* pValue);
  96. typedef void (*P_OSI_EVENT_HANDLER)(void* pValue);
  97. typedef void (*P_OSI_TASK_ENTRY)(void* pValue);
  98. typedef void (*P_OSI_INTR_ENTRY)(void);
  99. typedef void* OsiTaskHandle;
  100. /*!
  101. \brief This function registers an interrupt in NVIC table
  102. The sync object is used for synchronization between different thread or ISR and
  103. a thread.
  104. \param iIntrNum - Interrupt number to register
  105. \param pEntry - Pointer to the interrupt handler
  106. \return upon successful creation the function should return 0
  107. Otherwise, a negative value indicating the error code shall be returned
  108. \note
  109. \warning
  110. */
  111. OsiReturnVal_e osi_InterruptRegister(int iIntrNum,P_OSI_INTR_ENTRY pEntry,unsigned char ucPriority);
  112. /*!
  113. \brief This function De-registers an interrupt in NVIC table
  114. \param iIntrNum - Interrupt number to register
  115. \param pEntry - Pointer to the interrupt handler
  116. \return upon successful creation the function should return Positive number
  117. Otherwise, a negative value indicating the error code shall be returned
  118. \note
  119. \warning
  120. */
  121. void osi_InterruptDeRegister(int iIntrNum);
  122. /*!
  123. \brief This function creates a sync object
  124. The sync object is used for synchronization between different thread or ISR and
  125. a thread.
  126. \param pSyncObj - pointer to the sync object control block
  127. \return upon successful creation the function should return 0
  128. Otherwise, a negative value indicating the error code shall be returned
  129. \note
  130. \warning
  131. */
  132. OsiReturnVal_e osi_SyncObjCreate(OsiSyncObj_t* pSyncObj);
  133. /*!
  134. \brief This function deletes a sync object
  135. \param pSyncObj - pointer to the sync object control block
  136. \return upon successful deletion the function should return 0
  137. Otherwise, a negative value indicating the error code shall be returned
  138. \note
  139. \warning
  140. */
  141. OsiReturnVal_e osi_SyncObjDelete(OsiSyncObj_t* pSyncObj);
  142. /*!
  143. \brief This function generates a sync signal for the object.
  144. All suspended threads waiting on this sync object are resumed
  145. \param pSyncObj - pointer to the sync object control block
  146. \return upon successful signalling the function should return 0
  147. Otherwise, a negative value indicating the error code shall be returned
  148. \note the function could be called from ISR context
  149. \warning
  150. */
  151. OsiReturnVal_e osi_SyncObjSignal(OsiSyncObj_t* pSyncObj);
  152. /*!
  153. \brief This function generates a sync signal for the object.
  154. from ISR context.
  155. All suspended threads waiting on this sync object are resumed
  156. \param pSyncObj - pointer to the sync object control block
  157. \return upon successful signalling the function should return 0
  158. Otherwise, a negative value indicating the error code shall be returned
  159. \note the function is called from ISR context
  160. \warning
  161. */
  162. OsiReturnVal_e osi_SyncObjSignalFromISR(OsiSyncObj_t* pSyncObj);
  163. /*!
  164. \brief This function waits for a sync signal of the specific sync object
  165. \param pSyncObj - pointer to the sync object control block
  166. \param Timeout - numeric value specifies the maximum number of mSec to
  167. stay suspended while waiting for the sync signal
  168. Currently, the simple link driver uses only two values:
  169. - OSI_WAIT_FOREVER
  170. - OSI_NO_WAIT
  171. \return upon successful reception of the signal within the timeout window return 0
  172. Otherwise, a negative value indicating the error code shall be returned
  173. \note
  174. \warning
  175. */
  176. OsiReturnVal_e osi_SyncObjWait(OsiSyncObj_t* pSyncObj , OsiTime_t Timeout);
  177. /*!
  178. \brief This function clears a sync object
  179. \param pSyncObj - pointer to the sync object control block
  180. \return upon successful clearing the function should return 0
  181. Otherwise, a negative value indicating the error code shall be returned
  182. \note
  183. \warning
  184. */
  185. OsiReturnVal_e osi_SyncObjClear(OsiSyncObj_t* pSyncObj);
  186. /*!
  187. \brief This function creates a locking object.
  188. The locking object is used for protecting a shared resources between different
  189. threads.
  190. \param pLockObj - pointer to the locking object control block
  191. \return upon successful creation the function should return 0
  192. Otherwise, a negative value indicating the error code shall be returned
  193. \note
  194. \warning
  195. */
  196. OsiReturnVal_e osi_LockObjCreate(OsiLockObj_t* pLockObj);
  197. /*!
  198. \brief This function deletes a locking object.
  199. \param pLockObj - pointer to the locking object control block
  200. \return upon successful deletion the function should return 0
  201. Otherwise, a negative value indicating the error code shall be returned
  202. \note
  203. \warning
  204. */
  205. #define osi_LockObjDelete osi_SyncObjDelete
  206. /*!
  207. \brief This function locks a locking object.
  208. All other threads that call this function before this thread calls
  209. the osi_LockObjUnlock would be suspended
  210. \param pLockObj - pointer to the locking object control block
  211. \param Timeout - numeric value specifies the maximum number of mSec to
  212. stay suspended while waiting for the locking object
  213. Currently, the simple link driver uses only two values:
  214. - OSI_WAIT_FOREVER
  215. - OSI_NO_WAIT
  216. \return upon successful reception of the locking object the function should return 0
  217. Otherwise, a negative value indicating the error code shall be returned
  218. \note
  219. \warning
  220. */
  221. #define osi_LockObjLock osi_SyncObjWait
  222. /*!
  223. \brief This function unlock a locking object.
  224. \param pLockObj - pointer to the locking object control block
  225. \return upon successful unlocking the function should return 0
  226. Otherwise, a negative value indicating the error code shall be returned
  227. \note
  228. \warning
  229. */
  230. #define osi_LockObjUnlock osi_SyncObjSignal
  231. /*!
  232. \brief This function call the pEntry callback from a different context
  233. \param pEntry - pointer to the entry callback function
  234. \param pValue - pointer to any type of memory structure that would be
  235. passed to pEntry callback from the execution thread.
  236. \param flags - execution flags - reserved for future usage
  237. \return upon successful registration of the spawn the function should return 0
  238. (the function is not blocked till the end of the execution of the function
  239. and could be returned before the execution is actually completed)
  240. Otherwise, a negative value indicating the error code shall be returned
  241. \note
  242. \warning
  243. */
  244. /*!
  245. \brief This function creates a Task.
  246. Creates a new Task and add it to the last of tasks that are ready to run
  247. \param pEntry - pointer to the Task Function
  248. \param pcName - Task Name String
  249. \param usStackDepth - Stack Size Stack Size in 32-bit long words
  250. \param pvParameters - pointer to structure to be passed to the Task Function
  251. \param uxPriority - Task Priority
  252. \return upon successful unlocking the function should return 0
  253. Otherwise, a negative value indicating the error code shall be returned
  254. \note
  255. \warning
  256. */
  257. OsiReturnVal_e osi_TaskCreate(P_OSI_TASK_ENTRY pEntry,const signed char * const pcName,unsigned short usStackDepth,void *pvParameters,unsigned long uxPriority,OsiTaskHandle *pTaskHandle);
  258. /*!
  259. \brief This function Deletes a Task.
  260. Deletes a Task and remove it from list of running task
  261. \param pTaskHandle - Task Handle
  262. \note
  263. \warning
  264. */
  265. void osi_TaskDelete(OsiTaskHandle* pTaskHandle);
  266. /*!
  267. \brief This function call the pEntry callback from a different context
  268. \param pEntry - pointer to the entry callback function
  269. \param pValue - pointer to any type of memory structure that would be
  270. passed to pEntry callback from the execution thread.
  271. \param flags - execution flags - reserved for future usage
  272. \return upon successful registration of the spawn the function should return 0
  273. (the function is not blocked till the end of the execution of the function
  274. and could be returned before the execution is actually completed)
  275. Otherwise, a negative value indicating the error code shall be returned
  276. \note
  277. \warning
  278. */
  279. OsiReturnVal_e osi_Spawn(P_OSI_SPAWN_ENTRY pEntry , void* pValue , unsigned long flags);
  280. /*******************************************************************************
  281. This function creates a message queue that is typically used for inter thread
  282. communication.
  283. Parameters:
  284. pMsgQ - pointer to the message queue control block
  285. pMsgQName - pointer to the name of the message queue
  286. MsgSize - the size of the message.
  287. NOTICE: THE MESSGAE SIZE MUST BE SMALLER THAN 16
  288. MaxMsgs - maximum number of messages.
  289. Please note that this function allocates the entire memory required
  290. for the maximum number of messages (MsgSize * MaxMsgs).
  291. ********************************************************************************/
  292. OsiReturnVal_e osi_MsgQCreate(OsiMsgQ_t* pMsgQ ,
  293. char* pMsgQName,
  294. unsigned long MsgSize,
  295. unsigned long MaxMsgs);
  296. /*******************************************************************************
  297. This function deletes a specific message queue.
  298. All threads suspended waiting for a message from this queue are resumed with
  299. an error return value.
  300. Parameters:
  301. pMsgQ - pointer to the message queue control block
  302. ********************************************************************************/
  303. OsiReturnVal_e osi_MsgQDelete(OsiMsgQ_t* pMsgQ);
  304. /*******************************************************************************
  305. This function writes a message to a specific message queue.
  306. Notice that the message is copied to the queue from the memory area specified
  307. by pMsg pointer.
  308. --------------------------------------------------------------------------------
  309. THIS FUNCTION COULD BE CALLED FROM ISR AS LONG AS THE TIMEOUT PARAMETER IS
  310. SET TO "OSI_NO_WAIT"
  311. --------------------------------------------------------------------------------
  312. Parameters:
  313. pMsgQ - pointer to the message queue control block
  314. pMsg - pointer to the message
  315. Timeout - numeric value specifies the maximum number of mSec to stay
  316. suspended while waiting for available space for the message
  317. ********************************************************************************/
  318. OsiReturnVal_e osi_MsgQWrite(OsiMsgQ_t* pMsgQ, void* pMsg , OsiTime_t Timeout);
  319. /*******************************************************************************
  320. This function retrieves a message from the specified message queue. The
  321. retrieved message is copied from the queue into the memory area specified by
  322. the pMsg pointer
  323. Parameters:
  324. pMsgQ - pointer to the message queue control block
  325. pMsg - pointer that specify the location where to copy the message
  326. Timeout - numeric value specifies the maximum number of mSec to stay
  327. suspended while waiting for a message to be available
  328. ********************************************************************************/
  329. OsiReturnVal_e osi_MsgQRead(OsiMsgQ_t* pMsgQ, void* pMsg , OsiTime_t Timeout);
  330. /*!
  331. \brief This function starts the OS Scheduler
  332. \param - void
  333. \return - void
  334. \note
  335. \warning
  336. */
  337. void osi_start();
  338. /*!
  339. \brief Allocates Memory on Heap
  340. \param Size - Size of the Buffer to be allocated
  341. \sa
  342. \note
  343. \warning
  344. */
  345. void * mem_Malloc(unsigned long Size);
  346. /*!
  347. \brief Deallocates Memory
  348. \param pMem - Pointer to the Buffer to be freed
  349. \return void
  350. \sa
  351. \note
  352. \warning
  353. */
  354. void mem_Free(void *pMem);
  355. /*!
  356. \brief Set Memory
  357. \param pBuf - Pointer to the Buffer
  358. \param Val - Value to be set
  359. \param Size - Size of the memory to be set
  360. \sa
  361. \note
  362. \warning
  363. */
  364. void mem_set(void *pBuf,int Val,size_t Size);
  365. /*!
  366. \brief Copy Memory
  367. \param pDst - Pointer to the Destination Buffer
  368. \param pSrc - Pointer to the Source Buffer
  369. \param Size - Size of the memory to be copied
  370. \return void
  371. \note
  372. \warning
  373. */
  374. void mem_copy(void *pDst, void *pSrc,size_t Size);
  375. /*!
  376. \brief Enter Critical Section
  377. \sa
  378. \note
  379. \warning
  380. */
  381. void osi_EnterCritical(void);
  382. /*!
  383. \brief Exit Critical Section
  384. \sa
  385. \note
  386. \warning
  387. */
  388. void osi_ExitCritical(void);
  389. /*!
  390. \brief This function used to save the os context before sleep
  391. \param void
  392. \return void
  393. \note
  394. \warning
  395. */
  396. void osi_ContextSave();
  397. /*!
  398. \brief This function used to retrieve the context after sleep
  399. \param void
  400. \return void
  401. \note
  402. \warning
  403. */
  404. void osi_ContextRestore();
  405. /*!
  406. \brief This function used to suspend the task for the specified number of milli secs
  407. \param MilliSecs - Time in millisecs to suspend the task
  408. \return void
  409. \note
  410. \warning
  411. */
  412. void osi_Sleep(unsigned int MilliSecs);
  413. /*!
  414. \brief This function used to disable the tasks
  415. \param - void
  416. \return - void
  417. \note
  418. \warning
  419. */
  420. void osi_TaskDisable(void);
  421. /*!
  422. \brief This function used to enable all tasks
  423. \param - void
  424. \return - void
  425. \note
  426. \warning
  427. */
  428. void osi_TaskEnable(void);
  429. /*!
  430. \brief structure definition for simple link spawn message
  431. \note On each porting or platform the type could be whatever is needed - integer, pointer to structure etc.
  432. */
  433. typedef struct
  434. {
  435. P_OSI_SPAWN_ENTRY pEntry;
  436. void* pValue;
  437. }tSimpleLinkSpawnMsg;
  438. /* The queue used to send message to simple link spawn task. */
  439. extern void* xSimpleLinkSpawnQueue;
  440. /* API for SL Task*/
  441. OsiReturnVal_e VStartSimpleLinkSpawnTask(unsigned long uxPriority);
  442. void VDeleteSimpleLinkSpawnTask( void );
  443. #ifdef __cplusplus
  444. }
  445. #endif // __cplusplus
  446. #endif