uart.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /**
  2. * \file uart.h
  3. *
  4. * \brief This file contains the function prototypes for the device
  5. * abstraction layer for UART. It also contains some
  6. * related macro definitions and some files to be included.
  7. */
  8. /*
  9. * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
  10. */
  11. /*
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the
  22. * distribution.
  23. *
  24. * Neither the name of Texas Instruments Incorporated nor the names of
  25. * its contributors may be used to endorse or promote products derived
  26. * from this software without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  31. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  33. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  34. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  35. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  36. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  37. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. */
  41. #ifndef __UART_H__
  42. #define __UART_H__
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include "hw_uart.h"
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. #define UART_TX_FIFO_LENGTH 16
  50. #define UART_RX_FIFO_LENGTH 16
  51. #define UART_MAX_TRIAL_COUNT 0x0FFF
  52. #define BAUD_115200 115200
  53. //***********************************************************************
  54. // Values that can be used by the application for configuration via APIs
  55. //***********************************************************************/
  56. /***********************************************************************/
  57. /*
  58. ** The macros below are used to set the reciever trigger levels.
  59. ** One of the macros below is passed as 'rxLevel' to'UARTFIFOLevelSet',
  60. ** as 'flags' to 'UARTDMAEnable' and to 'UARTDMADisable'.
  61. */
  62. /* This is used to select the receiver trigger level as 1 byte.*/
  63. #define UART_RX_TRIG_LEVEL_1 (UART_FCR_RXFIFTL_CHAR1 \
  64. << UART_FCR_RXFIFTL_SHIFT)
  65. /* This is used to select the receiver trigger level as 4 bytes.*/
  66. #define UART_RX_TRIG_LEVEL_4 (UART_FCR_RXFIFTL_CHAR4 \
  67. << UART_FCR_RXFIFTL_SHIFT)
  68. /* This is used to select the receiver trigger level as 8 bytes.*/
  69. #define UART_RX_TRIG_LEVEL_8 (UART_FCR_RXFIFTL_CHAR8 \
  70. << UART_FCR_RXFIFTL_SHIFT)
  71. /* This is used to select the receiver trigger level as 14 bytes.*/
  72. #define UART_RX_TRIG_LEVEL_14 (UART_FCR_RXFIFTL_CHAR14 \
  73. << UART_FCR_RXFIFTL_SHIFT)
  74. /* This is used as a mask for the Receiver Trigger Level Selection bits.*/
  75. #define UART_RX_TRIG_LEVEL UART_FCR_RXFIFTL
  76. /***************************************************************************/
  77. /***************************************************************************/
  78. /*
  79. ** These macros are used to control bits in FIFO Control register(FCR).
  80. ** These are used in the functions: 'UARTFIFOEnable', 'UARTFIFOLevelSet'
  81. ** 'UARTDMAEnable', 'UARTDMADisable'.
  82. ** An appropriate combination of the below has to be sent as 'rxLevel' to
  83. ** 'UARTFIFOLevelSet', as 'flags' to 'UARTDMAEnable' and 'UARTDMADisable'.
  84. */
  85. /* This is used to enable/disable the DMA mode of operation.*/
  86. #define UART_DMAMODE UART_FCR_DMAMODE1
  87. /* This is used to clear the Transmitter FIFO and to reset the transmitter
  88. * FIFO counter.*/
  89. #define UART_TX_CLEAR UART_FCR_TXCLR
  90. /* This is used to clear the Receiver FIFO and to reset the receiver FIFO
  91. * counter.*/
  92. #define UART_RX_CLEAR UART_FCR_RXCLR
  93. /* This is used to enable/disable the FIFO mode of operation.*/
  94. #define UART_FIFO_MODE UART_FCR_FIFOEN
  95. /***************************************************************************/
  96. /************************************************************************/
  97. /*
  98. ** These macros are used to set the Parity settings.
  99. ** They are used in the functions 'UARTConfigSetExpClk', 'UARTConfigGetExpClk',
  100. ** 'UARTParityModeSet', 'UARTParityModeGet'.
  101. */
  102. /* This is used to set odd parity.*/
  103. #define UART_PARITY_ODD UART_LCR_PEN
  104. /* This is used to configure even parity.*/
  105. #define UART_PARITY_EVEN (UART_LCR_PEN | UART_LCR_EPS)
  106. /* This is used to configure odd parity with Stick Parity Enable.*/
  107. #define UART_PARITY_STICK_ODD (UART_PARITY_ODD | UART_LCR_SP)
  108. /* This is used to configure even parity with Stick Parity Enable.*/
  109. #define UART_PARITY_STICK_EVEN (UART_PARITY_EVEN | UART_LCR_SP)
  110. /* This is used to configure no parity.*/
  111. #define UART_PARITY_NONE UART_LCR_EPS_ODD
  112. /************************************************************************/
  113. /***************************************************************************/
  114. /*
  115. ** These macros are used to control bits in the Line Control Register(LCR).
  116. ** These are used in the functions: 'UARTConfigSetExpClk','UARTConfigGetExpClk'
  117. ** 'UARTParityModeSet', 'UARTParityModeGet'.
  118. ** An appropriate combination of the below has to be sent as 'config' parameter
  119. ** to 'UARTConfigSetExpClk',as 'parityMode' to 'UARTParityModeSet'.
  120. */
  121. /* This is used to select Divisor Latch Access Bit(DLAB).*/
  122. #define UART_DLAB UART_LCR_DLAB
  123. /* This is used to enable/disable Break Condition.*/
  124. #define UART_BREAK_CTRL UART_LCR_BC
  125. /* This is used to enable/disable Stick Parity Feature.*/
  126. #define UART_STICK_PARITY UART_LCR_SP
  127. /* This is used to select Even Parity /Odd parity.*/
  128. #define UART_SET_PARITY_TYPE UART_LCR_EPS
  129. /* This is used to enable/disable Parity feature.*/
  130. #define UART_PARITY UART_LCR_PEN
  131. /* This is used to select single/multiple stop bits.*/
  132. #define UART_STOP_BIT UART_LCR_STB
  133. /* This can be used as a mask for word length selection bits.*/
  134. #define UART_WORDL UART_LCR_WLS
  135. /* This is used to select character length as 5 bits per frame.*/
  136. #define UART_WORDL_5BITS UART_LCR_WLS_5BITS
  137. /* This is used to select character length as 6 bits per frame.*/
  138. #define UART_WORDL_6BITS UART_LCR_WLS_6BITS
  139. /* This is used to select character lenghth as 7 bits per frame.*/
  140. #define UART_WORDL_7BITS UART_LCR_WLS_7BITS
  141. /* This is used to select character length as 8 bits per frame.*/
  142. #define UART_WORDL_8BITS UART_LCR_WLS_8BITS
  143. /****************************************************************************/
  144. /*****************************************************************************/
  145. /*
  146. ** These macros are used to analyze bits in Line Status Register(LSR).
  147. ** These are used in the functions: 'UARTSpaceAvail','UARTCharsAvail',
  148. ** 'UARTCharGet','UARTCharGetNonBlocking'.'UARTCharPut','UARTCharPutNonBlocking'
  149. ** 'UARTRxErrorGet', 'UARTRxErrorGet'.
  150. */
  151. /* This is used to identify if Receiver FIFO error has occured. */
  152. #define UART_RXFIFO_ERROR UART_LSR_RXFIFOE
  153. /* This is used to identify if both the Transmitter Holding Register(THR) and
  154. * Transmitter Shift Register(TSR) are empty.*/
  155. #define UART_THR_TSR_EMPTY UART_LSR_TEMT
  156. /* This is used to identify if the THR alone is empty.*/
  157. #define UART_THR_EMPTY UART_LSR_THRE
  158. /* This is used to identify if the Break Indicator bit is set.*/
  159. #define UART_BREAK_IND UART_LSR_BI
  160. /* This is used to identify if Framing Error has occured.*/
  161. #define UART_FRAME_ERROR UART_LSR_FE
  162. /* This is used to identify if Parity Error has occured.*/
  163. #define UART_PARITY_ERROR UART_LSR_PE
  164. /* This is used to identify if Overrun Error has occured.*/
  165. #define UART_OVERRUN_ERROR UART_LSR_OE
  166. /* This is used to identify if atleast one full byte is present in the Receiver
  167. * FIFO(RBR register for non-FIFO mode) and ready to be read.*/
  168. #define UART_DATA_READY UART_LSR_DR
  169. /*******************************************************************************/
  170. /*******************************************************************************/
  171. /*
  172. ** These macros are used to enable/disable interrupts in the Interrupt Enable
  173. ** Register(IER).
  174. ** These are used in functions: 'UARTIntEnable', 'UARTIntDisable'.
  175. ** An approrpriate combination of the below has to be passed as 'intFlags'
  176. ** to 'UARTIntEnable'and also to 'UARTIntDisable'.
  177. */
  178. /* This is used to enable/disable Modem Status Interrupt.*/
  179. #define UART_INT_MODEM_STAT UART_IER_EDSSI
  180. /* This is used to enable/disable Line Status Interrupt.*/
  181. #define UART_INT_LINE_STAT UART_IER_ELSI
  182. /* This is used to enable/disable Transmitter Empty Interrupt.*/
  183. #define UART_INT_TX_EMPTY UART_IER_ETBEI
  184. /* This is used to enable/disable Receiver Data Available and Character Timeout
  185. Interrupt*/
  186. #define UART_INT_RXDATA_CTI UART_IER_ERBI
  187. /************************************************************************/
  188. /***************************************************************************/
  189. /*
  190. ** These macros are used to identify what events have generated interrupts.
  191. ** These are used in the function: 'UARTIntStatus'.
  192. ** One of the macro below is returned by 'UARTIntStatus'. Refer to
  193. ** definition of 'UARTIntStatus' function for more information.
  194. */
  195. /* This is used to identify whether FIFO mode is enabled or not.*/
  196. #define UART_FIFOEN_STAT UART_IIR_FIFOEN
  197. /* This can be used as a mask for the Interrupt Identification(INTID) bits in
  198. * the Interrupt Identification Register(IIR).*/
  199. #define UART_INTID UART_IIR_INTID
  200. /* This is used to identify whether Transmitter Empty event has generated an
  201. ** interrupt.
  202. */
  203. #define UART_INTID_TX_EMPTY UART_IIR_INTID_THRE
  204. /* This is used to identify whether Receiver Data Available event has generated
  205. * an interrupt.*/
  206. #define UART_INTID_RX_DATA UART_IIR_INTID_RDA
  207. /* This is used to identify whether Receiver Line Status event has generated
  208. * an interrupt.*/
  209. #define UART_INTID_RX_LINE_STAT UART_IIR_INTID_RLS
  210. /* This is used to identify whether Character Timeout event has generated an
  211. * interrupt.*/
  212. #define UART_INTID_CTI UART_IIR_INTID_CTI
  213. /* This is used to identify whether the servicing of any interrupt is pending
  214. * or not. */
  215. #define UART_INTID_IPEND UART_IIR_IPEND
  216. /***************************************************************************/
  217. /****************************************************************************/
  218. /*
  219. ** These macros are used to control bits in the Modem Control Register(MCR).
  220. ** These are used by the functions: 'UARTModemControlSet','UARTModemControlClear'
  221. ** 'UARTModemControlGet'.
  222. ** An appropriate combination of the below is passed as 'ctrlFlags' to
  223. ** 'UARTModemControlSet' and also to 'UARTModemControlClear'.
  224. */
  225. /* This is used to select Autoflow control feature.*/
  226. #define UART_AUTOFLOW UART_MCR_AFE
  227. /* This is used to select Loopback mode feature.*/
  228. #define UART_LOOPBACK UART_MCR_LOOP
  229. /* This is used to select OUT2 control bit.*/
  230. #define UART_OUT2_CTRL UART_MCR_OUT2
  231. /* This is used to select OUT1 control bit.*/
  232. #define UART_OUT1_CTRL UART_MCR_OUT1
  233. /* This is used to select Request To Send(RTS) bit.*/
  234. #define UART_RTS UART_MCR_RTS
  235. /****************************************************************************/
  236. /****************************************************************************/
  237. /* These macros are used to analyze the bits in Modem Status Register(MSR).
  238. ** An appropriate combination of the macros below is used in the function:
  239. ** 'UARTModemStatusGet'.
  240. */
  241. #define UART_CD UART_MSR_CD
  242. #define UART_RI UART_MSR_RI
  243. #define UART_DSR UART_MSR_DSR
  244. #define UART_CTS UART_MSR_CTS
  245. #define UART_DCD UART_MSR_DCD
  246. #define UART_TERI UART_MSR_TERI
  247. #define UART_DDSR UART_MSR_DDSR
  248. #define UART_DCTS UART_MSR_DCTS
  249. /****************************************************************************/
  250. /*
  251. ** These macros are used to control bits in Power and Emulation Management
  252. ** Register(PWREMU_MGMT).
  253. ** These are used in the functions: 'UARTEnable', 'UARTDisable'.
  254. */
  255. /* This is used to reset and enable/disable the Transmitter.*/
  256. #define UART_TX_RST_ENABLE UART_PWREMU_MGMT_UTRST
  257. /* This is used to reset and enable/disable the Receiver.*/
  258. #define UART_RX_RST_ENABLE UART_PWREMU_MGMT_URRST
  259. /* This is used to enable/disable Free Running Mode of operation. */
  260. #define UART_FREE_MODE UART_PWREMU_MGMT_FREE
  261. /****************************************************************************/
  262. /****************************************************************************/
  263. /*
  264. ** These macros are used to control bits in Mode Definition Register(MDR).
  265. ** These are used in the functions: 'UARTConfigSetExpClk',
  266. ** 'UARTConfigGetExpClk'
  267. */
  268. /* This is used to identify the Over-sampling rate being set.*/
  269. #define UART_OVER_SAMP_RATE UART_MDR_OSM_SEL
  270. /* This is used to select Over-sampling rate as 16. */
  271. #define UART_OVER_SAMP_RATE_16 UART_MDR_OSM_SEL_SHIFT
  272. /* This is used to select Over-sampling rate as 13.*/
  273. #define UART_OVER_SAMP_RATE_13 UART_MDR_OSM_SEL
  274. /*******************************************************************************/
  275. //**********************************************************************
  276. // API FUNCTION PROTOTYPES
  277. //**********************************************************************/
  278. void UARTConfigSetExpClk (unsigned int baseAdd, unsigned int uartClk,
  279. unsigned int baudrate, unsigned int config,
  280. unsigned int overSampRate);
  281. void UARTConfigGetExpClk (unsigned int baseAdd, unsigned int uartClk,
  282. unsigned int *pBaud, unsigned int *pConfig);
  283. void UARTFIFOLevelSet (unsigned int baseAdd, unsigned int rxLevel);
  284. void UARTParityModeSet(unsigned int baseAdd, unsigned int parityMode);
  285. unsigned int UARTParityModeGet(unsigned int baseAdd);
  286. void UARTEnable (unsigned int baseAdd);
  287. void UARTDisable (unsigned int baseAdd);
  288. void UARTFIFOEnable(unsigned int baseAdd);
  289. void UARTFIFODisable(unsigned int baseAdd);
  290. unsigned int UARTCharsAvail(unsigned int baseAdd);
  291. unsigned int UARTSpaceAvail(unsigned int baseAdd);
  292. unsigned int UARTCharPutNonBlocking(unsigned int baseAdd,
  293. unsigned char byteWrite);
  294. int UARTCharGetNonBlocking(unsigned int baseAdd);
  295. void UARTIntEnable( unsigned int baseAdd, unsigned int intFlags);
  296. void UARTIntDisable(unsigned int baseAdd, unsigned int intFlags);
  297. unsigned int UARTIntStatus(unsigned int baseAdd);
  298. int UARTCharGet(unsigned int baseAdd);
  299. void UARTCharPut(unsigned int baseAdd, unsigned char byteTx);
  300. void UARTBreakCtl(unsigned int baseAdd, unsigned int breakState);
  301. void UARTModemControlSet(unsigned int baseAdd, unsigned int ctrlFlags);
  302. void UARTModemControlClear(unsigned int baseAdd, unsigned int ctrlFlags);
  303. unsigned int UARTModemControlGet(unsigned int baseAdd);
  304. unsigned int UARTModemStatusGet(unsigned int baseAdd);
  305. unsigned int UARTRxErrorGet(unsigned int baseAdd);
  306. void UARTDMAEnable (unsigned int baseAdd, unsigned int flags);
  307. void UARTDMADisable (unsigned int baseAdd, unsigned int flags);
  308. #ifdef __cplusplus
  309. }
  310. #endif
  311. #endif