W25Q64.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. #define THIS_FILE_ID 13
  2. #include "includes.h"
  3. //4Kbytes为一个Sector
  4. //16个扇区为1个Block
  5. //W25X16
  6. //容量为2M字节,共有32个Block,512个Sector
  7. //先要调用函数进行sector解保护,或者全局解保护后才能写入.
  8. //W25系列上电默认没有软件写保护,故可直接写入
  9. #define sFLASH_CS_PIN GPIO_Pin_4 /* PA.04 */
  10. #define sFLASH_CS_GPIO_PORT GPIOA /* GPIOA */
  11. #define sFLASH_CS_GPIO_CLK RCC_APB2Periph_GPIOA
  12. #define sFLASH_SPI SPI1
  13. #define sFLASH_SPI_CLK RCC_APB2Periph_SPI1
  14. #define sFLASH_SPI_SCK_PIN GPIO_Pin_5 /* PA.05 */
  15. #define sFLASH_SPI_SCK_GPIO_PORT GPIOA /* GPIOA */
  16. #define sFLASH_SPI_SCK_GPIO_CLK RCC_APB2Periph_GPIOA
  17. #define sFLASH_SPI_MISO_PIN GPIO_Pin_6 /* PA.06 */
  18. #define sFLASH_SPI_MISO_GPIO_PORT GPIOA /* GPIOA */
  19. #define sFLASH_SPI_MISO_GPIO_CLK RCC_APB2Periph_GPIOA
  20. #define sFLASH_SPI_MOSI_PIN GPIO_Pin_7 /* PA.07 */
  21. #define sFLASH_SPI_MOSI_GPIO_PORT GPIOA /* GPIOA */
  22. #define sFLASH_SPI_MOSI_GPIO_CLK RCC_APB2Periph_GPIOA
  23. #define sFLASH_WP_PIN GPIO_Pin_4
  24. #define sFLASH_WP_GPIO_PORT GPIOC
  25. #define RXNE 0x01
  26. #define TXE 0x02
  27. #define BSY 0x80
  28. #define sFLASH_CS_LOW() GPIO_ResetBits(sFLASH_CS_GPIO_PORT, sFLASH_CS_PIN)
  29. #define sFLASH_CS_HIGH() GPIO_SetBits(sFLASH_CS_GPIO_PORT, sFLASH_CS_PIN)
  30. #define sFLASH_WP_EN() GPIO_ResetBits(sFLASH_WP_GPIO_PORT, sFLASH_WP_PIN)
  31. #define sFLASH_WP_DIS() GPIO_SetBits(sFLASH_WP_GPIO_PORT, sFLASH_WP_PIN)
  32. //指令表
  33. #define W25Q64_WriteEnable 0x06
  34. #define W25Q64_WriteDisable 0x04
  35. #define W25Q64_ReadStatusReg 0x05
  36. #define W25Q64_WriteStatusReg 0x01
  37. #define W25Q64_ReadData 0x03
  38. #define W25Q64_FastReadData 0x0B
  39. #define W25Q64_FastReadDual 0x3B //??
  40. #define W25Q64_PageProgram 0x02
  41. #define W25Q64_SectorErase 0x20 //erase 4k
  42. #define W25Q64_BlockErase 0x52 //erase 32k
  43. #define W25Q64_BigBlockErase 0xd8 //erase 64k
  44. #define W25Q64_ChipErase 0xC7 //0x60
  45. #define W25Q64_PowerDown 0xB9
  46. #define W25Q64_ReleasePowerDown 0xAB
  47. #define W25Q64_DeviceID 0x9F
  48. //#define W25Q64_UnprotectSector 0x39
  49. //#define W25Q64_ProtectSector 0x36
  50. uint32_t sFlash_ReadID(void); //读取FLASH ID
  51. u8 sFlash_ReadSR(void); //读取状态寄存器
  52. void sFlash_Write_SR(u8 sr); //写状态寄存器
  53. void sFlash_Write_Enable(void); //写使能
  54. void sFlash_Write_Disable(void); //写保护
  55. void sFlash_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead); //读取flash
  56. void sFlash_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite);//写入flash
  57. void sFlash_Erase_Chip(void); //整片擦除
  58. void sFlash_Erase_Sector(u32 Dst_Addr);//扇区擦除
  59. void sFlash_Wait_Busy(void); //等待空闲
  60. void sFlash_PowerDown(void); //进入掉电模式
  61. void sFlash_WAKEUP(void); //唤醒
  62. //void sFlash_Protect_Sector(u32 Dst_Addr);
  63. //void sFlash_Unprotect_Sector(u32 Dst_Addr);
  64. void sFlash_SetProtectMode(unsigned char mode);//设置保护模式 0--寄存器方式 1--硬件WP操作方式
  65. void sFlash_Global_Protect(void);
  66. void sFlash_Global_Unprotect(void);
  67. static unsigned char sFlashProtectMode=0;
  68. /**********************************************************************
  69. 1ms在8000~9000之间
  70. ***********************************************************************/
  71. void DelayMs(unsigned short ms)
  72. {
  73. unsigned short i;
  74. unsigned short t=ms;
  75. //IWDG_ReloadCounter();
  76. while(t--){
  77. for(i=0;i<6000;i++);
  78. //IWDG_ReloadCounter();
  79. }
  80. }
  81. /**********************************************************************
  82. 1ms在8000~9000之间
  83. fclk=24M
  84. 24*1.24=29.75M
  85. ***********************************************************************/
  86. void DelayUs(unsigned short us)
  87. {
  88. unsigned short i;
  89. unsigned short t=us;
  90. while(t--){
  91. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  92. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  93. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  94. }
  95. }
  96. /***************************************************************
  97. W25Q64_PortInit
  98. ****************************************************************/
  99. void W25Q64_PortInit(void)
  100. {
  101. GPIO_InitTypeDef GPIO_InitStructure;
  102. /*!< sFLASH_SPI_CS_GPIO, sFLASH_SPI_MOSI_GPIO, sFLASH_SPI_MISO_GPIO
  103. and sFLASH_SPI_SCK_GPIO Periph clock enable */
  104. RCC_APB2PeriphClockCmd(sFLASH_CS_GPIO_CLK | sFLASH_SPI_MOSI_GPIO_CLK | sFLASH_SPI_MISO_GPIO_CLK |
  105. sFLASH_SPI_SCK_GPIO_CLK, ENABLE);
  106. /*!< sFLASH_SPI Periph clock enable */
  107. RCC_APB2PeriphClockCmd(sFLASH_SPI_CLK, ENABLE);
  108. /*!< Configure sFLASH_SPI pins: SCK */
  109. GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_SCK_PIN;
  110. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  111. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  112. GPIO_Init(sFLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
  113. /*!< Configure sFLASH_SPI pins: MOSI */
  114. GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_MOSI_PIN;
  115. GPIO_Init(sFLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
  116. /*!< Configure sFLASH_SPI pins: MISO */
  117. GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_MISO_PIN;
  118. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  119. GPIO_Init(sFLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
  120. /*!< Configure sFLASH_CS_PIN pin: sFLASH Card CS pin */
  121. GPIO_InitStructure.GPIO_Pin = sFLASH_CS_PIN;
  122. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  123. GPIO_Init(sFLASH_CS_GPIO_PORT, &GPIO_InitStructure);
  124. //WP
  125. GPIO_InitStructure.GPIO_Pin = sFLASH_WP_PIN;
  126. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  127. GPIO_Init(sFLASH_WP_GPIO_PORT, &GPIO_InitStructure);
  128. sFLASH_WP_DIS();
  129. }
  130. /***************************************************************
  131. W25Q64_Init
  132. ****************************************************************/
  133. int W25Q64_Init(void)
  134. {
  135. uint32_t sFlashId;
  136. SPI_InitTypeDef SPI_InitStructure;
  137. // printf("sFlash Init...\r\n");
  138. W25Q64_PortInit();
  139. /*!< Deselect the FLASH: Chip Select high */
  140. sFLASH_CS_HIGH();
  141. /*!< SPI configuration */
  142. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  143. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  144. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  145. SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  146. SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  147. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  148. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;//4
  149. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  150. SPI_InitStructure.SPI_CRCPolynomial = 7;
  151. SPI_Init(sFLASH_SPI, &SPI_InitStructure);
  152. //
  153. /*!< Enable the sFLASH_SPI */
  154. SPI_Cmd(sFLASH_SPI, ENABLE);
  155. //
  156. DelayUs(1000);
  157. //
  158. sFlashId=sFlash_ReadID();
  159. //printf("sFlash ID=%04x\r\n",sFlashId);
  160. //if(sFlashId==0x00000000 || sFlashId==0xffffffff){
  161. // printf("sFlash Init Fail!\r\n");
  162. // return 0;
  163. //}
  164. //printf("sFlash Init OK!\r\n");
  165. return 1;
  166. }
  167. /**********************************************************************
  168. *
  169. ***********************************************************************/
  170. u8 SPIx_ReadWriteByte(u8 byte)
  171. {
  172. #if 1
  173. // < Loop while DR register in not emplty
  174. while (SPI_I2S_GetFlagStatus(sFLASH_SPI, SPI_I2S_FLAG_TXE) == RESET);
  175. // !< Send byte through the SPI1 peripheral
  176. SPI_I2S_SendData(sFLASH_SPI, byte);
  177. // !< Wait to receive a byte
  178. while (SPI_I2S_GetFlagStatus(sFLASH_SPI, SPI_I2S_FLAG_RXNE) == RESET);
  179. // !< Return the byte read from the SPI bus
  180. return SPI_I2S_ReceiveData(sFLASH_SPI);
  181. #else
  182. sFLASH_SPI->DR= byte;
  183. while (!(sFLASH_SPI->SR & RXNE));
  184. return (sFLASH_SPI->DR);
  185. #endif
  186. }
  187. void W25Q32SendBuf(u8 *buf,u32 len)
  188. {
  189. u32 i;
  190. for (i = 0; i < len; i++)
  191. {
  192. sFLASH_SPI->DR = buf[i];
  193. while (!(sFLASH_SPI->SR & TXE));
  194. sFLASH_SPI->DR;
  195. }
  196. while (sFLASH_SPI->SR & (BSY | RXNE))
  197. {
  198. sFLASH_SPI->DR;
  199. }
  200. }
  201. /*
  202. //SPIx 读写一个字节
  203. //返回值:读取到的字节
  204. u8 SPIx_ReadWriteByte(u8 TxData)
  205. {
  206. u8 retry=0;
  207. while((SPI1->SR&1<<1)==0)//等待发送区空
  208. {
  209. retry++;
  210. if(retry>200)return 0;
  211. }
  212. SPI1->DR=TxData; //发送一个byte
  213. retry=0;
  214. while((SPI1->SR&1<<0)==0) //等待接收完一个byte
  215. {
  216. retry++;
  217. if(retry>200)return 0;
  218. }
  219. return SPI1->DR; //返回收到的数据
  220. }
  221. */
  222. //读取sFlash的状态寄存器
  223. //BIT7 6 5 4 3 2 1 0
  224. //SPR RV TB BP2 BP1 BP0 WEL BUSY
  225. //SPR:默认0,状态寄存器保护位,配合WP使用
  226. //TB,BP2,BP1,BP0:FLASH区域写保护设置
  227. //WEL:写使能锁定
  228. //BUSY:忙标记位(1,忙;0,空闲)
  229. //默认:0x00
  230. u8 sFlash_ReadSR(void)
  231. {
  232. u8 byte=0;
  233. sFLASH_CS_LOW(); //使能器件
  234. SPIx_ReadWriteByte(W25Q64_ReadStatusReg); //发送读取状态寄存器命令
  235. byte=SPIx_ReadWriteByte(0Xff); //读取一个字节
  236. sFLASH_CS_HIGH(); //取消片选
  237. return byte;
  238. }
  239. //写sFlash状态寄存器
  240. //只有SPR,TB,BP2,BP1,BP0(bit 7,5,4,3,2)可以写!!!
  241. void sFlash_Write_SR(u8 sr)
  242. {
  243. sFLASH_CS_LOW(); //使能器件
  244. SPIx_ReadWriteByte(W25Q64_WriteStatusReg); //发送写取状态寄存器命令
  245. SPIx_ReadWriteByte(sr); //写入一个字节
  246. sFLASH_CS_HIGH(); //取消片选
  247. }
  248. //sFlash写使能
  249. //将WEL置位
  250. void sFlash_Write_Enable(void)
  251. {
  252. sFLASH_CS_LOW(); //使能器件
  253. SPIx_ReadWriteByte(W25Q64_WriteEnable); //发送写使能
  254. sFLASH_CS_HIGH(); //取消片选
  255. }
  256. //sFlash写禁止
  257. //将WEL清零
  258. void sFlash_Write_Disable(void)
  259. {
  260. sFLASH_CS_LOW(); //使能器件
  261. SPIx_ReadWriteByte(W25Q64_WriteDisable); //发送写禁止指令
  262. sFLASH_CS_HIGH(); //取消片选
  263. }
  264. //读取芯片ID W25X16的ID:0XEF14
  265. uint32_t sFlash_ReadID(void)
  266. {
  267. uint32_t Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;
  268. sFLASH_CS_LOW();
  269. SPIx_ReadWriteByte(W25Q64_DeviceID);//发送读取ID命令
  270. Temp0=SPIx_ReadWriteByte(0xFF);
  271. Temp1=SPIx_ReadWriteByte(0xFF);
  272. Temp2=SPIx_ReadWriteByte(0xFF);
  273. sFLASH_CS_HIGH();
  274. Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;
  275. return Temp;
  276. }
  277. //读取SPI FLASH
  278. //在指定地址开始读取指定长度的数据
  279. //pBuffer:数据存储区
  280. //ReadAddr:开始读取的地址(24bit)
  281. //NumByteToRead:要读取的字节数(最大65535)
  282. void sFlash_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)
  283. {
  284. u16 i;
  285. sFLASH_CS_LOW(); //使能器件
  286. //SPIx_ReadWriteByte(W25Q64_ReadData); //发送读取命令
  287. SPIx_ReadWriteByte(W25Q64_FastReadData);//快速读命令
  288. SPIx_ReadWriteByte((u8)((ReadAddr)>>16)); //发送24bit地址
  289. SPIx_ReadWriteByte((u8)((ReadAddr)>>8));
  290. SPIx_ReadWriteByte((u8)ReadAddr);
  291. pBuffer[0]=SPIx_ReadWriteByte(0XFF);//快速读命令丢弃一个字节
  292. for(i=0;i<NumByteToRead;i++)
  293. {
  294. pBuffer[i]=SPIx_ReadWriteByte(0XFF); //循环读数
  295. }
  296. sFLASH_CS_HIGH(); //取消片选
  297. }
  298. //SPI在一页(0~65535)内写入少于256个字节的数据
  299. //在指定地址开始写入最大256字节的数据
  300. //pBuffer:数据存储区
  301. //WriteAddr:开始写入的地址(24bit)
  302. //NumByteToWrite:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!
  303. void sFlash_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
  304. {
  305. u16 i;
  306. sFlash_Write_Enable(); //SET WEL
  307. sFLASH_CS_LOW(); //使能器件
  308. SPIx_ReadWriteByte(W25Q64_PageProgram); //发送写页命令
  309. SPIx_ReadWriteByte((u8)((WriteAddr)>>16)); //发送24bit地址
  310. SPIx_ReadWriteByte((u8)((WriteAddr)>>8));
  311. SPIx_ReadWriteByte((u8)WriteAddr);
  312. for(i=0;i<NumByteToWrite;i++)SPIx_ReadWriteByte(pBuffer[i]);//循环写数
  313. sFLASH_CS_HIGH(); //取消片选
  314. sFlash_Wait_Busy(); //等待写入结束
  315. }
  316. //无检验写SPI FLASH
  317. //必须确保所写的地址范围内的数据全部为0XFF,否则在非0XFF处写入的数据将失败!
  318. //具有自动换页功能
  319. //在指定地址开始写入指定长度的数据,但是要确保地址不越界!
  320. //pBuffer:数据存储区
  321. //WriteAddr:开始写入的地址(24bit)
  322. //NumByteToWrite:要写入的字节数(最大65535)
  323. //CHECK OK
  324. void sFlash_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
  325. {
  326. u16 pageremain;
  327. pageremain=256-WriteAddr%256; //单页剩余的字节数
  328. if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;//不大于256个字节
  329. while(1)
  330. {
  331. sFlash_Write_Page(pBuffer,WriteAddr,pageremain);
  332. if(NumByteToWrite==pageremain)break;//写入结束了
  333. else //NumByteToWrite>pageremain
  334. {
  335. pBuffer+=pageremain;
  336. WriteAddr+=pageremain;
  337. NumByteToWrite-=pageremain; //减去已经写入了的字节数
  338. if(NumByteToWrite>256)pageremain=256; //一次可以写入256个字节
  339. else pageremain=NumByteToWrite; //不够256个字节了
  340. }
  341. };
  342. }
  343. //写SPI FLASH
  344. //在指定地址开始写入指定长度的数据
  345. //该函数带擦除操作!
  346. //pBuffer:数据存储区
  347. //WriteAddr:开始写入的地址(24bit)
  348. //NumByteToWrite:要写入的字节数(最大65535)
  349. u8 sFlash_BUF[4096];
  350. void sFlash_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
  351. {
  352. u32 secpos;
  353. u16 secoff;
  354. u16 secremain;
  355. u16 i;
  356. secpos=WriteAddr/4096;//扇区地址 0~511 for w25x16
  357. secoff=WriteAddr%4096;//在扇区内的偏移
  358. secremain=4096-secoff;//扇区剩余空间大小
  359. if(NumByteToWrite<=secremain)secremain=NumByteToWrite;//不大于4096个字节
  360. while(1)
  361. {
  362. sFlash_Read(sFlash_BUF,secpos*4096,4096);//读出整个扇区的内容
  363. for(i=0;i<secremain;i++)//校验数据
  364. {
  365. if(sFlash_BUF[secoff+i]!=0XFF)break;//需要擦除
  366. }
  367. if(i<secremain)//需要擦除
  368. {
  369. sFlash_Erase_Sector(secpos);//擦除这个扇区
  370. for(i=0;i<secremain;i++) //复制
  371. {
  372. sFlash_BUF[i+secoff]=pBuffer[i];
  373. }
  374. sFlash_Write_NoCheck(sFlash_BUF,secpos*4096,4096);//写入整个扇区
  375. }else sFlash_Write_NoCheck(pBuffer,WriteAddr,secremain);//写已经擦除了的,直接写入扇区剩余区间.
  376. if(NumByteToWrite==secremain)break;//写入结束了
  377. else//写入未结束
  378. {
  379. secpos++;//扇区地址增1
  380. secoff=0;//偏移位置为0
  381. pBuffer+=secremain; //指针偏移
  382. WriteAddr+=secremain;//写地址偏移
  383. NumByteToWrite-=secremain; //字节数递减
  384. if(NumByteToWrite>4096)secremain=4096; //下一个扇区还是写不完
  385. else secremain=NumByteToWrite; //下一个扇区可以写完了
  386. }
  387. }
  388. }
  389. //擦除整个芯片
  390. //整片擦除时间:
  391. //W25X16:25s
  392. //W25X32:40s
  393. //W25X64:40s
  394. //等待时间超长...
  395. void sFlash_Erase_Chip(void)
  396. {
  397. sFlash_Write_Enable(); //SET WEL
  398. sFlash_Wait_Busy();
  399. sFLASH_CS_LOW(); //使能器件
  400. SPIx_ReadWriteByte(W25Q64_ChipErase); //发送片擦除命令
  401. sFLASH_CS_HIGH(); //取消片选
  402. sFlash_Wait_Busy(); //等待芯片擦除结束
  403. }
  404. //void sFlash_EraseChip(void)
  405. //{
  406. ///*!&lt; Send write enable instruction */
  407. //sFlash_Write_Enable();
  408. //sFlash_WaitForWriteEnd();
  409. ///*!&lt; Bulk Erase */
  410. ///*!&lt; Select the FLASH: Chip Select low */
  411. //W25X_FLASH_CS_LOW();
  412. ///*!&lt; Send Bulk Erase instruction */
  413. //sFlash_SendByte(W25X_CMD_ChipErase);
  414. ///*!&lt; Deselect the FLASH: Chip Select high */
  415. //W25X_FLASH_CS_HIGH();
  416. //
  417. ///*!&lt; Wait the end of Flash writing */
  418. //sFlash_WaitForWriteEnd();
  419. //}
  420. //擦除一个扇区
  421. //Dst_Addr:扇区地址 0~511 for w25x16
  422. //擦除一个山区的最少时间:150ms
  423. void sFlash_Erase_Sector(u32 Dst_Addr)
  424. {
  425. Dst_Addr*=4096;
  426. sFlash_Write_Enable(); //SET WEL
  427. sFlash_Wait_Busy();
  428. sFLASH_CS_LOW(); //使能器件
  429. SPIx_ReadWriteByte(W25Q64_SectorErase); //发送扇区擦除指令
  430. SPIx_ReadWriteByte((u8)((Dst_Addr)>>16)); //发送24bit地址
  431. SPIx_ReadWriteByte((u8)((Dst_Addr)>>8));
  432. SPIx_ReadWriteByte((u8)Dst_Addr);
  433. sFLASH_CS_HIGH(); //取消片选
  434. sFlash_Wait_Busy(); //等待擦除完成
  435. }
  436. //保护全芯片
  437. void sFlash_Global_Protect(void)
  438. {
  439. //sFlash_Wait_Busy();
  440. sFlash_Write_Enable(); //SET WEL
  441. sFlash_Wait_Busy();
  442. sFlash_Write_SR(0x3c);
  443. //sFlash_Wait_Busy();
  444. }
  445. //解保护全芯片
  446. void sFlash_Global_Unprotect(void)
  447. {
  448. //sFlash_Wait_Busy();
  449. sFlash_Write_Enable(); //SET WEL
  450. sFlash_Wait_Busy();
  451. sFlash_Write_SR(0x80);
  452. //sFlash_Wait_Busy();
  453. }
  454. //等待空闲
  455. void sFlash_Wait_Busy(void)
  456. {
  457. while ((sFlash_ReadSR()&0x01)==0x01){ // 等待BUSY位清空
  458. //IWDG_ReloadCounter();//喂狗
  459. }
  460. }
  461. //进入掉电模式
  462. void sFlash_PowerDown(void)
  463. {
  464. sFLASH_CS_LOW(); //使能器件
  465. SPIx_ReadWriteByte(W25Q64_PowerDown); //发送掉电命令
  466. sFLASH_CS_HIGH(); //取消片选
  467. DelayUs(3); //等待TPD
  468. }
  469. //唤醒
  470. void sFlash_WAKEUP(void)
  471. {
  472. sFLASH_CS_LOW(); //使能器件
  473. SPIx_ReadWriteByte(W25Q64_ReleasePowerDown); // send W25Q64_PowerDown command 0xAB
  474. sFLASH_CS_HIGH(); //取消片选
  475. DelayUs(3); //等待TRES1
  476. }
  477. /*********************************************
  478. *
  479. ***********************************************/
  480. void W25Q64Test(void)
  481. {
  482. uint32_t sFlashId;
  483. unsigned char sFlashSR;
  484. static int scStep=0;
  485. unsigned char t;
  486. char buf[200];
  487. int i;
  488. switch(scStep){
  489. case 0:
  490. sFlashId=sFlash_ReadID();
  491. printf("sFlashID=%04x\r\n",sFlashId);
  492. break;
  493. case 1:
  494. memset(buf,0,sizeof(buf));
  495. strcpy(buf,"333333333444444444455555555555\r\n");
  496. printf("sFlash Write=%s",buf);
  497. sFlash_Write((unsigned char *)buf,0,sizeof(buf));
  498. break;
  499. case 2:
  500. memset(buf,0,sizeof(buf));
  501. sFlash_Read((unsigned char *)buf,0,sizeof(buf));
  502. printf("sFlash Read=%s",buf);
  503. printf("sFlash write 8MB...\r\n");
  504. for(i=0;i<200;i++){
  505. buf[i]=i;
  506. }
  507. for(i=0;i<1000;i++){
  508. sFlash_Write_Page((unsigned char *)buf,i*4096,200);
  509. }
  510. printf("sFlash write 8MB OK!\r\n");
  511. break;
  512. case 3:
  513. break;
  514. }
  515. if(scStep<3)scStep++;
  516. }