MT_UART.c 14 KB

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