From 953396ea2857ad0224c25b725e85a04283a38576 Mon Sep 17 00:00:00 2001 From: solawc <1225093964@qq.com> Date: Tue, 30 May 2023 13:51:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9ID=E6=9E=9A=E4=B8=BE=EF=BC=8C?= =?UTF-8?q?=20=E6=9B=B4=E6=94=B9=E6=9C=AC=E7=89=88=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/w25qxxFlash.drawio | 106 +++++++++++ src/eflash.c | 420 +++++++++++++++++++++++++++++++++++++++++ src/eflash.h | 121 ++++++++++++ src/eflash_port.c | 49 +++++ src/eflash_port.h | 23 +++ src/hal_w25qxx.c | 354 ---------------------------------- src/hal_w25qxx.h | 89 --------- 7 files changed, 719 insertions(+), 443 deletions(-) create mode 100644 doc/w25qxxFlash.drawio create mode 100644 src/eflash.c create mode 100644 src/eflash.h create mode 100644 src/eflash_port.c create mode 100644 src/eflash_port.h delete mode 100644 src/hal_w25qxx.c delete mode 100644 src/hal_w25qxx.h diff --git a/doc/w25qxxFlash.drawio b/doc/w25qxxFlash.drawio new file mode 100644 index 0000000..9cd63ff --- /dev/null +++ b/doc/w25qxxFlash.drawio @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/eflash.c b/src/eflash.c new file mode 100644 index 0000000..145da65 --- /dev/null +++ b/src/eflash.c @@ -0,0 +1,420 @@ +/* + eflash.c + + Copyright (c) 2021-2023 sola + + This part of the code belongs to the corresponding platform that + I adapt to, has nothing to do with GRBL, and is only related to + the platform. Therefore, if you use this part of the code, + please indicate the source +*/ + +/**************************************************************** + * Flash for fatfs + * - flashInit() + * - flashReadSector() + * - flashWriteSector() + * - FLASH_SECTOR_SIZE = 4096 + ****************************************************************/ + +#include "eflash.h" + +// #ifdef HAS_W25Qxx + +eFLASH_t sFlash; + +// bool flash_dma_mode = false; + +static void w25qxxCsBegin(eFLASH_t *nFlash) +{ + nFlash->flashEnableTrans(); +} + +static void w25qxxCsEnd(eFLASH_t *nFlash) +{ + nFlash->flashDisableTrans(); +} + +void flashSpiInit(eFLASH_t *nFlash) +{ + if(sFlash.info.flash_mode == sFLAHS_SPI_MODE) { + nFlash->flashSpiGpioInit(); + nFlash->flashSpiInit(); + }else { + + } +} + +uint16_t w25qxxReadWriteByte(eFLASH_t *nFlash, uint16_t wdata) +{ + return nFlash->flashSpiReadWriteByte(wdata); +} + +/*--------------------------------------------------------------------------------------------*/ + +void w25qxxDelayUs(volatile uint32_t us) { + + while(us --); +} + +void w25qxxInit(eFLASH_t *nFlash) +{ + uint32_t get_id_size = 0x00; + + flashSpiInit(nFlash); + + w25qxxEnterFlashMode(nFlash); + + sFlash.info.flash_id = w25qxxRead_ID(nFlash); + + get_id_size = sFlash.info.flash_id & 0x00ffff; + sFlash.info.flash_man = sFlash.info.flash_id & 0xff0000; + + sFlash.info.addr_size = 16; /* Default address size is 16bit. */ + + switch(get_id_size) { + case sFLASH_ID_X16: sFlash.info.flash_size = (16 / 8) *1024; break; + case sFLASH_ID_16: sFlash.info.flash_size = (16 / 8) *1024; break; + case sFLASH_ID_64: sFlash.info.flash_size = (64 / 8) *1024; break; + case sFLASH_ID_128: sFlash.info.flash_size = (128 / 8) *1024; + sFlash.info.addr_size = 24; + break; + case sFLASH_ID_256: sFlash.info.flash_size = (258 / 8) *1024; + sFlash.info.addr_size = 24; + break; + default: sFlash.info.flash_size = 0; break; + } + + if(sFlash.info.flash_size != 0) { sFlash.info.flash_state = 1; } + else { sFlash.info.flash_state = 0; } +} + +uint32_t w25qxxRead_ID(eFLASH_t *nFlash) +{ + uint32_t id = 0; + w25qxxCsBegin(nFlash); + w25qxxReadWriteByte(nFlash, W25X_JedecDeviceID); + id |= w25qxxReadWriteByte(nFlash, 0xff) << 16; + id |= w25qxxReadWriteByte(nFlash, 0xff) << 8; + id |= w25qxxReadWriteByte(nFlash, 0xff) << 0; + w25qxxCsEnd(nFlash); + return id; +} + +uint8_t w25qxx_read_sr_reg(eFLASH_t *nFlash, uint8_t reg) +{ + uint8_t byte = 0, command = 0; + switch (reg) + { + case 1: + command = W25X_ReadStatusReg; + break; + case 2: + command = W25X_ReadStatusReg2; + break; + case 3: + command = W25X_ReadStatusReg3; + break; + default: + command = W25X_ReadStatusReg; + break; + } + w25qxxCsBegin(nFlash); + w25qxxReadWriteByte(nFlash, command); + byte = w25qxxReadWriteByte(nFlash, 0Xff); + w25qxxCsEnd(nFlash); + return byte; +} + +void w25qxx_write_enable(eFLASH_t *nFlash) +{ + w25qxxCsBegin(nFlash); + w25qxxReadWriteByte(nFlash, W25X_WriteEnable); + w25qxxCsEnd(nFlash); +} + +void w25qxx_write_disable(eFLASH_t *nFlash) +{ + w25qxxCsBegin(nFlash); + w25qxxReadWriteByte(nFlash, W25X_WriteDisable); + w25qxxCsEnd(nFlash); +} + +void w25qxxWaitBusy(eFLASH_t *nFlash) +{ + uint8_t FLASH_Status = 0; + uint32_t SPITimeout = 0xfffff; + w25qxxCsBegin(nFlash); + w25qxxReadWriteByte(nFlash, W25X_ReadStatusReg); + do + { + FLASH_Status = w25qxxReadWriteByte(nFlash, Dummy_Byte); + if((SPITimeout--) == 0) { return; } + w25qxxDelayUs(sFlash.info.flash_delay_time); + } while ((FLASH_Status & WIP_Flag) == 0x01); + w25qxxCsEnd(nFlash); +} + +void w25qxx_enter_power_down(eFLASH_t *nFlash) { + w25qxxCsBegin(nFlash); + w25qxxReadWriteByte(nFlash, W25X_PowerDown); + w25qxxCsEnd(nFlash); +} + +void w25qxx_wakeup(eFLASH_t *nFlash) { + w25qxxCsBegin(nFlash); + w25qxxReadWriteByte(nFlash, W25X_ReleasePowerDown); + w25qxxCsEnd(nFlash); +} + +void w25qxxEnterFlashMode(eFLASH_t *nFlash) +{ + uint8_t Temp; + w25qxxCsBegin(nFlash); + w25qxxReadWriteByte(nFlash, W25X_ReadStatusRegister3); + Temp = w25qxxReadWriteByte(nFlash, Dummy_Byte); + w25qxxCsEnd(nFlash); + + if((Temp & 0x01) == 0) + { + w25qxxCsBegin(nFlash); + w25qxxReadWriteByte(nFlash, W25X_Enter4ByteMode); + w25qxxCsEnd(nFlash); + } +} + +void w25qxxSectorErase(eFLASH_t *nFlash, uint32_t SectorAddr) { + + w25qxx_write_enable(nFlash); + + w25qxxCsBegin(nFlash); + + w25qxxReadWriteByte(nFlash, W25X_SectorErase); + + if(sFlash.info.addr_size == 24) { + w25qxxReadWriteByte(nFlash, (SectorAddr & 0xFF000000) >> 24); + } + + w25qxxReadWriteByte(nFlash, (SectorAddr & 0xFF0000) >> 16); + + w25qxxReadWriteByte(nFlash, (SectorAddr & 0xFF00) >> 8); + + w25qxxReadWriteByte(nFlash, SectorAddr & 0xFF); + + w25qxxCsEnd(nFlash); + + w25qxxWaitBusy(nFlash); +} + +void w25qxxBlockErase(eFLASH_t *nFlash, uint32_t BlockAddr) { + + w25qxx_write_enable(nFlash); + + w25qxxCsBegin(nFlash); + + w25qxxReadWriteByte(nFlash, W25X_BlockErase); + + if(sFlash.info.addr_size == 24) { + w25qxxReadWriteByte(nFlash, (BlockAddr & 0xFF000000) >> 24); + } + + w25qxxReadWriteByte(nFlash, (BlockAddr & 0xFF0000) >> 16); + + w25qxxReadWriteByte(nFlash, (BlockAddr & 0xFF00) >> 8); + + w25qxxReadWriteByte(nFlash, BlockAddr & 0xFF); + + w25qxxCsEnd(nFlash); + + w25qxxWaitBusy(nFlash); +} + +void w25qxxChipErase(eFLASH_t *nFlash) +{ + w25qxx_write_enable(nFlash); + + w25qxxCsBegin(nFlash); + + w25qxxReadWriteByte(nFlash, W25X_ChipErase); + + w25qxxCsEnd(nFlash); + + w25qxxWaitBusy(nFlash); +} + +void w25qxxPageWrite(eFLASH_t *nFlash, uint8_t* pBuffer, uint32_t WriteAddr, uint32_t NumByteToWrite) +{ + w25qxx_write_enable(nFlash); + w25qxxCsBegin(nFlash); + w25qxxReadWriteByte(nFlash, W25X_PageProgram); + + if(sFlash.info.addr_size == 24) { + w25qxxReadWriteByte(nFlash, (WriteAddr & 0xFF000000) >> 24); + } + w25qxxReadWriteByte(nFlash, (WriteAddr & 0xFF0000) >> 16); + w25qxxReadWriteByte(nFlash, (WriteAddr & 0xFF00) >> 8); + w25qxxReadWriteByte(nFlash, WriteAddr & 0xFF); + + if(NumByteToWrite > SPI_FLASH_PerWritePageSize) { + NumByteToWrite = SPI_FLASH_PerWritePageSize; + } + while (NumByteToWrite--) + { + w25qxxReadWriteByte(nFlash, *pBuffer); + pBuffer++; + } + w25qxxCsEnd(nFlash); + w25qxxWaitBusy(nFlash); +} + +void w25qxxBufferWrite(eFLASH_t *nFlash, uint8_t* pBuffer, uint32_t WriteAddr, uint32_t NumByteToWrite) +{ + uint8_t NumOfPage = 0, + NumOfSingle = 0, + Addr = 0, + count = 0, + temp = 0; + + Addr = WriteAddr % SPI_FLASH_PageSize; + count = SPI_FLASH_PageSize - Addr; + NumOfPage = NumByteToWrite / SPI_FLASH_PageSize; + NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize; + + if (Addr == 0) { + if (NumOfPage == 0) { + w25qxxPageWrite(nFlash, pBuffer, WriteAddr, NumByteToWrite); + } + else { + while (NumOfPage--) { + w25qxxPageWrite(nFlash, pBuffer, WriteAddr, SPI_FLASH_PageSize); + WriteAddr += SPI_FLASH_PageSize; + pBuffer += SPI_FLASH_PageSize; + } + w25qxxPageWrite(nFlash, pBuffer, WriteAddr, NumOfSingle); + } + } + else { + if (NumOfPage == 0) { + if (NumOfSingle > count) { + temp = NumOfSingle - count; + w25qxxPageWrite(nFlash, pBuffer, WriteAddr, count); + WriteAddr += count; + pBuffer += count; + w25qxxPageWrite(nFlash, pBuffer, WriteAddr, temp); + } + else { + w25qxxPageWrite(nFlash, pBuffer, WriteAddr, NumByteToWrite); + } + } + else { + NumByteToWrite -= count; + NumOfPage = NumByteToWrite / SPI_FLASH_PageSize; + NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize; + + w25qxxPageWrite(nFlash, pBuffer, WriteAddr, count); + WriteAddr += count; + pBuffer += count; + + while (NumOfPage--) { + w25qxxPageWrite(nFlash, pBuffer, WriteAddr, SPI_FLASH_PageSize); + WriteAddr += SPI_FLASH_PageSize; + pBuffer += SPI_FLASH_PageSize; + } + + if (NumOfSingle != 0) { + w25qxxPageWrite(nFlash, pBuffer, WriteAddr, NumOfSingle); + } + } + } +} + +void w25qxxBufferRead(eFLASH_t *nFlash, uint8_t* pBuffer, uint32_t ReadAddr, __IO uint32_t NumByteToRead) +{ + w25qxxCsBegin(nFlash); + w25qxxReadWriteByte(nFlash, W25X_ReadData); + + if(sFlash.info.addr_size == 24) { + w25qxxReadWriteByte(nFlash, (ReadAddr & 0xFF000000) >> 24); + } + w25qxxReadWriteByte(nFlash, (ReadAddr & 0xFF0000) >> 16); + w25qxxReadWriteByte(nFlash, (ReadAddr& 0xFF00) >> 8); + w25qxxReadWriteByte(nFlash, ReadAddr & 0xFF); + + // if (NumByteToRead <= 32 || !flash_dma_mode) { + while (NumByteToRead--) { + *pBuffer = w25qxxReadWriteByte(nFlash, Dummy_Byte); + pBuffer++; + } + // } + // else + // spi_flash_Read(pBuffer, NumByteToRead); + + w25qxxCsEnd(nFlash); +} + + +/************************************************************* + * w25qxx test + * For this test, I need to rewrite it to another way to test + * the performance difference of different Flash. + * - Erase time test. + * - Erase time test of single sector. + * - Time test for reading 10K data. + * - Time test for writing 10K data. + * **********************************************************/ +#define TxBufferSize1 (countof(TxBuffer1) - 1) +#define RxBufferSize1 (countof(TxBuffer1) - 1) +#define countof(a) (sizeof(a) / sizeof(*(a))) +#define BufferSize (countof(Tx_Buffer)-1) + +#define FLASH_WriteAddress 0x00000 +#define FLASH_ReadAddress FLASH_WriteAddress +#define FLASH_SectorToErase FLASH_WriteAddress + +uint8_t Tx_Buffer[1024 * 10];; +uint8_t Rx_Buffer[BufferSize]; +volatile uint32_t w25qxxTestCount = 0; // 1ms count +volatile uint32_t w25qxxGetTick = 0; +uint32_t getW25QxxCount() { + return w25qxxTestCount; +} + +void w25qxxTestBegin() { + w25qxxTestCount = 0; +} + +void w25qxxTestHandler(void) { + w25qxxTestCount++; +} + +void w25qxxTest() { + + uint32_t writeAddr = 0; + memset(Tx_Buffer, 0x31, sizeof(Tx_Buffer)); + + // 擦除FLASH + printf("Begin erase....\n"); + w25qxxTestBegin(); + w25qxxChipErase(&sFlash); + + // 往FLASH写入数据 + printf("Begin write data...\n"); + + for(int i = 0; i < sFlash.info.flash_size; i++) { + w25qxxBufferWrite(&sFlash, Tx_Buffer, writeAddr + i*1024, BufferSize); + } + // w25qxxBufferWrite(&sFlash, Tx_Buffer, FLASH_WriteAddress, BufferSize); + + + printf("begin read data....\n"); + + // w25qxxBufferRead(&sFlash, Rx_Buffer, FLASH_ReadAddress, BufferSize); + for(int i = 0; i < sFlash.info.flash_size; i++) { + w25qxxBufferRead(&sFlash, Rx_Buffer, writeAddr + i*1024, BufferSize); + printf("data:%s\n", Rx_Buffer); + } +} + +// #endif + + diff --git a/src/eflash.h b/src/eflash.h new file mode 100644 index 0000000..d510275 --- /dev/null +++ b/src/eflash.h @@ -0,0 +1,121 @@ +/* + eflash.h + + Copyright (c) 2021-2023 sola + + This part of the code belongs to the corresponding platform that + I adapt to, has nothing to do with GRBL, and is only related to + the platform. Therefore, if you use this part of the code, + please indicate the source +*/ + +#ifndef __eflash_h__ +#define __eflash_h__ + +// #include "../../../grbl_config.h" +#include "eflash_port.h" + +#define eFLASH_VERSION 20230530 + +// Flash choose +// EF -- W25QXX +typedef enum { + + /* W25QXX */ + sFLASH_ID_X16 = 0x3015, + sFLASH_ID_16 = 0x4015, + sFLASH_ID_64 = 0X4017, + sFLASH_ID_128 = 0X4018, + sFLASH_ID_256 = 0X4019, + +}sFLASH_ID_t; + + +#define sFLAHS_SPI_MODE 0 +#define sFLASH_QSPI_MODE 1 + +#define SPI_FLASH_PageSize 256 +#define SPI_FLASH_PerWritePageSize 256 + +// Flash cmd +#define W25X_WriteEnable 0x06 +#define W25X_WriteDisable 0x04 +#define W25X_ReadStatusReg 0x05 +#define W25X_ReadStatusReg2 0x35 +#define W25X_ReadStatusReg3 0x15 +#define W25X_WriteStatusReg 0x01 +#define W25X_WriteStatusReg2 0x31 +#define W25X_WriteStatusReg3 0x11 +#define W25X_ReadData 0x03 +#define W25X_FastReadData 0x0B +#define W25X_FastReadDual 0x3B +#define W25X_PageProgram 0x02 +#define W25X_BlockErase 0xD8 +#define W25X_SectorErase 0x20 +#define W25X_ChipErase 0xC7 +#define W25X_PowerDown 0xB9 +#define W25X_ReleasePowerDown 0xAB +#define W25X_DeviceID 0xAB +#define W25X_ManufactDeviceID 0x90 +#define W25X_JedecDeviceID 0x9F +#define W25X_Enter4ByteMode 0xB7 +#define W25X_ReadStatusRegister3 0x15 + +#define WIP_Flag 0x01 /* Write In Progress (WIP) flag */ +#define Dummy_Byte 0xFF + + +/* Flash support list. */ +enum { + W25QXX = 0xEF, + ZB25QXX = 0x5E, +}Flash_Man_t; + +typedef struct { + uint32_t flash_id; /* Flash ID */ + uint32_t flash_man; /* Flash device name */ + uint32_t flash_size; /* Flash size(KB) */ + uint32_t flash_delay_time; /* Flash delay */ + uint32_t sector_size; /* Flash sector size */ + uint8_t flash_mode; /* Flash mode(SPI or QSPI) */ + uint8_t flash_state; /* check if flash can't read, use for FATFS */ + uint8_t addr_size; +}eFLASH_INFO_t; + +typedef struct{ + + eFLASH_INFO_t info; + + /* Base Func */ + void (*flashSpiGpioInit)(void); /* 初始化GPIO的函数,包含GPIO复用 */ + void (*flashSpiInit)(void); /* 初始化SPI外设 */ + uint8_t (*flashSpiReadWriteByte)(uint8_t ); /* SPI读写函数 */ + bool (*flashIsTransFinish)(void); /* 判断是否传输完成 */ + void (*flashEnableTrans)(void); + void (*flashDisableTrans)(void); +}eFLASH_t; +extern eFLASH_t sFlash; + + +void flashSpiInit(eFLASH_t *nFlash); + +void w25qxxInit(eFLASH_t *nFlash); +uint32_t w25qxxRead_ID(eFLASH_t *nFlash); +uint16_t w25qxxReadWriteByte(eFLASH_t *nFlash, uint16_t wdata); + +void w25qxxEnterFlashMode(eFLASH_t *nFlash); +void w25qxxSectorErase(eFLASH_t *nFlash, uint32_t SectorAddr); +void w25qxxBlockErase(eFLASH_t *nFlash, uint32_t BlockAddr); +void w25qxxChipErase(eFLASH_t *nFlash); +void w25qxxBufferWrite(eFLASH_t *nFlash, uint8_t* pBuffer, uint32_t WriteAddr, uint32_t NumByteToWrite); +void w25qxxBufferRead(eFLASH_t *nFlash, uint8_t* pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead); + +void w25qxxTest(); +void w25qxxTestHandler(void); + +#ifdef USE_FATFS + bool w25qxx_fs_init(void); + void get_w25qxx_fafts_info(void); +#endif /* USE_FATFS */ + +#endif /* __hal_w25qxx_h__ */ diff --git a/src/eflash_port.c b/src/eflash_port.c new file mode 100644 index 0000000..92b16e5 --- /dev/null +++ b/src/eflash_port.c @@ -0,0 +1,49 @@ +/* + eflash_port.c + + Copyright (c) 2021-2023 sola + + This part of the code belongs to the corresponding platform that + I adapt to, has nothing to do with GRBL, and is only related to + the platform. Therefore, if you use this part of the code, + please indicate the source +*/ + +#include "eflash_port.h" + +/********************************************** + * Flash HAL SPI + * + * ********************************************/ + +/* GPIO 引脚初始化 */ +void flashSpiGpioInit(void) +{ + BspSpiGpioInit(); +} + +void w25qxx_spi_cs_enabel(void) { + BspSpiTranBegin(W25QXX_SPI_CS_GPIO, W25QXX_SPI_CS_PIN); +} + +void w25qxx_spi_cs_disable(void) { + BspSpiTranEnd(W25QXX_SPI_CS_GPIO, W25QXX_SPI_CS_PIN); +} + +void w25qxxSpiRegiest(void) { +#ifdef HAS_W25Qxx + sFlash.info.flash_mode = sFLAHS_SPI_MODE; + sFlash.info.flash_delay_time = 100; + sFlash.info.flash_id = 0; + sFlash.info.flash_size = 0; + sFlash.flashSpiInit = spi_for_w25qxx_init; + sFlash.flashSpiGpioInit = flashSpiGpioInit; + sFlash.flashSpiReadWriteByte = w25qxx_spi_read_write; + // sFlash.flashIsTransFinish = flashIsTransFinish; + sFlash.flashDisableTrans = w25qxx_spi_cs_disable; + sFlash.flashEnableTrans = w25qxx_spi_cs_enabel; +#endif +} + + + diff --git a/src/eflash_port.h b/src/eflash_port.h new file mode 100644 index 0000000..6ddbd21 --- /dev/null +++ b/src/eflash_port.h @@ -0,0 +1,23 @@ +/* + eflash_port.h + + Copyright (c) 2021-2023 sola + + This part of the code belongs to the corresponding platform that + I adapt to, has nothing to do with GRBL, and is only related to + the platform. Therefore, if you use this part of the code, + please indicate the source +*/ + +#ifndef __w25qxx_port_h +#define __w25qxx_port_h + +#include "main.h" + + +void flashSpiGpioInit(void); +void w25qxx_spi_cs_enabel(void); +void w25qxx_spi_cs_disable(void); +void w25qxxSpiRegiest(void); + +#endif diff --git a/src/hal_w25qxx.c b/src/hal_w25qxx.c deleted file mode 100644 index abac04b..0000000 --- a/src/hal_w25qxx.c +++ /dev/null @@ -1,354 +0,0 @@ -#include "hal_w25qxx.h" - -static void w25qxx_enable(NFLASH_t *nFlash) -{ - nFlash->w25qxx_enable_trans(); -} - -static void w25qxx_disable(NFLASH_t *nFlash) -{ - nFlash->w25qxx_disable_trans(); -} - -__WEAK void hal_w25qxx_spi_reg(NFLASH_t *nFlash) { - - //..TODO -} - -void hal_w25qxx_spi_init(NFLASH_t *nFlash) -{ - hal_w25qxx_spi_reg(nFlash); - - if(sFlash.flash_mode == sFLAHS_SPI_MODE) { - nFlash->w25qxx_spi_gpio_init(); - nFlash->w25qxx_spi_init(); - }else { - - } -} - -bool is_write_had_finish(void) -{ - if (__HAL_SPI_GET_FLAG(&w25qxx_spi, SPI_FLAG_TXE) == RESET) { return true; } - else { return false; } -} - -bool is_read_had_finish(void) { - if (__HAL_SPI_GET_FLAG(&w25qxx_spi, SPI_FLAG_RXNE) == RESET) { return true; } - else { return false;} -} - -/*--------------------------------------------------------------------------------------------*/ -uint16_t w25qxx_read_write_byte(NFLASH_t *nFlash, uint16_t wdata) -{ - - // return w25qxx_spi_read_write(wdata); - return nFlash->w25qxx_spi_read_write_byte(wdata); -} - -void w25qxx_init(NFLASH_t *nFlash) -{ - uint32_t get_id_size = 0x00; - - hal_w25qxx_spi_init(nFlash); - - w25qxx_enter_flash_mode(nFlash); - - sFlash.flash_id = w25qxx_read_id(nFlash); - - get_id_size = sFlash.flash_id & 0x00ffff; - - switch(get_id_size) { - case sFLASH_ID_X16: sFlash.flash_size = (16 / 8) *1024; break; - case sFLASH_ID_16: sFlash.flash_size = (16 / 8) *1024; break; - case sFLASH_ID_64: sFlash.flash_size = (64 / 8) *1024; break; - case sFLASH_ID_128: sFlash.flash_size = (128 / 8) *1024; break; - case sFLASH_ID_256: sFlash.flash_size = (258 / 8) *1024; break; - default: sFlash.flash_size = 0; break; - } - - if(sFlash.flash_size != 0) { sFlash.flash_state = 1; } - else { sFlash.flash_state = 0; } -} - -uint32_t w25qxx_read_id(NFLASH_t *nFlash) -{ - uint32_t id = 0; - w25qxx_enable(nFlash); - w25qxx_read_write_byte(nFlash, W25X_JedecDeviceID); - id |= w25qxx_read_write_byte(nFlash, 0xff) << 16; - id |= w25qxx_read_write_byte(nFlash, 0xff) << 8; - id |= w25qxx_read_write_byte(nFlash, 0xff) << 0; - w25qxx_disable(nFlash); - return id; -} - -uint8_t w25qxx_read_sr_reg(NFLASH_t *nFlash, uint8_t reg) -{ - uint8_t byte = 0, command = 0; - switch (reg) - { - case 1: - command = W25X_ReadStatusReg; - break; - case 2: - command = W25X_ReadStatusReg2; - break; - case 3: - command = W25X_ReadStatusReg3; - break; - default: - command = W25X_ReadStatusReg; - break; - } - w25qxx_enable(nFlash); - w25qxx_read_write_byte(nFlash, command); - byte = w25qxx_read_write_byte(nFlash, 0Xff); - w25qxx_disable(nFlash); - return byte; -} - -void w25qxx_write_sr_reg(NFLASH_t *nFlash, uint8_t reg, uint8_t sr) -{ - uint8_t command = 0; - switch (reg) - { - case 1: - command = W25X_ReadStatusReg; - break; - case 2: - command = W25X_ReadStatusReg2; - break; - case 3: - command = W25X_ReadStatusReg3; - break; - case 4: - command = W25X_PowerDown; - break; - - default: - command = W25X_ReadStatusReg; - break; - } - w25qxx_enable(nFlash); - w25qxx_read_write_byte(nFlash, command); - w25qxx_read_write_byte(nFlash, sr); - w25qxx_disable(nFlash); -} - -void w25qxx_write_enable(NFLASH_t *nFlash) -{ - w25qxx_enable(nFlash); - w25qxx_read_write_byte(nFlash, W25X_WriteEnable); - w25qxx_disable(nFlash); -} - -void w25qxx_write_disable(NFLASH_t *nFlash) -{ - w25qxx_enable(nFlash); - w25qxx_read_write_byte(nFlash, W25X_WriteDisable); - w25qxx_disable(nFlash); -} - -void w25qxx_wait_busy(NFLASH_t *nFlash) -{ - while ((w25qxx_read_sr_reg(nFlash, 1) & 0x01) == 0x01); // 等待BUSY位清�? -} - -void w25qxx_enter_power_down(NFLASH_t *nFlash) { - w25qxx_enable(nFlash); - w25qxx_read_write_byte(nFlash, W25X_PowerDown); - w25qxx_disable(nFlash); -} - -void w25qxx_wakeup(NFLASH_t *nFlash) { - w25qxx_enable(nFlash); - w25qxx_read_write_byte(nFlash, W25X_ReleasePowerDown); - w25qxx_disable(nFlash); -} - - -void w25qxx_enter_flash_mode(NFLASH_t *nFlash) -{ - uint8_t Temp; - w25qxx_enable(nFlash); - w25qxx_read_write_byte(nFlash, W25X_ReadStatusRegister3); - Temp = w25qxx_read_write_byte(nFlash, Dummy_Byte); - w25qxx_disable(nFlash); - - if((Temp & 0x01) == 0) - { - w25qxx_enable(nFlash); - w25qxx_read_write_byte(nFlash, W25X_Enter4ByteMode); - w25qxx_disable(nFlash); - } -} - -void w25qxx_sector_erase(NFLASH_t *nFlash, uint32_t SectorAddr) -{ - w25qxx_write_enable(nFlash); - - w25qxx_wait_busy(nFlash); - - w25qxx_enable(nFlash); - - w25qxx_read_write_byte(nFlash, W25X_SectorErase); - - w25qxx_read_write_byte(nFlash, (SectorAddr & 0xFF000000) >> 24); - - w25qxx_read_write_byte(nFlash, (SectorAddr & 0xFF0000) >> 16); - - w25qxx_read_write_byte(nFlash, (SectorAddr & 0xFF00) >> 8); - - w25qxx_read_write_byte(nFlash, SectorAddr & 0xFF); - - w25qxx_disable(nFlash); - - w25qxx_wait_busy(nFlash); -} - -void w25qxx_bulk_erase(NFLASH_t *nFlash) -{ - w25qxx_write_enable(nFlash); - - w25qxx_enable(nFlash); - - w25qxx_read_write_byte(nFlash, W25X_ChipErase); - - w25qxx_disable(nFlash); - - w25qxx_wait_busy(nFlash); -} - -void w25qxx_page_write(NFLASH_t *nFlash, uint8_t* pBuffer, uint32_t WriteAddr, uint32_t NumByteToWrite) -{ - w25qxx_write_enable(nFlash); - - w25qxx_enable(nFlash); - - w25qxx_read_write_byte(nFlash, W25X_PageProgram); - - w25qxx_read_write_byte(nFlash, (WriteAddr & 0xFF000000) >> 24); - - w25qxx_read_write_byte(nFlash, (WriteAddr & 0xFF0000) >> 16); - - w25qxx_read_write_byte(nFlash, (WriteAddr & 0xFF00) >> 8); - - w25qxx_read_write_byte(nFlash, WriteAddr & 0xFF); - - if(NumByteToWrite > SPI_FLASH_PerWritePageSize) - { - NumByteToWrite = SPI_FLASH_PerWritePageSize; - } - - while (NumByteToWrite--) - { - w25qxx_read_write_byte(nFlash, *pBuffer); - - pBuffer++; - } - - w25qxx_disable(nFlash); - - w25qxx_wait_busy(nFlash); -} - -void w25qxx_buffer_write(NFLASH_t *nFlash, uint8_t* pBuffer, uint32_t WriteAddr, uint32_t NumByteToWrite) -{ - uint8_t NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0; - - Addr = WriteAddr % SPI_FLASH_PageSize; - - count = SPI_FLASH_PageSize - Addr; - - NumOfPage = NumByteToWrite / SPI_FLASH_PageSize; - - NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize; - - if (Addr == 0) { - if (NumOfPage == 0) - { - w25qxx_page_write(nFlash, pBuffer, WriteAddr, NumByteToWrite); - } - else { - - while (NumOfPage--) { - w25qxx_page_write(nFlash, pBuffer, WriteAddr, SPI_FLASH_PageSize); - WriteAddr += SPI_FLASH_PageSize; - pBuffer += SPI_FLASH_PageSize; - } - - w25qxx_page_write(nFlash, pBuffer, WriteAddr, NumOfSingle); - } - } - - else { - if (NumOfPage == 0) { - - if (NumOfSingle > count) - { - temp = NumOfSingle - count; - - w25qxx_page_write(nFlash, pBuffer, WriteAddr, count); - - WriteAddr += count; - - pBuffer += count; - - w25qxx_page_write(nFlash, pBuffer, WriteAddr, temp); - } - else - { - w25qxx_page_write(nFlash, pBuffer, WriteAddr, NumByteToWrite); - } - } - else { - NumByteToWrite -= count; - NumOfPage = NumByteToWrite / SPI_FLASH_PageSize; - NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize; - - w25qxx_page_write(nFlash, pBuffer, WriteAddr, count); - - WriteAddr += count; - - pBuffer += count; - - while (NumOfPage--) - { - w25qxx_page_write(nFlash, pBuffer, WriteAddr, SPI_FLASH_PageSize); - WriteAddr += SPI_FLASH_PageSize; - pBuffer += SPI_FLASH_PageSize; - } - if (NumOfSingle != 0) - { - w25qxx_page_write(nFlash, pBuffer, WriteAddr, NumOfSingle); - } - } - } -} - -void w25qxx_buffer_read(NFLASH_t *nFlash, uint8_t* pBuffer, uint32_t ReadAddr, __IO uint32_t NumByteToRead) -{ - w25qxx_enable(nFlash); - - w25qxx_read_write_byte(nFlash, W25X_ReadData); - - w25qxx_read_write_byte(nFlash, (ReadAddr & 0xFF000000) >> 24); - - w25qxx_read_write_byte(nFlash, (ReadAddr & 0xFF0000) >> 16); - - w25qxx_read_write_byte(nFlash, (ReadAddr& 0xFF00) >> 8); - - w25qxx_read_write_byte(nFlash, ReadAddr & 0xFF); - - while (NumByteToRead--) - { - *pBuffer = w25qxx_read_write_byte(nFlash, Dummy_Byte); - - pBuffer++; - } - w25qxx_disable(nFlash); -} - - - diff --git a/src/hal_w25qxx.h b/src/hal_w25qxx.h deleted file mode 100644 index 1997c6f..0000000 --- a/src/hal_w25qxx.h +++ /dev/null @@ -1,89 +0,0 @@ -#ifndef __hal_w25qxx_h__ -#define __hal_w25qxx_h__ - -#include "../../../main.h" - -// Flash choose -// EF -#define sFLASH_ID_X16 0x3015 //W25X16 -#define sFLASH_ID_16 0x4015 //W25Q16 -#define sFLASH_ID_64 0X4017 //W25Q64 -#define sFLASH_ID_128 0X4018 //W25Q128 -#define sFLASH_ID_256 0X4019 //W25Q256 - -#define sFLAHS_SPI_MODE 0 -#define sFLASH_QSPI_MODE 1 - -#define SPI_FLASH_PageSize 256 -#define SPI_FLASH_PerWritePageSize 256 - -typedef struct{ - - uint32_t flash_id; // Flash ID - uint32_t flash_man; // Flash Man - uint32_t flash_size; - uint32_t flash_delay_time; - uint8_t flash_mode; - uint8_t flash_state; // check if flash can't read, use for FATFS - - /* Base Func */ - void (*w25qxx_spi_gpio_init)(void); // 初始化GPIO的函数,包含GPIO复用 - void (*w25qxx_spi_init)(void); // 初始化SPI外设 - uint8_t (*w25qxx_spi_read_write_byte)(uint8_t ); // SPI读写函数 - bool (*w25qxx_is_trans_finish)(void); // 判断是否传输完成 - void (*w25qxx_enable_trans)(void); - void (*w25qxx_disable_trans)(void); - -}NFLASH_t; -extern NFLASH_t sFlash; - -// Flash cmd -#define W25X_WriteEnable 0x06 -#define W25X_WriteDisable 0x04 -#define W25X_ReadStatusReg 0x05 -#define W25X_ReadStatusReg2 0x35 -#define W25X_ReadStatusReg3 0x15 -#define W25X_WriteStatusReg 0x01 -#define W25X_WriteStatusReg2 0x31 -#define W25X_WriteStatusReg3 0x11 -#define W25X_ReadData 0x03 -#define W25X_FastReadData 0x0B -#define W25X_FastReadDual 0x3B -#define W25X_PageProgram 0x02 -#define W25X_BlockErase 0xD8 -#define W25X_SectorErase 0x20 -#define W25X_ChipErase 0xC7 -#define W25X_PowerDown 0xB9 -#define W25X_ReleasePowerDown 0xAB -#define W25X_DeviceID 0xAB -#define W25X_ManufactDeviceID 0x90 -#define W25X_JedecDeviceID 0x9F -#define W25X_Enter4ByteMode 0xB7 -#define W25X_ReadStatusRegister3 0x15 - -#define WIP_Flag 0x01 /* Write In Progress (WIP) flag */ -#define Dummy_Byte 0xFF - -void hal_w25qxx_spi_init(NFLASH_t *nFlash); -__WEAK void hal_w25qxx_spi_reg(NFLASH_t *nFlash); - -void w25qxx_init(NFLASH_t *nFlash); -uint32_t w25qxx_read_id(NFLASH_t *nFlash); -uint16_t w25qxx_read_write_byte(NFLASH_t *nFlash, uint16_t wdata); -void w25qxx_read_buff(uint8_t *pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead); -void w15qxx_write_page(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite); -void w25qxx_write_no_check(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite); -void w25qxx_erase_chip(void); -void w25qxx_erase_sector(uint32_t Dst_Addr); -void w25qxx_write_buff(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite); - -void w25qxx_enter_flash_mode(NFLASH_t *nFlash); -void w25qxx_sector_erase(NFLASH_t *nFlash, uint32_t SectorAddr); -void w25qxx_bulk_erase(NFLASH_t *nFlash); -void w25qxx_buffer_write(NFLASH_t *nFlash, uint8_t* pBuffer, uint32_t WriteAddr, uint32_t NumByteToWrite); -void w25qxx_buffer_read(NFLASH_t *nFlash, uint8_t* pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead); - -void w25qxx_spi_regiest(); - - -#endif