fsl_lpuart.h 36 KB

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