w25qxx.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef __W25Qxx_H
  2. #define __W25Qxx_H
  3. #include "gpio.h"
  4. #include "main.h"
  5. #include "stm32l4xx_hal_gpio.h"
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #define W25X_WriteEnable 0x06 //FLASH 写使能
  10. #define W25X_WriteDisable 0x04
  11. #define W25X_ReadStatusReg 0x05 //FLASH 读取状态
  12. #define W25X_WriteStatusReg 0x01 //FLASH 写状态
  13. #define W25X_ReadData 0x03 //可低速下读FLASH数据
  14. #define W25X_FastReadData 0x0B //快速读取
  15. #define W25X_FastReadDual 0x3B
  16. #define W25X_PageProgram 0x02 //按页写FLASH
  17. #define W25X_BlockErase 0xD8
  18. #define W25X_SectorErase 0x20 //擦除FLASH扇区
  19. #define W25X_ChipErase 0xC7 //擦除FLASH块
  20. #define W25X_PowerDown 0xB9 //深度掉电模式功耗1uA
  21. #define W25X_ReleasePowerDown 0xAB //退出深度掉电模式
  22. #define W25X_DeviceID 0xAB
  23. #define W25X_ManufactDeviceID 0x90
  24. #define W25X_JedecDeviceID 0x9F //FLASH 读ID
  25. #define FLASH_CS_PIN GPIO_PIN_12
  26. #define FLASH_CS_PORT GPIOB
  27. #define SPI_FLASH_CS_LOW() HAL_GPIO_WritePin(FLASH_CS_PORT, FLASH_CS_PIN, GPIO_PIN_RESET);
  28. #define SPI_FLASH_CS_HIGH() HAL_GPIO_WritePin(FLASH_CS_PORT, FLASH_CS_PIN, GPIO_PIN_SET);
  29. #define W25Q128_FLASH_SIZE (16 * 1024 * 1024) // 16MB
  30. #define W25Q128_SECTOR_SIZE 4096 // 每扇区4KB
  31. #define W25Q128_PAGE_SIZE 256 // 每页256字节
  32. #define W25Q128_TEMP_DATA_MAX_ADDR (W25Q128_FLASH_SIZE) // 最大可写地址
  33. #define W25Q128_TEMP_DATA_BASE_ADDR 0x00000000 // 起始地址
  34. #define W25Q128_TOTAL_PAGES (W25Q128_FLASH_SIZE / W25Q128_PAGE_SIZE)
  35. #define W25Q128_SECTOR_COUNT (W25Q128_TEMP_DATA_MAX_ADDR / W25Q128_SECTOR_SIZE)
  36. #define FLASH_PRINT_START_PAGE 0
  37. #define FLASH_PRINT_END_PAGE 10 // 打印第0页到第10页,共11页
  38. extern uint32_t W25_Tempaddress;
  39. static uint8_t spi_flash_read_byte(void);
  40. static uint8_t spi_flash_send_byte(uint8_t byte);
  41. static void spi_flash_write_enable(void);
  42. void spi_flash_wait_for_write_end(void) ;
  43. uint32_t spi_flash_read_ID(void);
  44. void spi_flash_read(uint32_t addr,uint8_t *pdata, uint16_t size);
  45. void spi_flash_page_write(uint32_t addr, uint8_t *pdata, uint16_t size);
  46. void spi_flash_write(uint32_t addr, uint8_t *pdata, uint32_t size);
  47. void spi_flash_sector_erase(uint32_t sector_addr);
  48. void spi_flash_block_erase(void);
  49. void spi_flash_enter_deep_power_down(void);
  50. void spi_flash_release_from_deep_power_down(void);
  51. void flash_write_tempprecoh2_data(uint8_t* data, uint32_t length);
  52. void flash_read_all_temp_data(void);
  53. void flash_dma_print_all_pages_start(void);
  54. void flash_dma_print_next_page(void);
  55. void print_flash_pages(uint32_t start, uint32_t end);
  56. void flash_clear_all_data(void);
  57. #endif