uart.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994
  1. //*****************************************************************************
  2. //
  3. // uart.c - Driver for the UART.
  4. //
  5. // Copyright (c) 2005-2017 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.1.4.178 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. ASSERT(ui32Base == UART1_BASE);
  757. ASSERT((ui32Control & ~(UART_OUTPUT_RTS | UART_OUTPUT_DTR)) == 0);
  758. //
  759. // Set the appropriate modem control output bits.
  760. //
  761. ui32Temp = HWREG(ui32Base + UART_O_CTL);
  762. ui32Temp |= (ui32Control & (UART_OUTPUT_RTS | UART_OUTPUT_DTR));
  763. HWREG(ui32Base + UART_O_CTL) = ui32Temp;
  764. }
  765. //*****************************************************************************
  766. //
  767. //! Clears the states of the DTR and/or RTS modem control signals.
  768. //!
  769. //! \param ui32Base is the base address of the UART port.
  770. //! \param ui32Control is a bit-mapped flag indicating which modem control bits
  771. //! should be set.
  772. //!
  773. //! This function clears the states of the DTR or RTS modem handshake outputs
  774. //! from the UART.
  775. //!
  776. //! The \e ui32Control parameter is the logical OR of any of the following:
  777. //!
  778. //! - \b UART_OUTPUT_DTR - The modem control DTR signal
  779. //! - \b UART_OUTPUT_RTS - The modem control RTS signal
  780. //!
  781. //! \note The availability of hardware modem handshake signals varies with the
  782. //! Tiva part and UART in use. Please consult the datasheet for the part
  783. //! you are using to determine whether this support is available.
  784. //!
  785. //! \return None.
  786. //
  787. //*****************************************************************************
  788. void
  789. UARTModemControlClear(uint32_t ui32Base, uint32_t ui32Control)
  790. {
  791. uint32_t ui32Temp;
  792. //
  793. // Check the arguments.
  794. //
  795. ASSERT(ui32Base == UART1_BASE);
  796. ASSERT((ui32Control & ~(UART_OUTPUT_RTS | UART_OUTPUT_DTR)) == 0);
  797. //
  798. // Set the appropriate modem control output bits.
  799. //
  800. ui32Temp = HWREG(ui32Base + UART_O_CTL);
  801. ui32Temp &= ~(ui32Control & (UART_OUTPUT_RTS | UART_OUTPUT_DTR));
  802. HWREG(ui32Base + UART_O_CTL) = ui32Temp;
  803. }
  804. //*****************************************************************************
  805. //
  806. //! Gets the states of the DTR and RTS modem control signals.
  807. //!
  808. //! \param ui32Base is the base address of the UART port.
  809. //!
  810. //! This function returns the current states of each of the two UART modem
  811. //! control signals, DTR and RTS.
  812. //!
  813. //! \note The availability of hardware modem handshake signals varies with the
  814. //! Tiva part and UART in use. Please consult the datasheet for the part
  815. //! you are using to determine whether this support is available.
  816. //!
  817. //! \return Returns the states of the handshake output signals. This value is
  818. //! a logical OR combination of values \b UART_OUTPUT_RTS and
  819. //! \b UART_OUTPUT_DTR where the presence of each flag indicates that the
  820. //! associated signal is asserted.
  821. //
  822. //*****************************************************************************
  823. uint32_t
  824. UARTModemControlGet(uint32_t ui32Base)
  825. {
  826. //
  827. // Check the arguments.
  828. //
  829. ASSERT(ui32Base == UART1_BASE);
  830. return(HWREG(ui32Base + UART_O_CTL) & (UART_OUTPUT_RTS | UART_OUTPUT_DTR));
  831. }
  832. //*****************************************************************************
  833. //
  834. //! Gets the states of the RI, DCD, DSR and CTS modem status signals.
  835. //!
  836. //! \param ui32Base is the base address of the UART port.
  837. //!
  838. //! This function returns the current states of each of the four UART modem
  839. //! status signals, RI, DCD, DSR and CTS.
  840. //!
  841. //! \note The availability of hardware modem handshake signals varies with the
  842. //! Tiva part and UART in use. Please consult the datasheet for the part
  843. //! you are using to determine whether this support is available.
  844. //!
  845. //! \return Returns the states of the handshake output signals. This value
  846. //! is a logical OR combination of values \b UART_INPUT_RI,
  847. //! \b UART_INPUT_DCD, \b UART_INPUT_CTS and \b UART_INPUT_DSR where the
  848. //! presence of each flag indicates that the associated signal is asserted.
  849. //
  850. //*****************************************************************************
  851. uint32_t
  852. UARTModemStatusGet(uint32_t ui32Base)
  853. {
  854. //
  855. // Check the arguments.
  856. //
  857. ASSERT(ui32Base == UART1_BASE);
  858. return(HWREG(ui32Base + UART_O_FR) & (UART_INPUT_RI | UART_INPUT_DCD |
  859. UART_INPUT_CTS | UART_INPUT_DSR));
  860. }
  861. //*****************************************************************************
  862. //
  863. //! Sets the UART hardware flow control mode to be used.
  864. //!
  865. //! \param ui32Base is the base address of the UART port.
  866. //! \param ui32Mode indicates the flow control modes to be used. This
  867. //! parameter is a logical OR combination of values \b UART_FLOWCONTROL_TX and
  868. //! \b UART_FLOWCONTROL_RX to enable hardware transmit (CTS) and receive (RTS)
  869. //! flow control or \b UART_FLOWCONTROL_NONE to disable hardware flow control.
  870. //!
  871. //! This function configures the required hardware flow control modes. If
  872. //! \e ui32Mode contains flag \b UART_FLOWCONTROL_TX, data is only transmitted
  873. //! if the incoming CTS signal is asserted. If \e ui32Mode contains flag
  874. //! \b UART_FLOWCONTROL_RX, the RTS output is controlled by the hardware and is
  875. //! asserted only when there is space available in the receive FIFO. If no
  876. //! hardware flow control is required, \b UART_FLOWCONTROL_NONE should be
  877. //! passed.
  878. //!
  879. //! \note The availability of hardware flow control varies with the Tiva
  880. //! part and UART in use. Please consult the datasheet for the part you are
  881. //! using to determine whether this support is available.
  882. //!
  883. //! \return None.
  884. //
  885. //*****************************************************************************
  886. void
  887. UARTFlowControlSet(uint32_t ui32Base, uint32_t ui32Mode)
  888. {
  889. //
  890. // Check the arguments.
  891. //
  892. ASSERT(_UARTBaseValid(ui32Base));
  893. ASSERT((ui32Mode & ~(UART_FLOWCONTROL_TX | UART_FLOWCONTROL_RX)) == 0);
  894. //
  895. // Set the flow control mode as requested.
  896. //
  897. HWREG(ui32Base + UART_O_CTL) = ((HWREG(ui32Base + UART_O_CTL) &
  898. ~(UART_FLOWCONTROL_TX |
  899. UART_FLOWCONTROL_RX)) | ui32Mode);
  900. }
  901. //*****************************************************************************
  902. //
  903. //! Returns the UART hardware flow control mode currently in use.
  904. //!
  905. //! \param ui32Base is the base address of the UART port.
  906. //!
  907. //! This function returns the current hardware flow control mode.
  908. //!
  909. //! \note The availability of hardware flow control varies with the Tiva
  910. //! part and UART in use. Please consult the datasheet for the part you are
  911. //! using to determine whether this support is available.
  912. //!
  913. //! \return Returns the current flow control mode in use. This value is a
  914. //! logical OR combination of values \b UART_FLOWCONTROL_TX if transmit
  915. //! (CTS) flow control is enabled and \b UART_FLOWCONTROL_RX if receive (RTS)
  916. //! flow control is in use. If hardware flow control is disabled,
  917. //! \b UART_FLOWCONTROL_NONE is returned.
  918. //
  919. //*****************************************************************************
  920. uint32_t
  921. UARTFlowControlGet(uint32_t ui32Base)
  922. {
  923. //
  924. // Check the arguments.
  925. //
  926. ASSERT(_UARTBaseValid(ui32Base));
  927. return(HWREG(ui32Base + UART_O_CTL) & (UART_FLOWCONTROL_TX |
  928. UART_FLOWCONTROL_RX));
  929. }
  930. //*****************************************************************************
  931. //
  932. //! Sets the operating mode for the UART transmit interrupt.
  933. //!
  934. //! \param ui32Base is the base address of the UART port.
  935. //! \param ui32Mode is the operating mode for the transmit interrupt. It may
  936. //! be \b UART_TXINT_MODE_EOT to trigger interrupts when the transmitter is
  937. //! idle or \b UART_TXINT_MODE_FIFO to trigger based on the current transmit
  938. //! FIFO level.
  939. //!
  940. //! This function allows the mode of the UART transmit interrupt to be set. By
  941. //! default, the transmit interrupt is asserted when the FIFO level falls past
  942. //! a threshold set via a call to UARTFIFOLevelSet(). Alternatively, if this
  943. //! function is called with \e ui32Mode set to \b UART_TXINT_MODE_EOT, the
  944. //! transmit interrupt is asserted once the transmitter is completely idle -
  945. //! the transmit FIFO is empty and all bits, including any stop bits, have
  946. //! cleared the transmitter.
  947. //!
  948. //! \note The availability of end-of-transmission mode varies with the
  949. //! Tiva part in use. Please consult the datasheet for the part you are
  950. //! using to determine whether this support is available.
  951. //!
  952. //! \return None.
  953. //
  954. //*****************************************************************************
  955. void
  956. UARTTxIntModeSet(uint32_t ui32Base, uint32_t ui32Mode)
  957. {
  958. //
  959. // Check the arguments.
  960. //
  961. ASSERT(_UARTBaseValid(ui32Base));
  962. ASSERT((ui32Mode == UART_TXINT_MODE_EOT) ||
  963. (ui32Mode == UART_TXINT_MODE_FIFO));
  964. //
  965. // Set or clear the EOT bit of the UART control register as appropriate.
  966. //
  967. HWREG(ui32Base + UART_O_CTL) = ((HWREG(ui32Base + UART_O_CTL) &
  968. ~(UART_TXINT_MODE_EOT |
  969. UART_TXINT_MODE_FIFO)) | ui32Mode);
  970. }
  971. //*****************************************************************************
  972. //
  973. //! Returns the current operating mode for the UART transmit interrupt.
  974. //!
  975. //! \param ui32Base is the base address of the UART port.
  976. //!
  977. //! This function returns the current operating mode for the UART transmit
  978. //! interrupt. The return value is \b UART_TXINT_MODE_EOT if the transmit
  979. //! interrupt is currently configured to be asserted once the transmitter is
  980. //! completely idle - the transmit FIFO is empty and all bits, including any
  981. //! stop bits, have cleared the transmitter. The return value is
  982. //! \b UART_TXINT_MODE_FIFO if the interrupt is configured to be asserted based
  983. //! on the level of the transmit FIFO.
  984. //!
  985. //! \note The availability of end-of-transmission mode varies with the
  986. //! Tiva part in use. Please consult the datasheet for the part you are
  987. //! using to determine whether this support is available.
  988. //!
  989. //! \return Returns \b UART_TXINT_MODE_FIFO or \b UART_TXINT_MODE_EOT.
  990. //
  991. //*****************************************************************************
  992. uint32_t
  993. UARTTxIntModeGet(uint32_t ui32Base)
  994. {
  995. //
  996. // Check the arguments.
  997. //
  998. ASSERT(_UARTBaseValid(ui32Base));
  999. //
  1000. // Return the current transmit interrupt mode.
  1001. //
  1002. return(HWREG(ui32Base + UART_O_CTL) & (UART_TXINT_MODE_EOT |
  1003. UART_TXINT_MODE_FIFO));
  1004. }
  1005. //*****************************************************************************
  1006. //
  1007. //! Determines if there are any characters in the receive FIFO.
  1008. //!
  1009. //! \param ui32Base is the base address of the UART port.
  1010. //!
  1011. //! This function returns a flag indicating whether or not there is data
  1012. //! available in the receive FIFO.
  1013. //!
  1014. //! \return Returns \b true if there is data in the receive FIFO or \b false
  1015. //! if there is no data in the receive FIFO.
  1016. //
  1017. //*****************************************************************************
  1018. bool
  1019. UARTCharsAvail(uint32_t ui32Base)
  1020. {
  1021. //
  1022. // Check the arguments.
  1023. //
  1024. ASSERT(_UARTBaseValid(ui32Base));
  1025. //
  1026. // Return the availability of characters.
  1027. //
  1028. return((HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE) ? false : true);
  1029. }
  1030. //*****************************************************************************
  1031. //
  1032. //! Determines if there is any space in the transmit FIFO.
  1033. //!
  1034. //! \param ui32Base is the base address of the UART port.
  1035. //!
  1036. //! This function returns a flag indicating whether or not there is space
  1037. //! available in the transmit FIFO.
  1038. //!
  1039. //! \return Returns \b true if there is space available in the transmit FIFO
  1040. //! or \b false if there is no space available in the transmit FIFO.
  1041. //
  1042. //*****************************************************************************
  1043. bool
  1044. UARTSpaceAvail(uint32_t ui32Base)
  1045. {
  1046. //
  1047. // Check the arguments.
  1048. //
  1049. ASSERT(_UARTBaseValid(ui32Base));
  1050. //
  1051. // Return the availability of space.
  1052. //
  1053. return((HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF) ? false : true);
  1054. }
  1055. //*****************************************************************************
  1056. //
  1057. //! Receives a character from the specified port.
  1058. //!
  1059. //! \param ui32Base is the base address of the UART port.
  1060. //!
  1061. //! This function gets a character from the receive FIFO for the specified
  1062. //! port.
  1063. //!
  1064. //! \return Returns the character read from the specified port, cast as a
  1065. //! \e int32_t. A \b -1 is returned if there are no characters present in the
  1066. //! receive FIFO. The UARTCharsAvail() function should be called before
  1067. //! attempting to call this function.
  1068. //
  1069. //*****************************************************************************
  1070. int32_t
  1071. UARTCharGetNonBlocking(uint32_t ui32Base)
  1072. {
  1073. //
  1074. // Check the arguments.
  1075. //
  1076. ASSERT(_UARTBaseValid(ui32Base));
  1077. //
  1078. // See if there are any characters in the receive FIFO.
  1079. //
  1080. if(!(HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE))
  1081. {
  1082. //
  1083. // Read and return the next character.
  1084. //
  1085. return(HWREG(ui32Base + UART_O_DR));
  1086. }
  1087. else
  1088. {
  1089. //
  1090. // There are no characters, so return a failure.
  1091. //
  1092. return(-1);
  1093. }
  1094. }
  1095. //*****************************************************************************
  1096. //
  1097. //! Waits for 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. If there are no characters available, this function waits until a
  1103. //! character is received before returning.
  1104. //!
  1105. //! \return Returns the character read from the specified port, cast as a
  1106. //! \e int32_t.
  1107. //
  1108. //*****************************************************************************
  1109. int32_t
  1110. UARTCharGet(uint32_t ui32Base)
  1111. {
  1112. //
  1113. // Check the arguments.
  1114. //
  1115. ASSERT(_UARTBaseValid(ui32Base));
  1116. //
  1117. // Wait until a char is available.
  1118. //
  1119. while(HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE)
  1120. {
  1121. }
  1122. //
  1123. // Now get the char.
  1124. //
  1125. return(HWREG(ui32Base + UART_O_DR));
  1126. }
  1127. //*****************************************************************************
  1128. //
  1129. //! Sends a character to the specified port.
  1130. //!
  1131. //! \param ui32Base is the base address of the UART port.
  1132. //! \param ucData is the character to be transmitted.
  1133. //!
  1134. //! This function writes the character \e ucData to the transmit FIFO for the
  1135. //! specified port. This function does not block, so if there is no space
  1136. //! available, then a \b false is returned and the application must retry the
  1137. //! function later.
  1138. //!
  1139. //! \return Returns \b true if the character was successfully placed in the
  1140. //! transmit FIFO or \b false if there was no space available in the transmit
  1141. //! FIFO.
  1142. //
  1143. //*****************************************************************************
  1144. bool
  1145. UARTCharPutNonBlocking(uint32_t ui32Base, unsigned char ucData)
  1146. {
  1147. //
  1148. // Check the arguments.
  1149. //
  1150. ASSERT(_UARTBaseValid(ui32Base));
  1151. //
  1152. // See if there is space in the transmit FIFO.
  1153. //
  1154. if(!(HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF))
  1155. {
  1156. //
  1157. // Write this character to the transmit FIFO.
  1158. //
  1159. HWREG(ui32Base + UART_O_DR) = ucData;
  1160. //
  1161. // Success.
  1162. //
  1163. return(true);
  1164. }
  1165. else
  1166. {
  1167. //
  1168. // There is no space in the transmit FIFO, so return a failure.
  1169. //
  1170. return(false);
  1171. }
  1172. }
  1173. //*****************************************************************************
  1174. //
  1175. //! Waits to send a character from the specified port.
  1176. //!
  1177. //! \param ui32Base is the base address of the UART port.
  1178. //! \param ucData is the character to be transmitted.
  1179. //!
  1180. //! This function sends the character \e ucData to the transmit FIFO for the
  1181. //! specified port. If there is no space available in the transmit FIFO, this
  1182. //! function waits until there is space available before returning.
  1183. //!
  1184. //! \return None.
  1185. //
  1186. //*****************************************************************************
  1187. void
  1188. UARTCharPut(uint32_t ui32Base, unsigned char ucData)
  1189. {
  1190. //
  1191. // Check the arguments.
  1192. //
  1193. ASSERT(_UARTBaseValid(ui32Base));
  1194. //
  1195. // Wait until space is available.
  1196. //
  1197. while(HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF)
  1198. {
  1199. }
  1200. //
  1201. // Send the char.
  1202. //
  1203. HWREG(ui32Base + UART_O_DR) = ucData;
  1204. }
  1205. //*****************************************************************************
  1206. //
  1207. //! Causes a BREAK to be sent.
  1208. //!
  1209. //! \param ui32Base is the base address of the UART port.
  1210. //! \param bBreakState controls the output level.
  1211. //!
  1212. //! Calling this function with \e bBreakState set to \b true asserts a break
  1213. //! condition on the UART. Calling this function with \e bBreakState set to
  1214. //! \b false removes the break condition. For proper transmission of a break
  1215. //! command, the break must be asserted for at least two complete frames.
  1216. //!
  1217. //! \return None.
  1218. //
  1219. //*****************************************************************************
  1220. void
  1221. UARTBreakCtl(uint32_t ui32Base, bool bBreakState)
  1222. {
  1223. //
  1224. // Check the arguments.
  1225. //
  1226. ASSERT(_UARTBaseValid(ui32Base));
  1227. //
  1228. // Set the break condition as requested.
  1229. //
  1230. HWREG(ui32Base + UART_O_LCRH) =
  1231. (bBreakState ?
  1232. (HWREG(ui32Base + UART_O_LCRH) | UART_LCRH_BRK) :
  1233. (HWREG(ui32Base + UART_O_LCRH) & ~(UART_LCRH_BRK)));
  1234. }
  1235. //*****************************************************************************
  1236. //
  1237. //! Determines whether the UART transmitter is busy or not.
  1238. //!
  1239. //! \param ui32Base is the base address of the UART port.
  1240. //!
  1241. //! This function allows the caller to determine whether all transmitted bytes
  1242. //! have cleared the transmitter hardware. If \b false is returned, the
  1243. //! transmit FIFO is empty and all bits of the last transmitted character,
  1244. //! including all stop bits, have left the hardware shift register.
  1245. //!
  1246. //! \return Returns \b true if the UART is transmitting or \b false if all
  1247. //! transmissions are complete.
  1248. //
  1249. //*****************************************************************************
  1250. bool
  1251. UARTBusy(uint32_t ui32Base)
  1252. {
  1253. //
  1254. // Check the argument.
  1255. //
  1256. ASSERT(_UARTBaseValid(ui32Base));
  1257. //
  1258. // Determine if the UART is busy.
  1259. //
  1260. return((HWREG(ui32Base + UART_O_FR) & UART_FR_BUSY) ? true : false);
  1261. }
  1262. //*****************************************************************************
  1263. //
  1264. //! Registers an interrupt handler for a UART interrupt.
  1265. //!
  1266. //! \param ui32Base is the base address of the UART port.
  1267. //! \param pfnHandler is a pointer to the function to be called when the
  1268. //! UART interrupt occurs.
  1269. //!
  1270. //! This function does the actual registering of the interrupt handler. This
  1271. //! function enables the global interrupt in the interrupt controller; specific
  1272. //! UART interrupts must be enabled via UARTIntEnable(). It is the interrupt
  1273. //! handler's responsibility to clear the interrupt source.
  1274. //!
  1275. //! \sa IntRegister() for important information about registering interrupt
  1276. //! handlers.
  1277. //!
  1278. //! \return None.
  1279. //
  1280. //*****************************************************************************
  1281. void
  1282. UARTIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
  1283. {
  1284. uint32_t ui32Int;
  1285. //
  1286. // Check the arguments.
  1287. //
  1288. ASSERT(_UARTBaseValid(ui32Base));
  1289. //
  1290. // Determine the interrupt number based on the UART port.
  1291. //
  1292. ui32Int = _UARTIntNumberGet(ui32Base);
  1293. ASSERT(ui32Int != 0);
  1294. //
  1295. // Register the interrupt handler.
  1296. //
  1297. IntRegister(ui32Int, pfnHandler);
  1298. //
  1299. // Enable the UART interrupt.
  1300. //
  1301. IntEnable(ui32Int);
  1302. }
  1303. //*****************************************************************************
  1304. //
  1305. //! Unregisters an interrupt handler for a UART interrupt.
  1306. //!
  1307. //! \param ui32Base is the base address of the UART port.
  1308. //!
  1309. //! This function does the actual unregistering of the interrupt handler. It
  1310. //! clears the handler to be called when a UART interrupt occurs. This
  1311. //! function also masks off the interrupt in the interrupt controller so that
  1312. //! the interrupt handler no longer is called.
  1313. //!
  1314. //! \sa IntRegister() for important information about registering interrupt
  1315. //! handlers.
  1316. //!
  1317. //! \return None.
  1318. //
  1319. //*****************************************************************************
  1320. void
  1321. UARTIntUnregister(uint32_t ui32Base)
  1322. {
  1323. uint32_t ui32Int;
  1324. //
  1325. // Check the arguments.
  1326. //
  1327. ASSERT(_UARTBaseValid(ui32Base));
  1328. //
  1329. // Determine the interrupt number based on the UART port.
  1330. //
  1331. ui32Int = _UARTIntNumberGet(ui32Base);
  1332. ASSERT(ui32Int != 0);
  1333. //
  1334. // Disable the interrupt.
  1335. //
  1336. IntDisable(ui32Int);
  1337. //
  1338. // Unregister the interrupt handler.
  1339. //
  1340. IntUnregister(ui32Int);
  1341. }
  1342. //*****************************************************************************
  1343. //
  1344. //! Enables individual UART interrupt sources.
  1345. //!
  1346. //! \param ui32Base is the base address of the UART port.
  1347. //! \param ui32IntFlags is the bit mask of the interrupt sources to be enabled.
  1348. //!
  1349. //! This function enables the indicated UART interrupt sources. Only the
  1350. //! sources that are enabled can be reflected to the processor interrupt;
  1351. //! disabled sources have no effect on the processor.
  1352. //!
  1353. //! The \e ui32IntFlags parameter is the logical OR of any of the following:
  1354. //!
  1355. //! - \b UART_INT_9BIT - 9-bit Address Match interrupt
  1356. //! - \b UART_INT_OE - Overrun Error interrupt
  1357. //! - \b UART_INT_BE - Break Error interrupt
  1358. //! - \b UART_INT_PE - Parity Error interrupt
  1359. //! - \b UART_INT_FE - Framing Error interrupt
  1360. //! - \b UART_INT_RT - Receive Timeout interrupt
  1361. //! - \b UART_INT_TX - Transmit interrupt
  1362. //! - \b UART_INT_RX - Receive interrupt
  1363. //! - \b UART_INT_DSR - DSR interrupt
  1364. //! - \b UART_INT_DCD - DCD interrupt
  1365. //! - \b UART_INT_CTS - CTS interrupt
  1366. //! - \b UART_INT_RI - RI interrupt
  1367. //!
  1368. //! \return None.
  1369. //
  1370. //*****************************************************************************
  1371. void
  1372. UARTIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
  1373. {
  1374. //
  1375. // Check the arguments.
  1376. //
  1377. ASSERT(_UARTBaseValid(ui32Base));
  1378. //
  1379. // Enable the specified interrupts.
  1380. //
  1381. HWREG(ui32Base + UART_O_IM) |= ui32IntFlags;
  1382. }
  1383. //*****************************************************************************
  1384. //
  1385. //! Disables individual UART interrupt sources.
  1386. //!
  1387. //! \param ui32Base is the base address of the UART port.
  1388. //! \param ui32IntFlags is the bit mask of the interrupt sources to be
  1389. //! disabled.
  1390. //!
  1391. //! This function disables the indicated UART interrupt sources. Only the
  1392. //! sources that are enabled can be reflected to the processor interrupt;
  1393. //! disabled sources have no effect on the processor.
  1394. //!
  1395. //! The \e ui32IntFlags parameter has the same definition as the
  1396. //! \e ui32IntFlags parameter to UARTIntEnable().
  1397. //!
  1398. //! \return None.
  1399. //
  1400. //*****************************************************************************
  1401. void
  1402. UARTIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
  1403. {
  1404. //
  1405. // Check the arguments.
  1406. //
  1407. ASSERT(_UARTBaseValid(ui32Base));
  1408. //
  1409. // Disable the specified interrupts.
  1410. //
  1411. HWREG(ui32Base + UART_O_IM) &= ~(ui32IntFlags);
  1412. }
  1413. //*****************************************************************************
  1414. //
  1415. //! Gets the current interrupt status.
  1416. //!
  1417. //! \param ui32Base is the base address of the UART port.
  1418. //! \param bMasked is \b false if the raw interrupt status is required and
  1419. //! \b true if the masked interrupt status is required.
  1420. //!
  1421. //! This function returns the interrupt status for the specified UART. Either
  1422. //! the raw interrupt status or the status of interrupts that are allowed to
  1423. //! reflect to the processor can be returned.
  1424. //!
  1425. //! \return Returns the current interrupt status, enumerated as a bit field of
  1426. //! values described in UARTIntEnable().
  1427. //
  1428. //*****************************************************************************
  1429. uint32_t
  1430. UARTIntStatus(uint32_t ui32Base, bool bMasked)
  1431. {
  1432. //
  1433. // Check the arguments.
  1434. //
  1435. ASSERT(_UARTBaseValid(ui32Base));
  1436. //
  1437. // Return either the interrupt status or the raw interrupt status as
  1438. // requested.
  1439. //
  1440. if(bMasked)
  1441. {
  1442. return(HWREG(ui32Base + UART_O_MIS));
  1443. }
  1444. else
  1445. {
  1446. return(HWREG(ui32Base + UART_O_RIS));
  1447. }
  1448. }
  1449. //*****************************************************************************
  1450. //
  1451. //! Clears UART interrupt sources.
  1452. //!
  1453. //! \param ui32Base is the base address of the UART port.
  1454. //! \param ui32IntFlags is a bit mask of the interrupt sources to be cleared.
  1455. //!
  1456. //! The specified UART interrupt sources are cleared, so that they no longer
  1457. //! assert. This function must be called in the interrupt handler to keep the
  1458. //! interrupt from being triggered again immediately upon exit.
  1459. //!
  1460. //! The \e ui32IntFlags parameter has the same definition as the
  1461. //! \e ui32IntFlags parameter to UARTIntEnable().
  1462. //!
  1463. //! \note Because there is a write buffer in the Cortex-M processor, it may
  1464. //! take several clock cycles before the interrupt source is actually cleared.
  1465. //! Therefore, it is recommended that the interrupt source be cleared early in
  1466. //! the interrupt handler (as opposed to the very last action) to avoid
  1467. //! returning from the interrupt handler before the interrupt source is
  1468. //! actually cleared. Failure to do so may result in the interrupt handler
  1469. //! being immediately reentered (because the interrupt controller still sees
  1470. //! the interrupt source asserted).
  1471. //!
  1472. //! \return None.
  1473. //
  1474. //*****************************************************************************
  1475. void
  1476. UARTIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
  1477. {
  1478. //
  1479. // Check the arguments.
  1480. //
  1481. ASSERT(_UARTBaseValid(ui32Base));
  1482. //
  1483. // Clear the requested interrupt sources.
  1484. //
  1485. HWREG(ui32Base + UART_O_ICR) = ui32IntFlags;
  1486. }
  1487. //*****************************************************************************
  1488. //
  1489. //! Enable UART uDMA operation.
  1490. //!
  1491. //! \param ui32Base is the base address of the UART port.
  1492. //! \param ui32DMAFlags is a bit mask of the uDMA features to enable.
  1493. //!
  1494. //! The specified UART uDMA features are enabled. The UART can be
  1495. //! configured to use uDMA for transmit or receive and to disable
  1496. //! receive if an error occurs. The \e ui32DMAFlags parameter is the
  1497. //! logical OR of any of the following values:
  1498. //!
  1499. //! - \b UART_DMA_RX - enable uDMA for receive
  1500. //! - \b UART_DMA_TX - enable uDMA for transmit
  1501. //! - \b UART_DMA_ERR_RXSTOP - disable uDMA receive on UART error
  1502. //!
  1503. //! \note The uDMA controller must also be set up before DMA can be used
  1504. //! with the UART.
  1505. //!
  1506. //! \return None.
  1507. //
  1508. //*****************************************************************************
  1509. void
  1510. UARTDMAEnable(uint32_t ui32Base, uint32_t ui32DMAFlags)
  1511. {
  1512. //
  1513. // Check the arguments.
  1514. //
  1515. ASSERT(_UARTBaseValid(ui32Base));
  1516. //
  1517. // Set the requested bits in the UART uDMA control register.
  1518. //
  1519. HWREG(ui32Base + UART_O_DMACTL) |= ui32DMAFlags;
  1520. }
  1521. //*****************************************************************************
  1522. //
  1523. //! Disable UART uDMA operation.
  1524. //!
  1525. //! \param ui32Base is the base address of the UART port.
  1526. //! \param ui32DMAFlags is a bit mask of the uDMA features to disable.
  1527. //!
  1528. //! This function is used to disable UART uDMA features that were enabled
  1529. //! by UARTDMAEnable(). The specified UART uDMA features are disabled. The
  1530. //! \e ui32DMAFlags parameter is the logical OR of any of the following values:
  1531. //!
  1532. //! - \b UART_DMA_RX - disable uDMA for receive
  1533. //! - \b UART_DMA_TX - disable uDMA for transmit
  1534. //! - \b UART_DMA_ERR_RXSTOP - do not disable uDMA receive on UART error
  1535. //!
  1536. //! \return None.
  1537. //
  1538. //*****************************************************************************
  1539. void
  1540. UARTDMADisable(uint32_t ui32Base, uint32_t ui32DMAFlags)
  1541. {
  1542. //
  1543. // Check the arguments.
  1544. //
  1545. ASSERT(_UARTBaseValid(ui32Base));
  1546. //
  1547. // Clear the requested bits in the UART uDMA control register.
  1548. //
  1549. HWREG(ui32Base + UART_O_DMACTL) &= ~ui32DMAFlags;
  1550. }
  1551. //*****************************************************************************
  1552. //
  1553. //! Gets current receiver errors.
  1554. //!
  1555. //! \param ui32Base is the base address of the UART port.
  1556. //!
  1557. //! This function returns the current state of each of the 4 receiver error
  1558. //! sources. The returned errors are equivalent to the four error bits
  1559. //! returned via the previous call to UARTCharGet() or UARTCharGetNonBlocking()
  1560. //! with the exception that the overrun error is set immediately when the
  1561. //! overrun occurs rather than when a character is next read.
  1562. //!
  1563. //! \return Returns a logical OR combination of the receiver error flags,
  1564. //! \b UART_RXERROR_FRAMING, \b UART_RXERROR_PARITY, \b UART_RXERROR_BREAK
  1565. //! and \b UART_RXERROR_OVERRUN.
  1566. //
  1567. //*****************************************************************************
  1568. uint32_t
  1569. UARTRxErrorGet(uint32_t ui32Base)
  1570. {
  1571. //
  1572. // Check the arguments.
  1573. //
  1574. ASSERT(_UARTBaseValid(ui32Base));
  1575. //
  1576. // Return the current value of the receive status register.
  1577. //
  1578. return(HWREG(ui32Base + UART_O_RSR) & 0x0000000F);
  1579. }
  1580. //*****************************************************************************
  1581. //
  1582. //! Clears all reported receiver errors.
  1583. //!
  1584. //! \param ui32Base is the base address of the UART port.
  1585. //!
  1586. //! This function is used to clear all receiver error conditions reported via
  1587. //! UARTRxErrorGet(). If using the overrun, framing error, parity error or
  1588. //! break interrupts, this function must be called after clearing the interrupt
  1589. //! to ensure that later errors of the same type trigger another interrupt.
  1590. //!
  1591. //! \return None.
  1592. //
  1593. //*****************************************************************************
  1594. void
  1595. UARTRxErrorClear(uint32_t ui32Base)
  1596. {
  1597. //
  1598. // Check the arguments.
  1599. //
  1600. ASSERT(_UARTBaseValid(ui32Base));
  1601. //
  1602. // Any write to the Error Clear Register clears all bits which are
  1603. // currently set.
  1604. //
  1605. HWREG(ui32Base + UART_O_ECR) = 0;
  1606. }
  1607. //*****************************************************************************
  1608. //
  1609. //! Sets the baud clock source for the specified UART.
  1610. //!
  1611. //! \param ui32Base is the base address of the UART port.
  1612. //! \param ui32Source is the baud clock source for the UART.
  1613. //!
  1614. //! This function allows the baud clock source for the UART to be selected.
  1615. //! The possible clock source are the system clock (\b UART_CLOCK_SYSTEM) or
  1616. //! the precision internal oscillator (\b UART_CLOCK_PIOSC).
  1617. //!
  1618. //! Changing the baud clock source changes the baud rate generated by the
  1619. //! UART. Therefore, the baud rate should be reconfigured after any change to
  1620. //! the baud clock source.
  1621. //!
  1622. //! \note The ability to specify the UART baud clock source varies with the
  1623. //! Tiva part in use. Please consult the datasheet for the part you are
  1624. //! using to determine whether this support is available.
  1625. //!
  1626. //! \return None.
  1627. //
  1628. //*****************************************************************************
  1629. void
  1630. UARTClockSourceSet(uint32_t ui32Base, uint32_t ui32Source)
  1631. {
  1632. //
  1633. // Check the arguments.
  1634. //
  1635. ASSERT(_UARTBaseValid(ui32Base));
  1636. ASSERT((ui32Source == UART_CLOCK_SYSTEM) ||
  1637. (ui32Source == UART_CLOCK_PIOSC));
  1638. //
  1639. // Set the UART clock source.
  1640. //
  1641. HWREG(ui32Base + UART_O_CC) = ui32Source;
  1642. }
  1643. //*****************************************************************************
  1644. //
  1645. //! Gets the baud clock source for the specified UART.
  1646. //!
  1647. //! \param ui32Base is the base address of the UART port.
  1648. //!
  1649. //! This function returns the baud clock source for the specified UART. The
  1650. //! possible baud clock source are the system clock (\b UART_CLOCK_SYSTEM) or
  1651. //! the precision internal oscillator (\b UART_CLOCK_PIOSC).
  1652. //!
  1653. //! \note The ability to specify the UART baud clock source varies with the
  1654. //! Tiva part in use. Please consult the datasheet for the part you are
  1655. //! using to determine whether this support is available.
  1656. //!
  1657. //! \return None.
  1658. //
  1659. //*****************************************************************************
  1660. uint32_t
  1661. UARTClockSourceGet(uint32_t ui32Base)
  1662. {
  1663. //
  1664. // Check the arguments.
  1665. //
  1666. ASSERT(_UARTBaseValid(ui32Base));
  1667. //
  1668. // Return the UART clock source.
  1669. //
  1670. return(HWREG(ui32Base + UART_O_CC));
  1671. }
  1672. //*****************************************************************************
  1673. //
  1674. //! Enables 9-bit mode on the specified UART.
  1675. //!
  1676. //! \param ui32Base is the base address of the UART port.
  1677. //!
  1678. //! This function enables the 9-bit operational mode of the UART.
  1679. //!
  1680. //! \note The availability of 9-bit mode varies with the Tiva part in use.
  1681. //! Please consult the datasheet for the part you are using to determine
  1682. //! whether this support is available.
  1683. //!
  1684. //! \return None.
  1685. //
  1686. //*****************************************************************************
  1687. void
  1688. UART9BitEnable(uint32_t ui32Base)
  1689. {
  1690. //
  1691. // Check the arguments.
  1692. //
  1693. ASSERT(_UARTBaseValid(ui32Base));
  1694. //
  1695. // Enable 9-bit mode.
  1696. //
  1697. HWREG(ui32Base + UART_O_9BITADDR) |= UART_9BITADDR_9BITEN;
  1698. }
  1699. //*****************************************************************************
  1700. //
  1701. //! Disables 9-bit mode on the specified UART.
  1702. //!
  1703. //! \param ui32Base is the base address of the UART port.
  1704. //!
  1705. //! This function disables the 9-bit operational mode of the UART.
  1706. //!
  1707. //! \note The availability of 9-bit mode varies with the Tiva part in use.
  1708. //! Please consult the datasheet for the part you are using to determine
  1709. //! whether this support is available.
  1710. //!
  1711. //! \return None.
  1712. //
  1713. //*****************************************************************************
  1714. void
  1715. UART9BitDisable(uint32_t ui32Base)
  1716. {
  1717. //
  1718. // Check the arguments.
  1719. //
  1720. ASSERT(_UARTBaseValid(ui32Base));
  1721. //
  1722. // Disable 9-bit mode.
  1723. //
  1724. HWREG(ui32Base + UART_O_9BITADDR) &= ~UART_9BITADDR_9BITEN;
  1725. }
  1726. //*****************************************************************************
  1727. //
  1728. //! Sets the device address(es) for 9-bit mode.
  1729. //!
  1730. //! \param ui32Base is the base address of the UART port.
  1731. //! \param ui8Addr is the device address.
  1732. //! \param ui8Mask is the device address mask.
  1733. //!
  1734. //! This function configures the device address or range of device addresses
  1735. //! that respond to requests on the 9-bit UART port. The received address is
  1736. //! masked with the mask and then compared against the given address, allowing
  1737. //! either a single address (if \b ui8Mask is 0xff) or a set of addresses to be
  1738. //! matched.
  1739. //!
  1740. //! \note The availability of 9-bit mode varies with the Tiva part in use.
  1741. //! Please consult the datasheet for the part you are using to determine
  1742. //! whether this support is available.
  1743. //!
  1744. //! \return None.
  1745. //
  1746. //*****************************************************************************
  1747. void
  1748. UART9BitAddrSet(uint32_t ui32Base, uint8_t ui8Addr,
  1749. uint8_t ui8Mask)
  1750. {
  1751. //
  1752. // Check the arguments.
  1753. //
  1754. ASSERT(_UARTBaseValid(ui32Base));
  1755. //
  1756. // Set the address and mask.
  1757. //
  1758. HWREG(ui32Base + UART_O_9BITADDR) = ui8Addr << UART_9BITADDR_ADDR_S;
  1759. HWREG(ui32Base + UART_O_9BITAMASK) = ui8Mask << UART_9BITAMASK_MASK_S;
  1760. }
  1761. //*****************************************************************************
  1762. //
  1763. //! Sends an address character from the specified port when operating in 9-bit
  1764. //! mode.
  1765. //!
  1766. //! \param ui32Base is the base address of the UART port.
  1767. //! \param ui8Addr is the address to be transmitted.
  1768. //!
  1769. //! This function waits until all data has been sent from the specified port
  1770. //! and then sends the given address as an address byte. It then waits until
  1771. //! the address byte has been transmitted before returning.
  1772. //!
  1773. //! The normal data functions (UARTCharPut(), UARTCharPutNonBlocking(),
  1774. //! UARTCharGet(), and UARTCharGetNonBlocking()) are used to send and receive
  1775. //! data characters in 9-bit mode.
  1776. //!
  1777. //! \note The availability of 9-bit mode varies with the Tiva part in use.
  1778. //! Please consult the datasheet for the part you are using to determine
  1779. //! whether this support is available.
  1780. //!
  1781. //! \return None.
  1782. //
  1783. //*****************************************************************************
  1784. void
  1785. UART9BitAddrSend(uint32_t ui32Base, uint8_t ui8Addr)
  1786. {
  1787. uint32_t ui32LCRH;
  1788. //
  1789. // Check the arguments.
  1790. //
  1791. ASSERT(_UARTBaseValid(ui32Base));
  1792. //
  1793. // Wait until the FIFO is empty and the UART is not busy.
  1794. //
  1795. while((HWREG(ui32Base + UART_O_FR) & (UART_FR_TXFE | UART_FR_BUSY)) !=
  1796. UART_FR_TXFE)
  1797. {
  1798. }
  1799. //
  1800. // Force the address/data bit to 1 to indicate this is an address byte.
  1801. //
  1802. ui32LCRH = HWREG(ui32Base + UART_O_LCRH);
  1803. HWREG(ui32Base + UART_O_LCRH) = ((ui32LCRH & ~UART_LCRH_EPS) |
  1804. UART_LCRH_SPS | UART_LCRH_PEN);
  1805. //
  1806. // Send the address.
  1807. //
  1808. HWREG(ui32Base + UART_O_DR) = ui8Addr;
  1809. //
  1810. // Wait until the address has been sent.
  1811. //
  1812. while((HWREG(ui32Base + UART_O_FR) & (UART_FR_TXFE | UART_FR_BUSY)) !=
  1813. UART_FR_TXFE)
  1814. {
  1815. }
  1816. //
  1817. // Restore the address/data setting.
  1818. //
  1819. HWREG(ui32Base + UART_O_LCRH) = ui32LCRH;
  1820. }
  1821. //*****************************************************************************
  1822. //
  1823. //! Enables internal loopback mode for a UART port
  1824. //!
  1825. //! \param ui32Base is the base address of the UART port.
  1826. //!
  1827. //! This function configures a UART port in internal loopback mode to help with
  1828. //! diagnostics and debug. In this mode, the transmit and receive terminals of
  1829. //! the same UART port are internally connected. Hence, the data transmitted
  1830. //! on the UnTx output is received on the UxRx input, without having to go
  1831. //! through I/O's. UARTCharPut(), UARTCharGet() functions can be used along
  1832. //! with this function.
  1833. //!
  1834. //! \return None.
  1835. //
  1836. //*****************************************************************************
  1837. void UARTLoopbackEnable(uint32_t ui32Base)
  1838. {
  1839. //
  1840. // Check the arguments.
  1841. //
  1842. ASSERT(_UARTBaseValid(ui32Base));
  1843. //
  1844. // Write the Loopback Enable bit to register.
  1845. //
  1846. HWREG(ui32Base + UART_O_CTL) |= UART_CTL_LBE;
  1847. }
  1848. //*****************************************************************************
  1849. //
  1850. // Close the Doxygen group.
  1851. //! @}
  1852. //
  1853. //*****************************************************************************