fsl_dspi.h 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, 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_DSPI_H_
  35. #define _FSL_DSPI_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup dspi_driver
  39. * @{
  40. */
  41. /**********************************************************************************************************************
  42. * Definitions
  43. *********************************************************************************************************************/
  44. /*! @name Driver version */
  45. /*@{*/
  46. /*! @brief DSPI driver version 2.2.0. */
  47. #define FSL_DSPI_DRIVER_VERSION (MAKE_VERSION(2, 2, 0))
  48. /*@}*/
  49. #ifndef DSPI_DUMMY_DATA
  50. /*! @brief DSPI dummy data if there is no Tx data.*/
  51. #define DSPI_DUMMY_DATA (0x00U) /*!< Dummy data used for Tx if there is no txData. */
  52. #endif
  53. /*! @brief Status for the DSPI driver.*/
  54. enum _dspi_status
  55. {
  56. kStatus_DSPI_Busy = MAKE_STATUS(kStatusGroup_DSPI, 0), /*!< DSPI transfer is busy.*/
  57. kStatus_DSPI_Error = MAKE_STATUS(kStatusGroup_DSPI, 1), /*!< DSPI driver error. */
  58. kStatus_DSPI_Idle = MAKE_STATUS(kStatusGroup_DSPI, 2), /*!< DSPI is idle.*/
  59. kStatus_DSPI_OutOfRange = MAKE_STATUS(kStatusGroup_DSPI, 3) /*!< DSPI transfer out of range. */
  60. };
  61. /*! @brief DSPI status flags in SPIx_SR register.*/
  62. enum _dspi_flags
  63. {
  64. kDSPI_TxCompleteFlag = SPI_SR_TCF_MASK, /*!< Transfer Complete Flag. */
  65. kDSPI_EndOfQueueFlag = SPI_SR_EOQF_MASK, /*!< End of Queue Flag.*/
  66. kDSPI_TxFifoUnderflowFlag = SPI_SR_TFUF_MASK, /*!< Transmit FIFO Underflow Flag.*/
  67. kDSPI_TxFifoFillRequestFlag = SPI_SR_TFFF_MASK, /*!< Transmit FIFO Fill Flag.*/
  68. kDSPI_RxFifoOverflowFlag = SPI_SR_RFOF_MASK, /*!< Receive FIFO Overflow Flag.*/
  69. kDSPI_RxFifoDrainRequestFlag = SPI_SR_RFDF_MASK, /*!< Receive FIFO Drain Flag.*/
  70. kDSPI_TxAndRxStatusFlag = SPI_SR_TXRXS_MASK, /*!< The module is in Stopped/Running state.*/
  71. kDSPI_AllStatusFlag = SPI_SR_TCF_MASK | SPI_SR_EOQF_MASK | SPI_SR_TFUF_MASK | SPI_SR_TFFF_MASK | SPI_SR_RFOF_MASK |
  72. SPI_SR_RFDF_MASK | SPI_SR_TXRXS_MASK /*!< All statuses above.*/
  73. };
  74. /*! @brief DSPI interrupt source.*/
  75. enum _dspi_interrupt_enable
  76. {
  77. kDSPI_TxCompleteInterruptEnable = SPI_RSER_TCF_RE_MASK, /*!< TCF interrupt enable.*/
  78. kDSPI_EndOfQueueInterruptEnable = SPI_RSER_EOQF_RE_MASK, /*!< EOQF interrupt enable.*/
  79. kDSPI_TxFifoUnderflowInterruptEnable = SPI_RSER_TFUF_RE_MASK, /*!< TFUF interrupt enable.*/
  80. kDSPI_TxFifoFillRequestInterruptEnable = SPI_RSER_TFFF_RE_MASK, /*!< TFFF interrupt enable, DMA disable.*/
  81. kDSPI_RxFifoOverflowInterruptEnable = SPI_RSER_RFOF_RE_MASK, /*!< RFOF interrupt enable.*/
  82. kDSPI_RxFifoDrainRequestInterruptEnable = SPI_RSER_RFDF_RE_MASK, /*!< RFDF interrupt enable, DMA disable.*/
  83. kDSPI_AllInterruptEnable = SPI_RSER_TCF_RE_MASK | SPI_RSER_EOQF_RE_MASK | SPI_RSER_TFUF_RE_MASK |
  84. SPI_RSER_TFFF_RE_MASK | SPI_RSER_RFOF_RE_MASK | SPI_RSER_RFDF_RE_MASK
  85. /*!< All above interrupts enable.*/
  86. };
  87. /*! @brief DSPI DMA source.*/
  88. enum _dspi_dma_enable
  89. {
  90. kDSPI_TxDmaEnable = (SPI_RSER_TFFF_RE_MASK | SPI_RSER_TFFF_DIRS_MASK), /*!< TFFF flag generates DMA requests.
  91. No Tx interrupt request. */
  92. kDSPI_RxDmaEnable = (SPI_RSER_RFDF_RE_MASK | SPI_RSER_RFDF_DIRS_MASK) /*!< RFDF flag generates DMA requests.
  93. No Rx interrupt request. */
  94. };
  95. /*! @brief DSPI master or slave mode configuration.*/
  96. typedef enum _dspi_master_slave_mode
  97. {
  98. kDSPI_Master = 1U, /*!< DSPI peripheral operates in master mode.*/
  99. kDSPI_Slave = 0U /*!< DSPI peripheral operates in slave mode.*/
  100. } dspi_master_slave_mode_t;
  101. /*!
  102. * @brief DSPI Sample Point: Controls when the DSPI master samples SIN in the Modified Transfer Format. This field is
  103. * valid
  104. * only when the CPHA bit in the CTAR register is 0.
  105. */
  106. typedef enum _dspi_master_sample_point
  107. {
  108. kDSPI_SckToSin0Clock = 0U, /*!< 0 system clocks between SCK edge and SIN sample.*/
  109. kDSPI_SckToSin1Clock = 1U, /*!< 1 system clock between SCK edge and SIN sample.*/
  110. kDSPI_SckToSin2Clock = 2U /*!< 2 system clocks between SCK edge and SIN sample.*/
  111. } dspi_master_sample_point_t;
  112. /*! @brief DSPI Peripheral Chip Select (Pcs) configuration (which Pcs to configure).*/
  113. typedef enum _dspi_which_pcs_config
  114. {
  115. kDSPI_Pcs0 = 1U << 0, /*!< Pcs[0] */
  116. kDSPI_Pcs1 = 1U << 1, /*!< Pcs[1] */
  117. kDSPI_Pcs2 = 1U << 2, /*!< Pcs[2] */
  118. kDSPI_Pcs3 = 1U << 3, /*!< Pcs[3] */
  119. kDSPI_Pcs4 = 1U << 4, /*!< Pcs[4] */
  120. kDSPI_Pcs5 = 1U << 5 /*!< Pcs[5] */
  121. } dspi_which_pcs_t;
  122. /*! @brief DSPI Peripheral Chip Select (Pcs) Polarity configuration.*/
  123. typedef enum _dspi_pcs_polarity_config
  124. {
  125. kDSPI_PcsActiveHigh = 0U, /*!< Pcs Active High (idles low). */
  126. kDSPI_PcsActiveLow = 1U /*!< Pcs Active Low (idles high). */
  127. } dspi_pcs_polarity_config_t;
  128. /*! @brief DSPI Peripheral Chip Select (Pcs) Polarity.*/
  129. enum _dspi_pcs_polarity
  130. {
  131. kDSPI_Pcs0ActiveLow = 1U << 0, /*!< Pcs0 Active Low (idles high). */
  132. kDSPI_Pcs1ActiveLow = 1U << 1, /*!< Pcs1 Active Low (idles high). */
  133. kDSPI_Pcs2ActiveLow = 1U << 2, /*!< Pcs2 Active Low (idles high). */
  134. kDSPI_Pcs3ActiveLow = 1U << 3, /*!< Pcs3 Active Low (idles high). */
  135. kDSPI_Pcs4ActiveLow = 1U << 4, /*!< Pcs4 Active Low (idles high). */
  136. kDSPI_Pcs5ActiveLow = 1U << 5, /*!< Pcs5 Active Low (idles high). */
  137. kDSPI_PcsAllActiveLow = 0xFFU /*!< Pcs0 to Pcs5 Active Low (idles high). */
  138. };
  139. /*! @brief DSPI clock polarity configuration for a given CTAR.*/
  140. typedef enum _dspi_clock_polarity
  141. {
  142. kDSPI_ClockPolarityActiveHigh = 0U, /*!< CPOL=0. Active-high DSPI clock (idles low).*/
  143. kDSPI_ClockPolarityActiveLow = 1U /*!< CPOL=1. Active-low DSPI clock (idles high).*/
  144. } dspi_clock_polarity_t;
  145. /*! @brief DSPI clock phase configuration for a given CTAR.*/
  146. typedef enum _dspi_clock_phase
  147. {
  148. kDSPI_ClockPhaseFirstEdge = 0U, /*!< CPHA=0. Data is captured on the leading edge of the SCK and changed on the
  149. following edge.*/
  150. kDSPI_ClockPhaseSecondEdge = 1U /*!< CPHA=1. Data is changed on the leading edge of the SCK and captured on the
  151. following edge.*/
  152. } dspi_clock_phase_t;
  153. /*! @brief DSPI data shifter direction options for a given CTAR.*/
  154. typedef enum _dspi_shift_direction
  155. {
  156. kDSPI_MsbFirst = 0U, /*!< Data transfers start with most significant bit.*/
  157. kDSPI_LsbFirst = 1U /*!< Data transfers start with least significant bit.
  158. Shifting out of LSB is not supported for slave */
  159. } dspi_shift_direction_t;
  160. /*! @brief DSPI delay type selection.*/
  161. typedef enum _dspi_delay_type
  162. {
  163. kDSPI_PcsToSck = 1U, /*!< Pcs-to-SCK delay. */
  164. kDSPI_LastSckToPcs, /*!< The last SCK edge to Pcs delay. */
  165. kDSPI_BetweenTransfer /*!< Delay between transfers. */
  166. } dspi_delay_type_t;
  167. /*! @brief DSPI Clock and Transfer Attributes Register (CTAR) selection.*/
  168. typedef enum _dspi_ctar_selection
  169. {
  170. kDSPI_Ctar0 = 0U, /*!< CTAR0 selection option for master or slave mode; note that CTAR0 and CTAR0_SLAVE are the
  171. same register address. */
  172. kDSPI_Ctar1 = 1U, /*!< CTAR1 selection option for master mode only. */
  173. kDSPI_Ctar2 = 2U, /*!< CTAR2 selection option for master mode only; note that some devices do not support CTAR2. */
  174. kDSPI_Ctar3 = 3U, /*!< CTAR3 selection option for master mode only; note that some devices do not support CTAR3. */
  175. kDSPI_Ctar4 = 4U, /*!< CTAR4 selection option for master mode only; note that some devices do not support CTAR4. */
  176. kDSPI_Ctar5 = 5U, /*!< CTAR5 selection option for master mode only; note that some devices do not support CTAR5. */
  177. kDSPI_Ctar6 = 6U, /*!< CTAR6 selection option for master mode only; note that some devices do not support CTAR6. */
  178. kDSPI_Ctar7 = 7U /*!< CTAR7 selection option for master mode only; note that some devices do not support CTAR7. */
  179. } dspi_ctar_selection_t;
  180. #define DSPI_MASTER_CTAR_SHIFT (0U) /*!< DSPI master CTAR shift macro; used internally. */
  181. #define DSPI_MASTER_CTAR_MASK (0x0FU) /*!< DSPI master CTAR mask macro; used internally. */
  182. #define DSPI_MASTER_PCS_SHIFT (4U) /*!< DSPI master PCS shift macro; used internally. */
  183. #define DSPI_MASTER_PCS_MASK (0xF0U) /*!< DSPI master PCS mask macro; used internally. */
  184. /*! @brief Use this enumeration for the DSPI master transfer configFlags. */
  185. enum _dspi_transfer_config_flag_for_master
  186. {
  187. kDSPI_MasterCtar0 = 0U << DSPI_MASTER_CTAR_SHIFT, /*!< DSPI master transfer use CTAR0 setting. */
  188. kDSPI_MasterCtar1 = 1U << DSPI_MASTER_CTAR_SHIFT, /*!< DSPI master transfer use CTAR1 setting. */
  189. kDSPI_MasterCtar2 = 2U << DSPI_MASTER_CTAR_SHIFT, /*!< DSPI master transfer use CTAR2 setting. */
  190. kDSPI_MasterCtar3 = 3U << DSPI_MASTER_CTAR_SHIFT, /*!< DSPI master transfer use CTAR3 setting. */
  191. kDSPI_MasterCtar4 = 4U << DSPI_MASTER_CTAR_SHIFT, /*!< DSPI master transfer use CTAR4 setting. */
  192. kDSPI_MasterCtar5 = 5U << DSPI_MASTER_CTAR_SHIFT, /*!< DSPI master transfer use CTAR5 setting. */
  193. kDSPI_MasterCtar6 = 6U << DSPI_MASTER_CTAR_SHIFT, /*!< DSPI master transfer use CTAR6 setting. */
  194. kDSPI_MasterCtar7 = 7U << DSPI_MASTER_CTAR_SHIFT, /*!< DSPI master transfer use CTAR7 setting. */
  195. kDSPI_MasterPcs0 = 0U << DSPI_MASTER_PCS_SHIFT, /*!< DSPI master transfer use PCS0 signal. */
  196. kDSPI_MasterPcs1 = 1U << DSPI_MASTER_PCS_SHIFT, /*!< DSPI master transfer use PCS1 signal. */
  197. kDSPI_MasterPcs2 = 2U << DSPI_MASTER_PCS_SHIFT, /*!< DSPI master transfer use PCS2 signal.*/
  198. kDSPI_MasterPcs3 = 3U << DSPI_MASTER_PCS_SHIFT, /*!< DSPI master transfer use PCS3 signal. */
  199. kDSPI_MasterPcs4 = 4U << DSPI_MASTER_PCS_SHIFT, /*!< DSPI master transfer use PCS4 signal. */
  200. kDSPI_MasterPcs5 = 5U << DSPI_MASTER_PCS_SHIFT, /*!< DSPI master transfer use PCS5 signal. */
  201. kDSPI_MasterPcsContinuous = 1U << 20, /*!< Indicates whether the PCS signal is continuous. */
  202. kDSPI_MasterActiveAfterTransfer =
  203. 1U << 21, /*!< Indicates whether the PCS signal is active after the last frame transfer.*/
  204. };
  205. #define DSPI_SLAVE_CTAR_SHIFT (0U) /*!< DSPI slave CTAR shift macro; used internally. */
  206. #define DSPI_SLAVE_CTAR_MASK (0x07U) /*!< DSPI slave CTAR mask macro; used internally. */
  207. /*! @brief Use this enumeration for the DSPI slave transfer configFlags. */
  208. enum _dspi_transfer_config_flag_for_slave
  209. {
  210. kDSPI_SlaveCtar0 = 0U << DSPI_SLAVE_CTAR_SHIFT, /*!< DSPI slave transfer use CTAR0 setting. */
  211. /*!< DSPI slave can only use PCS0. */
  212. };
  213. /*! @brief DSPI transfer state, which is used for DSPI transactional API state machine. */
  214. enum _dspi_transfer_state
  215. {
  216. kDSPI_Idle = 0x0U, /*!< Nothing in the transmitter/receiver. */
  217. kDSPI_Busy, /*!< Transfer queue is not finished. */
  218. kDSPI_Error /*!< Transfer error. */
  219. };
  220. /*! @brief DSPI master command date configuration used for the SPIx_PUSHR.*/
  221. typedef struct _dspi_command_data_config
  222. {
  223. bool isPcsContinuous; /*!< Option to enable the continuous assertion of the chip select between transfers.*/
  224. dspi_ctar_selection_t whichCtar; /*!< The desired Clock and Transfer Attributes
  225. Register (CTAR) to use for CTAS.*/
  226. dspi_which_pcs_t whichPcs; /*!< The desired PCS signal to use for the data transfer.*/
  227. bool isEndOfQueue; /*!< Signals that the current transfer is the last in the queue.*/
  228. bool clearTransferCount; /*!< Clears the SPI Transfer Counter (SPI_TCNT) before transmission starts.*/
  229. } dspi_command_data_config_t;
  230. /*! @brief DSPI master ctar configuration structure.*/
  231. typedef struct _dspi_master_ctar_config
  232. {
  233. uint32_t baudRate; /*!< Baud Rate for DSPI. */
  234. uint32_t bitsPerFrame; /*!< Bits per frame, minimum 4, maximum 16.*/
  235. dspi_clock_polarity_t cpol; /*!< Clock polarity. */
  236. dspi_clock_phase_t cpha; /*!< Clock phase. */
  237. dspi_shift_direction_t direction; /*!< MSB or LSB data shift direction. */
  238. uint32_t pcsToSckDelayInNanoSec; /*!< PCS to SCK delay time in nanoseconds; setting to 0 sets the minimum
  239. delay. It also sets the boundary value if out of range.*/
  240. uint32_t lastSckToPcsDelayInNanoSec; /*!< The last SCK to PCS delay time in nanoseconds; setting to 0 sets the
  241. minimum delay. It also sets the boundary value if out of range.*/
  242. uint32_t betweenTransferDelayInNanoSec; /*!< After the SCK delay time in nanoseconds; setting to 0 sets the minimum
  243. delay. It also sets the boundary value if out of range.*/
  244. } dspi_master_ctar_config_t;
  245. /*! @brief DSPI master configuration structure.*/
  246. typedef struct _dspi_master_config
  247. {
  248. dspi_ctar_selection_t whichCtar; /*!< The desired CTAR to use. */
  249. dspi_master_ctar_config_t ctarConfig; /*!< Set the ctarConfig to the desired CTAR. */
  250. dspi_which_pcs_t whichPcs; /*!< The desired Peripheral Chip Select (pcs). */
  251. dspi_pcs_polarity_config_t pcsActiveHighOrLow; /*!< The desired PCS active high or low. */
  252. bool enableContinuousSCK; /*!< CONT_SCKE, continuous SCK enable. Note that the continuous SCK is only
  253. supported for CPHA = 1.*/
  254. bool enableRxFifoOverWrite; /*!< ROOE, receive FIFO overflow overwrite enable. If ROOE = 0, the incoming
  255. data is ignored and the data from the transfer that generated the overflow
  256. is also ignored. If ROOE = 1, the incoming data is shifted to the
  257. shift register. */
  258. bool enableModifiedTimingFormat; /*!< Enables a modified transfer format to be used if true.*/
  259. dspi_master_sample_point_t samplePoint; /*!< Controls when the module master samples SIN in the Modified Transfer
  260. Format. It's valid only when CPHA=0. */
  261. } dspi_master_config_t;
  262. /*! @brief DSPI slave ctar configuration structure.*/
  263. typedef struct _dspi_slave_ctar_config
  264. {
  265. uint32_t bitsPerFrame; /*!< Bits per frame, minimum 4, maximum 16.*/
  266. dspi_clock_polarity_t cpol; /*!< Clock polarity. */
  267. dspi_clock_phase_t cpha; /*!< Clock phase. */
  268. /*!< Slave only supports MSB and does not support LSB.*/
  269. } dspi_slave_ctar_config_t;
  270. /*! @brief DSPI slave configuration structure.*/
  271. typedef struct _dspi_slave_config
  272. {
  273. dspi_ctar_selection_t whichCtar; /*!< The desired CTAR to use. */
  274. dspi_slave_ctar_config_t ctarConfig; /*!< Set the ctarConfig to the desired CTAR. */
  275. bool enableContinuousSCK; /*!< CONT_SCKE, continuous SCK enable. Note that the continuous SCK is only
  276. supported for CPHA = 1.*/
  277. bool enableRxFifoOverWrite; /*!< ROOE, receive FIFO overflow overwrite enable. If ROOE = 0, the incoming
  278. data is ignored and the data from the transfer that generated the overflow
  279. is also ignored. If ROOE = 1, the incoming data is shifted to the
  280. shift register. */
  281. bool enableModifiedTimingFormat; /*!< Enables a modified transfer format to be used if true.*/
  282. dspi_master_sample_point_t samplePoint; /*!< Controls when the module master samples SIN in the Modified Transfer
  283. Format. It's valid only when CPHA=0. */
  284. } dspi_slave_config_t;
  285. /*!
  286. * @brief Forward declaration of the _dspi_master_handle typedefs.
  287. */
  288. typedef struct _dspi_master_handle dspi_master_handle_t;
  289. /*!
  290. * @brief Forward declaration of the _dspi_slave_handle typedefs.
  291. */
  292. typedef struct _dspi_slave_handle dspi_slave_handle_t;
  293. /*!
  294. * @brief Completion callback function pointer type.
  295. *
  296. * @param base DSPI peripheral address.
  297. * @param handle Pointer to the handle for the DSPI master.
  298. * @param status Success or error code describing whether the transfer completed.
  299. * @param userData Arbitrary pointer-dataSized value passed from the application.
  300. */
  301. typedef void (*dspi_master_transfer_callback_t)(SPI_Type *base,
  302. dspi_master_handle_t *handle,
  303. status_t status,
  304. void *userData);
  305. /*!
  306. * @brief Completion callback function pointer type.
  307. *
  308. * @param base DSPI peripheral address.
  309. * @param handle Pointer to the handle for the DSPI slave.
  310. * @param status Success or error code describing whether the transfer completed.
  311. * @param userData Arbitrary pointer-dataSized value passed from the application.
  312. */
  313. typedef void (*dspi_slave_transfer_callback_t)(SPI_Type *base,
  314. dspi_slave_handle_t *handle,
  315. status_t status,
  316. void *userData);
  317. /*! @brief DSPI master/slave transfer structure.*/
  318. typedef struct _dspi_transfer
  319. {
  320. uint8_t *txData; /*!< Send buffer. */
  321. uint8_t *rxData; /*!< Receive buffer. */
  322. volatile size_t dataSize; /*!< Transfer bytes. */
  323. uint32_t
  324. configFlags; /*!< Transfer transfer configuration flags; set from _dspi_transfer_config_flag_for_master if the
  325. transfer is used for master or _dspi_transfer_config_flag_for_slave enumeration if the transfer
  326. is used for slave.*/
  327. } dspi_transfer_t;
  328. /*! @brief DSPI half-duplex(master) transfer structure */
  329. typedef struct _dspi_half_duplex_transfer
  330. {
  331. uint8_t *txData; /*!< Send buffer */
  332. uint8_t *rxData; /*!< Receive buffer */
  333. size_t txDataSize; /*!< Transfer bytes for transmit */
  334. size_t rxDataSize; /*!< Transfer bytes */
  335. uint32_t configFlags; /*!< Transfer configuration flags; set from _dspi_transfer_config_flag_for_master. */
  336. bool isPcsAssertInTransfer; /*!< If Pcs pin keep assert between transmit and receive. true for assert and false for
  337. deassert. */
  338. bool isTransmitFirst; /*!< True for transmit first and false for receive first. */
  339. } dspi_half_duplex_transfer_t;
  340. /*! @brief DSPI master transfer handle structure used for transactional API. */
  341. struct _dspi_master_handle
  342. {
  343. uint32_t bitsPerFrame; /*!< The desired number of bits per frame. */
  344. volatile uint32_t command; /*!< The desired data command. */
  345. volatile uint32_t lastCommand; /*!< The desired last data command. */
  346. uint8_t fifoSize; /*!< FIFO dataSize. */
  347. volatile bool
  348. isPcsActiveAfterTransfer; /*!< Indicates whether the PCS signal is active after the last frame transfer.*/
  349. volatile bool isThereExtraByte; /*!< Indicates whether there are extra bytes.*/
  350. uint8_t *volatile txData; /*!< Send buffer. */
  351. uint8_t *volatile rxData; /*!< Receive buffer. */
  352. volatile size_t remainingSendByteCount; /*!< A number of bytes remaining to send.*/
  353. volatile size_t remainingReceiveByteCount; /*!< A number of bytes remaining to receive.*/
  354. size_t totalByteCount; /*!< A number of transfer bytes*/
  355. volatile uint8_t state; /*!< DSPI transfer state, see _dspi_transfer_state.*/
  356. dspi_master_transfer_callback_t callback; /*!< Completion callback. */
  357. void *userData; /*!< Callback user data. */
  358. };
  359. /*! @brief DSPI slave transfer handle structure used for the transactional API. */
  360. struct _dspi_slave_handle
  361. {
  362. uint32_t bitsPerFrame; /*!< The desired number of bits per frame. */
  363. volatile bool isThereExtraByte; /*!< Indicates whether there are extra bytes.*/
  364. uint8_t *volatile txData; /*!< Send buffer. */
  365. uint8_t *volatile rxData; /*!< Receive buffer. */
  366. volatile size_t remainingSendByteCount; /*!< A number of bytes remaining to send.*/
  367. volatile size_t remainingReceiveByteCount; /*!< A number of bytes remaining to receive.*/
  368. size_t totalByteCount; /*!< A number of transfer bytes*/
  369. volatile uint8_t state; /*!< DSPI transfer state.*/
  370. volatile uint32_t errorCount; /*!< Error count for slave transfer.*/
  371. dspi_slave_transfer_callback_t callback; /*!< Completion callback. */
  372. void *userData; /*!< Callback user data. */
  373. };
  374. /**********************************************************************************************************************
  375. * API
  376. *********************************************************************************************************************/
  377. #if defined(__cplusplus)
  378. extern "C" {
  379. #endif /*_cplusplus*/
  380. /*!
  381. * @name Initialization and deinitialization
  382. * @{
  383. */
  384. /*!
  385. * @brief Initializes the DSPI master.
  386. *
  387. * This function initializes the DSPI master configuration. This is an example use case.
  388. * @code
  389. * dspi_master_config_t masterConfig;
  390. * masterConfig.whichCtar = kDSPI_Ctar0;
  391. * masterConfig.ctarConfig.baudRate = 500000000U;
  392. * masterConfig.ctarConfig.bitsPerFrame = 8;
  393. * masterConfig.ctarConfig.cpol = kDSPI_ClockPolarityActiveHigh;
  394. * masterConfig.ctarConfig.cpha = kDSPI_ClockPhaseFirstEdge;
  395. * masterConfig.ctarConfig.direction = kDSPI_MsbFirst;
  396. * masterConfig.ctarConfig.pcsToSckDelayInNanoSec = 1000000000U / masterConfig.ctarConfig.baudRate ;
  397. * masterConfig.ctarConfig.lastSckToPcsDelayInNanoSec = 1000000000U / masterConfig.ctarConfig.baudRate ;
  398. * masterConfig.ctarConfig.betweenTransferDelayInNanoSec = 1000000000U / masterConfig.ctarConfig.baudRate ;
  399. * masterConfig.whichPcs = kDSPI_Pcs0;
  400. * masterConfig.pcsActiveHighOrLow = kDSPI_PcsActiveLow;
  401. * masterConfig.enableContinuousSCK = false;
  402. * masterConfig.enableRxFifoOverWrite = false;
  403. * masterConfig.enableModifiedTimingFormat = false;
  404. * masterConfig.samplePoint = kDSPI_SckToSin0Clock;
  405. * DSPI_MasterInit(base, &masterConfig, srcClock_Hz);
  406. * @endcode
  407. *
  408. * @param base DSPI peripheral address.
  409. * @param masterConfig Pointer to the structure dspi_master_config_t.
  410. * @param srcClock_Hz Module source input clock in Hertz.
  411. */
  412. void DSPI_MasterInit(SPI_Type *base, const dspi_master_config_t *masterConfig, uint32_t srcClock_Hz);
  413. /*!
  414. * @brief Sets the dspi_master_config_t structure to default values.
  415. *
  416. * The purpose of this API is to get the configuration structure initialized for the DSPI_MasterInit().
  417. * Users may use the initialized structure unchanged in the DSPI_MasterInit() or modify the structure
  418. * before calling the DSPI_MasterInit().
  419. * Example:
  420. * @code
  421. * dspi_master_config_t masterConfig;
  422. * DSPI_MasterGetDefaultConfig(&masterConfig);
  423. * @endcode
  424. * @param masterConfig pointer to dspi_master_config_t structure
  425. */
  426. void DSPI_MasterGetDefaultConfig(dspi_master_config_t *masterConfig);
  427. /*!
  428. * @brief DSPI slave configuration.
  429. *
  430. * This function initializes the DSPI slave configuration. This is an example use case.
  431. * @code
  432. * dspi_slave_config_t slaveConfig;
  433. * slaveConfig->whichCtar = kDSPI_Ctar0;
  434. * slaveConfig->ctarConfig.bitsPerFrame = 8;
  435. * slaveConfig->ctarConfig.cpol = kDSPI_ClockPolarityActiveHigh;
  436. * slaveConfig->ctarConfig.cpha = kDSPI_ClockPhaseFirstEdge;
  437. * slaveConfig->enableContinuousSCK = false;
  438. * slaveConfig->enableRxFifoOverWrite = false;
  439. * slaveConfig->enableModifiedTimingFormat = false;
  440. * slaveConfig->samplePoint = kDSPI_SckToSin0Clock;
  441. * DSPI_SlaveInit(base, &slaveConfig);
  442. * @endcode
  443. *
  444. * @param base DSPI peripheral address.
  445. * @param slaveConfig Pointer to the structure dspi_master_config_t.
  446. */
  447. void DSPI_SlaveInit(SPI_Type *base, const dspi_slave_config_t *slaveConfig);
  448. /*!
  449. * @brief Sets the dspi_slave_config_t structure to a default value.
  450. *
  451. * The purpose of this API is to get the configuration structure initialized for the DSPI_SlaveInit().
  452. * Users may use the initialized structure unchanged in the DSPI_SlaveInit() or modify the structure
  453. * before calling the DSPI_SlaveInit().
  454. * This is an example.
  455. * @code
  456. * dspi_slave_config_t slaveConfig;
  457. * DSPI_SlaveGetDefaultConfig(&slaveConfig);
  458. * @endcode
  459. * @param slaveConfig Pointer to the dspi_slave_config_t structure.
  460. */
  461. void DSPI_SlaveGetDefaultConfig(dspi_slave_config_t *slaveConfig);
  462. /*!
  463. * @brief De-initializes the DSPI peripheral. Call this API to disable the DSPI clock.
  464. * @param base DSPI peripheral address.
  465. */
  466. void DSPI_Deinit(SPI_Type *base);
  467. /*!
  468. * @brief Enables the DSPI peripheral and sets the MCR MDIS to 0.
  469. *
  470. * @param base DSPI peripheral address.
  471. * @param enable Pass true to enable module, false to disable module.
  472. */
  473. static inline void DSPI_Enable(SPI_Type *base, bool enable)
  474. {
  475. if (enable)
  476. {
  477. base->MCR &= ~SPI_MCR_MDIS_MASK;
  478. }
  479. else
  480. {
  481. base->MCR |= SPI_MCR_MDIS_MASK;
  482. }
  483. }
  484. /*!
  485. *@}
  486. */
  487. /*!
  488. * @name Status
  489. * @{
  490. */
  491. /*!
  492. * @brief Gets the DSPI status flag state.
  493. * @param base DSPI peripheral address.
  494. * @return DSPI status (in SR register).
  495. */
  496. static inline uint32_t DSPI_GetStatusFlags(SPI_Type *base)
  497. {
  498. return (base->SR);
  499. }
  500. /*!
  501. * @brief Clears the DSPI status flag.
  502. *
  503. * This function clears the desired status bit by using a write-1-to-clear. The user passes in the base and the
  504. * desired status bit to clear. The list of status bits is defined in the dspi_status_and_interrupt_request_t. The
  505. * function uses these bit positions in its algorithm to clear the desired flag state.
  506. * This is an example.
  507. * @code
  508. * DSPI_ClearStatusFlags(base, kDSPI_TxCompleteFlag|kDSPI_EndOfQueueFlag);
  509. * @endcode
  510. *
  511. * @param base DSPI peripheral address.
  512. * @param statusFlags The status flag used from the type dspi_flags.
  513. */
  514. static inline void DSPI_ClearStatusFlags(SPI_Type *base, uint32_t statusFlags)
  515. {
  516. base->SR = statusFlags; /*!< The status flags are cleared by writing 1 (w1c).*/
  517. }
  518. /*!
  519. *@}
  520. */
  521. /*!
  522. * @name Interrupts
  523. * @{
  524. */
  525. /*!
  526. * @brief Enables the DSPI interrupts.
  527. *
  528. * This function configures the various interrupt masks of the DSPI. The parameters are a base and an interrupt mask.
  529. * Note, for Tx Fill and Rx FIFO drain requests, enable the interrupt request and disable the DMA request.
  530. * Do not use this API(write to RSER register) while DSPI is in running state.
  531. *
  532. * @code
  533. * DSPI_EnableInterrupts(base, kDSPI_TxCompleteInterruptEnable | kDSPI_EndOfQueueInterruptEnable );
  534. * @endcode
  535. *
  536. * @param base DSPI peripheral address.
  537. * @param mask The interrupt mask; use the enum _dspi_interrupt_enable.
  538. */
  539. void DSPI_EnableInterrupts(SPI_Type *base, uint32_t mask);
  540. /*!
  541. * @brief Disables the DSPI interrupts.
  542. *
  543. * @code
  544. * DSPI_DisableInterrupts(base, kDSPI_TxCompleteInterruptEnable | kDSPI_EndOfQueueInterruptEnable );
  545. * @endcode
  546. *
  547. * @param base DSPI peripheral address.
  548. * @param mask The interrupt mask; use the enum _dspi_interrupt_enable.
  549. */
  550. static inline void DSPI_DisableInterrupts(SPI_Type *base, uint32_t mask)
  551. {
  552. base->RSER &= ~mask;
  553. }
  554. /*!
  555. *@}
  556. */
  557. /*!
  558. * @name DMA Control
  559. * @{
  560. */
  561. /*!
  562. * @brief Enables the DSPI DMA request.
  563. *
  564. * This function configures the Rx and Tx DMA mask of the DSPI. The parameters are a base and a DMA mask.
  565. * @code
  566. * DSPI_EnableDMA(base, kDSPI_TxDmaEnable | kDSPI_RxDmaEnable);
  567. * @endcode
  568. *
  569. * @param base DSPI peripheral address.
  570. * @param mask The interrupt mask; use the enum dspi_dma_enable.
  571. */
  572. static inline void DSPI_EnableDMA(SPI_Type *base, uint32_t mask)
  573. {
  574. base->RSER |= mask;
  575. }
  576. /*!
  577. * @brief Disables the DSPI DMA request.
  578. *
  579. * This function configures the Rx and Tx DMA mask of the DSPI. The parameters are a base and a DMA mask.
  580. * @code
  581. * SPI_DisableDMA(base, kDSPI_TxDmaEnable | kDSPI_RxDmaEnable);
  582. * @endcode
  583. *
  584. * @param base DSPI peripheral address.
  585. * @param mask The interrupt mask; use the enum dspi_dma_enable.
  586. */
  587. static inline void DSPI_DisableDMA(SPI_Type *base, uint32_t mask)
  588. {
  589. base->RSER &= ~mask;
  590. }
  591. /*!
  592. * @brief Gets the DSPI master PUSHR data register address for the DMA operation.
  593. *
  594. * This function gets the DSPI master PUSHR data register address because this value is needed for the DMA operation.
  595. *
  596. * @param base DSPI peripheral address.
  597. * @return The DSPI master PUSHR data register address.
  598. */
  599. static inline uint32_t DSPI_MasterGetTxRegisterAddress(SPI_Type *base)
  600. {
  601. return (uint32_t) & (base->PUSHR);
  602. }
  603. /*!
  604. * @brief Gets the DSPI slave PUSHR data register address for the DMA operation.
  605. *
  606. * This function gets the DSPI slave PUSHR data register address as this value is needed for the DMA operation.
  607. *
  608. * @param base DSPI peripheral address.
  609. * @return The DSPI slave PUSHR data register address.
  610. */
  611. static inline uint32_t DSPI_SlaveGetTxRegisterAddress(SPI_Type *base)
  612. {
  613. return (uint32_t) & (base->PUSHR_SLAVE);
  614. }
  615. /*!
  616. * @brief Gets the DSPI POPR data register address for the DMA operation.
  617. *
  618. * This function gets the DSPI POPR data register address as this value is needed for the DMA operation.
  619. *
  620. * @param base DSPI peripheral address.
  621. * @return The DSPI POPR data register address.
  622. */
  623. static inline uint32_t DSPI_GetRxRegisterAddress(SPI_Type *base)
  624. {
  625. return (uint32_t) & (base->POPR);
  626. }
  627. /*!
  628. *@}
  629. */
  630. /*!
  631. * @name Bus Operations
  632. * @{
  633. */
  634. /*!
  635. * @brief Configures the DSPI for master or slave.
  636. *
  637. * @param base DSPI peripheral address.
  638. * @param mode Mode setting (master or slave) of type dspi_master_slave_mode_t.
  639. */
  640. static inline void DSPI_SetMasterSlaveMode(SPI_Type *base, dspi_master_slave_mode_t mode)
  641. {
  642. base->MCR = (base->MCR & (~SPI_MCR_MSTR_MASK)) | SPI_MCR_MSTR(mode);
  643. }
  644. /*!
  645. * @brief Returns whether the DSPI module is in master mode.
  646. *
  647. * @param base DSPI peripheral address.
  648. * @return Returns true if the module is in master mode or false if the module is in slave mode.
  649. */
  650. static inline bool DSPI_IsMaster(SPI_Type *base)
  651. {
  652. return (bool)((base->MCR) & SPI_MCR_MSTR_MASK);
  653. }
  654. /*!
  655. * @brief Starts the DSPI transfers and clears HALT bit in MCR.
  656. *
  657. * This function sets the module to start data transfer in either master or slave mode.
  658. *
  659. * @param base DSPI peripheral address.
  660. */
  661. static inline void DSPI_StartTransfer(SPI_Type *base)
  662. {
  663. base->MCR &= ~SPI_MCR_HALT_MASK;
  664. }
  665. /*!
  666. * @brief Stops DSPI transfers and sets the HALT bit in MCR.
  667. *
  668. * This function stops data transfers in either master or slave modes.
  669. *
  670. * @param base DSPI peripheral address.
  671. */
  672. static inline void DSPI_StopTransfer(SPI_Type *base)
  673. {
  674. base->MCR |= SPI_MCR_HALT_MASK;
  675. }
  676. /*!
  677. * @brief Enables or disables the DSPI FIFOs.
  678. *
  679. * This function allows the caller to disable/enable the Tx and Rx FIFOs independently.
  680. * Note that to disable, pass in a logic 0 (false) for the particular FIFO configuration. To enable,
  681. * pass in a logic 1 (true).
  682. *
  683. * @param base DSPI peripheral address.
  684. * @param enableTxFifo Disables (false) the TX FIFO; Otherwise, enables (true) the TX FIFO
  685. * @param enableRxFifo Disables (false) the RX FIFO; Otherwise, enables (true) the RX FIFO
  686. */
  687. static inline void DSPI_SetFifoEnable(SPI_Type *base, bool enableTxFifo, bool enableRxFifo)
  688. {
  689. base->MCR = (base->MCR & (~(SPI_MCR_DIS_RXF_MASK | SPI_MCR_DIS_TXF_MASK))) | SPI_MCR_DIS_TXF(!enableTxFifo) |
  690. SPI_MCR_DIS_RXF(!enableRxFifo);
  691. }
  692. /*!
  693. * @brief Flushes the DSPI FIFOs.
  694. *
  695. * @param base DSPI peripheral address.
  696. * @param flushTxFifo Flushes (true) the Tx FIFO; Otherwise, does not flush (false) the Tx FIFO
  697. * @param flushRxFifo Flushes (true) the Rx FIFO; Otherwise, does not flush (false) the Rx FIFO
  698. */
  699. static inline void DSPI_FlushFifo(SPI_Type *base, bool flushTxFifo, bool flushRxFifo)
  700. {
  701. base->MCR = (base->MCR & (~(SPI_MCR_CLR_TXF_MASK | SPI_MCR_CLR_RXF_MASK))) | SPI_MCR_CLR_TXF(flushTxFifo) |
  702. SPI_MCR_CLR_RXF(flushRxFifo);
  703. }
  704. /*!
  705. * @brief Configures the DSPI peripheral chip select polarity simultaneously.
  706. * For example, PCS0 and PCS1 are set to active low and other PCS is set to active high. Note that the number of
  707. * PCSs is specific to the device.
  708. * @code
  709. * DSPI_SetAllPcsPolarity(base, kDSPI_Pcs0ActiveLow | kDSPI_Pcs1ActiveLow);
  710. @endcode
  711. * @param base DSPI peripheral address.
  712. * @param mask The PCS polarity mask; use the enum _dspi_pcs_polarity.
  713. */
  714. static inline void DSPI_SetAllPcsPolarity(SPI_Type *base, uint32_t mask)
  715. {
  716. base->MCR = (base->MCR & ~SPI_MCR_PCSIS_MASK) | SPI_MCR_PCSIS(mask);
  717. }
  718. /*!
  719. * @brief Sets the DSPI baud rate in bits per second.
  720. *
  721. * This function takes in the desired baudRate_Bps (baud rate) and calculates the nearest possible baud rate without
  722. * exceeding the desired baud rate, and returns the calculated baud rate in bits-per-second. It requires that the
  723. * caller also provide the frequency of the module source clock (in Hertz).
  724. *
  725. * @param base DSPI peripheral address.
  726. * @param whichCtar The desired Clock and Transfer Attributes Register (CTAR) of the type dspi_ctar_selection_t
  727. * @param baudRate_Bps The desired baud rate in bits per second
  728. * @param srcClock_Hz Module source input clock in Hertz
  729. * @return The actual calculated baud rate
  730. */
  731. uint32_t DSPI_MasterSetBaudRate(SPI_Type *base,
  732. dspi_ctar_selection_t whichCtar,
  733. uint32_t baudRate_Bps,
  734. uint32_t srcClock_Hz);
  735. /*!
  736. * @brief Manually configures the delay prescaler and scaler for a particular CTAR.
  737. *
  738. * This function configures the PCS to SCK delay pre-scalar (PcsSCK) and scalar (CSSCK), after SCK delay pre-scalar
  739. * (PASC) and scalar (ASC), and the delay after transfer pre-scalar (PDT) and scalar (DT).
  740. *
  741. * These delay names are available in the type dspi_delay_type_t.
  742. *
  743. * The user passes the delay to the configuration along with the prescaler and scaler value.
  744. * This allows the user to directly set the prescaler/scaler values if pre-calculated or
  745. * to manually increment either value.
  746. *
  747. * @param base DSPI peripheral address.
  748. * @param whichCtar The desired Clock and Transfer Attributes Register (CTAR) of type dspi_ctar_selection_t.
  749. * @param prescaler The prescaler delay value (can be an integer 0, 1, 2, or 3).
  750. * @param scaler The scaler delay value (can be any integer between 0 to 15).
  751. * @param whichDelay The desired delay to configure; must be of type dspi_delay_type_t
  752. */
  753. void DSPI_MasterSetDelayScaler(
  754. SPI_Type *base, dspi_ctar_selection_t whichCtar, uint32_t prescaler, uint32_t scaler, dspi_delay_type_t whichDelay);
  755. /*!
  756. * @brief Calculates the delay prescaler and scaler based on the desired delay input in nanoseconds.
  757. *
  758. * This function calculates the values for the following.
  759. * PCS to SCK delay pre-scalar (PCSSCK) and scalar (CSSCK), or
  760. * After SCK delay pre-scalar (PASC) and scalar (ASC), or
  761. * Delay after transfer pre-scalar (PDT) and scalar (DT).
  762. *
  763. * These delay names are available in the type dspi_delay_type_t.
  764. *
  765. * The user passes which delay to configure along with the desired delay value in nanoseconds. The function
  766. * calculates the values needed for the prescaler and scaler. Note that returning the calculated delay as an exact
  767. * delay match may not be possible. In this case, the closest match is calculated without going below the desired
  768. * delay value input.
  769. * It is possible to input a very large delay value that exceeds the capability of the part, in which case the maximum
  770. * supported delay is returned. The higher-level peripheral driver alerts the user of an out of range delay
  771. * input.
  772. *
  773. * @param base DSPI peripheral address.
  774. * @param whichCtar The desired Clock and Transfer Attributes Register (CTAR) of type dspi_ctar_selection_t.
  775. * @param whichDelay The desired delay to configure, must be of type dspi_delay_type_t
  776. * @param srcClock_Hz Module source input clock in Hertz
  777. * @param delayTimeInNanoSec The desired delay value in nanoseconds.
  778. * @return The actual calculated delay value.
  779. */
  780. uint32_t DSPI_MasterSetDelayTimes(SPI_Type *base,
  781. dspi_ctar_selection_t whichCtar,
  782. dspi_delay_type_t whichDelay,
  783. uint32_t srcClock_Hz,
  784. uint32_t delayTimeInNanoSec);
  785. /*!
  786. * @brief Writes data into the data buffer for master mode.
  787. *
  788. * In master mode, the 16-bit data is appended to the 16-bit command info. The command portion
  789. * provides characteristics of the data, such as the optional continuous chip select
  790. * operation between transfers, the desired Clock and Transfer Attributes register to use for the
  791. * associated SPI frame, the desired PCS signal to use for the data transfer, whether the current
  792. * transfer is the last in the queue, and whether to clear the transfer count (normally needed when
  793. * sending the first frame of a data packet). This is an example.
  794. * @code
  795. * dspi_command_data_config_t commandConfig;
  796. * commandConfig.isPcsContinuous = true;
  797. * commandConfig.whichCtar = kDSPICtar0;
  798. * commandConfig.whichPcs = kDSPIPcs0;
  799. * commandConfig.clearTransferCount = false;
  800. * commandConfig.isEndOfQueue = false;
  801. * DSPI_MasterWriteData(base, &commandConfig, dataWord);
  802. @endcode
  803. *
  804. * @param base DSPI peripheral address.
  805. * @param command Pointer to the command structure.
  806. * @param data The data word to be sent.
  807. */
  808. static inline void DSPI_MasterWriteData(SPI_Type *base, dspi_command_data_config_t *command, uint16_t data)
  809. {
  810. base->PUSHR = SPI_PUSHR_CONT(command->isPcsContinuous) | SPI_PUSHR_CTAS(command->whichCtar) |
  811. SPI_PUSHR_PCS(command->whichPcs) | SPI_PUSHR_EOQ(command->isEndOfQueue) |
  812. SPI_PUSHR_CTCNT(command->clearTransferCount) | SPI_PUSHR_TXDATA(data);
  813. }
  814. /*!
  815. * @brief Sets the dspi_command_data_config_t structure to default values.
  816. *
  817. * The purpose of this API is to get the configuration structure initialized for use in the DSPI_MasterWrite_xx().
  818. * Users may use the initialized structure unchanged in the DSPI_MasterWrite_xx() or modify the structure
  819. * before calling the DSPI_MasterWrite_xx().
  820. * This is an example.
  821. * @code
  822. * dspi_command_data_config_t command;
  823. * DSPI_GetDefaultDataCommandConfig(&command);
  824. * @endcode
  825. * @param command Pointer to the dspi_command_data_config_t structure.
  826. */
  827. void DSPI_GetDefaultDataCommandConfig(dspi_command_data_config_t *command);
  828. /*!
  829. * @brief Writes data into the data buffer master mode and waits till complete to return.
  830. *
  831. * In master mode, the 16-bit data is appended to the 16-bit command info. The command portion
  832. * provides characteristics of the data, such as the optional continuous chip select
  833. * operation between transfers, the desired Clock and Transfer Attributes register to use for the
  834. * associated SPI frame, the desired PCS signal to use for the data transfer, whether the current
  835. * transfer is the last in the queue, and whether to clear the transfer count (normally needed when
  836. * sending the first frame of a data packet). This is an example.
  837. * @code
  838. * dspi_command_config_t commandConfig;
  839. * commandConfig.isPcsContinuous = true;
  840. * commandConfig.whichCtar = kDSPICtar0;
  841. * commandConfig.whichPcs = kDSPIPcs1;
  842. * commandConfig.clearTransferCount = false;
  843. * commandConfig.isEndOfQueue = false;
  844. * DSPI_MasterWriteDataBlocking(base, &commandConfig, dataWord);
  845. * @endcode
  846. *
  847. * Note that this function does not return until after the transmit is complete. Also note that the DSPI must be
  848. * enabled and running to transmit data (MCR[MDIS] & [HALT] = 0). Because the SPI is a synchronous protocol,
  849. * the received data is available when the transmit completes.
  850. *
  851. * @param base DSPI peripheral address.
  852. * @param command Pointer to the command structure.
  853. * @param data The data word to be sent.
  854. */
  855. void DSPI_MasterWriteDataBlocking(SPI_Type *base, dspi_command_data_config_t *command, uint16_t data);
  856. /*!
  857. * @brief Returns the DSPI command word formatted to the PUSHR data register bit field.
  858. *
  859. * This function allows the caller to pass in the data command structure and returns the command word formatted
  860. * according to the DSPI PUSHR register bit field placement. The user can then "OR" the returned command word with the
  861. * desired data to send and use the function DSPI_HAL_WriteCommandDataMastermode or
  862. * DSPI_HAL_WriteCommandDataMastermodeBlocking to write the entire 32-bit command data word to the PUSHR. This helps
  863. * improve performance in cases where the command structure is constant. For example, the user calls this function
  864. * before starting a transfer to generate the command word. When they are ready to transmit the data, they OR
  865. * this formatted command word with the desired data to transmit. This process increases transmit performance when
  866. * compared to calling send functions, such as DSPI_HAL_WriteDataMastermode, which format the command word each time a
  867. * data word is to be sent.
  868. *
  869. * @param command Pointer to the command structure.
  870. * @return The command word formatted to the PUSHR data register bit field.
  871. */
  872. static inline uint32_t DSPI_MasterGetFormattedCommand(dspi_command_data_config_t *command)
  873. {
  874. /* Format the 16-bit command word according to the PUSHR data register bit field*/
  875. return (uint32_t)(SPI_PUSHR_CONT(command->isPcsContinuous) | SPI_PUSHR_CTAS(command->whichCtar) |
  876. SPI_PUSHR_PCS(command->whichPcs) | SPI_PUSHR_EOQ(command->isEndOfQueue) |
  877. SPI_PUSHR_CTCNT(command->clearTransferCount));
  878. }
  879. /*!
  880. * @brief Writes a 32-bit data word (16-bit command appended with 16-bit data) into the data
  881. * buffer master mode and waits till complete to return.
  882. *
  883. * In this function, the user must append the 16-bit data to the 16-bit command information and then provide the total
  884. * 32-bit word
  885. * as the data to send.
  886. * The command portion provides characteristics of the data, such as the optional continuous chip select operation
  887. * between transfers, the desired Clock and Transfer Attributes register to use for the associated SPI frame, the
  888. * desired PCS
  889. * signal to use for the data transfer, whether the current transfer is the last in the queue, and whether to clear the
  890. * transfer count (normally needed when sending the first frame of a data packet). The user is responsible for
  891. * appending this command with the data to send. This is an example:
  892. * @code
  893. * dataWord = <16-bit command> | <16-bit data>;
  894. * DSPI_MasterWriteCommandDataBlocking(base, dataWord);
  895. * @endcode
  896. *
  897. * Note that this function does not return until after the transmit is complete. Also note that the DSPI must be
  898. * enabled and running to transmit data (MCR[MDIS] & [HALT] = 0).
  899. * Because the SPI is a synchronous protocol, the received data is available when the transmit completes.
  900. *
  901. * For a blocking polling transfer, see methods below.
  902. * Option 1:
  903. * uint32_t command_to_send = DSPI_MasterGetFormattedCommand(&command);
  904. * uint32_t data0 = command_to_send | data_need_to_send_0;
  905. * uint32_t data1 = command_to_send | data_need_to_send_1;
  906. * uint32_t data2 = command_to_send | data_need_to_send_2;
  907. *
  908. * DSPI_MasterWriteCommandDataBlocking(base,data0);
  909. * DSPI_MasterWriteCommandDataBlocking(base,data1);
  910. * DSPI_MasterWriteCommandDataBlocking(base,data2);
  911. *
  912. * Option 2:
  913. * DSPI_MasterWriteDataBlocking(base,&command,data_need_to_send_0);
  914. * DSPI_MasterWriteDataBlocking(base,&command,data_need_to_send_1);
  915. * DSPI_MasterWriteDataBlocking(base,&command,data_need_to_send_2);
  916. *
  917. * @param base DSPI peripheral address.
  918. * @param data The data word (command and data combined) to be sent.
  919. */
  920. void DSPI_MasterWriteCommandDataBlocking(SPI_Type *base, uint32_t data);
  921. /*!
  922. * @brief Writes data into the data buffer in slave mode.
  923. *
  924. * In slave mode, up to 16-bit words may be written.
  925. *
  926. * @param base DSPI peripheral address.
  927. * @param data The data to send.
  928. */
  929. static inline void DSPI_SlaveWriteData(SPI_Type *base, uint32_t data)
  930. {
  931. base->PUSHR_SLAVE = data;
  932. }
  933. /*!
  934. * @brief Writes data into the data buffer in slave mode, waits till data was transmitted, and returns.
  935. *
  936. * In slave mode, up to 16-bit words may be written. The function first clears the transmit complete flag, writes data
  937. * into data register, and finally waits until the data is transmitted.
  938. *
  939. * @param base DSPI peripheral address.
  940. * @param data The data to send.
  941. */
  942. void DSPI_SlaveWriteDataBlocking(SPI_Type *base, uint32_t data);
  943. /*!
  944. * @brief Reads data from the data buffer.
  945. *
  946. * @param base DSPI peripheral address.
  947. * @return The data from the read data buffer.
  948. */
  949. static inline uint32_t DSPI_ReadData(SPI_Type *base)
  950. {
  951. return (base->POPR);
  952. }
  953. /*!
  954. * @brief Set up the dummy data.
  955. *
  956. * @param base DSPI peripheral address.
  957. * @param dummyData Data to be transferred when tx buffer is NULL.
  958. */
  959. void DSPI_SetDummyData(SPI_Type *base, uint8_t dummyData);
  960. /*!
  961. *@}
  962. */
  963. /*!
  964. * @name Transactional
  965. * @{
  966. */
  967. /*Transactional APIs*/
  968. /*!
  969. * @brief Initializes the DSPI master handle.
  970. *
  971. * This function initializes the DSPI handle, which can be used for other DSPI transactional APIs. Usually, for a
  972. * specified DSPI instance, call this API once to get the initialized handle.
  973. *
  974. * @param base DSPI peripheral base address.
  975. * @param handle DSPI handle pointer to dspi_master_handle_t.
  976. * @param callback DSPI callback.
  977. * @param userData Callback function parameter.
  978. */
  979. void DSPI_MasterTransferCreateHandle(SPI_Type *base,
  980. dspi_master_handle_t *handle,
  981. dspi_master_transfer_callback_t callback,
  982. void *userData);
  983. /*!
  984. * @brief DSPI master transfer data using polling.
  985. *
  986. * This function transfers data using polling. This is a blocking function, which does not return until all transfers
  987. * have been completed.
  988. *
  989. * @param base DSPI peripheral base address.
  990. * @param transfer Pointer to the dspi_transfer_t structure.
  991. * @return status of status_t.
  992. */
  993. status_t DSPI_MasterTransferBlocking(SPI_Type *base, dspi_transfer_t *transfer);
  994. /*!
  995. * @brief DSPI master transfer data using interrupts.
  996. *
  997. * This function transfers data using interrupts. This is a non-blocking function, which returns right away. When all
  998. * data is transferred, the callback function is called.
  999. * @param base DSPI peripheral base address.
  1000. * @param handle Pointer to the dspi_master_handle_t structure which stores the transfer state.
  1001. * @param transfer Pointer to the dspi_transfer_t structure.
  1002. * @return status of status_t.
  1003. */
  1004. status_t DSPI_MasterTransferNonBlocking(SPI_Type *base, dspi_master_handle_t *handle, dspi_transfer_t *transfer);
  1005. /*!
  1006. * @brief Transfers a block of data using a polling method.
  1007. *
  1008. * This function will do a half-duplex transfer for DSPI master, This is a blocking function,
  1009. * which does not retuen until all transfer have been completed. And data transfer will be half-duplex,
  1010. * users can set transmit first or receive first.
  1011. *
  1012. * @param base DSPI base pointer
  1013. * @param xfer pointer to dspi_half_duplex_transfer_t structure
  1014. * @return status of status_t.
  1015. */
  1016. status_t DSPI_MasterHalfDuplexTransferBlocking(SPI_Type *base, dspi_half_duplex_transfer_t *xfer);
  1017. /*!
  1018. * @brief Performs a non-blocking DSPI interrupt transfer.
  1019. *
  1020. * This function transfers data using interrupts, the transfer mechanism is half-duplex. This is a non-blocking
  1021. * function,
  1022. * which returns right away. When all data is transferred, the callback function is called.
  1023. *
  1024. * @param base DSPI peripheral base address.
  1025. * @param handle pointer to dspi_master_handle_t structure which stores the transfer state
  1026. * @param xfer pointer to dspi_half_duplex_transfer_t structure
  1027. * @return status of status_t.
  1028. */
  1029. status_t DSPI_MasterHalfDuplexTransferNonBlocking(SPI_Type *base,
  1030. dspi_master_handle_t *handle,
  1031. dspi_half_duplex_transfer_t *xfer);
  1032. /*!
  1033. * @brief Gets the master transfer count.
  1034. *
  1035. * This function gets the master transfer count.
  1036. *
  1037. * @param base DSPI peripheral base address.
  1038. * @param handle Pointer to the dspi_master_handle_t structure which stores the transfer state.
  1039. * @param count The number of bytes transferred by using the non-blocking transaction.
  1040. * @return status of status_t.
  1041. */
  1042. status_t DSPI_MasterTransferGetCount(SPI_Type *base, dspi_master_handle_t *handle, size_t *count);
  1043. /*!
  1044. * @brief DSPI master aborts a transfer using an interrupt.
  1045. *
  1046. * This function aborts a transfer using an interrupt.
  1047. *
  1048. * @param base DSPI peripheral base address.
  1049. * @param handle Pointer to the dspi_master_handle_t structure which stores the transfer state.
  1050. */
  1051. void DSPI_MasterTransferAbort(SPI_Type *base, dspi_master_handle_t *handle);
  1052. /*!
  1053. * @brief DSPI Master IRQ handler function.
  1054. *
  1055. * This function processes the DSPI transmit and receive IRQ.
  1056. * @param base DSPI peripheral base address.
  1057. * @param handle Pointer to the dspi_master_handle_t structure which stores the transfer state.
  1058. */
  1059. void DSPI_MasterTransferHandleIRQ(SPI_Type *base, dspi_master_handle_t *handle);
  1060. /*!
  1061. * @brief Initializes the DSPI slave handle.
  1062. *
  1063. * This function initializes the DSPI handle, which can be used for other DSPI transactional APIs. Usually, for a
  1064. * specified DSPI instance, call this API once to get the initialized handle.
  1065. *
  1066. * @param handle DSPI handle pointer to the dspi_slave_handle_t.
  1067. * @param base DSPI peripheral base address.
  1068. * @param callback DSPI callback.
  1069. * @param userData Callback function parameter.
  1070. */
  1071. void DSPI_SlaveTransferCreateHandle(SPI_Type *base,
  1072. dspi_slave_handle_t *handle,
  1073. dspi_slave_transfer_callback_t callback,
  1074. void *userData);
  1075. /*!
  1076. * @brief DSPI slave transfers data using an interrupt.
  1077. *
  1078. * This function transfers data using an interrupt. This is a non-blocking function, which returns right away. When all
  1079. * data is transferred, the callback function is called.
  1080. *
  1081. * @param base DSPI peripheral base address.
  1082. * @param handle Pointer to the dspi_slave_handle_t structure which stores the transfer state.
  1083. * @param transfer Pointer to the dspi_transfer_t structure.
  1084. * @return status of status_t.
  1085. */
  1086. status_t DSPI_SlaveTransferNonBlocking(SPI_Type *base, dspi_slave_handle_t *handle, dspi_transfer_t *transfer);
  1087. /*!
  1088. * @brief Gets the slave transfer count.
  1089. *
  1090. * This function gets the slave transfer count.
  1091. *
  1092. * @param base DSPI peripheral base address.
  1093. * @param handle Pointer to the dspi_master_handle_t structure which stores the transfer state.
  1094. * @param count The number of bytes transferred by using the non-blocking transaction.
  1095. * @return status of status_t.
  1096. */
  1097. status_t DSPI_SlaveTransferGetCount(SPI_Type *base, dspi_slave_handle_t *handle, size_t *count);
  1098. /*!
  1099. * @brief DSPI slave aborts a transfer using an interrupt.
  1100. *
  1101. * This function aborts a transfer using an interrupt.
  1102. *
  1103. * @param base DSPI peripheral base address.
  1104. * @param handle Pointer to the dspi_slave_handle_t structure which stores the transfer state.
  1105. */
  1106. void DSPI_SlaveTransferAbort(SPI_Type *base, dspi_slave_handle_t *handle);
  1107. /*!
  1108. * @brief DSPI Master IRQ handler function.
  1109. *
  1110. * This function processes the DSPI transmit and receive IRQ.
  1111. *
  1112. * @param base DSPI peripheral base address.
  1113. * @param handle Pointer to the dspi_slave_handle_t structure which stores the transfer state.
  1114. */
  1115. void DSPI_SlaveTransferHandleIRQ(SPI_Type *base, dspi_slave_handle_t *handle);
  1116. /*!
  1117. *@}
  1118. */
  1119. #if defined(__cplusplus)
  1120. }
  1121. #endif /*_cplusplus*/
  1122. /*!
  1123. *@}
  1124. */
  1125. #endif /*_FSL_DSPI_H_*/