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
ssp.c
Go to the documentation of this file.
1
/*
2
* @brief SSP example
3
* This example show how to use the SSP in 2 modes : Polling and Interrupt.
4
*
5
* @note
6
* Copyright(C) NXP Semiconductors, 2012
7
* All rights reserved.
8
*
9
* @par
10
* Software that is described herein is for illustrative purposes only
11
* which provides customers with programming information regarding the
12
* LPC products. This software is supplied "AS IS" without any warranties of
13
* any kind, and NXP Semiconductors and its licensor disclaim any and
14
* all warranties, express or implied, including all implied warranties of
15
* merchantability, fitness for a particular purpose and non-infringement of
16
* intellectual property rights. NXP Semiconductors assumes no responsibility
17
* or liability for the use of the software, conveys no license or rights under any
18
* patent, copyright, mask work right, or any other intellectual property rights in
19
* or to any products. NXP Semiconductors reserves the right to make changes
20
* in the software without notification. NXP Semiconductors also makes no
21
* representation or warranty that such application will be suitable for the
22
* specified use without further testing or modification.
23
*
24
* @par
25
* Permission to use, copy, modify, and distribute this software and its
26
* documentation is hereby granted, under NXP Semiconductors' and its
27
* licensor's relevant copyrights in the software, without fee, provided that it
28
* is used in conjunction with NXP Semiconductors microcontrollers. This
29
* copyright, permission, and disclaimer notice must appear in all copies of
30
* this code.
31
*/
32
33
#include "board.h"
34
87
/*****************************************************************************
88
* Private types/enumerations/variables
89
****************************************************************************/
90
91
#define LOOPBACK_TEST 1
92
#define SSP_MODE_TEST 1
/*1: Master, 0: Slave */
93
#define POLLING_MODE 1
94
#if POLLING_MODE
95
#define INTERRUPT_MODE 0
96
#else
97
#define INTERRUPT_MODE 1
98
#endif
99
#define BUFFER_SIZE (0x100)
100
#define SSP_DATA_BITS (SSP_BITS_8)
101
#define SSP_DATA_BIT_NUM(databits) (databits + 1)
102
#define SSP_DATA_BYTES(databits) (((databits) > SSP_BITS_8) ? 2 : 1)
103
#define SSP_LO_BYTE_MSK(databits) ((SSP_DATA_BYTES(databits) > 1) ? 0xFF : (0xFF >> \
104
(8 - SSP_DATA_BIT_NUM(databits))))
105
#define SSP_HI_BYTE_MSK(databits) ((SSP_DATA_BYTES(databits) > 1) ? (0xFF >> \
106
(16 - SSP_DATA_BIT_NUM(databits))) : 0)
107
#define LPC_SSP LPC_SSP0
108
#define SSP_IRQ SSP0_IRQn
109
#define SSPIRQHANDLER SSP0_IRQHandler
110
111
/* Tx buffer */
112
static
uint8_t
Tx_Buf
[
BUFFER_SIZE
];
113
114
/* Rx buffer */
115
static
uint8_t
Rx_Buf
[
BUFFER_SIZE
];
116
117
static
SSP_ConfigFormat
ssp_format
;
118
static
Chip_SSP_DATA_SETUP_T
xf_setup
;
119
static
volatile
uint8_t
isXferCompleted
= 0;
120
121
/*****************************************************************************
122
* Public types/enumerations/variables
123
****************************************************************************/
124
125
/*****************************************************************************
126
* Private functions
127
****************************************************************************/
128
129
/* Initialize buffer */
130
static
void
Buffer_Init
(
void
)
131
{
132
uint16_t i;
133
uint8_t ch = 0;
134
135
for
(i = 0; i <
BUFFER_SIZE
; i++) {
136
Tx_Buf
[i] = ch++;
137
Rx_Buf
[i] = 0xAA;
138
}
139
}
140
141
/* Verify buffer after transfer */
142
static
uint8_t
Buffer_Verify
(
void
)
143
{
144
uint16_t i;
145
uint8_t *src_addr = (uint8_t *) &
Tx_Buf
[0];
146
uint8_t *dest_addr = (uint8_t *) &
Rx_Buf
[0];
147
148
for
( i = 0; i <
BUFFER_SIZE
; i++ ) {
149
150
if
(((*src_addr) &
SSP_LO_BYTE_MSK
(ssp_format.
bits
)) !=
151
((*dest_addr) &
SSP_LO_BYTE_MSK
(ssp_format.
bits
))) {
152
return
1;
153
}
154
src_addr++;
155
dest_addr++;
156
157
if
(
SSP_DATA_BYTES
(ssp_format.
bits
) == 2) {
158
if
(((*src_addr) &
SSP_HI_BYTE_MSK
(ssp_format.
bits
)) !=
159
((*dest_addr) &
SSP_HI_BYTE_MSK
(ssp_format.
bits
))) {
160
return
1;
161
}
162
src_addr++;
163
dest_addr++;
164
i++;
165
}
166
}
167
return
0;
168
}
169
170
/*****************************************************************************
171
* Public functions
172
****************************************************************************/
173
#if INTERRUPT_MODE
174
178
void
SSPIRQHANDLER
(
void
)
179
{
180
Chip_SSP_Int_Disable
(
LPC_SSP
);
/* Disable all interrupt */
181
if
(
SSP_DATA_BYTES
(ssp_format.
bits
) == 1) {
182
Chip_SSP_Int_RWFrames8Bits
(
LPC_SSP
, &xf_setup);
183
}
184
else
{
185
Chip_SSP_Int_RWFrames16Bits
(
LPC_SSP
, &xf_setup);
186
}
187
188
if
((xf_setup.
rx_cnt
!= xf_setup.
length
) || (xf_setup.
tx_cnt
!= xf_setup.
length
)) {
189
Chip_SSP_Int_Enable
(
LPC_SSP
);
/* enable all interrupts */
190
}
191
else
{
192
isXferCompleted
= 1;
193
}
194
}
195
196
#endif
/*INTERRUPT_MODE*/
197
202
int
main
(
void
)
203
{
204
Board_Init
();
205
206
/* SSP initialization */
207
Board_SSP_Init
();
208
Board_LED_Set
(0,
false
);
209
Chip_SSP_Init
(
LPC_SSP
);
210
211
ssp_format.
frameFormat
=
SSP_FRAMEFORMAT_SPI
;
212
ssp_format.
bits
=
SSP_DATA_BITS
;
213
ssp_format.
clockMode
=
SSP_CLOCK_MODE0
;
214
Chip_SSP_SetFormat
(
LPC_SSP
, &ssp_format);
215
Chip_SSP_SetMaster
(
LPC_SSP
,
SSP_MODE_TEST
);
216
Chip_SSP_Enable
(
LPC_SSP
);
217
218
#if INTERRUPT_MODE
219
/* Setting SSP interrupt */
220
NVIC_EnableIRQ(
SSP_IRQ
);
221
#endif
222
223
#if LOOPBACK_TEST
224
Chip_SSP_EnableLoopBack
(
LPC_SSP
);
225
#endif
226
Buffer_Init
();
227
xf_setup.
length
=
BUFFER_SIZE
;
228
xf_setup.
tx_data
=
Tx_Buf
;
229
xf_setup.
rx_data
=
Rx_Buf
;
230
xf_setup.
rx_cnt
= xf_setup.
tx_cnt
= 0;
231
232
#if POLLING_MODE
233
Chip_SSP_RWFrames_Blocking
(
LPC_SSP
, &xf_setup);
234
#elif INTERRUPT_MODE
235
Chip_SSP_Int_FlushData
(
LPC_SSP
);
/* flush dummy data from SSP FiFO */
236
if
(
SSP_DATA_BYTES
(ssp_format.
bits
) == 1) {
237
Chip_SSP_Int_RWFrames8Bits
(
LPC_SSP
, &xf_setup);
238
}
239
else
{
240
Chip_SSP_Int_RWFrames16Bits
(
LPC_SSP
, &xf_setup);
241
}
242
243
Chip_SSP_Int_Enable
(
LPC_SSP
);
/* enable interrupt */
244
while
(!
isXferCompleted
) {}
245
#endif
/*INTERRUPT_MODE*/
246
247
if
(
Buffer_Verify
()) {
248
Board_LED_Set
(0,
false
);
/* Error */
249
}
250
else
{
251
Board_LED_Set
(0,
true
);
/* Success */
252
}
253
254
#if LOOPBACK_TEST
255
Chip_SSP_DisableLoopBack
(
LPC_SSP
);
256
#endif
257
/* DeInitialize SPI peripheral */
258
Chip_SSP_DeInit
(
LPC_SSP
);
259
260
while
(1){}
261
return
0;
262
}
263
applications
lpc11xx
examples
periph
periph_ssp
ssp.c
Generated on Fri May 10 2013 10:42:08 for LPCOpen Platform by
1.8.2