norflash(637).h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef _NORFLASH_H_
  2. #define _NORFLASH_H_
  3. /* includes ----------------------------------------------------------*/
  4. #include "stm32f1xx_hal.h"
  5. #include "spi.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdbool.h>
  10. #include <ctype.h>
  11. #include "main.h"
  12. #include "gpio.h"
  13. /* typedef -----------------------------------------------------------*/
  14. /* define ------------------------------------------------------------*/
  15. /* NORFLASH 片选引脚定义 */
  16. #define NORFLASH_CS_GPIO_PORT GPIOC
  17. #define NORFLASH_CS_GPIO_PIN GPIO_PIN_4
  18. #define NORFLASH_CS_GPIO_CLK_ENABLE() do{ __HAL_RCC_GPIOC_CLK_ENABLE(); }while(0) /* 时钟使能 */
  19. /* NORFLASH 片选信号 */
  20. #define NORFLASH_CS(x) do{ x ? \
  21. HAL_GPIO_WritePin(NORFLASH_CS_GPIO_PORT, NORFLASH_CS_GPIO_PIN, GPIO_PIN_SET) : \
  22. HAL_GPIO_WritePin(NORFLASH_CS_GPIO_PORT, NORFLASH_CS_GPIO_PIN, GPIO_PIN_RESET); \
  23. }while(0)
  24. /* FLASH 芯片列表 */
  25. #define W25Q80 0XEF13 /* W25Q80 芯片ID */
  26. #define W25Q16 0XEF14 /* W25Q16 芯片ID */
  27. #define W25Q32 0XEF15 /* W25Q32 芯片ID */
  28. #define W25Q64 0XEF16 /* W25Q64 芯片ID */
  29. #define W25Q128 0XEF17 /* W25Q128 芯片ID */
  30. #define W25Q256 0XEF18 /* W25Q256 芯片ID */
  31. #define BY25Q64 0X6816 /* BY25Q64 芯片ID */
  32. #define BY25Q128 0X6817 /* BY25Q128 芯片ID */
  33. #define NM25Q64 0X5216 /* NM25Q64 芯片ID */
  34. #define NM25Q128 0X5217 /* NM25Q128 芯片ID */
  35. // 指令表
  36. #define FLASH_WriteEnable 0x06
  37. #define FLASH_WriteDisable 0x04
  38. #define FLASH_ReadStatusReg1 0x05
  39. #define FLASH_ReadStatusReg2 0x35
  40. #define FLASH_ReadStatusReg3 0x15
  41. #define FLASH_WriteStatusReg1 0x01
  42. #define FLASH_WriteStatusReg2 0x31
  43. #define FLASH_WriteStatusReg3 0x11
  44. #define FLASH_ReadData 0x03
  45. #define FLASH_FastReadData 0x0B
  46. #define FLASH_FastReadDual 0x3B
  47. #define FLASH_FastReadQuad 0xEB
  48. #define FLASH_PageProgram 0x02
  49. #define FLASH_PageProgramQuad 0x32
  50. #define FLASH_BlockErase 0xD8
  51. #define FLASH_SectorErase 0x20
  52. #define FLASH_ChipErase 0xC7
  53. #define FLASH_PowerDown 0xB9
  54. #define FLASH_ReleasePowerDown 0xAB
  55. #define FLASH_DeviceID 0xAB
  56. #define FLASH_ManufactDeviceID 0x90
  57. #define FLASH_JedecDeviceID 0x9F
  58. #define FLASH_Enable4ByteAddr 0xB7
  59. #define FLASH_Exit4ByteAddr 0xE9
  60. #define FLASH_SetReadParam 0xC0
  61. #define FLASH_EnterQPIMode 0x38
  62. #define FLASH_ExitQPIMode 0xFF
  63. /* macro -------------------------------------------------------------*/
  64. /* variables ---------------------------------------------------------*/
  65. extern uint16_t norflash_TYPE; /* 定义FLASH芯片型号 */
  66. /* function prototypes -----------------------------------------------*/
  67. /* 静态函数 */
  68. static void norflash_wait_busy(void); /* 等待空闲 */
  69. static void norflash_send_address(uint32_t address);/* 发送地址 */
  70. static void norflash_write_page(uint8_t *pbuf, uint32_t addr, uint16_t datalen); /* 写入page */
  71. static void norflash_write_nocheck(uint8_t *pbuf, uint32_t addr, uint16_t datalen); /* 写flash,不带擦除 */
  72. /* 普通函数 */
  73. void norflash_init(void); /* 初始化25QXX */
  74. uint16_t norflash_read_id(void); /* 读取FLASH ID */
  75. void norflash_write_enable(void); /* 写使能 */
  76. uint8_t norflash_read_sr(uint8_t regno); /* 读取状态寄存器 */
  77. void norflash_write_sr(uint8_t regno,uint8_t sr); /* 写状态寄存器 */
  78. void norflash_erase_chip(void); /* 整片擦除 */
  79. void norflash_erase_sector(uint32_t saddr); /* 扇区擦除 */
  80. void norflash_read(uint8_t *pbuf, uint32_t addr, uint16_t datalen); /* 读取flash */
  81. void norflash_write(uint8_t *pbuf, uint32_t addr, uint16_t datalen); /* 写入flash */
  82. #endif