MDK下针对STM32H750的下载算法制作

查看keil帮助手册

安装keil后,可以在帮助中查看手册,也可以在浏览器中输入:

https://arm-software.github.io/CMSIS_5/Pack/html/flashAlgorithm.html

如果网页打开失败,可以试试在C:\Windows\System32\drivers\etc\hosts中添加185.199.109.153 arm-software.github.io然后保存退出,最后在cmd命令窗口执行ipconfig /flushdns,然后再打开
打开界面如下:
keil帮助
我们根据这个帮助来进行下载算法的工程创建,并且安装帮助文档的提示,我们需要实现 Init,UnInit,EraseSector和ProgramPage,EraseChip这几个函数,在实际操作过程中发现,还必须实现BlankCheck和Verify这两个函数,否则下载程序时,会提示超时而下载失败

Keil工程搭建

工程修改这里就不说了,玩过STM32的人,没几个不熟悉keil这个软件,按照帮助文档复制一个过来就好了,魔术棒下的设置都是设置好了的,不用修改。

FlashDev中调整设备参数

我开发板用的是一颗W25Q64
设置的参数如下:
算法参数
参数后面的英文注释很好理解,但是要说一说 Device Name这个参数,这个名字跟最后显示keil里面的算法名字有关,而跟keil工程输出文件名称无关
设备名

FlashPrg中的编程算法

/**************************************************************************//**
 * @file     FlashPrg.c
 * @brief    Flash Programming Functions adapted for New Device Flash
 * @version  V1.0.0
 * @date     10. January 2018
 ******************************************************************************/
/*
 * Copyright (c) 2010-2018 Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include "FlashOS.h"        // FlashOS Structures
#include "sys.h"
#include "delay.h"
#include "led.h"
#include "usart.h"
#include "norflash.h"
/* 
   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
 */
u32 BASEaddr = 0x00;		//全局基地址,MDK中可以手动设置起始地址
int Init (unsigned long adr, unsigned long clk, unsigned long fnc) {

  /* Add your Code */
	Stm32_Clock_Init(160,5,2,4);	//设置时钟,400Mhz
	LED_Init();						//初始化与LED连接的硬件接口
	NORFLASH_Init();				//W25QXX初始化
	BASEaddr= adr;		//获取地址偏移

  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 */
	NORFLASH_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 */
	NORFLASH_Erase_Sector((adr-BASEaddr)/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) {
	u8 check_buf =0;
  /* Add your Code */
	LED0(adr/4096);		//页编程的大小
	LED1(adr/4096);
	NORFLASH_Write(buf,adr-BASEaddr,sz);
  return (0);                                  // Finished without Errors
}


/*外部Flash必须实现以下两个函数,否则MDK会因无操作超时*/

int BlankCheck (unsigned long adr,unsigned long sz,unsigned char pat)
{
	
	return (0);		
}								 
	
//下载后校验下载的数据是否与编译成的文件一致
unsigned long Verify(unsigned long adr,unsigned long sz,unsigned char *buf){
	u8 check_buf[4096];
	int i;
	NORFLASH_Read(check_buf,adr-BASEaddr,sz);
	for(i=0;i<sz;i++)
	{

		if(buf[i] != check_buf[i])
		{
			break;
		}
	}

	return (adr + i);

}
下载后校验

QSPI和W25Q64的程序,直接借鉴了正点原子的代码

RAM for Algorithm 参数

在算法制作完成后,如果直接进行下载可是,几乎都会遇到如下提示:
错误
很大原因是 RAM for Algorithm 这个参数设置错误导致 这个参数keil一般默认是0x1000
RAM for Algorithm
这参数在帮助文档里面说是编程算法所占用的RAM大小
在这里插入图片描述
但是没有找到说如何去设置才算合适,我测试后发现一般将其设置为代码容量的2倍以上合适,要给算法程序运行留下足够的空间,但是不要超过区域所在的大小,这属于个人意见,有其他方法欢迎讨论。
在这里插入图片描述
最后可以用一个简单的工程进行下载验证

  • 3
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MDK程序下载 程序下载----FLM⽂件 ⽂件 MDK编程算法 编程算法   ⽤过MDK下载程序的⼩伙伴可能都知道,在下载程序之前需要都在Debug设置的Flash Download⼦选项卡选择编程算法。⼤多数时 候, 我们只要安装了芯⽚包之后,就可以直接得到对应的编程算法,并不需要我们去修改它。但是,当你是⼀个芯⽚包的开发者,或者你有独特 的下载需求(⽐如在你的程序⾥加⼊⼀些校验信息),这个时候你就需要去了解它了!   编程算法呢,说⽩就其实也就是⼀段程序,主要功能就是擦除相应的内存块,并将我们的程序写⼊到相应的内存区域上去。 在你点击下载按钮的时候,这段程序会被先下载RAM上(RAM for Algorithm上的设置),然后才会通过它,将你的程序写⼊到 指定的内存区域内。 实现⼀个⾃⼰的编程算法 实现⼀个⾃⼰的编程算法   怎么去实现⼀个⾃⼰的编程算法?⾸先我们找到⾃⼰的MDK的安装路径,进⼊到ARM\Flash⽂件夹下(例如: D:\Keil_v5\ARM\Flash)。 这⾥有个编程算法的⼯程模板,复制这个⼯程到你的⼯程⽂件夹下,重命名你⾃⼰的想要的名字。   打开⼯程,⾥⾯主要有两个⽂件 FlashPrg.c 和 FlashDev.c:   FlashDev.c主要实现了⼀个设备相关的结构体(根据⾃⼰的Flash情况去实现)      ⽐如STM32L051实现如下:   FlashPrg.c实现了⼏个Flash编程相关的函数:   根据⾃⼰的需要去实现,STM32L051实现如下: 1 /* ----------------------------------------------------------------------------- 2 * Copyright (c) 2014 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: 18. November 2014 22 * $Revision: V1.00 23 * 24 * Project: Flash Programming Functions for ST STM32L0xx Flash 25 * --------------------------------------------------------------------------- */ 26 27 /* History: 28 * Version 1.00 29 * Initial release 30 */ 31 32 #include "FlashOS.H" // FlashOS Structures 33 34 typedef volat

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值