fsl_lpspi.h 45 KB

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