CC35xxDriverLibrary
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) 2024 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 can be passed to SPISelectCSN
87 //
88 //*****************************************************************************
89 #define SPI_CSSEL_CS0 SPI_CTL0_CSSEL_CS0
90 #define SPI_CSSEL_CS1 SPI_CTL0_CSSEL_CS1
91 #define SPI_CSSEL_CS2 SPI_CTL0_CSSEL_CS2
92 #define SPI_CSSEL_CS3 SPI_CTL0_CSSEL_CS3
93 
94 //*****************************************************************************
95 //
96 // Values that are returned from SPIStatus
97 //
98 //*****************************************************************************
99 #define SPI_BUSY SPI_STA_BUSY_ACTIVE
100 #define SPI_RX_NOT_FULL SPI_STA_RNF_NOT_FULL
101 #define SPI_RX_EMPTY SPI_STA_RFE_EMPTY
102 #define SPI_TX_NOT_FULL SPI_STA_TNF_NOT_FULL
103 #define SPI_TX_EMPTY SPI_STA_TFE_EMPTY
104 #define SPI_STATUS_MASK 0x0000001F
105 
106 //*****************************************************************************
107 //
108 // Values that can be passed to SPIConfigSetExpClk
109 //
110 //*****************************************************************************
112 #define SPI_FRF_MOTO_MODE_0 (SPI_CTL0_FRF_MOTOROLA_3WIRE | SPI_CTL0_SPO_LO | SPI_CTL0_SPH_FIRST)
113 #define SPI_FRF_MOTO_MODE_1 (SPI_CTL0_FRF_MOTOROLA_3WIRE | SPI_CTL0_SPO_LO | SPI_CTL0_SPH_SECOND)
115 #define SPI_FRF_MOTO_MODE_2 (SPI_CTL0_FRF_MOTOROLA_3WIRE | SPI_CTL0_SPO_HI | SPI_CTL0_SPH_FIRST)
117 #define SPI_FRF_MOTO_MODE_3 (SPI_CTL0_FRF_MOTOROLA_3WIRE | SPI_CTL0_SPO_HI | SPI_CTL0_SPH_SECOND)
119 #define SPI_FRF_MOTO_MODE_4 (SPI_CTL0_FRF_MOTOROLA_4WIRE | SPI_CTL0_SPO_LO | SPI_CTL0_SPH_FIRST)
121 #define SPI_FRF_MOTO_MODE_5 (SPI_CTL0_FRF_MOTOROLA_4WIRE | SPI_CTL0_SPO_LO | SPI_CTL0_SPH_SECOND)
123 #define SPI_FRF_MOTO_MODE_6 (SPI_CTL0_FRF_MOTOROLA_4WIRE | SPI_CTL0_SPO_HI | SPI_CTL0_SPH_FIRST)
125 #define SPI_FRF_MOTO_MODE_7 (SPI_CTL0_FRF_MOTOROLA_4WIRE | SPI_CTL0_SPO_HI | SPI_CTL0_SPH_SECOND)
127 
128 #define SPI_FRF_TI SPI_CTL0_FRF_TI_SYNC
129 #define SPI_FRF_NMW SPI_CTL0_FRF_MICROWIRE
130 
131 #define SPI_MODE_CONTROLLER SPI_CTL1_MS_CONTROLLER
132 #define SPI_MODE_PERIPHERAL SPI_CTL1_MS_PERIPHERAL
133 #define SPI_MODE_PERIPHERAL_OD \
134  SPI_CTL1_SOD_ENABLE
135 
137 //*****************************************************************************
138 //
139 // Values that can be passed to SPIEnableDMA() and SPIDisableDMA()
140 //
141 //*****************************************************************************
142 #define SPI_DMA_TX SPI_DMACR_TXEN
143 #define SPI_DMA_RX SPI_DMACR_RXEN
144 
145 //*****************************************************************************
146 //
147 // API Functions and prototypes
148 //
149 //*****************************************************************************
150 
151 #ifdef DRIVERLIB_DEBUG
152 //*****************************************************************************
153 //
164 //
165 //*****************************************************************************
166 static bool SPIBaseValid(uint32_t base)
167 {
168  return ((base == SPI0_BASE) || (base == SPI1_BASE));
169 }
170 #endif
171 
172 //*****************************************************************************
173 //
230 //
231 //*****************************************************************************
232 extern void SPIConfigSetExpClk(uint32_t base,
233  uint32_t spiClk,
234  uint32_t protocol,
235  uint32_t mode,
236  uint32_t bitRate,
237  uint32_t dataWidth);
238 
239 //*****************************************************************************
240 //
249 //
250 //*****************************************************************************
251 __STATIC_INLINE void SPIEnable(uint32_t base)
252 {
253  // Check the arguments
254  ASSERT(SPIBaseValid(base));
255 
256  // Read-modify-write the enable bit
258 }
259 
260 //*****************************************************************************
261 //
269 //
270 //*****************************************************************************
271 __STATIC_INLINE void SPIDisable(uint32_t base)
272 {
273  // Check the arguments
274  ASSERT(SPIBaseValid(base));
275 
276  // Read-modify-write the enable bit
277  HWREG(base + SPI_O_CTL1) &= ~SPI_CTL1_EN_ENABLE;
278 }
279 
280 //*****************************************************************************
281 //
289 //
290 //*****************************************************************************
291 __STATIC_INLINE void SPIEnableHWCS(uint32_t base)
292 {
293  // Check the arguments
294  ASSERT(SPIBaseValid(base));
296 }
297 
298 //*****************************************************************************
299 //
307 //
308 //*****************************************************************************
309 __STATIC_INLINE void SPIDisableHWCS(uint32_t base)
310 {
311  // Check the arguments
312  ASSERT(SPIBaseValid(base));
314 }
315 
316 //*****************************************************************************
317 //
330 //
331 //*****************************************************************************
332 __STATIC_INLINE void SPISelectCSN(uint32_t base, uint32_t csn)
333 {
334  // Check the arguments
335  ASSERT(SPIBaseValid(base));
336 
337  HWREG(base + SPI_O_CTL0) &= ~SPI_CTL0_CSSEL_M;
338  HWREG(base + SPI_O_CTL0) |= (csn & SPI_CTL0_CSSEL_M);
339 }
340 
341 //*****************************************************************************
342 //
357 //
358 //*****************************************************************************
359 extern void SPIPutData(uint32_t base, uint32_t data);
360 
361 //*****************************************************************************
362 //
378 //
379 //*****************************************************************************
380 extern int32_t SPIPutDataNonBlocking(uint32_t base, uint32_t data);
381 
382 //*****************************************************************************
383 //
401 //
402 //*****************************************************************************
403 extern void SPIGetData(uint32_t base, uint32_t *data);
404 
405 //*****************************************************************************
406 //
425 //
426 //*****************************************************************************
427 extern int32_t SPIGetDataNonBlocking(uint32_t base, uint32_t *data);
428 
429 //*****************************************************************************
430 //
443 //
444 //*****************************************************************************
445 __STATIC_INLINE bool SPIBusy(uint32_t base)
446 {
447  // Check the arguments
448  ASSERT(SPIBaseValid(base));
449 
450  /* Determine if the SPI is busy. */
451  return ((HWREG(base + SPI_O_STA) & SPI_STA_BUSY) ? true : false);
452 }
453 
454 //*****************************************************************************
455 //
470 //
471 //*****************************************************************************
472 __STATIC_INLINE uint32_t SPIStatus(uint32_t base)
473 {
474  // Check the arguments
475  ASSERT(SPIBaseValid(base));
476 
477  // Return the status
478  return (HWREG(base + SPI_O_STA) & SPI_STATUS_MASK);
479 }
480 
481 //*****************************************************************************
482 //
503 //
504 //*****************************************************************************
505 extern void SPIRegisterInt(uint32_t base, void (*pfnHandler)(void));
506 
507 //*****************************************************************************
508 //
522 //
523 //*****************************************************************************
524 extern void SPIUnregisterInt(uint32_t base);
525 
526 //*****************************************************************************
527 //
547 //
548 //*****************************************************************************
549 __STATIC_INLINE void SPIEnableInt(uint32_t base, uint32_t intFlags)
550 {
551  // Check the arguments
552  ASSERT(SPIBaseValid(base));
553 
554  // Enable the specified interrupts
555  HWREG(base + SPI_O_IMASK) |= intFlags;
556 }
557 
558 //*****************************************************************************
559 //
577 //
578 //*****************************************************************************
579 __STATIC_INLINE void SPIDisableInt(uint32_t base, uint32_t intFlags)
580 {
581  // Check the arguments
582  ASSERT(SPIBaseValid(base));
583 
584  // Disable the specified interrupts
585  HWREG(base + SPI_O_IMASK) &= ~intFlags;
586 }
587 
588 //*****************************************************************************
589 //
626 //
627 //*****************************************************************************
628 __STATIC_INLINE void SPIClearInt(uint32_t base, uint32_t intFlags)
629 {
630  // Check the arguments
631  ASSERT(SPIBaseValid(base));
632 
633  // Clear the requested interrupt sources
634  HWREG(base + SPI_O_ICLR) = intFlags;
635 }
636 
637 //*****************************************************************************
638 //
660 //
661 //*****************************************************************************
662 __STATIC_INLINE uint32_t SPIIntStatus(uint32_t base, bool isMasked)
663 {
664  // Check the arguments
665  ASSERT(SPIBaseValid(base));
666 
667  /* Return either the interrupt status or the raw interrupt status as
668  requested. */
669  if (isMasked)
670  {
671  return (HWREG(base + SPI_O_MIS));
672  }
673  else
674  {
675  return (HWREG(base + SPI_O_RIS));
676  }
677 }
678 
679 //*****************************************************************************
680 //
696 //
697 //*****************************************************************************
698 __STATIC_INLINE void SPIEnableDMA(uint32_t base, uint32_t dmaFlags)
699 {
700  // Check the arguments
701  ASSERT(SPIBaseValid(base));
702 
703  // Set the requested bits in the SPI DMA control register
704  HWREG(base + SPI_O_DMACR) |= dmaFlags;
705 }
706 
707 //*****************************************************************************
708 //
721 //
722 //*****************************************************************************
723 __STATIC_INLINE void SPIDisableDMA(uint32_t base, uint32_t dmaFlags)
724 {
725  // Check the arguments
726  ASSERT(SPIBaseValid(base));
727 
728  // Clear the requested bits in the SPI DMA control register
729  HWREG(base + SPI_O_DMACR) &= ~dmaFlags;
730 }
731 
732 //*****************************************************************************
733 //
734 // Mark the end of the C bindings section for C++ compilers.
735 //
736 //*****************************************************************************
737 #ifdef __cplusplus
738 }
739 #endif
740 
741 //*****************************************************************************
742 //
746 //
747 //*****************************************************************************
748 
749 #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:223
__STATIC_INLINE void SPIEnableHWCS(uint32_t base)
Enables hardware chip select.
Definition: spi.h:291
#define SPI0_BASE
Definition: hw_memmap.h:90
#define HWREG(x)
Definition: hw_types.h:78
#define __STATIC_INLINE
Definition: hw_types.h:57
#define SPI_O_RIS
Definition: hw_spi.h:51
#define SPI_CTL0_CSSEL_M
Definition: hw_spi.h:1603
#define SPI_O_ICLR
Definition: hw_spi.h:60
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:549
__STATIC_INLINE void SPISelectCSN(uint32_t base, uint32_t csn)
Selects chip select to use.
Definition: spi.h:332
#define SPI_O_CTL1
Definition: hw_spi.h:75
#define SPI1_BASE
Definition: hw_memmap.h:91
__STATIC_INLINE void SPIDisableInt(uint32_t base, uint32_t intFlags)
Disables individual SPI interrupt sources.
Definition: spi.h:579
#define SPI_CTL1_EN_ENABLE
Definition: hw_spi.h:1632
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:723
#define SPI_CTL0_HWCSN_ENABLE
Definition: hw_spi.h:1461
__STATIC_INLINE void SPIEnableDMA(uint32_t base, uint32_t dmaFlags)
Enable SPI DMA operation.
Definition: spi.h:698
#define SPI_STA_BUSY
Definition: hw_spi.h:2340
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 SPIDisableHWCS(uint32_t base)
Disables hardware chip select.
Definition: spi.h:309
__STATIC_INLINE void SPIEnable(uint32_t base)
Enables the serial peripheral port.
Definition: spi.h:251
#define ASSERT(expr)
Definition: debug.h:81
#define SPI_O_MIS
Definition: hw_spi.h:54
__STATIC_INLINE void SPIClearInt(uint32_t base, uint32_t intFlags)
Clears SPI interrupt sources.
Definition: spi.h:628
#define SPI_O_STA
Definition: hw_spi.h:117
__STATIC_INLINE uint32_t SPIStatus(uint32_t base)
Get the status of the SPI data buffers.
Definition: spi.h:472
#define SPI_O_CTL0
Definition: hw_spi.h:72
#define SPI_STATUS_MASK
Mask for bits above.
Definition: spi.h:104
__STATIC_INLINE uint32_t SPIIntStatus(uint32_t base, bool isMasked)
Gets the current interrupt status.
Definition: spi.h:662
#define SPI_O_IMASK
Definition: hw_spi.h:48
__STATIC_INLINE void SPIDisable(uint32_t base)
Disables the serial peripheral port.
Definition: spi.h:271
#define SPI_O_DMACR
Definition: hw_spi.h:87
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:445
int32_t SPIGetDataNonBlocking(uint32_t base, uint32_t *data)
Gets a data element from the SPI receive FIFO.
Definition: spi.c:178