qspi_sdk_zynq

 

线性地址模式下下,只支持读,不支持写(spi to axi是数据,AXI TO SPI是指令转换)

 

所以更常用I/O模,因为可以读写。

 

自动模式在apb下要保持不断的发送,发送速度要大于接收速度。比较困难。

通常都用手动模式

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
/*
 * main.c
 *
 *  Created on: 2022年2月8日
 *      Author: lht
 */
/******************************************************************************
*
* Copyright (C) 2010 - 2014 Xilinx, Inc.  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/
/*****************************************************************************/
/**
*
* @file xqspips_flash_polled_example.c
*
*
* This file contains a design example using the QSPI driver (XQspiPs) in
* polled mode with a serial FLASH device. This examples performs
* some transfers in Auto mode and Manual start mode, to illustrate the modes
* available. It is recommended to use Manual CS + Auto start for
* best performance.
* The hardware which this example runs on. must have a serial FLASH (Numonyx
* N25Q, Winbond W25Q, or Spansion S25FL) for it to run. This example has been
* tested with the Numonyx Serial Flash (N25Q128).
*
* @note
*
* None.
*
* <pre>
* MODIFICATION HISTORY:
*
* Ver   Who Date     Changes
* ----- --- -------- -----------------------------------------------
* 1.00  sdm 11/25/10 First release
* 1.01  srt 06/12/12 Changed to meet frequency requirements of READ command
*                    for CR 663787
* 2.00a kka 22/08/12 Updated the example as XQspiPs_PolledTransfer API has
*            changed. Changed the prescalar to use divide by 8.
*            The user can change the prescalar to a maximum of
*            divide by 2 based on the reference clock in the
*            system.
*            Set the Holdb_dr bit in the configuration register using
*            XQSPIPS_HOLD_B_DRIVE_OPTION. Setting this bit
*            drives the HOLD bit of the QSPI controller.
*            This is required for QSPI to be used in Non QSPI boot
*            mode else there needs to be an external pullup on this
*            line.
*            See http://www.xilinx.com/support/answers/47596.htm.
* 2.01a sg  02/03/13 Created a function FlashReadID. Removed multiple
*            initialization using SetOptions.
*       ms  04/05/17 Modified Comment lines in functions to
*                    recognize it as documentation block for doxygen
*                    generation.
*</pre>
*
******************************************************************************/
 
/***************************** Include Files *********************************/
 
#include "xparameters.h"    /* SDK generated parameters */
#include "xqspips.h"        /* QSPI device driver 控制qspi控制器*/
#include "xil_printf.h"
 
/************************** Constant Definitions *****************************/
 
/*
 * The following constants map to the XPAR parameters created in the
 * xparameters.h file. They are defined here such that a user can easily
 * change all the needed parameters in one place.
 */
#define QSPI_DEVICE_ID      XPAR_XQSPIPS_0_DEVICE_ID
 
/*
 * The following constants define the commands which may be sent to the FLASH
 * device.
 */
#define WRITE_STATUS_CMD    0x01
#define WRITE_CMD       0x02
#define READ_CMD        0x03
#define WRITE_DISABLE_CMD   0x04
#define READ_STATUS_CMD     0x05
#define WRITE_ENABLE_CMD    0x06
#define FAST_READ_CMD       0x0B
#define DUAL_READ_CMD       0x3B
#define QUAD_READ_CMD       0x6B
#define BULK_ERASE_CMD      0xC7
#define SEC_ERASE_CMD       0xD8
#define READ_ID         0x9F
 
/*
 * The following constants define the offsets within a FlashBuffer data
 * type for each kind of data.  Note that the read data offset is not the
 * same as the write data because the QSPI driver is designed to allow full
 * duplex transfers such that the number of bytes received is the number
 * sent and received    定义了一些偏移量.
 */
#define COMMAND_OFFSET      0 /* FLASH instruction */
#define ADDRESS_1_OFFSET    1 /* MSB byte of address to read or write */
#define ADDRESS_2_OFFSET    2 /* Middle byte of address to read or write */
#define ADDRESS_3_OFFSET    3 /* LSB byte of address to read or write */
#define DATA_OFFSET     4 /* Start of Data for Read/Write  命令是一个字节,3个地址字节,所以数据是4*/
#define DUMMY_OFFSET        4 /* Dummy byte offset for fast, dual and quad 伪操作,dummy字节在fast/dual/quad read操作才会有
                     reads */
#define DUMMY_SIZE      1 /* Number of dummy bytes for fast, dual and
                     quad reads */
#define RD_ID_SIZE      4 /* Read ID command + 3 bytes ID response */
#define BULK_ERASE_SIZE     1 /* Bulk Erase command size */
#define SEC_ERASE_SIZE      4 /* Sector Erase command + Sector address */
 
/*
 * The following constants specify the extra bytes which are sent to the
 * FLASH on the QSPI interface, that are not data, but control information
 * which includes the command and address
 */
#define OVERHEAD_SIZE       4   //命令加地址总共4
 
/*
 * The following constants specify the page size, sector size, and number of
 * pages and sectors for the FLASH.  The page size specifies a max number of
 * bytes that can be written to the FLASH with a single transfer.
 */
#define SECTOR_SIZE     0x10000 //  扇区大小
#define NUM_SECTORS     0x100   //  多少个扇区
#define NUM_PAGES       0x10000 //  多少页
#define PAGE_SIZE       256     // 页的大小
 
/*
 * Number of flash pages to be written.
 */
#define PAGE_COUNT      16      //页的个数,相当于后面读写测试测试16页
 
/*
 * Flash address to which data is ot be written.
 */
#define TEST_ADDRESS        0x00055000  //定义测试的起始地址
#define UNIQUE_VALUE        0x05        //  相当于起始值,朝某个起始地址写入数据的时候这个值是多少
/*
 * The following constants specify the max amount of data and the size of the
 * the buffer required to hold the data and overhead to transfer the data to
 * and from the FLASH.
 */
#define MAX_DATA        PAGE_COUNT * PAGE_SIZE //读写测试量
 
/**************************** Type Definitions *******************************/
 
/***************** Macros (Inline Functions) Definitions *********************/
 
/************************** Function Prototypes ******************************/
 
void FlashErase(XQspiPs *QspiPtr, u32 Address, u32 ByteCount);
 
