uart.c 58 KB

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