CC23x0r2DriverLibrary
spi.h
Go to the documentation of this file.
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 //
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
74 #define SPI_DMA_DONE_RX SPI_IMASK_DMARX
75 #define SPI_IDLE SPI_IMASK_IDLE
76 #define SPI_TXEMPTY SPI_IMASK_TXEMPTY
77 #define SPI_TX SPI_IMASK_TX
78 #define SPI_RX \
79  SPI_IMASK_RX
80 #define SPI_RTOUT SPI_IMASK_RTOUT
81 #define SPI_PER SPI_IMASK_PER
82 #define SPI_RXFIFO_OVF SPI_IMASK_RXOVF
83 
84 //*****************************************************************************
85 //
86 // Values that are returned from SPIStatus
87 //
88 //*****************************************************************************
89 #define SPI_BUSY SPI_STA_BUSY_ACTIVE
90 #define SPI_RX_NOT_FULL SPI_STA_RNF_NOT_FULL
91 #define SPI_RX_EMPTY SPI_STA_RFE_EMPTY
92 #define SPI_TX_NOT_FULL SPI_STA_TNF_NOT_FULL
93 #define SPI_TX_EMPTY SPI_STA_TFE_EMPTY
94 #define SPI_STATUS_MASK 0x0000001F
95 
96 //*****************************************************************************
97 //
98 // Values that can be passed to SPIConfigSetExpClk
99 //
100 //*****************************************************************************
102 #define SPI_FRF_MOTO_MODE_0 (SPI_CTL0_FRF_MOTOROLA_3WIRE | SPI_CTL0_SPO_LO | SPI_CTL0_SPH_FIRST)
103 #define SPI_FRF_MOTO_MODE_1 (SPI_CTL0_FRF_MOTOROLA_3WIRE | SPI_CTL0_SPO_LO | SPI_CTL0_SPH_SECOND)
105 #define SPI_FRF_MOTO_MODE_2 (SPI_CTL0_FRF_MOTOROLA_3WIRE | SPI_CTL0_SPO_HI | SPI_CTL0_SPH_FIRST)
107 #define SPI_FRF_MOTO_MODE_3 (SPI_CTL0_FRF_MOTOROLA_3WIRE | SPI_CTL0_SPO_HI | SPI_CTL0_SPH_SECOND)
109 #define SPI_FRF_MOTO_MODE_4 (SPI_CTL0_FRF_MOTOROLA_4WIRE | SPI_CTL0_SPO_LO | SPI_CTL0_SPH_FIRST)
111 #define SPI_FRF_MOTO_MODE_5 (SPI_CTL0_FRF_MOTOROLA_4WIRE | SPI_CTL0_SPO_LO | SPI_CTL0_SPH_SECOND)
113 #define SPI_FRF_MOTO_MODE_6 (SPI_CTL0_FRF_MOTOROLA_4WIRE | SPI_CTL0_SPO_HI | SPI_CTL0_SPH_FIRST)
115 #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
119 #define SPI_FRF_NMW SPI_CTL0_FRF_MIRCOWIRE
120 
121 #define SPI_MODE_CONTROLLER SPI_CTL1_MS_CONTROLLER
122 #define SPI_MODE_PERIPHERAL SPI_CTL1_MS_PERIPHERAL
123 #define SPI_MODE_PERIPHERAL_OD \
124  SPI_CTL1_SOD_ENABLE
125 
127 //*****************************************************************************
128 //
129 // Values that can be passed to SPIEnableDMA() and SPIDisableDMA()
130 //
131 //*****************************************************************************
132 #define SPI_DMA_TX SPI_DMACR_TXEN
133 #define SPI_DMA_RX SPI_DMACR_RXEN
134 
135 //*****************************************************************************
136 //
137 // API Functions and prototypes
138 //
139 //*****************************************************************************
140 
141 #ifdef DRIVERLIB_DEBUG
142 //*****************************************************************************
143 //
154 //
155 //*****************************************************************************
156 static bool SPIBaseValid(uint32_t base)
157 {
158  return (base == SPI0_BASE);
159 }
160 #endif
161 
162 //*****************************************************************************
163 //
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 //
239 //
240 //*****************************************************************************
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 //
259 //
260 //*****************************************************************************
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 //
286 //
287 //*****************************************************************************
288 extern void SPIPutData(uint32_t base, uint32_t data);
289 
290 //*****************************************************************************
291 //
307 //
308 //*****************************************************************************
309 extern int32_t SPIPutDataNonBlocking(uint32_t base, uint32_t data);
310 
311 //*****************************************************************************
312 //
330 //
331 //*****************************************************************************
332 extern void SPIGetData(uint32_t base, uint32_t *data);
333 
334 //*****************************************************************************
335 //
354 //
355 //*****************************************************************************
356 extern int32_t SPIGetDataNonBlocking(uint32_t base, uint32_t *data);
357 
358 //*****************************************************************************
359 //
372 //
373 //*****************************************************************************
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 //
399 //
400 //*****************************************************************************
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 //
432 //
433 //*****************************************************************************
434 extern void SPIRegisterInt(uint32_t base, void (*pfnHandler)(void));
435 
436 //*****************************************************************************
437 //
451 //
452 //*****************************************************************************
453 extern void SPIUnregisterInt(uint32_t base);
454 
455 //*****************************************************************************
456 //
476 //
477 //*****************************************************************************
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 //
506 //
507 //*****************************************************************************
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 //
555 //
556 //*****************************************************************************
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 //
589 //
590 //*****************************************************************************
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 //
625 //
626 //*****************************************************************************
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 //
650 //
651 //*****************************************************************************
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 //
675 //
676 //*****************************************************************************
677 
678 #endif // __SPI_H__
void SPIGetData(uint32_t base, uint32_t *data)
Gets a data element from the SPI receive FIFO.
Definition: spi.c:162
void SPIUnregisterInt(uint32_t base)
Unregisters an interrupt handler for the Serial Peripheral Interface in the dynamic interrupt table...
Definition: spi.c:217
int32_t SPIPutDataNonBlocking(uint32_t base, uint32_t data)
Puts a data element into the SPI transmit FIFO.
Definition: spi.c:124
__STATIC_INLINE void SPIEnableInt(uint32_t base, uint32_t intFlags)
Enables individual SPI interrupt sources.
Definition: spi.h:478
__STATIC_INLINE void SPIDisableInt(uint32_t base, uint32_t intFlags)
Disables individual SPI interrupt sources.
Definition: spi.h:508
void SPIConfigSetExpClk(uint32_t base, uint32_t spiClk, uint32_t protocol, uint32_t mode, uint32_t bitRate, uint32_t dataWidth)
Configures the serial peripheral port.
Definition: spi.c:96
__STATIC_INLINE void SPIDisableDMA(uint32_t base, uint32_t dmaFlags)
Disable SPI DMA operation.
Definition: spi.h:652
__STATIC_INLINE void SPIEnableDMA(uint32_t base, uint32_t dmaFlags)
Enable SPI DMA operation.
Definition: spi.h:627
void SPIRegisterInt(uint32_t base, void(*pfnHandler)(void))
Registers an interrupt handler for the Serial Peripheral Interface in the dynamic interrupt table...
Definition: spi.c:200
__STATIC_INLINE void SPIEnable(uint32_t base)
Enables the serial peripheral port.
Definition: spi.h:241
#define ASSERT(expr)
Definition: debug.h:71
__STATIC_INLINE void SPIClearInt(uint32_t base, uint32_t intFlags)
Clears SPI interrupt sources.
Definition: spi.h:557
__STATIC_INLINE uint32_t SPIStatus(uint32_t base)
Get the status of the SPI data buffers.
Definition: spi.h:401
#define SPI_STATUS_MASK
Mask for bits above.
Definition: spi.h:94
__STATIC_INLINE uint32_t SPIIntStatus(uint32_t base, bool isMasked)
Gets the current interrupt status.
Definition: spi.h:591
#define __STATIC_INLINE
Definition: cmsis_gcc.h:47
__STATIC_INLINE void SPIDisable(uint32_t base)
Disables the serial peripheral port.
Definition: spi.h:261
void SPIPutData(uint32_t base, uint32_t data)
Puts a data element into the SPI transmit FIFO.
Definition: spi.c:146
__STATIC_INLINE bool SPIBusy(uint32_t base)
Determines whether the SPI transmitter is busy or not.
Definition: spi.h:374
int32_t SPIGetDataNonBlocking(uint32_t base, uint32_t *data)
Gets a data element from the SPI receive FIFO.
Definition: spi.c:178