void FlashWrite(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command);
 
void FlashRead(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command);
 
int FlashReadID(void);
 
int QspiFlashPolledExample(XQspiPs *QspiInstancePtr, u16 QspiDeviceId);
 
/************************** Variable Definitions *****************************/
 
/*
 * The instances to support the device drivers are global such that they
 * are initialized to zero each time the program runs. They could be local
 * but should at least be static so they are zeroed.
 */
static XQspiPs QspiInstance;
 
/*
 * The following variable allows a test value to be added to the values that
 * are written to the FLASH such that unique values can be generated to
 * guarantee the writes to the FLASH were successful
 */
int Test = 5;   //向flash里写数据,每次写完累加的量
 
/*
 * The following variables are used to read and write to the flash and they
 * are global to avoid having large buffers on the stack
 */
u8 ReadBuffer[MAX_DATA + DATA_OFFSET + DUMMY_SIZE]; //读出数据定义的数组
u8 WriteBuffer[PAGE_SIZE + DATA_OFFSET];    //写入数据定义的数组
 
/*****************************************************************************/
/**
*
* Main function to call the QSPI Flash example.
*
* @param    None
*
* @return   XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note     None
*
******************************************************************************/
int main(void)
{
    int Status;
 
    xil_printf("QSPI FLASH Polled Example Test \r\n");
 
    /*
     * Run the Qspi Interrupt example.
     */
    Status = QspiFlashPolledExample(&QspiInstance, QSPI_DEVICE_ID);
    if (Status != XST_SUCCESS) {
        xil_printf("QSPI FLASH Polled Example Test Failed\r\n");
        return XST_FAILURE;
    }
 
    xil_printf("Successfully ran QSPI FLASH Polled Example Test\r\n");
    return XST_SUCCESS;
}
 
/*****************************************************************************/
/**
*
* The purpose of this function is to illustrate how to use the XQspiPs
* device driver in polled mode. This function writes and reads data
* from a serial FLASH.
*
* @param    None.
*
* @return   XST_SUCCESS if successful, else XST_FAILURE.
*
* @note     None.
*
*****************************************************************************/
int QspiFlashPolledExample(XQspiPs *QspiInstancePtr, u16 QspiDeviceId)
{
    int Status;
    u8 *BufferPtr;
    u8 UniqueValue;
    int Count;
    int Page;
    XQspiPs_Config *QspiConfig;
 
    /*
     * Initialize the QSPI driver so that it's ready to use
     */
    QspiConfig = XQspiPs_LookupConfig(QspiDeviceId);
    if (NULL == QspiConfig) {
        return XST_FAILURE;
    }
 
    Status = XQspiPs_CfgInitialize(QspiInstancePtr, QspiConfig,
                    QspiConfig->BaseAddress);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Perform a self-test to check hardware build
     */
    Status = XQspiPs_SelfTest(QspiInstancePtr);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    /*
     * Initialize the write buffer for a pattern to write to the FLASH
     * and the read buffer to zero so it can be verified after the read,
     * the test value that is added to the unique value allows the value
     * to be changed in a debug environment to guarantee    初始化和赋值
     * 一开始进来4+(5+5)【unique=5,test=5,data_offset=4】
     */
    for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < PAGE_SIZE;
         Count++, UniqueValue++) {
        WriteBuffer[DATA_OFFSET + Count] = (u8)(UniqueValue + Test);
    }
    memset(ReadBuffer, 0x00, sizeof(ReadBuffer));//清零readbuffer数据,方便跟writebuffer数据比较
 
    /*
     * Set Manual Start and Manual Chip select options and drive HOLD_B
     * pin high.    设置成手动开始,手动片选信号。hold B是保持引脚,为低电平时暂停对flash操作
     */
    XQspiPs_SetOptions(QspiInstancePtr, XQSPIPS_MANUAL_START_OPTION |
            XQSPIPS_FORCE_SSELECT_OPTION |
            XQSPIPS_HOLD_B_DRIVE_OPTION);
 
    /*
     * Set the prescaler for QSPI clock 分频flash时钟
     */
    XQspiPs_SetClkPrescaler(QspiInstancePtr, XQSPIPS_CLK_PRESCALE_8);
 
    /*
     * Assert the FLASH chip select.    片选信号有效
     */
    XQspiPs_SetSlaveSelect(QspiInstancePtr);
 
 
    FlashReadID();  //读flashid
 
    /*
     * Erase the flash.
     */
    FlashErase(QspiInstancePtr, TEST_ADDRESS, MAX_DATA);//写之前先擦除  此处只擦除测试范围
 
    /*
     * Write the data in the write buffer to the serial FLASH a page at a
     * time, starting from TEST_ADDRESS     每个页分别写入数据
     */
    for (Page = 0; Page < PAGE_COUNT; Page++) {
        FlashWrite(QspiInstancePtr, (Page * PAGE_SIZE) + TEST_ADDRESS,
               PAGE_SIZE, WRITE_CMD);
    }
 
    /*
     * Read the contents of the FLASH from TEST_ADDRESS, using Normal Read
     * command. Change the prescaler as the READ command operates at a
     * lower frequency.
     */
    FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, READ_CMD);//把数据从falsh里读出来,读出的数据存放在readbuffer
 
    /*
     * Setup a pointer to the start of the data that was read into the read
     * buffer and verify the data read is the data that was written
     */
    BufferPtr = &ReadBuffer[DATA_OFFSET];
 
    for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;
         Count++, UniqueValue++) {
        if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {
            return XST_FAILURE;
        }
    }   //读到的数据跟测试的数据做比较,不一致返回错误的标志
        //后面都是读的操作了
    /*
     * Read the contents of the FLASH from TEST_ADDRESS, using Fast Read
     * command
     */
    memset(ReadBuffer, 0x00, sizeof(ReadBuffer));
    FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, FAST_READ_CMD);
 
    /*
     * Setup a pointer to the start of the data that was read into the read
     * buffer and verify the data read is the data that was written
     */
    BufferPtr = &ReadBuffer[DATA_OFFSET + DUMMY_SIZE];
 
    for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;
         Count++, UniqueValue++) {
        if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {
            return XST_FAILURE;
        }
    }
 
    /*
     * Read the contents of the FLASH from TEST_ADDRESS, using Dual Read
     * command
     */
    memset(ReadBuffer, 0x00, sizeof(ReadBuffer));
    FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, DUAL_READ_CMD);
 
    /*
     * Setup a pointer to the start of the data that was read into the read
     * buffer and verify the data read is the data that was written
     */
    BufferPtr = &ReadBuffer[DATA_OFFSET + DUMMY_SIZE];
 
    for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;
         Count++, UniqueValue++) {
        if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {
            return XST_FAILURE;
        }
    }
 
    /*
     * Read the contents of the FLASH from TEST_ADDRESS, using Quad Read
     * command
     */
    memset(ReadBuffer, 0x00, sizeof(ReadBuffer));
    FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, QUAD_READ_CMD);
 
    /*
     * Setup a pointer to the start of the data that was read into the read
     * buffer and verify the data read is the data that was written
     */
    BufferPtr = &ReadBuffer[DATA_OFFSET + DUMMY_SIZE];
 
    for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;
         Count++, UniqueValue++) {
        if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {
            return XST_FAILURE;
        }
    }
 
    /*
     * Initialize the write buffer for a pattern to write to the FLASH
     * and the read buffer to zero so it can be verified after the read,
     * the test value that is added to the unique value allows the value
     * to be changed in a debug environment to guarantee
     */
    for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < PAGE_SIZE;
         Count++, UniqueValue++) {
        WriteBuffer[DATA_OFFSET + Count] = (u8)(UniqueValue + Test);
    }
    memset(ReadBuffer, 0x00, sizeof(ReadBuffer));
 
    /*
     * Set Auto Start and Manual Chip select options and drive HOLD_B
     * pin high.
     */
    XQspiPs_SetOptions(QspiInstancePtr, XQSPIPS_FORCE_SSELECT_OPTION |
            XQSPIPS_HOLD_B_DRIVE_OPTION);
 
    /*
     * Erase the flash.
     */
    FlashErase(QspiInstancePtr, TEST_ADDRESS, MAX_DATA);
 
    /*
     * Write the data in the write buffer to the serial FLASH a page at a
     * time, starting from TEST_ADDRESS
     */
    for (Page = 0; Page < PAGE_COUNT; Page++) {
        FlashWrite(QspiInstancePtr, (Page * PAGE_SIZE) + TEST_ADDRESS,
               PAGE_SIZE, WRITE_CMD);
    }
 
    /*
     * Read the contents of the FLASH from TEST_ADDRESS, using Normal Read
     * command. Change the prescaler as the READ command operates at a
     * lower frequency.
     */
    FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, READ_CMD);
 
    /*
     * Setup a pointer to the start of the data that was read into the read
     * buffer and verify the data read is the data that was written
     */
    BufferPtr = &ReadBuffer[DATA_OFFSET];
 
    for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;
         Count++, UniqueValue++) {
        if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {
            return XST_FAILURE;
        }
    }
 
    /*
     * Read the contents of the FLASH from TEST_ADDRESS, using Fast Read
     * command
     */
    memset(ReadBuffer, 0x00, sizeof(ReadBuffer));
    FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, FAST_READ_CMD);
 
    /*
     * Setup a pointer to the start of the data that was read into the read
     * buffer and verify the data read is the data that was written
     */
    BufferPtr = &ReadBuffer[DATA_OFFSET + DUMMY_SIZE];
 
    for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;
         Count++, UniqueValue++) {
        if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {
            return XST_FAILURE;
        }
    }
 
    /*
     * Read the contents of the FLASH from TEST_ADDRESS, using Dual Read
     * command
     */
    memset(ReadBuffer, 0x00, sizeof(ReadBuffer));
    FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, DUAL_READ_CMD);
 
    /*
     * Setup a pointer to the start of the data that was read into the read
     * buffer and verify the data read is the data that was written
     */
    BufferPtr = &ReadBuffer[DATA_OFFSET + DUMMY_SIZE];
 
    for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;
         Count++, UniqueValue++) {
        if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {
            return XST_FAILURE;
        }
    }
 
    /*
     * Read the contents of the FLASH from TEST_ADDRESS, using Quad Read
     * command
     */
    memset(ReadBuffer, 0x00, sizeof(ReadBuffer));
    FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, QUAD_READ_CMD);
 
    /*
     * Setup a pointer to the start of the data that was read into the read
     * buffer and verify the data read is the data that was written
     */
    BufferPtr = &ReadBuffer[DATA_OFFSET + DUMMY_SIZE];
 
    for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;
         Count++, UniqueValue++) {
        if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {
            return XST_FAILURE;
        }
    }
 
    return XST_SUCCESS;
}
 
