LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 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, 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 
86 /*****************************************************************************
87  * Private types/enumerations/variables
88  ****************************************************************************/
89 
90 #define LOOPBACK_TEST 1
91 #define SSP_MODE_TEST 1 /*1: Master, 0: Slave */
92 #define POLLING_MODE 0
93 #if POLLING_MODE
94 #define INTERRUPT_MODE 0
95 #else
96 #define INTERRUPT_MODE 1
97 #endif
98 #define BUFFER_SIZE (0x100)
99 #define SSP_DATA_BITS (SSP_BITS_8)
100 #define SSP_DATA_BIT_NUM(databits) (databits + 1)
101 #define SSP_DATA_BYTES(databits) (((databits) > SSP_BITS_8) ? 2 : 1)
102 #define SSP_LO_BYTE_MSK(databits) ((SSP_DATA_BYTES(databits) > 1) ? 0xFF : (0xFF >> \
103  (8 - SSP_DATA_BIT_NUM(databits))))
104 #define SSP_HI_BYTE_MSK(databits) ((SSP_DATA_BYTES(databits) > 1) ? (0xFF >> \
105  (16 - SSP_DATA_BIT_NUM(databits))) : 0)
106 #define LPC_SSP LPC_SSP0
107 #define SSP_IRQ SSP0_IRQn
108 #define SSPIRQHANDLER SSP0_IRQHandler
109 
110 /* Tx buffer */
111 static uint8_t Tx_Buf[BUFFER_SIZE];
112 
113 /* Rx buffer */
114 static uint8_t Rx_Buf[BUFFER_SIZE];
115 
118 static volatile uint8_t isXferCompleted = 0;
119 
120 /*****************************************************************************
121  * Public types/enumerations/variables
122  ****************************************************************************/
123 
124 /*****************************************************************************
125  * Private functions
126  ****************************************************************************/
127 
128 /* Initialize buffer */
129 static void Buffer_Init(void)
130 {
131  uint16_t i;
132  uint8_t ch = 0;
133 
134  for (i = 0; i < BUFFER_SIZE; i++) {
135  Tx_Buf[i] = ch++;
136  Rx_Buf[i] = 0xAA;
137  }
138 }
139 
140 /* Verify buffer after transfer */
141 static uint8_t Buffer_Verify(void)
142 {
143  uint16_t i;
144  uint8_t *src_addr = (uint8_t *) &Tx_Buf[0];
145  uint8_t *dest_addr = (uint8_t *) &Rx_Buf[0];
146 
147  for ( i = 0; i < BUFFER_SIZE; i++ ) {
148 
149  if (((*src_addr) & SSP_LO_BYTE_MSK(ssp_format.bits)) !=
150  ((*dest_addr) & SSP_LO_BYTE_MSK(ssp_format.bits))) {
151  return 1;
152  }
153  src_addr++;
154  dest_addr++;
155 
156  if (SSP_DATA_BYTES(ssp_format.bits) == 2) {
157  if (((*src_addr) & SSP_HI_BYTE_MSK(ssp_format.bits)) !=
158  ((*dest_addr) & SSP_HI_BYTE_MSK(ssp_format.bits))) {
159  return 1;
160  }
161  src_addr++;
162  dest_addr++;
163  i++;
164  }
165  }
166  return 0;
167 }
168 
169 /*****************************************************************************
170  * Public functions
171  ****************************************************************************/
172 #if INTERRUPT_MODE
173 
177 void SSPIRQHANDLER(void)
178 {
179  Chip_SSP_Int_Disable(LPC_SSP); /* Disable all interrupt */
180  if (SSP_DATA_BYTES(ssp_format.bits) == 1) {
182  }
183  else {
185  }
186 
187  if ((xf_setup.rx_cnt != xf_setup.length) || (xf_setup.tx_cnt != xf_setup.length)) {
188  Chip_SSP_Int_Enable(LPC_SSP); /* enable all interrupts */
189  }
190  else {
191  isXferCompleted = 1;
192  }
193 }
194 
195 #endif /*INTERRUPT_MODE*/
196 
201 int main(void)
202 {
203  Board_Init();
204 
205  /* SSP initialization */
207  Board_LED_Set(0, false);
209 
210  ssp_format.frameFormat = SSP_FRAMEFORMAT_SPI;
211  ssp_format.bits = SSP_DATA_BITS;
212  ssp_format.clockMode = SSP_CLOCK_MODE0;
213  Chip_SSP_SetFormat(LPC_SSP, &ssp_format);
216 
217 #if INTERRUPT_MODE
218  /* Setting SSP interrupt */
219  NVIC_EnableIRQ(SSP_IRQ);
220 #endif
221 
222 #if LOOPBACK_TEST
224 #endif
225  Buffer_Init();
226  xf_setup.length = BUFFER_SIZE;
227  xf_setup.tx_data = Tx_Buf;
228  xf_setup.rx_data = Rx_Buf;
229  xf_setup.rx_cnt = xf_setup.tx_cnt = 0;
230 
231 #if POLLING_MODE
233 #elif INTERRUPT_MODE
234  Chip_SSP_Int_FlushData(LPC_SSP);/* flush dummy data from SSP FiFO */
235  if (SSP_DATA_BYTES(ssp_format.bits) == 1) {
237  }
238  else {
240  }
241 
242  Chip_SSP_Int_Enable(LPC_SSP); /* enable interrupt */
243  while (!isXferCompleted) {}
244 #endif /*INTERRUPT_MODE*/
245 
246  if (Buffer_Verify()) {
247  Board_LED_Set(0, false); /* Error */
248  }
249  else {
250  Board_LED_Set(0, true); /* Success */
251  }
252 
253 #if LOOPBACK_TEST
255 #endif
256  /* DeInitialize SPI peripheral */
258 
259  while (1) {
260  __WFI();
261  }
262 }
263