uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lpc17xx_spi.h
Go to the documentation of this file.
1 /***********************************************************************/
21 /* Peripheral group ----------------------------------------------------------- */
27 #ifndef LPC17XX_SPI_H_
28 #define LPC17XX_SPI_H_
29 
30 /* Includes ------------------------------------------------------------------- */
31 #include "LPC17xx.h"
32 #include "lpc_types.h"
33 
34 
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #endif
39 
40 /* Public Macros -------------------------------------------------------------- */
45 /*********************************************************************/
49 #define SPI_CPHA_FIRST ((uint32_t)(0))
50 #define SPI_CPHA_SECOND ((uint32_t)(1<<3))
51 
53 #define SPI_CPOL_HI ((uint32_t)(0))
54 #define SPI_CPOL_LO ((uint32_t)(1<<4))
55 
57 #define SPI_SLAVE_MODE ((uint32_t)(0))
58 #define SPI_MASTER_MODE ((uint32_t)(1<<5))
59 
61 #define SPI_DATA_MSB_FIRST ((uint32_t)(0))
62 #define SPI_DATA_LSB_FIRST ((uint32_t)(1<<6))
63 
65 #define SPI_DATABIT_16 SPI_SPCR_BITS(0)
66 #define SPI_DATABIT_8 SPI_SPCR_BITS(0x08)
67 #define SPI_DATABIT_9 SPI_SPCR_BITS(0x09)
68 #define SPI_DATABIT_10 SPI_SPCR_BITS(0x0A)
69 #define SPI_DATABIT_11 SPI_SPCR_BITS(0x0B)
70 #define SPI_DATABIT_12 SPI_SPCR_BITS(0x0C)
71 #define SPI_DATABIT_13 SPI_SPCR_BITS(0x0D)
72 #define SPI_DATABIT_14 SPI_SPCR_BITS(0x0E)
73 #define SPI_DATABIT_15 SPI_SPCR_BITS(0x0F)
75 /*********************************************************************/
79 #define SPI_STAT_ABRT SPI_SPSR_ABRT
80 
81 #define SPI_STAT_MODF SPI_SPSR_MODF
82 
83 #define SPI_STAT_ROVR SPI_SPSR_ROVR
84 
85 #define SPI_STAT_WCOL SPI_SPSR_WCOL
86 
87 #define SPI_STAT_SPIF SPI_SPSR_SPIF
88 
89 /* SPI Status Implementation definitions */
90 #define SPI_STAT_DONE (1UL<<8)
91 #define SPI_STAT_ERROR (1UL<<9)
98 /* Private Macros ------------------------------------------------------------- */
99 
103 /* --------------------- BIT DEFINITIONS -------------------------------------- */
104 /*********************************************************************/
109 #define SPI_SPCR_BIT_EN ((uint32_t)(1<<2))
110 
111 #define SPI_SPCR_CPHA_SECOND ((uint32_t)(1<<3))
112 
113 #define SPI_SPCR_CPOL_LOW ((uint32_t)(1<<4))
114 
115 #define SPI_SPCR_MSTR ((uint32_t)(1<<5))
116 
117 #define SPI_SPCR_LSBF ((uint32_t)(1<<6))
118 
119 #define SPI_SPCR_SPIE ((uint32_t)(1<<7))
120 
122 #define SPI_SPCR_BITS(n) ((n==0) ? ((uint32_t)0) : ((uint32_t)((n&0x0F)<<8)))
123 
124 #define SPI_SPCR_BITMASK ((uint32_t)(0xFFC))
125 
126 /*********************************************************************/
130 #define SPI_SPSR_ABRT ((uint32_t)(1<<3))
131 
132 #define SPI_SPSR_MODF ((uint32_t)(1<<4))
133 
134 #define SPI_SPSR_ROVR ((uint32_t)(1<<5))
135 
136 #define SPI_SPSR_WCOL ((uint32_t)(1<<6))
137 
138 #define SPI_SPSR_SPIF ((uint32_t)(1<<7))
139 
140 #define SPI_SPSR_BITMASK ((uint32_t)(0xF8))
141 
142 /*********************************************************************/
146 #define SPI_SPDR_LO_MASK ((uint32_t)(0xFF))
147 
148 #define SPI_SPDR_HI_MASK ((uint32_t)(0xFF00))
149 
150 #define SPI_SPDR_BITMASK ((uint32_t)(0xFFFF))
151 
152 /*********************************************************************/
156 #define SPI_SPCCR_COUNTER(n) ((uint32_t)(n&0xFF))
157 
158 #define SPI_SPCCR_BITMASK ((uint32_t)(0xFF))
159 
160 /***********************************************************************
161  * Macro defines for SPI Test Control Register
162  **********************************************************************/
164 #define SPI_SPTCR_TEST_MASK ((uint32_t)(0xFE))
165 
166 #define SPI_SPTCR_BITMASK ((uint32_t)(0xFE))
167 
168 /*********************************************************************/
172 #define SPI_SPTSR_ABRT ((uint32_t)(1<<3))
173 
174 #define SPI_SPTSR_MODF ((uint32_t)(1<<4))
175 
176 #define SPI_SPTSR_ROVR ((uint32_t)(1<<5))
177 
178 #define SPI_SPTSR_WCOL ((uint32_t)(1<<6))
179 
180 #define SPI_SPTSR_SPIF ((uint32_t)(1<<7))
181 
182 #define SPI_SPTSR_MASKBIT ((uint32_t)(0xF8))
183 
184 /*********************************************************************/
188 #define SPI_SPINT_INTFLAG ((uint32_t)(1<<0))
189 
190 #define SPI_SPINT_BITMASK ((uint32_t)(0x01))
191 
192 
193 /* ---------------- CHECK PARAMETER DEFINITIONS ---------------------------- */
195 #define PARAM_SPIx(n) (((uint32_t *)n)==((uint32_t *)LPC_SPI))
196 
198 #define PARAM_SPI_CPHA(n) ((n==SPI_CPHA_FIRST) || (n==SPI_CPHA_SECOND))
199 
201 #define PARAM_SPI_CPOL(n) ((n==SPI_CPOL_HI) || (n==SPI_CPOL_LO))
202 
204 #define PARAM_SPI_MODE(n) ((n==SPI_SLAVE_MODE) || (n==SPI_MASTER_MODE))
205 
207 #define PARAM_SPI_DATA_ORDER(n) ((n==SPI_DATA_MSB_FIRST) || (n==SPI_DATA_LSB_FIRST))
208 
210 #define PARAM_SPI_DATABIT(n) ((n==SPI_DATABIT_16) || (n==SPI_DATABIT_8) \
211 || (n==SPI_DATABIT_9) || (n==SPI_DATABIT_10) \
212 || (n==SPI_DATABIT_11) || (n==SPI_DATABIT_12) \
213 || (n==SPI_DATABIT_13) || (n==SPI_DATABIT_14) \
214 || (n==SPI_DATABIT_15))
215 
217 #define PARAM_SPI_STAT(n) ((n==SPI_STAT_ABRT) || (n==SPI_STAT_MODF) \
218 || (n==SPI_STAT_ROVR) || (n==SPI_STAT_WCOL) \
219 || (n==SPI_STAT_SPIF))
220 
226 /* Public Types --------------------------------------------------------------- */
232 typedef struct {
233  uint32_t Databit;
235  uint32_t CPHA;
238  uint32_t CPOL;
241  uint32_t Mode;
244  uint32_t DataOrder;
247  uint32_t ClockRate;
249 } SPI_CFG_Type;
250 
251 
255 typedef enum {
259 
263 typedef struct {
264  void *tx_data;
265  void *rx_data;
266  uint32_t length;
267  uint32_t counter;
268  uint32_t status;
270 
276 /* Public Functions ----------------------------------------------------------- */
281 /* SPI Init/DeInit functions ---------*/
282 void SPI_Init(LPC_SPI_TypeDef *SPIx, SPI_CFG_Type *SPI_ConfigStruct);
283 void SPI_DeInit(LPC_SPI_TypeDef *SPIx);
284 void SPI_SetClock (LPC_SPI_TypeDef *SPIx, uint32_t target_clock);
285 void SPI_ConfigStructInit(SPI_CFG_Type *SPI_InitStruct);
286 
287 /* SPI transfer functions ------------*/
288 void SPI_SendData(LPC_SPI_TypeDef *SPIx, uint16_t Data);
289 uint16_t SPI_ReceiveData(LPC_SPI_TypeDef *SPIx);
290 int32_t SPI_ReadWrite (LPC_SPI_TypeDef *SPIx, SPI_DATA_SETUP_Type *dataCfg, SPI_TRANSFER_Type xfType);
291 
292 /* SPI Interrupt functions ---------*/
293 void SPI_IntCmd(LPC_SPI_TypeDef *SPIx, FunctionalState NewState);
296 
297 /* SPI get information functions-----*/
298 uint8_t SPI_GetDataSize (LPC_SPI_TypeDef *SPIx);
299 uint32_t SPI_GetStatus(LPC_SPI_TypeDef *SPIx);
300 FlagStatus SPI_CheckStatus (uint32_t inputSPIStatus, uint8_t SPIStatus);
301 
306 #ifdef __cplusplus
307 }
308 #endif
309 
310 #endif /* LPC17XX_SPI_H_ */
311 
316 /* --------------------------------- End Of File ------------------------------ */