fsl_lpspi.h 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2020,2021 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_LPSPI_H_
  9. #define _FSL_LPSPI_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup lpspi_driver
  13. * @{
  14. */
  15. /**********************************************************************************************************************
  16. * Definitions
  17. *********************************************************************************************************************/
  18. /*! @name Driver version */
  19. /*@{*/
  20. /*! @brief LPSPI driver version. */
  21. #define FSL_LPSPI_DRIVER_VERSION (MAKE_VERSION(2, 2, 1))
  22. /*@}*/
  23. #ifndef LPSPI_DUMMY_DATA
  24. /*! @brief LPSPI dummy data if no Tx data.*/
  25. #define LPSPI_DUMMY_DATA (0x00U) /*!< Dummy data used for tx if there is not txData. */
  26. #endif
  27. /*! @brief Retry times for waiting flag. */
  28. #ifndef SPI_RETRY_TIMES
  29. #define SPI_RETRY_TIMES 0U /* Define to zero means keep waiting until the flag is assert/deassert. */
  30. #endif
  31. /*! @brief Global variable for dummy data value setting. */
  32. extern volatile uint8_t g_lpspiDummyData[];
  33. /*! @brief Status for the LPSPI driver.*/
  34. enum
  35. {
  36. kStatus_LPSPI_Busy = MAKE_STATUS(kStatusGroup_LPSPI, 0), /*!< LPSPI transfer is busy.*/
  37. kStatus_LPSPI_Error = MAKE_STATUS(kStatusGroup_LPSPI, 1), /*!< LPSPI driver error. */
  38. kStatus_LPSPI_Idle = MAKE_STATUS(kStatusGroup_LPSPI, 2), /*!< LPSPI is idle.*/
  39. kStatus_LPSPI_OutOfRange = MAKE_STATUS(kStatusGroup_LPSPI, 3), /*!< LPSPI transfer out Of range. */
  40. kStatus_LPSPI_Timeout = MAKE_STATUS(kStatusGroup_LPSPI, 4) /*!< LPSPI timeout polling status flags. */
  41. };
  42. /*! @brief LPSPI status flags in SPIx_SR register.*/
  43. enum _lpspi_flags
  44. {
  45. kLPSPI_TxDataRequestFlag = LPSPI_SR_TDF_MASK, /*!< Transmit data flag */
  46. kLPSPI_RxDataReadyFlag = LPSPI_SR_RDF_MASK, /*!< Receive data flag */
  47. kLPSPI_WordCompleteFlag = LPSPI_SR_WCF_MASK, /*!< Word Complete flag */
  48. kLPSPI_FrameCompleteFlag = LPSPI_SR_FCF_MASK, /*!< Frame Complete flag */
  49. kLPSPI_TransferCompleteFlag = LPSPI_SR_TCF_MASK, /*!< Transfer Complete flag */
  50. kLPSPI_TransmitErrorFlag = LPSPI_SR_TEF_MASK, /*!< Transmit Error flag (FIFO underrun) */
  51. kLPSPI_ReceiveErrorFlag = LPSPI_SR_REF_MASK, /*!< Receive Error flag (FIFO overrun) */
  52. kLPSPI_DataMatchFlag = LPSPI_SR_DMF_MASK, /*!< Data Match flag */
  53. kLPSPI_ModuleBusyFlag = LPSPI_SR_MBF_MASK, /*!< Module Busy flag */
  54. kLPSPI_AllStatusFlag = (LPSPI_SR_TDF_MASK | LPSPI_SR_RDF_MASK | LPSPI_SR_WCF_MASK | LPSPI_SR_FCF_MASK |
  55. LPSPI_SR_TCF_MASK | LPSPI_SR_TEF_MASK | LPSPI_SR_REF_MASK | LPSPI_SR_DMF_MASK |
  56. LPSPI_SR_MBF_MASK) /*!< Used for clearing all w1c status flags */
  57. };
  58. /*! @brief LPSPI interrupt source.*/
  59. enum _lpspi_interrupt_enable
  60. {
  61. kLPSPI_TxInterruptEnable = LPSPI_IER_TDIE_MASK, /*!< Transmit data interrupt enable */
  62. kLPSPI_RxInterruptEnable = LPSPI_IER_RDIE_MASK, /*!< Receive data interrupt enable */
  63. kLPSPI_WordCompleteInterruptEnable = LPSPI_IER_WCIE_MASK, /*!< Word complete interrupt enable */
  64. kLPSPI_FrameCompleteInterruptEnable = LPSPI_IER_FCIE_MASK, /*!< Frame complete interrupt enable */
  65. kLPSPI_TransferCompleteInterruptEnable = LPSPI_IER_TCIE_MASK, /*!< Transfer complete interrupt enable */
  66. kLPSPI_TransmitErrorInterruptEnable = LPSPI_IER_TEIE_MASK, /*!< Transmit error interrupt enable(FIFO underrun)*/
  67. kLPSPI_ReceiveErrorInterruptEnable = LPSPI_IER_REIE_MASK, /*!< Receive Error interrupt enable (FIFO overrun) */
  68. kLPSPI_DataMatchInterruptEnable = LPSPI_IER_DMIE_MASK, /*!< Data Match interrupt enable */
  69. kLPSPI_AllInterruptEnable =
  70. (LPSPI_IER_TDIE_MASK | LPSPI_IER_RDIE_MASK | LPSPI_IER_WCIE_MASK | LPSPI_IER_FCIE_MASK | LPSPI_IER_TCIE_MASK |
  71. LPSPI_IER_TEIE_MASK | LPSPI_IER_REIE_MASK | LPSPI_IER_DMIE_MASK) /*!< All above interrupts enable.*/
  72. };
  73. /*! @brief LPSPI DMA source.*/
  74. enum _lpspi_dma_enable
  75. {
  76. kLPSPI_TxDmaEnable = LPSPI_DER_TDDE_MASK, /*!< Transmit data DMA enable */
  77. kLPSPI_RxDmaEnable = LPSPI_DER_RDDE_MASK /*!< Receive data DMA enable */
  78. };
  79. /*! @brief LPSPI master or slave mode configuration.*/
  80. typedef enum _lpspi_master_slave_mode
  81. {
  82. kLPSPI_Master = 1U, /*!< LPSPI peripheral operates in master mode.*/
  83. kLPSPI_Slave = 0U /*!< LPSPI peripheral operates in slave mode.*/
  84. } lpspi_master_slave_mode_t;
  85. /*! @brief LPSPI Peripheral Chip Select (PCS) configuration (which PCS to configure).*/
  86. typedef enum _lpspi_which_pcs_config
  87. {
  88. kLPSPI_Pcs0 = 0U, /*!< PCS[0] */
  89. kLPSPI_Pcs1 = 1U, /*!< PCS[1] */
  90. kLPSPI_Pcs2 = 2U, /*!< PCS[2] */
  91. kLPSPI_Pcs3 = 3U /*!< PCS[3] */
  92. } lpspi_which_pcs_t;
  93. /*! @brief LPSPI Peripheral Chip Select (PCS) Polarity configuration.*/
  94. typedef enum _lpspi_pcs_polarity_config
  95. {
  96. kLPSPI_PcsActiveHigh = 1U, /*!< PCS Active High (idles low) */
  97. kLPSPI_PcsActiveLow = 0U /*!< PCS Active Low (idles high) */
  98. } lpspi_pcs_polarity_config_t;
  99. /*! @brief LPSPI Peripheral Chip Select (PCS) Polarity.*/
  100. enum _lpspi_pcs_polarity
  101. {
  102. kLPSPI_Pcs0ActiveLow = 1U << 0, /*!< Pcs0 Active Low (idles high). */
  103. kLPSPI_Pcs1ActiveLow = 1U << 1, /*!< Pcs1 Active Low (idles high). */
  104. kLPSPI_Pcs2ActiveLow = 1U << 2, /*!< Pcs2 Active Low (idles high). */
  105. kLPSPI_Pcs3ActiveLow = 1U << 3, /*!< Pcs3 Active Low (idles high). */
  106. kLPSPI_PcsAllActiveLow = 0xFU /*!< Pcs0 to Pcs5 Active Low (idles high). */
  107. };
  108. /*! @brief LPSPI clock polarity configuration.*/
  109. typedef enum _lpspi_clock_polarity
  110. {
  111. kLPSPI_ClockPolarityActiveHigh = 0U, /*!< CPOL=0. Active-high LPSPI clock (idles low)*/
  112. kLPSPI_ClockPolarityActiveLow = 1U /*!< CPOL=1. Active-low LPSPI clock (idles high)*/
  113. } lpspi_clock_polarity_t;
  114. /*! @brief LPSPI clock phase configuration.*/
  115. typedef enum _lpspi_clock_phase
  116. {
  117. kLPSPI_ClockPhaseFirstEdge = 0U, /*!< CPHA=0. Data is captured on the leading edge of the SCK and changed on the
  118. following edge.*/
  119. kLPSPI_ClockPhaseSecondEdge = 1U /*!< CPHA=1. Data is changed on the leading edge of the SCK and captured on the
  120. following edge.*/
  121. } lpspi_clock_phase_t;
  122. /*! @brief LPSPI data shifter direction options.*/
  123. typedef enum _lpspi_shift_direction
  124. {
  125. kLPSPI_MsbFirst = 0U, /*!< Data transfers start with most significant bit.*/
  126. kLPSPI_LsbFirst = 1U /*!< Data transfers start with least significant bit.*/
  127. } lpspi_shift_direction_t;
  128. /*! @brief LPSPI Host Request select configuration. */
  129. typedef enum _lpspi_host_request_select
  130. {
  131. kLPSPI_HostReqExtPin = 0U, /*!< Host Request is an ext pin. */
  132. kLPSPI_HostReqInternalTrigger = 1U /*!< Host Request is an internal trigger. */
  133. } lpspi_host_request_select_t;
  134. /*! @brief LPSPI Match configuration options. */
  135. typedef enum _lpspi_match_config
  136. {
  137. kLPSI_MatchDisabled = 0x0U, /*!< LPSPI Match Disabled. */
  138. kLPSI_1stWordEqualsM0orM1 = 0x2U, /*!< LPSPI Match Enabled. */
  139. kLPSI_AnyWordEqualsM0orM1 = 0x3U, /*!< LPSPI Match Enabled. */
  140. kLPSI_1stWordEqualsM0and2ndWordEqualsM1 = 0x4U, /*!< LPSPI Match Enabled. */
  141. kLPSI_AnyWordEqualsM0andNxtWordEqualsM1 = 0x5U, /*!< LPSPI Match Enabled. */
  142. kLPSI_1stWordAndM1EqualsM0andM1 = 0x6U, /*!< LPSPI Match Enabled. */
  143. kLPSI_AnyWordAndM1EqualsM0andM1 = 0x7U, /*!< LPSPI Match Enabled. */
  144. } lpspi_match_config_t;
  145. /*! @brief LPSPI pin (SDO and SDI) configuration. */
  146. typedef enum _lpspi_pin_config
  147. {
  148. kLPSPI_SdiInSdoOut = 0U, /*!< LPSPI SDI input, SDO output. */
  149. kLPSPI_SdiInSdiOut = 1U, /*!< LPSPI SDI input, SDI output. */
  150. kLPSPI_SdoInSdoOut = 2U, /*!< LPSPI SDO input, SDO output. */
  151. kLPSPI_SdoInSdiOut = 3U /*!< LPSPI SDO input, SDI output. */
  152. } lpspi_pin_config_t;
  153. /*! @brief LPSPI data output configuration. */
  154. typedef enum _lpspi_data_out_config
  155. {
  156. kLpspiDataOutRetained = 0U, /*!< Data out retains last value when chip select is de-asserted */
  157. kLpspiDataOutTristate = 1U /*!< Data out is tristated when chip select is de-asserted */
  158. } lpspi_data_out_config_t;
  159. /*! @brief LPSPI transfer width configuration. */
  160. typedef enum _lpspi_transfer_width
  161. {
  162. kLPSPI_SingleBitXfer = 0U, /*!< 1-bit shift at a time, data out on SDO, in on SDI (normal mode) */
  163. kLPSPI_TwoBitXfer = 1U, /*!< 2-bits shift out on SDO/SDI and in on SDO/SDI */
  164. kLPSPI_FourBitXfer = 2U /*!< 4-bits shift out on SDO/SDI/PCS[3:2] and in on SDO/SDI/PCS[3:2] */
  165. } lpspi_transfer_width_t;
  166. /*! @brief LPSPI delay type selection.*/
  167. typedef enum _lpspi_delay_type
  168. {
  169. kLPSPI_PcsToSck = 1U, /*!< PCS-to-SCK delay. */
  170. kLPSPI_LastSckToPcs, /*!< Last SCK edge to PCS delay. */
  171. kLPSPI_BetweenTransfer /*!< Delay between transfers. */
  172. } lpspi_delay_type_t;
  173. #define LPSPI_MASTER_PCS_SHIFT (4U) /*!< LPSPI master PCS shift macro , internal used. */
  174. #define LPSPI_MASTER_PCS_MASK (0xF0U) /*!< LPSPI master PCS shift macro , internal used. */
  175. /*! @brief Use this enumeration for LPSPI master transfer configFlags. */
  176. enum _lpspi_transfer_config_flag_for_master
  177. {
  178. kLPSPI_MasterPcs0 = 0U << LPSPI_MASTER_PCS_SHIFT, /*!< LPSPI master transfer use PCS0 signal */
  179. kLPSPI_MasterPcs1 = 1U << LPSPI_MASTER_PCS_SHIFT, /*!< LPSPI master transfer use PCS1 signal */
  180. kLPSPI_MasterPcs2 = 2U << LPSPI_MASTER_PCS_SHIFT, /*!< LPSPI master transfer use PCS2 signal */
  181. kLPSPI_MasterPcs3 = 3U << LPSPI_MASTER_PCS_SHIFT, /*!< LPSPI master transfer use PCS3 signal */
  182. kLPSPI_MasterPcsContinuous = 1U << 20, /*!< Is PCS signal continuous */
  183. kLPSPI_MasterByteSwap =
  184. 1U << 22 /*!< Is master swap the byte.
  185. * For example, when want to send data 1 2 3 4 5 6 7 8 (suppose you set
  186. * lpspi_shift_direction_t to MSB).
  187. * 1. If you set bitPerFrame = 8 , no matter the kLPSPI_MasterByteSwapyou flag is used
  188. * or not, the waveform is 1 2 3 4 5 6 7 8.
  189. * 2. If you set bitPerFrame = 16 :
  190. * (1) the waveform is 2 1 4 3 6 5 8 7 if you do not use the kLPSPI_MasterByteSwap flag.
  191. * (2) the waveform is 1 2 3 4 5 6 7 8 if you use the kLPSPI_MasterByteSwap flag.
  192. * 3. If you set bitPerFrame = 32 :
  193. * (1) the waveform is 4 3 2 1 8 7 6 5 if you do not use the kLPSPI_MasterByteSwap flag.
  194. * (2) the waveform is 1 2 3 4 5 6 7 8 if you use the kLPSPI_MasterByteSwap flag.
  195. */
  196. };
  197. #define LPSPI_SLAVE_PCS_SHIFT (4U) /*!< LPSPI slave PCS shift macro , internal used. */
  198. #define LPSPI_SLAVE_PCS_MASK (0xF0U) /*!< LPSPI slave PCS shift macro , internal used. */
  199. /*! @brief Use this enumeration for LPSPI slave transfer configFlags. */
  200. enum _lpspi_transfer_config_flag_for_slave
  201. {
  202. kLPSPI_SlavePcs0 = 0U << LPSPI_SLAVE_PCS_SHIFT, /*!< LPSPI slave transfer use PCS0 signal */
  203. kLPSPI_SlavePcs1 = 1U << LPSPI_SLAVE_PCS_SHIFT, /*!< LPSPI slave transfer use PCS1 signal */
  204. kLPSPI_SlavePcs2 = 2U << LPSPI_SLAVE_PCS_SHIFT, /*!< LPSPI slave transfer use PCS2 signal */
  205. kLPSPI_SlavePcs3 = 3U << LPSPI_SLAVE_PCS_SHIFT, /*!< LPSPI slave transfer use PCS3 signal */
  206. kLPSPI_SlaveByteSwap =
  207. 1U << 22 /*!< Is slave swap the byte.
  208. * For example, when want to send data 1 2 3 4 5 6 7 8 (suppose you set
  209. * lpspi_shift_direction_t to MSB).
  210. * 1. If you set bitPerFrame = 8 , no matter the kLPSPI_SlaveByteSwap flag is used
  211. * or not, the waveform is 1 2 3 4 5 6 7 8.
  212. * 2. If you set bitPerFrame = 16 :
  213. * (1) the waveform is 2 1 4 3 6 5 8 7 if you do not use the kLPSPI_SlaveByteSwap flag.
  214. * (2) the waveform is 1 2 3 4 5 6 7 8 if you use the kLPSPI_SlaveByteSwap flag.
  215. * 3. If you set bitPerFrame = 32 :
  216. * (1) the waveform is 4 3 2 1 8 7 6 5 if you do not use the kLPSPI_SlaveByteSwap flag.
  217. * (2) the waveform is 1 2 3 4 5 6 7 8 if you use the kLPSPI_SlaveByteSwap flag.
  218. */
  219. };
  220. /*! @brief LPSPI transfer state, which is used for LPSPI transactional API state machine. */
  221. enum _lpspi_transfer_state
  222. {
  223. kLPSPI_Idle = 0x0U, /*!< Nothing in the transmitter/receiver. */
  224. kLPSPI_Busy, /*!< Transfer queue is not finished. */
  225. kLPSPI_Error /*!< Transfer error. */
  226. };
  227. /*! @brief LPSPI master configuration structure.*/
  228. typedef struct _lpspi_master_config
  229. {
  230. uint32_t baudRate; /*!< Baud Rate for LPSPI. */
  231. uint32_t bitsPerFrame; /*!< Bits per frame, minimum 8, maximum 4096.*/
  232. lpspi_clock_polarity_t cpol; /*!< Clock polarity. */
  233. lpspi_clock_phase_t cpha; /*!< Clock phase. */
  234. lpspi_shift_direction_t direction; /*!< MSB or LSB data shift direction. */
  235. uint32_t pcsToSckDelayInNanoSec; /*!< PCS to SCK delay time in nanoseconds, setting to 0 sets the minimum delay.
  236. It sets the boundary value if out of range.*/
  237. uint32_t lastSckToPcsDelayInNanoSec; /*!< Last SCK to PCS delay time in nanoseconds, setting to 0 sets the minimum
  238. delay. It sets the boundary value if out of range.*/
  239. uint32_t betweenTransferDelayInNanoSec; /*!< After the SCK delay time with nanoseconds, setting to 0 sets the
  240. minimum delay. It sets the boundary value if out of range.*/
  241. lpspi_which_pcs_t whichPcs; /*!< Desired Peripheral Chip Select (PCS). */
  242. lpspi_pcs_polarity_config_t pcsActiveHighOrLow; /*!< Desired PCS active high or low */
  243. lpspi_pin_config_t pinCfg; /*!< Configures which pins are used for input and output data
  244. *during single bit transfers.*/
  245. lpspi_data_out_config_t dataOutConfig; /*!< Configures if the output data is tristated
  246. * between accesses (LPSPI_PCS is negated). */
  247. } lpspi_master_config_t;
  248. /*! @brief LPSPI slave configuration structure.*/
  249. typedef struct _lpspi_slave_config
  250. {
  251. uint32_t bitsPerFrame; /*!< Bits per frame, minimum 8, maximum 4096.*/
  252. lpspi_clock_polarity_t cpol; /*!< Clock polarity. */
  253. lpspi_clock_phase_t cpha; /*!< Clock phase. */
  254. lpspi_shift_direction_t direction; /*!< MSB or LSB data shift direction. */
  255. lpspi_which_pcs_t whichPcs; /*!< Desired Peripheral Chip Select (pcs) */
  256. lpspi_pcs_polarity_config_t pcsActiveHighOrLow; /*!< Desired PCS active high or low */
  257. lpspi_pin_config_t pinCfg; /*!< Configures which pins are used for input and output data
  258. *during single bit transfers.*/
  259. lpspi_data_out_config_t dataOutConfig; /*!< Configures if the output data is tristated
  260. * between accesses (LPSPI_PCS is negated). */
  261. } lpspi_slave_config_t;
  262. /*!
  263. * @brief Forward declaration of the _lpspi_master_handle typedefs.
  264. */
  265. typedef struct _lpspi_master_handle lpspi_master_handle_t;
  266. /*!
  267. * @brief Forward declaration of the _lpspi_slave_handle typedefs.
  268. */
  269. typedef struct _lpspi_slave_handle lpspi_slave_handle_t;
  270. /*!
  271. * @brief Master completion callback function pointer type.
  272. *
  273. * @param base LPSPI peripheral address.
  274. * @param handle Pointer to the handle for the LPSPI master.
  275. * @param status Success or error code describing whether the transfer is completed.
  276. * @param userData Arbitrary pointer-dataSized value passed from the application.
  277. */
  278. typedef void (*lpspi_master_transfer_callback_t)(LPSPI_Type *base,
  279. lpspi_master_handle_t *handle,
  280. status_t status,
  281. void *userData);
  282. /*!
  283. * @brief Slave completion callback function pointer type.
  284. *
  285. * @param base LPSPI peripheral address.
  286. * @param handle Pointer to the handle for the LPSPI slave.
  287. * @param status Success or error code describing whether the transfer is completed.
  288. * @param userData Arbitrary pointer-dataSized value passed from the application.
  289. */
  290. typedef void (*lpspi_slave_transfer_callback_t)(LPSPI_Type *base,
  291. lpspi_slave_handle_t *handle,
  292. status_t status,
  293. void *userData);
  294. /*! @brief LPSPI master/slave transfer structure.*/
  295. typedef struct _lpspi_transfer
  296. {
  297. uint8_t *txData; /*!< Send buffer. */
  298. uint8_t *rxData; /*!< Receive buffer. */
  299. volatile size_t dataSize; /*!< Transfer bytes. */
  300. uint32_t configFlags; /*!< Transfer transfer configuration flags. Set from _lpspi_transfer_config_flag_for_master if
  301. the transfer is used for master or _lpspi_transfer_config_flag_for_slave enumeration if the
  302. transfer is used for slave.*/
  303. } lpspi_transfer_t;
  304. /*! @brief LPSPI master transfer handle structure used for transactional API. */
  305. struct _lpspi_master_handle
  306. {
  307. volatile bool isPcsContinuous; /*!< Is PCS continuous in transfer. */
  308. volatile bool writeTcrInIsr; /*!< A flag that whether should write TCR in ISR. */
  309. volatile bool isByteSwap; /*!< A flag that whether should byte swap. */
  310. volatile bool isTxMask; /*!< A flag that whether TCR[TXMSK] is set. */
  311. volatile uint16_t bytesPerFrame; /*!< Number of bytes in each frame */
  312. volatile uint8_t fifoSize; /*!< FIFO dataSize. */
  313. volatile uint8_t rxWatermark; /*!< Rx watermark. */
  314. volatile uint8_t bytesEachWrite; /*!< Bytes for each write TDR. */
  315. volatile uint8_t bytesEachRead; /*!< Bytes for each read RDR. */
  316. uint8_t *volatile txData; /*!< Send buffer. */
  317. uint8_t *volatile rxData; /*!< Receive buffer. */
  318. volatile size_t txRemainingByteCount; /*!< Number of bytes remaining to send.*/
  319. volatile size_t rxRemainingByteCount; /*!< Number of bytes remaining to receive.*/
  320. volatile uint32_t writeRegRemainingTimes; /*!< Write TDR register remaining times. */
  321. volatile uint32_t readRegRemainingTimes; /*!< Read RDR register remaining times. */
  322. uint32_t totalByteCount; /*!< Number of transfer bytes*/
  323. uint32_t txBuffIfNull; /*!< Used if the txData is NULL. */
  324. volatile uint8_t state; /*!< LPSPI transfer state , _lpspi_transfer_state.*/
  325. lpspi_master_transfer_callback_t callback; /*!< Completion callback. */
  326. void *userData; /*!< Callback user data. */
  327. };
  328. /*! @brief LPSPI slave transfer handle structure used for transactional API. */
  329. struct _lpspi_slave_handle
  330. {
  331. volatile bool isByteSwap; /*!< A flag that whether should byte swap. */
  332. volatile uint8_t fifoSize; /*!< FIFO dataSize. */
  333. volatile uint8_t rxWatermark; /*!< Rx watermark. */
  334. volatile uint8_t bytesEachWrite; /*!< Bytes for each write TDR. */
  335. volatile uint8_t bytesEachRead; /*!< Bytes for each read RDR. */
  336. uint8_t *volatile txData; /*!< Send buffer. */
  337. uint8_t *volatile rxData; /*!< Receive buffer. */
  338. volatile size_t txRemainingByteCount; /*!< Number of bytes remaining to send.*/
  339. volatile size_t rxRemainingByteCount; /*!< Number of bytes remaining to receive.*/
  340. volatile uint32_t writeRegRemainingTimes; /*!< Write TDR register remaining times. */
  341. volatile uint32_t readRegRemainingTimes; /*!< Read RDR register remaining times. */
  342. uint32_t totalByteCount; /*!< Number of transfer bytes*/
  343. volatile uint8_t state; /*!< LPSPI transfer state , _lpspi_transfer_state.*/
  344. volatile uint32_t errorCount; /*!< Error count for slave transfer.*/
  345. lpspi_slave_transfer_callback_t callback; /*!< Completion callback. */
  346. void *userData; /*!< Callback user data. */
  347. };
  348. /**********************************************************************************************************************
  349. * API
  350. *********************************************************************************************************************/
  351. #if defined(__cplusplus)
  352. extern "C" {
  353. #endif /*_cplusplus*/
  354. /*!
  355. * @name Initialization and deinitialization
  356. * @{
  357. */
  358. /*!
  359. * @brief Initializes the LPSPI master.
  360. *
  361. * @param base LPSPI peripheral address.
  362. * @param masterConfig Pointer to structure lpspi_master_config_t.
  363. * @param srcClock_Hz Module source input clock in Hertz
  364. */
  365. void LPSPI_MasterInit(LPSPI_Type *base, const lpspi_master_config_t *masterConfig, uint32_t srcClock_Hz);
  366. /*!
  367. * @brief Sets the lpspi_master_config_t structure to default values.
  368. *
  369. * This API initializes the configuration structure for LPSPI_MasterInit().
  370. * The initialized structure can remain unchanged in LPSPI_MasterInit(), or can be modified
  371. * before calling the LPSPI_MasterInit().
  372. * Example:
  373. * @code
  374. * lpspi_master_config_t masterConfig;
  375. * LPSPI_MasterGetDefaultConfig(&masterConfig);
  376. * @endcode
  377. * @param masterConfig pointer to lpspi_master_config_t structure
  378. */
  379. void LPSPI_MasterGetDefaultConfig(lpspi_master_config_t *masterConfig);
  380. /*!
  381. * @brief LPSPI slave configuration.
  382. *
  383. * @param base LPSPI peripheral address.
  384. * @param slaveConfig Pointer to a structure lpspi_slave_config_t.
  385. */
  386. void LPSPI_SlaveInit(LPSPI_Type *base, const lpspi_slave_config_t *slaveConfig);
  387. /*!
  388. * @brief Sets the lpspi_slave_config_t structure to default values.
  389. *
  390. * This API initializes the configuration structure for LPSPI_SlaveInit().
  391. * The initialized structure can remain unchanged in LPSPI_SlaveInit() or can be modified
  392. * before calling the LPSPI_SlaveInit().
  393. * Example:
  394. * @code
  395. * lpspi_slave_config_t slaveConfig;
  396. * LPSPI_SlaveGetDefaultConfig(&slaveConfig);
  397. * @endcode
  398. * @param slaveConfig pointer to lpspi_slave_config_t structure.
  399. */
  400. void LPSPI_SlaveGetDefaultConfig(lpspi_slave_config_t *slaveConfig);
  401. /*!
  402. * @brief De-initializes the LPSPI peripheral. Call this API to disable the LPSPI clock.
  403. * @param base LPSPI peripheral address.
  404. */
  405. void LPSPI_Deinit(LPSPI_Type *base);
  406. /*!
  407. * @brief Restores the LPSPI peripheral to reset state. Note that this function
  408. * sets all registers to reset state. As a result, the LPSPI module can't work after calling
  409. * this API.
  410. * @param base LPSPI peripheral address.
  411. */
  412. void LPSPI_Reset(LPSPI_Type *base);
  413. /*!
  414. * @brief Get the LPSPI instance from peripheral base address.
  415. *
  416. * @param base LPSPI peripheral base address.
  417. * @return LPSPI instance.
  418. */
  419. uint32_t LPSPI_GetInstance(LPSPI_Type *base);
  420. /*!
  421. * @brief Enables the LPSPI peripheral and sets the MCR MDIS to 0.
  422. *
  423. * @param base LPSPI peripheral address.
  424. * @param enable Pass true to enable module, false to disable module.
  425. */
  426. static inline void LPSPI_Enable(LPSPI_Type *base, bool enable)
  427. {
  428. if (enable)
  429. {
  430. base->CR |= LPSPI_CR_MEN_MASK;
  431. }
  432. else
  433. {
  434. base->CR &= ~LPSPI_CR_MEN_MASK;
  435. }
  436. }
  437. /*!
  438. *@}
  439. */
  440. /*!
  441. * @name Status
  442. * @{
  443. */
  444. /*!
  445. * @brief Gets the LPSPI status flag state.
  446. * @param base LPSPI peripheral address.
  447. * @return The LPSPI status(in SR register).
  448. */
  449. static inline uint32_t LPSPI_GetStatusFlags(LPSPI_Type *base)
  450. {
  451. return (base->SR);
  452. }
  453. /*!
  454. * @brief Gets the LPSPI Tx FIFO size.
  455. * @param base LPSPI peripheral address.
  456. * @return The LPSPI Tx FIFO size.
  457. */
  458. static inline uint8_t LPSPI_GetTxFifoSize(LPSPI_Type *base)
  459. {
  460. return (1U << ((base->PARAM & LPSPI_PARAM_TXFIFO_MASK) >> LPSPI_PARAM_TXFIFO_SHIFT));
  461. }
  462. /*!
  463. * @brief Gets the LPSPI Rx FIFO size.
  464. * @param base LPSPI peripheral address.
  465. * @return The LPSPI Rx FIFO size.
  466. */
  467. static inline uint8_t LPSPI_GetRxFifoSize(LPSPI_Type *base)
  468. {
  469. return (1U << ((base->PARAM & LPSPI_PARAM_RXFIFO_MASK) >> LPSPI_PARAM_RXFIFO_SHIFT));
  470. }
  471. /*!
  472. * @brief Gets the LPSPI Tx FIFO count.
  473. * @param base LPSPI peripheral address.
  474. * @return The number of words in the transmit FIFO.
  475. */
  476. static inline uint32_t LPSPI_GetTxFifoCount(LPSPI_Type *base)
  477. {
  478. return ((base->FSR & LPSPI_FSR_TXCOUNT_MASK) >> LPSPI_FSR_TXCOUNT_SHIFT);
  479. }
  480. /*!
  481. * @brief Gets the LPSPI Rx FIFO count.
  482. * @param base LPSPI peripheral address.
  483. * @return The number of words in the receive FIFO.
  484. */
  485. static inline uint32_t LPSPI_GetRxFifoCount(LPSPI_Type *base)
  486. {
  487. return ((base->FSR & LPSPI_FSR_RXCOUNT_MASK) >> LPSPI_FSR_RXCOUNT_SHIFT);
  488. }
  489. /*!
  490. * @brief Clears the LPSPI status flag.
  491. *
  492. * This function clears the desired status bit by using a write-1-to-clear. The user passes in the base and the
  493. * desired status flag bit to clear. The list of status flags is defined in the _lpspi_flags.
  494. * Example usage:
  495. * @code
  496. * LPSPI_ClearStatusFlags(base, kLPSPI_TxDataRequestFlag|kLPSPI_RxDataReadyFlag);
  497. * @endcode
  498. *
  499. * @param base LPSPI peripheral address.
  500. * @param statusFlags The status flag used from type _lpspi_flags.
  501. */
  502. static inline void LPSPI_ClearStatusFlags(LPSPI_Type *base, uint32_t statusFlags)
  503. {
  504. base->SR = statusFlags; /*!< The status flags are cleared by writing 1 (w1c).*/
  505. }
  506. /*!
  507. *@}
  508. */
  509. /*!
  510. * @name Interrupts
  511. * @{
  512. */
  513. /*!
  514. * @brief Enables the LPSPI interrupts.
  515. *
  516. * This function configures the various interrupt masks of the LPSPI. The parameters are base and an interrupt mask.
  517. * Note that, for Tx fill and Rx FIFO drain requests, enabling the interrupt request disables the DMA request.
  518. *
  519. * @code
  520. * LPSPI_EnableInterrupts(base, kLPSPI_TxInterruptEnable | kLPSPI_RxInterruptEnable );
  521. * @endcode
  522. *
  523. * @param base LPSPI peripheral address.
  524. * @param mask The interrupt mask; Use the enum _lpspi_interrupt_enable.
  525. */
  526. static inline void LPSPI_EnableInterrupts(LPSPI_Type *base, uint32_t mask)
  527. {
  528. base->IER |= mask;
  529. }
  530. /*!
  531. * @brief Disables the LPSPI interrupts.
  532. *
  533. * @code
  534. * LPSPI_DisableInterrupts(base, kLPSPI_TxInterruptEnable | kLPSPI_RxInterruptEnable );
  535. * @endcode
  536. *
  537. * @param base LPSPI peripheral address.
  538. * @param mask The interrupt mask; Use the enum _lpspi_interrupt_enable.
  539. */
  540. static inline void LPSPI_DisableInterrupts(LPSPI_Type *base, uint32_t mask)
  541. {
  542. base->IER &= ~mask;
  543. }
  544. /*!
  545. *@}
  546. */
  547. /*!
  548. * @name DMA Control
  549. * @{
  550. */
  551. /*!
  552. * @brief Enables the LPSPI DMA request.
  553. *
  554. * This function configures the Rx and Tx DMA mask of the LPSPI. The parameters are base and a DMA mask.
  555. * @code
  556. * LPSPI_EnableDMA(base, kLPSPI_TxDmaEnable | kLPSPI_RxDmaEnable);
  557. * @endcode
  558. *
  559. * @param base LPSPI peripheral address.
  560. * @param mask The interrupt mask; Use the enum _lpspi_dma_enable.
  561. */
  562. static inline void LPSPI_EnableDMA(LPSPI_Type *base, uint32_t mask)
  563. {
  564. base->DER |= mask;
  565. }
  566. /*!
  567. * @brief Disables the LPSPI DMA request.
  568. *
  569. * This function configures the Rx and Tx DMA mask of the LPSPI. The parameters are base and a DMA mask.
  570. * @code
  571. * SPI_DisableDMA(base, kLPSPI_TxDmaEnable | kLPSPI_RxDmaEnable);
  572. * @endcode
  573. *
  574. * @param base LPSPI peripheral address.
  575. * @param mask The interrupt mask; Use the enum _lpspi_dma_enable.
  576. */
  577. static inline void LPSPI_DisableDMA(LPSPI_Type *base, uint32_t mask)
  578. {
  579. base->DER &= ~mask;
  580. }
  581. /*!
  582. * @brief Gets the LPSPI Transmit Data Register address for a DMA operation.
  583. *
  584. * This function gets the LPSPI Transmit Data Register address because this value is needed
  585. * for the DMA operation.
  586. * This function can be used for either master or slave mode.
  587. *
  588. * @param base LPSPI peripheral address.
  589. * @return The LPSPI Transmit Data Register address.
  590. */
  591. static inline uint32_t LPSPI_GetTxRegisterAddress(LPSPI_Type *base)
  592. {
  593. return (uint32_t) & (base->TDR);
  594. }
  595. /*!
  596. * @brief Gets the LPSPI Receive Data Register address for a DMA operation.
  597. *
  598. * This function gets the LPSPI Receive Data Register address because this value is needed
  599. * for the DMA operation.
  600. * This function can be used for either master or slave mode.
  601. *
  602. * @param base LPSPI peripheral address.
  603. * @return The LPSPI Receive Data Register address.
  604. */
  605. static inline uint32_t LPSPI_GetRxRegisterAddress(LPSPI_Type *base)
  606. {
  607. return (uint32_t) & (base->RDR);
  608. }
  609. /*!
  610. *@}
  611. */
  612. /*!
  613. * @name Bus Operations
  614. * @{
  615. */
  616. /*!
  617. * @brief Check the argument for transfer .
  618. *
  619. * @param base LPSPI peripheral address.
  620. * @param transfer the transfer struct to be used.
  621. * @param isEdma True to check for EDMA transfer, false to check interrupt non-blocking transfer
  622. * @return Return true for right and false for wrong.
  623. */
  624. bool LPSPI_CheckTransferArgument(LPSPI_Type *base, lpspi_transfer_t *transfer, bool isEdma);
  625. /*!
  626. * @brief Configures the LPSPI for either master or slave.
  627. *
  628. * Note that the CFGR1 should only be written when the LPSPI is disabled (LPSPIx_CR_MEN = 0).
  629. *
  630. * @param base LPSPI peripheral address.
  631. * @param mode Mode setting (master or slave) of type lpspi_master_slave_mode_t.
  632. */
  633. static inline void LPSPI_SetMasterSlaveMode(LPSPI_Type *base, lpspi_master_slave_mode_t mode)
  634. {
  635. base->CFGR1 = (base->CFGR1 & (~LPSPI_CFGR1_MASTER_MASK)) | LPSPI_CFGR1_MASTER(mode);
  636. }
  637. /*!
  638. * @brief Configures the peripheral chip select used for the transfer.
  639. *
  640. * @param base LPSPI peripheral address.
  641. * @param select LPSPI Peripheral Chip Select (PCS) configuration.
  642. */
  643. static inline void LPSPI_SelectTransferPCS(LPSPI_Type *base, lpspi_which_pcs_t select)
  644. {
  645. base->TCR = (base->TCR & (~LPSPI_TCR_PCS_MASK)) | LPSPI_TCR_PCS((uint8_t)select);
  646. }
  647. /*!
  648. * @brief Set the PCS signal to continuous or uncontinuous mode.
  649. *
  650. * @note In master mode, continuous transfer will keep the PCS asserted at the end of the frame size, until a command
  651. * word is received that starts a new frame. So PCS must be set back to uncontinuous when transfer finishes.
  652. * In slave mode, when continuous transfer is enabled, the LPSPI will only transmit the first frame size bits, after
  653. * that the LPSPI will transmit received data back (assuming a 32-bit shift register).
  654. *
  655. * @param base LPSPI peripheral address.
  656. * @param IsContinous True to set the transfer PCS to continuous mode, false to set to uncontinuous mode.
  657. */
  658. static inline void LPSPI_SetPCSContinous(LPSPI_Type *base, bool IsContinous)
  659. {
  660. if (IsContinous)
  661. {
  662. base->TCR |= LPSPI_TCR_CONT_MASK;
  663. }
  664. else
  665. {
  666. base->TCR &= ~LPSPI_TCR_CONT_MASK;
  667. }
  668. }
  669. /*!
  670. * @brief Returns whether the LPSPI module is in master mode.
  671. *
  672. * @param base LPSPI peripheral address.
  673. * @return Returns true if the module is in master mode or false if the module is in slave mode.
  674. */
  675. static inline bool LPSPI_IsMaster(LPSPI_Type *base)
  676. {
  677. return (bool)((base->CFGR1) & LPSPI_CFGR1_MASTER_MASK);
  678. }
  679. /*!
  680. * @brief Flushes the LPSPI FIFOs.
  681. *
  682. * @param base LPSPI peripheral address.
  683. * @param flushTxFifo Flushes (true) the Tx FIFO, else do not flush (false) the Tx FIFO.
  684. * @param flushRxFifo Flushes (true) the Rx FIFO, else do not flush (false) the Rx FIFO.
  685. */
  686. static inline void LPSPI_FlushFifo(LPSPI_Type *base, bool flushTxFifo, bool flushRxFifo)
  687. {
  688. base->CR |= ((uint32_t)flushTxFifo << LPSPI_CR_RTF_SHIFT) | ((uint32_t)flushRxFifo << LPSPI_CR_RRF_SHIFT);
  689. }
  690. /*!
  691. * @brief Sets the transmit and receive FIFO watermark values.
  692. *
  693. * This function allows the user to set the receive and transmit FIFO watermarks. The function
  694. * does not compare the watermark settings to the FIFO size. The FIFO watermark should not be
  695. * equal to or greater than the FIFO size. It is up to the higher level driver to make this check.
  696. *
  697. * @param base LPSPI peripheral address.
  698. * @param txWater The TX FIFO watermark value. Writing a value equal or greater than the FIFO size is truncated.
  699. * @param rxWater The RX FIFO watermark value. Writing a value equal or greater than the FIFO size is truncated.
  700. */
  701. static inline void LPSPI_SetFifoWatermarks(LPSPI_Type *base, uint32_t txWater, uint32_t rxWater)
  702. {
  703. base->FCR = LPSPI_FCR_TXWATER(txWater) | LPSPI_FCR_RXWATER(rxWater);
  704. }
  705. /*!
  706. * @brief Configures all LPSPI peripheral chip select polarities simultaneously.
  707. *
  708. * Note that the CFGR1 should only be written when the LPSPI is disabled (LPSPIx_CR_MEN = 0).
  709. *
  710. * This is an example: PCS0 and PCS1 set to active low and other PCSs set to active high. Note that the number of
  711. * PCS is device-specific.
  712. * @code
  713. * LPSPI_SetAllPcsPolarity(base, kLPSPI_Pcs0ActiveLow | kLPSPI_Pcs1ActiveLow);
  714. * @endcode
  715. *
  716. * @param base LPSPI peripheral address.
  717. * @param mask The PCS polarity mask; Use the enum _lpspi_pcs_polarity.
  718. */
  719. static inline void LPSPI_SetAllPcsPolarity(LPSPI_Type *base, uint32_t mask)
  720. {
  721. base->CFGR1 = (base->CFGR1 & ~LPSPI_CFGR1_PCSPOL_MASK) | LPSPI_CFGR1_PCSPOL(~mask);
  722. }
  723. /*!
  724. * @brief Configures the frame size.
  725. *
  726. * The minimum frame size is 8-bits and the maximum frame size is 4096-bits. If the frame size is less than or equal
  727. * to 32-bits, the word size and frame size are identical. If the frame size is greater than 32-bits, the word
  728. * size is 32-bits for each word except the last (the last word contains the remainder bits if the frame size is not
  729. * divisible by 32). The minimum word size is 2-bits. A frame size of 33-bits (or similar) is not supported.
  730. *
  731. * Note 1: The transmit command register should be initialized before enabling the LPSPI in slave mode, although
  732. * the command register does not update until after the LPSPI is enabled. After it is enabled, the transmit command
  733. * register
  734. * should only be changed if the LPSPI is idle.
  735. *
  736. * Note 2: The transmit and command FIFO is a combined FIFO that includes both transmit data and command words. That
  737. * means the TCR register should be written to when the Tx FIFO is not full.
  738. *
  739. * @param base LPSPI peripheral address.
  740. * @param frameSize The frame size in number of bits.
  741. */
  742. static inline void LPSPI_SetFrameSize(LPSPI_Type *base, uint32_t frameSize)
  743. {
  744. base->TCR = (base->TCR & ~LPSPI_TCR_FRAMESZ_MASK) | LPSPI_TCR_FRAMESZ(frameSize - 1U);
  745. }
  746. /*!
  747. * @brief Sets the LPSPI baud rate in bits per second.
  748. *
  749. * This function takes in the desired bitsPerSec (baud rate) and calculates the nearest
  750. * possible baud rate without exceeding the desired baud rate and returns the
  751. * calculated baud rate in bits-per-second. It requires the caller to provide
  752. * the frequency of the module source clock (in Hertz). Note that the baud rate
  753. * does not go into effect until the Transmit Control Register (TCR) is programmed
  754. * with the prescale value. Hence, this function returns the prescale tcrPrescaleValue
  755. * parameter for later programming in the TCR. The higher level
  756. * peripheral driver should alert the user of an out of range baud rate input.
  757. *
  758. * Note that the LPSPI module must first be disabled before configuring this.
  759. * Note that the LPSPI module must be configured for master mode before configuring this.
  760. *
  761. * @param base LPSPI peripheral address.
  762. * @param baudRate_Bps The desired baud rate in bits per second.
  763. * @param srcClock_Hz Module source input clock in Hertz.
  764. * @param tcrPrescaleValue The TCR prescale value needed to program the TCR.
  765. * @return The actual calculated baud rate. This function may also return a "0" if the
  766. * LPSPI is not configured for master mode or if the LPSPI module is not disabled.
  767. */
  768. uint32_t LPSPI_MasterSetBaudRate(LPSPI_Type *base,
  769. uint32_t baudRate_Bps,
  770. uint32_t srcClock_Hz,
  771. uint32_t *tcrPrescaleValue);
  772. /*!
  773. * @brief Manually configures a specific LPSPI delay parameter (module must be disabled to
  774. * change the delay values).
  775. *
  776. * This function configures the following:
  777. * SCK to PCS delay, or
  778. * PCS to SCK delay, or
  779. * The configurations must occur between the transfer delay.
  780. *
  781. * The delay names are available in type lpspi_delay_type_t.
  782. *
  783. * The user passes the desired delay along with the delay value.
  784. * This allows the user to directly set the delay values if they have
  785. * pre-calculated them or if they simply wish to manually increment the value.
  786. *
  787. * Note that the LPSPI module must first be disabled before configuring this.
  788. * Note that the LPSPI module must be configured for master mode before configuring this.
  789. *
  790. * @param base LPSPI peripheral address.
  791. * @param scaler The 8-bit delay value 0x00 to 0xFF (255).
  792. * @param whichDelay The desired delay to configure, must be of type lpspi_delay_type_t.
  793. */
  794. void LPSPI_MasterSetDelayScaler(LPSPI_Type *base, uint32_t scaler, lpspi_delay_type_t whichDelay);
  795. /*!
  796. * @brief Calculates the delay based on the desired delay input in nanoseconds (module must be
  797. * disabled to change the delay values).
  798. *
  799. * This function calculates the values for the following:
  800. * SCK to PCS delay, or
  801. * PCS to SCK delay, or
  802. * The configurations must occur between the transfer delay.
  803. *
  804. * The delay names are available in type lpspi_delay_type_t.
  805. *
  806. * The user passes the desired delay and the desired delay value in
  807. * nano-seconds. The function calculates the value needed for the desired delay parameter
  808. * and returns the actual calculated delay because an exact delay match may not be possible. In this
  809. * case, the closest match is calculated without going below the desired delay value input.
  810. * It is possible to input a very large delay value that exceeds the capability of the part, in
  811. * which case the maximum supported delay is returned. It is up to the higher level
  812. * peripheral driver to alert the user of an out of range delay input.
  813. *
  814. * Note that the LPSPI module must be configured for master mode before configuring this. And note that
  815. * the delayTime = LPSPI_clockSource / (PRESCALE * Delay_scaler).
  816. *
  817. * @param base LPSPI peripheral address.
  818. * @param delayTimeInNanoSec The desired delay value in nano-seconds.
  819. * @param whichDelay The desired delay to configuration, which must be of type lpspi_delay_type_t.
  820. * @param srcClock_Hz Module source input clock in Hertz.
  821. * @return actual Calculated delay value in nano-seconds.
  822. */
  823. uint32_t LPSPI_MasterSetDelayTimes(LPSPI_Type *base,
  824. uint32_t delayTimeInNanoSec,
  825. lpspi_delay_type_t whichDelay,
  826. uint32_t srcClock_Hz);
  827. /*!
  828. * @brief Writes data into the transmit data buffer.
  829. *
  830. * This function writes data passed in by the user to the Transmit Data Register (TDR).
  831. * The user can pass up to 32-bits of data to load into the TDR. If the frame size exceeds 32-bits,
  832. * the user has to manage sending the data one 32-bit word at a time.
  833. * Any writes to the TDR result in an immediate push to the transmit FIFO.
  834. * This function can be used for either master or slave modes.
  835. *
  836. * @param base LPSPI peripheral address.
  837. * @param data The data word to be sent.
  838. */
  839. static inline void LPSPI_WriteData(LPSPI_Type *base, uint32_t data)
  840. {
  841. base->TDR = data;
  842. }
  843. /*!
  844. * @brief Reads data from the data buffer.
  845. *
  846. * This function reads the data from the Receive Data Register (RDR).
  847. * This function can be used for either master or slave mode.
  848. *
  849. * @param base LPSPI peripheral address.
  850. * @return The data read from the data buffer.
  851. */
  852. static inline uint32_t LPSPI_ReadData(LPSPI_Type *base)
  853. {
  854. return (base->RDR);
  855. }
  856. /*!
  857. * @brief Set up the dummy data.
  858. *
  859. * @param base LPSPI peripheral address.
  860. * @param dummyData Data to be transferred when tx buffer is NULL.
  861. * Note:
  862. * This API has no effect when LPSPI in slave interrupt mode, because driver
  863. * will set the TXMSK bit to 1 if txData is NULL, no data is loaded from transmit
  864. * FIFO and output pin is tristated.
  865. */
  866. void LPSPI_SetDummyData(LPSPI_Type *base, uint8_t dummyData);
  867. /*!
  868. *@}
  869. */
  870. /*!
  871. * @name Transactional
  872. * @{
  873. */
  874. /*Transactional APIs*/
  875. /*!
  876. * @brief Initializes the LPSPI master handle.
  877. *
  878. * This function initializes the LPSPI handle, which can be used for other LPSPI transactional APIs. Usually, for a
  879. * specified LPSPI instance, call this API once to get the initialized handle.
  880. * @param base LPSPI peripheral address.
  881. * @param handle LPSPI handle pointer to lpspi_master_handle_t.
  882. * @param callback DSPI callback.
  883. * @param userData callback function parameter.
  884. */
  885. void LPSPI_MasterTransferCreateHandle(LPSPI_Type *base,
  886. lpspi_master_handle_t *handle,
  887. lpspi_master_transfer_callback_t callback,
  888. void *userData);
  889. /*!
  890. * @brief LPSPI master transfer data using a polling method.
  891. *
  892. * This function transfers data using a polling method. This is a blocking function, which does not return until all
  893. * transfers have been
  894. * completed.
  895. *
  896. * Note:
  897. * The transfer data size should be integer multiples of bytesPerFrame if bytesPerFrame is less than or equal to 4.
  898. * For bytesPerFrame greater than 4:
  899. * The transfer data size should be equal to bytesPerFrame if the bytesPerFrame is not integer multiples of 4.
  900. * Otherwise, the transfer data size can be an integer multiple of bytesPerFrame.
  901. *
  902. * @param base LPSPI peripheral address.
  903. * @param transfer pointer to lpspi_transfer_t structure.
  904. * @return status of status_t.
  905. */
  906. status_t LPSPI_MasterTransferBlocking(LPSPI_Type *base, lpspi_transfer_t *transfer);
  907. /*!
  908. * @brief LPSPI master transfer data using an interrupt method.
  909. *
  910. * This function transfers data using an interrupt method. This is a non-blocking function, which returns right away.
  911. * When all data is transferred, the callback function is called.
  912. *
  913. * Note:
  914. * The transfer data size should be integer multiples of bytesPerFrame if bytesPerFrame is less than or equal to 4.
  915. * For bytesPerFrame greater than 4:
  916. * The transfer data size should be equal to bytesPerFrame if the bytesPerFrame is not integer multiples of 4.
  917. * Otherwise, the transfer data size can be an integer multiple of bytesPerFrame.
  918. *
  919. * @param base LPSPI peripheral address.
  920. * @param handle pointer to lpspi_master_handle_t structure which stores the transfer state.
  921. * @param transfer pointer to lpspi_transfer_t structure.
  922. * @return status of status_t.
  923. */
  924. status_t LPSPI_MasterTransferNonBlocking(LPSPI_Type *base, lpspi_master_handle_t *handle, lpspi_transfer_t *transfer);
  925. /*!
  926. * @brief Gets the master transfer remaining bytes.
  927. *
  928. * This function gets the master transfer remaining bytes.
  929. *
  930. * @param base LPSPI peripheral address.
  931. * @param handle pointer to lpspi_master_handle_t structure which stores the transfer state.
  932. * @param count Number of bytes transferred so far by the non-blocking transaction.
  933. * @return status of status_t.
  934. */
  935. status_t LPSPI_MasterTransferGetCount(LPSPI_Type *base, lpspi_master_handle_t *handle, size_t *count);
  936. /*!
  937. * @brief LPSPI master abort transfer which uses an interrupt method.
  938. *
  939. * This function aborts a transfer which uses an interrupt method.
  940. *
  941. * @param base LPSPI peripheral address.
  942. * @param handle pointer to lpspi_master_handle_t structure which stores the transfer state.
  943. */
  944. void LPSPI_MasterTransferAbort(LPSPI_Type *base, lpspi_master_handle_t *handle);
  945. /*!
  946. * @brief LPSPI Master IRQ handler function.
  947. *
  948. * This function processes the LPSPI transmit and receive IRQ.
  949. *
  950. * @param base LPSPI peripheral address.
  951. * @param handle pointer to lpspi_master_handle_t structure which stores the transfer state.
  952. */
  953. void LPSPI_MasterTransferHandleIRQ(LPSPI_Type *base, lpspi_master_handle_t *handle);
  954. /*!
  955. * @brief Initializes the LPSPI slave handle.
  956. *
  957. * This function initializes the LPSPI handle, which can be used for other LPSPI transactional APIs. Usually, for a
  958. * specified LPSPI instance, call this API once to get the initialized handle.
  959. *
  960. * @param base LPSPI peripheral address.
  961. * @param handle LPSPI handle pointer to lpspi_slave_handle_t.
  962. * @param callback DSPI callback.
  963. * @param userData callback function parameter.
  964. */
  965. void LPSPI_SlaveTransferCreateHandle(LPSPI_Type *base,
  966. lpspi_slave_handle_t *handle,
  967. lpspi_slave_transfer_callback_t callback,
  968. void *userData);
  969. /*!
  970. * @brief LPSPI slave transfer data using an interrupt method.
  971. *
  972. * This function transfer data using an interrupt method. This is a non-blocking function, which returns right away.
  973. * When all data is transferred, the callback function is called.
  974. *
  975. * Note:
  976. * The transfer data size should be integer multiples of bytesPerFrame if bytesPerFrame is less than or equal to 4.
  977. * For bytesPerFrame greater than 4:
  978. * The transfer data size should be equal to bytesPerFrame if the bytesPerFrame is not an integer multiple of 4.
  979. * Otherwise, the transfer data size can be an integer multiple of bytesPerFrame.
  980. *
  981. * @param base LPSPI peripheral address.
  982. * @param handle pointer to lpspi_slave_handle_t structure which stores the transfer state.
  983. * @param transfer pointer to lpspi_transfer_t structure.
  984. * @return status of status_t.
  985. */
  986. status_t LPSPI_SlaveTransferNonBlocking(LPSPI_Type *base, lpspi_slave_handle_t *handle, lpspi_transfer_t *transfer);
  987. /*!
  988. * @brief Gets the slave transfer remaining bytes.
  989. *
  990. * This function gets the slave transfer remaining bytes.
  991. *
  992. * @param base LPSPI peripheral address.
  993. * @param handle pointer to lpspi_slave_handle_t structure which stores the transfer state.
  994. * @param count Number of bytes transferred so far by the non-blocking transaction.
  995. * @return status of status_t.
  996. */
  997. status_t LPSPI_SlaveTransferGetCount(LPSPI_Type *base, lpspi_slave_handle_t *handle, size_t *count);
  998. /*!
  999. * @brief LPSPI slave aborts a transfer which uses an interrupt method.
  1000. *
  1001. * This function aborts a transfer which uses an interrupt method.
  1002. *
  1003. * @param base LPSPI peripheral address.
  1004. * @param handle pointer to lpspi_slave_handle_t structure which stores the transfer state.
  1005. */
  1006. void LPSPI_SlaveTransferAbort(LPSPI_Type *base, lpspi_slave_handle_t *handle);
  1007. /*!
  1008. * @brief LPSPI Slave IRQ handler function.
  1009. *
  1010. * This function processes the LPSPI transmit and receives an IRQ.
  1011. *
  1012. * @param base LPSPI peripheral address.
  1013. * @param handle pointer to lpspi_slave_handle_t structure which stores the transfer state.
  1014. */
  1015. void LPSPI_SlaveTransferHandleIRQ(LPSPI_Type *base, lpspi_slave_handle_t *handle);
  1016. /*!
  1017. *@}
  1018. */
  1019. #if defined(__cplusplus)
  1020. }
  1021. #endif
  1022. /*! @}*/
  1023. #endif /*_FSL_LPSPI_H_*/