hal_uart.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**************************************************************************************************
  2. Filename: _hal_uart.c
  3. Revised: $Date: 2009-12-31 18:28:34 -0800 (Thu, 31 Dec 2009) $
  4. Revision: $Revision: 21422 $
  5. Description: This file contains the interface to the H/W UART driver.
  6. Copyright 2006-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 "hal_board_cfg.h"
  37. #include "hal_defs.h"
  38. #include "hal_types.h"
  39. #include "hal_uart.h"
  40. /*********************************************************************
  41. * MACROS
  42. */
  43. /*********************************************************************
  44. * TYPEDEFS
  45. */
  46. /*********************************************************************
  47. * CONSTANTS
  48. */
  49. /*********************************************************************
  50. * GLOBAL VARIABLES
  51. */
  52. /*********************************************************************
  53. * GLOBAL FUNCTIONS
  54. */
  55. /*********************************************************************
  56. * LOCAL VARIABLES
  57. */
  58. /*********************************************************************
  59. * LOCAL FUNCTIONS
  60. */
  61. #if HAL_UART_DMA
  62. #include "_hal_uart_dma.c"
  63. #endif
  64. #if HAL_UART_ISR
  65. #include "_hal_uart_isr.c"
  66. #endif
  67. #if HAL_UART_USB
  68. #include "_hal_uart_usb.c"
  69. #endif
  70. /******************************************************************************
  71. * @fn HalUARTInit
  72. *
  73. * @brief Initialize the UART
  74. *
  75. * @param none
  76. *
  77. * @return none
  78. *****************************************************************************/
  79. void HalUARTInit(void)
  80. {
  81. if (ZNP_CFG1_SPI == znpCfg1)
  82. {
  83. return;
  84. }
  85. #if HAL_UART_DMA
  86. HalUARTInitDMA();
  87. #endif
  88. #if HAL_UART_ISR
  89. HalUARTInitISR();
  90. #endif
  91. #if HAL_UART_USB
  92. HalUARTInitUSB();
  93. #endif
  94. }
  95. /******************************************************************************
  96. * @fn HalUARTOpen
  97. *
  98. * @brief Open a port according tp the configuration specified by parameter.
  99. *
  100. * @param port - UART port
  101. * config - contains configuration information
  102. *
  103. * @return Status of the function call
  104. *****************************************************************************/
  105. uint8 HalUARTOpen(uint8 port, halUARTCfg_t *config)
  106. {
  107. (void)port;
  108. (void)config;
  109. #if (HAL_UART_DMA == 1)
  110. if (port == HAL_UART_PORT_0) HalUARTOpenDMA(config);
  111. #endif
  112. #if (HAL_UART_DMA == 2)
  113. if (port == HAL_UART_PORT_1) HalUARTOpenDMA(config);
  114. #endif
  115. #if (HAL_UART_ISR == 1)
  116. if (port == HAL_UART_PORT_0) HalUARTOpenISR(config);
  117. #endif
  118. #if (HAL_UART_ISR == 2)
  119. if (port == HAL_UART_PORT_1) HalUARTOpenISR(config);
  120. #endif
  121. #if (HAL_UART_USB)
  122. HalUARTOpenUSB(config);
  123. #endif
  124. return HAL_UART_SUCCESS;
  125. }
  126. /*****************************************************************************
  127. * @fn HalUARTRead
  128. *
  129. * @brief Read a buffer from the UART
  130. *
  131. * @param port - USART module designation
  132. * buf - valid data buffer at least 'len' bytes in size
  133. * len - max length number of bytes to copy to 'buf'
  134. *
  135. * @return length of buffer that was read
  136. *****************************************************************************/
  137. uint16 HalUARTRead(uint8 port, uint8 *buf, uint16 len)
  138. {
  139. (void)port;
  140. (void)buf;
  141. (void)len;
  142. #if (HAL_UART_DMA == 1)
  143. if (port == HAL_UART_PORT_0) return HalUARTReadDMA(buf, len);
  144. #endif
  145. #if (HAL_UART_DMA == 2)
  146. if (port == HAL_UART_PORT_1) return HalUARTReadDMA(buf, len);
  147. #endif
  148. #if (HAL_UART_ISR == 1)
  149. if (port == HAL_UART_PORT_0) return HalUARTReadISR(buf, len);
  150. #endif
  151. #if (HAL_UART_ISR == 2)
  152. if (port == HAL_UART_PORT_1) return HalUARTReadISR(buf, len);
  153. #endif
  154. #if HAL_UART_USB
  155. return HalUARTRx(buf, len);
  156. #else
  157. return 0;
  158. #endif
  159. }
  160. /******************************************************************************
  161. * @fn HalUARTWrite
  162. *
  163. * @brief Write a buffer to the UART.
  164. *
  165. * @param port - UART port
  166. * buf - pointer to the buffer that will be written, not freed
  167. * len - length of
  168. *
  169. * @return length of the buffer that was sent
  170. *****************************************************************************/
  171. uint16 HalUARTWrite(uint8 port, uint8 *buf, uint16 len)
  172. {
  173. (void)port;
  174. (void)buf;
  175. (void)len;
  176. #if (HAL_UART_DMA == 1)
  177. if (port == HAL_UART_PORT_0) return HalUARTWriteDMA(buf, len);
  178. #endif
  179. #if (HAL_UART_DMA == 2)
  180. if (port == HAL_UART_PORT_1) return HalUARTWriteDMA(buf, len);
  181. #endif
  182. #if (HAL_UART_ISR == 1)
  183. if (port == HAL_UART_PORT_0) return HalUARTWriteISR(buf, len);
  184. #endif
  185. #if (HAL_UART_ISR == 2)
  186. if (port == HAL_UART_PORT_1) return HalUARTWriteISR(buf, len);
  187. #endif
  188. #if HAL_UART_USB
  189. HalUARTTx(buf, len);
  190. return len;
  191. #else
  192. return 0;
  193. #endif
  194. }
  195. /******************************************************************************
  196. * @fn HalUARTSuspend
  197. *
  198. * @brief Suspend UART hardware before entering PM mode 1, 2 or 3.
  199. *
  200. * @param None
  201. *
  202. * @return None
  203. *****************************************************************************/
  204. void HalUARTSuspend( void )
  205. {
  206. #if HAL_UART_DMA
  207. HalUARTSuspendDMA();
  208. #endif
  209. #if HAL_UART_ISR
  210. HalUARTSuspendISR();
  211. #endif
  212. }
  213. /******************************************************************************
  214. * @fn HalUARTResume
  215. *
  216. * @brief Resume UART hardware after exiting PM mode 1, 2 or 3.
  217. *
  218. * @param None
  219. *
  220. * @return None
  221. *****************************************************************************/
  222. void HalUARTResume( void )
  223. {
  224. #if HAL_UART_DMA
  225. HalUARTResumeDMA();
  226. #endif
  227. #if HAL_UART_ISR
  228. HalUARTResumeISR();
  229. #endif
  230. }
  231. /***************************************************************************************************
  232. * @fn HalUARTPoll
  233. *
  234. * @brief Poll the UART.
  235. *
  236. * @param none
  237. *
  238. * @return none
  239. *****************************************************************************/
  240. void HalUARTPoll(void)
  241. {
  242. if (ZNP_CFG1_SPI == znpCfg1)
  243. {
  244. return;
  245. }
  246. #if HAL_UART_DMA
  247. HalUARTPollDMA();
  248. #endif
  249. #if HAL_UART_ISR
  250. HalUARTPollISR();
  251. #endif
  252. #if HAL_UART_USB
  253. HalUARTPollUSB();
  254. #endif
  255. }
  256. /**************************************************************************************************
  257. * @fn Hal_UART_RxBufLen()
  258. *
  259. * @brief Calculate Rx Buffer length - the number of bytes in the buffer.
  260. *
  261. * @param port - UART port
  262. *
  263. * @return length of current Rx Buffer
  264. **************************************************************************************************/
  265. uint16 Hal_UART_RxBufLen( uint8 port )
  266. {
  267. (void)port;
  268. #if (HAL_UART_DMA == 1)
  269. if (port == HAL_UART_PORT_0) return HalUARTRxAvailDMA();
  270. #endif
  271. #if (HAL_UART_DMA == 2)
  272. if (port == HAL_UART_PORT_1) return HalUARTRxAvailDMA();
  273. #endif
  274. #if (HAL_UART_ISR == 1)
  275. if (port == HAL_UART_PORT_0) return HalUARTRxAvailISR();
  276. #endif
  277. #if (HAL_UART_ISR == 2)
  278. if (port == HAL_UART_PORT_1) return HalUARTRxAvailISR();
  279. #endif
  280. #if HAL_UART_USB
  281. return HalUARTRxAvailUSB();
  282. #else
  283. return 0;
  284. #endif
  285. }
  286. /******************************************************************************
  287. ******************************************************************************/