MT_UART.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /***************************************************************************************************
  2. Filename: MT_UART.c
  3. Revised: $Date: 2009-08-21 13:10:13 -0700 (Fri, 21 Aug 2009) $
  4. Revision: $Revision: 20630 $
  5. Description: This module handles anything dealing with the serial port.
  6. Copyright 2007-2009 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 "OSAL.h"
  38. #include "hal_uart.h"
  39. #include "MT.h"
  40. #include "MT_UART.h"
  41. #include "MT_X.h"
  42. #include "OSAL_Memory.h"
  43. /***************************************************************************************************
  44. * MACROS
  45. ***************************************************************************************************/
  46. /***************************************************************************************************
  47. * CONSTANTS
  48. ***************************************************************************************************/
  49. /* State values for ZTool protocal */
  50. #define SOP_STATE 0x00
  51. #define CMD_STATE1 0x01
  52. #define CMD_STATE2 0x02
  53. #define LEN_STATE 0x03
  54. #define DATA_STATE 0x04
  55. #define FCS_STATE 0x05
  56. /***************************************************************************************************
  57. * GLOBAL VARIABLES
  58. ***************************************************************************************************/
  59. /* Used to indentify the application ID for osal task */
  60. byte App_TaskID;
  61. /* ZTool protocal parameters */
  62. uint8 state;
  63. uint8 CMD_Token[2];
  64. uint8 LEN_Token;
  65. uint8 FSC_Token;
  66. mtOSALSerialData_t *pMsg;
  67. uint8 tempDataLen;
  68. #if defined (ZAPP_P1) || defined (ZAPP_P2)
  69. uint16 MT_UartMaxZAppBufLen;
  70. bool MT_UartZAppRxStatus;
  71. #endif
  72. /***************************************************************************************************
  73. * LOCAL FUNCTIONS
  74. ***************************************************************************************************/
  75. /***************************************************************************************************
  76. * @fn MT_UartInit
  77. *
  78. * @brief Initialize MT with UART support
  79. *
  80. * @param None
  81. *
  82. * @return None
  83. ***************************************************************************************************/
  84. void MT_UartInit ()
  85. {
  86. halUARTCfg_t uartConfig;
  87. /* Initialize APP ID */
  88. App_TaskID = 0;
  89. /* UART Configuration */
  90. uartConfig.configured = TRUE;
  91. uartConfig.baudRate = MT_UART_DEFAULT_BAUDRATE;
  92. uartConfig.flowControl = MT_UART_DEFAULT_OVERFLOW;
  93. uartConfig.flowControlThreshold = MT_UART_DEFAULT_THRESHOLD;
  94. uartConfig.rx.maxBufSize = MT_UART_DEFAULT_MAX_RX_BUFF;
  95. // Need extra Tx for flurry of IEEE responses on large network.
  96. uartConfig.tx.maxBufSize = 254;
  97. uartConfig.idleTimeout = MT_UART_DEFAULT_IDLE_TIMEOUT;
  98. uartConfig.intEnable = TRUE;
  99. #if defined (ZTOOL_P1) || defined (ZTOOL_P2)
  100. uartConfig.callBackFunc = MT_X_UartProcessZToolData;
  101. #elif defined (ZAPP_P1) || defined (ZAPP_P2)
  102. uartConfig.callBackFunc = MT_UartProcessZAppData;
  103. #else
  104. uartConfig.callBackFunc = NULL;
  105. #endif
  106. /* Start UART */
  107. #if defined (MT_UART_DEFAULT_PORT)
  108. HalUARTOpen (MT_UART_DEFAULT_PORT, &uartConfig);
  109. #else
  110. /* Silence IAR compiler warning */
  111. (void)uartConfig;
  112. #endif
  113. /* Initialize for ZApp */
  114. #if defined (ZAPP_P1) || defined (ZAPP_P2)
  115. /* Default max bytes that ZAPP can take */
  116. MT_UartMaxZAppBufLen = 1;
  117. MT_UartZAppRxStatus = MT_UART_ZAPP_RX_READY;
  118. #endif
  119. }
  120. /***************************************************************************************************
  121. * @fn MT_SerialRegisterTaskID
  122. *
  123. * @brief This function registers the taskID of the application so it knows
  124. * where to send the messages whent they come in.
  125. *
  126. * @param void
  127. *
  128. * @return void
  129. ***************************************************************************************************/
  130. void MT_UartRegisterTaskID( byte taskID )
  131. {
  132. App_TaskID = taskID;
  133. }
  134. /***************************************************************************************************
  135. * @fn SPIMgr_CalcFCS
  136. *
  137. * @brief Calculate the FCS of a message buffer by XOR'ing each byte.
  138. * Remember to NOT include SOP and FCS fields, so start at the CMD field.
  139. *
  140. * @param byte *msg_ptr - message pointer
  141. * @param byte len - length (in bytes) of message
  142. *
  143. * @return result byte
  144. ***************************************************************************************************/
  145. byte MT_UartCalcFCS( uint8 *msg_ptr, uint8 len )
  146. {
  147. byte x;
  148. byte xorResult;
  149. xorResult = 0;
  150. for ( x = 0; x < len; x++, msg_ptr++ )
  151. xorResult = xorResult ^ *msg_ptr;
  152. return ( xorResult );
  153. }
  154. /***************************************************************************************************
  155. * @fn MT_UartProcessZToolData
  156. *
  157. * @brief | SOP | Data Length | CMD | Data | FCS |
  158. * | 1 | 1 | 2 | 0-Len | 1 |
  159. *
  160. * Parses the data and determine either is SPI or just simply serial data
  161. * then send the data to correct place (MT or APP)
  162. *
  163. * @param port - UART port
  164. * event - Event that causes the callback
  165. *
  166. *
  167. * @return None
  168. ***************************************************************************************************/
  169. void MT_UartProcessZToolByte ( uint8 ch );
  170. void MT_UartProcessZToolByte ( uint8 ch )
  171. {
  172. switch (state)
  173. {
  174. case SOP_STATE:
  175. if (ch == MT_UART_SOF)
  176. state = LEN_STATE;
  177. break;
  178. case LEN_STATE:
  179. LEN_Token = ch;
  180. tempDataLen = 0;
  181. /* Allocate memory for the data */
  182. pMsg = (mtOSALSerialData_t *)osal_msg_allocate( sizeof ( mtOSALSerialData_t ) +
  183. MT_RPC_FRAME_HDR_SZ + LEN_Token );
  184. if (pMsg)
  185. {
  186. /* Fill up what we can */
  187. pMsg->hdr.event = CMD_SERIAL_MSG;
  188. pMsg->msg = (uint8*)(pMsg+1);
  189. pMsg->msg[MT_RPC_POS_LEN] = LEN_Token;
  190. state = CMD_STATE1;
  191. }
  192. else
  193. {
  194. state = SOP_STATE;
  195. return;
  196. }
  197. break;
  198. case CMD_STATE1:
  199. pMsg->msg[MT_RPC_POS_CMD0] = ch;
  200. state = CMD_STATE2;
  201. break;
  202. case CMD_STATE2:
  203. pMsg->msg[MT_RPC_POS_CMD1] = ch;
  204. /* If there is no data, skip to FCS state */
  205. if (LEN_Token)
  206. {
  207. state = DATA_STATE;
  208. }
  209. else
  210. {
  211. state = FCS_STATE;
  212. }
  213. break;
  214. case DATA_STATE:
  215. /* Fill in the buffer the first byte of the data */
  216. pMsg->msg[MT_RPC_FRAME_HDR_SZ + tempDataLen++] = ch;
  217. /* If number of bytes read is equal to data length, time to move on to FCS */
  218. if ( tempDataLen == LEN_Token )
  219. state = FCS_STATE;
  220. break;
  221. case FCS_STATE:
  222. FSC_Token = ch;
  223. /* Make sure it's correct */
  224. if ((MT_UartCalcFCS ((uint8*)&pMsg->msg[0], MT_RPC_FRAME_HDR_SZ + LEN_Token) == FSC_Token))
  225. {
  226. osal_msg_send( App_TaskID, (byte *)pMsg );
  227. }
  228. else
  229. {
  230. /* deallocate the msg */
  231. osal_msg_deallocate ( (uint8 *)pMsg );
  232. }
  233. /* Reset the state, send or discard the buffers at this point */
  234. state = SOP_STATE;
  235. break;
  236. default:
  237. break;
  238. }
  239. }
  240. #if defined (ZAPP_P1) || defined (ZAPP_P2)
  241. /***************************************************************************************************
  242. * @fn MT_UartProcessZAppData
  243. *
  244. * @brief | SOP | CMD | Data Length | FSC |
  245. * | 1 | 2 | 1 | 1 |
  246. *
  247. * Parses the data and determine either is SPI or just simply serial data
  248. * then send the data to correct place (MT or APP)
  249. *
  250. * @param port - UART port
  251. * event - Event that causes the callback
  252. *
  253. *
  254. * @return None
  255. ***************************************************************************************************/
  256. void MT_UartProcessZAppData ( uint8 port, uint8 event )
  257. {
  258. osal_event_hdr_t *msg_ptr;
  259. uint16 length = 0;
  260. uint16 rxBufLen = Hal_UART_RxBufLen(MT_UART_DEFAULT_PORT);
  261. /*
  262. If maxZAppBufferLength is 0 or larger than current length
  263. the entire length of the current buffer is returned.
  264. */
  265. if ((MT_UartMaxZAppBufLen != 0) && (MT_UartMaxZAppBufLen <= rxBufLen))
  266. {
  267. length = MT_UartMaxZAppBufLen;
  268. }
  269. else
  270. {
  271. length = rxBufLen;
  272. }
  273. /* Verify events */
  274. if (event == HAL_UART_TX_FULL)
  275. {
  276. // Do something when TX if full
  277. return;
  278. }
  279. if (event & ( HAL_UART_RX_FULL | HAL_UART_RX_ABOUT_FULL | HAL_UART_RX_TIMEOUT))
  280. {
  281. if ( App_TaskID )
  282. {
  283. /*
  284. If Application is ready to receive and there is something
  285. in the Rx buffer then send it up
  286. */
  287. if ((MT_UartZAppRxStatus == MT_UART_ZAPP_RX_READY ) && (length != 0))
  288. {
  289. /* Disable App flow control until it processes the current data */
  290. MT_UartAppFlowControl (MT_UART_ZAPP_RX_NOT_READY);
  291. /* 2 more bytes are added, 1 for CMD type, other for length */
  292. msg_ptr = (osal_event_hdr_t *)osal_msg_allocate( length + sizeof(osal_event_hdr_t) );
  293. if ( msg_ptr )
  294. {
  295. msg_ptr->event = SPI_INCOMING_ZAPP_DATA;
  296. msg_ptr->status = length;
  297. /* Read the data of Rx buffer */
  298. HalUARTRead( MT_UART_DEFAULT_PORT, (uint8 *)(msg_ptr + 1), length );
  299. /* Send the raw data to application...or where ever */
  300. osal_msg_send( App_TaskID, (uint8 *)msg_ptr );
  301. }
  302. }
  303. }
  304. }
  305. }
  306. /***************************************************************************************************
  307. * @fn SPIMgr_ZAppBufferLengthRegister
  308. *
  309. * @brief
  310. *
  311. * @param maxLen - Max Length that the application wants at a time
  312. *
  313. * @return None
  314. *
  315. ***************************************************************************************************/
  316. void MT_UartZAppBufferLengthRegister ( uint16 maxLen )
  317. {
  318. /* If the maxLen is larger than the RX buff, something is not right */
  319. if (maxLen <= MT_UART_DEFAULT_MAX_RX_BUFF)
  320. MT_UartMaxZAppBufLen = maxLen;
  321. else
  322. MT_UartMaxZAppBufLen = 1; /* default is 1 byte */
  323. }
  324. /***************************************************************************************************
  325. * @fn SPIMgr_AppFlowControl
  326. *
  327. * @brief
  328. *
  329. * @param status - ready to send or not
  330. *
  331. * @return None
  332. *
  333. ***************************************************************************************************/
  334. void MT_UartAppFlowControl ( bool status )
  335. {
  336. /* Make sure only update if needed */
  337. if (status != MT_UartZAppRxStatus )
  338. {
  339. MT_UartZAppRxStatus = status;
  340. }
  341. /* App is ready to read again, ProcessZAppData have to be triggered too */
  342. if (status == MT_UART_ZAPP_RX_READY)
  343. {
  344. MT_UartProcessZAppData (MT_UART_DEFAULT_PORT, HAL_UART_RX_TIMEOUT );
  345. }
  346. }
  347. #endif //ZAPP
  348. /***************************************************************************************************
  349. ***************************************************************************************************/