/*****************************************************************************/
/**
*
* This function writes to the  serial FLASH connected to the QSPI interface.
* All the data put into the buffer must be in the same page of the device with
* page boundaries being on 256 byte boundaries.
*
* @param    QspiPtr is a pointer to the QSPI driver component to use.
* @param    Address contains the address to write data to in the FLASH.
* @param    ByteCount contains the number of bytes to write.
* @param    Command is the command used to write data to the flash. QSPI
*       device supports only Page Program command to write data to the
*       flash.
*
* @return   None.
*
* @note     None.
*
******************************************************************************/
void FlashWrite(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command)
{
    u8 WriteEnableCmd = { WRITE_ENABLE_CMD };
    u8 ReadStatusCmd[] = { READ_STATUS_CMD, 0 };  /* must send 2 bytes */
    u8 FlashStatus[2];
 
    /*
     * Send the write enable command to the FLASH so that it can be
     * written to, this needs to be sent as a seperate transfer before
     * the write
     */
    XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL,
                sizeof(WriteEnableCmd));
 
 
    /*
     * Setup the write command with the specified address and data for the
     * FLASH
     */
    WriteBuffer[COMMAND_OFFSET]   = Command;
    WriteBuffer[ADDRESS_1_OFFSET] = (u8)((Address & 0xFF0000) >> 16);
    WriteBuffer[ADDRESS_2_OFFSET] = (u8)((Address & 0xFF00) >> 8);
    WriteBuffer[ADDRESS_3_OFFSET] = (u8)(Address & 0xFF);
 
    /*
     * Send the write command, address, and data to the FLASH to be
     * written, no receive buffer is specified since there is nothing to
     * receive
     */
    XQspiPs_PolledTransfer(QspiPtr, WriteBuffer, NULL,
                ByteCount + OVERHEAD_SIZE);
 
    /*
     * Wait for the write command to the FLASH to be completed, it takes
     * some time for the data to be written
     */
    while (1) {
        /*
         * Poll the status register of the FLASH to determine when it
         * completes, by sending a read status command and receiving the
         * status byte
         */
        XQspiPs_PolledTransfer(QspiPtr, ReadStatusCmd, FlashStatus,
                    sizeof(ReadStatusCmd));
 
        /*
         * If the status indicates the write is done, then stop waiting,
         * if a value of 0xFF in the status byte is read from the
         * device and this loop never exits, the device slave select is
         * possibly incorrect such that the device status is not being
         * read
         */
        if ((FlashStatus[1] & 0x01) == 0) {
            break;
        }
    }
}
 
