spi.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. *********************************************************************************************************
  3. *
  4. * 模块名称 : SPI总线驱动
  5. * 文件名称 : bsp_spi_bus.h
  6. * 版 本 : V1.0
  7. * 说 明 : 头文件
  8. *
  9. * Copyright (C), 2014-2015, 安富莱电子 www.armfly.com
  10. *
  11. *********************************************************************************************************
  12. */
  13. #ifndef __SPI_H
  14. #define __SPI_H
  15. #include "stm32f2xx_spi.h"
  16. #include <stdio.h>
  17. /* Private typedef -----------------------------------------------------------*/
  18. //#define sFLASH_ID 0xEF3015 //W25X16
  19. //#define sFLASH_ID 0xEF4015 //W25Q16
  20. //#define sFLASH_ID 0XEF4017 //W25Q64
  21. #define sFLASH_ID 0XEF4018 //W25Q128
  22. //#define SPI_FLASH_PageSize 4096
  23. #define SPI_FLASH_PageSize 256
  24. #define SPI_FLASH_PerWritePageSize 256
  25. /* Private define ------------------------------------------------------------*/
  26. /*命令定义-开头*******************************/
  27. #define W25X_WriteEnable 0x06
  28. #define W25X_WriteDisable 0x04
  29. #define W25X_ReadStatusReg 0x05
  30. #define W25X_WriteStatusReg 0x01
  31. #define W25X_ReadData 0x03
  32. #define W25X_FastReadData 0x0B
  33. #define W25X_FastReadDual 0x3B
  34. #define W25X_PageProgram 0x02
  35. #define W25X_BlockErase 0xD8
  36. #define W25X_SectorErase 0x20
  37. #define W25X_ChipErase 0xC7
  38. #define W25X_PowerDown 0xB9
  39. #define W25X_ReleasePowerDown 0xAB
  40. #define W25X_DeviceID 0xAB
  41. #define W25X_ManufactDeviceID 0x90
  42. #define W25X_JedecDeviceID 0x9F
  43. #define WIP_Flag 0x01 /* Write In Progress (WIP) flag */
  44. #define Dummy_Byte 0xFF
  45. /*命令定义-结尾*******************************/
  46. /*SPI接口定义-开头****************************/
  47. #define FLASH_SPI SPI1
  48. #define FLASH_SPI_CLK RCC_APB2Periph_SPI1
  49. #define FLASH_SPI_CLK_INIT RCC_APB2PeriphClockCmd
  50. #define FLASH_SPI_SCK_PIN GPIO_Pin_5
  51. #define FLASH_SPI_SCK_GPIO_PORT GPIOA
  52. #define FLASH_SPI_SCK_GPIO_CLK RCC_AHB1Periph_GPIOA
  53. #define FLASH_SPI_SCK_PINSOURCE GPIO_PinSource5
  54. #define FLASH_SPI_SCK_AF GPIO_AF_SPI1
  55. #define FLASH_SPI_MISO_PIN GPIO_Pin_6
  56. #define FLASH_SPI_MISO_GPIO_PORT GPIOA
  57. #define FLASH_SPI_MISO_GPIO_CLK RCC_AHB1Periph_GPIOA
  58. #define FLASH_SPI_MISO_PINSOURCE GPIO_PinSource6
  59. #define FLASH_SPI_MISO_AF GPIO_AF_SPI1
  60. #define FLASH_SPI_MOSI_PIN GPIO_Pin_7
  61. #define FLASH_SPI_MOSI_GPIO_PORT GPIOA
  62. #define FLASH_SPI_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOA
  63. #define FLASH_SPI_MOSI_PINSOURCE GPIO_PinSource7
  64. #define FLASH_SPI_MOSI_AF GPIO_AF_SPI1
  65. #define FLASH_CS_PIN GPIO_Pin_4
  66. #define FLASH_CS_GPIO_PORT GPIOA
  67. #define FLASH_CS_GPIO_CLK RCC_AHB1Periph_GPIOA
  68. #define SPI_FLASH_CS_LOW() {FLASH_CS_GPIO_PORT->BSRR=(uint32_t)FLASH_CS_PIN<<16;}
  69. #define SPI_FLASH_CS_HIGH() {FLASH_CS_GPIO_PORT->BSRR=FLASH_CS_PIN;}
  70. /*SPI接口定义-结尾****************************/
  71. /*等待超时时间*/
  72. #define SPIT_FLAG_TIMEOUT ((uint32_t)0x1000)
  73. #define SPIT_LONG_TIMEOUT ((uint32_t)(10 * SPIT_FLAG_TIMEOUT))
  74. /*信息输出*/
  75. #define FLASH_DEBUG_ON 1
  76. #define FLASH_INFO(fmt,arg...) printf("<<-FLASH-INFO->> "fmt"\n",##arg)
  77. #define FLASH_ERROR(fmt,arg...) printf("<<-FLASH-ERROR->> "fmt"\n",##arg)
  78. #define FLASH_DEBUG(fmt,arg...) do{\
  79. if(FLASH_DEBUG_ON)\
  80. printf("<<-FLASH-DEBUG->> [%d]"fmt"\n",__LINE__, ##arg);\
  81. }while(0)
  82. void SPI_FLASH_Init(void);
  83. void SPI_FLASH_SectorErase(u32 SectorAddr);
  84. void SPI_FLASH_BulkErase(void);
  85. void SPI_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite);
  86. void SPI_FLASH_BufferWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite);
  87. void SPI_FLASH_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead);
  88. u32 SPI_FLASH_ReadID(void);
  89. u32 SPI_FLASH_ReadDeviceID(void);
  90. void SPI_FLASH_StartReadSequence(u32 ReadAddr);
  91. void SPI_Flash_PowerDown(void);
  92. void SPI_Flash_WAKEUP(void);
  93. u8 SPI_FLASH_ReadByte(void);
  94. u8 SPI_FLASH_SendByte(u8 byte);
  95. u16 SPI_FLASH_SendHalfWord(u16 HalfWord);
  96. void SPI_FLASH_WriteEnable(void);
  97. void SPI_FLASH_WaitForWriteEnd(void);
  98. #endif
  99. /***************************** 安富莱电子 www.armfly.com (END OF FILE) *********************************/