OSAL_ZNP.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**************************************************************************************************
  2. Filename: OSAL_ZNP.c
  3. Revised: $Date: 2010-08-12 16:15:23 -0700 (Thu, 12 Aug 2010) $
  4. Revision: $Revision: 23398 $
  5. Description: This file is the Application-specific mandatory OSAL file.
  6. Copyright 2009-2010 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. /*********************************************************************
  34. * INCLUDES
  35. */
  36. #include "ZComDef.h"
  37. #include "hal_drivers.h"
  38. #include "hal_mcu.h"
  39. #include "OSAL.h"
  40. #include "OSAL_Clock.h"
  41. #include "OSAL_PwrMgr.h"
  42. #include "OSAL_Tasks.h"
  43. #include "MT.h"
  44. #include "MT_TASK.h"
  45. #include "nwk.h"
  46. #include "APS.h"
  47. #include "ZDApp.h"
  48. #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
  49. #include "ZDNwkMgr.h"
  50. #endif
  51. #if defined ( ZIGBEE_FRAGMENTATION )
  52. #include "aps_frag.h"
  53. #endif
  54. #if defined ( INTER_PAN )
  55. #include "stub_aps.h"
  56. #endif
  57. #include "sapi.h"
  58. #include "znp_app.h"
  59. #if defined ( ZCL_KEY_ESTABLISH )
  60. #include "zcl_key_establish.h"
  61. #endif
  62. /*********************************************************************
  63. * GLOBAL VARIABLES
  64. */
  65. // The order in this table must be identical to the task initialization calls below in osalInitTask.
  66. const pTaskEventHandlerFn tasksArr[] = {
  67. znpEventLoop,
  68. macEventLoop,
  69. nwk_event_loop,
  70. APS_event_loop,
  71. #if defined ( ZIGBEE_FRAGMENTATION )
  72. APSF_ProcessEvent,
  73. #endif
  74. ZDApp_event_loop,
  75. #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
  76. ZDNwkMgr_event_loop,
  77. #endif
  78. #if defined( INTER_PAN )
  79. StubAPS_ProcessEvent,
  80. #endif
  81. SAPI_ProcessEvent,
  82. #if defined ( TC_LINKKEY_JOIN )
  83. zclKeyEstablish_event_loop,
  84. #endif
  85. Hal_ProcessEvent
  86. };
  87. const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
  88. uint16 *tasksEvents;
  89. /*********************************************************************
  90. * FUNCTIONS
  91. *********************************************************************/
  92. void osal_start_znp(void);
  93. static void osal_run_task(uint8 idx);
  94. /*********************************************************************
  95. * @fn osalInitTasks
  96. *
  97. * @brief This function invokes the initialization function for each task.
  98. *
  99. * @param void
  100. *
  101. * @return none
  102. */
  103. void osalInitTasks( void )
  104. {
  105. uint8 taskID = 0;
  106. tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
  107. osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));
  108. znpInit( taskID++ );
  109. macTaskInit( taskID++ );
  110. nwk_init( taskID++ );
  111. APS_Init( taskID++ );
  112. #if defined ( ZIGBEE_FRAGMENTATION )
  113. APSF_Init( taskID++ );
  114. #endif
  115. ZDApp_Init( taskID++ );
  116. #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
  117. ZDNwkMgr_Init( taskID++ );
  118. #endif
  119. #if defined( INTER_PAN )
  120. StubAPS_Init( taskID++ );
  121. #endif
  122. SAPI_Init( taskID++ );
  123. #if defined ( TC_LINKKEY_JOIN )
  124. zclGeneral_KeyEstablish_Init( taskID++ );
  125. #endif
  126. Hal_Init( taskID );
  127. }
  128. /*********************************************************************
  129. * @fn osal_start_znp
  130. *
  131. * @brief
  132. *
  133. * This function is the main loop function of the task system. It
  134. * will look through all task events and call the task_event_processor()
  135. * function for the task with the event. If there are no events (for
  136. * all tasks), this function puts the processor into Sleep.
  137. * This Function doesn't return.
  138. *
  139. * @param void
  140. *
  141. * @return none
  142. */
  143. void osal_start_znp( void )
  144. {
  145. #if !defined ( ZBIT ) && !defined ( UBIT )
  146. for(;;) // Forever Loop
  147. #endif
  148. {
  149. #if defined( POWER_SAVING )
  150. uint8 busy = FALSE;
  151. #endif
  152. uint8 idx;
  153. osalTimeUpdate();
  154. Hal_ProcessPoll();
  155. for (idx = 1; idx < tasksCnt; idx++)
  156. {
  157. if (tasksEvents[idx])
  158. {
  159. osal_run_task(idx);
  160. #if defined( POWER_SAVING )
  161. busy = TRUE;
  162. #endif
  163. break;
  164. }
  165. }
  166. if (tasksEvents[0]) // Always run the ZNP task.
  167. {
  168. osal_run_task(0);
  169. #if defined( POWER_SAVING )
  170. busy = TRUE;
  171. #endif
  172. }
  173. #if defined( POWER_SAVING )
  174. if (!busy) // Complete pass through all task events with no activity?
  175. {
  176. osal_pwrmgr_powerconserve(); // Put the processor/system into sleep.
  177. }
  178. #endif
  179. }
  180. }
  181. /*********************************************************************
  182. * @fn osal_run_task
  183. *
  184. * @brief
  185. *
  186. * This function is the main loop function of the task system. It
  187. * will look through all task events and call the task_event_processor()
  188. * function for the task with the event. If there are no events (for
  189. * all tasks), this function puts the processor into Sleep.
  190. * This Function doesn't return.
  191. *
  192. * @param void
  193. *
  194. * @return none
  195. */
  196. static void osal_run_task(uint8 idx)
  197. {
  198. uint16 events;
  199. halIntState_t intState;
  200. HAL_ENTER_CRITICAL_SECTION(intState);
  201. events = tasksEvents[idx];
  202. tasksEvents[idx] = 0; // Clear the Events for this task.
  203. HAL_EXIT_CRITICAL_SECTION(intState);
  204. events = (tasksArr[idx])( idx, events );
  205. HAL_ENTER_CRITICAL_SECTION(intState);
  206. tasksEvents[idx] |= events; // Add back unprocessed events to the current task.
  207. HAL_EXIT_CRITICAL_SECTION(intState);
  208. }
  209. /*********************************************************************
  210. *********************************************************************/