OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [sw/] [apps/] [spiflash/] [spiflash-program.c] - Blame information for rev 408

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 349 julius
// Program that will erase and program an SPI flash
2
 
3 408 julius
#include "cpu-utils.h"
4
 
5 349 julius
#include "board.h"
6
#include "uart.h"
7
#include "simple-spi.h"
8
#include "printf.h"
9
 
10
 
11
unsigned long programming_file_start;
12
unsigned long programming_file_end;
13
unsigned long programming_file_length;
14
int spi_master;
15
char slave;
16
 
17
// Little program to dump the contents of the SPI flash memory it's connected 
18
// to on the board
19
 
20
void
21
spi_write_ignore_read(int core, char dat)
22
{
23
  spi_core_write_data(core,dat);
24
  while (!(spi_core_data_avail(core))); // Wait for the transaction (should 
25
                                        // generate a byte)
26
  spi_core_read_data(core);
27
}
28
 
29
char
30
spi_read_ignore_write(int core)
31
{
32
  spi_core_write_data(core, 0x00);
33
  while (!(spi_core_data_avail(core))); // Wait for the transaction (should 
34
                                        // generate a byte)
35
  return spi_core_read_data(core);
36
}
37
 
38
 
39
unsigned long
40
spi_read_id(int core, char slave_sel)
41
{
42
  unsigned long rdid;
43
  char* rdid_ptr = (char*) &rdid;
44
  int i;
45
  spi_core_slave_select(core, slave_sel); // Select slave
46
  rdid_ptr[3] = 0;
47
  // Send the RDID command
48
  spi_write_ignore_read(core,0x9f); // 0x9f is READ ID command
49
  // Now we read the next 3 bytes
50
  for(i=0;i<3;i++)
51
    {
52
      rdid_ptr[i] = spi_read_ignore_write(core);
53
    }
54
  spi_core_slave_select(core, 0); // Deselect slave
55
  return rdid;
56
}
57
 
58
// Read status regsiter
59
char
60
spi_read_sr(int core, char slave_sel)
61
{
62
  char rdsr;
63
  spi_core_slave_select(core, slave_sel); // Select slave
64
  // Send the RDSR command
65
  spi_write_ignore_read(core,0x05); // 0x05 is READ status register command
66
  rdsr = spi_read_ignore_write(core);
67
  spi_core_slave_select(core, 0); // Deselect slave  
68
  return rdsr;
69
}
70
 
71
 
72
void
73
spi_read_block(int core, char slave_sel, unsigned int addr, int num_bytes,
74
               char* buf)
75
{
76
  int i;
77
  spi_core_slave_select(core, slave_sel); // Select slave
78
  spi_write_ignore_read(core, 0x3); // READ command
79
  spi_write_ignore_read(core,((addr >> 16) & 0xff)); // addres high byte
80
  spi_write_ignore_read(core,((addr >> 8) & 0xff)); // addres middle byte
81
  spi_write_ignore_read(core,((addr >> 0) & 0xff)); // addres low byte
82
  for(i=0;i<num_bytes;i++)
83
    buf[i] = spi_read_ignore_write(core);
84
 
85
  spi_core_slave_select(core, 0); // Deselect slave  
86
}
87
 
88
 
89
void
90
spi_set_write_enable(int core, char slave_sel)
91
{
92
  spi_core_slave_select(core, slave_sel); // Select slave
93
  spi_write_ignore_read(core,0x06); // 0x06 is to set write enable
94
  spi_core_slave_select(core, 0); // Deselect slave  
95
}
96
 
97
 
98
// Write up to 256 bytes of data to a page, always from offset 0
99
void
100
spi_page_program(int core, char slave_sel, short page_num, int num_bytes,
101
                 char* buf)
102
{
103
 
104
  // Set WE latch
105
  spi_set_write_enable(core, slave_sel);
106
  while(!(spi_read_sr(core, slave_sel) & 0x2)); // Check it's set
107
 
108
  int i;
109
  if (!(num_bytes > 0)) return;
110
  if (num_bytes > 256) return;
111
  spi_core_slave_select(core, slave_sel); // Select slave
112
  spi_write_ignore_read(core, 0x2); // Page program command
113
  spi_write_ignore_read(core,((page_num >> 8) & 0xff)); // addres high byte
114
  spi_write_ignore_read(core,((page_num >> 0) & 0xff)); // addres middle byte
115
  spi_write_ignore_read(core,0); // addres low byte
116
  for(i=0;i<num_bytes;i++)
117
    spi_write_ignore_read(core, buf[i]);
118
  spi_core_slave_select(core, 0); // Deselect slave  
119
 
120
 // Now poll status reg for WIP bit
121
  while((spi_read_sr(core, slave_sel) & 0x1));
122
}
123
 
124
 
125
// Erase a sector - assumes 128KByte memory, so 4 sectors of 32KBytes each
126
void
127
spi_sector_erase(int core, char slave_sel, unsigned int sector)
128
{
129
  // Set WE latch
130
  spi_set_write_enable(core, slave_sel);
131
  while(!(spi_read_sr(core, slave_sel) & 0x2)); // Check it's set
132
 
133
  spi_core_slave_select(core, slave_sel); // Select slave
134
  spi_write_ignore_read(core, 0xd8); // Sector erase command
135
  spi_write_ignore_read(core,(sector>>1)&0x1); // sector select high bit (bit 
136
                                               // 16 of addr)
137
  spi_write_ignore_read(core,(sector&0x1)<<7); // sector select low bit (bit 15
138
                                               // of addr)
139
  spi_write_ignore_read(core,0); // addres low byte  
140
  spi_core_slave_select(core, 0); // Deselect slave  
141
 
142
  // Now poll status reg for WIP bit
143
  while((spi_read_sr(core, slave_sel) & 0x1));
144
 
145
}
146
 