/*****************************************************************************/
/**
*
* This function reads from the  serial FLASH connected to the
* QSPI interface.
*
* @param    QspiPtr is a pointer to the QSPI driver component to use.
* @param    Address contains the address to read data from in the FLASH.
* @param    ByteCount contains the number of bytes to read.
* @param    Command is the command used to read data from the flash. QSPI
*       device supports one of the Read, Fast Read, Dual Read and Fast
*       Read commands to read data from the flash.
*
* @return   None.
*
* @note     None.
*
******************************************************************************/
void FlashRead(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command)
{
    /*
     * Setup the write command with the specified address and data for the
     * FLASH
     */
    WriteBuffer[COMMAND_OFFSET]   = Command;
    WriteBuffer[ADDRESS_1_OFFSET] = (u8)((Address & 0xFF0000) >> 16);
    WriteBuffer[ADDRESS_2_OFFSET] = (u8)((Address & 0xFF00) >> 8);
    WriteBuffer[ADDRESS_3_OFFSET] = (u8)(Address & 0xFF);
 
    if ((Command == FAST_READ_CMD) || (Command == DUAL_READ_CMD) ||
        (Command == QUAD_READ_CMD)) {
        ByteCount += DUMMY_SIZE;
    }
    /*
     * Send the read command to the FLASH to read the specified number
     * of bytes from the FLASH, send the read command and address and
     * receive the specified number of bytes of data in the data buffer
     */
    XQspiPs_PolledTransfer(QspiPtr, WriteBuffer, ReadBuffer,//flash读出的数据存在readbuffer
                ByteCount + OVERHEAD_SIZE);
}
 
/*****************************************************************************/
/**
*
* This function erases the sectors in the  serial FLASH connected to the
* QSPI interface.
*
* @param    QspiPtr is a pointer to the QSPI driver component to use.
* @param    Address contains the address of the first sector which needs to
*       be erased.
* @param    ByteCount contains the total size to be erased.
*
* @return   None.
*
* @note     None.
*
******************************************************************************/
void FlashErase(XQspiPs *QspiPtr, u32 Address, u32 ByteCount)
{
    u8 WriteEnableCmd = { WRITE_ENABLE_CMD };
    u8 ReadStatusCmd[] = { READ_STATUS_CMD, 0 };  /* must send 2 bytes */
    u8 FlashStatus[2];
    int Sector;
 
    /*
     * If erase size is same as the total size of the flash, use bulk erase
     * command
     */
    if (ByteCount == (NUM_SECTORS * SECTOR_SIZE)) {
        /*
         * Send the write enable command to the FLASH so that it can be
         * written to, this needs to be sent as a seperate transfer
         * before the erase
         */
        XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL,
                  sizeof(WriteEnableCmd));
 
        /*
         * Setup the bulk erase command
         */
        WriteBuffer[COMMAND_OFFSET]   = BULK_ERASE_CMD;
 
        /*
         * Send the bulk erase command; no receive buffer is specified
         * since there is nothing to receive
         */
        XQspiPs_PolledTransfer(QspiPtr, WriteBuffer, NULL,
                    BULK_ERASE_SIZE);
 
        /*
         * Wait for the erase command to the FLASH to be completed
         */
        while (1) {
            /*
             * Poll the status register of the device to determine
             * when it completes, by sending a read status command
             * and receiving the status byte
             */
            XQspiPs_PolledTransfer(QspiPtr, ReadStatusCmd,
                        FlashStatus,
                        sizeof(ReadStatusCmd));
 
            /*
             * If the status indicates the write is done, then stop
             * waiting; if a value of 0xFF in the status byte is
             * read from the device and this loop never exits, the
             * device slave select is possibly incorrect such that
             * the device status is not being read
             */
            if ((FlashStatus[1] & 0x01) == 0) {
                break;
            }
        }
 
        return;
    }
 
    /*
     * If the erase size is less than the total size of the flash, use
     * sector erase command
     */
    for (Sector = 0; Sector < ((ByteCount / SECTOR_SIZE) + 1); Sector++) {
        /*
         * Send the write enable command to the SEEPOM so that it can be
         * written to, this needs to be sent as a seperate transfer
         * before the write
         */
        XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL,
                    sizeof(WriteEnableCmd));
 
        /*
         * Setup the write command with the specified address and data
         * for the FLASH
         */
        WriteBuffer[COMMAND_OFFSET]   = SEC_ERASE_CMD;
        WriteBuffer[ADDRESS_1_OFFSET] = (u8)(Address >> 16);
        WriteBuffer[ADDRESS_2_OFFSET] = (u8)(Address >> 8);
        WriteBuffer[ADDRESS_3_OFFSET] = (u8)(Address & 0xFF);
 
        /*
         * Send the sector erase command and address; no receive buffer
         * is specified since there is nothing to receive
         */
        XQspiPs_PolledTransfer(QspiPtr, WriteBuffer, NULL,
                    SEC_ERASE_SIZE);
 
        /*
         * Wait for the sector erse command to the FLASH to be completed
         */
        while (1) {
            /*
             * Poll the status register of the device to determine
             * when it completes, by sending a read status command
             * and receiving the status byte
             */
            XQspiPs_PolledTransfer(QspiPtr, ReadStatusCmd,
                        FlashStatus,
                        sizeof(ReadStatusCmd));
 
            /*
             * If the status indicates the write is done, then stop
             * waiting, if a value of 0xFF in the status byte is
             * read from the device and this loop never exits, the
             * device slave select is possibly incorrect such that
             * the device status is not being read
             */
            if ((FlashStatus[1] & 0x01) == 0) {
                break;
            }
        }
 
        Address += SECTOR_SIZE;
    }
}
 
