1 /*
2 *
3 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
36 /**
37 * @file spiWrite.c
38 *
39 * @brief
40 * Write to an SPI eeprom
41 */
43 #include "types.h"
44 #include "spi_api.h"
45 #include "target.h"
46 #include "pllapi.h"
47 #include <stdio.h>
48 #include <string.h>
50 #pragma DATA_SECTION(txData, ".tx_data")
51 #pragma DATA_SECTION(rxData, ".rx_data")
53 #define MAX_DATA_SIZE 0x40000 /* 256 kb */
54 uint32 txData[MAX_DATA_SIZE >> 2];
55 uint32 rxData[MAX_DATA_SIZE >> 2];
57 #define MAX_BLOCK_SIZE 2048
58 uint8 scratchBlock[MAX_BLOCK_SIZE];
60 unsigned int nbytes = MAX_DATA_SIZE;
61 unsigned int startAddress = 0;
63 /* Run time configuration */
64 unsigned int deviceFreqMhz = 1000;
65 unsigned int prediv = 1;
66 unsigned int mult = 20;
67 unsigned int postdiv = 2;
69 unsigned int spiBlockSize = 256;
70 unsigned int spiSectorSize = 4096; /* Erase size */
72 spiConfig_t cfg = {
74 0, /* port */
75 SPI_MODE, /* mode */
76 SPI_ADDR_WIDTH, /* address width */
77 SPI_NPIN, /* number of pins */
78 SPI_CSEL, /* csel */
79 SPI_CLKDIV, /* clock divider */
80 SPI_C2TDEL /* ct2delay */
82 };
86 int main (void)
87 {
88 int result;
89 int firstSector;
90 int lastSector;
91 int nblocks;
92 int i, j, k;
94 unsigned int addr;
96 /* This writer requires that the start address be block aligned */
97 if ((startAddress % spiBlockSize) != 0) {
98 printf ("spiWriter requires that the start address be block aligned, exiting\n");
99 return (-1);
100 }
102 /* Check that the block size is within range */
103 if (spiBlockSize > MAX_BLOCK_SIZE) {
104 printf ("The block size selected (%d) is greater then the max size of %d.\n", spiBlockSize, MAX_BLOCK_SIZE);
105 printf ("Increase the value of MAX_BLOCK_SIZE and recompile the program\n");
106 return (-1);
107 }
109 /* Power up the SPI */
110 #ifdef TARGET_PWR_SPI
111 devicePowerPeriph (TARGET_PWR_SPI);
112 #endif
115 /* Program the main system PLL */
116 hwPllSetPll (0, /* Main PLL */
117 prediv, /* Pre-divider */
118 mult, /* Multiplier */
119 postdiv); /* Post-divider */
122 result = hwSpiConfig (&cfg);
123 if (result != 0) {
124 printf ("hwSpiConfig returned error %d\n", result);
125 return (-1);
126 }
128 /* Erase all sectors that will be used */
129 firstSector = startAddress / spiSectorSize;
130 lastSector = (startAddress + nbytes + spiSectorSize - 1) / spiSectorSize;
132 printf ("Erasing sector: ");
133 for (i = firstSector; i <= lastSector; i++) {
134 printf ("%d ", i);
135 eraseSector (&cfg, i * spiSectorSize);
136 }
138 printf ("done\n");
141 /* Program all the data */
142 nblocks = (nbytes + spiBlockSize - 1) / spiBlockSize;
144 printf ("Writing blocks ");
145 for (i = 0, addr = startAddress; i < nblocks; i++, addr += spiBlockSize) {
147 result = writeBlock (&cfg, addr, &txData[(i * spiBlockSize) >> 2], spiBlockSize, scratchBlock);
148 if (result != 0) {
149 printf ("\nwriteBlock returned error code %d, exiting\n", result);
150 return (-1);
151 }
153 printf (".");
154 }
156 printf ("\n");
159 /* Read the data as bytes. Translate it to words */
160 printf ("Reading blocks ");
161 for (i = j = 0, addr = startAddress; i < nblocks; i++, addr += spiBlockSize) {
163 result = readBlock (&cfg, addr, scratchBlock, spiBlockSize);
165 if (result != 0) {
166 printf ("\nreadBlock returned error code %d, exiting\n", result);
167 return (-1);
168 }
170 for (k = 0; k < spiBlockSize; k += 4)
171 rxData[j++] = (scratchBlock[k+0] << 24) | (scratchBlock[k+1] << 16) | (scratchBlock[k+2] << 8) | scratchBlock[k+3];
173 printf (".");
174 }
176 printf ("\n");
178 /* Compare the results */
179 printf ("Checking the read back\n");
181 for (i = j = 0; i < nbytes >> 2; i++) {
183 if (txData[i] != rxData[i]) {
185 j += 1;
186 printf ("failure at word %d: Expected 0x%04x, found 0x%04x\n", i, txData[i], rxData[i]);
188 }
190 if (j >= 5) {
191 printf ("Too many errors, quitting\n");
192 break;
193 }
194 }
196 if (j == 0)
197 printf ("Read back verified\n");
200 return (0);
202 }