MT_TASK.c 9.5 KB

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