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 703

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 542 julius
int
250
console_get_num(void)
251
{
252
        char c = 0x30;
253
        int num_nums = 0;
254
        int num_nums_total;
255
        char nums[16]; // up to 16 decimal digits long
256
        int retval = 0;
257
        int decimal_multiplier;
258
        int i;
259
 
260
        printf("Enter decimal value: ");
261
 
262
        while (c >= 0x30 && c < 0x40)
263
        {
264
                c = uart_getc(DEFAULT_UART);
265
 
266
                if (c >= 0x30 && c < 0x40)
267
                {
268
                        printf("%d", c-0x30);
269
                        nums[num_nums] = c-0x30;
270
                        num_nums++;
271
                }
272
 
273
        }
274
        printf("\n");
275
 
276
        num_nums_total = num_nums;
277
 
278
        while(num_nums--)
279
        {
280
                decimal_multiplier = 1;
281
                for(i=1;i<num_nums_total - num_nums;i++)
282
                        decimal_multiplier *= 10;
283
                //printf("%d * %d\n",decimal_multiplier,nums[num_nums]);
284
 
285
                retval += (decimal_multiplier * nums[num_nums]);
286
        }
287
        //printf("%d\n",retval);
288
        return retval;
289
}
290
 
291
 
292
// HEX chars in ASCII:
293
// 0: 0x30 (48), 1: 0x31 ... 9: 0x39
294
// A: 0x41 (65), B: 0x42 ... F: 0x46
295
// a: 0x61 (97), b: 0x62 ... f: 0x66
296
 
297
#define IS_ASCII_HEX_CHAR(x) ((x>=0x41 && x<=0x46) || (x>=0x61 && x<=0x66) || \
298
                              (x>=0x30 && x<=0x39))
299
 
300
#define ASCII_TO_HEX_VAL(x) (x>=0x41 && x<=0x46) ? x - 55 :     \
301
        (x>=0x61 && x<=0x66) ? x - 87 : x - 48;
302
unsigned long
303
console_get_hex_num(void)
304
{
305
        char c = 0x30;
306
        char hexchar;
307
        int num_nums = 0;
308
        int num_nums_total;
309
        char nums[8]; // up to 8 decimal digits long
310
        unsigned long retval = 0;
311
        int base_multiplier;
312
        int i;
313
 
314
        printf("Enter hex value: ");
315
 
316
        while (IS_ASCII_HEX_CHAR(c))
317
        {
318
                c = uart_getc(DEFAULT_UART);
319
 
320
                if (IS_ASCII_HEX_CHAR(c) && num_nums < 8)
321
                {
322
                        //printf("%c 0x%02x", c&0xff, c&0xff);
323
                        hexchar = ASCII_TO_HEX_VAL(c);
324
                        printf("%1x", hexchar&0xff);
325
                        nums[num_nums] = hexchar;
326
                        num_nums++;
327
                }
328
                if (c==0x7f) //delete
329
                {
330
                        if (num_nums>0)
331
                        {
332
                                printf("%c %c",0x8, 0x8);
333
                                num_nums--;
334
                        }
335
                        c = 0x30;
336
                }
337
 
338
        }
339
        printf("\n");
340
 
341
        num_nums_total = num_nums;
342
 
343
        while(num_nums--)
344
        {
345
                base_multiplier = 1;
346
                for(i=1;i<num_nums_total - num_nums;i++)
347
                        base_multiplier *= 16;
348
                //printf("%d * %d\n",base_multiplier,nums[num_nums]);
349
 
350
                retval += (base_multiplier * nums[num_nums]);
351
        }
352
        //printf("%d\n",retval);
353
 
354
        return retval;
355
}
356
 
357
 
358
void
359
console_browse_buffer(char* buf)
360
{
361
        char c = 0;
362
        int offset = 0;
363
        const int linesize = 16;
364
        int i;
365
        printf("Press space to scroll through buffer, q to return\n");
366
        printf("+/- alter address offset\n");
367
        while (1)
368
        {
369
                c = uart_getc(DEFAULT_UART);
370
 
371
                if (c == 'q')
372
                        return;
373
                else if (c == 'r')
374
                        offset=0;
375
                else if (c == '+')
376
                {
377
                        if (offset <= (256 - linesize))
378
                                offset+=linesize;
379
                        printf("%04x:\r",offset);
380
                }
381
                else if (c == '-')
382
                {
383
                        if (offset >=linesize)
384
                                offset-=linesize;
385
 
386
                        printf("%04x:\r",offset);
387
                }
388
                else if (c == 0x20 && (offset < 256)) // space, print al ine
389
                {
390
                        printf("%04x:",offset);
391
                        // print another line of the buffer
392
                        for (i=0;i<linesize;i++)
393
                        {
394
                                printf(" %02x", buf[offset+i]&0xff);
395
                        }
396
                        printf("\n");
397
                        if (offset <= (256 - linesize))
398
                                offset += linesize;
399
 
400
                }
401
        }
402
 
403
}
404
 
405
 
406 349 julius
int
407
main()
408
{
409
 
410
  uart_init(0); // init the UART before we can printf
411
 
412
  volatile char c;
413
  int i,j;
414 542 julius
  char browse_buf[256];
415 349 julius
  spi_master = 0;
416
  slave = 1;
417
 
418
  spi_core_slave_select(spi_master, 0); // Deselect slaves
419
 
420
  // Clear the read FIFO
421
  while (spi_core_data_avail(spi_master))
422
    c = spi_core_read_data(spi_master);
423
 
424
  programming_file_start = (unsigned long) &spiprogram_data;
425
  programming_file_end = (unsigned long) &end_spiprogram_data;
426
  programming_file_length = programming_file_end - programming_file_start;
427
 
428
  // SPI core 0, should already be configured to read out data
429
  // when we reset.
430
 
431
  printf("\n\n\tSPI flash programming app\n\n");
432
 
433
  while(1){
434
    printf("[p,v,s,h] > ");
435
    c = uart_getc(DEFAULT_UART);
436
    printf("%c",c);
437
    printf("\n");
438
 
439
    if (c == 'h')
440
      printhelp();
441
    else if (c == 's')
442
      print_spi_status();
443
    else if (c == 'p')
444
      program_spi(spi_master, slave, (char *) &spiprogram_data, programming_file_length);
445
    else if (c == 'v')
446
      verify_spi(spi_master, slave, (char *) &spiprogram_data, programming_file_length);
447 542 julius
    else if ( c== 'r')
448
    {
449
            printf("Read page\n");
450
            spi_read_block(spi_master, slave, ((console_get_num())<<8),
451
                           256,
452
                           browse_buf);
453
            console_browse_buffer(browse_buf);
454
    }
455 349 julius
 
456
 
457
  }
458
 
459
  return 0;
460
 
461
}

powered by: WebSVN 2.1.0

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