uart_8xx.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * @brief LPC8xx UART driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2012
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licenser disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #include "chip.h"
  32. #include "uart_8xx.h"
  33. /*****************************************************************************
  34. * Private types/enumerations/variables
  35. ****************************************************************************/
  36. /*****************************************************************************
  37. * Public types/enumerations/variables
  38. ****************************************************************************/
  39. /*****************************************************************************
  40. * Private functions
  41. ****************************************************************************/
  42. /* Return UART clock ID from the UART register address */
  43. static CHIP_SYSCTL_CLOCK_T getUARTClockID(LPC_USART_T *pUART)
  44. {
  45. if (pUART == LPC_USART0) {
  46. return SYSCTL_CLOCK_UART0;
  47. }
  48. else if (pUART == LPC_USART1) {
  49. return SYSCTL_CLOCK_UART1;
  50. }
  51. return SYSCTL_CLOCK_UART2;
  52. }
  53. /*****************************************************************************
  54. * Public functions
  55. ****************************************************************************/
  56. /* Initialize the UART peripheral */
  57. void Chip_UART_Init(LPC_USART_T *pUART)
  58. {
  59. /* Enable USART clock */
  60. Chip_Clock_EnablePeriphClock(getUARTClockID(pUART));
  61. /* UART reset */
  62. if (pUART == LPC_USART0) {
  63. /* Peripheral reset control to USART0 */
  64. Chip_SYSCTL_PeriphReset(RESET_USART0);
  65. }
  66. else if (pUART == LPC_USART1) {
  67. /* Peripheral reset control to USART1 */
  68. Chip_SYSCTL_PeriphReset(RESET_USART1);
  69. }
  70. else {
  71. /* Peripheral reset control to USART2 */
  72. Chip_SYSCTL_PeriphReset(RESET_USART2);
  73. }
  74. }
  75. /* Initialize the UART peripheral */
  76. void Chip_UART_DeInit(LPC_USART_T *pUART)
  77. {
  78. /* Disable USART clock */
  79. Chip_Clock_DisablePeriphClock(getUARTClockID(pUART));
  80. }
  81. /* Transmit a byte array through the UART peripheral (non-blocking) */
  82. int Chip_UART_Send(LPC_USART_T *pUART, const void *data, int numBytes)
  83. {
  84. int sent = 0;
  85. uint8_t *p8 = (uint8_t *) data;
  86. /* Send until the transmit FIFO is full or out of bytes */
  87. while ((sent < numBytes) &&
  88. ((Chip_UART_GetStatus(pUART) & UART_STAT_TXRDY) != 0)) {
  89. Chip_UART_SendByte(pUART, *p8);
  90. p8++;
  91. sent++;
  92. }
  93. return sent;
  94. }
  95. /* Transmit a byte array through the UART peripheral (blocking) */
  96. int Chip_UART_SendBlocking(LPC_USART_T *pUART, const void *data, int numBytes)
  97. {
  98. int pass, sent = 0;
  99. uint8_t *p8 = (uint8_t *) data;
  100. while (numBytes > 0) {
  101. pass = Chip_UART_Send(pUART, p8, numBytes);
  102. numBytes -= pass;
  103. sent += pass;
  104. p8 += pass;
  105. }
  106. return sent;
  107. }
  108. /* Read data through the UART peripheral (non-blocking) */
  109. int Chip_UART_Read(LPC_USART_T *pUART, void *data, int numBytes)
  110. {
  111. int readBytes = 0;
  112. uint8_t *p8 = (uint8_t *) data;
  113. /* Send until the transmit FIFO is full or out of bytes */
  114. while ((readBytes < numBytes) &&
  115. ((Chip_UART_GetStatus(pUART) & UART_STAT_RXRDY) != 0)) {
  116. *p8 = Chip_UART_ReadByte(pUART);
  117. p8++;
  118. readBytes++;
  119. }
  120. return readBytes;
  121. }
  122. /* Read data through the UART peripheral (blocking) */
  123. int Chip_UART_ReadBlocking(LPC_USART_T *pUART, void *data, int numBytes)
  124. {
  125. int pass, readBytes = 0;
  126. uint8_t *p8 = (uint8_t *) data;
  127. while (numBytes > 0) {
  128. pass = Chip_UART_Read(pUART, p8, numBytes);
  129. numBytes -= pass;
  130. readBytes += pass;
  131. p8 += pass;
  132. }
  133. return readBytes;
  134. }
  135. /* Set baud rate for UART */
  136. void Chip_UART_SetBaud(LPC_USART_T *pUART, uint32_t baudrate)
  137. {
  138. uint32_t baudRateGenerator;
  139. baudRateGenerator = Chip_Clock_GetUSARTNBaseClockRate() / (16 * baudrate);
  140. pUART->BRG = baudRateGenerator - 1; /* baud rate */
  141. }
  142. /* UART receive-only interrupt handler for ring buffers */
  143. void Chip_UART_RXIntHandlerRB(LPC_USART_T *pUART, RINGBUFF_T *pRB)
  144. {
  145. /* New data will be ignored if data not popped in time */
  146. while ((Chip_UART_GetStatus(pUART) & UART_STAT_RXRDY) != 0) {
  147. uint8_t ch = Chip_UART_ReadByte(pUART);
  148. RingBuf_Write(pRB, &ch, 1);
  149. }
  150. }
  151. /* UART transmit-only interrupt handler for ring buffers */
  152. void Chip_UART_TXIntHandlerRB(LPC_USART_T *pUART, RINGBUFF_T *pRB)
  153. {
  154. uint8_t ch;
  155. /* Fill FIFO until full or until TX ring buffer is empty */
  156. while (((Chip_UART_GetStatus(pUART) & UART_STAT_TXRDY) != 0) &&
  157. RingBuf_Read(pRB, &ch, 1)) {
  158. Chip_UART_SendByte(pUART, ch);
  159. }
  160. }
  161. /* Populate a transmit ring buffer and start UART transmit */
  162. uint32_t Chip_UART_SendRB(LPC_USART_T *pUART, RINGBUFF_T *pRB, const void *data, int count)
  163. {
  164. uint32_t ret;
  165. uint8_t *p8 = (uint8_t *) data;
  166. /* Don't let UART transmit ring buffer change in the UART IRQ handler */
  167. Chip_UART_IntDisable(pUART, UART_INTEN_TXRDY);
  168. /* Move as much data as possible into transmit ring buffer */
  169. ret = RingBuf_Write(pRB, p8, count);
  170. Chip_UART_TXIntHandlerRB(pUART, pRB);
  171. /* Add additional data to transmit ring buffer if possible */
  172. ret += RingBuf_Write(pRB, (p8 + ret), (count - ret));
  173. /* Enable UART transmit interrupt */
  174. Chip_UART_IntEnable(pUART, UART_INTEN_TXRDY);
  175. return ret;
  176. }
  177. /* Copy data from a receive ring buffer */
  178. int Chip_UART_ReadRB(LPC_USART_T *pUART, RINGBUFF_T *pRB, void *data, int bytes)
  179. {
  180. (void) pUART;
  181. return RingBuf_Read(pRB, (uint8_t *) data, bytes);
  182. }
  183. /* UART receive/transmit interrupt handler for ring buffers */
  184. void Chip_UART_IRQRBHandler(LPC_USART_T *pUART, RINGBUFF_T *pRXRB, RINGBUFF_T *pTXRB)
  185. {
  186. /* Handle transmit interrupt if enabled */
  187. if ((Chip_UART_GetStatus(pUART) & UART_STAT_TXRDY) != 0) {
  188. Chip_UART_TXIntHandlerRB(pUART, pTXRB);
  189. /* Disable transmit interrupt if the ring buffer is empty */
  190. if (RingBuf_GetFreeBytes(pTXRB) == 0) {
  191. Chip_UART_IntDisable(pUART, UART_INTEN_TXRDY);
  192. }
  193. }
  194. /* Handle receive interrupt */
  195. Chip_UART_RXIntHandlerRB(pUART, pRXRB);
  196. }