/*****************************************************************************/
/**
*
* This function reads serial FLASH ID connected to the SPI interface.
*
* @param    None.
*
* @return   XST_SUCCESS if read id, otherwise XST_FAILURE.
*
* @note     None.
*
******************************************************************************/
int FlashReadID(void)
{
    int Status;
 
    /*
     * Read ID in Auto mode.
     */
    WriteBuffer[COMMAND_OFFSET]   = READ_ID;
    WriteBuffer[ADDRESS_1_OFFSET] = 0x23;       /* 3 dummy bytes */
    WriteBuffer[ADDRESS_2_OFFSET] = 0x08;
    WriteBuffer[ADDRESS_3_OFFSET] = 0x09;
 
    Status = XQspiPs_PolledTransfer(&QspiInstance, WriteBuffer, ReadBuffer,
                RD_ID_SIZE);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
 
    xil_printf("FlashID=0x%x 0x%x 0x%x\n\r", ReadBuffer[1], ReadBuffer[2],
           ReadBuffer[3]);
 
    return XST_SUCCESS;
}

  

 

 

/* * main.c * *  Created on: 2022年2月8日 *      Author: lht *//******************************************************************************** Copyright (C) 2010 - 2014 Xilinx, Inc.  All rights reserved.** Permission is hereby granted, free of charge, to any person obtaining a copy* of this software and associated documentation files (the "Software"), to deal* in the Software without restriction, including without limitation the rights* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell* copies of the Software, and to permit persons to whom the Software is* furnished to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be included in* all copies or substantial portions of the Software.** Use of the Software is limited solely to applications:* (a) running on a Xilinx device, or* (b) that interact with a Xilinx device through a bus or interconnect.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL* XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE* SOFTWARE.** Except as contained in this notice, the name of the Xilinx shall not be used* in advertising or otherwise to promote the sale, use or other dealings in* this Software without prior written authorization from Xilinx.*******************************************************************************//*****************************************************************************//**** @file xqspips_flash_polled_example.c*** This file contains a design example using the QSPI driver (XQspiPs) in* polled mode with a serial FLASH device. This examples performs* some transfers in Auto mode and Manual start mode, to illustrate the modes* available. It is recommended to use Manual CS + Auto start for* best performance.* The hardware which this example runs on. must have a serial FLASH (Numonyx* N25Q, Winbond W25Q, or Spansion S25FL) for it to run. This example has been* tested with the Numonyx Serial Flash (N25Q128).** @note** None.** <pre>* MODIFICATION HISTORY:** Ver   Who Date     Changes* ----- --- -------- -----------------------------------------------* 1.00  sdm 11/25/10 First release* 1.01  srt 06/12/12 Changed to meet frequency requirements of READ command*                    for CR 663787* 2.00akka 22/08/12 Updated the example as XQspiPs_PolledTransfer API has*     changed. Changed the prescalar to use divide by 8.*     The user can change the prescalar to a maximum of*     divide by 2 based on the reference clock in the*     system.*      Set the Holdb_dr bit in the configuration register using*     XQSPIPS_HOLD_B_DRIVE_OPTION. Setting this bit*     drives the HOLD bit of the QSPI controller.*     This is required for QSPI to be used in Non QSPI boot*     mode else there needs to be an external pullup on this*     line.*     See http://www.xilinx.com/support/answers/47596.htm.* 2.01a sg  02/03/13 Created a function FlashReadID. Removed multiple*     initialization using SetOptions.*       ms  04/05/17 Modified Comment lines in functions to*                    recognize it as documentation block for doxygen*                    generation.*</pre>*******************************************************************************/
/***************************** Include Files *********************************/
#include "xparameters.h"/* SDK generated parameters */#include "xqspips.h"/* QSPI device driver 控制qspi控制器*/#include "xil_printf.h"
/************************** Constant Definitions *****************************/
/* * The following constants map to the XPAR parameters created in the * xparameters.h file. They are defined here such that a user can easily * change all the needed parameters in one place. */#define QSPI_DEVICE_IDXPAR_XQSPIPS_0_DEVICE_ID
/* * The following constants define the commands which may be sent to the FLASH * device. */#define WRITE_STATUS_CMD0x01#define WRITE_CMD0x02#define READ_CMD0x03#define WRITE_DISABLE_CMD0x04#define READ_STATUS_CMD0x05#define WRITE_ENABLE_CMD0x06#define FAST_READ_CMD0x0B#define DUAL_READ_CMD0x3B#define QUAD_READ_CMD0x6B#define BULK_ERASE_CMD0xC7#defineSEC_ERASE_CMD0xD8#define READ_ID0x9F
/* * The following constants define the offsets within a FlashBuffer data * type for each kind of data.  Note that the read data offset is not the * same as the write data because the QSPI driver is designed to allow full * duplex transfers such that the number of bytes received is the number * sent and received定义了一些偏移量. */#define COMMAND_OFFSET0 /* FLASH instruction */#define ADDRESS_1_OFFSET1 /* MSB byte of address to read or write */#define ADDRESS_2_OFFSET2 /* Middle byte of address to read or write */#define ADDRESS_3_OFFSET3 /* LSB byte of address to read or write */#define DATA_OFFSET4 /* Start of Data for Read/Write  命令是一个字节,3个地址字节,所以数据是4*/#define DUMMY_OFFSET4 /* Dummy byte offset for fast, dual and quad 伪操作,dummy字节在fast/dual/quad read操作才会有     reads */#define DUMMY_SIZE1 /* Number of dummy bytes for fast, dual and     quad reads */#define RD_ID_SIZE4 /* Read ID command + 3 bytes ID response */#define BULK_ERASE_SIZE1 /* Bulk Erase command size */#define SEC_ERASE_SIZE4 /* Sector Erase command + Sector address */
/* * The following constants specify the extra bytes which are sent to the * FLASH on the QSPI interface, that are not data, but control information * which includes the command and address */#define OVERHEAD_SIZE4//命令加地址总共4
/* * The following constants specify the page size, sector size, and number of * pages and sectors for the FLASH.  The page size specifies a max number of * bytes that can be written to the FLASH with a single transfer. */#define SECTOR_SIZE0x10000 //扇区大小#define NUM_SECTORS0x100//多少个扇区#define NUM_PAGES0x10000 //多少页#define PAGE_SIZE256// 页的大小
/* * Number of flash pages to be written. */#define PAGE_COUNT16//页的个数,相当于后面读写测试测试16页
/* * Flash address to which data is ot be written. */#define TEST_ADDRESS0x00055000//定义测试的起始地址#define UNIQUE_VALUE0x05//相当于起始值,朝某个起始地址写入数据的时候这个值是多少/* * The following constants specify the max amount of data and the size of the * the buffer required to hold the data and overhead to transfer the data to * and from the FLASH. */#define MAX_DATAPAGE_COUNT * PAGE_SIZE //读写测试量
/**************************** Type Definitions *******************************/
/***************** Macros (Inline Functions) Definitions *********************/
/************************** Function Prototypes ******************************/
void FlashErase(XQspiPs *QspiPtr, u32 Address, u32 ByteCount);
void FlashWrite(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command);
void FlashRead(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command);
int FlashReadID(void);
int QspiFlashPolledExample(XQspiPs *QspiInstancePtr, u16 QspiDeviceId);
/************************** Variable Definitions *****************************/
/* * The instances to support the device drivers are global such that they * are initialized to zero each time the program runs. They could be local * but should at least be static so they are zeroed. */static XQspiPs QspiInstance;
/* * The following variable allows a test value to be added to the values that * are written to the FLASH such that unique values can be generated to * guarantee the writes to the FLASH were successful */int Test = 5;//向flash里写数据,每次写完累加的量
/* * The following variables are used to read and write to the flash and they * are global to avoid having large buffers on the stack */u8 ReadBuffer[MAX_DATA + DATA_OFFSET + DUMMY_SIZE]; //读出数据定义的数组u8 WriteBuffer[PAGE_SIZE + DATA_OFFSET];//写入数据定义的数组
/*****************************************************************************//**** Main function to call the QSPI Flash example.** @paramNone** @returnXST_SUCCESS if successful, otherwise XST_FAILURE.** @noteNone*******************************************************************************/int main(void){int Status;
xil_printf("QSPI FLASH Polled Example Test \r\n");
/* * Run the Qspi Interrupt example. */Status = QspiFlashPolledExample(&QspiInstance, QSPI_DEVICE_ID);if (Status != XST_SUCCESS) {xil_printf("QSPI FLASH Polled Example Test Failed\r\n");return XST_FAILURE;}
xil_printf("Successfully ran QSPI FLASH Polled Example Test\r\n");return XST_SUCCESS;}
/*****************************************************************************//**** The purpose of this function is to illustrate how to use the XQspiPs* device driver in polled mode. This function writes and reads data* from a serial FLASH.** @paramNone.** @returnXST_SUCCESS if successful, else XST_FAILURE.** @noteNone.******************************************************************************/int QspiFlashPolledExample(XQspiPs *QspiInstancePtr, u16 QspiDeviceId){int Status;u8 *BufferPtr;u8 UniqueValue;int Count;int Page;XQspiPs_Config *QspiConfig;
/* * Initialize the QSPI driver so that it's ready to use */QspiConfig = XQspiPs_LookupConfig(QspiDeviceId);if (NULL == QspiConfig) {return XST_FAILURE;}
Status = XQspiPs_CfgInitialize(QspiInstancePtr, QspiConfig,QspiConfig->BaseAddress);if (Status != XST_SUCCESS) {return XST_FAILURE;}
/* * Perform a self-test to check hardware build */Status = XQspiPs_SelfTest(QspiInstancePtr);if (Status != XST_SUCCESS) {return XST_FAILURE;}
/* * Initialize the write buffer for a pattern to write to the FLASH * and the read buffer to zero so it can be verified after the read, * the test value that is added to the unique value allows the value * to be changed in a debug environment to guarantee 初始化和赋值 * 一开始进来4+(5+5)【unique=5,test=5,data_offset=4】 */for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < PAGE_SIZE;     Count++, UniqueValue++) {WriteBuffer[DATA_OFFSET + Count] = (u8)(UniqueValue + Test);}memset(ReadBuffer, 0x00, sizeof(ReadBuffer));//清零readbuffer数据,方便跟writebuffer数据比较
/* * Set Manual Start and Manual Chip select options and drive HOLD_B * pin high.设置成手动开始,手动片选信号。hold B是保持引脚,为低电平时暂停对flash操作 */XQspiPs_SetOptions(QspiInstancePtr, XQSPIPS_MANUAL_START_OPTION |XQSPIPS_FORCE_SSELECT_OPTION |XQSPIPS_HOLD_B_DRIVE_OPTION);
/* * Set the prescaler for QSPI clock 分频flash时钟 */XQspiPs_SetClkPrescaler(QspiInstancePtr, XQSPIPS_CLK_PRESCALE_8);
/* * Assert the FLASH chip select.片选信号有效 */XQspiPs_SetSlaveSelect(QspiInstancePtr);

