AT25DF .c 19 KB

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