fsl_dspi.h 50 KB

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