1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
10 *
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef __ECSPI_H__
32 #define __ECSPI_H__
33
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include <assert.h>
37 #include "device_imx.h"
38
39 /*!
40 * @addtogroup ecspi_driver
41 * @{
42 */
43
44 /*******************************************************************************
45 * Definitions
46 ******************************************************************************/
47
48 /*! @brief Channel select. */
49 enum _ecspi_channel_select
50 {
51 ecspiSelectChannel0 = 0U, /*!< Select Channel 0. Chip Select 0 (SS0) is asserted.*/
52 ecspiSelectChannel1 = 1U, /*!< Select Channel 1. Chip Select 1 (SS1) is asserted.*/
53 ecspiSelectChannel2 = 2U, /*!< Select Channel 2. Chip Select 2 (SS2) is asserted.*/
54 ecspiSelectChannel3 = 3U, /*!< Select Channel 3. Chip Select 3 (SS3) is asserted.*/
55 };
56
57 /*! @brief Channel mode. */
58 enum _ecspi_master_slave_mode
59 {
60 ecspiSlaveMode = 0U, /*!< Set Slave Mode.*/
61 ecspiMasterMode = 1U, /*!< Set Master Mode.*/
62 };
63
64 /*! @brief Clock phase. */
65 enum _ecspi_clock_phase
66 {
67 ecspiClockPhaseFirstEdge = 0U, /*!< Data is captured on the leading edge of the SCK and
68 changed on the following edge.*/
69 ecspiClockPhaseSecondEdge = 1U, /*!< Data is changed on the leading edge of the SCK and
70 captured on the following edge.*/
71 };
72
73 /*! @brief Clock polarity. */
74 enum _ecspi_clock_polarity
75 {
76 ecspiClockPolarityActiveHigh = 0U, /*!< Active-high eCSPI clock (idles low).*/
77 ecspiClockPolarityActiveLow = 1U, /*!< Active-low eCSPI clock (idles high).*/
78 };
79
80 /*! @brief SS signal polarity. */
81 enum _ecspi_ss_polarity
82 {
83 ecspiSSPolarityActiveLow = 0U, /*!< Active-low, eCSPI SS signal.*/
84 ecspiSSPolarityActiveHigh = 1U, /*!< Active-high, eCSPI SS signal.*/
85 };
86
87 /*! @brief Inactive state of data line. */
88 enum _ecspi_dataline_inactivestate
89 {
90 ecspiDataLineStayHigh = 0U, /*!< Data line inactive state stay high.*/
91 ecspiDataLineStayLow = 1U, /*!< Data line inactive state stay low.*/
92 };
93
94 /*! @brief Inactive state of SCLK. */
95 enum _ecspi_sclk_inactivestate
96 {
97 ecspiSclkStayLow = 0U, /*!< SCLK inactive state stay low.*/
98 ecspiSclkStayHigh = 1U, /*!< SCLK line inactive state stay high.*/
99 };
100
101 /*! @brief sample period counter clock source. */
102 enum _ecspi_sampleperiod_clocksource
103 {
104 ecspiSclk = 0U, /*!< Sample period counter clock from SCLK.*/
105 ecspiLowFreq32K = 1U, /*!< Sample period counter clock from from LFRC (32.768 KHz).*/
106 };
107
108 /*! @brief DMA Source definition. */
109 enum _ecspi_dma_source
110 {
111 ecspiDmaTxfifoEmpty = 7U, /*!< TXFIFO Empty DMA Request.*/
112 ecspiDmaRxfifoRequest = 23U, /*!< RXFIFO DMA Request.*/
113 ecspiDmaRxfifoTail = 31U, /*!< RXFIFO TAIL DMA Request.*/
114 };
115
116 /*! @brief RXFIFO and TXFIFO threshold. */
117 enum _ecspi_fifothreshold
118 {
119 ecspiTxfifoThreshold = 0U, /*!< Defines the FIFO threshold that triggers a TX DMA/INT request.*/
120 ecspiRxfifoThreshold = 16U, /*!< defines the FIFO threshold that triggers a RX DMA/INT request.*/
121 };
122
123 /*! @brief Status flag. */
124 enum _ecspi_status_flag
125 {
126 ecspiFlagTxfifoEmpty = 1U << 0, /*!< TXFIFO Empty Flag.*/
127 ecspiFlagTxfifoDataRequest = 1U << 1, /*!< TXFIFO Data Request Flag.*/
128 ecspiFlagTxfifoFull = 1U << 2, /*!< TXFIFO Full Flag.*/
129 ecspiFlagRxfifoReady = 1U << 3, /*!< RXFIFO Ready Flag.*/
130 ecspiFlagRxfifoDataRequest = 1U << 4, /*!< RXFIFO Data Request Flag.*/
131 ecspiFlagRxfifoFull = 1U << 5, /*!< RXFIFO Full Flag.*/
132 ecspiFlagRxfifoOverflow = 1U << 6, /*!< RXFIFO Overflow Flag.*/
133 ecspiFlagTxfifoTc = 1U << 7, /*!< TXFIFO Transform Completed Flag.*/
134 };
135
136 /*! @brief Data Ready Control. */
137 enum _ecspi_data_ready
138 {
139 ecspiRdyNoCare = 0U, /*!< The SPI_RDY signal is ignored.*/
140 ecspiRdyFallEdgeTrig = 1U, /*!< Burst is triggered by the falling edge of the SPI_RDY signal (edge-triggered).*/
141 ecspiRdyLowLevelTrig = 2U, /*!< Burst is triggered by a low level of the SPI_RDY signal (level-triggered).*/
142 ecspiRdyReserved = 3U, /*!< Reserved.*/
143 };
144
145 /*! @brief Init structure. */
146 typedef struct _ecspi_init_config
147 {
148 uint32_t clockRate; /*!< Specifies ECSPII module clock freq.*/
149 uint32_t baudRate; /*!< Specifies desired eCSPI baud rate.*/
150 uint32_t channelSelect; /*!< Specifies the channel select.*/
151 uint32_t mode; /*!< Specifies the mode.*/
152 uint32_t burstLength; /*!< Specifies the length of a burst to be transferred.*/
153 uint32_t clockPhase; /*!< Specifies the clock phase.*/
154 uint32_t clockPolarity; /*!< Specifies the clock polarity.*/
155 bool ecspiAutoStart; /*!< Specifies the start mode.*/
156 } ecspi_init_config_t;
157
158 /*******************************************************************************
159 * API
160 ******************************************************************************/
161
162 #if defined(__cplusplus)
163 extern "C" {
164 #endif
165
166 /*!
167 * @name eCSPI Initialization and Configuration functions
168 * @{
169 */
170
171 /*!
172 * @brief Initializes the eCSPI module.
173 *
174 * @param base eCSPI base pointer.
175 * @param initConfig eCSPI initialization structure.
176 */
177 void ECSPI_Init(ECSPI_Type* base, const ecspi_init_config_t* initConfig);
178
179 /*!
180 * @brief Enables the specified eCSPI module.
181 *
182 * @param base eCSPI base pointer.
183 */
ECSPI_Enable(ECSPI_Type * base)184 static inline void ECSPI_Enable(ECSPI_Type* base)
185 {
186 /* Enable the eCSPI. */
187 ECSPI_CONREG_REG(base) |= ECSPI_CONREG_EN_MASK;
188 }
189
190 /*!
191 * @brief Disable the specified eCSPI module.
192 *
193 * @param base eCSPI base pointer.
194 */
ECSPI_Disable(ECSPI_Type * base)195 static inline void ECSPI_Disable(ECSPI_Type* base)
196 {
197 /* Enable the eCSPI. */
198 ECSPI_CONREG_REG(base) &= ~ECSPI_CONREG_EN_MASK;
199 }
200
201 /*!
202 * @brief Insert the number of wait states to be inserted in data transfers.
203 *
204 * @param base eCSPI base pointer.
205 * @param number the number of wait states.
206 */
ECSPI_InsertWaitState(ECSPI_Type * base,uint32_t number)207 static inline void ECSPI_InsertWaitState(ECSPI_Type* base, uint32_t number)
208 {
209 /* Configure the number of wait states inserted. */
210 ECSPI_PERIODREG_REG(base) = (ECSPI_PERIODREG_REG(base) & (~ECSPI_PERIODREG_SAMPLE_PERIOD_MASK)) |
211 ECSPI_PERIODREG_SAMPLE_PERIOD(number);
212 }
213
214 /*!
215 * @brief Set the clock source for the sample period counter.
216 *
217 * @param base eCSPI base pointer.
218 * @param source The clock source (see @ref _ecspi_sampleperiod_clocksource enumeration).
219 */
220 void ECSPI_SetSampClockSource(ECSPI_Type* base, uint32_t source);
221
222 /*!
223 * @brief Set the eCSPI clocks insert between the chip select active edge
224 * and the first eCSPI clock edge.
225 *
226 * @param base eCSPI base pointer.
227 * @param delay The number of wait states.
228 */
ECSPI_SetDelay(ECSPI_Type * base,uint32_t delay)229 static inline void ECSPI_SetDelay(ECSPI_Type* base, uint32_t delay)
230 {
231 /* Set the number of clocks insert. */
232 ECSPI_PERIODREG_REG(base) = (ECSPI_PERIODREG_REG(base) & (~ECSPI_PERIODREG_CSD_CTL_MASK)) |
233 ECSPI_PERIODREG_CSD_CTL(delay);
234 }
235
236 /*!
237 * @brief Set the inactive state of SCLK.
238 *
239 * @param base eCSPI base pointer.
240 * @param channel eCSPI channel select (see @ref _ecspi_channel_select enumeration).
241 * @param state SCLK inactive state (see @ref _ecspi_sclk_inactivestate enumeration).
242 */
ECSPI_SetSCLKInactiveState(ECSPI_Type * base,uint32_t channel,uint32_t state)243 static inline void ECSPI_SetSCLKInactiveState(ECSPI_Type* base, uint32_t channel, uint32_t state)
244 {
245 /* Configure the inactive state of SCLK. */
246 ECSPI_CONFIGREG_REG(base) = (ECSPI_CONFIGREG_REG(base) & (~ECSPI_CONFIGREG_SCLK_CTL(1 << channel))) |
247 ECSPI_CONFIGREG_SCLK_CTL((state & 1) << channel);
248 }
249
250 /*!
251 * @brief Set the inactive state of data line.
252 *
253 * @param base eCSPI base pointer.
254 * @param channel eCSPI channel select (see @ref _ecspi_channel_select enumeration).
255 * @param state Data line inactive state (see @ref _ecspi_dataline_inactivestate enumeration).
256 */
ECSPI_SetDataInactiveState(ECSPI_Type * base,uint32_t channel,uint32_t state)257 static inline void ECSPI_SetDataInactiveState(ECSPI_Type* base, uint32_t channel, uint32_t state)
258 {
259 /* Set the inactive state of Data Line. */
260 ECSPI_CONFIGREG_REG(base) = (ECSPI_CONFIGREG_REG(base) & (~ECSPI_CONFIGREG_DATA_CTL(1 << channel))) |
261 ECSPI_CONFIGREG_DATA_CTL((state & 1) << channel);
262 }
263
264 /*!
265 * @brief Trigger a burst.
266 *
267 * @param base eCSPI base pointer.
268 */
ECSPI_StartBurst(ECSPI_Type * base)269 static inline void ECSPI_StartBurst(ECSPI_Type* base)
270 {
271 /* Start a burst. */
272 ECSPI_CONREG_REG(base) |= ECSPI_CONREG_XCH_MASK;
273 }
274
275 /*!
276 * @brief Set the burst length.
277 *
278 * @param base eCSPI base pointer.
279 * @param length The value of burst length.
280 */
ECSPI_SetBurstLength(ECSPI_Type * base,uint32_t length)281 static inline void ECSPI_SetBurstLength(ECSPI_Type* base, uint32_t length)
282 {
283 /* Set the burst length according to length. */
284 ECSPI_CONREG_REG(base) = (ECSPI_CONREG_REG(base) & (~ECSPI_CONREG_BURST_LENGTH_MASK)) |
285 ECSPI_CONREG_BURST_LENGTH(length);
286 }
287
288 /*!
289 * @brief Set eCSPI SS Wave Form.
290 *
291 * @param base eCSPI base pointer.
292 * @param channel eCSPI channel selected (see @ref _ecspi_channel_select enumeration).
293 * @param ssMultiBurst For master mode, set true for multiple burst and false for one burst.
294 * For slave mode, set true to complete burst by SS signal edges and false to complete
295 * burst by number of bits received.
296 */
ECSPI_SetSSMultipleBurst(ECSPI_Type * base,uint32_t channel,bool ssMultiBurst)297 static inline void ECSPI_SetSSMultipleBurst(ECSPI_Type* base, uint32_t channel, bool ssMultiBurst)
298 {
299 /* Set the SS wave form. */
300 ECSPI_CONFIGREG_REG(base) = (ECSPI_CONFIGREG_REG(base) & (~ECSPI_CONFIGREG_SS_CTL(1 << channel))) |
301 ECSPI_CONFIGREG_SS_CTL(ssMultiBurst << channel);
302 }
303
304 /*!
305 * @brief Set eCSPI SS Polarity.
306 *
307 * @param base eCSPI base pointer.
308 * @param channel eCSPI channel selected (see @ref _ecspi_channel_select enumeration).
309 * @param polarity Set SS signal active logic (see @ref _ecspi_ss_polarity enumeration).
310 */
ECSPI_SetSSPolarity(ECSPI_Type * base,uint32_t channel,uint32_t polarity)311 static inline void ECSPI_SetSSPolarity(ECSPI_Type* base, uint32_t channel, uint32_t polarity)
312 {
313 /* Set the SS polarity. */
314 ECSPI_CONFIGREG_REG(base) = (ECSPI_CONFIGREG_REG(base) & (~ECSPI_CONFIGREG_SS_POL(1 << channel))) |
315 ECSPI_CONFIGREG_SS_POL(polarity << channel);
316 }
317
318 /*!
319 * @brief Set the Data Ready Control.
320 *
321 * @param base eCSPI base pointer.
322 * @param spidataready eCSPI data ready control (see @ref _ecspi_data_ready enumeration).
323 */
ECSPI_SetSPIDataReady(ECSPI_Type * base,uint32_t spidataready)324 static inline void ECSPI_SetSPIDataReady(ECSPI_Type* base, uint32_t spidataready)
325 {
326 /* Set the Data Ready Control. */
327 ECSPI_CONREG_REG(base) = (ECSPI_CONREG_REG(base) & (~ECSPI_CONREG_DRCTL_MASK)) |
328 ECSPI_CONREG_DRCTL(spidataready);
329 }
330
331 /*!
332 * @brief Calculated the eCSPI baud rate in bits per second.
333 * The calculated baud rate must not exceed the desired baud rate.
334 *
335 * @param base eCSPI base pointer.
336 * @param sourceClockInHz eCSPI Clock(SCLK) (in Hz).
337 * @param bitsPerSec the value of Baud Rate.
338 * @return The calculated baud rate in bits-per-second, the nearest possible
339 * baud rate without exceeding the desired baud rate.
340 */
341 uint32_t ECSPI_SetBaudRate(ECSPI_Type* base, uint32_t sourceClockInHz, uint32_t bitsPerSec);
342
343 /*@}*/
344
345 /*!
346 * @name Data transfers functions
347 * @{
348 */
349
350 /*!
351 * @brief Transmits a data to TXFIFO.
352 *
353 * @param base eCSPI base pointer.
354 * @param data Data to be transmitted.
355 */
ECSPI_SendData(ECSPI_Type * base,uint32_t data)356 static inline void ECSPI_SendData(ECSPI_Type* base, uint32_t data)
357 {
358 /* Write data to Transmit Data Register. */
359 ECSPI_TXDATA_REG(base) = data;
360 }
361
362 /*!
363 * @brief Receives a data from RXFIFO.
364 *
365 * @param base eCSPI base pointer.
366 * @return The value of received data.
367 */
ECSPI_ReceiveData(ECSPI_Type * base)368 static inline uint32_t ECSPI_ReceiveData(ECSPI_Type* base)
369 {
370 /* Read data from Receive Data Register. */
371 return ECSPI_RXDATA_REG(base);
372 }
373
374 /*!
375 * @brief Read the number of words in the RXFIFO.
376 *
377 * @param base eCSPI base pointer.
378 * @return The number of words in the RXFIFO.
379 */
ECSPI_GetRxfifoCounter(ECSPI_Type * base)380 static inline uint32_t ECSPI_GetRxfifoCounter(ECSPI_Type* base)
381 {
382 /* Get the number of words in the RXFIFO. */
383 return ((ECSPI_TESTREG_REG(base) & ECSPI_TESTREG_RXCNT_MASK) >> ECSPI_TESTREG_RXCNT_SHIFT);
384 }
385
386 /*!
387 * @brief Read the number of words in the TXFIFO.
388 *
389 * @param base eCSPI base pointer.
390 * @return The number of words in the TXFIFO.
391 */
ECSPI_GetTxfifoCounter(ECSPI_Type * base)392 static inline uint32_t ECSPI_GetTxfifoCounter(ECSPI_Type* base)
393 {
394 /* Get the number of words in the RXFIFO. */
395 return ((ECSPI_TESTREG_REG(base) & ECSPI_TESTREG_TXCNT_MASK) >> ECSPI_TESTREG_TXCNT_SHIFT);
396 }
397
398 /*@}*/
399
400 /*!
401 * @name DMA management functions
402 * @{
403 */
404
405 /*!
406 * @brief Enable or disable the specified DMA Source.
407 *
408 * @param base eCSPI base pointer.
409 * @param source specifies DMA source (see @ref _ecspi_dma_source enumeration).
410 * @param enable Enable/Disable specified DMA Source.
411 * - true: Enable specified DMA Source.
412 * - false: Disable specified DMA Source.
413 */
414 void ECSPI_SetDMACmd(ECSPI_Type* base, uint32_t source, bool enable);
415
416 /*!
417 * @brief Set the burst length of a DMA operation.
418 *
419 * @param base eCSPI base pointer.
420 * @param length Specifies the burst length of a DMA operation.
421 */
ECSPI_SetDMABurstLength(ECSPI_Type * base,uint32_t length)422 static inline void ECSPI_SetDMABurstLength(ECSPI_Type* base, uint32_t length)
423 {
424 /* Configure the burst length of a DMA operation. */
425 ECSPI_DMAREG_REG(base) = (ECSPI_DMAREG_REG(base) & (~ECSPI_DMAREG_RX_DMA_LENGTH_MASK)) |
426 ECSPI_DMAREG_RX_DMA_LENGTH(length);
427 }
428
429 /*!
430 * @brief Set the RXFIFO or TXFIFO threshold.
431 *
432 * @param base eCSPI base pointer.
433 * @param fifo Data transfer FIFO (see @ref _ecspi_fifothreshold enumeration).
434 * @param threshold Threshold value.
435 */
436 void ECSPI_SetFIFOThreshold(ECSPI_Type* base, uint32_t fifo, uint32_t threshold);
437
438 /*@}*/
439
440 /*!
441 * @name Interrupts and flags management functions
442 * @{
443 */
444
445 /*!
446 * @brief Enable or disable the specified eCSPI interrupts.
447 *
448 * @param base eCSPI base pointer.
449 * @param flags eCSPI status flag mask (see @ref _ecspi_status_flag for bit definition).
450 * @param enable Interrupt enable.
451 * - true: Enable specified eCSPI interrupts.
452 * - false: Disable specified eCSPI interrupts.
453 */
454 void ECSPI_SetIntCmd(ECSPI_Type* base, uint32_t flags, bool enable);
455
456 /*!
457 * @brief Checks whether the specified eCSPI flag is set or not.
458 *
459 * @param base eCSPI base pointer.
460 * @param flags eCSPI status flag mask (see @ref _ecspi_status_flag for bit definition).
461 * @return eCSPI status, each bit represents one status flag.
462 */
ECSPI_GetStatusFlag(ECSPI_Type * base,uint32_t flags)463 static inline uint32_t ECSPI_GetStatusFlag(ECSPI_Type* base, uint32_t flags)
464 {
465 /* return the vale of eCSPI status. */
466 return ECSPI_STATREG_REG(base) & flags;
467 }
468
469 /*!
470 * @brief Clear one or more eCSPI status flag.
471 *
472 * @param base eCSPI base pointer.
473 * @param flags eCSPI status flag mask (see @ref _ecspi_status_flag for bit definition).
474 */
ECSPI_ClearStatusFlag(ECSPI_Type * base,uint32_t flags)475 static inline void ECSPI_ClearStatusFlag(ECSPI_Type* base, uint32_t flags)
476 {
477 /* Write 1 to the status bit. */
478 ECSPI_STATREG_REG(base) = flags;
479 }
480
481 /*@}*/
482
483 #if defined(__cplusplus)
484 }
485 #endif
486
487 /*! @} */
488
489 #endif /*__ECSPI_H__*/
490
491 /*******************************************************************************
492 * EOF
493 ******************************************************************************/
494