前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >制作STM32F429的SPI FLASH下载算法

制作STM32F429的SPI FLASH下载算法

作者头像
AIoT-KK
发布2020-07-13 16:59:58
1.7K1
发布2020-07-13 16:59:58
举报
  1. 下载算法的模版在keil安装目录\ARM\Flash下的_Template文件夹,把此文件夹复制出来,进行修改。也可以自行修改此工程名。
  1. 打开此工程,选择自己的芯片类型:
  1. 添加W25QXX的驱动代码: 去正点原子的论坛下载STM32F29的寄存器工程,拷贝出HARDWARE文件夹下的SPI和W25QXX驱动,和SYSTEM文件夹。

添加W25QXX的驱动

4.修改FlashDev.c代码:

代码语言:javascript
复制
#include "FlashOS.H"        // FlashOS Structures


struct FlashDevice const FlashDevice  =  {
   FLASH_DRV_VERS,             // Driver Version, do not modify!
   "STM32F429_W25QXX",           // Device Name 
   EXTSPI,                     // Device Type
   0x90000000,                 // Device Start Address
   0x01000000,                 // Device Size in Bytes (16M)
   4096,                       // Programming Page Size
   0,                          // Reserved, must be 0
   0xFF,                       // Initial Content of Erased Memory
   1000,                        // Program Page Timeout 100 mSec
   3000,                       // Erase Sector Timeout 3000 mSec

// Specify Size and Address of Sectors
   0x001000, 0x000000,         // Sector Size  4kB (8 Sectors)
   SECTOR_END
};

5.修改FlashPrg.c代码:

代码语言:javascript
复制
#include "FlashOS.H"        // FlashOS Structures
#include "sys.h"
#include "delay.h"
#include "w25qxx.h"
#include "spi.h"
#include "usart.h" 

#define PAGE_SIZE            4096
/* 
   Mandatory Flash Programming Functions (Called by FlashOS):
                int Init        (unsigned long adr,   // Initialize Flash
                                 unsigned long clk,
                                 unsigned long fnc);
                int UnInit      (unsigned long fnc);  // De-initialize Flash
                int EraseSector (unsigned long adr);  // Erase Sector Function
                int ProgramPage (unsigned long adr,   // Program Page Function
                                 unsigned long sz,
                                 unsigned char *buf);

   Optional  Flash Programming Functions (Called by FlashOS):
                int BlankCheck  (unsigned long adr,   // Blank Check
                                 unsigned long sz,
                                 unsigned char pat);
                int EraseChip   (void);               // Erase complete Device
      unsigned long Verify      (unsigned long adr,   // Verify Function
                                 unsigned long sz,
                                 unsigned char *buf);

       - BlanckCheck  is necessary if Flash space is not mapped into CPU memory space
       - Verify       is necessary if Flash space is not mapped into CPU memory space
       - if EraseChip is not provided than EraseSector for all sectors is called
*/


/*
 *  Initialize Flash Programming Functions
 *    Parameter:      adr:  Device Base Address
 *                    clk:  Clock Frequency (Hz)
 *                    fnc:  Function Code (1 - Erase, 2 - Program, 3 - Verify)
 *    Return Value:   0 - OK,  1 - Failed
 */
uint8_t aux_buf[PAGE_SIZE];
uint32_t base_adr;
int Init (unsigned long adr, unsigned long clk, unsigned long fnc) {

  /* Add your Code */
    Stm32_Clock_Init(360,25,2,8);//设置时钟,180Mhz
    delay_init(180);            //初始化延时函数 
    //uart_init(90,115200);     //初始化串口波特率为115200
    W25QXX_Init();              //W25QXX初始化
    return (0);                                  // Finished without Errors
}


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

int UnInit (unsigned long fnc) {

  /* Add your Code */
  return (0);                                  // Finished without Errors
}


/*
 *  Erase complete Flash Memory
 *    Return Value:   0 - OK,  1 - Failed
 */

int EraseChip (void) {

  /* Add your Code */
    W25QXX_Erase_Chip();
  return (0);                                  // Finished without Errors
}


/*
 *  Erase Sector in Flash Memory
 *    Parameter:      adr:  Sector Address
 *    Return Value:   0 - OK,  1 - Failed
 */

int EraseSector (unsigned long adr) {

  /* Add your Code */
    W25QXX_Erase_Sector((adr-base_adr)/4096);
  return (0);                                  // Finished without Errors
}


/*
 *  Program Page in Flash Memory
 *    Parameter:      adr:  Page Start Address
 *                    sz:   Page Size
 *                    buf:  Page Data
 *    Return Value:   0 - OK,  1 - Failed
 */

int ProgramPage (unsigned long adr, unsigned long sz, unsigned char *buf) {

  /* Add your Code */
    W25QXX_Write_NoCheck(buf,adr-base_adr,sz);
  return (0);                                  // Finished without Errors
}
/*
 *  Blank Check Checks if Memory is Blank
 *    Parameter:      adr:  Block Start Address
 *                    sz:   Block Size (in bytes)
 *                    pat:  Block Pattern
 *    Return Value:   0 - OK,  1 - Failed
 */

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

    return (1);                                        /* Always Force Erase */
}
unsigned long Verify      (unsigned long adr,   // Verify Function
                           unsigned long sz,
                           unsigned char *buf)
{
        return 0;//直接返回0,表示成功                             
}

6.编译后将xxx.FLM拷贝到…\Keil_v5\ARM\Flash目录

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AIoT开源项目分享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档