LPCOpen Platform
v1.03
LPCOpen Platform for NXP LPC Microcontrollers
Main Page
Modules
Data Structures
Files
Related Pages
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Groups
Pages
spi.c
Go to the documentation of this file.
1
/*
2
* @brief SPI example
3
*
4
* @note
5
* Copyright(C) NXP Semiconductors, 2012
6
* All rights reserved.
7
*
8
* @par
9
* Software that is described herein is for illustrative purposes only
10
* which provides customers with programming information regarding the
11
* LPC products. This software is supplied "AS IS" without any warranties of
12
* any kind, and NXP Semiconductors and its licensor disclaim any and
13
* all warranties, express or implied, including all implied warranties of
14
* merchantability, fitness for a particular purpose and non-infringement of
15
* intellectual property rights. NXP Semiconductors assumes no responsibility
16
* or liability for the use of the software, conveys no license or rights under any
17
* patent, copyright, mask work right, or any other intellectual property rights in
18
* or to any products. NXP Semiconductors reserves the right to make changes
19
* in the software without notification. NXP Semiconductors also makes no
20
* representation or warranty that such application will be suitable for the
21
* specified use without further testing or modification.
22
*
23
* @par
24
* Permission to use, copy, modify, and distribute this software and its
25
* documentation is hereby granted, under NXP Semiconductors' and its
26
* licensor's relevant copyrights in the software, without fee, provided that it
27
* is used in conjunction with NXP Semiconductors microcontrollers. This
28
* copyright, permission, and disclaimer notice must appear in all copies of
29
* this code.
30
*/
31
32
#include "board.h"
33
83
/*****************************************************************************
84
* Private types/enumerations/variables
85
****************************************************************************/
86
87
#define RW_TEST 1
88
#define LOOPBACK_TEST 1
89
#define BUFFER_SIZE 10
90
#define SPI_MODE_TEST (SPI_MODE_MASTER)
91
//#define SPI_MODE_TEST (SPI_MODE_SLAVE)
92
#define POLLING_MODE 1
93
#if POLLING_MODE
94
#define INTERRUPT_MODE 0
95
#else
96
#define INTERRUPT_MODE 1
97
#endif
98
99
#define LPC_SPI LPC_SPI1
100
#define SPI_IRQ SPI1_IRQn
101
#define SPIIRQHANDLER SPI1_IRQHandler
102
103
/* Tx buffer */
104
static
uint16_t
TxBuf
[
BUFFER_SIZE
];
105
106
/* Rx buffer */
107
static
uint16_t
RxBuf
[
BUFFER_SIZE
];
108
109
static
SPI_CONFIG_T
ConfigStruct
;
110
static
SPI_DELAY_CONFIG_T
DelayConfigStruct
;
111
112
static
CHIP_SPI_DATA_SETUP_T
XfSetup
;
113
static
volatile
uint8_t
isXferCompleted
= 0;
114
115
/*****************************************************************************
116
* Public types/enumerations/variables
117
****************************************************************************/
118
119
/*****************************************************************************
120
* Private functions
121
****************************************************************************/
122
123
/* Initialize buffer */
124
static
void
bufferInit
(
void
)
125
{
126
uint16_t i;
127
uint16_t ch = 0;
128
129
for
(i = 0; i <
BUFFER_SIZE
; i++) {
130
TxBuf
[i] = ch++;
131
RxBuf
[i] = 0xAA;
132
}
133
}
134
135
#if (RW_TEST || LOOPBACK_TEST)
136
/* Verify buffer after transfer */
137
static
uint8_t
bufferVerify
(
void
)
138
{
139
uint16_t i;
140
uint16_t *src_addr = (uint16_t *) &
TxBuf
[0];
141
uint16_t *dest_addr = (uint16_t *) &
RxBuf
[0];
142
143
for
( i = 0; i <
BUFFER_SIZE
; i++ ) {
144
145
if
(*src_addr != *dest_addr) {
146
return
1;
147
}
148
src_addr++;
149
dest_addr++;
150
}
151
return
0;
152
}
153
154
#endif
155
156
/*****************************************************************************
157
* Public functions
158
****************************************************************************/
159
160
#if INTERRUPT_MODE
161
165
void
SPIIRQHANDLER
(
void
)
166
{
167
Chip_SPI_Int_Cmd
(
LPC_SPI
,
SPI_INTENCLR_TXDYEN
|
SPI_INTENCLR_RXDYEN
168
|
SPI_INTENCLR_RXOVEN
|
SPI_INTENCLR_TXUREN
,
DISABLE
);
/* Disable all interrupt */
169
if
(((XfSetup.
pRx
) && (XfSetup.
RxCnt
< XfSetup.
Length
))
170
|| ((XfSetup.
pTx
) && (XfSetup.
TxCnt
< XfSetup.
Length
))) {
171
Chip_SPI_Int_RWFrames
(
LPC_SPI
, &XfSetup);
172
Chip_SPI_Int_Cmd
(
LPC_SPI
,
SPI_INTENSET_TXDYEN
|
SPI_INTENSET_RXDYEN
173
|
SPI_INTENSET_RXOVEN
|
SPI_INTENSET_TXUREN
,
ENABLE
);
174
}
175
else
{
176
isXferCompleted
= 1;
177
}
178
}
179
180
#endif
/*INTERRUPT_MODE*/
181
186
int
main
(
void
)
187
{
188
Board_Init
();
189
190
/* SPI initialization */
191
Board_SPI_Init
(
LPC_SPI
);
192
193
ConfigStruct.
Mode
=
SPI_MODE_TEST
;
194
ConfigStruct.
ClkDiv
=
Chip_SPI_CalClkRateDivider
(
LPC_SPI
, 100000);
195
ConfigStruct.
ClockMode
=
SPI_CLOCK_CPHA0_CPOL0
;
196
ConfigStruct.
DataOrder
=
SPI_DATA_MSB_FIRST
;
197
ConfigStruct.
SSELPol
=
SPI_SSEL_ACTIVE_LO
;
198
Chip_SPI_Init
(
LPC_SPI
, &ConfigStruct);
199
200
DelayConfigStruct.
FrameDelay
= 0;
201
DelayConfigStruct.
PostDelay
= 0;
202
DelayConfigStruct.
PreDelay
= 0;
203
DelayConfigStruct.
TransferDelay
= 0;
204
Chip_SPI_DelayConfig
(
LPC_SPI
, &DelayConfigStruct);
205
206
#if INTERRUPT_MODE
207
/* Setting SPI interrupt */
208
NVIC_EnableIRQ(
SPI_IRQ
);
209
#endif
210
Chip_SPI_Enable
(
LPC_SPI
);
211
212
#if LOOPBACK_TEST
213
Chip_SPI_EnableLoopBack
(
LPC_SPI
);
214
#endif
215
bufferInit
();
216
XfSetup.
Length
=
BUFFER_SIZE
;
217
XfSetup.
pTx
=
TxBuf
;
218
XfSetup.
RxCnt
= XfSetup.
TxCnt
= 0;
219
XfSetup.
DataSize
= 8;
220
221
#if (RW_TEST || LOOPBACK_TEST)
222
XfSetup.
pRx
=
RxBuf
;
223
#else
224
XfSetup.
pRx
=
NULL
;
225
#endif
226
227
#if POLLING_MODE
228
#if (RW_TEST || LOOPBACK_TEST)
229
Chip_SPI_RWFrames_Blocking
(
LPC_SPI
, &XfSetup);
230
#else
231
Chip_SPI_WriteFrames_Blocking
(
LPC_SPI
, &XfSetup);
232
#endif
233
234
#elif INTERRUPT_MODE
235
Chip_SPI_Int_RWFrames
(
LPC_SPI
, &XfSetup);
236
Chip_SPI_Int_Cmd
(
LPC_SPI
,
SPI_INTENSET_TXDYEN
|
SPI_INTENSET_RXDYEN
237
|
SPI_INTENSET_RXOVEN
|
SPI_INTENSET_TXUREN
,
ENABLE
);
238
while
(!
isXferCompleted
) {}
239
while
(!(
Chip_SPI_GetStatus
(
LPC_SPI
) &
SPI_STAT_SSD
)) {}
/* Make sure the last frame sent completely */
240
#endif
/*INTERRUPT_MODE*/
241
242
#if (RW_TEST || LOOPBACK_TEST)
243
if
(
bufferVerify
()) {
244
Board_LED_Set
(0,
true
);
245
}
246
else
247
#endif
/*(RW_TEST || LOOPBACK_TEST)*/
248
{
249
Board_LED_Set
(1,
true
);
250
}
251
252
#if LOOPBACK_TEST
253
Chip_SPI_DisableLoopBack
(
LPC_SPI
);
254
#endif
255
/* DeInitialize SPI peripheral */
256
Chip_SPI_DeInit
(
LPC_SPI
);
257
258
while
(1) {}
259
return
0;
260
}
261
applications
lpc8xx
examples
periph
periph_spi
spi.c
Generated on Fri May 10 2013 10:42:00 for LPCOpen Platform by
1.8.2