- 下载算法的模版在keil安装目录ARMFlash下的_Template文件夹,把此文件夹复制出来,进行修改。也可以自行修改此工程名。
- 打开此工程,选择自己的芯片类型:
- 添加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_v5ARMFlash目录