fsl_ecspi.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*
  2. * Copyright (c) 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_ECSPI_H_
  31. #define _FSL_ECSPI_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup ecspi_driver
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief ECSPI driver version 2.0.0. */
  43. #define FSL_ECSPI_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  44. /*@}*/
  45. #ifndef ECSPI_DUMMYDATA
  46. /*! @brief ECSPI dummy transfer data, the data is sent while txBuff is NULL. */
  47. #define ECSPI_DUMMYDATA (0xFFFFFFFFU)
  48. #endif
  49. /*! @brief Return status for the ECSPI driver. */
  50. enum _ecspi_status
  51. {
  52. kStatus_ECSPI_Busy = MAKE_STATUS(kStatusGroup_ECSPI, 0), /*!< ECSPI bus is busy */
  53. kStatus_ECSPI_Idle = MAKE_STATUS(kStatusGroup_ECSPI, 1), /*!< ECSPI is idle */
  54. kStatus_ECSPI_Error = MAKE_STATUS(kStatusGroup_ECSPI, 2), /*!< ECSPI error */
  55. kStatus_ECSPI_HardwareOverFlow = MAKE_STATUS(kStatusGroup_ECSPI, 3), /*!< ECSPI hardware overflow */
  56. };
  57. /*! @brief ECSPI clock polarity configuration. */
  58. typedef enum _ecspi_clock_polarity
  59. {
  60. kECSPI_PolarityActiveHigh = 0x0U, /*!< Active-high ECSPI polarity high (idles low). */
  61. kECSPI_PolarityActiveLow, /*!< Active-low ECSPI polarity low (idles high). */
  62. } ecspi_clock_polarity_t;
  63. /*! @brief ECSPI clock phase configuration. */
  64. typedef enum _ecspi_clock_phase
  65. {
  66. kECSPI_ClockPhaseFirstEdge =
  67. 0x0U, /*!< First edge on SPSCK occurs at the middle of the first cycle of a data transfer. */
  68. kECSPI_ClockPhaseSecondEdge, /*!< First edge on SPSCK occurs at the start of the first cycle of a data transfer. */
  69. } ecspi_clock_phase_t;
  70. /*! @brief ECSPI interrupt sources. */
  71. enum _ecspi_interrupt_enable
  72. {
  73. kECSPI_TxfifoEmptyInterruptEnable = ECSPI_INTREG_TEEN_MASK, /*!< Transmit FIFO buffer empty interrupt */
  74. kECSPI_TxFifoDataRequstInterruptEnable = ECSPI_INTREG_TDREN_MASK, /*!< Transmit FIFO data requst interrupt */
  75. kECSPI_TxFifoFullInterruptEnable = ECSPI_INTREG_TFEN_MASK, /*!< Transmit FIFO full interrupt */
  76. kECSPI_RxFifoReadyInterruptEnable = ECSPI_INTREG_RREN_MASK, /*!< Receiver FIFO ready interrupt */
  77. kECSPI_RxFifoDataRequstInterruptEnable = ECSPI_INTREG_RDREN_MASK, /*!< Receiver FIFO data requst interrupt */
  78. kECSPI_RxFifoFullInterruptEnable = ECSPI_INTREG_RFEN_MASK, /*!< Receiver FIFO full interrupt */
  79. kECSPI_RxFifoOverFlowInterruptEnable = ECSPI_INTREG_ROEN_MASK, /*!< Receiver FIFO buffer overflow interrupt */
  80. kECSPI_TransferCompleteInterruptEnable = ECSPI_INTREG_TCEN_MASK, /*!< Transfer complete interrupt */
  81. kECSPI_AllInterruptEnable = (ECSPI_INTREG_TEEN_MASK | ECSPI_INTREG_TDREN_MASK | ECSPI_INTREG_TFEN_MASK |
  82. ECSPI_INTREG_RREN_MASK | ECSPI_INTREG_RDREN_MASK | ECSPI_INTREG_RFEN_MASK |
  83. ECSPI_INTREG_ROEN_MASK | ECSPI_INTREG_TCEN_MASK), /*!< All interrupt */
  84. };
  85. /*! @brief ECSPI status flags. */
  86. enum _ecspi_flags
  87. {
  88. kECSPI_TxfifoEmptyFlag = ECSPI_STATREG_TE_MASK, /*!< Transmit FIFO buffer empty flag */
  89. kECSPI_TxFifoDataRequstFlag = ECSPI_STATREG_TDR_MASK, /*!< Transmit FIFO data requst flag */
  90. kECSPI_TxFifoFullFlag = ECSPI_STATREG_TF_MASK, /*!< Transmit FIFO full flag */
  91. kECSPI_RxFifoReadyFlag = ECSPI_STATREG_RR_MASK, /*!< Receiver FIFO ready flag */
  92. kECSPI_RxFifoDataRequstFlag = ECSPI_STATREG_RDR_MASK, /*!< Receiver FIFO data requst flag */
  93. kECSPI_RxFifoFullFlag = ECSPI_STATREG_RF_MASK, /*!< Receiver FIFO full flag */
  94. kECSPI_RxFifoOverFlowFlag = ECSPI_STATREG_RO_MASK, /*!< Receiver FIFO buffer overflow flag */
  95. kECSPI_TransferCompleteFlag = ECSPI_STATREG_TC_MASK, /*!< Transfer complete flag */
  96. };
  97. /*! @brief ECSPI DMA enable.*/
  98. enum _ecspi_dma_enable_t
  99. {
  100. kECSPI_TxDmaEnable = ECSPI_DMAREG_TEDEN_MASK, /*!< Tx DMA request source */
  101. kECSPI_RxDmaEnable = ECSPI_DMAREG_RXDEN_MASK, /*!< Rx DMA request source */
  102. kECSPI_DmaAllEnable = (ECSPI_DMAREG_TEDEN_MASK | ECSPI_DMAREG_RXDEN_MASK) /*!< All DMA request source*/
  103. };
  104. /*! @brief ECSPI SPI_RDY signal configuration. */
  105. typedef enum _ecspi_data_ready
  106. {
  107. kECSPI_DataReadyIgnore = 0x0U, /*!< SPI_RDY signal is ignored */
  108. kECSPI_DataReadyFallingEdge, /*!< SPI_RDY signal will be triggerd by the falling edge */
  109. kECSPI_DataReadyLowLevel, /*!< SPI_RDY signal will be triggerd by a low level */
  110. } ecspi_Data_ready_t;
  111. /*! @brief ECSPI channel select source. */
  112. typedef enum _ecspi_channel_source
  113. {
  114. kECSPI_Channel0 = 0x0U, /*!< Channel 0 is selectd */
  115. kECSPI_Channel1, /*!< Channel 1 is selectd */
  116. kECSPI_Channel2, /*!< Channel 2 is selectd */
  117. kECSPI_Channel3, /*!< Channel 3 is selectd */
  118. } ecspi_channel_source_t;
  119. /*! @brief ECSPI master or slave mode configuration. */
  120. typedef enum _ecspi_master_slave_mode
  121. {
  122. kECSPI_Slave = 0U, /*!< ECSPI peripheral operates in slave mode.*/
  123. kECSPI_Master, /*!< ECSPI peripheral operates in master mode.*/
  124. } ecspi_master_slave_mode_t;
  125. /*! @brief ECSPI data line inactive state configuration. */
  126. typedef enum _ecspi_data_line_inactive_state_t
  127. {
  128. kECSPI_DataLineInactiveStateHigh = 0x0U, /*!< The data line inactive state stays high. */
  129. kECSPI_DataLineInactiveStateLow, /*!< The data line inactive state stays low. */
  130. } ecspi_data_line_inactive_state_t;
  131. /*! @brief ECSPI clock inactive state configuration. */
  132. typedef enum _ecspi_clock_inactive_state_t
  133. {
  134. kECSPI_ClockInactiveStateLow = 0x0U, /*!< The SCLK inactive state stays low. */
  135. kECSPI_ClockInactiveStateHigh, /*!< The SCLK inactive state stays high. */
  136. } ecspi_clock_inactive_state_t;
  137. /*! @brief ECSPI active state configuration.*/
  138. typedef enum _ecspi_chip_select_active_state_t
  139. {
  140. kECSPI_ChipSelectActiveStateLow = 0x0U, /*!< The SS signal line active stays low. */
  141. kECSPI_ChipSelectActiveStateHigh, /*!< The SS signal line active stays high. */
  142. } ecspi_chip_select_active_state_t;
  143. /*! @brief ECSPI wave form configuration.*/
  144. typedef enum _ecspi_wave_form_t
  145. {
  146. kECSPI_WaveFormSingle = 0x0U, /*!< The wave form for signal burst */
  147. kECSPI_WaveFormMultiple, /*!< The wave form for multiple burst */
  148. } ecspi_wave_form_t;
  149. /*! @brief ECSPI sample period clock configuration.*/
  150. typedef enum _ecspi_sample_period_clock_source
  151. {
  152. kECSPI_spiClock = 0x0U, /*!< The sample period clock source is SCLK. */
  153. kECSPI_lowFreqClock, /*!< The sample seriod clock source is low_frequency reference clock(32.768 kHz). */
  154. } ecspi_sample_period_clock_source_t;
  155. /*! @brief ECSPI user channel configure structure.*/
  156. typedef struct _ecspi_channel_config
  157. {
  158. ecspi_master_slave_mode_t channelMode; /*!< Channel mode */
  159. ecspi_clock_inactive_state_t clockInactiveState; /*!< Clock line (SCLK) inactive state */
  160. ecspi_data_line_inactive_state_t dataLineInactiveState; /*!< Data line (MOSI&MISO) inactive state */
  161. ecspi_chip_select_active_state_t chipSlectActiveState; /*!< Chip select(SS) line active state */
  162. ecspi_wave_form_t waveForm; /*!< Wave form */
  163. ecspi_clock_polarity_t polarity; /*!< Clock polarity */
  164. ecspi_clock_phase_t phase; /*!< Clock phase */
  165. } ecspi_channel_config_t;
  166. /*! @brief ECSPI master configure structure.*/
  167. typedef struct _ecspi_master_config
  168. {
  169. ecspi_channel_source_t channel; /*!< Channel number */
  170. ecspi_channel_config_t channelConfig; /*!< Channel configuration */
  171. ecspi_sample_period_clock_source_t samplePeriodClock; /*!< Sample period clock source */
  172. uint8_t burstLength; /*!< Burst length */
  173. uint8_t chipSelectDelay; /*!< SS delay time */
  174. uint16_t samplePeriod; /*!< Sample period */
  175. uint8_t txFifoThreshold; /*!< TX Threshold */
  176. uint8_t rxFifoThreshold; /*!< RX Threshold */
  177. uint32_t baudRate_Bps; /*!< ECSPI baud rate for master mode */
  178. } ecspi_master_config_t;
  179. /*! @brief ECSPI slave configure structure.*/
  180. typedef struct _ecspi_slave_config
  181. {
  182. ecspi_channel_source_t channel; /*Channel number */
  183. uint8_t burstLength; /*!< Burst length */
  184. uint8_t txFifoThreshold; /*!< TX Threshold */
  185. uint8_t rxFifoThreshold; /*!< RX Threshold */
  186. ecspi_channel_config_t channelConfig; /*!< Channel configuration */
  187. } ecspi_slave_config_t;
  188. /*! @brief ECSPI transfer structure */
  189. typedef struct _ecspi_transfer
  190. {
  191. uint32_t *txData; /*!< Send buffer */
  192. uint32_t *rxData; /*!< Receive buffer */
  193. size_t dataSize; /*!< Transfer bytes */
  194. ecspi_channel_source_t channel; /*!< ECSPI channel select */
  195. } ecspi_transfer_t;
  196. typedef struct _ecspi_master_handle ecspi_master_handle_t;
  197. /*! @brief Slave handle is the same with master handle */
  198. typedef ecspi_master_handle_t ecspi_slave_handle_t;
  199. /*! @brief ECSPI master callback for finished transmit */
  200. typedef void (*ecspi_master_callback_t)(ECSPI_Type *base,
  201. ecspi_master_handle_t *handle,
  202. status_t status,
  203. void *userData);
  204. /*! @brief ECSPI slave callback for finished transmit */
  205. typedef void (*ecspi_slave_callback_t)(ECSPI_Type *base, ecspi_slave_handle_t *handle, status_t status, void *userData);
  206. /*! @brief ECSPI master handle structure */
  207. struct _ecspi_master_handle
  208. {
  209. ecspi_channel_source_t channel; /*!< Channel number */
  210. uint32_t *volatile txData; /*!< Transfer buffer */
  211. uint32_t *volatile rxData; /*!< Receive buffer */
  212. volatile size_t txRemainingBytes; /*!< Send data remaining in bytes */
  213. volatile size_t rxRemainingBytes; /*!< Receive data remaining in bytes */
  214. volatile uint32_t state; /*!< ECSPI internal state */
  215. size_t transferSize; /*!< Bytes to be transferred */
  216. ecspi_master_callback_t callback; /*!< ECSPI callback */
  217. void *userData; /*!< Callback parameter */
  218. };
  219. #if defined(__cplusplus)
  220. extern "C" {
  221. #endif
  222. /*******************************************************************************
  223. * APIs
  224. ******************************************************************************/
  225. /*!
  226. * @name Initialization and deinitialization
  227. * @{
  228. */
  229. /*!
  230. * @brief Sets the ECSPI configuration structure to default values.
  231. *
  232. * The purpose of this API is to get the configuration structure initialized for use in ECSPI_MasterInit().
  233. * User may use the initialized structure unchanged in ECSPI_MasterInit, or modify
  234. * some fields of the structure before calling ECSPI_MasterInit. After calling this API,
  235. * the master is ready to transfer.
  236. * Example:
  237. @code
  238. ecspi_master_config_t config;
  239. ECSPI_MasterGetDefaultConfig(&config);
  240. @endcode
  241. *
  242. * @param config pointer to config structure
  243. */
  244. void ECSPI_MasterGetDefaultConfig(ecspi_master_config_t *config);
  245. /*!
  246. * @brief Initializes the ECSPI with configuration.
  247. *
  248. * The configuration structure can be filled by user from scratch, or be set with default
  249. * values by ECSPI_MasterGetDefaultConfig(). After calling this API, the slave is ready to transfer.
  250. * Example
  251. @code
  252. ecspi_master_config_t config = {
  253. .baudRate_Bps = 400000,
  254. ...
  255. };
  256. ECSPI_MasterInit(ECSPI0, &config);
  257. @endcode
  258. *
  259. * @param base ECSPI base pointer
  260. * @param config pointer to master configuration structure
  261. * @param srcClock_Hz Source clock frequency.
  262. */
  263. void ECSPI_MasterInit(ECSPI_Type *base, const ecspi_master_config_t *config, uint32_t srcClock_Hz);
  264. /*!
  265. * @brief Sets the ECSPI configuration structure to default values.
  266. *
  267. * The purpose of this API is to get the configuration structure initialized for use in ECSPI_SlaveInit().
  268. * User may use the initialized structure unchanged in ECSPI_SlaveInit(), or modify
  269. * some fields of the structure before calling ECSPI_SlaveInit(). After calling this API,
  270. * the master is ready to transfer.
  271. * Example:
  272. @code
  273. ecspi_Slaveconfig_t config;
  274. ECSPI_SlaveGetDefaultConfig(&config);
  275. @endcode
  276. *
  277. * @param config pointer to config structure
  278. */
  279. void ECSPI_SlaveGetDefaultConfig(ecspi_slave_config_t *config);
  280. /*!
  281. * @brief Initializes the ECSPI with configuration.
  282. *
  283. * The configuration structure can be filled by user from scratch, or be set with default
  284. * values by ECSPI_SlaveGetDefaultConfig(). After calling this API, the slave is ready to transfer.
  285. * Example
  286. @code
  287. ecspi_Salveconfig_t config = {
  288. .baudRate_Bps = 400000,
  289. ...
  290. };
  291. ECSPI_SlaveInit(ECSPI1, &config);
  292. @endcode
  293. *
  294. * @param base ECSPI base pointer
  295. * @param config pointer to master configuration structure
  296. */
  297. void ECSPI_SlaveInit(ECSPI_Type *base, const ecspi_slave_config_t *config);
  298. /*!
  299. * @brief De-initializes the ECSPI.
  300. *
  301. * Calling this API resets the ECSPI module, gates the ECSPI clock.
  302. * The ECSPI module can't work unless calling the ECSPI_MasterInit/ECSPI_SlaveInit to initialize module.
  303. *
  304. * @param base ECSPI base pointer
  305. */
  306. void ECSPI_Deinit(ECSPI_Type *base);
  307. /*!
  308. * @brief Enables or disables the ECSPI.
  309. *
  310. * @param base ECSPI base pointer
  311. * @param enable pass true to enable module, false to disable module
  312. */
  313. static inline void ECSPI_Enable(ECSPI_Type *base, bool enable)
  314. {
  315. if (enable)
  316. {
  317. base->CONREG |= ECSPI_CONREG_EN_MASK;
  318. }
  319. else
  320. {
  321. base->CONREG &= ~ECSPI_CONREG_EN_MASK;
  322. }
  323. }
  324. /*! @} */
  325. /*!
  326. * @name Status
  327. * @{
  328. */
  329. /*!
  330. * @brief Gets the status flag.
  331. *
  332. * @param base ECSPI base pointer
  333. * @return ECSPI Status, use status flag to AND #_ecspi_flags could get the related status.
  334. */
  335. static inline uint32_t ECSPI_GetStatusFlags(ECSPI_Type *base)
  336. {
  337. return (base->STATREG);
  338. }
  339. /*!
  340. * @brief Clear the status flag.
  341. *
  342. * @param base ECSPI base pointer
  343. * @param mask ECSPI Status, use status flag to AND #_ecspi_flags could get the related status.
  344. */
  345. static inline void ECSPI_ClearStatusFlags(ECSPI_Type *base, uint32_t mask)
  346. {
  347. base->STATREG |= mask;
  348. }
  349. /*! @} */
  350. /*!
  351. * @name Interrupts
  352. * @{
  353. */
  354. /*!
  355. * @brief Enables the interrupt for the ECSPI.
  356. *
  357. * @param base ECSPI base pointer
  358. * @param mask ECSPI interrupt source. The parameter can be any combination of the following values:
  359. * @arg kECSPI_TxfifoEmptyInterruptEnable
  360. * @arg kECSPI_TxFifoDataRequstInterruptEnable
  361. * @arg kECSPI_TxFifoFullInterruptEnable
  362. * @arg kECSPI_RxFifoReadyInterruptEnable
  363. * @arg kECSPI_RxFifoDataRequstInterruptEnable
  364. * @arg kECSPI_RxFifoFullInterruptEnable
  365. * @arg kECSPI_RxFifoOverFlowInterruptEnable
  366. * @arg kECSPI_TransferCompleteInterruptEnable
  367. * @arg kECSPI_AllInterruptEnable
  368. */
  369. static inline void ECSPI_EnableInterrupts(ECSPI_Type *base, uint32_t mask)
  370. {
  371. base->INTREG |= mask;
  372. }
  373. /*!
  374. * @brief Disables the interrupt for the ECSPI.
  375. *
  376. * @param base ECSPI base pointer
  377. * @param mask ECSPI interrupt source. The parameter can be any combination of the following values:
  378. * @arg kECSPI_TxfifoEmptyInterruptEnable
  379. * @arg kECSPI_TxFifoDataRequstInterruptEnable
  380. * @arg kECSPI_TxFifoFullInterruptEnable
  381. * @arg kECSPI_RxFifoReadyInterruptEnable
  382. * @arg kECSPI_RxFifoDataRequstInterruptEnable
  383. * @arg kECSPI_RxFifoFullInterruptEnable
  384. * @arg kECSPI_RxFifoOverFlowInterruptEnable
  385. * @arg kECSPI_TransferCompleteInterruptEnable
  386. * @arg kECSPI_AllInterruptEnable
  387. */
  388. static inline void ECSPI_DisableInterrupts(ECSPI_Type *base, uint32_t mask)
  389. {
  390. base->INTREG &= ~(mask);
  391. }
  392. /*! @} */
  393. /*!
  394. * @name Software Reset
  395. * @{
  396. */
  397. /*!
  398. * @brief Software reset.
  399. *
  400. * @param base ECSPI base pointer
  401. */
  402. static inline void ECSPI_SoftwareReset(ECSPI_Type *base)
  403. {
  404. /* Disables the block and resets the internal logic with the exception of the ECSPI control register */
  405. base->CONREG &= ~ECSPI_CONREG_EN_MASK;
  406. /* Software reset can not reset the control register, so clear the control register manually */
  407. base->CONREG = 0x0U;
  408. }
  409. /*! @} */
  410. /*!
  411. * @name Channel mode check
  412. * @{
  413. */
  414. /*!
  415. * @brief Mode check
  416. *
  417. * @param base ECSPI base pointer
  418. * @param channel ECSPI channel source
  419. * @return mode of channel
  420. */
  421. static inline bool ECSPI_IsMaster(ECSPI_Type *base, ecspi_channel_source_t channel)
  422. {
  423. return (bool)(((base->CONREG & ECSPI_CONREG_CHANNEL_MODE_MASK) >> (ECSPI_CONREG_CHANNEL_MODE_SHIFT + channel)) &
  424. 0x1U);
  425. }
  426. /*! @} */
  427. /*!
  428. * @name DMA Control
  429. * @{
  430. */
  431. /*!
  432. * @brief Enables the DMA source for ECSPI.
  433. *
  434. * @param base ECSPI base pointer
  435. * @param source ECSPI DMA source.
  436. * @param enable True means enable DMA, false means disable DMA
  437. */
  438. static inline void ECSPI_EnableDMA(ECSPI_Type *base, uint32_t mask, bool enable)
  439. {
  440. if (enable)
  441. {
  442. base->DMAREG |= mask;
  443. }
  444. else
  445. {
  446. base->DMAREG &= ~mask;
  447. }
  448. }
  449. /*! @} */
  450. /*!
  451. * @name FIFO Operation
  452. * @{
  453. */
  454. /*!
  455. * @brief Get the Tx FIFO data count.
  456. *
  457. * @param base ECSPI base pointer.
  458. * @return the number of words in Tx FIFO buffer.
  459. */
  460. static inline uint8_t ECSPI_GetTxFifoCount(ECSPI_Type *base)
  461. {
  462. return (uint8_t)((base->TESTREG & ECSPI_TESTREG_TXCNT_MASK) >> ECSPI_TESTREG_TXCNT_SHIFT);
  463. }
  464. /*!
  465. * @brief Get the Rx FIFO data count.
  466. *
  467. * @param base ECSPI base pointer.
  468. * @return the number of words in Rx FIFO buffer.
  469. */
  470. static inline uint8_t ECSPI_GetRxFifoCount(ECSPI_Type *base)
  471. {
  472. return (uint8_t)((base->TESTREG & ECSPI_TESTREG_RXCNT_MASK) >> ECSPI_TESTREG_RXCNT_SHIFT);
  473. }
  474. /*! @} */
  475. /*!
  476. * @name Bus Operations
  477. * @{
  478. */
  479. /*!
  480. * @brief Set channel select for transfer.
  481. *
  482. * @param base ECSPI base pointer
  483. * @param channel Channel source.
  484. */
  485. static inline void ECSPI_SetChannelSelect(ECSPI_Type *base, ecspi_channel_source_t channel)
  486. {
  487. /* Clear Channel select bits in CONREG register */
  488. uint32_t temp = base->CONREG & (~(ECSPI_CONREG_CHANNEL_SELECT_MASK));
  489. /* Set channel select bits */
  490. base->CONREG = (temp | ECSPI_CONREG_CHANNEL_SELECT(channel));
  491. }
  492. /*!
  493. * @brief Set channel select configuration for transfer.
  494. *
  495. * The purpose of this API is to set the channel will be use to transfer.
  496. * User may use this API after instance has been initialized or before transfer start.
  497. * The configuration structure #_ecspi_channel_config_ can be filled by user from scratch.
  498. * After calling this API, user can select this channel as transfer channel.
  499. *
  500. * @param base ECSPI base pointer
  501. * @param channel Channel source.
  502. * @param config Configuration struct of channel
  503. */
  504. void ECSPI_SetChannelConfig(ECSPI_Type *base, ecspi_channel_source_t channel, const ecspi_channel_config_t *config);
  505. /*!
  506. * @brief Sets the baud rate for ECSPI transfer. This is only used in master.
  507. *
  508. * @param base ECSPI base pointer
  509. * @param baudRate_Bps baud rate needed in Hz.
  510. * @param srcClock_Hz ECSPI source clock frequency in Hz.
  511. */
  512. void ECSPI_SetBaudRate(ECSPI_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
  513. /*!
  514. * @brief Sends a buffer of data bytes using a blocking method.
  515. *
  516. * @note This function blocks via polling until all bytes have been sent.
  517. *
  518. * @param base ECSPI base pointer
  519. * @param buffer The data bytes to send
  520. * @param size The number of data bytes to send
  521. */
  522. void ECSPI_WriteBlocking(ECSPI_Type *base, uint32_t *buffer, size_t size);
  523. /*!
  524. * @brief Writes a data into the ECSPI data register.
  525. *
  526. * @param base ECSPI base pointer
  527. * @param data Data needs to be write.
  528. */
  529. static inline void ECSPI_WriteData(ECSPI_Type *base, uint32_t data)
  530. {
  531. base->TXDATA = data;
  532. }
  533. /*!
  534. * @brief Gets a data from the ECSPI data register.
  535. *
  536. * @param base ECSPI base pointer
  537. * @return Data in the register.
  538. */
  539. static inline uint32_t ECSPI_ReadData(ECSPI_Type *base)
  540. {
  541. return (uint32_t)(base->RXDATA);
  542. }
  543. /*! @} */
  544. /*!
  545. * @name Transactional
  546. * @{
  547. */
  548. /*!
  549. * @brief Initializes the ECSPI master handle.
  550. *
  551. * This function initializes the ECSPI master handle which can be used for other ECSPI master transactional APIs.
  552. * Usually,
  553. * for a specified ECSPI instance, call this API once to get the initialized handle.
  554. *
  555. * @param base ECSPI peripheral base address.
  556. * @param handle ECSPI handle pointer.
  557. * @param callback Callback function.
  558. * @param userData User data.
  559. */
  560. void ECSPI_MasterTransferCreateHandle(ECSPI_Type *base,
  561. ecspi_master_handle_t *handle,
  562. ecspi_master_callback_t callback,
  563. void *userData);
  564. /*!
  565. * @brief Transfers a block of data using a polling method.
  566. *
  567. * @param base SPI base pointer
  568. * @param xfer pointer to spi_xfer_config_t structure
  569. * @retval kStatus_Success Successfully start a transfer.
  570. * @retval kStatus_InvalidArgument Input argument is invalid.
  571. */
  572. status_t ECSPI_MasterTransferBlocking(ECSPI_Type *base, ecspi_transfer_t *xfer);
  573. /*!
  574. * @brief Performs a non-blocking ECSPI interrupt transfer.
  575. *
  576. * @note The API immediately returns after transfer initialization is finished.
  577. * @note If ECSPI transfer data frame size is 16 bits, the transfer size cannot be an odd number.
  578. *
  579. * @param base ECSPI peripheral base address.
  580. * @param handle pointer to ecspi_master_handle_t structure which stores the transfer state
  581. * @param xfer pointer to ecspi_transfer_t structure
  582. * @retval kStatus_Success Successfully start a transfer.
  583. * @retval kStatus_InvalidArgument Input argument is invalid.
  584. * @retval kStatus_ECSPI_Busy ECSPI is not idle, is running another transfer.
  585. */
  586. status_t ECSPI_MasterTransferNonBlocking(ECSPI_Type *base, ecspi_master_handle_t *handle, ecspi_transfer_t *xfer);
  587. /*!
  588. * @brief Gets the bytes of the ECSPI interrupt transferred.
  589. *
  590. * @param base ECSPI peripheral base address.
  591. * @param handle Pointer to ECSPI transfer handle, this should be a static variable.
  592. * @param count Transferred bytes of ECSPI master.
  593. * @retval kStatus_ECSPI_Success Succeed get the transfer count.
  594. * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
  595. */
  596. status_t ECSPI_MasterTransferGetCount(ECSPI_Type *base, ecspi_master_handle_t *handle, size_t *count);
  597. /*!
  598. * @brief Aborts an ECSPI transfer using interrupt.
  599. *
  600. * @param base ECSPI peripheral base address.
  601. * @param handle Pointer to ECSPI transfer handle, this should be a static variable.
  602. */
  603. void ECSPI_MasterTransferAbort(ECSPI_Type *base, ecspi_master_handle_t *handle);
  604. /*!
  605. * @brief Interrupts the handler for the ECSPI.
  606. *
  607. * @param base ECSPI peripheral base address.
  608. * @param handle pointer to ecspi_master_handle_t structure which stores the transfer state.
  609. */
  610. void ECSPI_MasterTransferHandleIRQ(ECSPI_Type *base, ecspi_master_handle_t *handle);
  611. /*!
  612. * @brief Initializes the ECSPI slave handle.
  613. *
  614. * This function initializes the ECSPI slave handle which can be used for other ECSPI slave transactional APIs. Usually,
  615. * for a specified ECSPI instance, call this API once to get the initialized handle.
  616. *
  617. * @param base ECSPI peripheral base address.
  618. * @param handle ECSPI handle pointer.
  619. * @param callback Callback function.
  620. * @param userData User data.
  621. */
  622. void ECSPI_SlaveTransferCreateHandle(ECSPI_Type *base,
  623. ecspi_slave_handle_t *handle,
  624. ecspi_slave_callback_t callback,
  625. void *userData);
  626. /*!
  627. * @brief Performs a non-blocking ECSPI slave interrupt transfer.
  628. *
  629. * @note The API returns immediately after the transfer initialization is finished.
  630. *
  631. * @param base ECSPI peripheral base address.
  632. * @param handle pointer to ecspi_master_handle_t structure which stores the transfer state
  633. * @param xfer pointer to ecspi_transfer_t structure
  634. * @retval kStatus_Success Successfully start a transfer.
  635. * @retval kStatus_InvalidArgument Input argument is invalid.
  636. * @retval kStatus_ECSPI_Busy ECSPI is not idle, is running another transfer.
  637. */
  638. static inline status_t ECSPI_SlaveTransferNonBlocking(ECSPI_Type *base,
  639. ecspi_slave_handle_t *handle,
  640. ecspi_transfer_t *xfer)
  641. {
  642. return ECSPI_MasterTransferNonBlocking(base, handle, xfer);
  643. }
  644. /*!
  645. * @brief Gets the bytes of the ECSPI interrupt transferred.
  646. *
  647. * @param base ECSPI peripheral base address.
  648. * @param handle Pointer to ECSPI transfer handle, this should be a static variable.
  649. * @param count Transferred bytes of ECSPI slave.
  650. * @retval kStatus_ECSPI_Success Succeed get the transfer count.
  651. * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
  652. */
  653. static inline status_t ECSPI_SlaveTransferGetCount(ECSPI_Type *base, ecspi_slave_handle_t *handle, size_t *count)
  654. {
  655. return ECSPI_MasterTransferGetCount(base, handle, count);
  656. }
  657. /*!
  658. * @brief Aborts an ECSPI slave transfer using interrupt.
  659. *
  660. * @param base ECSPI peripheral base address.
  661. * @param handle Pointer to ECSPI transfer handle, this should be a static variable.
  662. */
  663. static inline void ECSPI_SlaveTransferAbort(ECSPI_Type *base, ecspi_slave_handle_t *handle)
  664. {
  665. ECSPI_MasterTransferAbort(base, handle);
  666. }
  667. /*!
  668. * @brief Interrupts a handler for the ECSPI slave.
  669. *
  670. * @param base ECSPI peripheral base address.
  671. * @param handle pointer to ecspi_slave_handle_t structure which stores the transfer state
  672. */
  673. void ECSPI_SlaveTransferHandleIRQ(ECSPI_Type *base, ecspi_slave_handle_t *handle);
  674. /*! @} */
  675. #if defined(__cplusplus)
  676. }
  677. #endif
  678. /*! @} */
  679. #endif /* _FSL_ECSPI_H_*/