147
// Erase entire device
148
void
149
spi_bulk_erase(int core, char slave_sel)
150
{
151
 // Set WE latch
152
  spi_set_write_enable(core, slave_sel);
153
  while(!(spi_read_sr(core, slave_sel) & 0x2)); // Check it's set
154
 
155
  spi_core_slave_select(core, slave_sel); // Select slave
156
  spi_write_ignore_read(core, 0xc7); // Bulk erase
157
  spi_core_slave_select(core, 0); // Deselect slave  
158
 
159
 // Now poll status reg for WIP bit
160
  while((spi_read_sr(core, slave_sel) & 0x1));
161
}
162
 
163
extern unsigned long spiprogram_data, _spiprogram_data;
164
extern unsigned long end_spiprogram_data, _end_spiprogram_data;
165
 
166
#define printhelp() printf("\nUsage: \n\t[p]rogram\t\twrite program to flash\n\t[v]erify\t\tveryify written program\n\t[s]tatus\t\tprint status of SPI flash\n\n")
167
 
168
void
169
print_spi_status(void)
170
{
171
 
172
  printf("SPI core: %d\n",spi_master);
173
  printf("SPI slave select: 0x%x\n",slave&0xff);
174
 
175
  printf("SPI slave info:\n");
176
  printf("\tID:\t%x\n", spi_read_id(spi_master, slave));
177
  printf("\tSR:\t%x\n", spi_read_sr(spi_master, slave));
178
  printf("\n");
179
  printf("Programming file from 0x%x-0x%x, %d bytes\n",programming_file_start,
180
         programming_file_end,
181
         programming_file_length);
182
  printf("Embedded length: %d\n", (unsigned long) spiprogram_data);
183
}
184
 
185
 
186
// Verify contents of SPI flash
187
void
188
verify_spi(int core, char slave_sel, char * data, unsigned int length)
189
{
190
  unsigned int i;
191
  unsigned int page_num = 0;
192
  unsigned int bytes_this_page;
193
  char verify_buf[256];
194
  int verify_error = 0;
195
  printf("* Verifying contents of SPI flash.\n");
196
  while(length)
197
    {
198
      bytes_this_page = (length >= 256) ? 256 : length;
199
      spi_read_block(spi_master, slave, (page_num<<8), bytes_this_page,
200
                     verify_buf);
201
      for(i=0;i<bytes_this_page;i++)
202
        {
203
          if (verify_buf[i] != data[i])
204
            {
205
              printf("* Verify error! Byte %d of page %d - was 0x%x, expected 0x%x\n", i, page_num, verify_buf[i] & 0xff, data[i] & 0xff);
206
              verify_error=1;
207
            }
208
        }
209
      data +=256;
210
      length -= (length >= 256) ? 256 : length;
211
      page_num++;
212
    }
213
  if (verify_error)
214
    printf("* SPI flash verify NOT OK\n");
215
  else
216
    printf("* SPI flash verify OK\n");
217
 
218
}
219
 
220
// Function to fully erase and program SPI flash
221
void
222
program_spi(int core, char slave_sel, char * data, unsigned int length)
223
{
224
  char *program_data_ptr = data;
225
  unsigned int program_data_length = length;
226
  unsigned int i;
227
  unsigned int page_num = 0;
228
  unsigned int bytes_this_page;
229
  printf("* Erasing SPI flash\n");
230
  spi_bulk_erase(core,slave_sel);
231
  printf("* SPI flash erased\n");
232
  while(length)
233
    {
234
      bytes_this_page = (length >= 256) ? 256 : length;
235
      printf("* SPI page %d being programmed with %d bytes", page_num, bytes_this_page);
236
      spi_page_program(core, slave_sel, page_num, bytes_this_page, data);
237
      printf("\n");
238
      data +=256;
239
      length -= (length >= 256) ? 256 : length;
240
      page_num++;
241
    }
242
  printf("* SPI flash programmed\n");
243
 
244
  verify_spi(core,slave_sel,program_data_ptr,program_data_length);
245
 
246
}
247
 
248
 
249
int
250
main()
251
{
252
 
253
  uart_init(0); // init the UART before we can printf
254
 
255
  volatile char c;
256
  int i,j;
257
  spi_master = 0;
258
  slave = 1;
259
 
260
  spi_core_slave_select(spi_master, 0); // Deselect slaves
261
 
262
  // Clear the read FIFO
263
  while (spi_core_data_avail(spi_master))
264
    c = spi_core_read_data(spi_master);
265
 
266
  programming_file_start = (unsigned long) &spiprogram_data;
267
  programming_file_end = (unsigned long) &end_spiprogram_data;
268
  programming_file_length = programming_file_end - programming_file_start;
269
 
270
  // SPI core 0, should already be configured to read out data
271
  // when we reset.
272
 
273
  printf("\n\n\tSPI flash programming app\n\n");
274
 
275
  while(1){
276
    printf("[p,v,s,h] > ");
277
    c = uart_getc(DEFAULT_UART);
278
    printf("%c",c);
279
    printf("\n");
280
 
281
    if (c == 'h')
282
      printhelp();
283
    else if (c == 's')
284
      print_spi_status();
285
    else if (c == 'p')
286
      program_spi(spi_master, slave, (char *) &spiprogram_data, programming_file_length);
287
    else if (c == 'v')
288
      verify_spi(spi_master, slave, (char *) &spiprogram_data, programming_file_length);
289
 
290
 
291
  }
292
 
293
  return 0;
294
 
295
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.