fsl_lpuart.h 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. /*
  2. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2021 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_LPUART_H_
  9. #define _FSL_LPUART_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup lpuart_driver
  13. * @{
  14. */
  15. /*******************************************************************************
  16. * Definitions
  17. ******************************************************************************/
  18. /*! @name Driver version */
  19. /*@{*/
  20. /*! @brief LPUART driver version. */
  21. #define FSL_LPUART_DRIVER_VERSION (MAKE_VERSION(2, 5, 2))
  22. /*@}*/
  23. /*! @brief Retry times for waiting flag. */
  24. #ifndef UART_RETRY_TIMES
  25. #define UART_RETRY_TIMES 0U /* Defining to zero means to keep waiting for the flag until it is assert/deassert. */
  26. #endif
  27. /*! @brief Error codes for the LPUART driver. */
  28. enum
  29. {
  30. kStatus_LPUART_TxBusy = MAKE_STATUS(kStatusGroup_LPUART, 0), /*!< TX busy */
  31. kStatus_LPUART_RxBusy = MAKE_STATUS(kStatusGroup_LPUART, 1), /*!< RX busy */
  32. kStatus_LPUART_TxIdle = MAKE_STATUS(kStatusGroup_LPUART, 2), /*!< LPUART transmitter is idle. */
  33. kStatus_LPUART_RxIdle = MAKE_STATUS(kStatusGroup_LPUART, 3), /*!< LPUART receiver is idle. */
  34. kStatus_LPUART_TxWatermarkTooLarge = MAKE_STATUS(kStatusGroup_LPUART, 4), /*!< TX FIFO watermark too large */
  35. kStatus_LPUART_RxWatermarkTooLarge = MAKE_STATUS(kStatusGroup_LPUART, 5), /*!< RX FIFO watermark too large */
  36. kStatus_LPUART_FlagCannotClearManually = MAKE_STATUS(kStatusGroup_LPUART, 6), /*!< Some flag can't manually clear */
  37. kStatus_LPUART_Error = MAKE_STATUS(kStatusGroup_LPUART, 7), /*!< Error happens on LPUART. */
  38. kStatus_LPUART_RxRingBufferOverrun =
  39. MAKE_STATUS(kStatusGroup_LPUART, 8), /*!< LPUART RX software ring buffer overrun. */
  40. kStatus_LPUART_RxHardwareOverrun = MAKE_STATUS(kStatusGroup_LPUART, 9), /*!< LPUART RX receiver overrun. */
  41. kStatus_LPUART_NoiseError = MAKE_STATUS(kStatusGroup_LPUART, 10), /*!< LPUART noise error. */
  42. kStatus_LPUART_FramingError = MAKE_STATUS(kStatusGroup_LPUART, 11), /*!< LPUART framing error. */
  43. kStatus_LPUART_ParityError = MAKE_STATUS(kStatusGroup_LPUART, 12), /*!< LPUART parity error. */
  44. kStatus_LPUART_BaudrateNotSupport =
  45. MAKE_STATUS(kStatusGroup_LPUART, 13), /*!< Baudrate is not support in current clock source */
  46. kStatus_LPUART_IdleLineDetected = MAKE_STATUS(kStatusGroup_LPUART, 14), /*!< IDLE flag. */
  47. kStatus_LPUART_Timeout = MAKE_STATUS(kStatusGroup_LPUART, 15), /*!< LPUART times out. */
  48. };
  49. /*! @brief LPUART parity mode. */
  50. typedef enum _lpuart_parity_mode
  51. {
  52. kLPUART_ParityDisabled = 0x0U, /*!< Parity disabled */
  53. kLPUART_ParityEven = 0x2U, /*!< Parity enabled, type even, bit setting: PE|PT = 10 */
  54. kLPUART_ParityOdd = 0x3U, /*!< Parity enabled, type odd, bit setting: PE|PT = 11 */
  55. } lpuart_parity_mode_t;
  56. /*! @brief LPUART data bits count. */
  57. typedef enum _lpuart_data_bits
  58. {
  59. kLPUART_EightDataBits = 0x0U, /*!< Eight data bit */
  60. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  61. kLPUART_SevenDataBits = 0x1U, /*!< Seven data bit */
  62. #endif
  63. } lpuart_data_bits_t;
  64. /*! @brief LPUART stop bit count. */
  65. typedef enum _lpuart_stop_bit_count
  66. {
  67. kLPUART_OneStopBit = 0U, /*!< One stop bit */
  68. kLPUART_TwoStopBit = 1U, /*!< Two stop bits */
  69. } lpuart_stop_bit_count_t;
  70. #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT) && FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
  71. /*! @brief LPUART transmit CTS source. */
  72. typedef enum _lpuart_transmit_cts_source
  73. {
  74. kLPUART_CtsSourcePin = 0U, /*!< CTS resource is the LPUART_CTS pin. */
  75. kLPUART_CtsSourceMatchResult = 1U, /*!< CTS resource is the match result. */
  76. } lpuart_transmit_cts_source_t;
  77. /*! @brief LPUART transmit CTS configure. */
  78. typedef enum _lpuart_transmit_cts_config
  79. {
  80. kLPUART_CtsSampleAtStart = 0U, /*!< CTS input is sampled at the start of each character. */
  81. kLPUART_CtsSampleAtIdle = 1U, /*!< CTS input is sampled when the transmitter is idle */
  82. } lpuart_transmit_cts_config_t;
  83. #endif
  84. /*! @brief LPUART idle flag type defines when the receiver starts counting. */
  85. typedef enum _lpuart_idle_type_select
  86. {
  87. kLPUART_IdleTypeStartBit = 0U, /*!< Start counting after a valid start bit. */
  88. kLPUART_IdleTypeStopBit = 1U, /*!< Start counting after a stop bit. */
  89. } lpuart_idle_type_select_t;
  90. /*! @brief LPUART idle detected configuration.
  91. * This structure defines the number of idle characters that must be received before
  92. * the IDLE flag is set.
  93. */
  94. typedef enum _lpuart_idle_config
  95. {
  96. kLPUART_IdleCharacter1 = 0U, /*!< the number of idle characters. */
  97. kLPUART_IdleCharacter2 = 1U, /*!< the number of idle characters. */
  98. kLPUART_IdleCharacter4 = 2U, /*!< the number of idle characters. */
  99. kLPUART_IdleCharacter8 = 3U, /*!< the number of idle characters. */
  100. kLPUART_IdleCharacter16 = 4U, /*!< the number of idle characters. */
  101. kLPUART_IdleCharacter32 = 5U, /*!< the number of idle characters. */
  102. kLPUART_IdleCharacter64 = 6U, /*!< the number of idle characters. */
  103. kLPUART_IdleCharacter128 = 7U, /*!< the number of idle characters. */
  104. } lpuart_idle_config_t;
  105. /*!
  106. * @brief LPUART interrupt configuration structure, default settings all disabled.
  107. *
  108. * This structure contains the settings for all LPUART interrupt configurations.
  109. */
  110. enum _lpuart_interrupt_enable
  111. {
  112. #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
  113. kLPUART_LinBreakInterruptEnable = (LPUART_BAUD_LBKDIE_MASK >> 8U), /*!< LIN break detect. bit 7 */
  114. #endif
  115. kLPUART_RxActiveEdgeInterruptEnable = (LPUART_BAUD_RXEDGIE_MASK >> 8U), /*!< Receive Active Edge. bit 6 */
  116. kLPUART_TxDataRegEmptyInterruptEnable = (LPUART_CTRL_TIE_MASK), /*!< Transmit data register empty. bit 23 */
  117. kLPUART_TransmissionCompleteInterruptEnable = (LPUART_CTRL_TCIE_MASK), /*!< Transmission complete. bit 22 */
  118. kLPUART_RxDataRegFullInterruptEnable = (LPUART_CTRL_RIE_MASK), /*!< Receiver data register full. bit 21 */
  119. kLPUART_IdleLineInterruptEnable = (LPUART_CTRL_ILIE_MASK), /*!< Idle line. bit 20 */
  120. kLPUART_RxOverrunInterruptEnable = (LPUART_CTRL_ORIE_MASK), /*!< Receiver Overrun. bit 27 */
  121. kLPUART_NoiseErrorInterruptEnable = (LPUART_CTRL_NEIE_MASK), /*!< Noise error flag. bit 26 */
  122. kLPUART_FramingErrorInterruptEnable = (LPUART_CTRL_FEIE_MASK), /*!< Framing error flag. bit 25 */
  123. kLPUART_ParityErrorInterruptEnable = (LPUART_CTRL_PEIE_MASK), /*!< Parity error flag. bit 24 */
  124. #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
  125. kLPUART_Match1InterruptEnable = (LPUART_CTRL_MA1IE_MASK), /*!< Parity error flag. bit 15 */
  126. kLPUART_Match2InterruptEnable = (LPUART_CTRL_MA2IE_MASK), /*!< Parity error flag. bit 14 */
  127. #endif
  128. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  129. kLPUART_TxFifoOverflowInterruptEnable = (LPUART_FIFO_TXOFE_MASK), /*!< Transmit FIFO Overflow. bit 9 */
  130. kLPUART_RxFifoUnderflowInterruptEnable = (LPUART_FIFO_RXUFE_MASK), /*!< Receive FIFO Underflow. bit 8 */
  131. #endif
  132. kLPUART_AllInterruptEnable = kLPUART_RxActiveEdgeInterruptEnable | kLPUART_TxDataRegEmptyInterruptEnable |
  133. kLPUART_TransmissionCompleteInterruptEnable | kLPUART_RxDataRegFullInterruptEnable |
  134. kLPUART_IdleLineInterruptEnable | kLPUART_RxOverrunInterruptEnable |
  135. kLPUART_NoiseErrorInterruptEnable | kLPUART_FramingErrorInterruptEnable |
  136. kLPUART_ParityErrorInterruptEnable
  137. #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
  138. | kLPUART_LinBreakInterruptEnable
  139. #endif
  140. #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
  141. | kLPUART_Match1InterruptEnable | kLPUART_Match2InterruptEnable
  142. #endif
  143. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  144. | kLPUART_TxFifoOverflowInterruptEnable | kLPUART_RxFifoUnderflowInterruptEnable
  145. #endif
  146. ,
  147. };
  148. /*!
  149. * @brief LPUART status flags.
  150. *
  151. * This provides constants for the LPUART status flags for use in the LPUART functions.
  152. */
  153. enum _lpuart_flags
  154. {
  155. kLPUART_TxDataRegEmptyFlag =
  156. (LPUART_STAT_TDRE_MASK), /*!< Transmit data register empty flag, sets when transmit buffer is empty. bit 23 */
  157. kLPUART_TransmissionCompleteFlag =
  158. (LPUART_STAT_TC_MASK), /*!< Transmission complete flag, sets when transmission activity complete. bit 22 */
  159. kLPUART_RxDataRegFullFlag = (LPUART_STAT_RDRF_MASK), /*!< Receive data register full flag, sets when the receive
  160. data buffer is full. bit 21 */
  161. kLPUART_IdleLineFlag = (LPUART_STAT_IDLE_MASK), /*!< Idle line detect flag, sets when idle line detected. bit 20 */
  162. kLPUART_RxOverrunFlag = (LPUART_STAT_OR_MASK), /*!< Receive Overrun, sets when new data is received before data is
  163. read from receive register. bit 19 */
  164. kLPUART_NoiseErrorFlag = (LPUART_STAT_NF_MASK), /*!< Receive takes 3 samples of each received bit. If any of these
  165. samples differ, noise flag sets. bit 18 */
  166. kLPUART_FramingErrorFlag =
  167. (LPUART_STAT_FE_MASK), /*!< Frame error flag, sets if logic 0 was detected where stop bit expected. bit 17 */
  168. kLPUART_ParityErrorFlag = (LPUART_STAT_PF_MASK), /*!< If parity enabled, sets upon parity error detection. bit 16 */
  169. #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
  170. kLPUART_LinBreakFlag = (LPUART_STAT_LBKDIF_MASK), /*!< LIN break detect interrupt flag, sets when LIN break
  171. char detected and LIN circuit enabled. bit 31 */
  172. #endif
  173. kLPUART_RxActiveEdgeFlag = (LPUART_STAT_RXEDGIF_MASK), /*!< Receive pin active edge interrupt flag, sets when active
  174. edge detected. bit 30 */
  175. kLPUART_RxActiveFlag =
  176. (LPUART_STAT_RAF_MASK), /*!< Receiver Active Flag (RAF), sets at beginning of valid start. bit 24 */
  177. #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
  178. kLPUART_DataMatch1Flag =
  179. LPUART_STAT_MA1F_MASK, /*!< The next character to be read from LPUART_DATA matches MA1. bit 15 */
  180. kLPUART_DataMatch2Flag =
  181. LPUART_STAT_MA2F_MASK, /*!< The next character to be read from LPUART_DATA matches MA2. bit 14 */
  182. #endif
  183. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  184. kLPUART_TxFifoEmptyFlag =
  185. (LPUART_FIFO_TXEMPT_MASK >> 16), /*!< TXEMPT bit, sets if transmit buffer is empty. bit 7 */
  186. kLPUART_RxFifoEmptyFlag =
  187. (LPUART_FIFO_RXEMPT_MASK >> 16), /*!< RXEMPT bit, sets if receive buffer is empty. bit 6 */
  188. kLPUART_TxFifoOverflowFlag =
  189. (LPUART_FIFO_TXOF_MASK >> 16), /*!< TXOF bit, sets if transmit buffer overflow occurred. bit 1 */
  190. kLPUART_RxFifoUnderflowFlag =
  191. (LPUART_FIFO_RXUF_MASK >> 16), /*!< RXUF bit, sets if receive buffer underflow occurred. bit 0 */
  192. #endif
  193. kLPUART_AllClearFlags = kLPUART_RxActiveEdgeFlag | kLPUART_IdleLineFlag | kLPUART_RxOverrunFlag |
  194. kLPUART_NoiseErrorFlag | kLPUART_FramingErrorFlag | kLPUART_ParityErrorFlag
  195. #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
  196. | kLPUART_DataMatch1Flag | kLPUART_DataMatch2Flag
  197. #endif
  198. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  199. | kLPUART_TxFifoOverflowFlag | kLPUART_RxFifoUnderflowFlag
  200. #endif
  201. #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
  202. | kLPUART_LinBreakFlag
  203. #endif
  204. ,
  205. kLPUART_AllFlags =
  206. kLPUART_RxActiveEdgeFlag | kLPUART_IdleLineFlag | kLPUART_RxOverrunFlag | kLPUART_TxDataRegEmptyFlag |
  207. kLPUART_TransmissionCompleteFlag | kLPUART_RxDataRegFullFlag | kLPUART_RxActiveFlag | kLPUART_NoiseErrorFlag |
  208. kLPUART_FramingErrorFlag | kLPUART_ParityErrorFlag
  209. #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
  210. | kLPUART_DataMatch1Flag | kLPUART_DataMatch2Flag
  211. #endif
  212. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  213. | kLPUART_TxFifoOverflowFlag | kLPUART_RxFifoUnderflowFlag | kLPUART_TxFifoEmptyFlag | kLPUART_RxFifoEmptyFlag
  214. #endif
  215. #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
  216. | kLPUART_LinBreakFlag
  217. #endif
  218. ,
  219. };
  220. /*! @brief LPUART configuration structure. */
  221. typedef struct _lpuart_config
  222. {
  223. uint32_t baudRate_Bps; /*!< LPUART baud rate */
  224. lpuart_parity_mode_t parityMode; /*!< Parity mode, disabled (default), even, odd */
  225. lpuart_data_bits_t dataBitsCount; /*!< Data bits count, eight (default), seven */
  226. bool isMsb; /*!< Data bits order, LSB (default), MSB */
  227. #if defined(FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT
  228. lpuart_stop_bit_count_t stopBitCount; /*!< Number of stop bits, 1 stop bit (default) or 2 stop bits */
  229. #endif
  230. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  231. uint8_t txFifoWatermark; /*!< TX FIFO watermark */
  232. uint8_t rxFifoWatermark; /*!< RX FIFO watermark */
  233. #endif
  234. #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT) && FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
  235. bool enableRxRTS; /*!< RX RTS enable */
  236. bool enableTxCTS; /*!< TX CTS enable */
  237. lpuart_transmit_cts_source_t txCtsSource; /*!< TX CTS source */
  238. lpuart_transmit_cts_config_t txCtsConfig; /*!< TX CTS configure */
  239. #endif
  240. lpuart_idle_type_select_t rxIdleType; /*!< RX IDLE type. */
  241. lpuart_idle_config_t rxIdleConfig; /*!< RX IDLE configuration. */
  242. bool enableTx; /*!< Enable TX */
  243. bool enableRx; /*!< Enable RX */
  244. } lpuart_config_t;
  245. /*! @brief LPUART transfer structure. */
  246. typedef struct _lpuart_transfer
  247. {
  248. /*
  249. * Use separate TX and RX data pointer, because TX data is const data.
  250. * The member data is kept for backward compatibility.
  251. */
  252. union
  253. {
  254. uint8_t *data; /*!< The buffer of data to be transfer.*/
  255. uint8_t *rxData; /*!< The buffer to receive data. */
  256. const uint8_t *txData; /*!< The buffer of data to be sent. */
  257. };
  258. size_t dataSize; /*!< The byte count to be transfer. */
  259. } lpuart_transfer_t;
  260. /* Forward declaration of the handle typedef. */
  261. typedef struct _lpuart_handle lpuart_handle_t;
  262. /*! @brief LPUART transfer callback function. */
  263. typedef void (*lpuart_transfer_callback_t)(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData);
  264. /*! @brief LPUART handle structure. */
  265. struct _lpuart_handle
  266. {
  267. const uint8_t *volatile txData; /*!< Address of remaining data to send. */
  268. volatile size_t txDataSize; /*!< Size of the remaining data to send. */
  269. size_t txDataSizeAll; /*!< Size of the data to send out. */
  270. uint8_t *volatile rxData; /*!< Address of remaining data to receive. */
  271. volatile size_t rxDataSize; /*!< Size of the remaining data to receive. */
  272. size_t rxDataSizeAll; /*!< Size of the data to receive. */
  273. uint8_t *rxRingBuffer; /*!< Start address of the receiver ring buffer. */
  274. size_t rxRingBufferSize; /*!< Size of the ring buffer. */
  275. volatile uint16_t rxRingBufferHead; /*!< Index for the driver to store received data into ring buffer. */
  276. volatile uint16_t rxRingBufferTail; /*!< Index for the user to get data from the ring buffer. */
  277. lpuart_transfer_callback_t callback; /*!< Callback function. */
  278. void *userData; /*!< LPUART callback function parameter.*/
  279. volatile uint8_t txState; /*!< TX transfer state. */
  280. volatile uint8_t rxState; /*!< RX transfer state. */
  281. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  282. bool isSevenDataBits; /*!< Seven data bits flag. */
  283. #endif
  284. };
  285. /* Typedef for interrupt handler. */
  286. typedef void (*lpuart_isr_t)(LPUART_Type *base, void *handle);
  287. /*******************************************************************************
  288. * Variables
  289. ******************************************************************************/
  290. /* Array of LPUART handle. */
  291. extern void *s_lpuartHandle[];
  292. /* Array of LPUART IRQ number. */
  293. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  294. extern const IRQn_Type s_lpuartTxIRQ[];
  295. #else
  296. extern const IRQn_Type s_lpuartIRQ[];
  297. #endif
  298. /* LPUART ISR for transactional APIs. */
  299. extern lpuart_isr_t s_lpuartIsr;
  300. /*******************************************************************************
  301. * API
  302. ******************************************************************************/
  303. #if defined(__cplusplus)
  304. extern "C" {
  305. #endif /* _cplusplus */
  306. #if defined(FSL_FEATURE_LPUART_HAS_GLOBAL) && FSL_FEATURE_LPUART_HAS_GLOBAL
  307. /*!
  308. * @name Software Reset
  309. * @{
  310. */
  311. /*!
  312. * @brief Resets the LPUART using software.
  313. *
  314. * This function resets all internal logic and registers except the Global Register.
  315. * Remains set until cleared by software.
  316. *
  317. * @param base LPUART peripheral base address.
  318. */
  319. static inline void LPUART_SoftwareReset(LPUART_Type *base)
  320. {
  321. base->GLOBAL |= LPUART_GLOBAL_RST_MASK;
  322. base->GLOBAL &= ~LPUART_GLOBAL_RST_MASK;
  323. }
  324. /* @} */
  325. #endif /*FSL_FEATURE_LPUART_HAS_GLOBAL*/
  326. /*!
  327. * @name Initialization and deinitialization
  328. * @{
  329. */
  330. /*!
  331. * @brief Initializes an LPUART instance with the user configuration structure and the peripheral clock.
  332. *
  333. * This function configures the LPUART module with user-defined settings. Call the LPUART_GetDefaultConfig() function
  334. * to configure the configuration structure and get the default configuration.
  335. * The example below shows how to use this API to configure the LPUART.
  336. * @code
  337. * lpuart_config_t lpuartConfig;
  338. * lpuartConfig.baudRate_Bps = 115200U;
  339. * lpuartConfig.parityMode = kLPUART_ParityDisabled;
  340. * lpuartConfig.dataBitsCount = kLPUART_EightDataBits;
  341. * lpuartConfig.isMsb = false;
  342. * lpuartConfig.stopBitCount = kLPUART_OneStopBit;
  343. * lpuartConfig.txFifoWatermark = 0;
  344. * lpuartConfig.rxFifoWatermark = 1;
  345. * LPUART_Init(LPUART1, &lpuartConfig, 20000000U);
  346. * @endcode
  347. *
  348. * @param base LPUART peripheral base address.
  349. * @param config Pointer to a user-defined configuration structure.
  350. * @param srcClock_Hz LPUART clock source frequency in HZ.
  351. * @retval kStatus_LPUART_BaudrateNotSupport Baudrate is not support in current clock source.
  352. * @retval kStatus_Success LPUART initialize succeed
  353. */
  354. status_t LPUART_Init(LPUART_Type *base, const lpuart_config_t *config, uint32_t srcClock_Hz);
  355. /*!
  356. * @brief Deinitializes a LPUART instance.
  357. *
  358. * This function waits for transmit to complete, disables TX and RX, and disables the LPUART clock.
  359. *
  360. * @param base LPUART peripheral base address.
  361. */
  362. void LPUART_Deinit(LPUART_Type *base);
  363. /*!
  364. * @brief Gets the default configuration structure.
  365. *
  366. * This function initializes the LPUART configuration structure to a default value. The default
  367. * values are:
  368. * lpuartConfig->baudRate_Bps = 115200U;
  369. * lpuartConfig->parityMode = kLPUART_ParityDisabled;
  370. * lpuartConfig->dataBitsCount = kLPUART_EightDataBits;
  371. * lpuartConfig->isMsb = false;
  372. * lpuartConfig->stopBitCount = kLPUART_OneStopBit;
  373. * lpuartConfig->txFifoWatermark = 0;
  374. * lpuartConfig->rxFifoWatermark = 1;
  375. * lpuartConfig->rxIdleType = kLPUART_IdleTypeStartBit;
  376. * lpuartConfig->rxIdleConfig = kLPUART_IdleCharacter1;
  377. * lpuartConfig->enableTx = false;
  378. * lpuartConfig->enableRx = false;
  379. *
  380. * @param config Pointer to a configuration structure.
  381. */
  382. void LPUART_GetDefaultConfig(lpuart_config_t *config);
  383. /* @} */
  384. /*!
  385. * @name Module configuration
  386. * @{
  387. */
  388. /*!
  389. * @brief Sets the LPUART instance baudrate.
  390. *
  391. * This function configures the LPUART module baudrate. This function is used to update
  392. * the LPUART module baudrate after the LPUART module is initialized by the LPUART_Init.
  393. * @code
  394. * LPUART_SetBaudRate(LPUART1, 115200U, 20000000U);
  395. * @endcode
  396. *
  397. * @param base LPUART peripheral base address.
  398. * @param baudRate_Bps LPUART baudrate to be set.
  399. * @param srcClock_Hz LPUART clock source frequency in HZ.
  400. * @retval kStatus_LPUART_BaudrateNotSupport Baudrate is not supported in the current clock source.
  401. * @retval kStatus_Success Set baudrate succeeded.
  402. */
  403. status_t LPUART_SetBaudRate(LPUART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
  404. /*!
  405. * @brief Enable 9-bit data mode for LPUART.
  406. *
  407. * This function set the 9-bit mode for LPUART module. The 9th bit is not used for parity thus can be modified by user.
  408. *
  409. * @param base LPUART peripheral base address.
  410. * @param enable true to enable, flase to disable.
  411. */
  412. void LPUART_Enable9bitMode(LPUART_Type *base, bool enable);
  413. /*!
  414. * @brief Set the LPUART address.
  415. *
  416. * This function configures the address for LPUART module that works as slave in 9-bit data mode. One or two address
  417. * fields can be configured. When the address field's match enable bit is set, the frame it receices with MSB being
  418. * 1 is considered as an address frame, otherwise it is considered as data frame. Once the address frame matches one
  419. * of slave's own addresses, this slave is addressed. This address frame and its following data frames are stored in
  420. * the receive buffer, otherwise the frames will be discarded. To un-address a slave, just send an address frame with
  421. * unmatched address.
  422. *
  423. * @note Any LPUART instance joined in the multi-slave system can work as slave. The position of the address mark is the
  424. * same as the parity bit when parity is enabled for 8 bit and 9 bit data formats.
  425. *
  426. * @param base LPUART peripheral base address.
  427. * @param address1 LPUART slave address1.
  428. * @param address2 LPUART slave address2.
  429. */
  430. static inline void LPUART_SetMatchAddress(LPUART_Type *base, uint16_t address1, uint16_t address2)
  431. {
  432. /* Configure match address. */
  433. uint32_t address = ((uint32_t)address2 << 16U) | (uint32_t)address1 | 0x1000100UL;
  434. base->MATCH = address;
  435. }
  436. /*!
  437. * @brief Enable the LPUART match address feature.
  438. *
  439. * @param base LPUART peripheral base address.
  440. * @param match1 true to enable match address1, false to disable.
  441. * @param match2 true to enable match address2, false to disable.
  442. */
  443. static inline void LPUART_EnableMatchAddress(LPUART_Type *base, bool match1, bool match2)
  444. {
  445. /* Configure match address1 enable bit. */
  446. if (match1)
  447. {
  448. base->BAUD |= (uint32_t)LPUART_BAUD_MAEN1_MASK;
  449. }
  450. else
  451. {
  452. base->BAUD &= ~(uint32_t)LPUART_BAUD_MAEN1_MASK;
  453. }
  454. /* Configure match address2 enable bit. */
  455. if (match2)
  456. {
  457. base->BAUD |= (uint32_t)LPUART_BAUD_MAEN2_MASK;
  458. }
  459. else
  460. {
  461. base->BAUD &= ~(uint32_t)LPUART_BAUD_MAEN2_MASK;
  462. }
  463. }
  464. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  465. /*!
  466. * @brief Sets the rx FIFO watermark.
  467. *
  468. * @param base LPUART peripheral base address.
  469. * @param water Rx FIFO watermark.
  470. */
  471. static inline void LPUART_SetRxFifoWatermark(LPUART_Type *base, uint8_t water)
  472. {
  473. assert((uint8_t)FSL_FEATURE_LPUART_FIFO_SIZEn(base) > water);
  474. base->WATER = (base->WATER & ~LPUART_WATER_RXWATER_MASK) | LPUART_WATER_RXWATER(water);
  475. }
  476. /*!
  477. * @brief Sets the tx FIFO watermark.
  478. *
  479. * @param base LPUART peripheral base address.
  480. * @param water Tx FIFO watermark.
  481. */
  482. static inline void LPUART_SetTxFifoWatermark(LPUART_Type *base, uint8_t water)
  483. {
  484. assert((uint8_t)FSL_FEATURE_LPUART_FIFO_SIZEn(base) > water);
  485. base->WATER = (base->WATER & ~LPUART_WATER_TXWATER_MASK) | LPUART_WATER_TXWATER(water);
  486. }
  487. #endif
  488. /* @} */
  489. /*!
  490. * @name Status
  491. * @{
  492. */
  493. /*!
  494. * @brief Gets LPUART status flags.
  495. *
  496. * This function gets all LPUART status flags. The flags are returned as the logical
  497. * OR value of the enumerators @ref _lpuart_flags. To check for a specific status,
  498. * compare the return value with enumerators in the @ref _lpuart_flags.
  499. * For example, to check whether the TX is empty:
  500. * @code
  501. * if (kLPUART_TxDataRegEmptyFlag & LPUART_GetStatusFlags(LPUART1))
  502. * {
  503. * ...
  504. * }
  505. * @endcode
  506. *
  507. * @param base LPUART peripheral base address.
  508. * @return LPUART status flags which are ORed by the enumerators in the _lpuart_flags.
  509. */
  510. uint32_t LPUART_GetStatusFlags(LPUART_Type *base);
  511. /*!
  512. * @brief Clears status flags with a provided mask.
  513. *
  514. * This function clears LPUART status flags with a provided mask. Automatically cleared flags
  515. * can't be cleared by this function.
  516. * Flags that can only cleared or set by hardware are:
  517. * kLPUART_TxDataRegEmptyFlag, kLPUART_TransmissionCompleteFlag, kLPUART_RxDataRegFullFlag,
  518. * kLPUART_RxActiveFlag, kLPUART_NoiseErrorInRxDataRegFlag, kLPUART_ParityErrorInRxDataRegFlag,
  519. * kLPUART_TxFifoEmptyFlag,kLPUART_RxFifoEmptyFlag
  520. * Note: This API should be called when the Tx/Rx is idle, otherwise it takes no effects.
  521. *
  522. * @param base LPUART peripheral base address.
  523. * @param mask the status flags to be cleared. The user can use the enumerators in the
  524. * _lpuart_status_flag_t to do the OR operation and get the mask.
  525. * @return 0 succeed, others failed.
  526. * @retval kStatus_LPUART_FlagCannotClearManually The flag can't be cleared by this function but
  527. * it is cleared automatically by hardware.
  528. * @retval kStatus_Success Status in the mask are cleared.
  529. */
  530. status_t LPUART_ClearStatusFlags(LPUART_Type *base, uint32_t mask);
  531. /* @} */
  532. /*!
  533. * @name Interrupts
  534. * @{
  535. */
  536. /*!
  537. * @brief Enables LPUART interrupts according to a provided mask.
  538. *
  539. * This function enables the LPUART interrupts according to a provided mask. The mask
  540. * is a logical OR of enumeration members. See the @ref _lpuart_interrupt_enable.
  541. * This examples shows how to enable TX empty interrupt and RX full interrupt:
  542. * @code
  543. * LPUART_EnableInterrupts(LPUART1,kLPUART_TxDataRegEmptyInterruptEnable | kLPUART_RxDataRegFullInterruptEnable);
  544. * @endcode
  545. *
  546. * @param base LPUART peripheral base address.
  547. * @param mask The interrupts to enable. Logical OR of the enumeration _uart_interrupt_enable.
  548. */
  549. void LPUART_EnableInterrupts(LPUART_Type *base, uint32_t mask);
  550. /*!
  551. * @brief Disables LPUART interrupts according to a provided mask.
  552. *
  553. * This function disables the LPUART interrupts according to a provided mask. The mask
  554. * is a logical OR of enumeration members. See @ref _lpuart_interrupt_enable.
  555. * This example shows how to disable the TX empty interrupt and RX full interrupt:
  556. * @code
  557. * LPUART_DisableInterrupts(LPUART1,kLPUART_TxDataRegEmptyInterruptEnable | kLPUART_RxDataRegFullInterruptEnable);
  558. * @endcode
  559. *
  560. * @param base LPUART peripheral base address.
  561. * @param mask The interrupts to disable. Logical OR of @ref _lpuart_interrupt_enable.
  562. */
  563. void LPUART_DisableInterrupts(LPUART_Type *base, uint32_t mask);
  564. /*!
  565. * @brief Gets enabled LPUART interrupts.
  566. *
  567. * This function gets the enabled LPUART interrupts. The enabled interrupts are returned
  568. * as the logical OR value of the enumerators @ref _lpuart_interrupt_enable. To check
  569. * a specific interrupt enable status, compare the return value with enumerators
  570. * in @ref _lpuart_interrupt_enable.
  571. * For example, to check whether the TX empty interrupt is enabled:
  572. * @code
  573. * uint32_t enabledInterrupts = LPUART_GetEnabledInterrupts(LPUART1);
  574. *
  575. * if (kLPUART_TxDataRegEmptyInterruptEnable & enabledInterrupts)
  576. * {
  577. * ...
  578. * }
  579. * @endcode
  580. *
  581. * @param base LPUART peripheral base address.
  582. * @return LPUART interrupt flags which are logical OR of the enumerators in @ref _lpuart_interrupt_enable.
  583. */
  584. uint32_t LPUART_GetEnabledInterrupts(LPUART_Type *base);
  585. /* @} */
  586. #if defined(FSL_FEATURE_LPUART_HAS_DMA_ENABLE) && FSL_FEATURE_LPUART_HAS_DMA_ENABLE
  587. /*!
  588. * @name DMA Configuration
  589. * @{
  590. */
  591. /*!
  592. * @brief Gets the LPUART data register address.
  593. *
  594. * This function returns the LPUART data register address, which is mainly used by the DMA/eDMA.
  595. *
  596. * @param base LPUART peripheral base address.
  597. * @return LPUART data register addresses which are used both by the transmitter and receiver.
  598. */
  599. static inline uint32_t LPUART_GetDataRegisterAddress(LPUART_Type *base)
  600. {
  601. return (uint32_t) & (base->DATA);
  602. }
  603. /*!
  604. * @brief Enables or disables the LPUART transmitter DMA request.
  605. *
  606. * This function enables or disables the transmit data register empty flag, STAT[TDRE], to generate DMA requests.
  607. *
  608. * @param base LPUART peripheral base address.
  609. * @param enable True to enable, false to disable.
  610. */
  611. static inline void LPUART_EnableTxDMA(LPUART_Type *base, bool enable)
  612. {
  613. if (enable)
  614. {
  615. base->BAUD |= LPUART_BAUD_TDMAE_MASK;
  616. }
  617. else
  618. {
  619. base->BAUD &= ~LPUART_BAUD_TDMAE_MASK;
  620. }
  621. }
  622. /*!
  623. * @brief Enables or disables the LPUART receiver DMA.
  624. *
  625. * This function enables or disables the receiver data register full flag, STAT[RDRF], to generate DMA requests.
  626. *
  627. * @param base LPUART peripheral base address.
  628. * @param enable True to enable, false to disable.
  629. */
  630. static inline void LPUART_EnableRxDMA(LPUART_Type *base, bool enable)
  631. {
  632. if (enable)
  633. {
  634. base->BAUD |= LPUART_BAUD_RDMAE_MASK;
  635. }
  636. else
  637. {
  638. base->BAUD &= ~LPUART_BAUD_RDMAE_MASK;
  639. }
  640. }
  641. /* @} */
  642. #endif /* FSL_FEATURE_LPUART_HAS_DMA_ENABLE */
  643. /*!
  644. * @name Bus Operations
  645. * @{
  646. */
  647. /*!
  648. * @brief Get the LPUART instance from peripheral base address.
  649. *
  650. * @param base LPUART peripheral base address.
  651. * @return LPUART instance.
  652. */
  653. uint32_t LPUART_GetInstance(LPUART_Type *base);
  654. /*!
  655. * @brief Enables or disables the LPUART transmitter.
  656. *
  657. * This function enables or disables the LPUART transmitter.
  658. *
  659. * @param base LPUART peripheral base address.
  660. * @param enable True to enable, false to disable.
  661. */
  662. static inline void LPUART_EnableTx(LPUART_Type *base, bool enable)
  663. {
  664. if (enable)
  665. {
  666. base->CTRL |= LPUART_CTRL_TE_MASK;
  667. }
  668. else
  669. {
  670. base->CTRL &= ~LPUART_CTRL_TE_MASK;
  671. }
  672. }
  673. /*!
  674. * @brief Enables or disables the LPUART receiver.
  675. *
  676. * This function enables or disables the LPUART receiver.
  677. *
  678. * @param base LPUART peripheral base address.
  679. * @param enable True to enable, false to disable.
  680. */
  681. static inline void LPUART_EnableRx(LPUART_Type *base, bool enable)
  682. {
  683. if (enable)
  684. {
  685. base->CTRL |= LPUART_CTRL_RE_MASK;
  686. }
  687. else
  688. {
  689. base->CTRL &= ~LPUART_CTRL_RE_MASK;
  690. }
  691. }
  692. /*!
  693. * @brief Writes to the transmitter register.
  694. *
  695. * This function writes data to the transmitter register directly. The upper layer must
  696. * ensure that the TX register is empty or that the TX FIFO has room before calling this function.
  697. *
  698. * @param base LPUART peripheral base address.
  699. * @param data Data write to the TX register.
  700. */
  701. static inline void LPUART_WriteByte(LPUART_Type *base, uint8_t data)
  702. {
  703. base->DATA = data;
  704. }
  705. /*!
  706. * @brief Reads the receiver register.
  707. *
  708. * This function reads data from the receiver register directly. The upper layer must
  709. * ensure that the receiver register is full or that the RX FIFO has data before calling this function.
  710. *
  711. * @param base LPUART peripheral base address.
  712. * @return Data read from data register.
  713. */
  714. static inline uint8_t LPUART_ReadByte(LPUART_Type *base)
  715. {
  716. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  717. uint32_t ctrl = base->CTRL;
  718. uint8_t result;
  719. bool isSevenDataBits = (((ctrl & LPUART_CTRL_M7_MASK) != 0U) ||
  720. (((ctrl & LPUART_CTRL_M7_MASK) == 0U) && ((ctrl & LPUART_CTRL_M_MASK) == 0U) &&
  721. ((ctrl & LPUART_CTRL_PE_MASK) != 0U)));
  722. if (isSevenDataBits)
  723. {
  724. result = (uint8_t)(base->DATA & 0x7FU);
  725. }
  726. else
  727. {
  728. result = (uint8_t)base->DATA;
  729. }
  730. return result;
  731. #else
  732. return (uint8_t)(base->DATA);
  733. #endif
  734. }
  735. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  736. /*!
  737. * @brief Gets the rx FIFO data count.
  738. *
  739. * @param base LPUART peripheral base address.
  740. * @return rx FIFO data count.
  741. */
  742. static inline uint8_t LPUART_GetRxFifoCount(LPUART_Type *base)
  743. {
  744. return (uint8_t)((base->WATER & LPUART_WATER_RXCOUNT_MASK) >> LPUART_WATER_RXCOUNT_SHIFT);
  745. }
  746. /*!
  747. * @brief Gets the tx FIFO data count.
  748. *
  749. * @param base LPUART peripheral base address.
  750. * @return tx FIFO data count.
  751. */
  752. static inline uint8_t LPUART_GetTxFifoCount(LPUART_Type *base)
  753. {
  754. return (uint8_t)((base->WATER & LPUART_WATER_TXCOUNT_MASK) >> LPUART_WATER_TXCOUNT_SHIFT);
  755. }
  756. #endif
  757. /*!
  758. * @brief Transmit an address frame in 9-bit data mode.
  759. *
  760. * @param base LPUART peripheral base address.
  761. * @param address LPUART slave address.
  762. */
  763. void LPUART_SendAddress(LPUART_Type *base, uint8_t address);
  764. /*!
  765. * @brief Writes to the transmitter register using a blocking method.
  766. *
  767. * This function polls the transmitter register, first waits for the register to be empty or TX FIFO to have room,
  768. * and writes data to the transmitter buffer, then waits for the dat to be sent out to the bus.
  769. *
  770. * @param base LPUART peripheral base address.
  771. * @param data Start address of the data to write.
  772. * @param length Size of the data to write.
  773. * @retval kStatus_LPUART_Timeout Transmission timed out and was aborted.
  774. * @retval kStatus_Success Successfully wrote all data.
  775. */
  776. status_t LPUART_WriteBlocking(LPUART_Type *base, const uint8_t *data, size_t length);
  777. /*!
  778. * @brief Reads the receiver data register using a blocking method.
  779. *
  780. * This function polls the receiver register, waits for the receiver register full or receiver FIFO
  781. * has data, and reads data from the TX register.
  782. *
  783. * @param base LPUART peripheral base address.
  784. * @param data Start address of the buffer to store the received data.
  785. * @param length Size of the buffer.
  786. * @retval kStatus_LPUART_RxHardwareOverrun Receiver overrun happened while receiving data.
  787. * @retval kStatus_LPUART_NoiseError Noise error happened while receiving data.
  788. * @retval kStatus_LPUART_FramingError Framing error happened while receiving data.
  789. * @retval kStatus_LPUART_ParityError Parity error happened while receiving data.
  790. * @retval kStatus_LPUART_Timeout Transmission timed out and was aborted.
  791. * @retval kStatus_Success Successfully received all data.
  792. */
  793. status_t LPUART_ReadBlocking(LPUART_Type *base, uint8_t *data, size_t length);
  794. /* @} */
  795. /*!
  796. * @name Transactional
  797. * @{
  798. */
  799. /*!
  800. * @brief Initializes the LPUART handle.
  801. *
  802. * This function initializes the LPUART handle, which can be used for other LPUART
  803. * transactional APIs. Usually, for a specified LPUART instance,
  804. * call this API once to get the initialized handle.
  805. *
  806. * The LPUART driver supports the "background" receiving, which means that user can set up
  807. * an RX ring buffer optionally. Data received is stored into the ring buffer even when the
  808. * user doesn't call the LPUART_TransferReceiveNonBlocking() API. If there is already data received
  809. * in the ring buffer, the user can get the received data from the ring buffer directly.
  810. * The ring buffer is disabled if passing NULL as @p ringBuffer.
  811. *
  812. * @param base LPUART peripheral base address.
  813. * @param handle LPUART handle pointer.
  814. * @param callback Callback function.
  815. * @param userData User data.
  816. */
  817. void LPUART_TransferCreateHandle(LPUART_Type *base,
  818. lpuart_handle_t *handle,
  819. lpuart_transfer_callback_t callback,
  820. void *userData);
  821. /*!
  822. * @brief Transmits a buffer of data using the interrupt method.
  823. *
  824. * This function send data using an interrupt method. This is a non-blocking function, which
  825. * returns directly without waiting for all data written to the transmitter register. When
  826. * all data is written to the TX register in the ISR, the LPUART driver calls the callback
  827. * function and passes the @ref kStatus_LPUART_TxIdle as status parameter.
  828. *
  829. * @note The kStatus_LPUART_TxIdle is passed to the upper layer when all data are written
  830. * to the TX register. However, there is no check to ensure that all the data sent out. Before disabling the TX,
  831. * check the kLPUART_TransmissionCompleteFlag to ensure that the transmit is finished.
  832. *
  833. * @param base LPUART peripheral base address.
  834. * @param handle LPUART handle pointer.
  835. * @param xfer LPUART transfer structure, see #lpuart_transfer_t.
  836. * @retval kStatus_Success Successfully start the data transmission.
  837. * @retval kStatus_LPUART_TxBusy Previous transmission still not finished, data not all written to the TX register.
  838. * @retval kStatus_InvalidArgument Invalid argument.
  839. */
  840. status_t LPUART_TransferSendNonBlocking(LPUART_Type *base, lpuart_handle_t *handle, lpuart_transfer_t *xfer);
  841. /*!
  842. * @brief Sets up the RX ring buffer.
  843. *
  844. * This function sets up the RX ring buffer to a specific UART handle.
  845. *
  846. * When the RX ring buffer is used, data received is stored into the ring buffer even when
  847. * the user doesn't call the UART_TransferReceiveNonBlocking() API. If there is already data received
  848. * in the ring buffer, the user can get the received data from the ring buffer directly.
  849. *
  850. * @note When using RX ring buffer, one byte is reserved for internal use. In other
  851. * words, if @p ringBufferSize is 32, then only 31 bytes are used for saving data.
  852. *
  853. * @param base LPUART peripheral base address.
  854. * @param handle LPUART handle pointer.
  855. * @param ringBuffer Start address of ring buffer for background receiving. Pass NULL to disable the ring buffer.
  856. * @param ringBufferSize size of the ring buffer.
  857. */
  858. void LPUART_TransferStartRingBuffer(LPUART_Type *base,
  859. lpuart_handle_t *handle,
  860. uint8_t *ringBuffer,
  861. size_t ringBufferSize);
  862. /*!
  863. * @brief Aborts the background transfer and uninstalls the ring buffer.
  864. *
  865. * This function aborts the background transfer and uninstalls the ring buffer.
  866. *
  867. * @param base LPUART peripheral base address.
  868. * @param handle LPUART handle pointer.
  869. */
  870. void LPUART_TransferStopRingBuffer(LPUART_Type *base, lpuart_handle_t *handle);
  871. /*!
  872. * @brief Get the length of received data in RX ring buffer.
  873. *
  874. * @param base LPUART peripheral base address.
  875. * @param handle LPUART handle pointer.
  876. * @return Length of received data in RX ring buffer.
  877. */
  878. size_t LPUART_TransferGetRxRingBufferLength(LPUART_Type *base, lpuart_handle_t *handle);
  879. /*!
  880. * @brief Aborts the interrupt-driven data transmit.
  881. *
  882. * This function aborts the interrupt driven data sending. The user can get the remainBtyes to find out
  883. * how many bytes are not sent out.
  884. *
  885. * @param base LPUART peripheral base address.
  886. * @param handle LPUART handle pointer.
  887. */
  888. void LPUART_TransferAbortSend(LPUART_Type *base, lpuart_handle_t *handle);
  889. /*!
  890. * @brief Gets the number of bytes that have been sent out to bus.
  891. *
  892. * This function gets the number of bytes that have been sent out to bus by an interrupt method.
  893. *
  894. * @param base LPUART peripheral base address.
  895. * @param handle LPUART handle pointer.
  896. * @param count Send bytes count.
  897. * @retval kStatus_NoTransferInProgress No send in progress.
  898. * @retval kStatus_InvalidArgument Parameter is invalid.
  899. * @retval kStatus_Success Get successfully through the parameter \p count;
  900. */
  901. status_t LPUART_TransferGetSendCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count);
  902. /*!
  903. * @brief Receives a buffer of data using the interrupt method.
  904. *
  905. * This function receives data using an interrupt method. This is a non-blocking function
  906. * which returns without waiting to ensure that all data are received.
  907. * If the RX ring buffer is used and not empty, the data in the ring buffer is copied and
  908. * the parameter @p receivedBytes shows how many bytes are copied from the ring buffer.
  909. * After copying, if the data in the ring buffer is not enough for read, the receive
  910. * request is saved by the LPUART driver. When the new data arrives, the receive request
  911. * is serviced first. When all data is received, the LPUART driver notifies the upper layer
  912. * through a callback function and passes a status parameter kStatus_UART_RxIdle.
  913. * For example, the upper layer needs 10 bytes but there are only 5 bytes in ring buffer.
  914. * The 5 bytes are copied to xfer->data, which returns with the
  915. * parameter @p receivedBytes set to 5. For the remaining 5 bytes, the newly arrived data is
  916. * saved from xfer->data[5]. When 5 bytes are received, the LPUART driver notifies the upper layer.
  917. * If the RX ring buffer is not enabled, this function enables the RX and RX interrupt
  918. * to receive data to xfer->data. When all data is received, the upper layer is notified.
  919. *
  920. * @param base LPUART peripheral base address.
  921. * @param handle LPUART handle pointer.
  922. * @param xfer LPUART transfer structure, see uart_transfer_t.
  923. * @param receivedBytes Bytes received from the ring buffer directly.
  924. * @retval kStatus_Success Successfully queue the transfer into the transmit queue.
  925. * @retval kStatus_LPUART_RxBusy Previous receive request is not finished.
  926. * @retval kStatus_InvalidArgument Invalid argument.
  927. */
  928. status_t LPUART_TransferReceiveNonBlocking(LPUART_Type *base,
  929. lpuart_handle_t *handle,
  930. lpuart_transfer_t *xfer,
  931. size_t *receivedBytes);
  932. /*!
  933. * @brief Aborts the interrupt-driven data receiving.
  934. *
  935. * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to find out
  936. * how many bytes not received yet.
  937. *
  938. * @param base LPUART peripheral base address.
  939. * @param handle LPUART handle pointer.
  940. */
  941. void LPUART_TransferAbortReceive(LPUART_Type *base, lpuart_handle_t *handle);
  942. /*!
  943. * @brief Gets the number of bytes that have been received.
  944. *
  945. * This function gets the number of bytes that have been received.
  946. *
  947. * @param base LPUART peripheral base address.
  948. * @param handle LPUART handle pointer.
  949. * @param count Receive bytes count.
  950. * @retval kStatus_NoTransferInProgress No receive in progress.
  951. * @retval kStatus_InvalidArgument Parameter is invalid.
  952. * @retval kStatus_Success Get successfully through the parameter \p count;
  953. */
  954. status_t LPUART_TransferGetReceiveCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count);
  955. /*!
  956. * @brief LPUART IRQ handle function.
  957. *
  958. * This function handles the LPUART transmit and receive IRQ request.
  959. *
  960. * @param base LPUART peripheral base address.
  961. * @param irqHandle LPUART handle pointer.
  962. */
  963. void LPUART_TransferHandleIRQ(LPUART_Type *base, void *irqHandle);
  964. /*!
  965. * @brief LPUART Error IRQ handle function.
  966. *
  967. * This function handles the LPUART error IRQ request.
  968. *
  969. * @param base LPUART peripheral base address.
  970. * @param irqHandle LPUART handle pointer.
  971. */
  972. void LPUART_TransferHandleErrorIRQ(LPUART_Type *base, void *irqHandle);
  973. /* @} */
  974. #if defined(__cplusplus)
  975. }
  976. #endif
  977. /*! @}*/
  978. #endif /* _FSL_LPUART_H_ */