AT25DF .c 18 KB

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