Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

1238 rader
52 KiB

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