hal_uart.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**************************************************************************************************
  2. Filename: _hal_uart.c
  3. Revised: $Date: 2009-06-12 09:16:43 -0700 (Fri, 12 Jun 2009) $
  4. Revision: $Revision: 20142 $
  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 HAL_UART_DMA
  82. HalUARTInitDMA();
  83. #endif
  84. #if HAL_UART_ISR
  85. HalUARTInitISR();
  86. #endif
  87. #if HAL_UART_USB
  88. HalUARTInitUSB();
  89. #endif
  90. }
  91. /******************************************************************************
  92. * @fn HalUARTOpen
  93. *
  94. * @brief Open a port according tp the configuration specified by parameter.
  95. *
  96. * @param port - UART port
  97. * config - contains configuration information
  98. *
  99. * @return Status of the function call
  100. *****************************************************************************/
  101. uint8 HalUARTOpen(uint8 port, halUARTCfg_t *config)
  102. {
  103. (void)port;
  104. (void)config;
  105. #if (HAL_UART_DMA == 1) //ĬÈϵÈÓÚ HAL_UART_DMA 1
  106. if (port == HAL_UART_PORT_0) HalUARTOpenDMA(config);
  107. #endif
  108. #if (HAL_UART_DMA == 2)
  109. if (port == HAL_UART_PORT_1) HalUARTOpenDMA(config);
  110. #endif
  111. #if (HAL_UART_ISR == 1)
  112. if (port == HAL_UART_PORT_0) HalUARTOpenISR(config);
  113. #endif
  114. #if (HAL_UART_ISR == 2)
  115. if (port == HAL_UART_PORT_1) HalUARTOpenISR(config);
  116. #endif
  117. #if (HAL_UART_USB)
  118. HalUARTOpenUSB(config);
  119. #endif
  120. return HAL_UART_SUCCESS;
  121. }
  122. /*****************************************************************************
  123. * @fn HalUARTRead
  124. *
  125. * @brief Read a buffer from the UART
  126. *
  127. * @param port - USART module designation
  128. * buf - valid data buffer at least 'len' bytes in size
  129. * len - max length number of bytes to copy to 'buf'
  130. *
  131. * @return length of buffer that was read
  132. *****************************************************************************/
  133. uint16 HalUARTRead(uint8 port, uint8 *buf, uint16 len)
  134. {
  135. (void)port;
  136. (void)buf;
  137. (void)len;
  138. #if (HAL_UART_DMA == 1)
  139. if (port == HAL_UART_PORT_0) return HalUARTReadDMA(buf, len);
  140. #endif
  141. #if (HAL_UART_DMA == 2)
  142. if (port == HAL_UART_PORT_1) return HalUARTReadDMA(buf, len);
  143. #endif
  144. #if (HAL_UART_ISR == 1)
  145. if (port == HAL_UART_PORT_0) return HalUARTReadISR(buf, len);
  146. #endif
  147. #if (HAL_UART_ISR == 2)
  148. if (port == HAL_UART_PORT_1) return HalUARTReadISR(buf, len);
  149. #endif
  150. #if HAL_UART_USB
  151. return HalUARTRx(buf, len);
  152. #else
  153. return 0;
  154. #endif
  155. }
  156. /******************************************************************************
  157. * @fn HalUARTWrite
  158. *
  159. * @brief Write a buffer to the UART.
  160. *
  161. * @param port - UART port
  162. * buf - pointer to the buffer that will be written, not freed
  163. * len - length of
  164. *
  165. * @return length of the buffer that was sent
  166. *****************************************************************************/
  167. uint16 HalUARTWrite(uint8 port, uint8 *buf, uint16 len)
  168. {
  169. (void)port;
  170. (void)buf;
  171. (void)len;
  172. #if (HAL_UART_DMA == 1)
  173. if (port == HAL_UART_PORT_0) return HalUARTWriteDMA(buf, len);
  174. #endif
  175. #if (HAL_UART_DMA == 2)
  176. if (port == HAL_UART_PORT_1) return HalUARTWriteDMA(buf, len);
  177. #endif
  178. #if (HAL_UART_ISR == 1)
  179. if (port == HAL_UART_PORT_0) return HalUARTWriteISR(buf, len);
  180. #endif
  181. #if (HAL_UART_ISR == 2)
  182. if (port == HAL_UART_PORT_1) return HalUARTWriteISR(buf, len);
  183. #endif
  184. #if HAL_UART_USB
  185. HalUARTTx(buf, len);
  186. return len;
  187. #else
  188. return 0;
  189. #endif
  190. }
  191. /******************************************************************************
  192. * @fn HalUARTSuspend
  193. *
  194. * @brief Suspend UART hardware before entering PM mode 1, 2 or 3.
  195. *
  196. * @param None
  197. *
  198. * @return None
  199. *****************************************************************************/
  200. void HalUARTSuspend( void )
  201. {
  202. #if HAL_UART_DMA
  203. HalUARTSuspendDMA();
  204. #endif
  205. #if HAL_UART_ISR
  206. HalUARTSuspendISR();
  207. #endif
  208. }
  209. /******************************************************************************
  210. * @fn HalUARTResume
  211. *
  212. * @brief Resume UART hardware after exiting PM mode 1, 2 or 3.
  213. *
  214. * @param None
  215. *
  216. * @return None
  217. *****************************************************************************/
  218. void HalUARTResume( void )
  219. {
  220. #if HAL_UART_DMA
  221. HalUARTResumeDMA();
  222. #endif
  223. #if HAL_UART_ISR
  224. HalUARTResumeISR();
  225. #endif
  226. }
  227. /***************************************************************************************************
  228. * @fn HalUARTPoll
  229. *
  230. * @brief Poll the UART.
  231. *
  232. * @param none
  233. *
  234. * @return none
  235. *****************************************************************************/
  236. void HalUARTPoll(void)
  237. {
  238. #if HAL_UART_DMA
  239. HalUARTPollDMA();
  240. #endif
  241. #if HAL_UART_ISR
  242. HalUARTPollISR();
  243. #endif
  244. #if HAL_UART_USB
  245. HalUARTPollUSB();
  246. #endif
  247. }
  248. /**************************************************************************************************
  249. * @fn Hal_UART_RxBufLen()
  250. *
  251. * @brief Calculate Rx Buffer length - the number of bytes in the buffer.
  252. *
  253. * @param port - UART port
  254. *
  255. * @return length of current Rx Buffer
  256. **************************************************************************************************/
  257. uint16 Hal_UART_RxBufLen( uint8 port )
  258. {
  259. (void)port;
  260. #if (HAL_UART_DMA == 1)
  261. if (port == HAL_UART_PORT_0) return HalUARTRxAvailDMA();
  262. #endif
  263. #if (HAL_UART_DMA == 2)
  264. if (port == HAL_UART_PORT_1) return HalUARTRxAvailDMA();
  265. #endif
  266. #if (HAL_UART_ISR == 1)
  267. if (port == HAL_UART_PORT_0) return HalUARTRxAvailISR();
  268. #endif
  269. #if (HAL_UART_ISR == 2)
  270. if (port == HAL_UART_PORT_1) return HalUARTRxAvailISR();
  271. #endif
  272. #if HAL_UART_USB
  273. return HalUARTRxAvailUSB();
  274. #else
  275. return 0;
  276. #endif
  277. }
  278. /******************************************************************************
  279. ******************************************************************************/