View difference between Paste ID: EYeUznsZ and YpSK6r8h
SHOW: | | - or go back to the newest paste.
1
//*****************************************************************************
2
//
3
// spi_master.c - Example demonstrating how to configure SSI0 in SPI master
4
//                mode.
5
//
6
// Copyright (c) 2010-2013 Texas Instruments Incorporated.  All rights reserved.
7
// Software License Agreement
8
//
9
//   Redistribution and use in source and binary forms, with or without
10
//   modification, are permitted provided that the following conditions
11
//   are met:
12
//
13
//   Redistributions of source code must retain the above copyright
14
//   notice, this list of conditions and the following disclaimer.
15
//
16
//   Redistributions in binary form must reproduce the above copyright
17
//   notice, this list of conditions and the following disclaimer in the
18
//   documentation and/or other materials provided with the
19
//   distribution.
20
//
21
//   Neither the name of Texas Instruments Incorporated nor the names of
22
//   its contributors may be used to endorse or promote products derived
23
//   from this software without specific prior written permission.
24
//
25
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
//
37
// This is part of revision 2.0.1.11577 of the Tiva Firmware Development Package.
38
//
39
//*****************************************************************************
40
41
#include <stdbool.h>
42
#include <stdint.h>
43
#include "inc/hw_memmap.h"
44
#include "driverlib/gpio.h"
45
#include "driverlib/pin_map.h"
46
#include "driverlib/ssi.h"
47
#include "driverlib/sysctl.h"
48
#include "driverlib/uart.h"
49
#include "utils/uartstdio.h"
50
51
//*****************************************************************************
52
//
53
//! \addtogroup ssi_examples_list
54
//! <h1>SPI Master (spi_master)</h1>
55
//!
56
//! This example shows how to configure the SSI0 as SPI Master.  The code will
57
//! send three characters on the master Tx then polls the receive FIFO until
58
//! 3 characters are received on the master Rx.
59
//!
60
//! This example uses the following peripherals and I/O signals.  You must
61
//! review these and change as needed for your own board:
62
//! - SSI0 peripheral
63
//! - GPIO Port A peripheral (for SSI0 pins)
64
//! - SSI0Clk - PA2
65
//! - SSI0Fss - PA3
66
//! - SSI0Rx  - PA4
67
//! - SSI0Tx  - PA5
68
//!
69
//! The following UART signals are configured only for displaying console
70
//! messages for this example.  These are not required for operation of SSI0.
71
//! - UART0 peripheral
72
//! - GPIO Port A peripheral (for UART0 pins)
73
//! - UART0RX - PA0
74
//! - UART0TX - PA1
75
//!
76
//! This example uses the following interrupt handlers.  To use this example
77
//! in your own application you must add these interrupt handlers to your
78
//! vector table.
79
//! - None.
80
//
81
//*****************************************************************************
82
83
//*****************************************************************************
84
//
85
// Number of bytes to send and receive.
86
//
87
//*****************************************************************************
88
#define NUM_SSI_DATA            5
89
90
void sendReceiveSPI(uint32_t *pui32DataTx, uint32_t *pui32DataRx, uint32_t size);
91
92
//*****************************************************************************
93
//
94
// This function sets up UART0 to be used for a console to display information
95
// as the example is running.
96
//
97
//*****************************************************************************
98
void
99
InitConsole(void)
100
{
101
    //
102
    // Enable GPIO port A which is used for UART0 pins.
103
    // TODO: change this to whichever GPIO port you are using.
104
    //
105
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
106
107
    //
108
    // Configure the pin muxing for UART0 functions on port A0 and A1.
109
    // This step is not necessary if your part does not support pin muxing.
110
    // TODO: change this to select the port/pin you are using.
111
    //
112
    GPIOPinConfigure(GPIO_PA0_U0RX);
113
    GPIOPinConfigure(GPIO_PA1_U0TX);
114
115
    //
116
    // Enable UART0 so that we can configure the clock.
117
    //
118
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
119
120
    //
121
    // Use the internal 16MHz oscillator as the UART clock source.
122
    //
123
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
124
125
    //
126
    // Select the alternate (UART) function for these pins.
127
    // TODO: change this to select the port/pin you are using.
128
    //
129
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
130
131
    //
132
    // Initialize the UART for console I/O.
133
    //
134
    UARTStdioConfig(0, 115200, 16000000);
135
}
136
137
//*****************************************************************************
138
//
139
// Configure SSI0 in master Freescale (SPI) mode.  This example will send out
140
// 3 bytes of data, then wait for 3 bytes of data to come in.  This will all be
141
// done using the polling method.
142
//
143
//*****************************************************************************
144
int main(void) {
145
    uint32_t pui32DataTx[NUM_SSI_DATA];
146
    uint32_t pui32DataRx[NUM_SSI_DATA];
147
148
    //
149
    // Set the clocking to run directly from the external crystal/oscillator.
150
    // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
151
    // crystal on your board.
152
    //
153-
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
153+
//    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
154-
                   SYSCTL_XTAL_16MHZ);
