W25Q64.c 17 KB

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