obj_spi_w25qxx.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #ifdef USE_W25QXX_SPI
  2. #ifndef OBJ_SPI_W25QXX_H
  3. #define OBJ_SPI_W25QXX_H
  4. #include <stdbool.h>
  5. #include "stm32f7xx_hal.h"
  6. #include "stm32f7xx_hal_spi.h"
  7. #define SPIx SPI2
  8. #define SPIx_CLK_ENABLE() __HAL_RCC_SPI2_CLK_ENABLE()
  9. #define SPIx_SCK_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
  10. #define SPIx_MISO_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
  11. #define SPIx_MOSI_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
  12. #define SPIx_CS_GPIO_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE()
  13. #define SPIx_FORCE_RESET() __HAL_RCC_SPI2_FORCE_RESET()
  14. #define SPIx_RELEASE_RESET() __HAL_RCC_SPI2_RELEASE_RESET()
  15. /* Definition for SPIx Pins */
  16. #define SPIx_SCK_PIN GPIO_PIN_13
  17. #define SPIx_SCK_GPIO_PORT GPIOB
  18. #define SPIx_MISO_PIN GPIO_PIN_14
  19. #define SPIx_MISO_GPIO_PORT GPIOB
  20. #define SPIx_MOSI_PIN GPIO_PIN_15
  21. #define SPIx_MOSI_GPIO_PORT GPIOB
  22. #define _W25QXX_SPI hspi_w25q
  23. #if 0
  24. #define _W25QXX_CS_GPIO GPIOD
  25. #define _W25QXX_CS_PIN GPIO_PIN_8
  26. #else
  27. #define _W25QXX_CS_GPIO GPIOB
  28. #define _W25QXX_CS_PIN GPIO_PIN_12
  29. #endif
  30. #define _W25QXX_USE_FREERTOS 0
  31. #define _W25QXX_DEBUG 0
  32. #define W25QXX_CS_HIGH() {HAL_GPIO_WritePin(_W25QXX_CS_GPIO,_W25QXX_CS_PIN,GPIO_PIN_SET);\
  33. delay_us(1);}while(0);
  34. #define W25QXX_CS_LOW() HAL_GPIO_WritePin(_W25QXX_CS_GPIO,_W25QXX_CS_PIN,GPIO_PIN_RESET)
  35. typedef enum
  36. {
  37. ID_W25Q10 = 1,
  38. ID_W25Q20,
  39. ID_W25Q40,
  40. ID_W25Q80,
  41. ID_W25Q16,
  42. ID_W25Q32,
  43. ID_W25Q64,
  44. ID_W25Q128,
  45. ID_W25Q256,
  46. ID_W25Q512,
  47. }W25QXX_ID_t;
  48. typedef struct
  49. {
  50. W25QXX_ID_t ID;
  51. uint8_t UniqID[8];
  52. uint16_t PageSize;
  53. uint32_t PageCount;
  54. uint32_t SectorSize;
  55. uint32_t SectorCount;
  56. uint32_t BlockSize;
  57. uint32_t BlockCount; // so block trong chip flash
  58. uint32_t CapacityInKiloByte;
  59. uint8_t StatusRegister1;
  60. uint8_t StatusRegister2;
  61. uint8_t StatusRegister3;
  62. uint8_t Lock;
  63. }w25qxx_t;
  64. extern SPI_HandleTypeDef _W25QXX_SPI;
  65. extern w25qxx_t w25qxx;
  66. //############################################################################
  67. // in Page,Sector and block read/write functions, can put 0 to read maximum bytes
  68. //############################################################################
  69. bool W25qxx_Init(void);
  70. void W25qxx_Cs_Init(void);
  71. void W25qxx_Spi_Init(void);
  72. uint32_t W25qxx_ReadID(void);
  73. // BLOCK > SECTOR , w25q128 => 16 sector = 1 block
  74. void W25qxx_EraseChip(void);
  75. void W25qxx_EraseSector(uint32_t SectorAddr);
  76. void W25qxx_EraseBlock(uint32_t BlockAddr);
  77. // tra ve so page trong 1 sector, so page trong 1 block.....
  78. uint32_t W25qxx_PageToSector(uint32_t PageAddress);
  79. uint32_t W25qxx_PageToBlock(uint32_t PageAddress);
  80. uint32_t W25qxx_SectorToBlock(uint32_t SectorAddress);
  81. uint32_t W25qxx_SectorToPage(uint32_t SectorAddress);
  82. uint32_t W25qxx_BlockToPage(uint32_t BlockAddress);
  83. // kiem tra bo nho co con trong khong, neu con trong có the ghi data vao
  84. // offset_in_byte byte bat dau ghi
  85. // numbyte_to_checkup => so byte can check, ghi trong page/sector/block
  86. bool W25qxx_IsEmptyPage(uint32_t Page_Address,uint32_t OffsetInByte,uint32_t NumByteToCheck_up_to_PageSize);
  87. bool W25qxx_IsEmptySector(uint32_t Sector_Address,uint32_t OffsetInByte,uint32_t NumByteToCheck_up_to_SectorSize);
  88. bool W25qxx_IsEmptyBlock(uint32_t Block_Address,uint32_t OffsetInByte,uint32_t NumByteToCheck_up_to_BlockSize);
  89. void W25qxx_WriteByte(uint8_t pBuffer,uint32_t Bytes_Address);
  90. void W25qxx_WritePage(uint8_t *pBuffer ,uint32_t Page_Address,uint32_t OffsetInByte,uint32_t NumByteToWrite_up_to_PageSize);
  91. void W25qxx_WriteSector(uint8_t *pBuffer,uint32_t Sector_Address,uint32_t OffsetInByte,uint32_t NumByteToWrite_up_to_SectorSize);
  92. void W25qxx_WriteBlock(uint8_t* pBuffer,uint32_t Block_Address,uint32_t OffsetInByte,uint32_t NumByteToWrite_up_to_BlockSize);
  93. void W25qxx_ReadByte(uint8_t *pBuffer,uint32_t Bytes_Address);
  94. void W25qxx_ReadBytes(uint8_t *pBuffer,uint32_t ReadAddr,uint32_t NumByteToRead);
  95. void W25qxx_ReadPage(uint8_t *pBuffer,uint32_t Page_Address,uint32_t OffsetInByte,uint32_t NumByteToRead_up_to_PageSize);
  96. void W25qxx_ReadSector(uint8_t *pBuffer,uint32_t Sector_Address,uint32_t OffsetInByte,uint32_t NumByteToRead_up_to_SectorSize);
  97. void W25qxx_ReadBlock(uint8_t* pBuffer,uint32_t Block_Address,uint32_t OffsetInByte,uint32_t NumByteToRead_up_to_BlockSize);
  98. //############################################################################
  99. #endif //----------------------OBJ_SPI_W25QXX_H-----------------------------//
  100. #endif //----------------------USE_W25QXX_SPI-------------------------------//