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 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
| #include <stdio.h>
#include "xil_printf.h" #include <xiic.h> #include "LP8758.h" #include "xparameters.h" #include "axi_power_manager.h" #include "xspi.h" #include "xil_exception.h" #include "xintc.h"
#define AXI_POWER_MANAGEMENT_BASE_ADDR XPAR_AXI_POWER_MANAGER_0_S00_AXI_BASEADDR #define I2C_LP8758_ADDRESS 0x60 #define SPI_DEVICE_ID XPAR_SPI_0_DEVICE_ID #define INTC_DEVICE_ID XPAR_INTC_0_DEVICE_ID #define SPI_INTR_ID XPAR_INTC_0_SPI_0_VEC_ID #define FIRMWARE_UPDATE_INTR_ID XPAR_MICROBLAZE_0_AXI_INTC_FIRMWARE_TO_DRAM_0_FLASH_UPDATE_INTR_INTR #define INTC_BASEADDR XPAR_INTC_0_BASEADDR
#define SPI_SELECT 0x01 #define COMMAND_WRITE_STATUS_REGISTER 0x01 #define COMMAND_STATUSREG_READ 0x05 #define COMMAND_WRITE_ENABLE 0x06 #define COMMAND_ENTER_QUAD_MODE 0x35 #define COMMAND_EXIT_QUAD_MODE 0xF5 #define COMMAND_ENTER_4BYTE_ADDRESS_MODE 0xB7 #define COMMAND_EXIT_4BYTE_ADDRESS_MODE 0xE9 #define COMMAND_READ_FLAG_STATUS 0x70 #define COMMAND_CLEAR_FLAG_STATUS 0x50 #define COMMAND_WRITE_DISABLE 0x04 #define COMMAND_READ_EXTENDED_ADDRESS 0xC8 #define COMMAND_WRITE_EXTENDED_ADDRESS 0xC5
#define COMMAND_PAGE_PROGRAM 0x02 #define COMMAND_QUAD_WRITE 0x32 #define COMMAND_4BYTE_PAGE_PROGRAM 0x12 #define COMMAND_EXTENDED_QUAD_INPUT_FAST_PROGRAM 0x32
#define COMMAND_READ_ID 0x9F #define COMMAND_READ_DISCOVERY 0x5A #define COMMAND_RANDOM_READ 0x03 #define COMMAND_DUAL_READ 0x3B #define COMMAND_DUAL_IO_READ 0xBB #define COMMAND_QUAD_READ 0x6B #define COMMAND_QUAD_IO_READ 0xEB #define COMMAND_4BYTE_READ 0x13 #define COMMAND_4BYTE_FAST_READ 0x0C #define COMMAND_4BYTE_DUAL_OUTPUT_FAST_READ 0x3C #define COMMAND_4BYTE_DUAL_INPUTOUTPUT_FAST_READ 0XBC #define COMMAND_4BYTE_QUAD_OUTPUT_FAST_READ 0x6C #define COMMAND_4BYTE_QUAD_INPUTOUTPUT_FASTREAD 0xEC
#define COMMAND_BLOCK64_ERASE 0xD8 #define COMMAND_CHIP_ERASE 0xC7 #define COMMAND_SE 0x20 #define COMMAND_4BYTE_SUBSECTOR_ERASE 0x21
#define READ_WRITE_EXTRA_BYTES 4 #define WRITE_ENABLE_BYTES 1 #define SECTOR_ERASE_BYTES 4 #define BULK_ERASE_BYTES 1 #define STATUS_READ_BYTES 2 #define STATUS_WRITE_BYTES 2 #define FLASH_SR_IS_READY_MASK 0x01
#define PAGE_SIZE 256 #define NUMB_SECTORS 512 #define BYTE_PER_BLOCK 65536 #define NUMB_SUBSECTORS 8192 #define BYTE_PER_SUBSECTOR 4096 #define NOB_PAGES 131072
#define FLASH_UPDATE_ADDRESS 0x00800000
#define BYTE1 0 #define BYTE2 1 #define BYTE3 2 #define BYTE4 3 #define BYTE5 4 #define BYTE6 5 #define BYTE7 6 #define BYTE8 7 #define DUAL_READ_DUMMY_BYTES 2 #define QUAD_READ_DUMMY_BYTES 4 #define DUAL_IO_READ_DUMMY_BYTES 2 #define QUAD_IO_READ_DUMMY_BYTES 5 #define DDR_ADDR0 0x8C000000 #define DDR_ADDR1 0x86000000
#define INTC static XIntc #define INTC_HANDLER XIntc_InterruptHandler static XSpi Spi; INTC InterruptController;
static int COMMAND_ON_THE_FLY_PAGE_PROGRAM=0x02; volatile static int TransferInProgress; static int ErrorCount; static int qspi_init_flag=0; static int update_flash_flag =0;
static u8 ReadBuffer[PAGE_SIZE + READ_WRITE_EXTRA_BYTES + 5]; static u8 WriteBuffer[PAGE_SIZE + READ_WRITE_EXTRA_BYTES + 5];
void init_platform(void); void cleanup_platform(void); int SpiFlashWriteEnable(XSpi *SpiPtr); int SpiFlashWrite(XSpi *SpiPtr, u32 Addr, u32 ByteCount, u8 WriteCmd); int SpiFlashWrite_File(XSpi *SpiPtr, u32 Addr, u32 ByteCount, u8 WriteCmd, u32 ddrvector); int SpiFlashRead(XSpi *SpiPtr, u32 Addr, u32 ByteCount, u8 ReadCmd); int SpiFlashBulkErase(XSpi *SpiPtr); int SpiFlashSectorErase(XSpi *SpiPtr, u32 Addr); int SpiFlashGetStatus(XSpi *SpiPtr); int SpiFlashQuadEnable(XSpi *SpiPtr); int SpiFlashEnableHPM(XSpi *SpiPtr); static int SpiFlashWaitForFlashReady(void); void SpiHandler(void *CallBackRef, u32 StatusEvent, unsigned int ByteCount); static int SetupInterruptSystem(XSpi *SpiPtr); void firmware_update_handler(void * CallBackRef); int qspi_flash_geo(void); int qspi_read_flash(u32 StartAddr, u32 NoByteToRead); int System_init_startup (void); int qspi_flash_erase (void); int pgm_flash_file (void); int qspi_ease_entire_flash (void); int qspi_erase_sector_flash (u32 OfsetAddr, u32 SectCount); int qspi_flash_erase_main (u32 OfsetAddr, u32 SectCount ); int SpiFlashReadID(XSpi *SpiPtr); int SpiFlashReadIDINT(XSpi *SpiPtr); int Spi_Blank_Check(u32 StartAddr, u32 NoByteToRead); int qspi_program_flash (u32 StartAddr, u32 NoOfPage); int flash_erase_for_file (u32 StartAddr, u32 NoOfPage); int qspi_readback_flash(u32 StartAddr, u32 NoByteToRead); unsigned int convertToDecimal(char const* hexstring); static int DownloadSerialDataToQSPIFlash(u32 StartAddr, u32 NoByteToRead); static int TeraTermFile_Receive ( u32 StartAddr,u32 NoByteToRead); int read_rs232 (char* buf, int nbytes); void TimerCounterHandler(void *CallBackRef, u8 TmrCtrNumber); void TimerCntOutHandler(void *CallBackRef, u8 TmrCtrNumber); void TmrCtrDisableIntr(XIntc* IntcInstancePtr, u16 IntrId); void TmrCtr_FastHandler(void) __attribute__ ((fast_interrupt)); int SpiFlash4byteexit(XSpi *SpiPtr);
int System_init_startup (void) { int Status; XSpi_Config *ConfigPtr; ConfigPtr = XSpi_LookupConfig(SPI_DEVICE_ID); if (ConfigPtr == NULL) { return XST_DEVICE_NOT_FOUND; }
Status = XSpi_CfgInitialize(&Spi, ConfigPtr, ConfigPtr->BaseAddress); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = SetupInterruptSystem(&Spi); if(Status != XST_SUCCESS) { return XST_FAILURE; } XSpi_SetStatusHandler(&Spi, &Spi, (XSpi_StatusHandler)SpiHandler); Status = XSpi_SetOptions(&Spi, XSP_MASTER_OPTION | XSP_MANUAL_SSELECT_OPTION); if(Status != XST_SUCCESS) { return XST_FAILURE; } Status = XSpi_SetSlaveSelect(&Spi, SPI_SELECT); if(Status != XST_SUCCESS) { return XST_FAILURE; } XSpi_Start(&Spi); Status = SpiFlashWriteEnable(&Spi); if(Status != XST_SUCCESS) { return XST_FAILURE; } return XST_SUCCESS; }
void SpiHandler(void *CallBackRef, u32 StatusEvent, unsigned int ByteCount) { TransferInProgress = FALSE; if (StatusEvent != XST_SPI_TRANSFER_DONE) { ErrorCount++; } }
int SpiFlashWaitForFlashReady(void) { int Status; u8 StatusReg;
while(1) {
Status = SpiFlashGetStatus(&Spi); if(Status != XST_SUCCESS) { return XST_FAILURE; } StatusReg = ReadBuffer[1]; if((StatusReg & FLASH_SR_IS_READY_MASK) == 0) { break; } else xil_printf("%c%c%c%c%c%c",95,8,92,8,47,8);
} return XST_SUCCESS; }
int SpiFlashGetStatus(XSpi *SpiPtr) { int Status;
WriteBuffer[BYTE1] = COMMAND_STATUSREG_READ;
TransferInProgress = TRUE; Status = XSpi_Transfer(SpiPtr, WriteBuffer, ReadBuffer, STATUS_READ_BYTES); if(Status != XST_SUCCESS) { return XST_FAILURE; }
while(TransferInProgress); if(ErrorCount != 0) { ErrorCount = 0; return XST_FAILURE; }
return XST_SUCCESS; }
int SpiFlashWriteEnable(XSpi *SpiPtr) { int Status; Status = SpiFlashWaitForFlashReady(); if(Status != XST_SUCCESS) { return XST_FAILURE; } WriteBuffer[BYTE1] = COMMAND_WRITE_ENABLE; TransferInProgress = TRUE; Status = XSpi_Transfer(SpiPtr, WriteBuffer, NULL, WRITE_ENABLE_BYTES); if(Status != XST_SUCCESS) { return XST_FAILURE; } while(TransferInProgress); if(ErrorCount != 0) { ErrorCount = 0; return XST_FAILURE; } return XST_SUCCESS; }
void firmware_update_handler(void * CallBackRef) { update_flash_flag = 1; print("Update Flash interrupt occur\r\n"); }
static int SetupInterruptSystem(XSpi *SpiPtr) {
int Status;
Status = XIntc_Initialize(&InterruptController, INTC_DEVICE_ID); if(Status != XST_SUCCESS) { return XST_FAILURE; } Status = XIntc_Connect(&InterruptController, SPI_INTR_ID, (XInterruptHandler)XSpi_InterruptHandler, (void *)SpiPtr); if(Status != XST_SUCCESS) { return XST_FAILURE; } Status = XIntc_Connect(&InterruptController, FIRMWARE_UPDATE_INTR_ID, (XInterruptHandler)firmware_update_handler, (void *)SpiPtr); if(Status != XST_SUCCESS) { return XST_FAILURE; } Status = XIntc_Start(&InterruptController, XIN_REAL_MODE); if(Status != XST_SUCCESS) { return XST_FAILURE; } XIntc_Enable(&InterruptController, SPI_INTR_ID); XIntc_Enable(&InterruptController, FIRMWARE_UPDATE_INTR_ID);
Xil_ExceptionInit();
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler)INTC_HANDLER, &InterruptController); Xil_ExceptionEnable();
return XST_SUCCESS; }
int qspi_flash_geo(void) { int Status;
Status = SpiFlashReadID(&Spi); if(Status != XST_SUCCESS) { return XST_FAILURE; } return XST_SUCCESS; }
int SpiFlashReadID(XSpi *SpiPtr) { int Status; Status = SpiFlashWaitForFlashReady(); if(Status != XST_SUCCESS) { return XST_FAILURE; }
WriteBuffer[BYTE1] = COMMAND_READ_ID; TransferInProgress = TRUE; Status = XSpi_Transfer(SpiPtr, WriteBuffer, ReadBuffer, READ_WRITE_EXTRA_BYTES + 20); if(Status != XST_SUCCESS) { return XST_FAILURE; } while(TransferInProgress); if(ErrorCount != 0) { ErrorCount = 0; return XST_FAILURE; } if ( (ReadBuffer[1] == 0x20)) { xil_printf("\n\rManufacturer ID:\t0x%x\t:= MICRON\n\r", ReadBuffer[1]); if ( (ReadBuffer[2] == 0xBA)){ xil_printf("Memory Type:\t\t0x%x\t:= 3V0\n\r", ReadBuffer[2]); } else{ if ((ReadBuffer[2] == 0xBB)) { xil_printf("Memory Type:\t\t0x%x\t:= 1V8\n\r", ReadBuffer[2]); } else xil_printf("Memory Type:\t\t0x%x\t:= QSPI Data\n\r", ReadBuffer[2]); } if ((ReadBuffer[3] == 0x17)) { xil_printf("Memory Capacity:\t0x%x\t:=\t64Mbit\n\r", ReadBuffer[3]); } else if ((ReadBuffer[3] == 0x18)) { xil_printf("Memory Capacity:\t0x%x\t:=\t128Mbit\n\r", ReadBuffer[3]); } else if ( (ReadBuffer[3] == 0x19)) { xil_printf("Memory Capacity:\t0x%x\t:= 256Mbit\n\r", ReadBuffer[3]); } else if ((ReadBuffer[3] == 0x20)) { xil_printf("Memory Capacity:\t0x%x\t:= 512Mbit\n\r", ReadBuffer[3]); } else if ((ReadBuffer[3] == 0x21)) { xil_printf("Memory Capacity:\t0x%x\t:= 1Gbit\n\r", ReadBuffer[3]); } else if ((ReadBuffer[3] == 0x22)) { xil_printf("Memory Capacity:\t0x%x\t:= 2Gbit\n\r", ReadBuffer[3]); } if ((ReadBuffer[5]== 0x00)&&(ReadBuffer[6]==0x00)) { xil_printf("Memory Part:\t\t0x%02x\t:= N25Q\n\r", ReadBuffer[5]); COMMAND_ON_THE_FLY_PAGE_PROGRAM = COMMAND_PAGE_PROGRAM; } else { xil_printf("Memory Part:\t\t0x%02x\t:= MT25QU\n\r", ReadBuffer[5]); COMMAND_ON_THE_FLY_PAGE_PROGRAM = COMMAND_4BYTE_PAGE_PROGRAM; }
}
else if ((ReadBuffer[1] == 0x01)) { xil_printf("\n\rManufacturer ID: \tSPANSION\n\r"); if ((ReadBuffer[3] == 0x18)) { xil_printf("Memory Capacity\t=\t256Mbit\n\r"); } else if ((ReadBuffer[3] == 0x19)) { xil_printf("Memory Capacity\t=\t512Mbit\n\r"); } else if ((ReadBuffer[3] == 0x20)) { xil_printf("Memory Capacity\t=\t1024Mbit\n\r");
} } else if ((ReadBuffer[1] == 0xEF)) { xil_printf("\n\rManufacturer ID\t=\tWINBOND\n\r"); if ((ReadBuffer[3] == 0x18)) { xil_printf("Memory Capacity\t=\t128Mbit\n\r"); } } else if((ReadBuffer[1] == 0xC2)){ xil_printf("\n\rManufacturer ID\t=\tMacronix\n\r"); if ((ReadBuffer[3] == 0x19)) { xil_printf("Memory Capacity\t=\t256Mbit\n\r"); } } return XST_SUCCESS; }
static int DownloadSerialDataToQSPIFlash(u32 StartAddr, u32 FileByteCount) { int quq_int, remaind_int, NoOfSector, NoOfPage;
print("\r\nTotalByteRecived =\t"); putnum(FileByteCount); print ("\r\nFlashAddress Offset = \t"); putnum(StartAddr);
NoOfSector = (FileByteCount/BYTE_PER_BLOCK); NoOfPage = (FileByteCount/PAGE_SIZE);
quq_int = (FileByteCount / BYTE_PER_BLOCK); remaind_int = (FileByteCount - (quq_int * BYTE_PER_BLOCK));
if (remaind_int != 0) { NoOfSector = (NoOfSector +1); } quq_int = (FileByteCount / PAGE_SIZE); remaind_int = (FileByteCount - ( quq_int * PAGE_SIZE));
if (remaind_int != 0) { NoOfPage = (NoOfPage+1); }
print ("\r\nNoOfSector= "); putnum(NoOfSector); print ("\r\nNoOfPage= "); putnum(NoOfPage); print ("\r\nProgramming QSPI flash Start"); qspi_program_flash(StartAddr,NoOfPage); print ("\r\nProgramming QSPI flash end"); return XST_SUCCESS; }
int qspi_program_flash(u32 StartAddr,u32 NoOfPage) { u32 ddrvector=0; int Status;
Status = XSpi_SetSlaveSelect(&Spi, SPI_SELECT); if(Status != XST_SUCCESS) { return XST_FAILURE; }
XSpi_Start(&Spi); Status = SpiFlashWriteEnable(&Spi); if(Status != XST_SUCCESS) { return XST_FAILURE; }
while (NoOfPage !=0) { Status = SpiFlashWriteEnable(&Spi); if(Status != XST_SUCCESS) { return XST_FAILURE; } xil_printf("write file page left:%d\r\n", NoOfPage); Status = SpiFlashWrite_File(&Spi, StartAddr, PAGE_SIZE, COMMAND_ON_THE_FLY_PAGE_PROGRAM, ddrvector); if(Status != XST_SUCCESS) { return XST_FAILURE; } else { NoOfPage--; StartAddr = (StartAddr + PAGE_SIZE); ddrvector = (ddrvector + PAGE_SIZE); } } return XST_SUCCESS; }
int SpiFlash4bytemodeEnable(XSpi *SpiPtr) { int Status;
Status = SpiFlashWriteEnable(&Spi); if(Status != XST_SUCCESS) { return XST_FAILURE; }
Status = SpiFlashWaitForFlashReady(); if(Status != XST_SUCCESS) { return XST_FAILURE; }
WriteBuffer[BYTE1] = COMMAND_ENTER_4BYTE_ADDRESS_MODE;
TransferInProgress = TRUE; Status = XSpi_Transfer(SpiPtr, WriteBuffer, NULL, WRITE_ENABLE_BYTES); if(Status != XST_SUCCESS) { return XST_FAILURE; }
while(TransferInProgress); if(ErrorCount != 0) { ErrorCount = 0; return XST_FAILURE; }
return XST_SUCCESS; }
int SpiFlashWrite_File(XSpi *SpiPtr, u32 Addr, u32 ByteCount, u8 WriteCmd, u32 ddrvector) { u32 Index; int Status; unsigned char *DDR_MEMB1 = (unsigned char *)DDR_ADDR0; Status = SpiFlash4bytemodeEnable(&Spi); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = SpiFlashWriteEnable(&Spi); if(Status != XST_SUCCESS) { return XST_FAILURE; }
Status = SpiFlashWaitForFlashReady(); if(Status != XST_SUCCESS) { return XST_FAILURE; } WriteBuffer[BYTE1] = WriteCmd; WriteBuffer[BYTE2] = (u8) (Addr >> 24); WriteBuffer[BYTE3] = (u8) (Addr >> 16); WriteBuffer[BYTE4] = (u8) (Addr >> 8); WriteBuffer[BYTE5] = (u8) Addr;
for(Index = 5; Index < (ByteCount + READ_WRITE_EXTRA_BYTES +1); Index++, ddrvector++) { WriteBuffer[Index] = DDR_MEMB1[ddrvector]; }
TransferInProgress = TRUE; Status = XSpi_Transfer(SpiPtr, WriteBuffer, NULL, (ByteCount + READ_WRITE_EXTRA_BYTES +1)); if(Status != XST_SUCCESS) { return XST_FAILURE; } while(TransferInProgress); if(ErrorCount != 0) { ErrorCount = 0; return XST_FAILURE; } Status = SpiFlash4byteexit(&Spi); if (Status != XST_SUCCESS) { return XST_FAILURE; } return XST_SUCCESS; }
int SpiFlashSectorErase(XSpi *SpiPtr, u32 Addr) { u32 Index; int Status; Status = SpiFlash4bytemodeEnable(&Spi); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = SpiFlashWriteEnable(&Spi); if(Status != XST_SUCCESS) { return XST_FAILURE; }
Status = SpiFlashWaitForFlashReady(); if(Status != XST_SUCCESS) { return XST_FAILURE; } WriteBuffer[BYTE1] = COMMAND_SE; WriteBuffer[BYTE2] = (u8) (Addr >> 24); WriteBuffer[BYTE3] = (u8) (Addr >> 16); WriteBuffer[BYTE4] = (u8) (Addr >> 8); WriteBuffer[BYTE5] = (u8) Addr; TransferInProgress = TRUE; Status = XSpi_Transfer(SpiPtr, WriteBuffer, NULL, (SECTOR_ERASE_BYTES +1)); if(Status != XST_SUCCESS) { return XST_FAILURE; } while(TransferInProgress); if(ErrorCount != 0) { ErrorCount = 0; return XST_FAILURE; } Status = SpiFlash4byteexit(&Spi); if (Status != XST_SUCCESS) { return XST_FAILURE; } return XST_SUCCESS; }
int SpiFlashBlockErase(XSpi *SpiPtr, u32 Addr) { u32 Index; int Status; Status = SpiFlash4bytemodeEnable(&Spi); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = SpiFlashWriteEnable(&Spi); if(Status != XST_SUCCESS) { return XST_FAILURE; }
Status = SpiFlashWaitForFlashReady(); if(Status != XST_SUCCESS) { return XST_FAILURE; } WriteBuffer[BYTE1] = COMMAND_BLOCK64_ERASE; WriteBuffer[BYTE2] = (u8) (Addr >> 24); WriteBuffer[BYTE3] = (u8) (Addr >> 16); WriteBuffer[BYTE4] = (u8) (Addr >> 8); WriteBuffer[BYTE5] = (u8) Addr; TransferInProgress = TRUE; Status = XSpi_Transfer(SpiPtr, WriteBuffer, NULL, (SECTOR_ERASE_BYTES +1)); if(Status != XST_SUCCESS) { return XST_FAILURE; } while(TransferInProgress); if(ErrorCount != 0) { ErrorCount = 0; return XST_FAILURE; } Status = SpiFlash4byteexit(&Spi); if (Status != XST_SUCCESS) { return XST_FAILURE; } return XST_SUCCESS; }
int flash_erase_for_file(u32 StartAddr, u32 FileByteCount){
int quq_int, remaind_int, NoOfSector, NoOfPage; xil_printf("\r\nTotalByteRecived =\t%d",FileByteCount); xil_printf("\r\nFlashAddress Offset = \t"); putnum(StartAddr);
NoOfSector = (FileByteCount/BYTE_PER_BLOCK); NoOfPage = (FileByteCount/PAGE_SIZE);
quq_int = (FileByteCount / BYTE_PER_BLOCK); remaind_int = (FileByteCount - (quq_int * BYTE_PER_BLOCK));
if (remaind_int != 0) { NoOfSector = (NoOfSector +1); } quq_int = (FileByteCount / PAGE_SIZE); remaind_int = (FileByteCount - ( quq_int * PAGE_SIZE));
if (remaind_int != 0) { NoOfPage = (NoOfPage+1); }
xil_printf ("\r\nNoOfSector=%d",NoOfSector); xil_printf ("\r\nNoOfPage=%d", NoOfPage); xil_printf ("\r\nEraseing QSPI flash Start");
int Status; while (NoOfSector !=0) { Status = SpiFlashBlockErase(&Spi, StartAddr); xil_printf("Blcok left: %d\r\n", NoOfSector);
if(Status != XST_SUCCESS) { return XST_FAILURE; } else { NoOfSector--; StartAddr = (StartAddr + BYTE_PER_BLOCK); } } return XST_SUCCESS; }
int SpiFlash4byteexit(XSpi *SpiPtr) { int Status;
Status = SpiFlashWriteEnable(&Spi); if(Status != XST_SUCCESS) { return XST_FAILURE; }
Status = SpiFlashWaitForFlashReady(); if(Status != XST_SUCCESS) { return XST_FAILURE; }
WriteBuffer[BYTE1] = COMMAND_EXIT_4BYTE_ADDRESS_MODE;
TransferInProgress = TRUE; Status = XSpi_Transfer(SpiPtr, WriteBuffer, NULL, WRITE_ENABLE_BYTES); if(Status != XST_SUCCESS) { return XST_FAILURE; }
while(TransferInProgress); if(ErrorCount != 0) { ErrorCount = 0; return XST_FAILURE; } return XST_SUCCESS; }
int qspi_read_flash(u32 StartAddr, u32 NoByteToRead) { int Status, CntLine =0; u32 Index; int remaind_int, NoOfPage=0, NoOfPage_strt=0, NoOfPage_tot=0; if (qspi_init_flag ==0) { Status = System_init_startup (); if (Status != XST_SUCCESS) { return XST_FAILURE; } else qspi_init_flag=1;
} print ("\n\r\n\rPerforming Flash Read Operation...\r\n"); print ("\n\rFlash Start Address:\t0x");putnum(StartAddr); print ("\n\rFlash End Address:\t0x");putnum((NoByteToRead +StartAddr));
NoOfPage = (NoByteToRead/PAGE_SIZE); remaind_int = (NoByteToRead - ( NoOfPage * PAGE_SIZE)); if (remaind_int != 0) { NoOfPage = (NoOfPage+1); } if ( StartAddr !=0x00000) { NoOfPage_strt = (StartAddr/PAGE_SIZE); remaind_int = (StartAddr -(NoOfPage_strt * PAGE_SIZE)); if (remaind_int != 0) { NoOfPage_strt = (NoOfPage_strt+1); } NoOfPage_tot = NoOfPage_strt + NoOfPage; } if ( (StartAddr==0x000000) || (NoOfPage_tot<=NOB_PAGES)) { xil_printf("\n\rOffset(h):\t0x00\t0x01\t0x02\t0x03\t0x04\t0x05\t0x06\t0x07\n\r"); CntLine =0; Index =0; xil_printf("\n\r0x%08x:\t", (StartAddr));
while (NoOfPage !=0 ) { Status = SpiFlashWriteEnable(&Spi); if(Status != XST_SUCCESS) { } for(Index = 0; Index < (PAGE_SIZE + READ_WRITE_EXTRA_BYTES+1); Index++) { ReadBuffer[Index] = 0x0; } Status = SpiFlashRead(&Spi, StartAddr, PAGE_SIZE, COMMAND_RANDOM_READ); if(Status != XST_SUCCESS) { return XST_FAILURE; } for(Index = 5; Index <= ((PAGE_SIZE + READ_WRITE_EXTRA_BYTES)); Index++) { xil_printf("0x%02x\t", (ReadBuffer[Index ])); if (CntLine > 6) { CntLine =0; xil_printf("\n\r0x%08x:\t", (StartAddr + (Index-4)) ); } else CntLine++; } NoOfPage--; StartAddr = (StartAddr + PAGE_SIZE); } } else { print ("\n\rNumber of Pages Excced the device settings!"); }
return XST_SUCCESS; }
int SpiFlashRead(XSpi *SpiPtr, u32 Addr, u32 ByteCount, u8 ReadCmd) { int Status; Status = SpiFlash4bytemodeEnable(&Spi); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = SpiFlashWriteEnable(&Spi); if(Status != XST_SUCCESS) { return XST_FAILURE; }
Status = SpiFlashWaitForFlashReady(); if(Status != XST_SUCCESS) { return XST_FAILURE; } WriteBuffer[BYTE1] = ReadCmd; WriteBuffer[BYTE2] = (u8) (Addr >> 24); WriteBuffer[BYTE3] = (u8) (Addr >> 16); WriteBuffer[BYTE4] = (u8) (Addr >> 8); WriteBuffer[BYTE5] = (u8) Addr;
if (ReadCmd == COMMAND_DUAL_READ) { ByteCount += DUAL_READ_DUMMY_BYTES; } else if (ReadCmd == COMMAND_DUAL_IO_READ) { ByteCount += DUAL_READ_DUMMY_BYTES; } else if (ReadCmd == COMMAND_QUAD_IO_READ) { ByteCount += QUAD_IO_READ_DUMMY_BYTES; } else if (ReadCmd==COMMAND_QUAD_READ) { ByteCount += QUAD_READ_DUMMY_BYTES; }else if (ReadCmd==COMMAND_4BYTE_QUAD_OUTPUT_FAST_READ) { ByteCount += QUAD_IO_READ_DUMMY_BYTES; }else if (ReadCmd==COMMAND_RANDOM_READ) { ByteCount += 1; }
TransferInProgress = TRUE; Status = XSpi_Transfer( SpiPtr, WriteBuffer, ReadBuffer, (ByteCount + READ_WRITE_EXTRA_BYTES)); if(Status != XST_SUCCESS) { return XST_FAILURE; } while(TransferInProgress); if(ErrorCount != 0) { ErrorCount = 0; return XST_FAILURE; } Status = SpiFlash4byteexit(&Spi); if (Status != XST_SUCCESS) { return XST_FAILURE; } return XST_SUCCESS; }
int main() { int Status; int i=0;
for(i=0; i<10; i++){ usleep(1000000); print(".....\n\r"); } AXI_POWER_MANAGER_mWriteReg(AXI_POWER_MANAGEMENT_BASE_ADDR, 0, 0);
print("start config lp8758 \n\r"); LP8758_WR_REG(XPAR_IIC_0_BASEADDR, 0x02, 0x88); LP8758_WR_REG(XPAR_IIC_0_BASEADDR, 0x03, 0xD2); LP8758_WR_REG(XPAR_IIC_0_BASEADDR, 0x04, 0x88); LP8758_WR_REG(XPAR_IIC_0_BASEADDR, 0x05, 0xD2); LP8758_WR_REG(XPAR_IIC_0_BASEADDR, 0x06, 0x88); LP8758_WR_REG(XPAR_IIC_0_BASEADDR, 0x07, 0xD2); LP8758_WR_REG(XPAR_IIC_0_BASEADDR, 0x08, 0x88); LP8758_WR_REG(XPAR_IIC_0_BASEADDR, 0x09, 0xD2);
LP8758_WR_REG(XPAR_IIC_0_BASEADDR, 0x0A, 0xA2); LP8758_WR_REG(XPAR_IIC_0_BASEADDR, 0x0C, 0xD4); LP8758_WR_REG(XPAR_IIC_0_BASEADDR, 0x0E, 0x75); LP8758_WR_REG(XPAR_IIC_0_BASEADDR, 0x10, 0xB1); print("Configure LP8758 done \n\r"); for(i=0; i<2; i++){ usleep(1000000); print(".....\n\r"); } AXI_POWER_MANAGER_mWriteReg(AXI_POWER_MANAGEMENT_BASE_ADDR, 0, 1);
print("\n\r\t Read Quad SPI flash\t\r\n"); Status = System_init_startup (); if (Status != XST_SUCCESS) { return XST_FAILURE; } else{ qspi_init_flag=1; }
while(1){ if(update_flash_flag == 0x01){ update_flash_flag=0; u32 bytes_count = AXI_POWER_MANAGER_mReadReg(AXI_POWER_MANAGEMENT_BASE_ADDR, AXI_POWER_MANAGER_S00_AXI_SLV_REG1_OFFSET); xil_printf("firmware contain %d bytes\r\n", bytes_count); print("\n\r\t Start Erase a page\t\r\n"); Status = flash_erase_for_file(FLASH_UPDATE_ADDRESS, bytes_count); if (Status != XST_SUCCESS) { return XST_FAILURE; } unsigned char *DDR_MEMB1 = (unsigned char *)DDR_ADDR0; for(int i =0; i<64; i++){ xil_printf("DDR_MEMB1[%d]=%x\r\n",i,DDR_MEMB1[i]); }
print("\n\r\t write file to flash \t\r\n"); Status = DownloadSerialDataToQSPIFlash(FLASH_UPDATE_ADDRESS, bytes_count); if (Status != XST_SUCCESS) { return XST_FAILURE; } } print("hello microphase, microphase, microphase, updated image\r\n"); print("####, updated image\r\n"); print("xxxx####, updated image\r\n"); usleep(1000000); }
return 0; }
|