| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- /*
- * QSPI NOR 驱动 (GD25Q128)。
- * 注意!App 侧不访问 NOR 芯片: 本文件在 App 工程中没有调用点, NOR 的读/写/擦只发生在
- * Bootloader (暂存区收发、备份、回滚、校验)。
- * 注意!所以本文件比 BL 侧同名文件少一批修复(出错上报 / 忙等超时 / 越界保护)——那是
- * "改动只落在 BL", 不是漏同步, 别为此把本文件改成和 BL 一样。
- * 将来 App 若真要读 NOR, 应把 BL 版整份同步过来, 不要在旧版上打补丁。
- */
- #include "Int_flash.h"
- #include "quadspi.h"
- #include <string.h>
- extern QSPI_HandleTypeDef hqspi;
- /* SPI Flash 地址位数:2 = 24位地址,3 = 32位地址(探测 ID 后更新) */
- static volatile uint8_t g_int_flash_addrw = 2;
- /* 4KB 扇区读-改-写缓冲区 */
- static uint8_t g_int_flash_buf[4096];
- /*
- * 发送一条 QSPI 命令
- * mode 位域定义:
- * [1:0] 指令模式 (0=无, 1=单线, 2=双线, 3=四线)
- * [3:2] 地址模式 (0=无, 1=单线, 2=双线, 3=四线)
- * [5:4] 地址宽度 (0=8位, 1=16位, 2=24位, 3=32位)
- * [7:6] 数据模式 (0=无, 1=单线, 2=双线, 3=四线)
- */
- static void Int_flash_send_cmd(uint8_t cmd, uint32_t addr, uint8_t mode, uint8_t dummy)
- {
- QSPI_CommandTypeDef qc = {0};
- qc.Instruction = cmd;
- qc.Address = addr;
- qc.DummyCycles = dummy;
- switch ((mode >> 0) & 0x03)
- {
- case 1: qc.InstructionMode = QSPI_INSTRUCTION_1_LINE; break;
- case 2: qc.InstructionMode = QSPI_INSTRUCTION_2_LINES; break;
- case 3: qc.InstructionMode = QSPI_INSTRUCTION_4_LINES; break;
- default: qc.InstructionMode = QSPI_INSTRUCTION_NONE; break;
- }
- switch ((mode >> 2) & 0x03)
- {
- case 1: qc.AddressMode = QSPI_ADDRESS_1_LINE; break;
- case 2: qc.AddressMode = QSPI_ADDRESS_2_LINES; break;
- case 3: qc.AddressMode = QSPI_ADDRESS_4_LINES; break;
- default: qc.AddressMode = QSPI_ADDRESS_NONE; break;
- }
- switch ((mode >> 4) & 0x03)
- {
- case 0: qc.AddressSize = QSPI_ADDRESS_8_BITS; break;
- case 1: qc.AddressSize = QSPI_ADDRESS_16_BITS; break;
- case 2: qc.AddressSize = QSPI_ADDRESS_24_BITS; break;
- default: qc.AddressSize = QSPI_ADDRESS_32_BITS; break;
- }
- switch ((mode >> 6) & 0x03)
- {
- case 1: qc.DataMode = QSPI_DATA_1_LINE; break;
- case 2: qc.DataMode = QSPI_DATA_2_LINES; break;
- case 3: qc.DataMode = QSPI_DATA_4_LINES; break;
- default: qc.DataMode = QSPI_DATA_NONE; break;
- }
- qc.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
- qc.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
- qc.DdrMode = QSPI_DDR_MODE_DISABLE;
- qc.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
- HAL_QSPI_Command(&hqspi, &qc, 5000);
- }
- /* QSPI 接收 datalen 个字节存入 buf */
- static uint8_t Int_flash_receive(uint8_t *buf, uint32_t datalen)
- {
- hqspi.Instance->DLR = datalen - 1;
- if (HAL_QSPI_Receive(&hqspi, buf, 5000) == HAL_OK)
- {
- return 0;
- }
- return 1;
- }
- /* QSPI 从 buf 发送 datalen 个字节 */
- static uint8_t Int_flash_transmit(uint8_t *buf, uint32_t datalen)
- {
- hqspi.Instance->DLR = datalen - 1;
- if (HAL_QSPI_Transmit(&hqspi, buf, 5000) == HAL_OK)
- {
- return 0;
- }
- return 1;
- }
- /* 写使能(置位 SR1 的 WEL 位) */
- static void Int_flash_write_enable(void)
- {
- Int_flash_send_cmd(INT_FLASH_CMD_WRITE_ENABLE, 0, 0x01, 0);
- }
- /* 写禁止(清除 SR1 的 WEL 位) */
- //static void Int_flash_write_disable(void)
- //{
- // Int_flash_send_cmd(INT_FLASH_CMD_WRITE_DISABLE, 0, 0x01, 0);
- //}
- /* 读取状态寄存器(1~3),数据单线模式 */
- static uint8_t Int_flash_read_sr(uint8_t regno)
- {
- uint8_t byte = 0;
- uint8_t command = INT_FLASH_CMD_READ_STATUS_1;
- switch (regno)
- {
- case 2: command = INT_FLASH_CMD_READ_STATUS_2; break;
- case 3: command = INT_FLASH_CMD_READ_STATUS_3; break;
- default: command = INT_FLASH_CMD_READ_STATUS_1; break;
- }
- Int_flash_send_cmd(command, 0, 0x41, 0); /* 单线数据,无地址,单线指令 */
- Int_flash_receive(&byte, 1);
- return byte;
- }
- /* 写状态寄存器(1~3),数据单线模式 */
- static void Int_flash_write_sr(uint8_t regno, uint8_t sr)
- {
- uint8_t command = INT_FLASH_CMD_WRITE_STATUS_1;
- switch (regno)
- {
- case 2: command = INT_FLASH_CMD_WRITE_STATUS_2; break;
- case 3: command = INT_FLASH_CMD_WRITE_STATUS_3; break;
- default: command = INT_FLASH_CMD_WRITE_STATUS_1; break;
- }
- Int_flash_send_cmd(command, 0, 0x41, 0); /* 单线数据,无地址,单线指令 */
- Int_flash_transmit(&sr, 1);
- }
- /* 使能 QE 位(SR2 bit1),以使用 IO2/IO3 四线模式 */
- static void Int_flash_qe_enable(void)
- {
- uint8_t stareg2 = Int_flash_read_sr(2);
- if ((stareg2 & 0x02) == 0)
- {
- Int_flash_write_enable();
- stareg2 |= (1 << 1);
- Int_flash_write_sr(2, stareg2);
- }
- }
- /* 编程一个页(最多 256 字节),四线 I/O,不跨页边界 */
- static void Int_flash_write_page(uint8_t *pbuf, uint32_t addr, uint16_t datalen)
- {
- Int_flash_write_enable();
- /* 四线数据 | 24/32位单线地址 | 单线指令 */
- Int_flash_send_cmd(INT_FLASH_CMD_PAGE_PROGRAM_QUAD, addr,
- (3 << 6) | (g_int_flash_addrw << 4) | (1 << 2) | (1 << 0), 0);
- Int_flash_transmit(pbuf, datalen);
- Int_flash_wait_busy();
- }
- void Int_flash_wait_busy(void)
- {
- while ((Int_flash_read_sr(1) & 0x01) == 0x01);
- }
- uint8_t Int_flash_init(void)
- {
- /* QSPI 外设已由 MX_QUADSPI_Init() 提前初始化 */
- Int_flash_qe_enable();
- g_int_flash_addrw = 2;
- if (Int_flash_read_id() == INT_FLASH_ID_GD25Q128)
- {
- return 0;
- }
- return 1;
- }
- uint16_t Int_flash_read_id(void)
- {
- uint8_t temp[2];
- uint16_t deviceid;
- /* 单线数据 2B | 24位单线地址 | 单线指令 */
- Int_flash_send_cmd(INT_FLASH_CMD_MANUF_DEVICE_ID, 0,
- (1 << 6) | (2 << 4) | (1 << 2) | (1 << 0), 0);
- Int_flash_receive(temp, 2);
- deviceid = (uint16_t)((temp[0] << 8) | temp[1]);
- return deviceid;
- }
- uint8_t Int_flash_read(uint8_t *pbuf, uint32_t addr, uint32_t datalen)
- {
- /* 四线数据 | 24/32位四线地址 | 单线指令,6 个空转周期 */
- Int_flash_send_cmd(INT_FLASH_CMD_FAST_READ_QUAD, addr,
- (3 << 6) | (g_int_flash_addrw << 4) | (3 << 2) | (1 << 0), 6);
- return Int_flash_receive(pbuf, datalen);
- }
- uint8_t Int_flash_erase_sector(uint32_t saddr)
- {
- uint32_t byteaddr = saddr * INT_FLASH_SECTOR_SIZE;
- Int_flash_write_enable();
- Int_flash_wait_busy();
- /* 无数据 | 24/32位单线地址 | 单线指令 */
- Int_flash_send_cmd(INT_FLASH_CMD_SECTOR_ERASE, byteaddr,
- (0 << 6) | (g_int_flash_addrw << 4) | (1 << 2) | (1 << 0), 0);
- Int_flash_wait_busy();
- return 0;
- }
- uint8_t Int_flash_erase_block(uint32_t baddr)
- {
- uint32_t byteaddr = baddr * INT_FLASH_BLOCK_SIZE;
- Int_flash_write_enable();
- Int_flash_wait_busy();
- /* 无数据 | 24/32位单线地址 | 单线指令 */
- Int_flash_send_cmd(INT_FLASH_CMD_BLOCK_ERASE, byteaddr,
- (0 << 6) | (g_int_flash_addrw << 4) | (1 << 2) | (1 << 0), 0);
- Int_flash_wait_busy();
- return 0;
- }
- uint8_t Int_flash_erase_chip(void)
- {
- Int_flash_write_enable();
- Int_flash_wait_busy();
- /* 无数据 | 无地址 | 单线指令 */
- Int_flash_send_cmd(INT_FLASH_CMD_CHIP_ERASE, 0, 0x01, 0);
- Int_flash_wait_busy();
- return 0;
- }
- /* 按页边界逐段写入(不跨页),确保每个 PAGE_PROGRAM 都在单个 256 字节页内 */
- static void Int_flash_write_nocheck(uint8_t *pbuf, uint32_t addr, uint32_t datalen)
- {
- uint16_t pagerem;
- pagerem = INT_FLASH_PAGE_SIZE - (addr % INT_FLASH_PAGE_SIZE);
- if (datalen <= pagerem)
- {
- pagerem = datalen;
- }
- while (1)
- {
- Int_flash_write_page(pbuf, addr, pagerem);
- if (datalen == pagerem)
- {
- break;
- }
- pbuf += pagerem;
- addr += pagerem;
- datalen -= pagerem;
- pagerem = (datalen > INT_FLASH_PAGE_SIZE) ? INT_FLASH_PAGE_SIZE : datalen;
- }
- }
- uint8_t Int_flash_write(uint8_t *pbuf, uint32_t addr, uint32_t datalen)
- {
- uint32_t secpos;
- uint16_t secoff;
- uint16_t secremain;
- uint16_t i;
- uint8_t *flash_buf = g_int_flash_buf;
- secpos = addr / INT_FLASH_SECTOR_SIZE;
- secoff = addr % INT_FLASH_SECTOR_SIZE;
- secremain = INT_FLASH_SECTOR_SIZE - secoff;
- if (datalen <= secremain)
- {
- secremain = datalen;
- }
- while (1)
- {
- Int_flash_read(flash_buf, secpos * INT_FLASH_SECTOR_SIZE, INT_FLASH_SECTOR_SIZE);
- for (i = 0; i < secremain; i++)
- {
- if (flash_buf[secoff + i] != 0xFF)
- {
- break;
- }
- }
- if (i < secremain)
- {
- Int_flash_erase_sector(secpos);
- for (i = 0; i < secremain; i++)
- {
- flash_buf[secoff + i] = pbuf[i];
- }
- Int_flash_write_nocheck(flash_buf, secpos * INT_FLASH_SECTOR_SIZE,
- INT_FLASH_SECTOR_SIZE);
- }
- else
- {
- Int_flash_write_nocheck(pbuf, addr, secremain);
- }
- if (datalen == secremain)
- {
- break;
- }
- secpos++;
- secoff = 0;
- pbuf += secremain;
- addr += secremain;
- datalen -= secremain;
- secremain = (datalen > INT_FLASH_SECTOR_SIZE) ? INT_FLASH_SECTOR_SIZE : datalen;
- }
- return 0;
- }
|