找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5511|回复: 0
打印 上一主题 下一主题
收起左侧

16MB的FLASHW25Q128驱动

[复制链接]
跳转到指定楼层
楼主
ID:75926 发表于 2015-4-10 17:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #define SPI_SCK_LOW() GPIOB->ODR&=~(1<<3)
  2. #define SPI_SCK_HIGH()         GPIOB->ODR|=1<<3

  3. #define SPI_MOSI_LOW()GPIOB->ODR&=~(1<<5)
  4. #define SPI_MOSI_HIGH()GPIOB->ODR|=1<<5

  5. #define SPI_CS_LOW()GPIOB->ODR&=~(1<<14)
  6. #define SPI_CS_HIGH()        GPIOB->ODR|=1<<14

  7. #define SPI_MISO_Read()(GPIOB->IDR&(1<<4))


  8. #define W25X_WriteEnable          0x06     //写允许命令
  9. #define W25X_WriteDisable          0x04    //禁止命令
  10. #define W25X_ReadStatusReg      0x05   //读状态寄存器
  11. #define W25X_WriteStatusReg      0x01  //写状态寄存器
  12. #define W25X_ReadData                0x03  //读数据
  13. #define W25X_FastReadData         0x0B //快读
  14. #define W25X_FastReadDual         0x3B  
  15. #define W25X_PageProgram          0x02  //页写
  16. #define W25X_BlockErase               0xD8  //快擦除
  17. #define W25X_SectorErase         0x20  //扇区擦除
  18. #define W25X_ChipErase                            0xC7  //整盘擦除
  19. #define W25X_PowerDown                         0xB9  //低功耗
  20. #define W25X_ReleasePowerDown           0xAB
  21. #define W25X_DeviceID                             0xAB
  22. #define W25X_ManufactDeviceID             0x90
  23. #define W25X_JedecDeviceID                 0x9F


  24. static void SPI_SendByte(uint8_t Byte)         //使用SPI总线发送1个字节的数据
  25. {
  26. //uint8_t Cnt;
  27. //SPI_SCK_LOW();
  28. //for(Cnt=0;Cnt<8;Cnt++)
  29. //{
  30. //if(Byte&0x80)
  31. //SPI_MOSI_HIGH();
  32. //else
  33. //SPI_MOSI_LOW();
  34. //SPI_SCK_HIGH();
  35. //Byte<<=1;
  36. //SPI_SCK_LOW();
  37. //}
  38. while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
  39. SPI_I2S_SendData(SPI1,Byte);
  40. while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
  41. SPI_I2S_ReceiveData(SPI1);
  42. }

  43. static uint8_t SPI_ReceiveByte(void)   //使用SPI总线接收1个字节的数据
  44. {
  45. //uint8_t Byte=0,Cnt;
  46. ////GPIOB->ODR|=1<<4;
  47. //for(Cnt=0;Cnt<8;Cnt++)
  48. //{
  49. //SPI_SCK_HIGH();
  50. //Byte<<=1;
  51. //if(SPI_MISO_Read())
  52. //Byte++;
  53. //SPI_SCK_LOW();
  54. //}
  55. //return Byte;
  56. while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
  57. SPI_I2S_SendData(SPI1,0xff);
  58. while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
  59. return SPI_I2S_ReceiveData(SPI1);
  60. }
  61. /************************FLASH****************************/

  62. uint8_t FLASH_ReadStatusReg(void)   //读状态寄存器
  63. {
  64. uint8_t Status;
  65. SPI_CS_LOW();        
  66. SPI_SendByte(W25X_ReadStatusReg);      
  67. Status=SPI_ReceiveByte();        
  68. SPI_CS_HIGH();     
  69. return Status;
  70. }

  71. void FLASH_WriteEnable(void)
  72. {
  73. SPI_CS_LOW();
  74. SPI_SendByte(W25X_WriteEnable);
  75. SPI_CS_HIGH();
  76. }
  77. void FLASH_WriteByte(uint32_t Address,uint8_t Byte)
  78. {
  79. FLASH_WriteEnable();//改变磁盘的操作都需要写允许命令
  80. SPI_CS_LOW();
  81. SPI_SendByte(W25X_PageProgram);
  82. SPI_SendByte(Address>>16);
  83. SPI_SendByte(Address>>8);
  84. SPI_SendByte(Address);
  85. SPI_SendByte(Byte);
  86. SPI_CS_HIGH();
  87. while(FLASH_ReadStatusReg()&0x01);
  88. }

  89. void FLASH_ReadByte(uint32_t Address ,uint8_t *pByte)
  90. {
  91. SPI_CS_LOW();
  92. SPI_SendByte(W25X_ReadData);
  93. SPI_SendByte(Address>>16);
  94. SPI_SendByte(Address>>8);
  95. SPI_SendByte(Address);
  96. *pByte=SPI_ReceiveByte();
  97. SPI_CS_HIGH();
  98. }

  99. uint16_t FLASH_ReadID(void)
  100. {
  101. uint16_t Temp=0;
  102. SPI_CS_LOW();
  103. SPI_SendByte(W25X_ManufactDeviceID);
  104. SPI_SendByte(0x00);
  105. SPI_SendByte(0x00);
  106. SPI_SendByte(0x00);
  107. Temp|=SPI_ReceiveByte()<<8;
  108. Temp|=SPI_ReceiveByte();
  109. SPI_CS_HIGH();
  110. return Temp;
  111. }

  112. void FLASH_Erase_Sector(uint32_t Address)
  113. {
  114. FLASH_WriteEnable();
  115. while(FLASH_ReadStatusReg()&0x01);
  116. SPI_CS_LOW();
  117. SPI_SendByte(W25X_SectorErase);
  118. SPI_SendByte(Address>>16);
  119. SPI_SendByte(Address>>8);
  120. SPI_SendByte(Address);
  121. SPI_CS_HIGH();
  122. while(FLASH_ReadStatusReg()&0x01);
  123. }

  124. void FLASH_Wrase_Chip(void)
  125. {
  126. FLASH_WriteEnable();
  127. SPI_SendByte(0x00);
  128. while(FLASH_ReadStatusReg()&0x01);
  129. SPI_CS_LOW();
  130. SPI_SendByte(W25X_ChipErase);
  131. SPI_CS_HIGH();
  132. while(FLASH_ReadStatusReg()&0x01);
  133. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表