My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lpc17xx_uart.c 41KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. /**********************************************************************
  2. * $Id$ lpc17xx_uart.c 2011-06-06
  3. *//**
  4. * @file lpc17xx_uart.c
  5. * @brief Contains all functions support for UART firmware library
  6. * on LPC17xx
  7. * @version 3.2
  8. * @date 25. July. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. /* Peripheral group ----------------------------------------------------------- */
  33. /** @addtogroup UART
  34. * @{
  35. */
  36. /* Includes ------------------------------------------------------------------- */
  37. #include "lpc17xx_uart.h"
  38. #include "lpc17xx_clkpwr.h"
  39. /* If this source file built with example, the LPC17xx FW library configuration
  40. * file in each example directory ("lpc17xx_libcfg.h") must be included,
  41. * otherwise the default FW library configuration file must be included instead
  42. */
  43. #ifdef __BUILD_WITH_EXAMPLE__
  44. #include "lpc17xx_libcfg.h"
  45. #else
  46. #include "lpc17xx_libcfg_default.h"
  47. #endif /* __BUILD_WITH_EXAMPLE__ */
  48. #ifdef _UART
  49. /* Private Functions ---------------------------------------------------------- */
  50. static Status uart_set_divisors(LPC_UART_TypeDef *UARTx, uint32_t baudrate);
  51. /*********************************************************************//**
  52. * @brief Determines best dividers to get a target clock rate
  53. * @param[in] UARTx Pointer to selected UART peripheral, should be:
  54. * - LPC_UART0: UART0 peripheral
  55. * - LPC_UART1: UART1 peripheral
  56. * - LPC_UART2: UART2 peripheral
  57. * - LPC_UART3: UART3 peripheral
  58. * @param[in] baudrate Desired UART baud rate.
  59. * @return Error status, could be:
  60. * - SUCCESS
  61. * - ERROR
  62. **********************************************************************/
  63. static Status uart_set_divisors(LPC_UART_TypeDef *UARTx, uint32_t baudrate)
  64. {
  65. Status errorStatus = ERROR;
  66. uint32_t uClk;
  67. uint32_t d, m, bestd, bestm, tmp;
  68. uint64_t best_divisor, divisor;
  69. uint32_t current_error, best_error;
  70. uint32_t recalcbaud;
  71. /* get UART block clock */
  72. if (UARTx == (LPC_UART_TypeDef *)LPC_UART0)
  73. {
  74. uClk = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_UART0);
  75. }
  76. else if (UARTx == (LPC_UART_TypeDef *)LPC_UART1)
  77. {
  78. uClk = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_UART1);
  79. }
  80. else if (UARTx == LPC_UART2)
  81. {
  82. uClk = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_UART2);
  83. }
  84. else if (UARTx == LPC_UART3)
  85. {
  86. uClk = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_UART3);
  87. }
  88. /* In the Uart IP block, baud rate is calculated using FDR and DLL-DLM registers
  89. * The formula is :
  90. * BaudRate= uClk * (mulFracDiv/(mulFracDiv+dividerAddFracDiv) / (16 * (DLL)
  91. * It involves floating point calculations. That's the reason the formulae are adjusted with
  92. * Multiply and divide method.*/
  93. /* The value of mulFracDiv and dividerAddFracDiv should comply to the following expressions:
  94. * 0 < mulFracDiv <= 15, 0 <= dividerAddFracDiv <= 15 */
  95. best_error = 0xFFFFFFFF; /* Worst case */
  96. bestd = 0;
  97. bestm = 0;
  98. best_divisor = 0;
  99. for (m = 1 ; m <= 15 ;m++)
  100. {
  101. for (d = 0 ; d < m ; d++)
  102. {
  103. divisor = ((uint64_t)uClk<<28)*m/(baudrate*(m+d));
  104. current_error = divisor & 0xFFFFFFFF;
  105. tmp = divisor>>32;
  106. /* Adjust error */
  107. if(current_error > ((uint32_t)1<<31)){
  108. current_error = -current_error;
  109. tmp++;
  110. }
  111. if(tmp<1 || tmp>65536) /* Out of range */
  112. continue;
  113. if( current_error < best_error){
  114. best_error = current_error;
  115. best_divisor = tmp;
  116. bestd = d;
  117. bestm = m;
  118. if(best_error == 0) break;
  119. }
  120. } /* end of inner for loop */
  121. if (best_error == 0)
  122. break;
  123. } /* end of outer for loop */
  124. if(best_divisor == 0) return ERROR; /* can not find best match */
  125. recalcbaud = (uClk>>4) * bestm/(best_divisor * (bestm + bestd));
  126. /* reuse best_error to evaluate baud error*/
  127. if(baudrate>recalcbaud) best_error = baudrate - recalcbaud;
  128. else best_error = recalcbaud -baudrate;
  129. best_error = best_error * 100 / baudrate;
  130. if (best_error < UART_ACCEPTED_BAUDRATE_ERROR)
  131. {
  132. if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  133. {
  134. ((LPC_UART1_TypeDef *)UARTx)->LCR |= UART_LCR_DLAB_EN;
  135. ((LPC_UART1_TypeDef *)UARTx)->/*DLIER.*/DLM = UART_LOAD_DLM(best_divisor);
  136. ((LPC_UART1_TypeDef *)UARTx)->/*RBTHDLR.*/DLL = UART_LOAD_DLL(best_divisor);
  137. /* Then reset DLAB bit */
  138. ((LPC_UART1_TypeDef *)UARTx)->LCR &= (~UART_LCR_DLAB_EN) & UART_LCR_BITMASK;
  139. ((LPC_UART1_TypeDef *)UARTx)->FDR = (UART_FDR_MULVAL(bestm) \
  140. | UART_FDR_DIVADDVAL(bestd)) & UART_FDR_BITMASK;
  141. }
  142. else
  143. {
  144. UARTx->LCR |= UART_LCR_DLAB_EN;
  145. UARTx->/*DLIER.*/DLM = UART_LOAD_DLM(best_divisor);
  146. UARTx->/*RBTHDLR.*/DLL = UART_LOAD_DLL(best_divisor);
  147. /* Then reset DLAB bit */
  148. UARTx->LCR &= (~UART_LCR_DLAB_EN) & UART_LCR_BITMASK;
  149. UARTx->FDR = (UART_FDR_MULVAL(bestm) \
  150. | UART_FDR_DIVADDVAL(bestd)) & UART_FDR_BITMASK;
  151. }
  152. errorStatus = SUCCESS;
  153. }
  154. return errorStatus;
  155. }
  156. /* End of Private Functions ---------------------------------------------------- */
  157. /* Public Functions ----------------------------------------------------------- */
  158. /** @addtogroup UART_Public_Functions
  159. * @{
  160. */
  161. /* UART Init/DeInit functions -------------------------------------------------*/
  162. /********************************************************************//**
  163. * @brief Initializes the UARTx peripheral according to the specified
  164. * parameters in the UART_ConfigStruct.
  165. * @param[in] UARTx UART peripheral selected, should be:
  166. * - LPC_UART0: UART0 peripheral
  167. * - LPC_UART1: UART1 peripheral
  168. * - LPC_UART2: UART2 peripheral
  169. * - LPC_UART3: UART3 peripheral
  170. * @param[in] UART_ConfigStruct Pointer to a UART_CFG_Type structure
  171. * that contains the configuration information for the
  172. * specified UART peripheral.
  173. * @return None
  174. *********************************************************************/
  175. void UART_Init(LPC_UART_TypeDef *UARTx, UART_CFG_Type *UART_ConfigStruct)
  176. {
  177. uint32_t tmp;
  178. // For debug mode
  179. CHECK_PARAM(PARAM_UARTx(UARTx));
  180. CHECK_PARAM(PARAM_UART_DATABIT(UART_ConfigStruct->Databits));
  181. CHECK_PARAM(PARAM_UART_STOPBIT(UART_ConfigStruct->Stopbits));
  182. CHECK_PARAM(PARAM_UART_PARITY(UART_ConfigStruct->Parity));
  183. #ifdef _UART0
  184. if(UARTx == (LPC_UART_TypeDef *)LPC_UART0)
  185. {
  186. /* Set up clock and power for UART module */
  187. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART0, ENABLE);
  188. }
  189. #endif
  190. #ifdef _UART1
  191. if(((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  192. {
  193. /* Set up clock and power for UART module */
  194. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART1, ENABLE);
  195. }
  196. #endif
  197. #ifdef _UART2
  198. if(UARTx == LPC_UART2)
  199. {
  200. /* Set up clock and power for UART module */
  201. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART2, ENABLE);
  202. }
  203. #endif
  204. #ifdef _UART3
  205. if(UARTx == LPC_UART3)
  206. {
  207. /* Set up clock and power for UART module */
  208. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART3, ENABLE);
  209. }
  210. #endif
  211. if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  212. {
  213. /* FIFOs are empty */
  214. ((LPC_UART1_TypeDef *)UARTx)->/*IIFCR.*/FCR = ( UART_FCR_FIFO_EN \
  215. | UART_FCR_RX_RS | UART_FCR_TX_RS);
  216. // Disable FIFO
  217. ((LPC_UART1_TypeDef *)UARTx)->/*IIFCR.*/FCR = 0;
  218. // Dummy reading
  219. while (((LPC_UART1_TypeDef *)UARTx)->LSR & UART_LSR_RDR)
  220. {
  221. tmp = ((LPC_UART1_TypeDef *)UARTx)->/*RBTHDLR.*/RBR;
  222. }
  223. ((LPC_UART1_TypeDef *)UARTx)->TER = UART_TER_TXEN;
  224. // Wait for current transmit complete
  225. while (!(((LPC_UART1_TypeDef *)UARTx)->LSR & UART_LSR_THRE));
  226. // Disable Tx
  227. ((LPC_UART1_TypeDef *)UARTx)->TER = 0;
  228. // Disable interrupt
  229. ((LPC_UART1_TypeDef *)UARTx)->/*DLIER.*/IER = 0;
  230. // Set LCR to default state
  231. ((LPC_UART1_TypeDef *)UARTx)->LCR = 0;
  232. // Set ACR to default state
  233. ((LPC_UART1_TypeDef *)UARTx)->ACR = 0;
  234. // Set Modem Control to default state
  235. ((LPC_UART1_TypeDef *)UARTx)->MCR = 0;
  236. // Set RS485 control to default state
  237. ((LPC_UART1_TypeDef *)UARTx)->RS485CTRL = 0;
  238. // Set RS485 delay timer to default state
  239. ((LPC_UART1_TypeDef *)UARTx)->RS485DLY = 0;
  240. // Set RS485 addr match to default state
  241. ((LPC_UART1_TypeDef *)UARTx)->ADRMATCH = 0;
  242. //Dummy Reading to Clear Status
  243. tmp = ((LPC_UART1_TypeDef *)UARTx)->MSR;
  244. tmp = ((LPC_UART1_TypeDef *)UARTx)->LSR;
  245. }
  246. else
  247. {
  248. /* FIFOs are empty */
  249. UARTx->/*IIFCR.*/FCR = ( UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS);
  250. // Disable FIFO
  251. UARTx->/*IIFCR.*/FCR = 0;
  252. // Dummy reading
  253. while (UARTx->LSR & UART_LSR_RDR)
  254. {
  255. tmp = UARTx->/*RBTHDLR.*/RBR;
  256. }
  257. UARTx->TER = UART_TER_TXEN;
  258. // Wait for current transmit complete
  259. while (!(UARTx->LSR & UART_LSR_THRE));
  260. // Disable Tx
  261. UARTx->TER = 0;
  262. // Disable interrupt
  263. UARTx->/*DLIER.*/IER = 0;
  264. // Set LCR to default state
  265. UARTx->LCR = 0;
  266. // Set ACR to default state
  267. UARTx->ACR = 0;
  268. // Dummy reading
  269. tmp = UARTx->LSR;
  270. }
  271. if (UARTx == LPC_UART3)
  272. {
  273. // Set IrDA to default state
  274. UARTx->ICR = 0;
  275. }
  276. // Set Line Control register ----------------------------
  277. uart_set_divisors(UARTx, (UART_ConfigStruct->Baud_rate));
  278. if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  279. {
  280. tmp = (((LPC_UART1_TypeDef *)UARTx)->LCR & (UART_LCR_DLAB_EN | UART_LCR_BREAK_EN)) \
  281. & UART_LCR_BITMASK;
  282. }
  283. else
  284. {
  285. tmp = (UARTx->LCR & (UART_LCR_DLAB_EN | UART_LCR_BREAK_EN)) & UART_LCR_BITMASK;
  286. }
  287. switch (UART_ConfigStruct->Databits){
  288. case UART_DATABIT_5:
  289. tmp |= UART_LCR_WLEN5;
  290. break;
  291. case UART_DATABIT_6:
  292. tmp |= UART_LCR_WLEN6;
  293. break;
  294. case UART_DATABIT_7:
  295. tmp |= UART_LCR_WLEN7;
  296. break;
  297. case UART_DATABIT_8:
  298. default:
  299. tmp |= UART_LCR_WLEN8;
  300. break;
  301. }
  302. if (UART_ConfigStruct->Parity == UART_PARITY_NONE)
  303. {
  304. // Do nothing...
  305. }
  306. else
  307. {
  308. tmp |= UART_LCR_PARITY_EN;
  309. switch (UART_ConfigStruct->Parity)
  310. {
  311. case UART_PARITY_ODD:
  312. tmp |= UART_LCR_PARITY_ODD;
  313. break;
  314. case UART_PARITY_EVEN:
  315. tmp |= UART_LCR_PARITY_EVEN;
  316. break;
  317. case UART_PARITY_SP_1:
  318. tmp |= UART_LCR_PARITY_F_1;
  319. break;
  320. case UART_PARITY_SP_0:
  321. tmp |= UART_LCR_PARITY_F_0;
  322. break;
  323. default:
  324. break;
  325. }
  326. }
  327. switch (UART_ConfigStruct->Stopbits){
  328. case UART_STOPBIT_2:
  329. tmp |= UART_LCR_STOPBIT_SEL;
  330. break;
  331. case UART_STOPBIT_1:
  332. default:
  333. // Do no thing
  334. break;
  335. }
  336. // Write back to LCR, configure FIFO and Disable Tx
  337. if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  338. {
  339. ((LPC_UART1_TypeDef *)UARTx)->LCR = (uint8_t)(tmp & UART_LCR_BITMASK);
  340. }
  341. else
  342. {
  343. UARTx->LCR = (uint8_t)(tmp & UART_LCR_BITMASK);
  344. }
  345. }
  346. /*********************************************************************//**
  347. * @brief De-initializes the UARTx peripheral registers to their
  348. * default reset values.
  349. * @param[in] UARTx UART peripheral selected, should be:
  350. * - LPC_UART0: UART0 peripheral
  351. * - LPC_UART1: UART1 peripheral
  352. * - LPC_UART2: UART2 peripheral
  353. * - LPC_UART3: UART3 peripheral
  354. * @return None
  355. **********************************************************************/
  356. void UART_DeInit(LPC_UART_TypeDef* UARTx)
  357. {
  358. // For debug mode
  359. CHECK_PARAM(PARAM_UARTx(UARTx));
  360. UART_TxCmd(UARTx, DISABLE);
  361. #ifdef _UART0
  362. if (UARTx == (LPC_UART_TypeDef *)LPC_UART0)
  363. {
  364. /* Set up clock and power for UART module */
  365. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART0, DISABLE);
  366. }
  367. #endif
  368. #ifdef _UART1
  369. if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  370. {
  371. /* Set up clock and power for UART module */
  372. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART1, DISABLE);
  373. }
  374. #endif
  375. #ifdef _UART2
  376. if (UARTx == LPC_UART2)
  377. {
  378. /* Set up clock and power for UART module */
  379. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART2, DISABLE);
  380. }
  381. #endif
  382. #ifdef _UART3
  383. if (UARTx == LPC_UART3)
  384. {
  385. /* Set up clock and power for UART module */
  386. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART3, DISABLE);
  387. }
  388. #endif
  389. }
  390. /*****************************************************************************//**
  391. * @brief Fills each UART_InitStruct member with its default value:
  392. * - 9600 bps
  393. * - 8-bit data
  394. * - 1 Stopbit
  395. * - None Parity
  396. * @param[in] UART_InitStruct Pointer to a UART_CFG_Type structure
  397. * which will be initialized.
  398. * @return None
  399. *******************************************************************************/
  400. void UART_ConfigStructInit(UART_CFG_Type *UART_InitStruct)
  401. {
  402. UART_InitStruct->Baud_rate = 9600;
  403. UART_InitStruct->Databits = UART_DATABIT_8;
  404. UART_InitStruct->Parity = UART_PARITY_NONE;
  405. UART_InitStruct->Stopbits = UART_STOPBIT_1;
  406. }
  407. /* UART Send/Recieve functions -------------------------------------------------*/
  408. /*********************************************************************//**
  409. * @brief Transmit a single data through UART peripheral
  410. * @param[in] UARTx UART peripheral selected, should be:
  411. * - LPC_UART0: UART0 peripheral
  412. * - LPC_UART1: UART1 peripheral
  413. * - LPC_UART2: UART2 peripheral
  414. * - LPC_UART3: UART3 peripheral
  415. * @param[in] Data Data to transmit (must be 8-bit long)
  416. * @return None
  417. **********************************************************************/
  418. void UART_SendByte(LPC_UART_TypeDef* UARTx, uint8_t Data)
  419. {
  420. CHECK_PARAM(PARAM_UARTx(UARTx));
  421. if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  422. {
  423. ((LPC_UART1_TypeDef *)UARTx)->/*RBTHDLR.*/THR = Data & UART_THR_MASKBIT;
  424. }
  425. else
  426. {
  427. UARTx->/*RBTHDLR.*/THR = Data & UART_THR_MASKBIT;
  428. }
  429. }
  430. /*********************************************************************//**
  431. * @brief Receive a single data from UART peripheral
  432. * @param[in] UARTx UART peripheral selected, should be:
  433. * - LPC_UART0: UART0 peripheral
  434. * - LPC_UART1: UART1 peripheral
  435. * - LPC_UART2: UART2 peripheral
  436. * - LPC_UART3: UART3 peripheral
  437. * @return Data received
  438. **********************************************************************/
  439. uint8_t UART_ReceiveByte(LPC_UART_TypeDef* UARTx)
  440. {
  441. CHECK_PARAM(PARAM_UARTx(UARTx));
  442. if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  443. {
  444. return (((LPC_UART1_TypeDef *)UARTx)->/*RBTHDLR.*/RBR & UART_RBR_MASKBIT);
  445. }
  446. else
  447. {
  448. return (UARTx->/*RBTHDLR.*/RBR & UART_RBR_MASKBIT);
  449. }
  450. }
  451. /*********************************************************************//**
  452. * @brief Send a block of data via UART peripheral
  453. * @param[in] UARTx Selected UART peripheral used to send data, should be:
  454. * - LPC_UART0: UART0 peripheral
  455. * - LPC_UART1: UART1 peripheral
  456. * - LPC_UART2: UART2 peripheral
  457. * - LPC_UART3: UART3 peripheral
  458. * @param[in] txbuf Pointer to Transmit buffer
  459. * @param[in] buflen Length of Transmit buffer
  460. * @param[in] flag Flag used in UART transfer, should be
  461. * NONE_BLOCKING or BLOCKING
  462. * @return Number of bytes sent.
  463. *
  464. * Note: when using UART in BLOCKING mode, a time-out condition is used
  465. * via defined symbol UART_BLOCKING_TIMEOUT.
  466. **********************************************************************/
  467. uint32_t UART_Send(LPC_UART_TypeDef *UARTx, uint8_t *txbuf,
  468. uint32_t buflen, TRANSFER_BLOCK_Type flag)
  469. {
  470. uint32_t bToSend, bSent, timeOut, fifo_cnt;
  471. uint8_t *pChar = txbuf;
  472. bToSend = buflen;
  473. // blocking mode
  474. if (flag == BLOCKING) {
  475. bSent = 0;
  476. while (bToSend){
  477. timeOut = UART_BLOCKING_TIMEOUT;
  478. // Wait for THR empty with timeout
  479. while (!(UARTx->LSR & UART_LSR_THRE)) {
  480. if (timeOut == 0) break;
  481. timeOut--;
  482. }
  483. // Time out!
  484. if(timeOut == 0) break;
  485. fifo_cnt = UART_TX_FIFO_SIZE;
  486. while (fifo_cnt && bToSend){
  487. UART_SendByte(UARTx, (*pChar++));
  488. fifo_cnt--;
  489. bToSend--;
  490. bSent++;
  491. }
  492. }
  493. }
  494. // None blocking mode
  495. else {
  496. bSent = 0;
  497. while (bToSend) {
  498. if (!(UARTx->LSR & UART_LSR_THRE)){
  499. break;
  500. }
  501. fifo_cnt = UART_TX_FIFO_SIZE;
  502. while (fifo_cnt && bToSend) {
  503. UART_SendByte(UARTx, (*pChar++));
  504. bToSend--;
  505. fifo_cnt--;
  506. bSent++;
  507. }
  508. }
  509. }
  510. return bSent;
  511. }
  512. /*********************************************************************//**
  513. * @brief Receive a block of data via UART peripheral
  514. * @param[in] UARTx Selected UART peripheral used to send data,
  515. * should be:
  516. * - LPC_UART0: UART0 peripheral
  517. * - LPC_UART1: UART1 peripheral
  518. * - LPC_UART2: UART2 peripheral
  519. * - LPC_UART3: UART3 peripheral
  520. * @param[out] rxbuf Pointer to Received buffer
  521. * @param[in] buflen Length of Received buffer
  522. * @param[in] flag Flag mode, should be NONE_BLOCKING or BLOCKING
  523. * @return Number of bytes received
  524. *
  525. * Note: when using UART in BLOCKING mode, a time-out condition is used
  526. * via defined symbol UART_BLOCKING_TIMEOUT.
  527. **********************************************************************/
  528. uint32_t UART_Receive(LPC_UART_TypeDef *UARTx, uint8_t *rxbuf, \
  529. uint32_t buflen, TRANSFER_BLOCK_Type flag)
  530. {
  531. uint32_t bToRecv, bRecv, timeOut;
  532. uint8_t *pChar = rxbuf;
  533. bToRecv = buflen;
  534. // Blocking mode
  535. if (flag == BLOCKING) {
  536. bRecv = 0;
  537. while (bToRecv){
  538. timeOut = UART_BLOCKING_TIMEOUT;
  539. while (!(UARTx->LSR & UART_LSR_RDR)){
  540. if (timeOut == 0) break;
  541. timeOut--;
  542. }
  543. // Time out!
  544. if(timeOut == 0) break;
  545. // Get data from the buffer
  546. (*pChar++) = UART_ReceiveByte(UARTx);
  547. bToRecv--;
  548. bRecv++;
  549. }
  550. }
  551. // None blocking mode
  552. else {
  553. bRecv = 0;
  554. while (bToRecv) {
  555. if (!(UARTx->LSR & UART_LSR_RDR)) {
  556. break;
  557. } else {
  558. (*pChar++) = UART_ReceiveByte(UARTx);
  559. bRecv++;
  560. bToRecv--;
  561. }
  562. }
  563. }
  564. return bRecv;
  565. }
  566. /*********************************************************************//**
  567. * @brief Force BREAK character on UART line, output pin UARTx TXD is
  568. forced to logic 0.
  569. * @param[in] UARTx UART peripheral selected, should be:
  570. * - LPC_UART0: UART0 peripheral
  571. * - LPC_UART1: UART1 peripheral
  572. * - LPC_UART2: UART2 peripheral
  573. * - LPC_UART3: UART3 peripheral
  574. * @return None
  575. **********************************************************************/
  576. void UART_ForceBreak(LPC_UART_TypeDef* UARTx)
  577. {
  578. CHECK_PARAM(PARAM_UARTx(UARTx));
  579. if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  580. {
  581. ((LPC_UART1_TypeDef *)UARTx)->LCR |= UART_LCR_BREAK_EN;
  582. }
  583. else
  584. {
  585. UARTx->LCR |= UART_LCR_BREAK_EN;
  586. }
  587. }
  588. /********************************************************************//**
  589. * @brief Enable or disable specified UART interrupt.
  590. * @param[in] UARTx UART peripheral selected, should be
  591. * - LPC_UART0: UART0 peripheral
  592. * - LPC_UART1: UART1 peripheral
  593. * - LPC_UART2: UART2 peripheral
  594. * - LPC_UART3: UART3 peripheral
  595. * @param[in] UARTIntCfg Specifies the interrupt flag,
  596. * should be one of the following:
  597. - UART_INTCFG_RBR : RBR Interrupt enable
  598. - UART_INTCFG_THRE : THR Interrupt enable
  599. - UART_INTCFG_RLS : RX line status interrupt enable
  600. - UART1_INTCFG_MS : Modem status interrupt enable (UART1 only)
  601. - UART1_INTCFG_CTS : CTS1 signal transition interrupt enable (UART1 only)
  602. - UART_INTCFG_ABEO : Enables the end of auto-baud interrupt
  603. - UART_INTCFG_ABTO : Enables the auto-baud time-out interrupt
  604. * @param[in] NewState New state of specified UART interrupt type,
  605. * should be:
  606. * - ENALBE: Enable this UART interrupt type.
  607. * - DISALBE: Disable this UART interrupt type.
  608. * @return None
  609. *********************************************************************/
  610. void UART_IntConfig(LPC_UART_TypeDef *UARTx, UART_INT_Type UARTIntCfg, FunctionalState NewState)
  611. {
  612. uint32_t tmp;
  613. CHECK_PARAM(PARAM_UARTx(UARTx));
  614. CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
  615. switch(UARTIntCfg){
  616. case UART_INTCFG_RBR:
  617. tmp = UART_IER_RBRINT_EN;
  618. break;
  619. case UART_INTCFG_THRE:
  620. tmp = UART_IER_THREINT_EN;
  621. break;
  622. case UART_INTCFG_RLS:
  623. tmp = UART_IER_RLSINT_EN;
  624. break;
  625. case UART1_INTCFG_MS:
  626. tmp = UART1_IER_MSINT_EN;
  627. break;
  628. case UART1_INTCFG_CTS:
  629. tmp = UART1_IER_CTSINT_EN;
  630. break;
  631. case UART_INTCFG_ABEO:
  632. tmp = UART_IER_ABEOINT_EN;
  633. break;
  634. case UART_INTCFG_ABTO:
  635. tmp = UART_IER_ABTOINT_EN;
  636. break;
  637. }
  638. if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1)
  639. {
  640. CHECK_PARAM((PARAM_UART_INTCFG(UARTIntCfg)) || (PARAM_UART1_INTCFG(UARTIntCfg)));
  641. }
  642. else
  643. {
  644. CHECK_PARAM(PARAM_UART_INTCFG(UARTIntCfg));
  645. }
  646. if (NewState == ENABLE)
  647. {
  648. if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1)
  649. {
  650. ((LPC_UART1_TypeDef *)UARTx)->/*DLIER.*/IER |= tmp;
  651. }
  652. else
  653. {
  654. UARTx->/*DLIER.*/IER |= tmp;
  655. }
  656. }
  657. else
  658. {
  659. if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1)
  660. {
  661. ((LPC_UART1_TypeDef *)UARTx)->/*DLIER.*/IER &= (~tmp) & UART1_IER_BITMASK;
  662. }
  663. else
  664. {
  665. UARTx->/*DLIER.*/IER &= (~tmp) & UART_IER_BITMASK;
  666. }
  667. }
  668. }
  669. /********************************************************************//**
  670. * @brief Get current value of Line Status register in UART peripheral.
  671. * @param[in] UARTx UART peripheral selected, should be:
  672. * - LPC_UART0: UART0 peripheral
  673. * - LPC_UART1: UART1 peripheral
  674. * - LPC_UART2: UART2 peripheral
  675. * - LPC_UART3: UART3 peripheral
  676. * @return Current value of Line Status register in UART peripheral.
  677. * Note: The return value of this function must be ANDed with each member in
  678. * UART_LS_Type enumeration to determine current flag status
  679. * corresponding to each Line status type. Because some flags in
  680. * Line Status register will be cleared after reading, the next reading
  681. * Line Status register could not be correct. So this function used to
  682. * read Line status register in one time only, then the return value
  683. * used to check all flags.
  684. *********************************************************************/
  685. uint8_t UART_GetLineStatus(LPC_UART_TypeDef* UARTx)
  686. {
  687. CHECK_PARAM(PARAM_UARTx(UARTx));
  688. if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  689. {
  690. return ((((LPC_UART1_TypeDef *)LPC_UART1)->LSR) & UART_LSR_BITMASK);
  691. }
  692. else
  693. {
  694. return ((UARTx->LSR) & UART_LSR_BITMASK);
  695. }
  696. }
  697. /********************************************************************//**
  698. * @brief Get Interrupt Identification value
  699. * @param[in] UARTx UART peripheral selected, should be:
  700. * - LPC_UART0: UART0 peripheral
  701. * - LPC_UART1: UART1 peripheral
  702. * - LPC_UART2: UART2 peripheral
  703. * - LPC_UART3: UART3 peripheral
  704. * @return Current value of UART UIIR register in UART peripheral.
  705. *********************************************************************/
  706. uint32_t UART_GetIntId(LPC_UART_TypeDef* UARTx)
  707. {
  708. CHECK_PARAM(PARAM_UARTx(UARTx));
  709. return (UARTx->IIR & 0x03CF);
  710. }
  711. /*********************************************************************//**
  712. * @brief Check whether if UART is busy or not
  713. * @param[in] UARTx UART peripheral selected, should be:
  714. * - LPC_UART0: UART0 peripheral
  715. * - LPC_UART1: UART1 peripheral
  716. * - LPC_UART2: UART2 peripheral
  717. * - LPC_UART3: UART3 peripheral
  718. * @return RESET if UART is not busy, otherwise return SET.
  719. **********************************************************************/
  720. FlagStatus UART_CheckBusy(LPC_UART_TypeDef *UARTx)
  721. {
  722. if (UARTx->LSR & UART_LSR_TEMT){
  723. return RESET;
  724. } else {
  725. return SET;
  726. }
  727. }
  728. /*********************************************************************//**
  729. * @brief Configure FIFO function on selected UART peripheral
  730. * @param[in] UARTx UART peripheral selected, should be:
  731. * - LPC_UART0: UART0 peripheral
  732. * - LPC_UART1: UART1 peripheral
  733. * - LPC_UART2: UART2 peripheral
  734. * - LPC_UART3: UART3 peripheral
  735. * @param[in] FIFOCfg Pointer to a UART_FIFO_CFG_Type Structure that
  736. * contains specified information about FIFO configuration
  737. * @return none
  738. **********************************************************************/
  739. void UART_FIFOConfig(LPC_UART_TypeDef *UARTx, UART_FIFO_CFG_Type *FIFOCfg)
  740. {
  741. uint8_t tmp = 0;
  742. CHECK_PARAM(PARAM_UARTx(UARTx));
  743. CHECK_PARAM(PARAM_UART_FIFO_LEVEL(FIFOCfg->FIFO_Level));
  744. CHECK_PARAM(PARAM_FUNCTIONALSTATE(FIFOCfg->FIFO_DMAMode));
  745. CHECK_PARAM(PARAM_FUNCTIONALSTATE(FIFOCfg->FIFO_ResetRxBuf));
  746. CHECK_PARAM(PARAM_FUNCTIONALSTATE(FIFOCfg->FIFO_ResetTxBuf));
  747. tmp |= UART_FCR_FIFO_EN;
  748. switch (FIFOCfg->FIFO_Level){
  749. case UART_FIFO_TRGLEV0:
  750. tmp |= UART_FCR_TRG_LEV0;
  751. break;
  752. case UART_FIFO_TRGLEV1:
  753. tmp |= UART_FCR_TRG_LEV1;
  754. break;
  755. case UART_FIFO_TRGLEV2:
  756. tmp |= UART_FCR_TRG_LEV2;
  757. break;
  758. case UART_FIFO_TRGLEV3:
  759. default:
  760. tmp |= UART_FCR_TRG_LEV3;
  761. break;
  762. }
  763. if (FIFOCfg->FIFO_ResetTxBuf == ENABLE)
  764. {
  765. tmp |= UART_FCR_TX_RS;
  766. }
  767. if (FIFOCfg->FIFO_ResetRxBuf == ENABLE)
  768. {
  769. tmp |= UART_FCR_RX_RS;
  770. }
  771. if (FIFOCfg->FIFO_DMAMode == ENABLE)
  772. {
  773. tmp |= UART_FCR_DMAMODE_SEL;
  774. }
  775. //write to FIFO control register
  776. if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  777. {
  778. ((LPC_UART1_TypeDef *)UARTx)->/*IIFCR.*/FCR = tmp & UART_FCR_BITMASK;
  779. }
  780. else
  781. {
  782. UARTx->/*IIFCR.*/FCR = tmp & UART_FCR_BITMASK;
  783. }
  784. }
  785. /*****************************************************************************//**
  786. * @brief Fills each UART_FIFOInitStruct member with its default value:
  787. * - FIFO_DMAMode = DISABLE
  788. * - FIFO_Level = UART_FIFO_TRGLEV0
  789. * - FIFO_ResetRxBuf = ENABLE
  790. * - FIFO_ResetTxBuf = ENABLE
  791. * - FIFO_State = ENABLE
  792. * @param[in] UART_FIFOInitStruct Pointer to a UART_FIFO_CFG_Type structure
  793. * which will be initialized.
  794. * @return None
  795. *******************************************************************************/
  796. void UART_FIFOConfigStructInit(UART_FIFO_CFG_Type *UART_FIFOInitStruct)
  797. {
  798. UART_FIFOInitStruct->FIFO_DMAMode = DISABLE;
  799. UART_FIFOInitStruct->FIFO_Level = UART_FIFO_TRGLEV0;
  800. UART_FIFOInitStruct->FIFO_ResetRxBuf = ENABLE;
  801. UART_FIFOInitStruct->FIFO_ResetTxBuf = ENABLE;
  802. }
  803. /*********************************************************************//**
  804. * @brief Start/Stop Auto Baudrate activity
  805. * @param[in] UARTx UART peripheral selected, should be
  806. * - LPC_UART0: UART0 peripheral
  807. * - LPC_UART1: UART1 peripheral
  808. * - LPC_UART2: UART2 peripheral
  809. * - LPC_UART3: UART3 peripheral
  810. * @param[in] ABConfigStruct A pointer to UART_AB_CFG_Type structure that
  811. * contains specified information about UART
  812. * auto baudrate configuration
  813. * @param[in] NewState New State of Auto baudrate activity, should be:
  814. * - ENABLE: Start this activity
  815. * - DISABLE: Stop this activity
  816. * Note: Auto-baudrate mode enable bit will be cleared once this mode
  817. * completed.
  818. * @return none
  819. **********************************************************************/
  820. void UART_ABCmd(LPC_UART_TypeDef *UARTx, UART_AB_CFG_Type *ABConfigStruct, \
  821. FunctionalState NewState)
  822. {
  823. uint32_t tmp;
  824. CHECK_PARAM(PARAM_UARTx(UARTx));
  825. CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
  826. tmp = 0;
  827. if (NewState == ENABLE) {
  828. if (ABConfigStruct->ABMode == UART_AUTOBAUD_MODE1){
  829. tmp |= UART_ACR_MODE;
  830. }
  831. if (ABConfigStruct->AutoRestart == ENABLE){
  832. tmp |= UART_ACR_AUTO_RESTART;
  833. }
  834. }
  835. if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  836. {
  837. if (NewState == ENABLE)
  838. {
  839. // Clear DLL and DLM value
  840. ((LPC_UART1_TypeDef *)UARTx)->LCR |= UART_LCR_DLAB_EN;
  841. ((LPC_UART1_TypeDef *)UARTx)->DLL = 0;
  842. ((LPC_UART1_TypeDef *)UARTx)->DLM = 0;
  843. ((LPC_UART1_TypeDef *)UARTx)->LCR &= ~UART_LCR_DLAB_EN;
  844. // FDR value must be reset to default value
  845. ((LPC_UART1_TypeDef *)UARTx)->FDR = 0x10;
  846. ((LPC_UART1_TypeDef *)UARTx)->ACR = UART_ACR_START | tmp;
  847. }
  848. else
  849. {
  850. ((LPC_UART1_TypeDef *)UARTx)->ACR = 0;
  851. }
  852. }
  853. else
  854. {
  855. if (NewState == ENABLE)
  856. {
  857. // Clear DLL and DLM value
  858. UARTx->LCR |= UART_LCR_DLAB_EN;
  859. UARTx->DLL = 0;
  860. UARTx->DLM = 0;
  861. UARTx->LCR &= ~UART_LCR_DLAB_EN;
  862. // FDR value must be reset to default value
  863. UARTx->FDR = 0x10;
  864. UARTx->ACR = UART_ACR_START | tmp;
  865. }
  866. else
  867. {
  868. UARTx->ACR = 0;
  869. }
  870. }
  871. }
  872. /*********************************************************************//**
  873. * @brief Clear Autobaud Interrupt Pending
  874. * @param[in] UARTx UART peripheral selected, should be
  875. * - LPC_UART0: UART0 peripheral
  876. * - LPC_UART1: UART1 peripheral
  877. * - LPC_UART2: UART2 peripheral
  878. * - LPC_UART3: UART3 peripheral
  879. * @param[in] ABIntType type of auto-baud interrupt, should be:
  880. * - UART_AUTOBAUD_INTSTAT_ABEO: End of Auto-baud interrupt
  881. * - UART_AUTOBAUD_INTSTAT_ABTO: Auto-baud time out interrupt
  882. * @return none
  883. **********************************************************************/
  884. void UART_ABClearIntPending(LPC_UART_TypeDef *UARTx, UART_ABEO_Type ABIntType)
  885. {
  886. CHECK_PARAM(PARAM_UARTx(UARTx));
  887. if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  888. {
  889. UARTx->ACR |= ABIntType;
  890. }
  891. else
  892. UARTx->ACR |= ABIntType;
  893. }
  894. /*********************************************************************//**
  895. * @brief Enable/Disable transmission on UART TxD pin
  896. * @param[in] UARTx UART peripheral selected, should be:
  897. * - LPC_UART0: UART0 peripheral
  898. * - LPC_UART1: UART1 peripheral
  899. * - LPC_UART2: UART2 peripheral
  900. * - LPC_UART3: UART3 peripheral
  901. * @param[in] NewState New State of Tx transmission function, should be:
  902. * - ENABLE: Enable this function
  903. - DISABLE: Disable this function
  904. * @return none
  905. **********************************************************************/
  906. void UART_TxCmd(LPC_UART_TypeDef *UARTx, FunctionalState NewState)
  907. {
  908. CHECK_PARAM(PARAM_UARTx(UARTx));
  909. CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
  910. if (NewState == ENABLE)
  911. {
  912. if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  913. {
  914. ((LPC_UART1_TypeDef *)UARTx)->TER |= UART_TER_TXEN;
  915. }
  916. else
  917. {
  918. UARTx->TER |= UART_TER_TXEN;
  919. }
  920. }
  921. else
  922. {
  923. if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1)
  924. {
  925. ((LPC_UART1_TypeDef *)UARTx)->TER &= (~UART_TER_TXEN) & UART_TER_BITMASK;
  926. }
  927. else
  928. {
  929. UARTx->TER &= (~UART_TER_TXEN) & UART_TER_BITMASK;
  930. }
  931. }
  932. }
  933. /* UART IrDA functions ---------------------------------------------------*/
  934. #ifdef _UART3
  935. /*********************************************************************//**
  936. * @brief Enable or disable inverting serial input function of IrDA
  937. * on UART peripheral.
  938. * @param[in] UARTx UART peripheral selected, should be LPC_UART3 (only)
  939. * @param[in] NewState New state of inverting serial input, should be:
  940. * - ENABLE: Enable this function.
  941. * - DISABLE: Disable this function.
  942. * @return none
  943. **********************************************************************/
  944. void UART_IrDAInvtInputCmd(LPC_UART_TypeDef* UARTx, FunctionalState NewState)
  945. {
  946. CHECK_PARAM(PARAM_UART_IrDA(UARTx));
  947. CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
  948. if (NewState == ENABLE)
  949. {
  950. UARTx->ICR |= UART_ICR_IRDAINV;
  951. }
  952. else if (NewState == DISABLE)
  953. {
  954. UARTx->ICR &= (~UART_ICR_IRDAINV) & UART_ICR_BITMASK;
  955. }
  956. }
  957. /*********************************************************************//**
  958. * @brief Enable or disable IrDA function on UART peripheral.
  959. * @param[in] UARTx UART peripheral selected, should be LPC_UART3 (only)
  960. * @param[in] NewState New state of IrDA function, should be:
  961. * - ENABLE: Enable this function.
  962. * - DISABLE: Disable this function.
  963. * @return none
  964. **********************************************************************/
  965. void UART_IrDACmd(LPC_UART_TypeDef* UARTx, FunctionalState NewState)
  966. {
  967. CHECK_PARAM(PARAM_UART_IrDA(UARTx));
  968. CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
  969. if (NewState == ENABLE)
  970. {
  971. UARTx->ICR |= UART_ICR_IRDAEN;
  972. }
  973. else
  974. {
  975. UARTx->ICR &= (~UART_ICR_IRDAEN) & UART_ICR_BITMASK;
  976. }
  977. }
  978. /*********************************************************************//**
  979. * @brief Configure Pulse divider for IrDA function on UART peripheral.
  980. * @param[in] UARTx UART peripheral selected, should be LPC_UART3 (only)
  981. * @param[in] PulseDiv Pulse Divider value from Peripheral clock,
  982. * should be one of the following:
  983. - UART_IrDA_PULSEDIV2 : Pulse width = 2 * Tpclk
  984. - UART_IrDA_PULSEDIV4 : Pulse width = 4 * Tpclk
  985. - UART_IrDA_PULSEDIV8 : Pulse width = 8 * Tpclk
  986. - UART_IrDA_PULSEDIV16 : Pulse width = 16 * Tpclk
  987. - UART_IrDA_PULSEDIV32 : Pulse width = 32 * Tpclk
  988. - UART_IrDA_PULSEDIV64 : Pulse width = 64 * Tpclk
  989. - UART_IrDA_PULSEDIV128 : Pulse width = 128 * Tpclk
  990. - UART_IrDA_PULSEDIV256 : Pulse width = 256 * Tpclk
  991. * @return none
  992. **********************************************************************/
  993. void UART_IrDAPulseDivConfig(LPC_UART_TypeDef *UARTx, UART_IrDA_PULSE_Type PulseDiv)
  994. {
  995. uint32_t tmp, tmp1;
  996. CHECK_PARAM(PARAM_UART_IrDA(UARTx));
  997. CHECK_PARAM(PARAM_UART_IrDA_PULSEDIV(PulseDiv));
  998. tmp1 = UART_ICR_PULSEDIV(PulseDiv);
  999. tmp = UARTx->ICR & (~UART_ICR_PULSEDIV(7));
  1000. tmp |= tmp1 | UART_ICR_FIXPULSE_EN;
  1001. UARTx->ICR = tmp & UART_ICR_BITMASK;
  1002. }
  1003. #endif
  1004. /* UART1 FullModem function ---------------------------------------------*/
  1005. #ifdef _UART1
  1006. /*********************************************************************//**
  1007. * @brief Force pin DTR/RTS corresponding to given state (Full modem mode)
  1008. * @param[in] UARTx LPC_UART1 (only)
  1009. * @param[in] Pin Pin that NewState will be applied to, should be:
  1010. * - UART1_MODEM_PIN_DTR: DTR pin.
  1011. * - UART1_MODEM_PIN_RTS: RTS pin.
  1012. * @param[in] NewState New State of DTR/RTS pin, should be:
  1013. * - INACTIVE: Force the pin to inactive signal.
  1014. - ACTIVE: Force the pin to active signal.
  1015. * @return none
  1016. **********************************************************************/
  1017. void UART_FullModemForcePinState(LPC_UART1_TypeDef *UARTx, UART_MODEM_PIN_Type Pin, \
  1018. UART1_SignalState NewState)
  1019. {
  1020. uint8_t tmp = 0;
  1021. CHECK_PARAM(PARAM_UART1_MODEM(UARTx));
  1022. CHECK_PARAM(PARAM_UART1_MODEM_PIN(Pin));
  1023. CHECK_PARAM(PARAM_UART1_SIGNALSTATE(NewState));
  1024. switch (Pin){
  1025. case UART1_MODEM_PIN_DTR:
  1026. tmp = UART1_MCR_DTR_CTRL;
  1027. break;
  1028. case UART1_MODEM_PIN_RTS:
  1029. tmp = UART1_MCR_RTS_CTRL;
  1030. break;
  1031. default:
  1032. break;
  1033. }
  1034. if (NewState == ACTIVE){
  1035. UARTx->MCR |= tmp;
  1036. } else {
  1037. UARTx->MCR &= (~tmp) & UART1_MCR_BITMASK;
  1038. }
  1039. }
  1040. /*********************************************************************//**
  1041. * @brief Configure Full Modem mode for UART peripheral
  1042. * @param[in] UARTx LPC_UART1 (only)
  1043. * @param[in] Mode Full Modem mode, should be:
  1044. * - UART1_MODEM_MODE_LOOPBACK: Loop back mode.
  1045. * - UART1_MODEM_MODE_AUTO_RTS: Auto-RTS mode.
  1046. * - UART1_MODEM_MODE_AUTO_CTS: Auto-CTS mode.
  1047. * @param[in] NewState New State of this mode, should be:
  1048. * - ENABLE: Enable this mode.
  1049. - DISABLE: Disable this mode.
  1050. * @return none
  1051. **********************************************************************/
  1052. void UART_FullModemConfigMode(LPC_UART1_TypeDef *UARTx, UART_MODEM_MODE_Type Mode, \
  1053. FunctionalState NewState)
  1054. {
  1055. uint8_t tmp;
  1056. CHECK_PARAM(PARAM_UART1_MODEM(UARTx));
  1057. CHECK_PARAM(PARAM_UART1_MODEM_MODE(Mode));
  1058. CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
  1059. switch(Mode){
  1060. case UART1_MODEM_MODE_LOOPBACK:
  1061. tmp = UART1_MCR_LOOPB_EN;
  1062. break;
  1063. case UART1_MODEM_MODE_AUTO_RTS:
  1064. tmp = UART1_MCR_AUTO_RTS_EN;
  1065. break;
  1066. case UART1_MODEM_MODE_AUTO_CTS:
  1067. tmp = UART1_MCR_AUTO_CTS_EN;
  1068. break;
  1069. default:
  1070. break;
  1071. }
  1072. if (NewState == ENABLE)
  1073. {
  1074. UARTx->MCR |= tmp;
  1075. }
  1076. else
  1077. {
  1078. UARTx->MCR &= (~tmp) & UART1_MCR_BITMASK;
  1079. }
  1080. }
  1081. /*********************************************************************//**
  1082. * @brief Get current status of modem status register
  1083. * @param[in] UARTx LPC_UART1 (only)
  1084. * @return Current value of modem status register
  1085. * Note: The return value of this function must be ANDed with each member
  1086. * UART_MODEM_STAT_type enumeration to determine current flag status
  1087. * corresponding to each modem flag status. Because some flags in
  1088. * modem status register will be cleared after reading, the next reading
  1089. * modem register could not be correct. So this function used to
  1090. * read modem status register in one time only, then the return value
  1091. * used to check all flags.
  1092. **********************************************************************/
  1093. uint8_t UART_FullModemGetStatus(LPC_UART1_TypeDef *UARTx)
  1094. {
  1095. CHECK_PARAM(PARAM_UART1_MODEM(UARTx));
  1096. return ((UARTx->MSR) & UART1_MSR_BITMASK);
  1097. }
  1098. /* UART RS485 functions --------------------------------------------------------------*/
  1099. /*********************************************************************//**
  1100. * @brief Configure UART peripheral in RS485 mode according to the specified
  1101. * parameters in the RS485ConfigStruct.
  1102. * @param[in] UARTx LPC_UART1 (only)
  1103. * @param[in] RS485ConfigStruct Pointer to a UART1_RS485_CTRLCFG_Type structure
  1104. * that contains the configuration information for specified UART
  1105. * in RS485 mode.
  1106. * @return None
  1107. **********************************************************************/
  1108. void UART_RS485Config(LPC_UART1_TypeDef *UARTx, UART1_RS485_CTRLCFG_Type *RS485ConfigStruct)
  1109. {
  1110. uint32_t tmp;
  1111. CHECK_PARAM(PARAM_UART1_MODEM(UARTx));
  1112. CHECK_PARAM(PARAM_FUNCTIONALSTATE(RS485ConfigStruct->AutoAddrDetect_State));
  1113. CHECK_PARAM(PARAM_FUNCTIONALSTATE(RS485ConfigStruct->AutoDirCtrl_State));
  1114. CHECK_PARAM(PARAM_UART1_RS485_CFG_DELAYVALUE(RS485ConfigStruct->DelayValue));
  1115. CHECK_PARAM(PARAM_SETSTATE(RS485ConfigStruct->DirCtrlPol_Level));
  1116. CHECK_PARAM(PARAM_UART_RS485_DIRCTRL_PIN(RS485ConfigStruct->DirCtrlPin));
  1117. CHECK_PARAM(PARAM_UART1_RS485_CFG_MATCHADDRVALUE(RS485ConfigStruct->MatchAddrValue));
  1118. CHECK_PARAM(PARAM_FUNCTIONALSTATE(RS485ConfigStruct->NormalMultiDropMode_State));
  1119. CHECK_PARAM(PARAM_FUNCTIONALSTATE(RS485ConfigStruct->Rx_State));
  1120. tmp = 0;
  1121. // If Auto Direction Control is enabled - This function is used in Master mode
  1122. if (RS485ConfigStruct->AutoDirCtrl_State == ENABLE)
  1123. {
  1124. tmp |= UART1_RS485CTRL_DCTRL_EN;
  1125. // Set polar
  1126. if (RS485ConfigStruct->DirCtrlPol_Level == SET)
  1127. {
  1128. tmp |= UART1_RS485CTRL_OINV_1;
  1129. }
  1130. // Set pin according to
  1131. if (RS485ConfigStruct->DirCtrlPin == UART1_RS485_DIRCTRL_DTR)
  1132. {
  1133. tmp |= UART1_RS485CTRL_SEL_DTR;
  1134. }
  1135. // Fill delay time
  1136. UARTx->RS485DLY = RS485ConfigStruct->DelayValue & UART1_RS485DLY_BITMASK;
  1137. }
  1138. // MultiDrop mode is enable
  1139. if (RS485ConfigStruct->NormalMultiDropMode_State == ENABLE)
  1140. {
  1141. tmp |= UART1_RS485CTRL_NMM_EN;
  1142. }
  1143. // Auto Address Detect function
  1144. if (RS485ConfigStruct->AutoAddrDetect_State == ENABLE)
  1145. {
  1146. tmp |= UART1_RS485CTRL_AADEN;
  1147. // Fill Match Address
  1148. UARTx->ADRMATCH = RS485ConfigStruct->MatchAddrValue & UART1_RS485ADRMATCH_BITMASK;
  1149. }
  1150. // Receiver is disable
  1151. if (RS485ConfigStruct->Rx_State == DISABLE)
  1152. {
  1153. tmp |= UART1_RS485CTRL_RX_DIS;
  1154. }
  1155. // write back to RS485 control register
  1156. UARTx->RS485CTRL = tmp & UART1_RS485CTRL_BITMASK;
  1157. // Enable Parity function and leave parity in stick '0' parity as default
  1158. UARTx->LCR |= (UART_LCR_PARITY_F_0 | UART_LCR_PARITY_EN);
  1159. }
  1160. /*********************************************************************//**
  1161. * @brief Enable/Disable receiver in RS485 module in UART1
  1162. * @param[in] UARTx LPC_UART1 (only)
  1163. * @param[in] NewState New State of command, should be:
  1164. * - ENABLE: Enable this function.
  1165. * - DISABLE: Disable this function.
  1166. * @return None
  1167. **********************************************************************/
  1168. void UART_RS485ReceiverCmd(LPC_UART1_TypeDef *UARTx, FunctionalState NewState)
  1169. {
  1170. if (NewState == ENABLE){
  1171. UARTx->RS485CTRL &= ~UART1_RS485CTRL_RX_DIS;
  1172. } else {
  1173. UARTx->RS485CTRL |= UART1_RS485CTRL_RX_DIS;
  1174. }
  1175. }
  1176. /*********************************************************************//**
  1177. * @brief Send data on RS485 bus with specified parity stick value (9-bit mode).
  1178. * @param[in] UARTx LPC_UART1 (only)
  1179. * @param[in] pDatFrm Pointer to data frame.
  1180. * @param[in] size Size of data.
  1181. * @param[in] ParityStick Parity Stick value, should be 0 or 1.
  1182. * @return None
  1183. **********************************************************************/
  1184. uint32_t UART_RS485Send(LPC_UART1_TypeDef *UARTx, uint8_t *pDatFrm, \
  1185. uint32_t size, uint8_t ParityStick)
  1186. {
  1187. uint8_t tmp, save;
  1188. uint32_t cnt;
  1189. if (ParityStick){
  1190. save = tmp = UARTx->LCR & UART_LCR_BITMASK;
  1191. tmp &= ~(UART_LCR_PARITY_EVEN);
  1192. UARTx->LCR = tmp;
  1193. cnt = UART_Send((LPC_UART_TypeDef *)UARTx, pDatFrm, size, BLOCKING);
  1194. while (!(UARTx->LSR & UART_LSR_TEMT));
  1195. UARTx->LCR = save;
  1196. } else {
  1197. cnt = UART_Send((LPC_UART_TypeDef *)UARTx, pDatFrm, size, BLOCKING);
  1198. while (!(UARTx->LSR & UART_LSR_TEMT));
  1199. }
  1200. return cnt;
  1201. }
  1202. /*********************************************************************//**
  1203. * @brief Send Slave address frames on RS485 bus.
  1204. * @param[in] UARTx LPC_UART1 (only)
  1205. * @param[in] SlvAddr Slave Address.
  1206. * @return None
  1207. **********************************************************************/
  1208. void UART_RS485SendSlvAddr(LPC_UART1_TypeDef *UARTx, uint8_t SlvAddr)
  1209. {
  1210. UART_RS485Send(UARTx, &SlvAddr, 1, 1);
  1211. }
  1212. /*********************************************************************//**
  1213. * @brief Send Data frames on RS485 bus.
  1214. * @param[in] UARTx LPC_UART1 (only)
  1215. * @param[in] pData Pointer to data to be sent.
  1216. * @param[in] size Size of data frame to be sent.
  1217. * @return None
  1218. **********************************************************************/
  1219. uint32_t UART_RS485SendData(LPC_UART1_TypeDef *UARTx, uint8_t *pData, uint32_t size)
  1220. {
  1221. return (UART_RS485Send(UARTx, pData, size, 0));
  1222. }
  1223. #endif /* _UART1 */
  1224. #endif /* _UART */
  1225. /**
  1226. * @}
  1227. */
  1228. /**
  1229. * @}
  1230. */
  1231. /* --------------------------------- End Of File ------------------------------ */