1 /******************************************************************************
2  *  Filename:       spi.h
3  *
4  *  Description:    Defines and prototypes for the Serial Peripheral Interface (SPI).
5  *
6  *  Copyright (c) 2023 Texas Instruments Incorporated
7  *
8  *  Redistribution and use in source and binary forms, with or without
9  *  modification, are permitted provided that the following conditions are met:
10  *
11  *  1) Redistributions of source code must retain the above copyright notice,
12  *     this list of conditions and the following disclaimer.
13  *
14  *  2) Redistributions in binary form must reproduce the above copyright notice,
15  *     this list of conditions and the following disclaimer in the documentation
16  *     and/or other materials provided with the distribution.
17  *
18  *  3) 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  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  *  POSSIBILITY OF SUCH DAMAGE.
33  *
34  ******************************************************************************/
35 
36 #ifndef __SPI_H__
37 #define __SPI_H__
38 
39 //*****************************************************************************
40 //
41 //! \addtogroup peripheral_group
42 //! @{
43 //! \addtogroup spi_api
44 //! @{
45 //
46 //*****************************************************************************
47 
48 //*****************************************************************************
49 //
50 // If building with a C++ compiler, make all of the definitions in this header
51 // have a C binding.
52 //
53 //*****************************************************************************
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 #include <stdbool.h>
59 #include <stdint.h>
60 #include "../inc/hw_ints.h"
61 #include "../inc/hw_memmap.h"
62 #include "../inc/hw_types.h"
63 #include "../inc/hw_spi.h"
64 #include "debug.h"
65 #include "interrupt.h"
66 
67 //*****************************************************************************
68 //
69 // Values that can be passed to SPIEnableInt(), SPIDisableInt(), and SPIClearInt()
70 // as the intFlags parameter, and returned by SPIIntStatus().
71 //
72 //*****************************************************************************
73 #define SPI_DMA_DONE_TX SPI_IMASK_DMATX   //!< DMA Done 1 event for TX event mask
74 #define SPI_DMA_DONE_RX SPI_IMASK_DMARX   //!< DMA Done 1 event for RX event mask
75 #define SPI_IDLE        SPI_IMASK_IDLE    //!< SPI Idle event mask
76 #define SPI_TXEMPTY     SPI_IMASK_TXEMPTY //!< Transmit FIFO Empty event mask
77 #define SPI_TX          SPI_IMASK_TX      //!< Transmit FIFO event mask
78 #define SPI_RX \
79     SPI_IMASK_RX //!< Receive FIFO event. This interrupt is set if the selected Receive FIFO level has been reached
80 #define SPI_RTOUT      SPI_IMASK_RTOUT //!< Enable SPI Receive Time-Out event mask
81 #define SPI_PER        SPI_IMASK_PER   //!< Parity error event mask
82 #define SPI_RXFIFO_OVF SPI_IMASK_RXOVF //!< RXFIFO overflow event mask
83 
84 //*****************************************************************************
85 //
86 // Values that are returned from SPIStatus
87 //
88 //*****************************************************************************
89 #define SPI_BUSY        SPI_STA_BUSY_ACTIVE  //!< Busy
90 #define SPI_RX_NOT_FULL SPI_STA_RNF_NOT_FULL //!< Receive FIFO not full
91 #define SPI_RX_EMPTY    SPI_STA_RFE_EMPTY    //!< Receive FIFO empty
92 #define SPI_TX_NOT_FULL SPI_STA_TNF_NOT_FULL //!< Transmit FIFO not full
93 #define SPI_TX_EMPTY    SPI_STA_TFE_EMPTY    //!< Transmit FIFO empty
94 #define SPI_STATUS_MASK 0x0000001F           //!< Mask for bits above
95 
96 //*****************************************************************************
97 //
98 // Values that can be passed to SPIConfigSetExpClk
99 //
100 //*****************************************************************************
101 //! Motorola format 3-wire, polarity 0, phase 0
102 #define SPI_FRF_MOTO_MODE_0 (SPI_CTL0_FRF_MOTOROLA_3WIRE | SPI_CTL0_SPO_LO | SPI_CTL0_SPH_FIRST)
103 //! Motorola format 3-wire, polarity 0, phase 1
104 #define SPI_FRF_MOTO_MODE_1 (SPI_CTL0_FRF_MOTOROLA_3WIRE | SPI_CTL0_SPO_LO | SPI_CTL0_SPH_SECOND)
105 //! Motorola format 3-wire, polarity 1, phase 0
106 #define SPI_FRF_MOTO_MODE_2 (SPI_CTL0_FRF_MOTOROLA_3WIRE | SPI_CTL0_SPO_HI | SPI_CTL0_SPH_FIRST)
107 //! Motorola format 3-wire, polarity 1, phase 1
108 #define SPI_FRF_MOTO_MODE_3 (SPI_CTL0_FRF_MOTOROLA_3WIRE | SPI_CTL0_SPO_HI | SPI_CTL0_SPH_SECOND)
109 //! Motorola format 4-wire, polarity 0, phase 0
110 #define SPI_FRF_MOTO_MODE_4 (SPI_CTL0_FRF_MOTOROLA_4WIRE | SPI_CTL0_SPO_LO | SPI_CTL0_SPH_FIRST)
111 //! Motorola format 4-wire, polarity 0, phase 1
112 #define SPI_FRF_MOTO_MODE_5 (SPI_CTL0_FRF_MOTOROLA_4WIRE | SPI_CTL0_SPO_LO | SPI_CTL0_SPH_SECOND)
113 //! Motorola format 4-wire, polarity 1, phase 0
114 #define SPI_FRF_MOTO_MODE_6 (SPI_CTL0_FRF_MOTOROLA_4WIRE | SPI_CTL0_SPO_HI | SPI_CTL0_SPH_FIRST)
115 //! Motorola format 4-wire, polarity 1, phase 1
116 #define SPI_FRF_MOTO_MODE_7 (SPI_CTL0_FRF_MOTOROLA_4WIRE | SPI_CTL0_SPO_HI | SPI_CTL0_SPH_SECOND)
117 
118 #define SPI_FRF_TI  SPI_CTL0_FRF_TI_SYNC   //!< TI Sync frame format
119 #define SPI_FRF_NMW SPI_CTL0_FRF_MIRCOWIRE //!< MicroWire frame format
120 
121 #define SPI_MODE_CONTROLLER SPI_CTL1_MS_CONTROLLER //!< SPI controller
122 #define SPI_MODE_PERIPHERAL SPI_CTL1_MS_PERIPHERAL //!< SPI peripheral
123 #define SPI_MODE_PERIPHERAL_OD \
124     SPI_CTL1_SOD_ENABLE //!< SPI peripheral with POCI
125                         //!< output disabled
126 
127 //*****************************************************************************
128 //
129 // Values that can be passed to SPIEnableDMA() and SPIDisableDMA()
130 //
131 //*****************************************************************************
132 #define SPI_DMA_TX SPI_DMACR_TXEN //!< Enable DMA for transmit
133 #define SPI_DMA_RX SPI_DMACR_RXEN //!< Enable DMA for receive
134 
135 //*****************************************************************************
136 //
137 // API Functions and prototypes
138 //
139 //*****************************************************************************
140 
141 #ifdef DRIVERLIB_DEBUG
142 //*****************************************************************************
143 //
144 //! \internal
145 //!
146 //! \brief Checks an SPI base address.
147 //!
148 //! This function determines if an SPI module base address is valid.
149 //!
150 //! \param base specifies the SPI module base address.
151 //!
152 //! \return Returns \c true if the base address is valid and \c false
153 //! otherwise.
154 //
155 //*****************************************************************************
SPIBaseValid(uint32_t base)156 static bool SPIBaseValid(uint32_t base)
157 {
158     return (base == SPI0_BASE);
159 }
160 #endif
161 
162 //*****************************************************************************
163 //
164 //! \brief Configures the serial peripheral port.
165 //!
166 //! This function configures the serial peripheral port.  It sets
167 //! the SPI protocol, mode of operation, bit rate, and data width.
168 //!
169 //! The \c protocol parameter defines the data frame format. The Motorola
170 //! frame formats imply the following polarity and phase configurations:
171 //!
172 //! <pre>
173 //! Polarity Phase   Motorola Protocol                  Mode
174 //!   0       0           3-wire                SPI_FRF_MOTO_MODE_0
175 //!   0       1           3-wire                SPI_FRF_MOTO_MODE_1
176 //!   1       0           3-wire                SPI_FRF_MOTO_MODE_2
177 //!   1       1           3-wire                SPI_FRF_MOTO_MODE_3
178 //!   0       0           4-wire                SPI_FRF_MOTO_MODE_4
179 //!   0       1           4-wire                SPI_FRF_MOTO_MODE_5
180 //!   1       0           4-wire                SPI_FRF_MOTO_MODE_6
181 //!   1       1           4-wire                SPI_FRF_MOTO_MODE_7
182 //! </pre>
183 //!
184 //! The \c mode parameter defines the operating mode of the SPI module.
185 //! The SPI module can operate as a controller or peripheral; if a peripheral, the SPI can be
186 //! configured to disable output on its serial output line.
187 //!
188 //! The \c bitRate parameter defines the bit rate for the SPI.
189 //!
190 //! The \c dataWidth parameter defines the width of the data transfers, and
191 //! can be a value between 4 and 32, for controller mode, and 7 to 32, in peripheral mode.
192 //!
193 //! \note The peripheral clock is not necessarily the same as the processor clock.
194 //! The frequency of the peripheral clock is set by the system control.
195 //!
196 //! \param base specifies the SPI module base address.
197 //! \param spiClk is the rate of the clock supplied to the SPI module.
198 //! \param protocol specifies the data transfer protocol.
199 //! The parameter can be one of the following values:
200 //! - \ref SPI_FRF_MOTO_MODE_0
201 //! - \ref SPI_FRF_MOTO_MODE_1
202 //! - \ref SPI_FRF_MOTO_MODE_2
203 //! - \ref SPI_FRF_MOTO_MODE_3
204 //! - \ref SPI_FRF_MOTO_MODE_4
205 //! - \ref SPI_FRF_MOTO_MODE_5
206 //! - \ref SPI_FRF_MOTO_MODE_6
207 //! - \ref SPI_FRF_MOTO_MODE_7
208 //! - \ref SPI_FRF_TI
209 //! - \ref SPI_FRF_NMW.
210 //! \param mode specifies the mode of operation.
211 //! The parameter can be one of the following values:
212 //! - \ref SPI_MODE_CONTROLLER
213 //! - \ref SPI_MODE_PERIPHERAL
214 //! - \ref SPI_MODE_PERIPHERAL_OD
215 //! \param bitRate specifies the clock rate.
216 //! \param dataWidth specifies number of bits transferred per frame.
217 //! Must be a value between 4 and 16 for controller mode, and 7 to 16 in peripheral mode.
218 //!
219 //! \return None
220 //
221 //*****************************************************************************
222 extern void SPIConfigSetExpClk(uint32_t base,
223                                uint32_t spiClk,
224                                uint32_t protocol,
225                                uint32_t mode,
226                                uint32_t bitRate,
227                                uint32_t dataWidth);
228 
229 //*****************************************************************************
230 //
231 //! \brief Enables the serial peripheral port.
232 //!
233 //! This function enables operation of the serial peripheral port.  The
234 //! serial peripheral port must be configured before it is enabled.
235 //!
236 //! \param base specifies the SPI module base address.
237 //!
238 //! \return None
239 //
240 //*****************************************************************************
SPIEnable(uint32_t base)241 __STATIC_INLINE void SPIEnable(uint32_t base)
242 {
243     // Check the arguments
244     ASSERT(SPIBaseValid(base));
245 
246     // Read-modify-write the enable bit
247     HWREG(base + SPI_O_CTL1) |= SPI_CTL1_EN_EN;
248 }
249 
250 //*****************************************************************************
251 //
252 //! \brief Disables the serial peripheral port.
253 //!
254 //! This function disables operation of the serial peripheral port.
255 //!
256 //! \param base specifies the SPI module base address.
257 //!
258 //! \return None
259 //
260 //*****************************************************************************
SPIDisable(uint32_t base)261 __STATIC_INLINE void SPIDisable(uint32_t base)
262 {
263     // Check the arguments
264     ASSERT(SPIBaseValid(base));
265 
266     // Read-modify-write the enable bit
267     HWREG(base + SPI_O_CTL1) &= ~SPI_CTL1_EN_EN;
268 }
269 
270 //*****************************************************************************
271 //
272 //! \brief Puts a data element into the SPI transmit FIFO.
273 //!
274 //! This function places the supplied data into the transmit FIFO of the
275 //! specified SPI module.
276 //!
277 //! \note The upper 32 - N bits of the \c data are discarded by the
278 //! hardware, where N is the data width as configured by \ref SPIConfigSetExpClk().
279 //! For example, if the interface is configured for 8-bit data width, the upper
280 //! 24 bits of \c data are discarded.
281 //!
282 //! \param base specifies the SPI module base address.
283 //! \param data is the data to be transmitted over the SPI interface.
284 //!
285 //! \return None
286 //
287 //*****************************************************************************
288 extern void SPIPutData(uint32_t base, uint32_t data);
289 
290 //*****************************************************************************
291 //
292 //! \brief Puts a data element into the SPI transmit FIFO.
293 //!
294 //! This function places the supplied data into the transmit FIFO of the
295 //! specified SPI module. If there is no space in the FIFO, then this function
296 //! returns a zero.
297 //!
298 //! \note The upper 32 - N bits of the \c data are discarded by the hardware,
299 //! where N is the data width as configured by \ref SPIConfigSetExpClk(). For
300 //! example, if the interface is configured for 8-bit data width, the upper 24
301 //! bits of \c data are discarded.
302 //!
303 //! \param base specifies the SPI module base address.
304 //! \param data is the data to be transmitted over the SPI interface.
305 //!
306 //! \return Returns the number of elements written to the SPI transmit FIFO.
307 //
308 //*****************************************************************************
309 extern int32_t SPIPutDataNonBlocking(uint32_t base, uint32_t data);
310 
311 //*****************************************************************************
312 //
313 //! \brief Gets a data element from the SPI receive FIFO.
314 //!
315 //! This function gets received data from the receive FIFO of the specified
316 //! SPI module and places that data into the location specified by the
317 //! \c data parameter.
318 //!
319 //! \note Only the lower N bits of the value written to \c data contain
320 //! valid data, where N is the data width as configured by
321 //! \ref SPIConfigSetExpClk(). For example, if the interface is configured for
322 //! 8-bit data width, only the lower 8 bits of the value written to
323 //! \c data contain valid data.
324 //!
325 //! \param base specifies the SPI module base address.
326 //! \param data is a pointer to a storage location for data that was
327 //! received over the SPI interface.
328 //!
329 //! \return None
330 //
331 //*****************************************************************************
332 extern void SPIGetData(uint32_t base, uint32_t *data);
333 
334 //*****************************************************************************
335 //
336 //! \brief Gets a data element from the SPI receive FIFO.
337 //!
338 //! This function gets received data from the receive FIFO of the specified SPI
339 //! module and places that data into the location specified by the \c data
340 //! parameter. If there is no data in the FIFO, then this function returns a
341 //! zero.
342 //!
343 //! \note Only the lower N bits of the value written to \c data contain
344 //! valid data, where N is the data width as configured by
345 //! \ref SPIConfigSetExpClk(). For example, if the interface is configured for
346 //! 8-bit data width, only the lower 8 bits of the value written to \c data
347 //! contain valid data.
348 //!
349 //! \param base specifies the SPI module base address.
350 //! \param data is a pointer to a storage location for data that was
351 //! received over the SPI interface.
352 //!
353 //! \return Returns the number of elements read from the SPI receive FIFO.
354 //
355 //*****************************************************************************
356 extern int32_t SPIGetDataNonBlocking(uint32_t base, uint32_t *data);
357 
358 //*****************************************************************************
359 //
360 //! \brief Determines whether the SPI transmitter is busy or not.
361 //!
362 //! Allows the caller to determine whether all transmitted bytes have cleared
363 //! the transmitter hardware. If \c false is returned, then the transmit FIFO
364 //! is empty and all bits of the last transmitted word have left the hardware
365 //! shift register.
366 //!
367 //! \param base is the base address of the SPI port.
368 //!
369 //! \return Returns status of the SPI transmit buffer.
370 //! - \c true  : SPI is transmitting.
371 //! - \c false : SPI transmissions are complete.
372 //
373 //*****************************************************************************
SPIBusy(uint32_t base)374 __STATIC_INLINE bool SPIBusy(uint32_t base)
375 {
376     // Check the arguments
377     ASSERT(SPIBaseValid(base));
378 
379     /* Determine if the SPI is busy. */
380     return ((HWREG(base + SPI_O_STA) & SPI_STA_BUSY) ? true : false);
381 }
382 
383 //*****************************************************************************
384 //
385 //! \brief Get the status of the SPI data buffers.
386 //!
387 //! This function is used to poll the status of the internal FIFOs in the SPI
388 //! module. The status of both TX and RX FIFO is returned.
389 //!
390 //! \param base specifies the SPI module base address.
391 //!
392 //! \return Returns the current status of the internal SPI data buffers.
393 //! The status is a bitwise OR'ed combination of:
394 //! - \ref SPI_BUSY        : Busy
395 //! - \ref SPI_RX_NOT_FULL : Receive FIFO not full
396 //! - \ref SPI_RX_EMPTY    : Receive FIFO empty
397 //! - \ref SPI_TX_NOT_FULL : Transmit FIFO not full
398 //! - \ref SPI_TX_EMPTY    : Transmit FIFO empty
399 //
400 //*****************************************************************************
SPIStatus(uint32_t base)401 __STATIC_INLINE uint32_t SPIStatus(uint32_t base)
402 {
403     // Check the arguments
404     ASSERT(SPIBaseValid(base));
405 
406     // Return the status
407     return (HWREG(base + SPI_O_STA) & SPI_STATUS_MASK);
408 }
409 
410 //*****************************************************************************
411 //
412 //! \brief Registers an interrupt handler for the
413 //! Serial Peripheral Interface in the dynamic interrupt table.
414 //!
415 //! \note Only use this function if you want to use the dynamic vector table (in SRAM)!
416 //!
417 //! This function registers a function as the interrupt handler for a specific
418 //! interrupt and enables the corresponding interrupt in the interrupt controller.
419 //!
420 //! Specific SPI interrupts must be enabled via \ref SPIEnableInt(). If necessary,
421 //! it is the interrupt handler's responsibility to clear the interrupt source
422 //! via \ref SPIClearInt().
423 //!
424 //! \param base specifies the SPI module base address.
425 //! \param pfnHandler is a pointer to the function to be called when the
426 //! serial peripheral port interrupt occurs.
427 //!
428 //! \return None
429 //!
430 //! \sa \ref IntRegister() for important information about registering interrupt
431 //! handlers.
432 //
433 //*****************************************************************************
434 extern void SPIRegisterInt(uint32_t base, void (*pfnHandler)(void));
435 
436 //*****************************************************************************
437 //
438 //! \brief Unregisters an interrupt handler for the
439 //! Serial Peripheral Interface in the dynamic interrupt table.
440 //!
441 //! This function will clear the handler to be called when a SPI
442 //! interrupt occurs. This will also mask off the interrupt in the interrupt
443 //! controller so that the interrupt handler no longer is called.
444 //!
445 //! \param base specifies the SPI module base address.
446 //!
447 //! \return None
448 //!
449 //! \sa \ref IntRegister() for important information about registering interrupt
450 //! handlers.
451 //
452 //*****************************************************************************
453 extern void SPIUnregisterInt(uint32_t base);
454 
455 //*****************************************************************************
456 //
457 //! \brief Enables individual SPI interrupt sources.
458 //!
459 //! Enables the indicated SPI interrupt sources. Only the sources that are
460 //! enabled can be reflected to the processor interrupt; disabled sources have
461 //! no effect on the processor.
462 //!
463 //! \param base specifies the SPI module base address.
464 //! \param intFlags is a bit mask of the interrupt sources to be enabled.
465 //! - \ref  SPI_DMA_DONE_TX
466 //! - \ref  SPI_DMA_DONE_RX
467 //! - \ref  SPI_IDLE
468 //! - \ref  SPI_TXEMPTY
469 //! - \ref  SPI_TX
470 //! - \ref  SPI_RX
471 //! - \ref  SPI_RTOUT
472 //! - \ref  SPI_PER
473 //! - \ref  SPI_RXFIFO_OVF
474 //!
475 //! \return None
476 //
477 //*****************************************************************************
SPIEnableInt(uint32_t base,uint32_t intFlags)478 __STATIC_INLINE void SPIEnableInt(uint32_t base, uint32_t intFlags)
479 {
480     // Check the arguments
481     ASSERT(SPIBaseValid(base));
482 
483     // Enable the specified interrupts
484     HWREG(base + SPI_O_IMASK) |= intFlags;
485 }
486 
487 //*****************************************************************************
488 //
489 //! \brief Disables individual SPI interrupt sources.
490 //!
491 //! Disables the indicated SPI interrupt sources.
492 //!
493 //! \param base specifies the SPI module base address.
494 //! \param intFlags is a bit mask of the interrupt sources to be disabled.
495 //! - \ref  SPI_DMA_DONE_TX
496 //! - \ref  SPI_DMA_DONE_RX
497 //! - \ref  SPI_IDLE
498 //! - \ref  SPI_TXEMPTY
499 //! - \ref  SPI_TX
500 //! - \ref  SPI_RX
501 //! - \ref  SPI_RTOUT
502 //! - \ref  SPI_PER
503 //! - \ref  SPI_RXFIFO_OVF
504 //!
505 //! \return None
506 //
507 //*****************************************************************************
SPIDisableInt(uint32_t base,uint32_t intFlags)508 __STATIC_INLINE void SPIDisableInt(uint32_t base, uint32_t intFlags)
509 {
510     // Check the arguments
511     ASSERT(SPIBaseValid(base));
512 
513     // Disable the specified interrupts
514     HWREG(base + SPI_O_IMASK) &= ~intFlags;
515 }
516 
517 //*****************************************************************************
518 //
519 //! \brief Clears SPI interrupt sources.
520 //!
521 //! The specified SPI interrupt sources are cleared so that they no longer
522 //! assert. This function must be called in the interrupt handler to keep the
523 //! interrupts from being recognized again immediately upon exit.
524 //!
525 //! \note Due to write buffers and synchronizers in the system it may take several
526 //! clock cycles from a register write clearing an event in a module and until the
527 //! event is actually cleared in the NVIC of the system CPU. It is recommended to
528 //! clear the event source early in the interrupt service routine (ISR) to allow
529 //! the event clear to propagate to the NVIC before returning from the ISR.
530 //! At the same time, an early event clear allows new events of the same type to be
531 //! pended instead of ignored if the event is cleared later in the ISR.
532 //! It is the responsibility of the programmer to make sure that enough time has passed
533 //! before returning from the ISR to avoid false re-triggering of the cleared event.
534 //! A simple, although not necessarily optimal, way of clearing an event before
535 //! returning from the ISR is:
536 //! -# Write to clear event (interrupt source). (buffered write)
537 //! -# Dummy read from the event source module. (making sure the write has propagated)
538 //! -# Wait two system CPU clock cycles (user code or two NOPs),
539 //! (allowing cleared event to propagate through any synchronizers)
540 //!
541 //! \param base specifies the SPI module base address.
542 //! \param intFlags is a bit mask of the interrupt sources to be cleared.
543 //! The parameter can consist of either or both of:
544 //! - \ref  SPI_DMA_DONE_TX
545 //! - \ref  SPI_DMA_DONE_RX
546 //! - \ref  SPI_IDLE
547 //! - \ref  SPI_TXEMPTY
548 //! - \ref  SPI_TX
549 //! - \ref  SPI_RX
550 //! - \ref  SPI_RTOUT
551 //! - \ref  SPI_PER
552 //! - \ref  SPI_RXFIFO_OVF
553 //!
554 //! \return None
555 //
556 //*****************************************************************************
SPIClearInt(uint32_t base,uint32_t intFlags)557 __STATIC_INLINE void SPIClearInt(uint32_t base, uint32_t intFlags)
558 {
559     // Check the arguments
560     ASSERT(SPIBaseValid(base));
561 
562     // Clear the requested interrupt sources
563     HWREG(base + SPI_O_ICLR) = intFlags;
564 }
565 
566 //*****************************************************************************
567 //
568 //! \brief Gets the current interrupt status.
569 //!
570 //! This function returns the interrupt status for the SPI module.  Either the
571 //! raw interrupt status or the status of interrupts that are allowed to
572 //! reflect to the processor can be returned.
573 //!
574 //! \param base specifies the SPI module base address.
575 //! \param isMasked selects either raw or masked interrupt.
576 //! \c false : Raw interrupt status is required.
577 //! \c true  : Masked interrupt status is required.
578 //!
579 //! \return Returns the current interrupt status as an OR'ed combination of:
580 //! - \ref  SPI_DMA_DONE_TX
581 //! - \ref  SPI_DMA_DONE_RX
582 //! - \ref  SPI_IDLE
583 //! - \ref  SPI_TXEMPTY
584 //! - \ref  SPI_TX
585 //! - \ref  SPI_RX
586 //! - \ref  SPI_RTOUT
587 //! - \ref  SPI_PER
588 //! - \ref  SPI_RXFIFO_OVF
589 //
590 //*****************************************************************************
SPIIntStatus(uint32_t base,bool isMasked)591 __STATIC_INLINE uint32_t SPIIntStatus(uint32_t base, bool isMasked)
592 {
593     // Check the arguments
594     ASSERT(SPIBaseValid(base));
595 
596     /* Return either the interrupt status or the raw interrupt status as
597     requested. */
598     if (isMasked)
599     {
600         return (HWREG(base + SPI_O_MIS));
601     }
602     else
603     {
604         return (HWREG(base + SPI_O_RIS));
605     }
606 }
607 
608 //*****************************************************************************
609 //
610 //! \brief Enable SPI DMA operation.
611 //!
612 //! The specified SPI DMA features are enabled. The SPI can be
613 //! configured to use DMA for transmit and/or receive data transfers.
614 //!
615 //! \note The uDMA controller must also be set up before DMA can be used
616 //! with the SPI.
617 //!
618 //! \param base is the base address of the SPI port.
619 //! \param dmaFlags is a bit mask of the DMA features to enable.
620 //! The parameter is the bitwise OR of any of the following values:
621 //! - \ref SPI_DMA_RX : Enable DMA for receive.
622 //! - \ref SPI_DMA_TX : Enable DMA for transmit.
623 //!
624 //! \return None
625 //
626 //*****************************************************************************
SPIEnableDMA(uint32_t base,uint32_t dmaFlags)627 __STATIC_INLINE void SPIEnableDMA(uint32_t base, uint32_t dmaFlags)
628 {
629     // Check the arguments
630     ASSERT(SPIBaseValid(base));
631 
632     // Set the requested bits in the SPI DMA control register
633     HWREG(base + SPI_O_DMACR) |= dmaFlags;
634 }
635 
636 //*****************************************************************************
637 //
638 //! \brief Disable SPI DMA operation.
639 //!
640 //! This function is used to disable SPI DMA features that were enabled
641 //! by \ref SPIEnableDMA(). The specified SPI DMA features are disabled.
642 //!
643 //! \param base is the base address of the SPI port.
644 //! \param dmaFlags is a bit mask of the DMA features to disable.
645 //! The parameter is the bitwise OR of any of the following values:
646 //! - \ref SPI_DMA_RX : Disable DMA for receive.
647 //! - \ref SPI_DMA_TX : Disable DMA for transmit.
648 //!
649 //! \return None
650 //
651 //*****************************************************************************
SPIDisableDMA(uint32_t base,uint32_t dmaFlags)652 __STATIC_INLINE void SPIDisableDMA(uint32_t base, uint32_t dmaFlags)
653 {
654     // Check the arguments
655     ASSERT(SPIBaseValid(base));
656 
657     // Clear the requested bits in the SPI DMA control register
658     HWREG(base + SPI_O_DMACR) &= ~dmaFlags;
659 }
660 
661 //*****************************************************************************
662 //
663 // Mark the end of the C bindings section for C++ compilers.
664 //
665 //*****************************************************************************
666 #ifdef __cplusplus
667 }
668 #endif
669 
670 //*****************************************************************************
671 //
672 //! Close the Doxygen group.
673 //! @}
674 //! @}
675 //
676 //*****************************************************************************
677 
678 #endif // __SPI_H__
679