里飞网

 找回密码
 立即注册
查看: 6007|回复: 0
打印 上一主题 下一主题

MDK下载算法-W25Q256

[复制链接]

45

主题

291

帖子

2508

积分

版主

Rank: 7Rank: 7Rank: 7

积分
2508
跳转到指定楼层
楼主
发表于 2019-12-25 17:48:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 阿里兄 于 2019-12-25 17:50 编辑

MDK下载算法-W25Q256
参考了正点原子
说在前面:
1、下载算法就是把固件通过用户提供的读、写、擦除函数写到指定的地址处。
2、QSPI的映射地址是0x90000000,而W25Q256的地址是从0开始,而下载算法提供的地址是用户工程设置的IROM地址如0x90000000,读、写、擦除函数都要减去此偏移地址。
3、下载完成后需复位W25Q256芯片,在UnInit加入复位函数,如不加,在下载完成后读到全是0x88,也不能在线仿真,需要断电才能正确使用,这个问题坑了我两天。
4、如果下载出现 insufficient RAM for Flash Algorithms.则加大设置的烧写下载设置里面 :   Project->Options->Utilities->...Settings->“RAM for Algorithm ” size = 0x8000 <--加大这个。
5、如下载后验证不通过,则取消校验,Project->Options->Utilities->...Settings,取消Verify前面的勾。
6、下载完成后,只是把固件下载到了W25Q256里面,而不能马上运行,这是因为STM32上电或复位后还是跑到0x8000000地址处执行。
7、想在W25Q256上运行程序,需增加Bootloader,即在内部FLASH增加程序跳转,跳转前需把QSPI设置为内存映射模式。

  1. struct FlashDevice const FlashDevice  =  {
  2.    FLASH_DRV_VERS,             // Driver Version, do not modify!
  3.    "STM32F750_W25Q256_lfly",                           // Device Name
  4.    EXTSPI,                     // Device Type
  5.    0x90000000,                 // Device Start Address
  6.    0x02000000,                 // Device Size (32MB)
  7.    4096,                       // Programming Page Size
  8.    0,                          // Reserved, must be 0
  9.    0xFF,                       // Initial Content of Erased Memory
  10.    1000,                       // Program Page Timeout 300 mSec
  11.    3000,                       // Erase Sector Timeout 3000 mSec

  12. // Specify Size and Address of Sectors
  13.    0x001000, 0x000000,         // Sector Size 4kB (8192 Sectors)
  14.    SECTOR_END
  15. };