154+
//    		SYSCTL_XTAL_4_91MHZ);
155
    SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_25MHZ |
156
                           SYSCTL_OSC_MAIN);
157
158
    //
159
    // Set up the serial console to use for displaying messages.  This is
160
    // just for this example program and is not needed for SSI operation.
161
    //
162
    InitConsole();
163
164
    //
165
    // Display the setup on the console.
166
    //
167
    UARTprintf("\n\n\n--------------------------------------\n");
168
    if (SysCtlClockGet()/1000000 > 0)
169
    	UARTprintf("System Clock Speed %dMHz\n",SysCtlClockGet()/1000000);
170
    else if (SysCtlClockGet()/1000 > 0)
171
    	UARTprintf("System Clock Speed %dKHz\n",SysCtlClockGet()/1000);
172
    else
173
    	UARTprintf("System Clock Speed %dHz\n",SysCtlClockGet());
174
175
176
177
    SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
178
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
179
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
180
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
181
182
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_4);
183
    GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_6|GPIO_PIN_7);
184
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_6|GPIO_PIN_7);
185
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_PIN_4);
186
187
    GPIOPinConfigure(GPIO_PA2_SSI0CLK);
188
    GPIOPinConfigure(GPIO_PA3_SSI0FSS);
189
    GPIOPinConfigure(GPIO_PA4_SSI0RX);
190
    GPIOPinConfigure(GPIO_PA5_SSI0TX);
191
192
    // The pins are assigned as follows:
193
    //      PA5 - SSI0Tx
194
    //      PA4 - SSI0Rx
195
    //      PA3 - SSI0Fss
196
    //      PA2 - SSI0CLK
197
    GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 | GPIO_PIN_3 |
198
                   GPIO_PIN_2);
199
200
    //
201
    // Configure and enable the SSI port for SPI master mode.  Use SSI0,
202-
    SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,
202+
203-
                       SSI_MODE_MASTER, 2000000, 8);
203+
204
    // For SPI mode, you can set the polarity of the SSI clock when the SSI
205
    // unit is idle.  You can also configure what clock edge you want to
206
    // capture data on.  Please reference the datasheet for more information on
207
    // the different SPI modes.
208
    //
209
    SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_3,
210
                       SSI_MODE_MASTER, 8333, 8);
211
    SSIEnable(SSI0_BASE);
212
213-
    pui32DataTx[0] = 0x00;
213+
214
215
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6, 0);
216
    SysCtlDelay(SysCtlClockGet()/3000);
217
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6, GPIO_PIN_6);
218
    SysCtlDelay(SysCtlClockGet());
219-
    sendReceiveSPI(pui32DataTx, pui32DataRx, 5);
219+
220
    /*pui32DataTx[0] = 0x00;
221-
    pui32DataTx[0] = 0x00;
221+
222-
	pui32DataTx[1] = 0x09;
222+
223
    pui32DataTx[3] = 0x01;
224
    pui32DataTx[4] = 0x10;
225
226
    sendReceiveSPI(pui32DataTx, pui32DataRx, 5);*/
227-
	sendReceiveSPI(pui32DataTx, pui32DataRx, 5);
227+
    /*while(1) {
228
    	GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6, 0);
229
    	SysCtlDelay(SysCtlClockGet()/3000000);
230
    	GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6, GPIO_PIN_6);
231
    	SysCtlDelay(SysCtlClockGet()/3000000);
232
    }*/
233
234
    pui32DataTx[0] = 0x40;
235
	pui32DataTx[1] = 0x1e;
236
	pui32DataTx[2] = 0x00;
237-
	GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_7, 0);
237+
238
	pui32DataTx[4] = 0x00;
239
	uint32_t i = 0;
240
    while (i++ < 2) {
241
		sendReceiveSPI(pui32DataTx, pui32DataRx, 5);
242
243
    }
244-
	GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_7, GPIO_PIN_7);
244+
245
    while (1);
246
}
247
248
249
void sendReceiveSPI(uint32_t *pui32DataTx, uint32_t *pui32DataRx, uint32_t size) {
250
	uint32_t ui32Index;
251
252
	UARTprintf("\nSending\t\t");
253
	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0);
254
	for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++)
255
		UARTprintf("0x%02x ",pui32DataTx[ui32Index]);
256
257
	for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++)
258
		SSIDataPut(SSI0_BASE, pui32DataTx[ui32Index]);
259
260
	while(SSIBusy(SSI0_BASE));
261
	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_PIN_4);
262
263
	UARTprintf("\nReceiving\t");
264
	for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++) {
265
		SSIDataGet(SSI0_BASE, &pui32DataRx[ui32Index]);
266
		pui32DataRx[ui32Index] &= 0x00FF;
267
		UARTprintf("0x%02x ",pui32DataRx[ui32Index]);
268
	}
269
	UARTprintf("\n");
270
	SysCtlDelay(SysCtlClockGet()/6);
271
}