uart.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  1. //*****************************************************************************
  2. //
  3. // uart.c - Driver for the UART.
  4. //
  5. // Copyright (c) 2005-2020 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. //
  12. // Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. //
  15. // Redistributions in binary form must reproduce the above copyright
  16. // notice, this list of conditions and the following disclaimer in the
  17. // documentation and/or other materials provided with the
  18. // distribution.
  19. //
  20. // Neither the name of Texas Instruments Incorporated nor the names of
  21. // its contributors may be used to endorse or promote products derived
  22. // from this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. // This is part of revision 2.2.0.295 of the Tiva Peripheral Driver Library.
  37. //
  38. //*****************************************************************************
  39. //*****************************************************************************
  40. //
  41. //! \addtogroup uart_api
  42. //! @{
  43. //
  44. //*****************************************************************************
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include "inc/hw_ints.h"
  48. #include "inc/hw_memmap.h"
  49. #include "inc/hw_sysctl.h"
  50. #include "inc/hw_types.h"
  51. #include "inc/hw_uart.h"
  52. #include "driverlib/debug.h"
  53. #include "driverlib/interrupt.h"
  54. #include "driverlib/uart.h"
  55. //*****************************************************************************
  56. //
  57. // The system clock divider defining the maximum baud rate supported by the
  58. // UART.
  59. //
  60. //*****************************************************************************
  61. #define UART_CLK_DIVIDER 8
  62. //*****************************************************************************
  63. //
  64. // A mapping of UART base address to interrupt number.
  65. //
  66. //*****************************************************************************
  67. static const uint32_t g_ppui32UARTIntMap[][2] =
  68. {
  69. { UART0_BASE, INT_UART0_TM4C123 },
  70. { UART1_BASE, INT_UART1_TM4C123 },
  71. { UART2_BASE, INT_UART2_TM4C123 },
  72. { UART3_BASE, INT_UART3_TM4C123 },
  73. { UART4_BASE, INT_UART4_TM4C123 },
  74. { UART5_BASE, INT_UART5_TM4C123 },
  75. { UART6_BASE, INT_UART6_TM4C123 },
  76. { UART7_BASE, INT_UART7_TM4C123 },
  77. };
  78. static const uint_fast8_t g_ui8UARTIntMapRows =
  79. sizeof(g_ppui32UARTIntMap) / sizeof(g_ppui32UARTIntMap[0]);
  80. static const uint32_t g_ppui32UARTIntMapSnowflake[][2] =
  81. {
  82. { UART0_BASE, INT_UART0_TM4C129 },
  83. { UART1_BASE, INT_UART1_TM4C129 },
  84. { UART2_BASE, INT_UART2_TM4C129 },
  85. { UART3_BASE, INT_UART3_TM4C129 },
  86. { UART4_BASE, INT_UART4_TM4C129 },
  87. { UART5_BASE, INT_UART5_TM4C129 },
  88. { UART6_BASE, INT_UART6_TM4C129 },
  89. { UART7_BASE, INT_UART7_TM4C129 },
  90. };
  91. static const uint_fast8_t g_ui8UARTIntMapRowsSnowflake =
  92. sizeof(g_ppui32UARTIntMapSnowflake) /
  93. sizeof(g_ppui32UARTIntMapSnowflake[0]);
  94. //*****************************************************************************
  95. //
  96. //! \internal
  97. //! Checks a UART base address.
  98. //!
  99. //! \param ui32Base is the base address of the UART port.
  100. //!
  101. //! This function determines if a UART port base address is valid.
  102. //!
  103. //! \return Returns \b true if the base address is valid and \b false
  104. //! otherwise.
  105. //
  106. //*****************************************************************************
  107. #ifdef DEBUG
  108. static bool
  109. _UARTBaseValid(uint32_t ui32Base)
  110. {
  111. return((ui32Base == UART0_BASE) || (ui32Base == UART1_BASE) ||
  112. (ui32Base == UART2_BASE) || (ui32Base == UART3_BASE) ||
  113. (ui32Base == UART4_BASE) || (ui32Base == UART5_BASE) ||
  114. (ui32Base == UART6_BASE) || (ui32Base == UART7_BASE));
  115. }
  116. #endif
  117. //*****************************************************************************
  118. //
  119. //! \internal
  120. //! Gets the UART interrupt number.
  121. //!
  122. //! \param ui32Base is the base address of the UART port.
  123. //!
  124. //! Given a UART base address, this function returns the corresponding
  125. //! interrupt number.
  126. //!
  127. //! \return Returns a UART interrupt number, or 0 if \e ui32Base is invalid.
  128. //
  129. //*****************************************************************************
  130. static uint32_t
  131. _UARTIntNumberGet(uint32_t ui32Base)
  132. {
  133. uint_fast8_t ui8Idx, ui8Rows;
  134. const uint32_t (*ppui32UARTIntMap)[2];
  135. //
  136. // Default interrupt map.
  137. //
  138. ppui32UARTIntMap = g_ppui32UARTIntMap;
  139. ui8Rows = g_ui8UARTIntMapRows;
  140. if(CLASS_IS_TM4C129)
  141. {
  142. ppui32UARTIntMap = g_ppui32UARTIntMapSnowflake;
  143. ui8Rows = g_ui8UARTIntMapRowsSnowflake;
  144. }
  145. //
  146. // Loop through the table that maps UART base addresses to interrupt
  147. // numbers.
  148. //
  149. for(ui8Idx = 0; ui8Idx < ui8Rows; ui8Idx++)
  150. {
  151. //
  152. // See if this base address matches.
  153. //
  154. if(ppui32UARTIntMap[ui8Idx][0] == ui32Base)
  155. {
  156. //
  157. // Return the corresponding interrupt number.
  158. //
  159. return(ppui32UARTIntMap[ui8Idx][1]);
  160. }
  161. }
  162. //
  163. // The base address could not be found, so return an error.
  164. //
  165. return(0);
  166. }
  167. //*****************************************************************************
  168. //
  169. //! Sets the type of parity.
  170. //!
  171. //! \param ui32Base is the base address of the UART port.
  172. //! \param ui32Parity specifies the type of parity to use.
  173. //!
  174. //! This function configures the type of parity to use for transmitting and
  175. //! expect when receiving. The \e ui32Parity parameter must be one of
  176. //! \b UART_CONFIG_PAR_NONE, \b UART_CONFIG_PAR_EVEN, \b UART_CONFIG_PAR_ODD,
  177. //! \b UART_CONFIG_PAR_ONE, or \b UART_CONFIG_PAR_ZERO. The last two
  178. //! parameters allow direct control of the parity bit; it is always either one
  179. //! or zero based on the mode.
  180. //!
  181. //! \return None.
  182. //
  183. //*****************************************************************************
  184. void
  185. UARTParityModeSet(uint32_t ui32Base, uint32_t ui32Parity)
  186. {
  187. //
  188. // Check the arguments.
  189. //
  190. ASSERT(_UARTBaseValid(ui32Base));
  191. ASSERT((ui32Parity == UART_CONFIG_PAR_NONE) ||
  192. (ui32Parity == UART_CONFIG_PAR_EVEN) ||
  193. (ui32Parity == UART_CONFIG_PAR_ODD) ||
  194. (ui32Parity == UART_CONFIG_PAR_ONE) ||
  195. (ui32Parity == UART_CONFIG_PAR_ZERO));
  196. //
  197. // Set the parity mode.
  198. //
  199. HWREG(ui32Base + UART_O_LCRH) = ((HWREG(ui32Base + UART_O_LCRH) &
  200. ~(UART_LCRH_SPS | UART_LCRH_EPS |
  201. UART_LCRH_PEN)) | ui32Parity);
  202. }
  203. //*****************************************************************************
  204. //
  205. //! Gets the type of parity currently being used.
  206. //!
  207. //! \param ui32Base is the base address of the UART port.
  208. //!
  209. //! This function gets the type of parity used for transmitting data and
  210. //! expected when receiving data.
  211. //!
  212. //! \return Returns the current parity settings, specified as one of
  213. //! \b UART_CONFIG_PAR_NONE, \b UART_CONFIG_PAR_EVEN, \b UART_CONFIG_PAR_ODD,
  214. //! \b UART_CONFIG_PAR_ONE, or \b UART_CONFIG_PAR_ZERO.
  215. //
  216. //*****************************************************************************
  217. uint32_t
  218. UARTParityModeGet(uint32_t ui32Base)
  219. {
  220. //
  221. // Check the arguments.
  222. //
  223. ASSERT(_UARTBaseValid(ui32Base));
  224. //
  225. // Return the current parity setting.
  226. //
  227. return(HWREG(ui32Base + UART_O_LCRH) &
  228. (UART_LCRH_SPS | UART_LCRH_EPS | UART_LCRH_PEN));
  229. }
  230. //*****************************************************************************
  231. //
  232. //! Sets the FIFO level at which interrupts are generated.
  233. //!
  234. //! \param ui32Base is the base address of the UART port.
  235. //! \param ui32TxLevel is the transmit FIFO interrupt level, specified as one
  236. //! of \b UART_FIFO_TX1_8, \b UART_FIFO_TX2_8, \b UART_FIFO_TX4_8,
  237. //! \b UART_FIFO_TX6_8, or \b UART_FIFO_TX7_8.
  238. //! \param ui32RxLevel is the receive FIFO interrupt level, specified as one of
  239. //! \b UART_FIFO_RX1_8, \b UART_FIFO_RX2_8, \b UART_FIFO_RX4_8,
  240. //! \b UART_FIFO_RX6_8, or \b UART_FIFO_RX7_8.
  241. //!
  242. //! This function configures the FIFO level at which transmit and receive
  243. //! interrupts are generated.
  244. //!
  245. //! \return None.
  246. //
  247. //*****************************************************************************
  248. void
  249. UARTFIFOLevelSet(uint32_t ui32Base, uint32_t ui32TxLevel,
  250. uint32_t ui32RxLevel)
  251. {
  252. //
  253. // Check the arguments.
  254. //
  255. ASSERT(_UARTBaseValid(ui32Base));
  256. ASSERT((ui32TxLevel == UART_FIFO_TX1_8) ||
  257. (ui32TxLevel == UART_FIFO_TX2_8) ||
  258. (ui32TxLevel == UART_FIFO_TX4_8) ||
  259. (ui32TxLevel == UART_FIFO_TX6_8) ||
  260. (ui32TxLevel == UART_FIFO_TX7_8));
  261. ASSERT((ui32RxLevel == UART_FIFO_RX1_8) ||
  262. (ui32RxLevel == UART_FIFO_RX2_8) ||
  263. (ui32RxLevel == UART_FIFO_RX4_8) ||
  264. (ui32RxLevel == UART_FIFO_RX6_8) ||
  265. (ui32RxLevel == UART_FIFO_RX7_8));
  266. //
  267. // Set the FIFO interrupt levels.
  268. //
  269. HWREG(ui32Base + UART_O_IFLS) = ui32TxLevel | ui32RxLevel;
  270. }
  271. //*****************************************************************************
  272. //
  273. //! Gets the FIFO level at which interrupts are generated.
  274. //!
  275. //! \param ui32Base is the base address of the UART port.
  276. //! \param pui32TxLevel is a pointer to storage for the transmit FIFO level,
  277. //! returned as one of \b UART_FIFO_TX1_8, \b UART_FIFO_TX2_8,
  278. //! \b UART_FIFO_TX4_8, \b UART_FIFO_TX6_8, or \b UART_FIFO_TX7_8.
  279. //! \param pui32RxLevel is a pointer to storage for the receive FIFO level,
  280. //! returned as one of \b UART_FIFO_RX1_8, \b UART_FIFO_RX2_8,
  281. //! \b UART_FIFO_RX4_8, \b UART_FIFO_RX6_8, or \b UART_FIFO_RX7_8.
  282. //!
  283. //! This function gets the FIFO level at which transmit and receive interrupts
  284. //! are generated.
  285. //!
  286. //! \return None.
  287. //
  288. //*****************************************************************************
  289. void
  290. UARTFIFOLevelGet(uint32_t ui32Base, uint32_t *pui32TxLevel,
  291. uint32_t *pui32RxLevel)
  292. {
  293. uint32_t ui32Temp;
  294. //
  295. // Check the arguments.
  296. //
  297. ASSERT(_UARTBaseValid(ui32Base));
  298. //
  299. // Read the FIFO level register.
  300. //
  301. ui32Temp = HWREG(ui32Base + UART_O_IFLS);
  302. //
  303. // Extract the transmit and receive FIFO levels.
  304. //
  305. *pui32TxLevel = ui32Temp & UART_IFLS_TX_M;
  306. *pui32RxLevel = ui32Temp & UART_IFLS_RX_M;
  307. }
  308. //*****************************************************************************
  309. //
  310. //! Sets the configuration of a UART.
  311. //!
  312. //! \param ui32Base is the base address of the UART port.
  313. //! \param ui32UARTClk is the rate of the clock supplied to the UART module.
  314. //! \param ui32Baud is the desired baud rate.
  315. //! \param ui32Config is the data format for the port (number of data bits,
  316. //! number of stop bits, and parity).
  317. //!
  318. //! This function configures the UART for operation in the specified data
  319. //! format. The baud rate is provided in the \e ui32Baud parameter and the
  320. //! data format in the \e ui32Config parameter.
  321. //!
  322. //! The \e ui32Config parameter is the logical OR of three values: the number
  323. //! of data bits, the number of stop bits, and the parity.
  324. //! \b UART_CONFIG_WLEN_8, \b UART_CONFIG_WLEN_7, \b UART_CONFIG_WLEN_6, and
  325. //! \b UART_CONFIG_WLEN_5 select from eight to five data bits per byte
  326. //! (respectively). \b UART_CONFIG_STOP_ONE and \b UART_CONFIG_STOP_TWO select
  327. //! one or two stop bits (respectively). \b UART_CONFIG_PAR_NONE,
  328. //! \b UART_CONFIG_PAR_EVEN, \b UART_CONFIG_PAR_ODD, \b UART_CONFIG_PAR_ONE,
  329. //! and \b UART_CONFIG_PAR_ZERO select the parity mode (no parity bit, even
  330. //! parity bit, odd parity bit, parity bit always one, and parity bit always
  331. //! zero, respectively).
  332. //!
  333. //! The peripheral clock is the same as the processor clock. The frequency of
  334. //! the system clock is the value returned by SysCtlClockGet() for TM4C123x
  335. //! devices or the value returned by SysCtlClockFreqSet() for TM4C129x devices,
  336. //! or it can be explicitly hard coded if it is constant and known (to save the
  337. //! code/execution overhead of a call to SysCtlClockGet() or fetch of the
  338. //! variable call holding the return value of SysCtlClockFreqSet()).
  339. //!
  340. //! The function disables the UART by calling UARTDisable() before changing the
  341. //! the parameters and enables the UART by calling UARTEnable().
  342. //!
  343. //! For Tiva parts that have the ability to specify the UART baud clock
  344. //! source (via UARTClockSourceSet()), the peripheral clock can be changed to
  345. //! PIOSC. In this case, the peripheral clock should be specified as
  346. //! 16,000,000 (the nominal rate of PIOSC).
  347. //!
  348. //! \return None.
  349. //
  350. //*****************************************************************************
  351. void
  352. UARTConfigSetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk,
  353. uint32_t ui32Baud, uint32_t ui32Config)
  354. {
  355. uint32_t ui32Div;
  356. //
  357. // Check the arguments.
  358. //
  359. ASSERT(_UARTBaseValid(ui32Base));
  360. ASSERT(ui32Baud != 0);
  361. ASSERT(ui32UARTClk >= (ui32Baud * UART_CLK_DIVIDER));
  362. //
  363. // Stop the UART.
  364. //
  365. UARTDisable(ui32Base);
  366. //
  367. // Is the required baud rate greater than the maximum rate supported
  368. // without the use of high speed mode?
  369. //
  370. if((ui32Baud * 16) > ui32UARTClk)
  371. {
  372. //
  373. // Enable high speed mode.
  374. //
  375. HWREG(ui32Base + UART_O_CTL) |= UART_CTL_HSE;
  376. //
  377. // Half the supplied baud rate to compensate for enabling high speed
  378. // mode. This allows the following code to be common to both cases.
  379. //
  380. ui32Baud /= 2;
  381. }
  382. else
  383. {
  384. //
  385. // Disable high speed mode.
  386. //
  387. HWREG(ui32Base + UART_O_CTL) &= ~(UART_CTL_HSE);
  388. }
  389. //
  390. // Compute the fractional baud rate divider.
  391. //
  392. ui32Div = (((ui32UARTClk * 8) / ui32Baud) + 1) / 2;
  393. //
  394. // Set the baud rate.
  395. //
  396. HWREG(ui32Base + UART_O_IBRD) = ui32Div / 64;
  397. HWREG(ui32Base + UART_O_FBRD) = ui32Div % 64;
  398. //
  399. // Set parity, data length, and number of stop bits.
  400. //
  401. HWREG(ui32Base + UART_O_LCRH) = ui32Config;
  402. //
  403. // Clear the flags register.
  404. //
  405. HWREG(ui32Base + UART_O_FR) = 0;
  406. //
  407. // Start the UART.
  408. //
  409. UARTEnable(ui32Base);
  410. }
  411. //*****************************************************************************
  412. //
  413. //! Gets the current configuration of a UART.
  414. //!
  415. //! \param ui32Base is the base address of the UART port.
  416. //! \param ui32UARTClk is the rate of the clock supplied to the UART module.
  417. //! \param pui32Baud is a pointer to storage for the baud rate.
  418. //! \param pui32Config is a pointer to storage for the data format.
  419. //!
  420. //! This function determines the baud rate and data format for the UART, given
  421. //! an explicitly provided peripheral clock (hence the ExpClk suffix). The
  422. //! returned baud rate is the actual baud rate; it may not be the exact baud
  423. //! rate requested or an ``official'' baud rate. The data format returned in
  424. //! \e pui32Config is enumerated the same as the \e ui32Config parameter of
  425. //! UARTConfigSetExpClk().
  426. //!
  427. //! The peripheral clock is the same as the processor clock. The frequency of
  428. //! the system clock is the value returned by SysCtlClockGet() for TM4C123x
  429. //! devices or the value returned by SysCtlClockFreqSet() for TM4C129x devices,
  430. //! or it can be explicitly hard coded if it is constant and known (to save the
  431. //! code/execution overhead of a call to SysCtlClockGet() or fetch of the
  432. //! variable call holding the return value of SysCtlClockFreqSet()).
  433. //!
  434. //! For Tiva parts that have the ability to specify the UART baud clock
  435. //! source (via UARTClockSourceSet()), the peripheral clock can be changed to
  436. //! PIOSC. In this case, the peripheral clock should be specified as
  437. //! 16,000,000 (the nominal rate of PIOSC).
  438. //!
  439. //! \return None.
  440. //
  441. //*****************************************************************************
  442. void
  443. UARTConfigGetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk,
  444. uint32_t *pui32Baud, uint32_t *pui32Config)
  445. {
  446. uint32_t ui32Int, ui32Frac;
  447. //
  448. // Check the arguments.
  449. //
  450. ASSERT(_UARTBaseValid(ui32Base));
  451. //
  452. // Compute the baud rate.
  453. //
  454. ui32Int = HWREG(ui32Base + UART_O_IBRD);
  455. ui32Frac = HWREG(ui32Base + UART_O_FBRD);
  456. *pui32Baud = (ui32UARTClk * 4) / ((64 * ui32Int) + ui32Frac);
  457. //
  458. // See if high speed mode enabled.
  459. //
  460. if(HWREG(ui32Base + UART_O_CTL) & UART_CTL_HSE)
  461. {
  462. //
  463. // High speed mode is enabled so the actual baud rate is actually
  464. // double what was just calculated.
  465. //
  466. *pui32Baud *= 2;
  467. }
  468. //
  469. // Get the parity, data length, and number of stop bits.
  470. //
  471. *pui32Config = (HWREG(ui32Base + UART_O_LCRH) &
  472. (UART_LCRH_SPS | UART_LCRH_WLEN_M | UART_LCRH_STP2 |
  473. UART_LCRH_EPS | UART_LCRH_PEN));
  474. }
  475. //*****************************************************************************
  476. //
  477. //! Enables transmitting and receiving.
  478. //!
  479. //! \param ui32Base is the base address of the UART port.
  480. //!
  481. //! This function enables the UART and its transmit and receive FIFOs.
  482. //!
  483. //! \return None.
  484. //
  485. //*****************************************************************************
  486. void
  487. UARTEnable(uint32_t ui32Base)
  488. {
  489. //
  490. // Check the arguments.
  491. //
  492. ASSERT(_UARTBaseValid(ui32Base));
  493. //
  494. // Enable the FIFO.
  495. //
  496. HWREG(ui32Base + UART_O_LCRH) |= UART_LCRH_FEN;
  497. //
  498. // Enable RX, TX, and the UART.
  499. //
  500. HWREG(ui32Base + UART_O_CTL) |= (UART_CTL_UARTEN | UART_CTL_TXE |
  501. UART_CTL_RXE);
  502. }
  503. //*****************************************************************************
  504. //
  505. //! Disables transmitting and receiving.
  506. //!
  507. //! \param ui32Base is the base address of the UART port.
  508. //!
  509. //! This function disables the UART, waits for the end of transmission of the
  510. //! current character, and flushes the transmit FIFO.
  511. //!
  512. //! \return None.
  513. //
  514. //*****************************************************************************
  515. void
  516. UARTDisable(uint32_t ui32Base)
  517. {
  518. //
  519. // Check the arguments.
  520. //
  521. ASSERT(_UARTBaseValid(ui32Base));
  522. //
  523. // Wait for end of TX.
  524. //
  525. while(HWREG(ui32Base + UART_O_FR) & UART_FR_BUSY)
  526. {
  527. }
  528. //
  529. // Disable the FIFO.
  530. //
  531. HWREG(ui32Base + UART_O_LCRH) &= ~(UART_LCRH_FEN);
  532. //
  533. // Disable the UART.
  534. //
  535. HWREG(ui32Base + UART_O_CTL) &= ~(UART_CTL_UARTEN | UART_CTL_TXE |
  536. UART_CTL_RXE);
  537. }
  538. //*****************************************************************************
  539. //
  540. //! Enables the transmit and receive FIFOs.
  541. //!
  542. //! \param ui32Base is the base address of the UART port.
  543. //!
  544. //! This functions enables the transmit and receive FIFOs in the UART.
  545. //!
  546. //! \return None.
  547. //
  548. //*****************************************************************************
  549. void
  550. UARTFIFOEnable(uint32_t ui32Base)
  551. {
  552. //
  553. // Check the arguments.
  554. //
  555. ASSERT(_UARTBaseValid(ui32Base));
  556. //
  557. // Enable the FIFO.
  558. //
  559. HWREG(ui32Base + UART_O_LCRH) |= UART_LCRH_FEN;
  560. }
  561. //*****************************************************************************
  562. //
  563. //! Disables the transmit and receive FIFOs.
  564. //!
  565. //! \param ui32Base is the base address of the UART port.
  566. //!
  567. //! This function disables the transmit and receive FIFOs in the UART.
  568. //!
  569. //! \return None.
  570. //
  571. //*****************************************************************************
  572. void
  573. UARTFIFODisable(uint32_t ui32Base)
  574. {
  575. //
  576. // Check the arguments.
  577. //
  578. ASSERT(_UARTBaseValid(ui32Base));
  579. //
  580. // Disable the FIFO.
  581. //
  582. HWREG(ui32Base + UART_O_LCRH) &= ~(UART_LCRH_FEN);
  583. }
  584. //*****************************************************************************
  585. //
  586. //! Enables SIR (IrDA) mode on the specified UART.
  587. //!
  588. //! \param ui32Base is the base address of the UART port.
  589. //! \param bLowPower indicates if SIR Low Power Mode is to be used.
  590. //!
  591. //! This function enables SIR (IrDA) mode on the UART. If the \e bLowPower
  592. //! flag is set, then SIR low power mode will be selected as well. This
  593. //! function only has an effect if the UART has not been enabled by a call to
  594. //! UARTEnable(). The call UARTEnableSIR() must be made before a call to
  595. //! UARTConfigSetExpClk() because the UARTConfigSetExpClk() function calls the
  596. //! UARTEnable() function. Another option is to call UARTDisable() followed by
  597. //! UARTEnableSIR() and then enable the UART by calling UARTEnable().
  598. //!
  599. //! \note The availability of SIR (IrDA) operation varies with the Tiva
  600. //! part in use. Please consult the datasheet for the part you are using to
  601. //! determine whether this support is available.
  602. //!
  603. //! \return None.
  604. //
  605. //*****************************************************************************
  606. void
  607. UARTEnableSIR(uint32_t ui32Base, bool bLowPower)
  608. {
  609. //
  610. // Check the arguments.
  611. //
  612. ASSERT(_UARTBaseValid(ui32Base));
  613. //
  614. // Enable SIR and SIRLP (if appropriate).
  615. //
  616. if(bLowPower)
  617. {
  618. HWREG(ui32Base + UART_O_CTL) |= (UART_CTL_SIREN | UART_CTL_SIRLP);
  619. }
  620. else
  621. {
  622. HWREG(ui32Base + UART_O_CTL) |= (UART_CTL_SIREN);
  623. }
  624. }
  625. //*****************************************************************************
  626. //
  627. //! Disables SIR (IrDA) mode on the specified UART.
  628. //!
  629. //! \param ui32Base is the base address of the UART port.
  630. //!
  631. //! This function disables SIR(IrDA) mode on the UART. This function only has
  632. //! an effect if the UART has not been enabled by a call to UARTEnable(). The
  633. //! call UARTEnableSIR() must be made before a call to UARTConfigSetExpClk()
  634. //! because the UARTConfigSetExpClk() function calls the UARTEnable() function.
  635. //! Another option is to call UARTDisable() followed by UARTEnableSIR() and
  636. //! then enable the UART by calling UARTEnable().
  637. //!
  638. //! \note The availability of SIR (IrDA) operation varies with the Tiva
  639. //! part in use. Please consult the datasheet for the part you are using to
  640. //! determine whether this support is available.
  641. //!
  642. //! \return None.
  643. //
  644. //*****************************************************************************
  645. void
  646. UARTDisableSIR(uint32_t ui32Base)
  647. {
  648. //
  649. // Check the arguments.
  650. //
  651. ASSERT(_UARTBaseValid(ui32Base));
  652. //
  653. // Disable SIR and SIRLP (if appropriate).
  654. //
  655. HWREG(ui32Base + UART_O_CTL) &= ~(UART_CTL_SIREN | UART_CTL_SIRLP);
  656. }
  657. //*****************************************************************************
  658. //
  659. //! Enables ISO7816 smart card mode on the specified UART.
  660. //!
  661. //! \param ui32Base is the base address of the UART port.
  662. //!
  663. //! This function enables the SMART control bit for the ISO7816 smart card mode
  664. //! on the UART. This call also sets 8-bit word length and even parity as
  665. //! required by ISO7816.
  666. //!
  667. //! \note The availability of ISO7816 smart card mode varies with the Tiva
  668. //! part and UART in use. Please consult the datasheet for the part you are
  669. //! using to determine whether this support is available.
  670. //!
  671. //! \return None.
  672. //
  673. //*****************************************************************************
  674. void
  675. UARTSmartCardEnable(uint32_t ui32Base)
  676. {
  677. uint32_t ui32Val;
  678. //
  679. // Check the arguments.
  680. //
  681. ASSERT(_UARTBaseValid(ui32Base));
  682. //
  683. // Set 8-bit word length, even parity, 2 stop bits (note that although the
  684. // STP2 bit is ignored when in smartcard mode, this code lets the caller
  685. // read back the actual setting in use).
  686. //
  687. ui32Val = HWREG(ui32Base + UART_O_LCRH);
  688. ui32Val &= ~(UART_LCRH_SPS | UART_LCRH_EPS | UART_LCRH_PEN |
  689. UART_LCRH_WLEN_M);
  690. ui32Val |= UART_LCRH_WLEN_8 | UART_LCRH_PEN | UART_LCRH_EPS |
  691. UART_LCRH_STP2;
  692. HWREG(ui32Base + UART_O_LCRH) = ui32Val;
  693. //
  694. // Enable SMART mode.
  695. //
  696. HWREG(ui32Base + UART_O_CTL) |= UART_CTL_SMART;
  697. }
  698. //*****************************************************************************
  699. //
  700. //! Disables ISO7816 smart card mode on the specified UART.
  701. //!
  702. //! \param ui32Base is the base address of the UART port.
  703. //!
  704. //! This function clears the SMART (ISO7816 smart card) bit in the UART
  705. //! control register.
  706. //!
  707. //! \note The availability of ISO7816 smart card mode varies with the Tiva
  708. //! part and UART in use. Please consult the datasheet for the part you are
  709. //! using to determine whether this support is available.
  710. //!
  711. //! \return None.
  712. //
  713. //*****************************************************************************
  714. void
  715. UARTSmartCardDisable(uint32_t ui32Base)
  716. {
  717. //
  718. // Check the arguments.
  719. //
  720. ASSERT(_UARTBaseValid(ui32Base));
  721. //
  722. // Disable the SMART bit.
  723. //
  724. HWREG(ui32Base + UART_O_CTL) &= ~UART_CTL_SMART;
  725. }
  726. //*****************************************************************************
  727. //
  728. //! Sets the states of the DTR and/or RTS modem control signals.
  729. //!
  730. //! \param ui32Base is the base address of the UART port.
  731. //! \param ui32Control is a bit-mapped flag indicating which modem control bits
  732. //! should be set.
  733. //!
  734. //! This function configures the states of the DTR or RTS modem handshake
  735. //! outputs from the UART.
  736. //!
  737. //! The \e ui32Control parameter is the logical OR of any of the following:
  738. //!
  739. //! - \b UART_OUTPUT_DTR - The modem control DTR signal
  740. //! - \b UART_OUTPUT_RTS - The modem control RTS signal
  741. //!
  742. //! \note The availability of hardware modem handshake signals varies with the
  743. //! Tiva part and UART in use. Please consult the datasheet for the part
  744. //! you are using to determine whether this support is available.
  745. //!
  746. //! \return None.
  747. //
  748. //*****************************************************************************
  749. void
  750. UARTModemControlSet(uint32_t ui32Base, uint32_t ui32Control)
  751. {
  752. uint32_t ui32Temp;
  753. //
  754. // Check the arguments.
  755. //
  756. #if defined(TARGET_IS_TM4C123_RA1) || defined(TARGET_IS_TM4C123_RA2) || \
  757. defined(TARGET_IS_TM4C123_RA3) || defined(TARGET_IS_TM4C123_RB0) || \
  758. defined(TARGET_IS_TM4C123_RB1)
  759. ASSERT(ui32Base == UART1_BASE);
  760. #else
  761. ASSERT((ui32Base == UART0_BASE) ||
  762. (ui32Base == UART1_BASE) ||
  763. (ui32Base == UART2_BASE) ||
  764. (ui32Base == UART3_BASE) ||
  765. (ui32Base == UART4_BASE));
  766. #endif
  767. ASSERT((ui32Control & ~(UART_OUTPUT_RTS | UART_OUTPUT_DTR)) == 0);
  768. //
  769. // Set the appropriate modem control output bits.
  770. //
  771. ui32Temp = HWREG(ui32Base + UART_O_CTL);
  772. ui32Temp |= (ui32Control & (UART_OUTPUT_RTS | UART_OUTPUT_DTR));
  773. HWREG(ui32Base + UART_O_CTL) = ui32Temp;
  774. }
  775. //*****************************************************************************
  776. //
  777. //! Clears the states of the DTR and/or RTS modem control signals.
  778. //!
  779. //! \param ui32Base is the base address of the UART port.
  780. //! \param ui32Control is a bit-mapped flag indicating which modem control bits
  781. //! should be set.
  782. //!
  783. //! This function clears the states of the DTR or RTS modem handshake outputs
  784. //! from the UART.
  785. //!
  786. //! The \e ui32Control parameter is the logical OR of any of the following:
  787. //!
  788. //! - \b UART_OUTPUT_DTR - The modem control DTR signal
  789. //! - \b UART_OUTPUT_RTS - The modem control RTS signal
  790. //!
  791. //! \note The availability of hardware modem handshake signals varies with the
  792. //! Tiva part and UART in use. Please consult the datasheet for the part
  793. //! you are using to determine whether this support is available.
  794. //!
  795. //! \return None.
  796. //
  797. //*****************************************************************************
  798. void
  799. UARTModemControlClear(uint32_t ui32Base, uint32_t ui32Control)
  800. {
  801. uint32_t ui32Temp;
  802. //
  803. // Check the arguments.
  804. //
  805. #if defined(TARGET_IS_TM4C123_RA1) || defined(TARGET_IS_TM4C123_RA2) || \
  806. defined(TARGET_IS_TM4C123_RA3) || defined(TARGET_IS_TM4C123_RB0) || \
  807. defined(TARGET_IS_TM4C123_RB1)
  808. ASSERT(ui32Base == UART1_BASE);
  809. #else
  810. ASSERT((ui32Base == UART0_BASE) ||
  811. (ui32Base == UART1_BASE) ||
  812. (ui32Base == UART2_BASE) ||
  813. (ui32Base == UART3_BASE) ||
  814. (ui32Base == UART4_BASE));
  815. #endif
  816. ASSERT((ui32Control & ~(UART_OUTPUT_RTS | UART_OUTPUT_DTR)) == 0);
  817. //
  818. // Set the appropriate modem control output bits.
  819. //
  820. ui32Temp = HWREG(ui32Base + UART_O_CTL);
  821. ui32Temp &= ~(ui32Control & (UART_OUTPUT_RTS | UART_OUTPUT_DTR));
  822. HWREG(ui32Base + UART_O_CTL) = ui32Temp;
  823. }
  824. //*****************************************************************************
  825. //
  826. //! Gets the states of the DTR and RTS modem control signals.
  827. //!
  828. //! \param ui32Base is the base address of the UART port.
  829. //!
  830. //! This function returns the current states of each of the two UART modem
  831. //! control signals, DTR and RTS.
  832. //!
  833. //! \note The availability of hardware modem handshake signals varies with the
  834. //! Tiva part and UART in use. Please consult the datasheet for the part
  835. //! you are using to determine whether this support is available.
  836. //!
  837. //! \return Returns the states of the handshake output signals. This value is
  838. //! a logical OR combination of values \b UART_OUTPUT_RTS and
  839. //! \b UART_OUTPUT_DTR where the presence of each flag indicates that the
  840. //! associated signal is asserted.
  841. //
  842. //*****************************************************************************
  843. uint32_t
  844. UARTModemControlGet(uint32_t ui32Base)
  845. {
  846. //
  847. // Check the arguments.
  848. //
  849. #if defined(TARGET_IS_TM4C123_RA1) || defined(TARGET_IS_TM4C123_RA2) || \
  850. defined(TARGET_IS_TM4C123_RA3) || defined(TARGET_IS_TM4C123_RB0) || \
  851. defined(TARGET_IS_TM4C123_RB1)
  852. ASSERT(ui32Base == UART1_BASE);
  853. #else
  854. ASSERT((ui32Base == UART0_BASE) ||
  855. (ui32Base == UART1_BASE) ||
  856. (ui32Base == UART2_BASE) ||
  857. (ui32Base == UART3_BASE) ||
  858. (ui32Base == UART4_BASE));
  859. #endif
  860. return(HWREG(ui32Base + UART_O_CTL) & (UART_OUTPUT_RTS | UART_OUTPUT_DTR));
  861. }
  862. //*****************************************************************************
  863. //
  864. //! Gets the states of the RI, DCD, DSR and CTS modem status signals.
  865. //!
  866. //! \param ui32Base is the base address of the UART port.
  867. //!
  868. //! This function returns the current states of each of the four UART modem
  869. //! status signals, RI, DCD, DSR and CTS.
  870. //!
  871. //! \note The availability of hardware modem handshake signals varies with the
  872. //! Tiva part and UART in use. Please consult the datasheet for the part
  873. //! you are using to determine whether this support is available.
  874. //!
  875. //! \return Returns the states of the handshake output signals. This value
  876. //! is a logical OR combination of values \b UART_INPUT_RI,
  877. //! \b UART_INPUT_DCD, \b UART_INPUT_CTS and \b UART_INPUT_DSR where the
  878. //! presence of each flag indicates that the associated signal is asserted.
  879. //
  880. //*****************************************************************************
  881. uint32_t
  882. UARTModemStatusGet(uint32_t ui32Base)
  883. {
  884. //
  885. // Check the arguments.
  886. //
  887. #if defined(TARGET_IS_TM4C123_RA1) || defined(TARGET_IS_TM4C123_RA2) || \
  888. defined(TARGET_IS_TM4C123_RA3) || defined(TARGET_IS_TM4C123_RB0) || \
  889. defined(TARGET_IS_TM4C123_RB1)
  890. ASSERT(ui32Base == UART1_BASE);
  891. #else
  892. ASSERT((ui32Base == UART0_BASE) ||
  893. (ui32Base == UART1_BASE) ||
  894. (ui32Base == UART2_BASE) ||
  895. (ui32Base == UART3_BASE) ||
  896. (ui32Base == UART4_BASE));
  897. #endif
  898. return(HWREG(ui32Base + UART_O_FR) & (UART_INPUT_RI | UART_INPUT_DCD |
  899. UART_INPUT_CTS | UART_INPUT_DSR));
  900. }
  901. //*****************************************************************************
  902. //
  903. //! Sets the UART hardware flow control mode to be used.
  904. //!
  905. //! \param ui32Base is the base address of the UART port.
  906. //! \param ui32Mode indicates the flow control modes to be used. This
  907. //! parameter is a logical OR combination of values \b UART_FLOWCONTROL_TX and
  908. //! \b UART_FLOWCONTROL_RX to enable hardware transmit (CTS) and receive (RTS)
  909. //! flow control or \b UART_FLOWCONTROL_NONE to disable hardware flow control.
  910. //!
  911. //! This function configures the required hardware flow control modes. If
  912. //! \e ui32Mode contains flag \b UART_FLOWCONTROL_TX, data is only transmitted
  913. //! if the incoming CTS signal is asserted. If \e ui32Mode contains flag
  914. //! \b UART_FLOWCONTROL_RX, the RTS output is controlled by the hardware and is
  915. //! asserted only when there is space available in the receive FIFO. If no
  916. //! hardware flow control is required, \b UART_FLOWCONTROL_NONE should be
  917. //! passed.
  918. //!
  919. //! \note The availability of hardware flow control varies with the Tiva
  920. //! part and UART in use. Please consult the datasheet for the part you are
  921. //! using to determine whether this support is available.
  922. //!
  923. //! \return None.
  924. //
  925. //*****************************************************************************
  926. void
  927. UARTFlowControlSet(uint32_t ui32Base, uint32_t ui32Mode)
  928. {
  929. //
  930. // Check the arguments.
  931. //
  932. ASSERT(_UARTBaseValid(ui32Base));
  933. ASSERT((ui32Mode & ~(UART_FLOWCONTROL_TX | UART_FLOWCONTROL_RX)) == 0);
  934. //
  935. // Set the flow control mode as requested.
  936. //
  937. HWREG(ui32Base + UART_O_CTL) = ((HWREG(ui32Base + UART_O_CTL) &
  938. ~(UART_FLOWCONTROL_TX |
  939. UART_FLOWCONTROL_RX)) | ui32Mode);
  940. }
  941. //*****************************************************************************
  942. //
  943. //! Returns the UART hardware flow control mode currently in use.
  944. //!
  945. //! \param ui32Base is the base address of the UART port.
  946. //!
  947. //! This function returns the current hardware flow control mode.
  948. //!
  949. //! \note The availability of hardware flow control varies with the Tiva
  950. //! part and UART in use. Please consult the datasheet for the part you are
  951. //! using to determine whether this support is available.
  952. //!
  953. //! \return Returns the current flow control mode in use. This value is a
  954. //! logical OR combination of values \b UART_FLOWCONTROL_TX if transmit
  955. //! (CTS) flow control is enabled and \b UART_FLOWCONTROL_RX if receive (RTS)
  956. //! flow control is in use. If hardware flow control is disabled,
  957. //! \b UART_FLOWCONTROL_NONE is returned.
  958. //
  959. //*****************************************************************************
  960. uint32_t
  961. UARTFlowControlGet(uint32_t ui32Base)
  962. {
  963. //
  964. // Check the arguments.
  965. //
  966. ASSERT(_UARTBaseValid(ui32Base));
  967. return(HWREG(ui32Base + UART_O_CTL) & (UART_FLOWCONTROL_TX |
  968. UART_FLOWCONTROL_RX));
  969. }
  970. //*****************************************************************************
  971. //
  972. //! Sets the operating mode for the UART transmit interrupt.
  973. //!
  974. //! \param ui32Base is the base address of the UART port.
  975. //! \param ui32Mode is the operating mode for the transmit interrupt. It may
  976. //! be \b UART_TXINT_MODE_EOT to trigger interrupts when the transmitter is
  977. //! idle or \b UART_TXINT_MODE_FIFO to trigger based on the current transmit
  978. //! FIFO level.
  979. //!
  980. //! This function allows the mode of the UART transmit interrupt to be set. By
  981. //! default, the transmit interrupt is asserted when the FIFO level falls past
  982. //! a threshold set via a call to UARTFIFOLevelSet(). Alternatively, if this
  983. //! function is called with \e ui32Mode set to \b UART_TXINT_MODE_EOT, the
  984. //! transmit interrupt is asserted once the transmitter is completely idle -
  985. //! the transmit FIFO is empty and all bits, including any stop bits, have
  986. //! cleared the transmitter.
  987. //!
  988. //! \note The availability of end-of-transmission mode varies with the
  989. //! Tiva part in use. Please consult the datasheet for the part you are
  990. //! using to determine whether this support is available.
  991. //!
  992. //! \return None.
  993. //
  994. //*****************************************************************************
  995. void
  996. UARTTxIntModeSet(uint32_t ui32Base, uint32_t ui32Mode)
  997. {
  998. //
  999. // Check the arguments.
  1000. //
  1001. ASSERT(_UARTBaseValid(ui32Base));
  1002. ASSERT((ui32Mode == UART_TXINT_MODE_EOT) ||
  1003. (ui32Mode == UART_TXINT_MODE_FIFO));
  1004. //
  1005. // Set or clear the EOT bit of the UART control register as appropriate.
  1006. //
  1007. HWREG(ui32Base + UART_O_CTL) = ((HWREG(ui32Base + UART_O_CTL) &
  1008. ~(UART_TXINT_MODE_EOT |
  1009. UART_TXINT_MODE_FIFO)) | ui32Mode);
  1010. }
  1011. //*****************************************************************************
  1012. //
  1013. //! Returns the current operating mode for the UART transmit interrupt.
  1014. //!
  1015. //! \param ui32Base is the base address of the UART port.
  1016. //!
  1017. //! This function returns the current operating mode for the UART transmit
  1018. //! interrupt. The return value is \b UART_TXINT_MODE_EOT if the transmit
  1019. //! interrupt is currently configured to be asserted once the transmitter is
  1020. //! completely idle - the transmit FIFO is empty and all bits, including any
  1021. //! stop bits, have cleared the transmitter. The return value is
  1022. //! \b UART_TXINT_MODE_FIFO if the interrupt is configured to be asserted based
  1023. //! on the level of the transmit FIFO.
  1024. //!
  1025. //! \note The availability of end-of-transmission mode varies with the
  1026. //! Tiva part in use. Please consult the datasheet for the part you are
  1027. //! using to determine whether this support is available.
  1028. //!
  1029. //! \return Returns \b UART_TXINT_MODE_FIFO or \b UART_TXINT_MODE_EOT.
  1030. //
  1031. //*****************************************************************************
  1032. uint32_t
  1033. UARTTxIntModeGet(uint32_t ui32Base)
  1034. {
  1035. //
  1036. // Check the arguments.
  1037. //
  1038. ASSERT(_UARTBaseValid(ui32Base));
  1039. //
  1040. // Return the current transmit interrupt mode.
  1041. //
  1042. return(HWREG(ui32Base + UART_O_CTL) & (UART_TXINT_MODE_EOT |
  1043. UART_TXINT_MODE_FIFO));
  1044. }
  1045. //*****************************************************************************
  1046. //
  1047. //! Determines if there are any characters in the receive FIFO.
  1048. //!
  1049. //! \param ui32Base is the base address of the UART port.
  1050. //!
  1051. //! This function returns a flag indicating whether or not there is data
  1052. //! available in the receive FIFO.
  1053. //!
  1054. //! \return Returns \b true if there is data in the receive FIFO or \b false
  1055. //! if there is no data in the receive FIFO.
  1056. //
  1057. //*****************************************************************************
  1058. bool
  1059. UARTCharsAvail(uint32_t ui32Base)
  1060. {
  1061. //
  1062. // Check the arguments.
  1063. //
  1064. ASSERT(_UARTBaseValid(ui32Base));
  1065. //
  1066. // Return the availability of characters.
  1067. //
  1068. return((HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE) ? false : true);
  1069. }
  1070. //*****************************************************************************
  1071. //
  1072. //! Determines if there is any space in the transmit FIFO.
  1073. //!
  1074. //! \param ui32Base is the base address of the UART port.
  1075. //!
  1076. //! This function returns a flag indicating whether or not there is space
  1077. //! available in the transmit FIFO.
  1078. //!
  1079. //! \return Returns \b true if there is space available in the transmit FIFO
  1080. //! or \b false if there is no space available in the transmit FIFO.
  1081. //
  1082. //*****************************************************************************
  1083. bool
  1084. UARTSpaceAvail(uint32_t ui32Base)
  1085. {
  1086. //
  1087. // Check the arguments.
  1088. //
  1089. ASSERT(_UARTBaseValid(ui32Base));
  1090. //
  1091. // Return the availability of space.
  1092. //
  1093. return((HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF) ? false : true);
  1094. }
  1095. //*****************************************************************************
  1096. //
  1097. //! Receives a character from the specified port.
  1098. //!
  1099. //! \param ui32Base is the base address of the UART port.
  1100. //!
  1101. //! This function gets a character from the receive FIFO for the specified
  1102. //! port.
  1103. //!
  1104. //! \return Returns the character read from the specified port, cast as a
  1105. //! \e int32_t. A \b -1 is returned if there are no characters present in the
  1106. //! receive FIFO. The UARTCharsAvail() function should be called before
  1107. //! attempting to call this function.
  1108. //
  1109. //*****************************************************************************
  1110. int32_t
  1111. UARTCharGetNonBlocking(uint32_t ui32Base)
  1112. {
  1113. //
  1114. // Check the arguments.
  1115. //
  1116. ASSERT(_UARTBaseValid(ui32Base));
  1117. //
  1118. // See if there are any characters in the receive FIFO.
  1119. //
  1120. if(!(HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE))
  1121. {
  1122. //
  1123. // Read and return the next character.
  1124. //
  1125. return(HWREG(ui32Base + UART_O_DR));
  1126. }
  1127. else
  1128. {
  1129. //
  1130. // There are no characters, so return a failure.
  1131. //
  1132. return(-1);
  1133. }
  1134. }
  1135. //*****************************************************************************
  1136. //
  1137. //! Waits for a character from the specified port.
  1138. //!
  1139. //! \param ui32Base is the base address of the UART port.
  1140. //!
  1141. //! This function gets a character from the receive FIFO for the specified
  1142. //! port. If there are no characters available, this function waits until a
  1143. //! character is received before returning.
  1144. //!
  1145. //! \return Returns the character read from the specified port, cast as a
  1146. //! \e int32_t.
  1147. //
  1148. //*****************************************************************************
  1149. int32_t
  1150. UARTCharGet(uint32_t ui32Base)
  1151. {
  1152. //
  1153. // Check the arguments.
  1154. //
  1155. ASSERT(_UARTBaseValid(ui32Base));
  1156. //
  1157. // Wait until a char is available.
  1158. //
  1159. while(HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE)
  1160. {
  1161. }
  1162. //
  1163. // Now get the char.
  1164. //
  1165. return(HWREG(ui32Base + UART_O_DR));
  1166. }
  1167. //*****************************************************************************
  1168. //
  1169. //! Sends a character to the specified port.
  1170. //!
  1171. //! \param ui32Base is the base address of the UART port.
  1172. //! \param ucData is the character to be transmitted.
  1173. //!
  1174. //! This function writes the character \e ucData to the transmit FIFO for the
  1175. //! specified port. This function does not block, so if there is no space
  1176. //! available, then a \b false is returned and the application must retry the
  1177. //! function later.
  1178. //!
  1179. //! \return Returns \b true if the character was successfully placed in the
  1180. //! transmit FIFO or \b false if there was no space available in the transmit
  1181. //! FIFO.
  1182. //
  1183. //*****************************************************************************
  1184. bool
  1185. UARTCharPutNonBlocking(uint32_t ui32Base, unsigned char ucData)
  1186. {
  1187. //
  1188. // Check the arguments.
  1189. //
  1190. ASSERT(_UARTBaseValid(ui32Base));
  1191. //
  1192. // See if there is space in the transmit FIFO.
  1193. //
  1194. if(!(HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF))
  1195. {
  1196. //
  1197. // Write this character to the transmit FIFO.
  1198. //
  1199. HWREG(ui32Base + UART_O_DR) = ucData;
  1200. //
  1201. // Success.
  1202. //
  1203. return(true);
  1204. }
  1205. else
  1206. {
  1207. //
  1208. // There is no space in the transmit FIFO, so return a failure.
  1209. //
  1210. return(false);
  1211. }
  1212. }
  1213. //*****************************************************************************
  1214. //
  1215. //! Waits to send a character from the specified port.
  1216. //!
  1217. //! \param ui32Base is the base address of the UART port.
  1218. //! \param ucData is the character to be transmitted.
  1219. //!
  1220. //! This function sends the character \e ucData to the transmit FIFO for the
  1221. //! specified port. If there is no space available in the transmit FIFO, this
  1222. //! function waits until there is space available before returning.
  1223. //!
  1224. //! \return None.
  1225. //
  1226. //*****************************************************************************
  1227. void
  1228. UARTCharPut(uint32_t ui32Base, unsigned char ucData)
  1229. {
  1230. //
  1231. // Check the arguments.
  1232. //
  1233. ASSERT(_UARTBaseValid(ui32Base));
  1234. //
  1235. // Wait until space is available.
  1236. //
  1237. while(HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF)
  1238. {
  1239. }
  1240. //
  1241. // Send the char.
  1242. //
  1243. HWREG(ui32Base + UART_O_DR) = ucData;
  1244. }
  1245. //*****************************************************************************
  1246. //
  1247. //! Causes a BREAK to be sent.
  1248. //!
  1249. //! \param ui32Base is the base address of the UART port.
  1250. //! \param bBreakState controls the output level.
  1251. //!
  1252. //! Calling this function with \e bBreakState set to \b true asserts a break
  1253. //! condition on the UART. Calling this function with \e bBreakState set to
  1254. //! \b false removes the break condition. For proper transmission of a break
  1255. //! command, the break must be asserted for at least two complete frames.
  1256. //!
  1257. //! \return None.
  1258. //
  1259. //*****************************************************************************
  1260. void
  1261. UARTBreakCtl(uint32_t ui32Base, bool bBreakState)
  1262. {
  1263. //
  1264. // Check the arguments.
  1265. //
  1266. ASSERT(_UARTBaseValid(ui32Base));
  1267. //
  1268. // Set the break condition as requested.
  1269. //
  1270. HWREG(ui32Base + UART_O_LCRH) =
  1271. (bBreakState ?
  1272. (HWREG(ui32Base + UART_O_LCRH) | UART_LCRH_BRK) :
  1273. (HWREG(ui32Base + UART_O_LCRH) & ~(UART_LCRH_BRK)));
  1274. }
  1275. //*****************************************************************************
  1276. //
  1277. //! Determines whether the UART transmitter is busy or not.
  1278. //!
  1279. //! \param ui32Base is the base address of the UART port.
  1280. //!
  1281. //! This function allows the caller to determine whether all transmitted bytes
  1282. //! have cleared the transmitter hardware. If \b false is returned, the
  1283. //! transmit FIFO is empty and all bits of the last transmitted character,
  1284. //! including all stop bits, have left the hardware shift register.
  1285. //!
  1286. //! \return Returns \b true if the UART is transmitting or \b false if all
  1287. //! transmissions are complete.
  1288. //
  1289. //*****************************************************************************
  1290. bool
  1291. UARTBusy(uint32_t ui32Base)
  1292. {
  1293. //
  1294. // Check the argument.
  1295. //
  1296. ASSERT(_UARTBaseValid(ui32Base));
  1297. //
  1298. // Determine if the UART is busy.
  1299. //
  1300. return((HWREG(ui32Base + UART_O_FR) & UART_FR_BUSY) ? true : false);
  1301. }
  1302. //*****************************************************************************
  1303. //
  1304. //! Registers an interrupt handler for a UART interrupt.
  1305. //!
  1306. //! \param ui32Base is the base address of the UART port.
  1307. //! \param pfnHandler is a pointer to the function to be called when the
  1308. //! UART interrupt occurs.
  1309. //!
  1310. //! This function does the actual registering of the interrupt handler. This
  1311. //! function enables the global interrupt in the interrupt controller; specific
  1312. //! UART interrupts must be enabled via UARTIntEnable(). It is the interrupt
  1313. //! handler's responsibility to clear the interrupt source.
  1314. //!
  1315. //! \sa IntRegister() for important information about registering interrupt
  1316. //! handlers.
  1317. //!
  1318. //! \return None.
  1319. //
  1320. //*****************************************************************************
  1321. void
  1322. UARTIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
  1323. {
  1324. uint32_t ui32Int;
  1325. //
  1326. // Check the arguments.
  1327. //
  1328. ASSERT(_UARTBaseValid(ui32Base));
  1329. //
  1330. // Determine the interrupt number based on the UART port.
  1331. //
  1332. ui32Int = _UARTIntNumberGet(ui32Base);
  1333. ASSERT(ui32Int != 0);
  1334. //
  1335. // Register the interrupt handler.
  1336. //
  1337. IntRegister(ui32Int, pfnHandler);
  1338. //
  1339. // Enable the UART interrupt.
  1340. //
  1341. IntEnable(ui32Int);
  1342. }
  1343. //*****************************************************************************
  1344. //
  1345. //! Unregisters an interrupt handler for a UART interrupt.
  1346. //!
  1347. //! \param ui32Base is the base address of the UART port.
  1348. //!
  1349. //! This function does the actual unregistering of the interrupt handler. It
  1350. //! clears the handler to be called when a UART interrupt occurs. This
  1351. //! function also masks off the interrupt in the interrupt controller so that
  1352. //! the interrupt handler no longer is called.
  1353. //!
  1354. //! \sa IntRegister() for important information about registering interrupt
  1355. //! handlers.
  1356. //!
  1357. //! \return None.
  1358. //
  1359. //*****************************************************************************
  1360. void
  1361. UARTIntUnregister(uint32_t ui32Base)
  1362. {
  1363. uint32_t ui32Int;
  1364. //
  1365. // Check the arguments.
  1366. //
  1367. ASSERT(_UARTBaseValid(ui32Base));
  1368. //
  1369. // Determine the interrupt number based on the UART port.
  1370. //
  1371. ui32Int = _UARTIntNumberGet(ui32Base);
  1372. ASSERT(ui32Int != 0);
  1373. //
  1374. // Disable the interrupt.
  1375. //
  1376. IntDisable(ui32Int);
  1377. //
  1378. // Unregister the interrupt handler.
  1379. //
  1380. IntUnregister(ui32Int);
  1381. }
  1382. //*****************************************************************************
  1383. //
  1384. //! Enables individual UART interrupt sources.
  1385. //!
  1386. //! \param ui32Base is the base address of the UART port.
  1387. //! \param ui32IntFlags is the bit mask of the interrupt sources to be enabled.
  1388. //!
  1389. //! This function enables the indicated UART interrupt sources. Only the
  1390. //! sources that are enabled can be reflected to the processor interrupt;
  1391. //! disabled sources have no effect on the processor.
  1392. //!
  1393. //! The \e ui32IntFlags parameter is the logical OR of any of the following:
  1394. //!
  1395. //! - \b UART_INT_9BIT - 9-bit Address Match interrupt
  1396. //! - \b UART_INT_OE - Overrun Error interrupt
  1397. //! - \b UART_INT_BE - Break Error interrupt
  1398. //! - \b UART_INT_PE - Parity Error interrupt
  1399. //! - \b UART_INT_FE - Framing Error interrupt
  1400. //! - \b UART_INT_RT - Receive Timeout interrupt
  1401. //! - \b UART_INT_TX - Transmit interrupt
  1402. //! - \b UART_INT_RX - Receive interrupt
  1403. //! - \b UART_INT_DSR - DSR interrupt
  1404. //! - \b UART_INT_DCD - DCD interrupt
  1405. //! - \b UART_INT_CTS - CTS interrupt
  1406. //! - \b UART_INT_RI - RI interrupt
  1407. //!
  1408. //! \return None.
  1409. //
  1410. //*****************************************************************************
  1411. void
  1412. UARTIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
  1413. {
  1414. //
  1415. // Check the arguments.
  1416. //
  1417. ASSERT(_UARTBaseValid(ui32Base));
  1418. //
  1419. // Enable the specified interrupts.
  1420. //
  1421. HWREG(ui32Base + UART_O_IM) |= ui32IntFlags;
  1422. }
  1423. //*****************************************************************************
  1424. //
  1425. //! Disables individual UART interrupt sources.
  1426. //!
  1427. //! \param ui32Base is the base address of the UART port.
  1428. //! \param ui32IntFlags is the bit mask of the interrupt sources to be
  1429. //! disabled.
  1430. //!
  1431. //! This function disables the indicated UART interrupt sources. Only the
  1432. //! sources that are enabled can be reflected to the processor interrupt;
  1433. //! disabled sources have no effect on the processor.
  1434. //!
  1435. //! The \e ui32IntFlags parameter has the same definition as the
  1436. //! \e ui32IntFlags parameter to UARTIntEnable().
  1437. //!
  1438. //! \return None.
  1439. //
  1440. //*****************************************************************************
  1441. void
  1442. UARTIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
  1443. {
  1444. //
  1445. // Check the arguments.
  1446. //
  1447. ASSERT(_UARTBaseValid(ui32Base));
  1448. //
  1449. // Disable the specified interrupts.
  1450. //
  1451. HWREG(ui32Base + UART_O_IM) &= ~(ui32IntFlags);
  1452. }
  1453. //*****************************************************************************
  1454. //
  1455. //! Gets the current interrupt status.
  1456. //!
  1457. //! \param ui32Base is the base address of the UART port.
  1458. //! \param bMasked is \b false if the raw interrupt status is required and
  1459. //! \b true if the masked interrupt status is required.
  1460. //!
  1461. //! This function returns the interrupt status for the specified UART. Either
  1462. //! the raw interrupt status or the status of interrupts that are allowed to
  1463. //! reflect to the processor can be returned.
  1464. //!
  1465. //! \return Returns the current interrupt status, enumerated as a bit field of
  1466. //! values described in UARTIntEnable().
  1467. //
  1468. //*****************************************************************************
  1469. uint32_t
  1470. UARTIntStatus(uint32_t ui32Base, bool bMasked)
  1471. {
  1472. //
  1473. // Check the arguments.
  1474. //
  1475. ASSERT(_UARTBaseValid(ui32Base));
  1476. //
  1477. // Return either the interrupt status or the raw interrupt status as
  1478. // requested.
  1479. //
  1480. if(bMasked)
  1481. {
  1482. return(HWREG(ui32Base + UART_O_MIS));
  1483. }
  1484. else
  1485. {
  1486. return(HWREG(ui32Base + UART_O_RIS));
  1487. }
  1488. }
  1489. //*****************************************************************************
  1490. //
  1491. //! Clears UART interrupt sources.
  1492. //!
  1493. //! \param ui32Base is the base address of the UART port.
  1494. //! \param ui32IntFlags is a bit mask of the interrupt sources to be cleared.
  1495. //!
  1496. //! The specified UART interrupt sources are cleared, so that they no longer
  1497. //! assert. This function must be called in the interrupt handler to keep the
  1498. //! interrupt from being triggered again immediately upon exit.
  1499. //!
  1500. //! The \e ui32IntFlags parameter has the same definition as the
  1501. //! \e ui32IntFlags parameter to UARTIntEnable().
  1502. //!
  1503. //! \note Because there is a write buffer in the Cortex-M processor, it may
  1504. //! take several clock cycles before the interrupt source is actually cleared.
  1505. //! Therefore, it is recommended that the interrupt source be cleared early in
  1506. //! the interrupt handler (as opposed to the very last action) to avoid
  1507. //! returning from the interrupt handler before the interrupt source is
  1508. //! actually cleared. Failure to do so may result in the interrupt handler
  1509. //! being immediately reentered (because the interrupt controller still sees
  1510. //! the interrupt source asserted).
  1511. //!
  1512. //! \return None.
  1513. //
  1514. //*****************************************************************************
  1515. void
  1516. UARTIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
  1517. {
  1518. //
  1519. // Check the arguments.
  1520. //
  1521. ASSERT(_UARTBaseValid(ui32Base));
  1522. //
  1523. // Clear the requested interrupt sources.
  1524. //
  1525. HWREG(ui32Base + UART_O_ICR) = ui32IntFlags;
  1526. }
  1527. //*****************************************************************************
  1528. //
  1529. //! Enable UART uDMA operation.
  1530. //!
  1531. //! \param ui32Base is the base address of the UART port.
  1532. //! \param ui32DMAFlags is a bit mask of the uDMA features to enable.
  1533. //!
  1534. //! The specified UART uDMA features are enabled. The UART can be
  1535. //! configured to use uDMA for transmit or receive and to disable
  1536. //! receive if an error occurs. The \e ui32DMAFlags parameter is the
  1537. //! logical OR of any of the following values:
  1538. //!
  1539. //! - \b UART_DMA_RX - enable uDMA for receive
  1540. //! - \b UART_DMA_TX - enable uDMA for transmit
  1541. //! - \b UART_DMA_ERR_RXSTOP - disable uDMA receive on UART error
  1542. //!
  1543. //! \note The uDMA controller must also be set up before DMA can be used
  1544. //! with the UART.
  1545. //!
  1546. //! \return None.
  1547. //
  1548. //*****************************************************************************
  1549. void
  1550. UARTDMAEnable(uint32_t ui32Base, uint32_t ui32DMAFlags)
  1551. {
  1552. //
  1553. // Check the arguments.
  1554. //
  1555. ASSERT(_UARTBaseValid(ui32Base));
  1556. //
  1557. // Set the requested bits in the UART uDMA control register.
  1558. //
  1559. HWREG(ui32Base + UART_O_DMACTL) |= ui32DMAFlags;
  1560. }
  1561. //*****************************************************************************
  1562. //
  1563. //! Disable UART uDMA operation.
  1564. //!
  1565. //! \param ui32Base is the base address of the UART port.
  1566. //! \param ui32DMAFlags is a bit mask of the uDMA features to disable.
  1567. //!
  1568. //! This function is used to disable UART uDMA features that were enabled
  1569. //! by UARTDMAEnable(). The specified UART uDMA features are disabled. The
  1570. //! \e ui32DMAFlags parameter is the logical OR of any of the following values:
  1571. //!
  1572. //! - \b UART_DMA_RX - disable uDMA for receive
  1573. //! - \b UART_DMA_TX - disable uDMA for transmit
  1574. //! - \b UART_DMA_ERR_RXSTOP - do not disable uDMA receive on UART error
  1575. //!
  1576. //! \return None.
  1577. //
  1578. //*****************************************************************************
  1579. void
  1580. UARTDMADisable(uint32_t ui32Base, uint32_t ui32DMAFlags)
  1581. {
  1582. //
  1583. // Check the arguments.
  1584. //
  1585. ASSERT(_UARTBaseValid(ui32Base));
  1586. //
  1587. // Clear the requested bits in the UART uDMA control register.
  1588. //
  1589. HWREG(ui32Base + UART_O_DMACTL) &= ~ui32DMAFlags;
  1590. }
  1591. //*****************************************************************************
  1592. //
  1593. //! Gets current receiver errors.
  1594. //!
  1595. //! \param ui32Base is the base address of the UART port.
  1596. //!
  1597. //! This function returns the current state of each of the 4 receiver error
  1598. //! sources. The returned errors are equivalent to the four error bits
  1599. //! returned via the previous call to UARTCharGet() or UARTCharGetNonBlocking()
  1600. //! with the exception that the overrun error is set immediately when the
  1601. //! overrun occurs rather than when a character is next read.
  1602. //!
  1603. //! \return Returns a logical OR combination of the receiver error flags,
  1604. //! \b UART_RXERROR_FRAMING, \b UART_RXERROR_PARITY, \b UART_RXERROR_BREAK
  1605. //! and \b UART_RXERROR_OVERRUN.
  1606. //
  1607. //*****************************************************************************
  1608. uint32_t
  1609. UARTRxErrorGet(uint32_t ui32Base)
  1610. {
  1611. //
  1612. // Check the arguments.
  1613. //
  1614. ASSERT(_UARTBaseValid(ui32Base));
  1615. //
  1616. // Return the current value of the receive status register.
  1617. //
  1618. return(HWREG(ui32Base + UART_O_RSR) & 0x0000000F);
  1619. }
  1620. //*****************************************************************************
  1621. //
  1622. //! Clears all reported receiver errors.
  1623. //!
  1624. //! \param ui32Base is the base address of the UART port.
  1625. //!
  1626. //! This function is used to clear all receiver error conditions reported via
  1627. //! UARTRxErrorGet(). If using the overrun, framing error, parity error or
  1628. //! break interrupts, this function must be called after clearing the interrupt
  1629. //! to ensure that later errors of the same type trigger another interrupt.
  1630. //!
  1631. //! \return None.
  1632. //
  1633. //*****************************************************************************
  1634. void
  1635. UARTRxErrorClear(uint32_t ui32Base)
  1636. {
  1637. //
  1638. // Check the arguments.
  1639. //
  1640. ASSERT(_UARTBaseValid(ui32Base));
  1641. //
  1642. // Any write to the Error Clear Register clears all bits which are
  1643. // currently set.
  1644. //
  1645. HWREG(ui32Base + UART_O_ECR) = 0;
  1646. }
  1647. //*****************************************************************************
  1648. //
  1649. //! Sets the baud clock source for the specified UART.
  1650. //!
  1651. //! \param ui32Base is the base address of the UART port.
  1652. //! \param ui32Source is the baud clock source for the UART.
  1653. //!
  1654. //! This function allows the baud clock source for the UART to be selected.
  1655. //! The possible clock source are the system clock (\b UART_CLOCK_SYSTEM) or
  1656. //! the precision internal oscillator (\b UART_CLOCK_PIOSC).
  1657. //!
  1658. //! Changing the baud clock source changes the baud rate generated by the
  1659. //! UART. Therefore, the baud rate should be reconfigured after any change to
  1660. //! the baud clock source.
  1661. //!
  1662. //! \note The ability to specify the UART baud clock source varies with the
  1663. //! Tiva part in use. Please consult the datasheet for the part you are
  1664. //! using to determine whether this support is available.
  1665. //!
  1666. //! \return None.
  1667. //
  1668. //*****************************************************************************
  1669. void
  1670. UARTClockSourceSet(uint32_t ui32Base, uint32_t ui32Source)
  1671. {
  1672. //
  1673. // Check the arguments.
  1674. //
  1675. ASSERT(_UARTBaseValid(ui32Base));
  1676. ASSERT((ui32Source == UART_CLOCK_SYSTEM) ||
  1677. (ui32Source == UART_CLOCK_PIOSC));
  1678. //
  1679. // Set the UART clock source.
  1680. //
  1681. HWREG(ui32Base + UART_O_CC) = ui32Source;
  1682. }
  1683. //*****************************************************************************
  1684. //
  1685. //! Gets the baud clock source for the specified UART.
  1686. //!
  1687. //! \param ui32Base is the base address of the UART port.
  1688. //!
  1689. //! This function returns the baud clock source for the specified UART. The
  1690. //! possible baud clock source are the system clock (\b UART_CLOCK_SYSTEM) or
  1691. //! the precision internal oscillator (\b UART_CLOCK_PIOSC).
  1692. //!
  1693. //! \note The ability to specify the UART baud clock source varies with the
  1694. //! Tiva part in use. Please consult the datasheet for the part you are
  1695. //! using to determine whether this support is available.
  1696. //!
  1697. //! \return None.
  1698. //
  1699. //*****************************************************************************
  1700. uint32_t
  1701. UARTClockSourceGet(uint32_t ui32Base)
  1702. {
  1703. //
  1704. // Check the arguments.
  1705. //
  1706. ASSERT(_UARTBaseValid(ui32Base));
  1707. //
  1708. // Return the UART clock source.
  1709. //
  1710. return(HWREG(ui32Base + UART_O_CC));
  1711. }
  1712. //*****************************************************************************
  1713. //
  1714. //! Enables 9-bit mode on the specified UART.
  1715. //!
  1716. //! \param ui32Base is the base address of the UART port.
  1717. //!
  1718. //! This function enables the 9-bit operational mode of the UART.
  1719. //!
  1720. //! \note The availability of 9-bit mode varies with the Tiva part in use.
  1721. //! Please consult the datasheet for the part you are using to determine
  1722. //! whether this support is available.
  1723. //!
  1724. //! \return None.
  1725. //
  1726. //*****************************************************************************
  1727. void
  1728. UART9BitEnable(uint32_t ui32Base)
  1729. {
  1730. //
  1731. // Check the arguments.
  1732. //
  1733. ASSERT(_UARTBaseValid(ui32Base));
  1734. //
  1735. // Enable 9-bit mode.
  1736. //
  1737. HWREG(ui32Base + UART_O_9BITADDR) |= UART_9BITADDR_9BITEN;
  1738. }
  1739. //*****************************************************************************
  1740. //
  1741. //! Disables 9-bit mode on the specified UART.
  1742. //!
  1743. //! \param ui32Base is the base address of the UART port.
  1744. //!
  1745. //! This function disables the 9-bit operational mode of the UART.
  1746. //!
  1747. //! \note The availability of 9-bit mode varies with the Tiva part in use.
  1748. //! Please consult the datasheet for the part you are using to determine
  1749. //! whether this support is available.
  1750. //!
  1751. //! \return None.
  1752. //
  1753. //*****************************************************************************
  1754. void
  1755. UART9BitDisable(uint32_t ui32Base)
  1756. {
  1757. //
  1758. // Check the arguments.
  1759. //
  1760. ASSERT(_UARTBaseValid(ui32Base));
  1761. //
  1762. // Disable 9-bit mode.
  1763. //
  1764. HWREG(ui32Base + UART_O_9BITADDR) &= ~UART_9BITADDR_9BITEN;
  1765. }
  1766. //*****************************************************************************
  1767. //
  1768. //! Sets the device address(es) for 9-bit mode.
  1769. //!
  1770. //! \param ui32Base is the base address of the UART port.
  1771. //! \param ui8Addr is the device address.
  1772. //! \param ui8Mask is the device address mask.
  1773. //!
  1774. //! This function configures the device address or range of device addresses
  1775. //! that respond to requests on the 9-bit UART port. The received address is
  1776. //! masked with the mask and then compared against the given address, allowing
  1777. //! either a single address (if \b ui8Mask is 0xff) or a set of addresses to be
  1778. //! matched.
  1779. //!
  1780. //! \note The availability of 9-bit mode varies with the Tiva part in use.
  1781. //! Please consult the datasheet for the part you are using to determine
  1782. //! whether this support is available.
  1783. //!
  1784. //! \return None.
  1785. //
  1786. //*****************************************************************************
  1787. void
  1788. UART9BitAddrSet(uint32_t ui32Base, uint8_t ui8Addr,
  1789. uint8_t ui8Mask)
  1790. {
  1791. //
  1792. // Check the arguments.
  1793. //
  1794. ASSERT(_UARTBaseValid(ui32Base));
  1795. //
  1796. // Set the address and mask.
  1797. //
  1798. HWREG(ui32Base + UART_O_9BITADDR) = ui8Addr << UART_9BITADDR_ADDR_S;
  1799. HWREG(ui32Base + UART_O_9BITAMASK) = ui8Mask << UART_9BITAMASK_MASK_S;
  1800. }
  1801. //*****************************************************************************
  1802. //
  1803. //! Sends an address character from the specified port when operating in 9-bit
  1804. //! mode.
  1805. //!
  1806. //! \param ui32Base is the base address of the UART port.
  1807. //! \param ui8Addr is the address to be transmitted.
  1808. //!
  1809. //! This function waits until all data has been sent from the specified port
  1810. //! and then sends the given address as an address byte. It then waits until
  1811. //! the address byte has been transmitted before returning.
  1812. //!
  1813. //! The normal data functions (UARTCharPut(), UARTCharPutNonBlocking(),
  1814. //! UARTCharGet(), and UARTCharGetNonBlocking()) are used to send and receive
  1815. //! data characters in 9-bit mode.
  1816. //!
  1817. //! \note The availability of 9-bit mode varies with the Tiva part in use.
  1818. //! Please consult the datasheet for the part you are using to determine
  1819. //! whether this support is available.
  1820. //!
  1821. //! \return None.
  1822. //
  1823. //*****************************************************************************
  1824. void
  1825. UART9BitAddrSend(uint32_t ui32Base, uint8_t ui8Addr)
  1826. {
  1827. uint32_t ui32LCRH;
  1828. //
  1829. // Check the arguments.
  1830. //
  1831. ASSERT(_UARTBaseValid(ui32Base));
  1832. //
  1833. // Wait until the FIFO is empty and the UART is not busy.
  1834. //
  1835. while((HWREG(ui32Base + UART_O_FR) & (UART_FR_TXFE | UART_FR_BUSY)) !=
  1836. UART_FR_TXFE)
  1837. {
  1838. }
  1839. //
  1840. // Force the address/data bit to 1 to indicate this is an address byte.
  1841. //
  1842. ui32LCRH = HWREG(ui32Base + UART_O_LCRH);
  1843. HWREG(ui32Base + UART_O_LCRH) = ((ui32LCRH & ~UART_LCRH_EPS) |
  1844. UART_LCRH_SPS | UART_LCRH_PEN);
  1845. //
  1846. // Send the address.
  1847. //
  1848. HWREG(ui32Base + UART_O_DR) = ui8Addr;
  1849. //
  1850. // Wait until the address has been sent.
  1851. //
  1852. while((HWREG(ui32Base + UART_O_FR) & (UART_FR_TXFE | UART_FR_BUSY)) !=
  1853. UART_FR_TXFE)
  1854. {
  1855. }
  1856. //
  1857. // Restore the address/data setting.
  1858. //
  1859. HWREG(ui32Base + UART_O_LCRH) = ui32LCRH;
  1860. }
  1861. //*****************************************************************************
  1862. //
  1863. //! Enables internal loopback mode for a UART port
  1864. //!
  1865. //! \param ui32Base is the base address of the UART port.
  1866. //!
  1867. //! This function configures a UART port in internal loopback mode to help with
  1868. //! diagnostics and debug. In this mode, the transmit and receive terminals of
  1869. //! the same UART port are internally connected. Hence, the data transmitted
  1870. //! on the UnTx output is received on the UxRx input, without having to go
  1871. //! through I/O's. UARTCharPut(), UARTCharGet() functions can be used along
  1872. //! with this function.
  1873. //!
  1874. //! \return None.
  1875. //
  1876. //*****************************************************************************
  1877. void UARTLoopbackEnable(uint32_t ui32Base)
  1878. {
  1879. //
  1880. // Check the arguments.
  1881. //
  1882. ASSERT(_UARTBaseValid(ui32Base));
  1883. //
  1884. // Write the Loopback Enable bit to register.
  1885. //
  1886. HWREG(ui32Base + UART_O_CTL) |= UART_CTL_LBE;
  1887. }
  1888. //*****************************************************************************
  1889. //
  1890. // Close the Doxygen group.
  1891. //! @}
  1892. //
  1893. //*****************************************************************************