spiFlash.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "spiFlash.h"
  2. #include <stdint.h>
  3. #include "nwy_spi.h"
  4. #define Xm25_JedecDeviceID 0x9f
  5. #define Xm25_SectorEarse 0x20
  6. #define Xm25_ReadData 0x03
  7. #define Xm25_WriteEnable 0x06
  8. #define Xm25_PageProgram 0x02
  9. #define Xm25_ReadStatusReg 0x05
  10. #define PAGE_RW_SIZE 256
  11. static int spiFlashFp=SPI_EC_ERROR;
  12. /*外部SPI FLASH初始化*/
  13. bool spiFlashInit(void){
  14. int fp=SPI_EC_ERROR;
  15. if(SPI_EC_ERROR!=spiFlashFp) return true;
  16. fp=nwy_spi_init(NAME_SPI_BUS_1, SPI_MODE_0, 8000000, 8);
  17. if(fp==SPI_EC_ERROR) return false;
  18. spiFlashFp=fp;
  19. return true;
  20. }
  21. /*释放SPI FLASH接口*/
  22. void spiFlashDeInit(void){
  23. if(spiFlashFp==SPI_EC_ERROR) return;
  24. nwy_spi_deinit(spiFlashFp);
  25. spiFlashFp=SPI_EC_ERROR;
  26. }
  27. /*读取SPI FLASH 设备ID*/
  28. unsigned int spiFlashGetID(void){
  29. if(spiFlashFp==SPI_EC_ERROR) return 0;
  30. return nwy_read_flash_id(spiFlashFp, Xm25_JedecDeviceID);
  31. }
  32. /*SPI FLASH sector擦出*/
  33. bool spiFlashSectorErace(unsigned int addr){
  34. if(spiFlashFp==SPI_EC_ERROR) return false;
  35. if(addr % SECTOR_SIZE_BYTE) return false;
  36. nwy_spi_flash_sector_erase(spiFlashFp,Xm25_WriteEnable,Xm25_ReadStatusReg,Xm25_SectorEarse,addr);
  37. return true;
  38. }
  39. /*SPI FLASH写数据*/
  40. bool spiFlashDataWrite(unsigned int addr, unsigned char *data_ptr, int size){
  41. int i,len;
  42. if(spiFlashFp==SPI_EC_ERROR) return false;
  43. i=size;
  44. while(i>0){
  45. if(i>PAGE_RW_SIZE) len=PAGE_RW_SIZE;
  46. else len=i;
  47. nwy_spi_flash_write(spiFlashFp, Xm25_WriteEnable,Xm25_ReadStatusReg,Xm25_PageProgram, data_ptr, addr, len);
  48. i -= len;
  49. }
  50. return true;
  51. }
  52. /*SPI FLASH读数据*/
  53. bool spiFlashDataRead(unsigned int addr, unsigned char *rec_ptr, int size){
  54. int i,len,j;
  55. if(spiFlashFp==SPI_EC_ERROR) return false;
  56. i=size;
  57. j=0;
  58. while(i>0){
  59. if(i>PAGE_RW_SIZE) len=PAGE_RW_SIZE;
  60. else len=i;
  61. nwy_spi_flash_read(spiFlashFp,Xm25_ReadData, rec_ptr+j, addr, len);
  62. i -= len;
  63. j += len;
  64. }
  65. return true;
  66. }
  67. #ifdef TEST_SPIFLASH_ON
  68. /*SPI FLASH SECTOR 测试*/
  69. bool spiFlashSectorTest(unsigned short sector, SPIFLASH_INFO *spiFlashInfo){
  70. unsigned int addr;
  71. unsigned char Winfo[PAGE_RW_SIZE];
  72. unsigned char Rinfo[PAGE_RW_SIZE];
  73. int i,len;
  74. static unsigned char w=0;
  75. if(false==spiFlashInit()){
  76. spiFlashInfo->id=0;
  77. spiFlashInfo->errno=1;
  78. return false;
  79. }
  80. spiFlashInfo->id=spiFlashGetID();
  81. addr=sector*SECTOR_SIZE_BYTE;
  82. if(false==spiFlashSectorErace(addr)){
  83. spiFlashInfo->errno=2;
  84. return false;
  85. }
  86. for(i=0;i<PAGE_RW_SIZE;i++){
  87. Winfo[i]=w+i;
  88. w++;
  89. }
  90. i=SECTOR_SIZE_BYTE;
  91. while(i>0){
  92. if(i>PAGE_RW_SIZE) len=PAGE_RW_SIZE;
  93. else len=i;
  94. if(false==spiFlashDataWrite(addr, Winfo, len)){
  95. spiFlashInfo->errno=3;
  96. spiFlashDeInit();
  97. return false;
  98. }
  99. addr += len;
  100. i -= len;
  101. }
  102. addr=sector*SECTOR_SIZE_BYTE;
  103. i=SECTOR_SIZE_BYTE;
  104. while(i>0){
  105. if(i>PAGE_RW_SIZE) len=PAGE_RW_SIZE;
  106. else len=i;
  107. memset(Rinfo, 0, PAGE_RW_SIZE);
  108. if(false==spiFlashDataRead(addr, Rinfo, len)){
  109. spiFlashInfo->errno=4;
  110. spiFlashDeInit();
  111. return false;
  112. }
  113. addr += len;
  114. i -= len;
  115. if(0!=memcmp(Winfo, Rinfo, len)){
  116. spiFlashInfo->errno=5;
  117. spiFlashDeInit();
  118. return false;
  119. }
  120. }
  121. return true;
  122. }
  123. #endif