制作STM32F429的外部SPI-FLASH下载算法

联系作者:

加我微信,备注“加群”,加入技术交流群
在这里插入图片描述
下载算法的模版在keil安装目录\ARM\Flash下的_Template文件夹,把此文件夹复制出来,进行修改。也可以自行修改此工程名。

打开此工程,选择自己的芯片类型:

添加W25QXX的驱动代码:
去正点原子的论坛下载STM32F29的寄存器工程,拷贝出HARDWARE文件夹下的SPI和W25QXX驱动,和SYSTEM文件夹。

添加W25QXX的驱动

修改FlashDev.c 中外部flash的大小:

#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
};

修改FlashPrg.c,按照模版添加驱动代码:

#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,表示成功							  
}

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

代码下载:

代码持续更新中:github代码下载地址https://gitee.com/Aladdin-Wang/hellotouchGFX.git
觉得有用的话,欢迎打个小星星

关注公众号,后续有精彩内容会第一时间发送给您!
在这里插入图片描述

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WALI-KANG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值