|
| 1 | +/* |
| 2 | + Winbond SPIFlash read and write |
| 3 | + |
| 4 | + This example read/write a W25QXX Flash by SPI port. |
| 5 | + Pins: |
| 6 | + CS: PA2 (003 is PC4) |
| 7 | + MOSI: PA7 (003 is PC6) |
| 8 | + MISO: PA6 (003 is PC7) |
| 9 | + SCK: PA5 (003 is PC5) |
| 10 | + created 30 Jurn 2023 |
| 11 | + by TempersLee |
| 12 | + */ |
| 13 | + |
| 14 | +#include <SPI.h> |
| 15 | + |
| 16 | +// Winbond SPIFLASH ID |
| 17 | +#define W25Q80 0xEF13 |
| 18 | +#define W25Q16 0xEF14 |
| 19 | +#define W25Q32 0xEF15 |
| 20 | +#define W25Q64 0xEF16 |
| 21 | +#define W25Q128 0xEF17 |
| 22 | + |
| 23 | +/* Winbond SPIFalsh Instruction List */ |
| 24 | +#define W25X_WriteEnable 0x06 |
| 25 | +#define W25X_WriteDisable 0x04 |
| 26 | +#define W25X_ReadStatusReg 0x05 |
| 27 | +#define W25X_WriteStatusReg 0x01 |
| 28 | +#define W25X_ReadData 0x03 |
| 29 | +#define W25X_FastReadData 0x0B |
| 30 | +#define W25X_FastReadDual 0x3B |
| 31 | +#define W25X_PageProgram 0x02 |
| 32 | +#define W25X_BlockErase 0xD8 |
| 33 | +#define W25X_SectorErase 0x20 |
| 34 | +#define W25X_ChipErase 0xC7 |
| 35 | +#define W25X_PowerDown 0xB9 |
| 36 | +#define W25X_ReleasePowerDown 0xAB |
| 37 | +#define W25X_DeviceID 0xAB |
| 38 | +#define W25X_ManufactDeviceID 0x90 |
| 39 | +#define W25X_JedecDeviceID 0x9F |
| 40 | + |
| 41 | + |
| 42 | +//define CS pin |
| 43 | +#define chipSelectPin A2 |
| 44 | + |
| 45 | + |
| 46 | +uint16_t flashType; |
| 47 | +uint8_t readBuffer[256]; |
| 48 | +uint8_t writeBuffer[256]; |
| 49 | + |
| 50 | +void setup() { |
| 51 | + uint16_t i=0; |
| 52 | + Serial.begin(115200); |
| 53 | + // start the SPI library: |
| 54 | + SPI.begin(); |
| 55 | + Serial.printf("%s Chip ID: 0x%08x\r\n", "Hello CH32duino!", DBGMCU_GetDEVID()); |
| 56 | + |
| 57 | + // initialize the chip select pins: |
| 58 | + pinMode(chipSelectPin, OUTPUT); |
| 59 | + digitalWrite(chipSelectPin, HIGH); |
| 60 | + delay(100); |
| 61 | + |
| 62 | + flashType = readFlashID(); |
| 63 | + switch (flashType) |
| 64 | + { |
| 65 | + case W25Q16: |
| 66 | + Serial.printf("W25Q16 is OK!\r\n"); |
| 67 | + break; |
| 68 | + case W25Q32: |
| 69 | + Serial.printf("W25Q32 is OK!\r\n"); |
| 70 | + break; |
| 71 | + case W25Q64: |
| 72 | + Serial.printf("W25Q64 is OK!\r\n"); |
| 73 | + break; |
| 74 | + case W25Q80: |
| 75 | + Serial.printf("W25Q80 is OK!\r\n"); |
| 76 | + break; |
| 77 | + case W25Q128: |
| 78 | + Serial.printf("W25Q128 is OK!\r\n"); |
| 79 | + break; |
| 80 | + default: |
| 81 | + Serial.printf("Flash is not valid!\r\n"); |
| 82 | + break; |
| 83 | + } |
| 84 | + |
| 85 | + //initial the writeBuffer |
| 86 | + for(i=0; i<256; i++) |
| 87 | + { |
| 88 | + writeBuffer[i] = i; |
| 89 | + } |
| 90 | + |
| 91 | + //erase sector 0 |
| 92 | + Serial.printf("Erase Flash:\r\n"); |
| 93 | + eraseSector(0); |
| 94 | + readFlashToBuff(readBuffer, 0, 256); |
| 95 | + for(i=0; i<256; i++) |
| 96 | + { |
| 97 | + if(i%16==0 && i!=0) Serial.printf("\r\n"); |
| 98 | + Serial.printf("%02x ",readBuffer[i]); |
| 99 | + } |
| 100 | + Serial.printf("\r\n"); |
| 101 | + |
| 102 | + Serial.printf("Write Flash:\r\n"); |
| 103 | + writeFlashPage(writeBuffer, 0, 256); |
| 104 | + readFlashToBuff(readBuffer, 0, 256); |
| 105 | + for(i=0; i<256; i++) |
| 106 | + { |
| 107 | + if(i%16==0 && i!=0) Serial.printf("\r\n"); |
| 108 | + Serial.printf("%02x ",readBuffer[i]); |
| 109 | + } |
| 110 | + Serial.printf("\r\n"); |
| 111 | + |
| 112 | +} |
| 113 | + |
| 114 | +void loop() { |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +//Read Flash ID |
| 124 | +uint16_t readFlashID(void) |
| 125 | +{ |
| 126 | + uint16_t id; |
| 127 | + digitalWrite(chipSelectPin, LOW); |
| 128 | + SPI.transfer(W25X_ManufactDeviceID); |
| 129 | + SPI.transfer(0x00); |
| 130 | + SPI.transfer(0x00); |
| 131 | + SPI.transfer(0x00); |
| 132 | + id |= SPI.transfer(0xFF) << 8; |
| 133 | + id |= SPI.transfer(0xFF); |
| 134 | + digitalWrite(chipSelectPin, HIGH); |
| 135 | + return id; |
| 136 | +} |
| 137 | + |
| 138 | +//Read Flash SR |
| 139 | +//bit7 6 5 4 3 2 1 0 |
| 140 | +//SPR RV TB BP2 BP1 BP0 WEL BSY |
| 141 | +uint8_t readFlashSR(void) |
| 142 | +{ |
| 143 | + uint8_t status; |
| 144 | + digitalWrite(chipSelectPin, LOW); |
| 145 | + SPI.transfer(W25X_ReadStatusReg); |
| 146 | + status = SPI.transfer(0xff); |
| 147 | + digitalWrite(chipSelectPin, HIGH); |
| 148 | + return status; |
| 149 | +} |
| 150 | + |
| 151 | +//Write Flash SR |
| 152 | +void writeFlashSR(uint8_t status) |
| 153 | +{ |
| 154 | + digitalWrite(chipSelectPin, LOW); |
| 155 | + SPI.transfer(W25X_WriteStatusReg); |
| 156 | + SPI.transfer(status); |
| 157 | + digitalWrite(chipSelectPin, HIGH); |
| 158 | +} |
| 159 | + |
| 160 | +//Write Enable |
| 161 | +void writeEnable(void) |
| 162 | +{ |
| 163 | + digitalWrite(chipSelectPin, LOW); |
| 164 | + SPI.transfer( W25X_WriteEnable ); |
| 165 | + digitalWrite(chipSelectPin, HIGH); |
| 166 | +} |
| 167 | + |
| 168 | +//Write Disable |
| 169 | +void writeDisable(void) |
| 170 | +{ |
| 171 | + digitalWrite(chipSelectPin, LOW); |
| 172 | + SPI.transfer( W25X_WriteDisable ); |
| 173 | + digitalWrite(chipSelectPin, HIGH); |
| 174 | +} |
| 175 | + |
| 176 | +//Erase sector,,4K bytes per sector |
| 177 | +void eraseSector(uint32_t sectorNum) |
| 178 | +{ |
| 179 | + sectorNum *= 4096; |
| 180 | + writeEnable(); |
| 181 | + while( ( readFlashSR() & 0x01 ) == 0x01 ); //waite for busy |
| 182 | + |
| 183 | + digitalWrite(chipSelectPin, LOW); |
| 184 | + SPI.transfer(W25X_SectorErase); |
| 185 | + SPI.transfer((uint8_t)(sectorNum >> 16)); |
| 186 | + SPI.transfer((uint8_t)(sectorNum >> 8)); |
| 187 | + SPI.transfer((uint8_t)(sectorNum)); |
| 188 | + digitalWrite(chipSelectPin, HIGH); |
| 189 | + |
| 190 | + while( ( readFlashSR() & 0x01 ) == 0x01 ); //waite for busy |
| 191 | +} |
| 192 | + |
| 193 | + |
| 194 | +//read flash data to pBuf |
| 195 | +void readFlashToBuff(uint8_t *pBuf, uint32_t readAddr, uint16_t size) |
| 196 | +{ |
| 197 | + uint16_t i; |
| 198 | + digitalWrite(chipSelectPin, LOW); |
| 199 | + SPI.transfer(W25X_ReadData); |
| 200 | + SPI.transfer((uint8_t)(readAddr >> 16)); |
| 201 | + SPI.transfer((uint8_t)(readAddr >> 8)); |
| 202 | + SPI.transfer((uint8_t)(readAddr)); |
| 203 | + for(i=0; i<size; i++) |
| 204 | + { |
| 205 | + pBuf[i] = SPI.transfer(0xff); |
| 206 | + } |
| 207 | + digitalWrite(chipSelectPin, HIGH); |
| 208 | +} |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | +//write data to flash |
| 213 | +void writeFlashPage(uint8_t *pBuf, uint32_t writeAddr, uint16_t size) |
| 214 | +{ |
| 215 | + uint16_t i; |
| 216 | + writeEnable(); |
| 217 | + while( ( readFlashSR() & 0x01 ) == 0x01 ); //waite for busy |
| 218 | + digitalWrite(chipSelectPin, LOW); |
| 219 | + SPI.transfer(W25X_PageProgram); |
| 220 | + SPI.transfer((uint8_t)(writeAddr >> 16)); |
| 221 | + SPI.transfer((uint8_t)(writeAddr >> 8)); |
| 222 | + SPI.transfer((uint8_t)(writeAddr)); |
| 223 | + for(i=0; i<size; i++) |
| 224 | + { |
| 225 | + SPI.transfer(pBuf[i]); |
| 226 | + } |
| 227 | + digitalWrite(chipSelectPin, HIGH); |
| 228 | + while( ( readFlashSR() & 0x01 ) == 0x01 ); //waite for busy |
| 229 | +} |
0 commit comments