fsl_dspi.h 53 KB

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