fsl_lpuart.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_LPUART_H_
  31. #define _FSL_LPUART_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup lpuart_driver
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief LPUART driver version 2.2.5. */
  43. #define FSL_LPUART_DRIVER_VERSION (MAKE_VERSION(2, 2, 5))
  44. /*@}*/
  45. /*! @brief Error codes for the LPUART driver. */
  46. enum _lpuart_status
  47. {
  48. kStatus_LPUART_TxBusy = MAKE_STATUS(kStatusGroup_LPUART, 0), /*!< TX busy */
  49. kStatus_LPUART_RxBusy = MAKE_STATUS(kStatusGroup_LPUART, 1), /*!< RX busy */
  50. kStatus_LPUART_TxIdle = MAKE_STATUS(kStatusGroup_LPUART, 2), /*!< LPUART transmitter is idle. */
  51. kStatus_LPUART_RxIdle = MAKE_STATUS(kStatusGroup_LPUART, 3), /*!< LPUART receiver is idle. */
  52. kStatus_LPUART_TxWatermarkTooLarge = MAKE_STATUS(kStatusGroup_LPUART, 4), /*!< TX FIFO watermark too large */
  53. kStatus_LPUART_RxWatermarkTooLarge = MAKE_STATUS(kStatusGroup_LPUART, 5), /*!< RX FIFO watermark too large */
  54. kStatus_LPUART_FlagCannotClearManually = MAKE_STATUS(kStatusGroup_LPUART, 6), /*!< Some flag can't manually clear */
  55. kStatus_LPUART_Error = MAKE_STATUS(kStatusGroup_LPUART, 7), /*!< Error happens on LPUART. */
  56. kStatus_LPUART_RxRingBufferOverrun =
  57. MAKE_STATUS(kStatusGroup_LPUART, 8), /*!< LPUART RX software ring buffer overrun. */
  58. kStatus_LPUART_RxHardwareOverrun = MAKE_STATUS(kStatusGroup_LPUART, 9), /*!< LPUART RX receiver overrun. */
  59. kStatus_LPUART_NoiseError = MAKE_STATUS(kStatusGroup_LPUART, 10), /*!< LPUART noise error. */
  60. kStatus_LPUART_FramingError = MAKE_STATUS(kStatusGroup_LPUART, 11), /*!< LPUART framing error. */
  61. kStatus_LPUART_ParityError = MAKE_STATUS(kStatusGroup_LPUART, 12), /*!< LPUART parity error. */
  62. kStatus_LPUART_BaudrateNotSupport =
  63. MAKE_STATUS(kStatusGroup_LPUART, 13), /*!< Baudrate is not support in current clock source */
  64. kStatus_LPUART_IdleLineDetected = MAKE_STATUS(kStatusGroup_LPUART, 14), /*!< IDLE flag. */
  65. };
  66. /*! @brief LPUART parity mode. */
  67. typedef enum _lpuart_parity_mode
  68. {
  69. kLPUART_ParityDisabled = 0x0U, /*!< Parity disabled */
  70. kLPUART_ParityEven = 0x2U, /*!< Parity enabled, type even, bit setting: PE|PT = 10 */
  71. kLPUART_ParityOdd = 0x3U, /*!< Parity enabled, type odd, bit setting: PE|PT = 11 */
  72. } lpuart_parity_mode_t;
  73. /*! @brief LPUART data bits count. */
  74. typedef enum _lpuart_data_bits
  75. {
  76. kLPUART_EightDataBits = 0x0U, /*!< Eight data bit */
  77. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  78. kLPUART_SevenDataBits = 0x1U, /*!< Seven data bit */
  79. #endif
  80. } lpuart_data_bits_t;
  81. /*! @brief LPUART stop bit count. */
  82. typedef enum _lpuart_stop_bit_count
  83. {
  84. kLPUART_OneStopBit = 0U, /*!< One stop bit */
  85. kLPUART_TwoStopBit = 1U, /*!< Two stop bits */
  86. } lpuart_stop_bit_count_t;
  87. #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT) && FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
  88. /*! @brief LPUART transmit CTS source. */
  89. typedef enum _lpuart_transmit_cts_source
  90. {
  91. kLPUART_CtsSourcePin = 0U, /*!< CTS resource is the LPUART_CTS pin. */
  92. kLPUART_CtsSourceMatchResult = 1U, /*!< CTS resource is the match result. */
  93. } lpuart_transmit_cts_source_t;
  94. /*! @brief LPUART transmit CTS configure. */
  95. typedef enum _lpuart_transmit_cts_config
  96. {
  97. kLPUART_CtsSampleAtStart = 0U, /*!< CTS input is sampled at the start of each character. */
  98. kLPUART_CtsSampleAtIdle = 1U, /*!< CTS input is sampled when the transmitter is idle */
  99. } lpuart_transmit_cts_config_t;
  100. #endif
  101. /*! @brief LPUART idle flag type defines when the receiver starts counting. */
  102. typedef enum _lpuart_idle_type_select
  103. {
  104. kLPUART_IdleTypeStartBit = 0U, /*!< Start counting after a valid start bit. */
  105. kLPUART_IdleTypeStopBit = 1U, /*!< Start conuting after a stop bit. */
  106. } lpuart_idle_type_select_t;
  107. /*! @brief LPUART idle detected configuration.
  108. * This structure defines the number of idle characters that must be received before
  109. * the IDLE flag is set.
  110. */
  111. typedef enum _lpuart_idle_config
  112. {
  113. kLPUART_IdleCharacter1 = 0U, /*!< the number of idle characters. */
  114. kLPUART_IdleCharacter2 = 1U, /*!< the number of idle characters. */
  115. kLPUART_IdleCharacter4 = 2U, /*!< the number of idle characters. */
  116. kLPUART_IdleCharacter8 = 3U, /*!< the number of idle characters. */
  117. kLPUART_IdleCharacter16 = 4U, /*!< the number of idle characters. */
  118. kLPUART_IdleCharacter32 = 5U, /*!< the number of idle characters. */
  119. kLPUART_IdleCharacter64 = 6U, /*!< the number of idle characters. */
  120. kLPUART_IdleCharacter128 = 7U, /*!< the number of idle characters. */
  121. } lpuart_idle_config_t;
  122. /*!
  123. * @brief LPUART interrupt configuration structure, default settings all disabled.
  124. *
  125. * This structure contains the settings for all LPUART interrupt configurations.
  126. */
  127. enum _lpuart_interrupt_enable
  128. {
  129. #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
  130. kLPUART_LinBreakInterruptEnable = (LPUART_BAUD_LBKDIE_MASK >> 8), /*!< LIN break detect. */
  131. #endif
  132. kLPUART_RxActiveEdgeInterruptEnable = (LPUART_BAUD_RXEDGIE_MASK >> 8), /*!< Receive Active Edge. */
  133. kLPUART_TxDataRegEmptyInterruptEnable = (LPUART_CTRL_TIE_MASK), /*!< Transmit data register empty. */
  134. kLPUART_TransmissionCompleteInterruptEnable = (LPUART_CTRL_TCIE_MASK), /*!< Transmission complete. */
  135. kLPUART_RxDataRegFullInterruptEnable = (LPUART_CTRL_RIE_MASK), /*!< Receiver data register full. */
  136. kLPUART_IdleLineInterruptEnable = (LPUART_CTRL_ILIE_MASK), /*!< Idle line. */
  137. kLPUART_RxOverrunInterruptEnable = (LPUART_CTRL_ORIE_MASK), /*!< Receiver Overrun. */
  138. kLPUART_NoiseErrorInterruptEnable = (LPUART_CTRL_NEIE_MASK), /*!< Noise error flag. */
  139. kLPUART_FramingErrorInterruptEnable = (LPUART_CTRL_FEIE_MASK), /*!< Framing error flag. */
  140. kLPUART_ParityErrorInterruptEnable = (LPUART_CTRL_PEIE_MASK), /*!< Parity error flag. */
  141. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  142. kLPUART_TxFifoOverflowInterruptEnable = (LPUART_FIFO_TXOFE_MASK >> 8), /*!< Transmit FIFO Overflow. */
  143. kLPUART_RxFifoUnderflowInterruptEnable = (LPUART_FIFO_RXUFE_MASK >> 8), /*!< Receive FIFO Underflow. */
  144. #endif
  145. };
  146. /*!
  147. * @brief LPUART status flags.
  148. *
  149. * This provides constants for the LPUART status flags for use in the LPUART functions.
  150. */
  151. enum _lpuart_flags
  152. {
  153. kLPUART_TxDataRegEmptyFlag =
  154. (LPUART_STAT_TDRE_MASK), /*!< Transmit data register empty flag, sets when transmit buffer is empty */
  155. kLPUART_TransmissionCompleteFlag =
  156. (LPUART_STAT_TC_MASK), /*!< Transmission complete flag, sets when transmission activity complete */
  157. kLPUART_RxDataRegFullFlag =
  158. (LPUART_STAT_RDRF_MASK), /*!< Receive data register full flag, sets when the receive data buffer is full */
  159. kLPUART_IdleLineFlag = (LPUART_STAT_IDLE_MASK), /*!< Idle line detect flag, sets when idle line detected */
  160. kLPUART_RxOverrunFlag = (LPUART_STAT_OR_MASK), /*!< Receive Overrun, sets when new data is received before data is
  161. read from receive register */
  162. kLPUART_NoiseErrorFlag = (LPUART_STAT_NF_MASK), /*!< Receive takes 3 samples of each received bit. If any of these
  163. samples differ, noise flag sets */
  164. kLPUART_FramingErrorFlag =
  165. (LPUART_STAT_FE_MASK), /*!< Frame error flag, sets if logic 0 was detected where stop bit expected */
  166. kLPUART_ParityErrorFlag = (LPUART_STAT_PF_MASK), /*!< If parity enabled, sets upon parity error detection */
  167. #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
  168. kLPUART_LinBreakFlag = (LPUART_STAT_LBKDIF_MASK), /*!< LIN break detect interrupt flag, sets when LIN break char
  169. detected and LIN circuit enabled */
  170. #endif
  171. kLPUART_RxActiveEdgeFlag =
  172. (LPUART_STAT_RXEDGIF_MASK), /*!< Receive pin active edge interrupt flag, sets when active edge detected */
  173. kLPUART_RxActiveFlag =
  174. (LPUART_STAT_RAF_MASK), /*!< Receiver Active Flag (RAF), sets at beginning of valid start bit */
  175. #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
  176. kLPUART_DataMatch1Flag = LPUART_STAT_MA1F_MASK, /*!< The next character to be read from LPUART_DATA matches MA1*/
  177. kLPUART_DataMatch2Flag = LPUART_STAT_MA2F_MASK, /*!< The next character to be read from LPUART_DATA matches MA2*/
  178. #endif
  179. #if defined(FSL_FEATURE_LPUART_HAS_EXTENDED_DATA_REGISTER_FLAGS) && FSL_FEATURE_LPUART_HAS_EXTENDED_DATA_REGISTER_FLAGS
  180. kLPUART_NoiseErrorInRxDataRegFlag =
  181. (LPUART_DATA_NOISY_MASK >> 10), /*!< NOISY bit, sets if noise detected in current data word */
  182. kLPUART_ParityErrorInRxDataRegFlag =
  183. (LPUART_DATA_PARITYE_MASK >> 10), /*!< PARITYE bit, sets if noise detected in current data word */
  184. #endif
  185. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  186. kLPUART_TxFifoEmptyFlag = (LPUART_FIFO_TXEMPT_MASK >> 16), /*!< TXEMPT bit, sets if transmit buffer is empty */
  187. kLPUART_RxFifoEmptyFlag = (LPUART_FIFO_RXEMPT_MASK >> 16), /*!< RXEMPT bit, sets if receive buffer is empty */
  188. kLPUART_TxFifoOverflowFlag =
  189. (LPUART_FIFO_TXOF_MASK >> 16), /*!< TXOF bit, sets if transmit buffer overflow occurred */
  190. kLPUART_RxFifoUnderflowFlag =
  191. (LPUART_FIFO_RXUF_MASK >> 16), /*!< RXUF bit, sets if receive buffer underflow occurred */
  192. #endif
  193. };
  194. /*! @brief LPUART configuration structure. */
  195. typedef struct _lpuart_config
  196. {
  197. uint32_t baudRate_Bps; /*!< LPUART baud rate */
  198. lpuart_parity_mode_t parityMode; /*!< Parity mode, disabled (default), even, odd */
  199. lpuart_data_bits_t dataBitsCount; /*!< Data bits count, eight (default), seven */
  200. bool isMsb; /*!< Data bits order, LSB (default), MSB */
  201. #if defined(FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT
  202. lpuart_stop_bit_count_t stopBitCount; /*!< Number of stop bits, 1 stop bit (default) or 2 stop bits */
  203. #endif
  204. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  205. uint8_t txFifoWatermark; /*!< TX FIFO watermark */
  206. uint8_t rxFifoWatermark; /*!< RX FIFO watermark */
  207. #endif
  208. #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT) && FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
  209. bool enableRxRTS; /*!< RX RTS enable */
  210. bool enableTxCTS; /*!< TX CTS enable */
  211. lpuart_transmit_cts_source_t txCtsSource; /*!< TX CTS source */
  212. lpuart_transmit_cts_config_t txCtsConfig; /*!< TX CTS configure */
  213. #endif
  214. lpuart_idle_type_select_t rxIdleType; /*!< RX IDLE type. */
  215. lpuart_idle_config_t rxIdleConfig; /*!< RX IDLE configuration. */
  216. bool enableTx; /*!< Enable TX */
  217. bool enableRx; /*!< Enable RX */
  218. } lpuart_config_t;
  219. /*! @brief LPUART transfer structure. */
  220. typedef struct _lpuart_transfer
  221. {
  222. uint8_t *data; /*!< The buffer of data to be transfer.*/
  223. size_t dataSize; /*!< The byte count to be transfer. */
  224. } lpuart_transfer_t;
  225. /* Forward declaration of the handle typedef. */
  226. typedef struct _lpuart_handle lpuart_handle_t;
  227. /*! @brief LPUART transfer callback function. */
  228. typedef void (*lpuart_transfer_callback_t)(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData);
  229. /*! @brief LPUART handle structure. */
  230. struct _lpuart_handle
  231. {
  232. uint8_t *volatile txData; /*!< Address of remaining data to send. */
  233. volatile size_t txDataSize; /*!< Size of the remaining data to send. */
  234. size_t txDataSizeAll; /*!< Size of the data to send out. */
  235. uint8_t *volatile rxData; /*!< Address of remaining data to receive. */
  236. volatile size_t rxDataSize; /*!< Size of the remaining data to receive. */
  237. size_t rxDataSizeAll; /*!< Size of the data to receive. */
  238. uint8_t *rxRingBuffer; /*!< Start address of the receiver ring buffer. */
  239. size_t rxRingBufferSize; /*!< Size of the ring buffer. */
  240. volatile uint16_t rxRingBufferHead; /*!< Index for the driver to store received data into ring buffer. */
  241. volatile uint16_t rxRingBufferTail; /*!< Index for the user to get data from the ring buffer. */
  242. lpuart_transfer_callback_t callback; /*!< Callback function. */
  243. void *userData; /*!< LPUART callback function parameter.*/
  244. volatile uint8_t txState; /*!< TX transfer state. */
  245. volatile uint8_t rxState; /*!< RX transfer state. */
  246. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  247. bool isSevenDataBits; /*!< Seven data bits flag. */
  248. #endif
  249. };
  250. /*******************************************************************************
  251. * API
  252. ******************************************************************************/
  253. #if defined(__cplusplus)
  254. extern "C" {
  255. #endif /* _cplusplus */
  256. #if defined(FSL_FEATURE_LPUART_HAS_GLOBAL) && FSL_FEATURE_LPUART_HAS_GLOBAL
  257. /*!
  258. * @name Software Reset
  259. * @{
  260. */
  261. /*!
  262. * @brief Resets the LPUART using software.
  263. *
  264. * This function resets all internal logic and registers except the Global Register.
  265. * Remains set until cleared by software.
  266. *
  267. * @param base LPUART peripheral base address.
  268. */
  269. static inline void LPUART_SoftwareReset(LPUART_Type *base)
  270. {
  271. base->GLOBAL |= LPUART_GLOBAL_RST_MASK;
  272. base->GLOBAL &= ~LPUART_GLOBAL_RST_MASK;
  273. }
  274. /* @} */
  275. #endif /*FSL_FEATURE_LPUART_HAS_GLOBAL*/
  276. /*!
  277. * @name Initialization and deinitialization
  278. * @{
  279. */
  280. /*!
  281. * @brief Initializes an LPUART instance with the user configuration structure and the peripheral clock.
  282. *
  283. * This function configures the LPUART module with user-defined settings. Call the LPUART_GetDefaultConfig() function
  284. * to configure the configuration structure and get the default configuration.
  285. * The example below shows how to use this API to configure the LPUART.
  286. * @code
  287. * lpuart_config_t lpuartConfig;
  288. * lpuartConfig.baudRate_Bps = 115200U;
  289. * lpuartConfig.parityMode = kLPUART_ParityDisabled;
  290. * lpuartConfig.dataBitsCount = kLPUART_EightDataBits;
  291. * lpuartConfig.isMsb = false;
  292. * lpuartConfig.stopBitCount = kLPUART_OneStopBit;
  293. * lpuartConfig.txFifoWatermark = 0;
  294. * lpuartConfig.rxFifoWatermark = 1;
  295. * LPUART_Init(LPUART1, &lpuartConfig, 20000000U);
  296. * @endcode
  297. *
  298. * @param base LPUART peripheral base address.
  299. * @param config Pointer to a user-defined configuration structure.
  300. * @param srcClock_Hz LPUART clock source frequency in HZ.
  301. * @retval kStatus_LPUART_BaudrateNotSupport Baudrate is not support in current clock source.
  302. * @retval kStatus_Success LPUART initialize succeed
  303. */
  304. status_t LPUART_Init(LPUART_Type *base, const lpuart_config_t *config, uint32_t srcClock_Hz);
  305. /*!
  306. * @brief Deinitializes a LPUART instance.
  307. *
  308. * This function waits for transmit to complete, disables TX and RX, and disables the LPUART clock.
  309. *
  310. * @param base LPUART peripheral base address.
  311. */
  312. void LPUART_Deinit(LPUART_Type *base);
  313. /*!
  314. * @brief Gets the default configuration structure.
  315. *
  316. * This function initializes the LPUART configuration structure to a default value. The default
  317. * values are:
  318. * lpuartConfig->baudRate_Bps = 115200U;
  319. * lpuartConfig->parityMode = kLPUART_ParityDisabled;
  320. * lpuartConfig->dataBitsCount = kLPUART_EightDataBits;
  321. * lpuartConfig->isMsb = false;
  322. * lpuartConfig->stopBitCount = kLPUART_OneStopBit;
  323. * lpuartConfig->txFifoWatermark = 0;
  324. * lpuartConfig->rxFifoWatermark = 1;
  325. * lpuartConfig->rxIdleType = kLPUART_IdleTypeStartBit;
  326. * lpuartConfig->rxIdleConfig = kLPUART_IdleCharacter1;
  327. * lpuartConfig->enableTx = false;
  328. * lpuartConfig->enableRx = false;
  329. *
  330. * @param config Pointer to a configuration structure.
  331. */
  332. void LPUART_GetDefaultConfig(lpuart_config_t *config);
  333. /*!
  334. * @brief Sets the LPUART instance baudrate.
  335. *
  336. * This function configures the LPUART module baudrate. This function is used to update
  337. * the LPUART module baudrate after the LPUART module is initialized by the LPUART_Init.
  338. * @code
  339. * LPUART_SetBaudRate(LPUART1, 115200U, 20000000U);
  340. * @endcode
  341. *
  342. * @param base LPUART peripheral base address.
  343. * @param baudRate_Bps LPUART baudrate to be set.
  344. * @param srcClock_Hz LPUART clock source frequency in HZ.
  345. * @retval kStatus_LPUART_BaudrateNotSupport Baudrate is not supported in the current clock source.
  346. * @retval kStatus_Success Set baudrate succeeded.
  347. */
  348. status_t LPUART_SetBaudRate(LPUART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
  349. /* @} */
  350. /*!
  351. * @name Status
  352. * @{
  353. */
  354. /*!
  355. * @brief Gets LPUART status flags.
  356. *
  357. * This function gets all LPUART status flags. The flags are returned as the logical
  358. * OR value of the enumerators @ref _lpuart_flags. To check for a specific status,
  359. * compare the return value with enumerators in the @ref _lpuart_flags.
  360. * For example, to check whether the TX is empty:
  361. * @code
  362. * if (kLPUART_TxDataRegEmptyFlag & LPUART_GetStatusFlags(LPUART1))
  363. * {
  364. * ...
  365. * }
  366. * @endcode
  367. *
  368. * @param base LPUART peripheral base address.
  369. * @return LPUART status flags which are ORed by the enumerators in the _lpuart_flags.
  370. */
  371. uint32_t LPUART_GetStatusFlags(LPUART_Type *base);
  372. /*!
  373. * @brief Clears status flags with a provided mask.
  374. *
  375. * This function clears LPUART status flags with a provided mask. Automatically cleared flags
  376. * can't be cleared by this function.
  377. * Flags that can only cleared or set by hardware are:
  378. * kLPUART_TxDataRegEmptyFlag, kLPUART_TransmissionCompleteFlag, kLPUART_RxDataRegFullFlag,
  379. * kLPUART_RxActiveFlag, kLPUART_NoiseErrorInRxDataRegFlag, kLPUART_ParityErrorInRxDataRegFlag,
  380. * kLPUART_TxFifoEmptyFlag,kLPUART_RxFifoEmptyFlag
  381. * Note: This API should be called when the Tx/Rx is idle, otherwise it takes no effects.
  382. *
  383. * @param base LPUART peripheral base address.
  384. * @param mask the status flags to be cleared. The user can use the enumerators in the
  385. * _lpuart_status_flag_t to do the OR operation and get the mask.
  386. * @return 0 succeed, others failed.
  387. * @retval kStatus_LPUART_FlagCannotClearManually The flag can't be cleared by this function but
  388. * it is cleared automatically by hardware.
  389. * @retval kStatus_Success Status in the mask are cleared.
  390. */
  391. status_t LPUART_ClearStatusFlags(LPUART_Type *base, uint32_t mask);
  392. /* @} */
  393. /*!
  394. * @name Interrupts
  395. * @{
  396. */
  397. /*!
  398. * @brief Enables LPUART interrupts according to a provided mask.
  399. *
  400. * This function enables the LPUART interrupts according to a provided mask. The mask
  401. * is a logical OR of enumeration members. See the @ref _lpuart_interrupt_enable.
  402. * This examples shows how to enable TX empty interrupt and RX full interrupt:
  403. * @code
  404. * LPUART_EnableInterrupts(LPUART1,kLPUART_TxDataRegEmptyInterruptEnable | kLPUART_RxDataRegFullInterruptEnable);
  405. * @endcode
  406. *
  407. * @param base LPUART peripheral base address.
  408. * @param mask The interrupts to enable. Logical OR of @ref _uart_interrupt_enable.
  409. */
  410. void LPUART_EnableInterrupts(LPUART_Type *base, uint32_t mask);
  411. /*!
  412. * @brief Disables LPUART interrupts according to a provided mask.
  413. *
  414. * This function disables the LPUART interrupts according to a provided mask. The mask
  415. * is a logical OR of enumeration members. See @ref _lpuart_interrupt_enable.
  416. * This example shows how to disable the TX empty interrupt and RX full interrupt:
  417. * @code
  418. * LPUART_DisableInterrupts(LPUART1,kLPUART_TxDataRegEmptyInterruptEnable | kLPUART_RxDataRegFullInterruptEnable);
  419. * @endcode
  420. *
  421. * @param base LPUART peripheral base address.
  422. * @param mask The interrupts to disable. Logical OR of @ref _lpuart_interrupt_enable.
  423. */
  424. void LPUART_DisableInterrupts(LPUART_Type *base, uint32_t mask);
  425. /*!
  426. * @brief Gets enabled LPUART interrupts.
  427. *
  428. * This function gets the enabled LPUART interrupts. The enabled interrupts are returned
  429. * as the logical OR value of the enumerators @ref _lpuart_interrupt_enable. To check
  430. * a specific interrupt enable status, compare the return value with enumerators
  431. * in @ref _lpuart_interrupt_enable.
  432. * For example, to check whether the TX empty interrupt is enabled:
  433. * @code
  434. * uint32_t enabledInterrupts = LPUART_GetEnabledInterrupts(LPUART1);
  435. *
  436. * if (kLPUART_TxDataRegEmptyInterruptEnable & enabledInterrupts)
  437. * {
  438. * ...
  439. * }
  440. * @endcode
  441. *
  442. * @param base LPUART peripheral base address.
  443. * @return LPUART interrupt flags which are logical OR of the enumerators in @ref _lpuart_interrupt_enable.
  444. */
  445. uint32_t LPUART_GetEnabledInterrupts(LPUART_Type *base);
  446. #if defined(FSL_FEATURE_LPUART_HAS_DMA_ENABLE) && FSL_FEATURE_LPUART_HAS_DMA_ENABLE
  447. /*!
  448. * @brief Gets the LPUART data register address.
  449. *
  450. * This function returns the LPUART data register address, which is mainly used by the DMA/eDMA.
  451. *
  452. * @param base LPUART peripheral base address.
  453. * @return LPUART data register addresses which are used both by the transmitter and receiver.
  454. */
  455. static inline uint32_t LPUART_GetDataRegisterAddress(LPUART_Type *base)
  456. {
  457. return (uint32_t) & (base->DATA);
  458. }
  459. /*!
  460. * @brief Enables or disables the LPUART transmitter DMA request.
  461. *
  462. * This function enables or disables the transmit data register empty flag, STAT[TDRE], to generate DMA requests.
  463. *
  464. * @param base LPUART peripheral base address.
  465. * @param enable True to enable, false to disable.
  466. */
  467. static inline void LPUART_EnableTxDMA(LPUART_Type *base, bool enable)
  468. {
  469. if (enable)
  470. {
  471. base->BAUD |= LPUART_BAUD_TDMAE_MASK;
  472. }
  473. else
  474. {
  475. base->BAUD &= ~LPUART_BAUD_TDMAE_MASK;
  476. }
  477. }
  478. /*!
  479. * @brief Enables or disables the LPUART receiver DMA.
  480. *
  481. * This function enables or disables the receiver data register full flag, STAT[RDRF], to generate DMA requests.
  482. *
  483. * @param base LPUART peripheral base address.
  484. * @param enable True to enable, false to disable.
  485. */
  486. static inline void LPUART_EnableRxDMA(LPUART_Type *base, bool enable)
  487. {
  488. if (enable)
  489. {
  490. base->BAUD |= LPUART_BAUD_RDMAE_MASK;
  491. }
  492. else
  493. {
  494. base->BAUD &= ~LPUART_BAUD_RDMAE_MASK;
  495. }
  496. }
  497. /* @} */
  498. #endif /* FSL_FEATURE_LPUART_HAS_DMA_ENABLE */
  499. /*!
  500. * @name Bus Operations
  501. * @{
  502. */
  503. /*!
  504. * @brief Enables or disables the LPUART transmitter.
  505. *
  506. * This function enables or disables the LPUART transmitter.
  507. *
  508. * @param base LPUART peripheral base address.
  509. * @param enable True to enable, false to disable.
  510. */
  511. static inline void LPUART_EnableTx(LPUART_Type *base, bool enable)
  512. {
  513. if (enable)
  514. {
  515. base->CTRL |= LPUART_CTRL_TE_MASK;
  516. }
  517. else
  518. {
  519. base->CTRL &= ~LPUART_CTRL_TE_MASK;
  520. }
  521. }
  522. /*!
  523. * @brief Enables or disables the LPUART receiver.
  524. *
  525. * This function enables or disables the LPUART receiver.
  526. *
  527. * @param base LPUART peripheral base address.
  528. * @param enable True to enable, false to disable.
  529. */
  530. static inline void LPUART_EnableRx(LPUART_Type *base, bool enable)
  531. {
  532. if (enable)
  533. {
  534. base->CTRL |= LPUART_CTRL_RE_MASK;
  535. }
  536. else
  537. {
  538. base->CTRL &= ~LPUART_CTRL_RE_MASK;
  539. }
  540. }
  541. /*!
  542. * @brief Writes to the transmitter register.
  543. *
  544. * This function writes data to the transmitter register directly. The upper layer must
  545. * ensure that the TX register is empty or that the TX FIFO has room before calling this function.
  546. *
  547. * @param base LPUART peripheral base address.
  548. * @param data Data write to the TX register.
  549. */
  550. static inline void LPUART_WriteByte(LPUART_Type *base, uint8_t data)
  551. {
  552. base->DATA = data;
  553. }
  554. /*!
  555. * @brief Reads the receiver register.
  556. *
  557. * This function reads data from the receiver register directly. The upper layer must
  558. * ensure that the receiver register is full or that the RX FIFO has data before calling this function.
  559. *
  560. * @param base LPUART peripheral base address.
  561. * @return Data read from data register.
  562. */
  563. static inline uint8_t LPUART_ReadByte(LPUART_Type *base)
  564. {
  565. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  566. uint32_t ctrl = base->CTRL;
  567. bool isSevenDataBits =
  568. ((ctrl & LPUART_CTRL_M7_MASK) ||
  569. ((!(ctrl & LPUART_CTRL_M7_MASK)) && (!(ctrl & LPUART_CTRL_M_MASK)) && (ctrl & LPUART_CTRL_PE_MASK)));
  570. if (isSevenDataBits)
  571. {
  572. return (base->DATA & 0x7F);
  573. }
  574. else
  575. {
  576. return base->DATA;
  577. }
  578. #else
  579. return base->DATA;
  580. #endif
  581. }
  582. /*!
  583. * @brief Writes to the transmitter register using a blocking method.
  584. *
  585. * This function polls the transmitter register, waits for the register to be empty or for TX FIFO to have
  586. * room, and writes data to the transmitter buffer.
  587. *
  588. * @note This function does not check whether all data has been sent out to the bus.
  589. * Before disabling the transmitter, check the kLPUART_TransmissionCompleteFlag to ensure that the transmit is
  590. * finished.
  591. *
  592. * @param base LPUART peripheral base address.
  593. * @param data Start address of the data to write.
  594. * @param length Size of the data to write.
  595. */
  596. void LPUART_WriteBlocking(LPUART_Type *base, const uint8_t *data, size_t length);
  597. /*!
  598. * @brief Reads the receiver data register using a blocking method.
  599. *
  600. * This function polls the receiver register, waits for the receiver register full or receiver FIFO
  601. * has data, and reads data from the TX register.
  602. *
  603. * @param base LPUART peripheral base address.
  604. * @param data Start address of the buffer to store the received data.
  605. * @param length Size of the buffer.
  606. * @retval kStatus_LPUART_RxHardwareOverrun Receiver overrun happened while receiving data.
  607. * @retval kStatus_LPUART_NoiseError Noise error happened while receiving data.
  608. * @retval kStatus_LPUART_FramingError Framing error happened while receiving data.
  609. * @retval kStatus_LPUART_ParityError Parity error happened while receiving data.
  610. * @retval kStatus_Success Successfully received all data.
  611. */
  612. status_t LPUART_ReadBlocking(LPUART_Type *base, uint8_t *data, size_t length);
  613. /* @} */
  614. /*!
  615. * @name Transactional
  616. * @{
  617. */
  618. /*!
  619. * @brief Initializes the LPUART handle.
  620. *
  621. * This function initializes the LPUART handle, which can be used for other LPUART
  622. * transactional APIs. Usually, for a specified LPUART instance,
  623. * call this API once to get the initialized handle.
  624. *
  625. * The LPUART driver supports the "background" receiving, which means that user can set up
  626. * an RX ring buffer optionally. Data received is stored into the ring buffer even when the
  627. * user doesn't call the LPUART_TransferReceiveNonBlocking() API. If there is already data received
  628. * in the ring buffer, the user can get the received data from the ring buffer directly.
  629. * The ring buffer is disabled if passing NULL as @p ringBuffer.
  630. *
  631. * @param base LPUART peripheral base address.
  632. * @param handle LPUART handle pointer.
  633. * @param callback Callback function.
  634. * @param userData User data.
  635. */
  636. void LPUART_TransferCreateHandle(LPUART_Type *base,
  637. lpuart_handle_t *handle,
  638. lpuart_transfer_callback_t callback,
  639. void *userData);
  640. /*!
  641. * @brief Transmits a buffer of data using the interrupt method.
  642. *
  643. * This function send data using an interrupt method. This is a non-blocking function, which
  644. * returns directly without waiting for all data written to the transmitter register. When
  645. * all data is written to the TX register in the ISR, the LPUART driver calls the callback
  646. * function and passes the @ref kStatus_LPUART_TxIdle as status parameter.
  647. *
  648. * @note The kStatus_LPUART_TxIdle is passed to the upper layer when all data are written
  649. * to the TX register. However, there is no check to ensure that all the data sent out. Before disabling the TX,
  650. * check the kLPUART_TransmissionCompleteFlag to ensure that the transmit is finished.
  651. *
  652. * @param base LPUART peripheral base address.
  653. * @param handle LPUART handle pointer.
  654. * @param xfer LPUART transfer structure, see #lpuart_transfer_t.
  655. * @retval kStatus_Success Successfully start the data transmission.
  656. * @retval kStatus_LPUART_TxBusy Previous transmission still not finished, data not all written to the TX register.
  657. * @retval kStatus_InvalidArgument Invalid argument.
  658. */
  659. status_t LPUART_TransferSendNonBlocking(LPUART_Type *base, lpuart_handle_t *handle, lpuart_transfer_t *xfer);
  660. /*!
  661. * @brief Sets up the RX ring buffer.
  662. *
  663. * This function sets up the RX ring buffer to a specific UART handle.
  664. *
  665. * When the RX ring buffer is used, data received is stored into the ring buffer even when
  666. * the user doesn't call the UART_TransferReceiveNonBlocking() API. If there is already data received
  667. * in the ring buffer, the user can get the received data from the ring buffer directly.
  668. *
  669. * @note When using RX ring buffer, one byte is reserved for internal use. In other
  670. * words, if @p ringBufferSize is 32, then only 31 bytes are used for saving data.
  671. *
  672. * @param base LPUART peripheral base address.
  673. * @param handle LPUART handle pointer.
  674. * @param ringBuffer Start address of ring buffer for background receiving. Pass NULL to disable the ring buffer.
  675. * @param ringBufferSize size of the ring buffer.
  676. */
  677. void LPUART_TransferStartRingBuffer(LPUART_Type *base,
  678. lpuart_handle_t *handle,
  679. uint8_t *ringBuffer,
  680. size_t ringBufferSize);
  681. /*!
  682. * @brief Aborts the background transfer and uninstalls the ring buffer.
  683. *
  684. * This function aborts the background transfer and uninstalls the ring buffer.
  685. *
  686. * @param base LPUART peripheral base address.
  687. * @param handle LPUART handle pointer.
  688. */
  689. void LPUART_TransferStopRingBuffer(LPUART_Type *base, lpuart_handle_t *handle);
  690. /*!
  691. * @brief Get the length of received data in RX ring buffer.
  692. *
  693. * @userData handle LPUART handle pointer.
  694. * @return Length of received data in RX ring buffer.
  695. */
  696. size_t LPUART_TransferGetRxRingBufferLength(LPUART_Type *base, lpuart_handle_t *handle);
  697. /*!
  698. * @brief Aborts the interrupt-driven data transmit.
  699. *
  700. * This function aborts the interrupt driven data sending. The user can get the remainBtyes to find out
  701. * how many bytes are not sent out.
  702. *
  703. * @param base LPUART peripheral base address.
  704. * @param handle LPUART handle pointer.
  705. */
  706. void LPUART_TransferAbortSend(LPUART_Type *base, lpuart_handle_t *handle);
  707. /*!
  708. * @brief Gets the number of bytes that have been written to the LPUART transmitter register.
  709. *
  710. * This function gets the number of bytes that have been written to LPUART TX
  711. * register by an interrupt method.
  712. *
  713. * @param base LPUART peripheral base address.
  714. * @param handle LPUART handle pointer.
  715. * @param count Send bytes count.
  716. * @retval kStatus_NoTransferInProgress No send in progress.
  717. * @retval kStatus_InvalidArgument Parameter is invalid.
  718. * @retval kStatus_Success Get successfully through the parameter \p count;
  719. */
  720. status_t LPUART_TransferGetSendCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count);
  721. /*!
  722. * @brief Receives a buffer of data using the interrupt method.
  723. *
  724. * This function receives data using an interrupt method. This is a non-blocking function
  725. * which returns without waiting to ensure that all data are received.
  726. * If the RX ring buffer is used and not empty, the data in the ring buffer is copied and
  727. * the parameter @p receivedBytes shows how many bytes are copied from the ring buffer.
  728. * After copying, if the data in the ring buffer is not enough for read, the receive
  729. * request is saved by the LPUART driver. When the new data arrives, the receive request
  730. * is serviced first. When all data is received, the LPUART driver notifies the upper layer
  731. * through a callback function and passes a status parameter @ref kStatus_UART_RxIdle.
  732. * For example, the upper layer needs 10 bytes but there are only 5 bytes in ring buffer.
  733. * The 5 bytes are copied to xfer->data, which returns with the
  734. * parameter @p receivedBytes set to 5. For the remaining 5 bytes, the newly arrived data is
  735. * saved from xfer->data[5]. When 5 bytes are received, the LPUART driver notifies the upper layer.
  736. * If the RX ring buffer is not enabled, this function enables the RX and RX interrupt
  737. * to receive data to xfer->data. When all data is received, the upper layer is notified.
  738. *
  739. * @param base LPUART peripheral base address.
  740. * @param handle LPUART handle pointer.
  741. * @param xfer LPUART transfer structure, see #uart_transfer_t.
  742. * @param receivedBytes Bytes received from the ring buffer directly.
  743. * @retval kStatus_Success Successfully queue the transfer into the transmit queue.
  744. * @retval kStatus_LPUART_RxBusy Previous receive request is not finished.
  745. * @retval kStatus_InvalidArgument Invalid argument.
  746. */
  747. status_t LPUART_TransferReceiveNonBlocking(LPUART_Type *base,
  748. lpuart_handle_t *handle,
  749. lpuart_transfer_t *xfer,
  750. size_t *receivedBytes);
  751. /*!
  752. * @brief Aborts the interrupt-driven data receiving.
  753. *
  754. * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to find out
  755. * how many bytes not received yet.
  756. *
  757. * @param base LPUART peripheral base address.
  758. * @param handle LPUART handle pointer.
  759. */
  760. void LPUART_TransferAbortReceive(LPUART_Type *base, lpuart_handle_t *handle);
  761. /*!
  762. * @brief Gets the number of bytes that have been received.
  763. *
  764. * This function gets the number of bytes that have been received.
  765. *
  766. * @param base LPUART peripheral base address.
  767. * @param handle LPUART handle pointer.
  768. * @param count Receive bytes count.
  769. * @retval kStatus_NoTransferInProgress No receive in progress.
  770. * @retval kStatus_InvalidArgument Parameter is invalid.
  771. * @retval kStatus_Success Get successfully through the parameter \p count;
  772. */
  773. status_t LPUART_TransferGetReceiveCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count);
  774. /*!
  775. * @brief LPUART IRQ handle function.
  776. *
  777. * This function handles the LPUART transmit and receive IRQ request.
  778. *
  779. * @param base LPUART peripheral base address.
  780. * @param handle LPUART handle pointer.
  781. */
  782. void LPUART_TransferHandleIRQ(LPUART_Type *base, lpuart_handle_t *handle);
  783. /*!
  784. * @brief LPUART Error IRQ handle function.
  785. *
  786. * This function handles the LPUART error IRQ request.
  787. *
  788. * @param base LPUART peripheral base address.
  789. * @param handle LPUART handle pointer.
  790. */
  791. void LPUART_TransferHandleErrorIRQ(LPUART_Type *base, lpuart_handle_t *handle);
  792. /* @} */
  793. #if defined(__cplusplus)
  794. }
  795. #endif
  796. /*! @}*/
  797. #endif /* _FSL_LPUART_H_ */