复制代码
  1. /* -----------------------------------------------------------------------------
  2. * Copyright (c) 2016 ARM Ltd.
  3. *
  4. * This software is provided 'as-is', without any express or implied warranty.
  5. * In no event will the authors be held liable for any damages arising from
  6. * the use of this software. Permission is granted to anyone to use this
  7. * software for any purpose, including commercial applications, and to alter
  8. * it and redistribute it freely, subject to the following restrictions:
  9. *
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. *    claim that you wrote the original software. If you use this software in
  12. *    a product, an acknowledgment in the product documentation would be
  13. *    appreciated but is not required.
  14. *
  15. * 2. Altered source versions must be plainly marked as such, and must not be
  16. *    misrepresented as being the original software.
  17. *
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. *
  20. *
  21. * $Date:        10. October 2018
  22. * $Revision:    V1.0.0
  23. *
  24. * Project:      Flash Device Description for
  25. *               STM32H743 W25Q256 SPIFI Flash
  26. * --------------------------------------------------------------------------- */

  27. #include "FlashOS.H"        // FlashOS Structures
  28. #include "sys.h"
  29. #include "delay.h"
  30. #include "w25qxx.h"
  31. #include "qspi.h"
  32. #include "mpu.h"


  33. #define PAGE_SIZE            4096

  34. /*
  35. *  Initialize Flash Programming Functions
  36. *    Parameter:      adr:  Device Base Address
  37. *                    clk:  Clock Frequency (Hz)
  38. *                    fnc:  Function Code (1 - Erase, 2 - Program, 3 - Verify)
  39. *    Return Value:   0 - OK,  1 - Failed
  40. */

  41. uint8_t aux_buf[PAGE_SIZE];
  42. uint32_t base_adr;

  43. //这个函数在keil下载后,在擦除、编程都会重新调用一次此函数,而且在调用此函数之前keil会复位单片机,相当于单片机跑起来的初始化功能要在擦除、编程时都要初始化一遍
  44. int Init (unsigned long adr, unsigned long clk, unsigned long fnc) {

  45.   
  46.         //MPU_Memory_Protection();        //保护相关存储区域
  47.         
  48.         Stm32_Clock_Init(432,25,2,9);//设置时钟,216Mhz
  49.         delay_init(216);                        //延时初始化        
  50.         W25QXX_Init();                            //初始化W25QXX
  51.         base_adr = adr;
  52.         return (0);
  53. }


  54. /*
  55. *  De-Initialize Flash Programming Functions
  56. *    Parameter:      fnc:  Function Code (1 - Erase, 2 - Program, 3 - Verify)
  57. *    Return Value:   0 - OK,  1 - Failed
  58. */

  59. int UnInit (unsigned long fnc) {
  60.         W25QXX_Reset();
  61.         return (0);
  62. }


  63. /*
  64. *  Erase complete Flash Memory
  65. *    Return Value:   0 - OK,  1 - Failed
  66. */

  67. int EraseChip (void) {
  68.         W25QXX_Erase_Chip();
  69.         return (0);                                        /* Finished without Errors */
  70. }


  71. /*
  72. *  Erase Sector in Flash Memory
  73. *    Parameter:      adr:  Sector Address
  74. *    Return Value:   0 - OK,  1 - Failed
  75. */

  76. int EraseSector (unsigned long adr) {
  77.         W25QXX_Erase_Sector((adr-base_adr)/4096);
  78.         return (0);                                        /* Finished without Errors */
  79. }


  80. /*
  81. *  Blank Check Checks if Memory is Blank
  82. *    Parameter:      adr:  Block Start Address
  83. *                    sz:   Block Size (in bytes)
  84. *                    pat:  Block Pattern
  85. *    Return Value:   0 - OK,  1 - Failed
  86. */

  87. int BlankCheck (unsigned long adr, unsigned long sz, unsigned char pat) {

  88.         return (1);                                        /* Always Force Erase */
  89. }


  90. /*
  91. *  Program Page in Flash Memory
  92. *    Parameter:      adr:  Page Start Address
  93. *                    sz:   Page Size
  94. *                    buf:  Page Data
  95. *    Return Value:   0 - OK,  1 - Failed
  96. */

  97. int ProgramPage (unsigned long adr, unsigned long sz, unsigned char *buf) {
  98.         W25QXX_Write_NoCheck(buf,adr-base_adr,sz);
  99.         return (0);                                        /* Finished without Errors */
  100. }


  101. /*  
  102. *  Verify Flash Contents
  103. *    Parameter:      adr:  Start Address
  104. *                    sz:   Size (in bytes)
  105. *                    buf:  Data
  106. *    Return Value:   (adr+sz) - OK, Failed Address
  107. */

  108. /*
  109.    Verify function is obsolete because all other function leave
  110.     the SPIFI in memory mode so a memory compare could be used.
  111. */
  112. unsigned long Verify (unsigned long adr, unsigned long sz, unsigned char *buf) {

  113.         int i;
  114.         W25QXX_Read(aux_buf, adr-base_adr, sz);
  115.         for (i = 0; i< PAGE_SIZE; i++)
  116.         {
  117.                 if (aux_buf[i] != buf[i])
  118.                 return (adr+i);                   // Verification Failed (return address)
  119.         }
  120.         return (adr+sz);                      // Done successfully
  121. }
复制代码

回复

使用道具 举报

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

本版积分规则

QQ|Archiver|手机版|小黑屋|里飞网  

GMT+8, 2024-5-3 16:52 , Processed in 0.068001 second(s), 4 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

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