116 lines
4.0 KiB
C
116 lines
4.0 KiB
C
|
|
#ifndef __W25Q64_H
|
|||
|
|
#define __W25Q64_H
|
|||
|
|
|
|||
|
|
#include <stdint.h>
|
|||
|
|
#include "main.h"
|
|||
|
|
|
|||
|
|
#ifdef __cplusplus
|
|||
|
|
extern "C" {
|
|||
|
|
#endif
|
|||
|
|
|
|||
|
|
/* -------------------------------------------------
|
|||
|
|
* W25Q64 <EFBFBD>ⲿ SPI Flash <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
|
|||
|
|
* Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SPI<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> GPIO<EFBFBD><EFBFBD>:
|
|||
|
|
* CS -> PB4
|
|||
|
|
* CLK -> PB3
|
|||
|
|
* DO -> PB5 (Flash <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> / MCU <EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
|
* DI -> PA15 (Flash <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> / MCU <EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
|
* ------------------------------------------------- */
|
|||
|
|
|
|||
|
|
#define W25Q64_FLASH_SIZE (8ul * 1024 * 1024) /* 8MB */
|
|||
|
|
#define W25Q64_SECTOR_SIZE 4096u
|
|||
|
|
#define W25Q64_BLOCK_SIZE 65536u
|
|||
|
|
#define W25Q64_PAGE_SIZE 256u
|
|||
|
|
|
|||
|
|
#define W25Q64_JEDEC_ID 0xEF4017u /* W25Q64JV */
|
|||
|
|
|
|||
|
|
/* æ<>ȴ<EFBFBD><C8B4><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>/64KB <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
|
#define W25Q64_BUSY_TIMEOUT_MS 5000u
|
|||
|
|
#define W25Q64_CHIP_ERASE_TIMEOUT_MS 60000u
|
|||
|
|
|
|||
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
|
#define W25Q64_CMD_WRITE_ENABLE 0x06u
|
|||
|
|
#define W25Q64_CMD_WRITE_DISABLE 0x04u
|
|||
|
|
#define W25Q64_CMD_READ_STATUS1 0x05u
|
|||
|
|
#define W25Q64_CMD_READ_DATA 0x03u
|
|||
|
|
#define W25Q64_CMD_PAGE_PROGRAM 0x02u
|
|||
|
|
#define W25Q64_CMD_SECTOR_ERASE 0x20u
|
|||
|
|
#define W25Q64_CMD_BLOCK_ERASE 0xD8u
|
|||
|
|
#define W25Q64_CMD_CHIP_ERASE 0xC7u
|
|||
|
|
#define W25Q64_CMD_READ_ID 0x9Fu
|
|||
|
|
|
|||
|
|
/* <20>ⲿ Flash <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֣<EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD> 256KB<4B><42><EFBFBD>㹻<EFBFBD><E3B9BB><EFBFBD><EFBFBD> 224KB <20><> APP<50><50> */
|
|||
|
|
#define W25Q64_SLOT_SIZE 0x40000u /* 256KB */
|
|||
|
|
#define W25Q64_SLOT_BACKUP_ADDR 0x000000u /* <20><> A<><41><EFBFBD>ɰ汾 / <20><><EFBFBD>˾<EFBFBD><CBBE><EFBFBD> */
|
|||
|
|
#define W25Q64_SLOT_NEW_ADDR 0x040000u /* <20><> B<><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>صĹ̼<C4B9> */
|
|||
|
|
|
|||
|
|
/* <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 4KB <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>࣬<EFBFBD><E0A3AC><EFBFBD><EFBFBD><EFBFBD>ܿ<EFBFBD><DCBF>̼<EFBFBD><CCBC><EFBFBD> B <20><> 0x040000~0x07FFFF */
|
|||
|
|
#define W25Q64_INFO_SECTOR_A 0x080000u
|
|||
|
|
#define W25Q64_INFO_SECTOR_B 0x081000u
|
|||
|
|
#define W25Q64_INFO_MAGIC 0x5741544Fu /* 'WATO' */
|
|||
|
|
|
|||
|
|
/* OTA ״̬ */
|
|||
|
|
#define W25Q64_STATE_IDLE 0u
|
|||
|
|
#define W25Q64_STATE_DOWNLOADED 1u
|
|||
|
|
#define W25Q64_STATE_COPYING 2u
|
|||
|
|
#define W25Q64_STATE_INSTALLED 3u
|
|||
|
|
#define W25Q64_STATE_ROLLBACK 4u
|
|||
|
|
|
|||
|
|
#define W25Q64_MAX_TRIALS 3u
|
|||
|
|
|
|||
|
|
#pragma pack(push, 1)
|
|||
|
|
typedef struct {
|
|||
|
|
uint32_t magic; /* 0x5741544F */
|
|||
|
|
uint32_t sequence; /* д<><D0B4><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>Խ<EFBFBD><D4BD>Խ<EFBFBD><D4BD> */
|
|||
|
|
uint16_t new_version; /* <20>°汾<C2B0><E6B1BE> */
|
|||
|
|
uint32_t new_size; /* <20>¹̼<C2B9><CCBC><EFBFBD>С */
|
|||
|
|
uint16_t new_crc16; /* <20>¹̼<C2B9> MODBUS CRC16 */
|
|||
|
|
uint8_t new_valid; /* <20><> B <20>̼<EFBFBD><CCBC>Ƿ<EFBFBD><C7B7><EFBFBD>Ч */
|
|||
|
|
uint8_t update_state; /* <20><>ǰ OTA ״̬ */
|
|||
|
|
uint16_t boot_trial_count; /* <20>ѳ<EFBFBD><D1B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
|
uint8_t app_confirmed; /* APP <20><><EFBFBD><EFBFBD><EFBFBD>ȶ<EFBFBD><C8B6><EFBFBD><EFBFBD><EFBFBD> 1 */
|
|||
|
|
uint8_t reserved1;
|
|||
|
|
uint16_t backup_version; /* <20><><EFBFBD>ݹ̼<DDB9><CCBC>汾<EFBFBD><E6B1BE> */
|
|||
|
|
uint32_t backup_size; /* <20><><EFBFBD>ݹ̼<DDB9><CCBC><EFBFBD>С */
|
|||
|
|
uint16_t backup_crc16; /* <20><><EFBFBD>ݹ̼<DDB9> CRC16 */
|
|||
|
|
uint8_t backup_valid; /* <20><> A <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>Ч */
|
|||
|
|
uint8_t reserved2;
|
|||
|
|
uint16_t checksum; /* ǰ<><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD>ۼӺ<DBBC> */
|
|||
|
|
} W25Q64_OtaInfo;
|
|||
|
|
#pragma pack(pop)
|
|||
|
|
|
|||
|
|
/* <20><><EFBFBD><EFBFBD> SPI Flash <20><><EFBFBD><EFBFBD> */
|
|||
|
|
uint8_t W25Q64_Init(void);
|
|||
|
|
void W25Q64_ReadID(uint8_t *id3);
|
|||
|
|
uint8_t W25Q64_ReadStatus(void);
|
|||
|
|
uint8_t W25Q64_WaitBusy(uint32_t timeout_ms); /* <20><><EFBFBD><EFBFBD> 0=<3D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1=<3D><>ʱ */
|
|||
|
|
uint8_t W25Q64_WriteEnable(void); /* <20><><EFBFBD><EFBFBD> 0=<3D>ɹ<EFBFBD><C9B9><EFBFBD>1=<3D><>ʱ */
|
|||
|
|
void W25Q64_SectorErase(uint32_t addr);
|
|||
|
|
void W25Q64_BlockErase(uint32_t addr);
|
|||
|
|
void W25Q64_ChipErase(void);
|
|||
|
|
void W25Q64_PageProgram(uint32_t addr, const uint8_t *data, uint16_t len);
|
|||
|
|
void W25Q64_Read(uint32_t addr, uint8_t *buf, uint32_t len);
|
|||
|
|
|
|||
|
|
void W25Q64_EraseRegion(uint32_t addr, uint32_t size);
|
|||
|
|
int W25Q64_WriteBuffer(uint32_t addr, const uint8_t *data, uint32_t len);
|
|||
|
|
int W25Q64_Verify(uint32_t addr, const uint8_t *data, uint32_t len);
|
|||
|
|
|
|||
|
|
/* OTA ר<><D7A8><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
|
int W25Q64_OTA_ReadInfo(W25Q64_OtaInfo *info);
|
|||
|
|
int W25Q64_OTA_WriteInfo(const W25Q64_OtaInfo *info);
|
|||
|
|
int W25Q64_OTA_SetNewInfo(uint16_t version, uint32_t size, uint16_t crc16);
|
|||
|
|
int W25Q64_OTA_SetBackupInfo(uint32_t size, uint16_t crc16);
|
|||
|
|
int W25Q64_OTA_ClearNew(void);
|
|||
|
|
int W25Q64_OTA_SetState(uint8_t state);
|
|||
|
|
int W25Q64_OTA_IncTrial(void);
|
|||
|
|
int W25Q64_OTA_SetConfirmed(void);
|
|||
|
|
int W25Q64_OTA_ClearConfirmed(void);
|
|||
|
|
|
|||
|
|
uint16_t W25Q64_CRC16(const uint8_t *data, uint32_t len);
|
|||
|
|
|
|||
|
|
#ifdef __cplusplus
|
|||
|
|
}
|
|||
|
|
#endif
|
|||
|
|
|
|||
|
|
#endif /* __W25Q64_H */
|