obj_spi_flash.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #ifndef __SPI_FLASH_H
  2. #define __SPI_FLASH_H
  3. #include "stm32f7xx.h"
  4. #include <stdio.h>
  5. /* Private typedef -----------------------------------------------------------*/
  6. //#define sFLASH_ID 0xEF3015 //W25X16
  7. //#define sFLASH_ID 0xEF4015 //W25Q16
  8. //#define sFLASH_ID 0XEF4017 //W25Q64
  9. #define sFLASH_ID 0XEF4018 //W25Q128
  10. //#define SPI_FLASH_PageSize 4096
  11. #define SPI_FLASH_PageSize 256
  12. #define SPI_FLASH_PerWritePageSize 256
  13. /* Private define ------------------------------------------------------------*/
  14. /*命令定义-开头*******************************/
  15. #define W25X_WriteEnable 0x06
  16. #define W25X_WriteDisable 0x04
  17. #define W25X_ReadStatusReg 0x05
  18. #define W25X_WriteStatusReg 0x01
  19. #define W25X_ReadData 0x03
  20. #define W25X_FastReadData 0x0B
  21. #define W25X_FastReadDual 0x3B
  22. #define W25X_PageProgram 0x02
  23. #define W25X_BlockErase 0xD8
  24. #define W25X_SectorErase 0x20
  25. #define W25X_ChipErase 0xC7
  26. #define W25X_PowerDown 0xB9
  27. #define W25X_ReleasePowerDown 0xAB
  28. #define W25X_DeviceID 0xAB
  29. #define W25X_ManufactDeviceID 0x90
  30. #define W25X_JedecDeviceID 0x9F
  31. #define WIP_Flag 0x01 /* Write In Progress (WIP) flag */
  32. #define Dummy_Byte 0xFF
  33. /*命令定义-结尾*******************************/
  34. #define SPIx SPI2
  35. #define SPIx_CLK_ENABLE() __HAL_RCC_SPI2_CLK_ENABLE()
  36. #define SPIx_SCK_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
  37. #define SPIx_MISO_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
  38. #define SPIx_MOSI_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
  39. #define SPIx_CS_GPIO_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE()
  40. #define SPIx_FORCE_RESET() __HAL_RCC_SPI2_FORCE_RESET()
  41. #define SPIx_RELEASE_RESET() __HAL_RCC_SPI2_RELEASE_RESET()
  42. /* Definition for SPIx Pins */
  43. #define SPIx_SCK_PIN GPIO_PIN_13
  44. #define SPIx_SCK_GPIO_PORT GPIOB
  45. #define SPIx_MISO_PIN GPIO_PIN_14
  46. #define SPIx_MISO_GPIO_PORT GPIOB
  47. #define SPIx_MOSI_PIN GPIO_PIN_15
  48. #define SPIx_MOSI_GPIO_PORT GPIOB
  49. //#define FLASH_CS_PIN GPIO_PIN_12
  50. //#define FLASH_CS_GPIO_PORT GPIOB
  51. #define FLASH_CS_PIN GPIO_PIN_8
  52. #define FLASH_CS_GPIO_PORT GPIOD
  53. #define SPI_FLASH_CS_LOW() HAL_GPIO_WritePin(FLASH_CS_GPIO_PORT, FLASH_CS_PIN, GPIO_PIN_RESET)
  54. #define SPI_FLASH_CS_HIGH() HAL_GPIO_WritePin(FLASH_CS_GPIO_PORT, FLASH_CS_PIN, GPIO_PIN_SET)
  55. /*SPI接口定义-结尾****************************/
  56. /*等待超时时间*/
  57. #define SPIT_FLAG_TIMEOUT ((uint32_t)0x10000)
  58. #define SPIT_LONG_TIMEOUT ((uint32_t)(10 * SPIT_FLAG_TIMEOUT))
  59. /*信息输出*/
  60. #define FLASH_DEBUG_ON 1
  61. #define FLASH_INFO(fmt,arg...) printf("<<-FLASH-INFO->> "fmt"\n",##arg)
  62. #define FLASH_ERROR(fmt,arg...) printf("<<-FLASH-ERROR->> "fmt"\n",##arg)
  63. #define FLASH_DEBUG(fmt,arg...) do{\
  64. if(FLASH_DEBUG_ON)\
  65. printf("<<-FLASH-DEBUG->> [%d]"fmt"\n",__LINE__, ##arg);\
  66. }while(0)
  67. void SPI_FLASH_W25Q_Init(void);
  68. void SPI_FLASH_Init(void);
  69. void SPI_FLASH_SectorErase(uint32_t SectorAddr);
  70. void SPI_FLASH_BulkErase(void);
  71. void SPI_FLASH_PageWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite);
  72. void SPI_FLASH_BufferWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite);
  73. void SPI_FLASH_BufferRead(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead);
  74. uint32_t SPI_FLASH_ReadID(void);
  75. uint32_t SPI_FLASH_ReadDeviceID(void);
  76. void SPI_FLASH_StartReadSequence(uint32_t ReadAddr);
  77. void SPI_Flash_PowerDown(void);
  78. void SPI_Flash_WAKEUP(void);
  79. uint8_t SPI_FLASH_ReadByte(void);
  80. uint8_t SPI_FLASH_SendByte(uint8_t byte);
  81. uint16_t SPI_FLASH_SendHalfWord(uint16_t HalfWord);
  82. void SPI_FLASH_WriteEnable(void);
  83. void SPI_FLASH_WaitForWriteEnd(void);
  84. #endif /* __SPI_FLASH_H */