MT_TASK.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /***************************************************************************************************
  2. Filename: MT_TASK.c
  3. Revised: $Date: 2011-06-07 15:36:01 -0700 (Tue, 07 Jun 2011) $
  4. Revision: $Revision: 26245 $
  5. Description: MonitorTest Task handling routines
  6. Copyright 2007-2011 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 "MT_TASK.h"
  38. #include "MT.h"
  39. #include "MT_DEBUG.h"
  40. #include "MT_UART.h"
  41. #include "MT_UTIL.h"
  42. #include "MT_SYS.h"
  43. #if !defined( NONWK )
  44. #include "MT_ZDO.h"
  45. #include "MT_AF.h"
  46. #endif /* NONWK */
  47. #include "hal_uart.h"
  48. #include "OSAL_Memory.h"
  49. /***************************************************************************************************
  50. * LOCAL FUNCTIONS
  51. ***************************************************************************************************/
  52. static void MT_ProcessIncomingCommand( mtOSALSerialData_t *msg );
  53. /***************************************************************************************************
  54. * GLOBALS
  55. ***************************************************************************************************/
  56. uint8 MT_TaskID;
  57. /***************************************************************************************************
  58. * @fn MT_TaskInit
  59. *
  60. * @brief MonitorTest Task Initialization. This function is put into the
  61. * task table.
  62. *
  63. * @param task_id - task ID of the MT Task
  64. *
  65. * @return void
  66. ***************************************************************************************************/
  67. void MT_TaskInit(uint8 task_id)
  68. {
  69. MT_TaskID = task_id;
  70. /* Initialize the Serial port */
  71. MT_UartInit();
  72. /* Register taskID - Do this after UartInit() because it will reset the taskID */
  73. MT_UartRegisterTaskID(task_id);
  74. osal_set_event(task_id, MT_SECONDARY_INIT_EVENT);
  75. }
  76. /**************************************************************************************************
  77. * @fn MT_ProcessEvent
  78. *
  79. * @brief MonitorTest Task Event Processor. This task is put into the task table.
  80. *
  81. * @param task_id - task ID of the MT Task
  82. * @param events - event(s) for the MT Task
  83. *
  84. * @return Bit mask of the unprocessed MT Task events.
  85. **************************************************************************************************/
  86. UINT16 MT_ProcessEvent(uint8 task_id, uint16 events)
  87. {
  88. /* Could be multiple events, so switch won't work */
  89. if ( events & SYS_EVENT_MSG )
  90. {
  91. uint8 *msg_ptr = osal_msg_receive(task_id);
  92. if (msg_ptr != NULL)
  93. {
  94. MT_ProcessIncomingCommand((mtOSALSerialData_t *)msg_ptr);
  95. osal_msg_deallocate(msg_ptr);
  96. }
  97. /* Return unproccessed events */
  98. return (events ^ SYS_EVENT_MSG);
  99. }
  100. if ( events & MT_SECONDARY_INIT_EVENT )
  101. {
  102. MT_Init();
  103. /* Return unproccessed events */
  104. return (events ^ MT_SECONDARY_INIT_EVENT);
  105. }
  106. if ( events & MT_ZTOOL_SERIAL_RCV_BUFFER_FULL )
  107. {
  108. /* Return unproccessed events */
  109. return (events ^ MT_ZTOOL_SERIAL_RCV_BUFFER_FULL);
  110. }
  111. #if !defined( NONWK )
  112. if ( events & MT_AF_EXEC_EVT )
  113. {
  114. MT_AfExec();
  115. return (events ^ MT_AF_EXEC_EVT);
  116. }
  117. #endif /* NONWK */
  118. /* Handle MT_SYS_OSAL_START_TIMER callbacks */
  119. #if defined MT_SYS_FUNC
  120. if ( events & (MT_SYS_OSAL_EVENT_MASK))
  121. {
  122. if (events & MT_SYS_OSAL_EVENT_0)
  123. {
  124. MT_SysOsalTimerExpired(0x00);
  125. events ^= MT_SYS_OSAL_EVENT_0;
  126. }
  127. if (events & MT_SYS_OSAL_EVENT_1)
  128. {
  129. MT_SysOsalTimerExpired(0x01);
  130. events ^= MT_SYS_OSAL_EVENT_1;
  131. }
  132. if (events & MT_SYS_OSAL_EVENT_2)
  133. {
  134. MT_SysOsalTimerExpired(0x02);
  135. events ^= MT_SYS_OSAL_EVENT_2;
  136. }
  137. if (events & MT_SYS_OSAL_EVENT_3)
  138. {
  139. MT_SysOsalTimerExpired(0x03);
  140. events ^= MT_SYS_OSAL_EVENT_3;
  141. }
  142. return events;
  143. }
  144. #endif
  145. /* Discard or make more handlers */
  146. return 0;
  147. } /* MT_ProcessEvent() */
  148. /***************************************************************************************************
  149. * @fn MT_ProcessIncomingCommand
  150. *
  151. * @brief
  152. *
  153. * Process Event Messages.
  154. *
  155. * @param *msg - pointer to event message
  156. *
  157. * @return
  158. ***************************************************************************************************/
  159. static void MT_ProcessIncomingCommand( mtOSALSerialData_t *msg )
  160. {
  161. uint8 len, *msg_ptr = msg->msg;
  162. /* Use the first byte of the message as the command ID */
  163. switch ( msg->hdr.event )
  164. {
  165. case CMD_SERIAL_MSG:
  166. MT_ProcessIncoming(msg_ptr);
  167. break;
  168. case CMD_DEBUG_MSG:
  169. MT_ProcessDebugMsg( (mtDebugMsg_t *)msg );
  170. break;
  171. case CB_FUNC:
  172. /*
  173. Build SPI message here instead of redundantly calling MT_BuildSPIMsg
  174. because we have copied data already in the allocated message
  175. */
  176. /* msg_ptr is the beginning of the intended SPI message */
  177. len = SPI_0DATA_MSG_LEN + msg_ptr[DATALEN_FIELD];
  178. /*
  179. FCS goes to the last byte in the message and is calculated over all
  180. the bytes except FCS and SOP
  181. */
  182. msg_ptr[len-1] = MT_UartCalcFCS(msg_ptr + 1, (uint8)(len-2));
  183. #ifdef MT_UART_DEFAULT_PORT
  184. HalUARTWrite ( MT_UART_DEFAULT_PORT, msg_ptr, len );
  185. #endif
  186. break;
  187. case CMD_DEBUG_STR:
  188. MT_ProcessDebugStr( (mtDebugStr_t *)msg );
  189. break;
  190. #if !defined ( NONWK )
  191. case MT_SYS_APP_RSP_MSG:
  192. len = SPI_0DATA_MSG_LEN + msg_ptr[DATALEN_FIELD];
  193. MTProcessAppRspMsg( msg_ptr, len );
  194. break;
  195. #endif // NONWK
  196. #if defined (MT_UTIL_FUNC)
  197. #if defined ZCL_KEY_ESTABLISH
  198. case ZCL_KEY_ESTABLISH_IND:
  199. MT_UtilKeyEstablishInd((keyEstablishmentInd_t *)msg);
  200. break;
  201. #endif
  202. #endif
  203. #ifdef MT_ZDO_CB_FUNC
  204. case ZDO_STATE_CHANGE:
  205. MT_ZdoStateChangeCB((osal_event_hdr_t *)msg);
  206. break;
  207. #endif
  208. default:
  209. break;
  210. }
  211. }
  212. #ifdef MT_TASK
  213. /***************************************************************************************************
  214. * @fn MT_TransportAlloc
  215. *
  216. * @brief Allocate memory for transport msg
  217. *
  218. * @param uint8 cmd0 - The first byte of the MT command id containing the command type and subsystem.
  219. * uint8 len - length
  220. *
  221. * @return pointer the allocated memory or NULL if fail to allocate the memory
  222. ***************************************************************************************************/
  223. uint8 *MT_TransportAlloc(uint8 cmd0, uint8 len)
  224. {
  225. uint8 *p;
  226. (void)cmd0; // Intentionally unreferenced parameter
  227. /* Allocate a buffer of data length + SOP+CMD+FCS (5 bytes) */
  228. p = osal_msg_allocate(len + SPI_0DATA_MSG_LEN);
  229. if (p)
  230. {
  231. p++; /* Save space for SOP_VALUE, msg structure */
  232. return p;
  233. }
  234. else
  235. {
  236. return NULL;
  237. }
  238. }
  239. /***************************************************************************************************
  240. * @fn MT_TransportSend
  241. *
  242. * @brief Fill in SOP and FCS then send out the msg
  243. *
  244. * @param uint8 *pBuf - pointer to the message that contains CMD, length, data and FCS
  245. *
  246. * @return None
  247. ***************************************************************************************************/
  248. void MT_TransportSend(uint8 *pBuf)
  249. {
  250. uint8 *msgPtr;
  251. uint8 dataLen = pBuf[0]; /* Data length is on byte #1 from the pointer */
  252. /* Move back to the SOP */
  253. msgPtr = pBuf-1;
  254. /* Insert SOP */
  255. msgPtr[0] = MT_UART_SOF;
  256. /* Insert FCS */
  257. msgPtr[SPI_0DATA_MSG_LEN - 1 + dataLen] = MT_UartCalcFCS (pBuf, (3 + dataLen));
  258. /* Send to UART */
  259. #ifdef MT_UART_DEFAULT_PORT
  260. HalUARTWrite(MT_UART_DEFAULT_PORT, msgPtr, dataLen + SPI_0DATA_MSG_LEN);
  261. #endif
  262. /* Deallocate */
  263. osal_msg_deallocate(msgPtr);
  264. }
  265. #endif /* MT_TASK */
  266. /***************************************************************************************************
  267. ***************************************************************************************************/