FlashReadID();//读flashid
/* * Erase the flash. */FlashErase(QspiInstancePtr, TEST_ADDRESS, MAX_DATA);//写之前先擦除  此处只擦除测试范围
/* * Write the data in the write buffer to the serial FLASH a page at a * time, starting from TEST_ADDRESS每个页分别写入数据 */for (Page = 0; Page < PAGE_COUNT; Page++) {FlashWrite(QspiInstancePtr, (Page * PAGE_SIZE) + TEST_ADDRESS,   PAGE_SIZE, WRITE_CMD);}
/* * Read the contents of the FLASH from TEST_ADDRESS, using Normal Read * command. Change the prescaler as the READ command operates at a * lower frequency. */FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, READ_CMD);//把数据从falsh里读出来,读出的数据存放在readbuffer
/* * Setup a pointer to the start of the data that was read into the read * buffer and verify the data read is the data that was written */BufferPtr = &ReadBuffer[DATA_OFFSET];
for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;     Count++, UniqueValue++) {if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {return XST_FAILURE;}}//读到的数据跟测试的数据做比较,不一致返回错误的标志//后面都是读的操作了/* * Read the contents of the FLASH from TEST_ADDRESS, using Fast Read * command */memset(ReadBuffer, 0x00, sizeof(ReadBuffer));FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, FAST_READ_CMD);
/* * Setup a pointer to the start of the data that was read into the read * buffer and verify the data read is the data that was written */BufferPtr = &ReadBuffer[DATA_OFFSET + DUMMY_SIZE];
for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;     Count++, UniqueValue++) {if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {return XST_FAILURE;}}
/* * Read the contents of the FLASH from TEST_ADDRESS, using Dual Read * command */memset(ReadBuffer, 0x00, sizeof(ReadBuffer));FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, DUAL_READ_CMD);
/* * Setup a pointer to the start of the data that was read into the read * buffer and verify the data read is the data that was written */BufferPtr = &ReadBuffer[DATA_OFFSET + DUMMY_SIZE];
for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;     Count++, UniqueValue++) {if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {return XST_FAILURE;}}
/* * Read the contents of the FLASH from TEST_ADDRESS, using Quad Read * command */memset(ReadBuffer, 0x00, sizeof(ReadBuffer));FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, QUAD_READ_CMD);
/* * Setup a pointer to the start of the data that was read into the read * buffer and verify the data read is the data that was written */BufferPtr = &ReadBuffer[DATA_OFFSET + DUMMY_SIZE];
for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;     Count++, UniqueValue++) {if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {return XST_FAILURE;}}
/* * Initialize the write buffer for a pattern to write to the FLASH * and the read buffer to zero so it can be verified after the read, * the test value that is added to the unique value allows the value * to be changed in a debug environment to guarantee */for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < PAGE_SIZE;     Count++, UniqueValue++) {WriteBuffer[DATA_OFFSET + Count] = (u8)(UniqueValue + Test);}memset(ReadBuffer, 0x00, sizeof(ReadBuffer));
/* * Set Auto Start and Manual Chip select options and drive HOLD_B * pin high. */XQspiPs_SetOptions(QspiInstancePtr, XQSPIPS_FORCE_SSELECT_OPTION |XQSPIPS_HOLD_B_DRIVE_OPTION);
/* * Erase the flash. */FlashErase(QspiInstancePtr, TEST_ADDRESS, MAX_DATA);
/* * Write the data in the write buffer to the serial FLASH a page at a * time, starting from TEST_ADDRESS */for (Page = 0; Page < PAGE_COUNT; Page++) {FlashWrite(QspiInstancePtr, (Page * PAGE_SIZE) + TEST_ADDRESS,   PAGE_SIZE, WRITE_CMD);}
/* * Read the contents of the FLASH from TEST_ADDRESS, using Normal Read * command. Change the prescaler as the READ command operates at a * lower frequency. */FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, READ_CMD);
/* * Setup a pointer to the start of the data that was read into the read * buffer and verify the data read is the data that was written */BufferPtr = &ReadBuffer[DATA_OFFSET];
for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;     Count++, UniqueValue++) {if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {return XST_FAILURE;}}
/* * Read the contents of the FLASH from TEST_ADDRESS, using Fast Read * command */memset(ReadBuffer, 0x00, sizeof(ReadBuffer));FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, FAST_READ_CMD);
/* * Setup a pointer to the start of the data that was read into the read * buffer and verify the data read is the data that was written */BufferPtr = &ReadBuffer[DATA_OFFSET + DUMMY_SIZE];
for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;     Count++, UniqueValue++) {if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {return XST_FAILURE;}}
/* * Read the contents of the FLASH from TEST_ADDRESS, using Dual Read * command */memset(ReadBuffer, 0x00, sizeof(ReadBuffer));FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, DUAL_READ_CMD);
/* * Setup a pointer to the start of the data that was read into the read * buffer and verify the data read is the data that was written */BufferPtr = &ReadBuffer[DATA_OFFSET + DUMMY_SIZE];
for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;     Count++, UniqueValue++) {if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {return XST_FAILURE;}}
/* * Read the contents of the FLASH from TEST_ADDRESS, using Quad Read * command */memset(ReadBuffer, 0x00, sizeof(ReadBuffer));FlashRead(QspiInstancePtr, TEST_ADDRESS, MAX_DATA, QUAD_READ_CMD);
/* * Setup a pointer to the start of the data that was read into the read * buffer and verify the data read is the data that was written */BufferPtr = &ReadBuffer[DATA_OFFSET + DUMMY_SIZE];
for (UniqueValue = UNIQUE_VALUE, Count = 0; Count < MAX_DATA;     Count++, UniqueValue++) {if (BufferPtr[Count] != (u8)(UniqueValue + Test)) {return XST_FAILURE;}}
return XST_SUCCESS;}
/*****************************************************************************//**** This function writes to the  serial FLASH connected to the QSPI interface.* All the data put into the buffer must be in the same page of the device with* page boundaries being on 256 byte boundaries.** @paramQspiPtr is a pointer to the QSPI driver component to use.* @paramAddress contains the address to write data to in the FLASH.* @paramByteCount contains the number of bytes to write.* @paramCommand is the command used to write data to the flash. QSPI*device supports only Page Program command to write data to the*flash.** @returnNone.** @noteNone.*******************************************************************************/void FlashWrite(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command){u8 WriteEnableCmd = { WRITE_ENABLE_CMD };u8 ReadStatusCmd[] = { READ_STATUS_CMD, 0 };  /* must send 2 bytes */u8 FlashStatus[2];
/* * Send the write enable command to the FLASH so that it can be * written to, this needs to be sent as a seperate transfer before * the write */XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL,sizeof(WriteEnableCmd));

/* * Setup the write command with the specified address and data for the * FLASH */WriteBuffer[COMMAND_OFFSET]   = Command;WriteBuffer[ADDRESS_1_OFFSET] = (u8)((Address & 0xFF0000) >> 16);WriteBuffer[ADDRESS_2_OFFSET] = (u8)((Address & 0xFF00) >> 8);WriteBuffer[ADDRESS_3_OFFSET] = (u8)(Address & 0xFF);
/* * Send the write command, address, and data to the FLASH to be * written, no receive buffer is specified since there is nothing to * receive */XQspiPs_PolledTransfer(QspiPtr, WriteBuffer, NULL,ByteCount + OVERHEAD_SIZE);
/* * Wait for the write command to the FLASH to be completed, it takes * some time for the data to be written */while (1) {/* * Poll the status register of the FLASH to determine when it * completes, by sending a read status command and receiving the * status byte */XQspiPs_PolledTransfer(QspiPtr, ReadStatusCmd, FlashStatus,sizeof(ReadStatusCmd));
/* * If the status indicates the write is done, then stop waiting, * if a value of 0xFF in the status byte is read from the * device and this loop never exits, the device slave select is * possibly incorrect such that the device status is not being * read */if ((FlashStatus[1] & 0x01) == 0) {break;}}}
/*****************************************************************************//**** This function reads from the  serial FLASH connected to the* QSPI interface.** @paramQspiPtr is a pointer to the QSPI driver component to use.* @paramAddress contains the address to read data from in the FLASH.* @paramByteCount contains the number of bytes to read.* @paramCommand is the command used to read data from the flash. QSPI*device supports one of the Read, Fast Read, Dual Read and Fast*Read commands to read data from the flash.** @returnNone.** @noteNone.*******************************************************************************/void FlashRead(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command){/* * Setup the write command with the specified address and data for the * FLASH */WriteBuffer[COMMAND_OFFSET]   = Command;WriteBuffer[ADDRESS_1_OFFSET] = (u8)((Address & 0xFF0000) >> 16);WriteBuffer[ADDRESS_2_OFFSET] = (u8)((Address & 0xFF00) >> 8);WriteBuffer[ADDRESS_3_OFFSET] = (u8)(Address & 0xFF);
if ((Command == FAST_READ_CMD) || (Command == DUAL_READ_CMD) ||    (Command == QUAD_READ_CMD)) {ByteCount += DUMMY_SIZE;}/* * Send the read command to the FLASH to read the specified number * of bytes from the FLASH, send the read command and address and * receive the specified number of bytes of data in the data buffer */XQspiPs_PolledTransfer(QspiPtr, WriteBuffer, ReadBuffer,//flash读出的数据存在readbufferByteCount + OVERHEAD_SIZE);}
/*****************************************************************************//**** This function erases the sectors in the  serial FLASH connected to the* QSPI interface.** @paramQspiPtr is a pointer to the QSPI driver component to use.* @paramAddress contains the address of the first sector which needs to*be erased.* @paramByteCount contains the total size to be erased.** @returnNone.** @noteNone.*******************************************************************************/void FlashErase(XQspiPs *QspiPtr, u32 Address, u32 ByteCount){u8 WriteEnableCmd = { WRITE_ENABLE_CMD };u8 ReadStatusCmd[] = { READ_STATUS_CMD, 0 };  /* must send 2 bytes */u8 FlashStatus[2];int Sector;
/* * If erase size is same as the total size of the flash, use bulk erase * command */if (ByteCount == (NUM_SECTORS * SECTOR_SIZE)) {/* * Send the write enable command to the FLASH so that it can be * written to, this needs to be sent as a seperate transfer * before the erase */XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL,  sizeof(WriteEnableCmd));
/* * Setup the bulk erase command */WriteBuffer[COMMAND_OFFSET]   = BULK_ERASE_CMD;
/* * Send the bulk erase command; no receive buffer is specified * since there is nothing to receive */XQspiPs_PolledTransfer(QspiPtr, WriteBuffer, NULL,BULK_ERASE_SIZE);
/* * Wait for the erase command to the FLASH to be completed */while (1) {/* * Poll the status register of the device to determine * when it completes, by sending a read status command * and receiving the status byte */XQspiPs_PolledTransfer(QspiPtr, ReadStatusCmd,FlashStatus,sizeof(ReadStatusCmd));
/* * If the status indicates the write is done, then stop * waiting; if a value of 0xFF in the status byte is * read from the device and this loop never exits, the * device slave select is possibly incorrect such that * the device status is not being read */if ((FlashStatus[1] & 0x01) == 0) {break;}}
return;}
/* * If the erase size is less than the total size of the flash, use * sector erase command */for (Sector = 0; Sector < ((ByteCount / SECTOR_SIZE) + 1); Sector++) {/* * Send the write enable command to the SEEPOM so that it can be * written to, this needs to be sent as a seperate transfer * before the write */XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL,sizeof(WriteEnableCmd));
/* * Setup the write command with the specified address and data * for the FLASH */WriteBuffer[COMMAND_OFFSET]   = SEC_ERASE_CMD;WriteBuffer[ADDRESS_1_OFFSET] = (u8)(Address >> 16);WriteBuffer[ADDRESS_2_OFFSET] = (u8)(Address >> 8);WriteBuffer[ADDRESS_3_OFFSET] = (u8)(Address & 0xFF);
/* * Send the sector erase command and address; no receive buffer * is specified since there is nothing to receive */XQspiPs_PolledTransfer(QspiPtr, WriteBuffer, NULL,SEC_ERASE_SIZE);
/* * Wait for the sector erse command to the FLASH to be completed */while (1) {/* * Poll the status register of the device to determine * when it completes, by sending a read status command * and receiving the status byte */XQspiPs_PolledTransfer(QspiPtr, ReadStatusCmd,FlashStatus,sizeof(ReadStatusCmd));
/* * If the status indicates the write is done, then stop * waiting, if a value of 0xFF in the status byte is * read from the device and this loop never exits, the * device slave select is possibly incorrect such that * the device status is not being read */if ((FlashStatus[1] & 0x01) == 0) {break;}}
Address += SECTOR_SIZE;}}
/*****************************************************************************//**** This function reads serial FLASH ID connected to the SPI interface.** @paramNone.** @returnXST_SUCCESS if read id, otherwise XST_FAILURE.** @noteNone.*******************************************************************************/int FlashReadID(void){int Status;
/* * Read ID in Auto mode. */WriteBuffer[COMMAND_OFFSET]   = READ_ID;WriteBuffer[ADDRESS_1_OFFSET] = 0x23;/* 3 dummy bytes */WriteBuffer[ADDRESS_2_OFFSET] = 0x08;WriteBuffer[ADDRESS_3_OFFSET] = 0x09;
Status = XQspiPs_PolledTransfer(&QspiInstance, WriteBuffer, ReadBuffer,RD_ID_SIZE);if (Status != XST_SUCCESS) {return XST_FAILURE;}
xil_printf("FlashID=0x%x 0x%x 0x%x\n\r", ReadBuffer[1], ReadBuffer[2],   ReadBuffer[3]);
return XST_SUCCESS;}

posted @   涛大林  阅读(336)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示