文档地址
进入网站选择升级的芯片配置好,下载bootloader
地址
填写分区配置
填写加密等配置
下载地址
进入如下目录:rt-thread/bsp选择合适的平台,我这里选择rt-thread/bsp/stm32/stm32f103-atk-warshipv3
进入目录,右键在这打开env
键入命令:menuconfig
回车
进入如下界面
SPACE选中ota
回车键进入二级菜单,选中如下
他会自动选择如下的FAL组件
选中如下,开启片内flash
通过左键右键,选择Save,回车键,回车键,即可保存更改。
键入scons --target=mdk5
+回车键,生成工程!
KEIL生成bin文件设置
fromelf --bin !L --output rtthread.bin
增加以下代码,设置中断向量表偏移
#define RT_APP_PART_ADDR 0x8040000/**< 目前APP*/
#define APP_VERSION "V1.0.0"
/**
* Function ota_app_vtor_reconfig
* Description Set Vector Table base location to the start addr of app(RT_APP_PART_ADDR).
*/
static int ota_app_vtor_reconfig(void)
{
#define NVIC_VTOR_MASK 0x3FFFFF80
/* Set the Vector Table base location by user application firmware definition */
SCB->VTOR = RT_APP_PART_ADDR & NVIC_VTOR_MASK;
return 0;
}
INIT_BOARD_EXPORT(ota_app_vtor_reconfig);
增加以下代码支持FAL,并初始化
#include "fal.h"//!<增加FAL组件
/*main.c中初始化FAL组件*/
fal_init();
/*打印版本号*/
rt_kprintf("The Current Vsersion is :%s\n",APP_VERSION);
此文件可有FAL源码中例程里找到,复制一份并修改即可
/*
* File : fal_cfg.h
* This file is part of FAL (Flash Abstraction Layer) package
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2018-05-17 armink the first version
*/
#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_
#include
#include
#define NOR_FLASH_DEV_NAME "norflash0"/**< 未使用 */
/* ===================== Flash device Configuration ========================= */
extern const struct fal_flash_dev stm32f1_onchip_flash;
//extern struct fal_flash_dev nor_flash0;/**< 未使用 */
/* flash device table */
#define FAL_FLASH_DEV_TABLE \
{ \
&stm32f1_onchip_flash, \
}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
/* partition table */
#define FAL_PART_TABLE \
{ \
{FAL_PART_MAGIC_WORD, "bl", "stm32_onchip", 0, 128*1024, 0}, \
{FAL_PART_MAGIC_WORD, "download", "stm32_onchip", 128*1024, 128*1024, 0}, \
{FAL_PART_MAGIC_WORD, "app", "stm32_onchip", 256*1024, 128*1024, 0}, \
{FAL_PART_MAGIC_WORD, "factory", "stm32_onchip", 384*1024, 128*1024, 0}, \
}
#endif /* FAL_PART_HAS_TABLE_CFG */
#endif /* _FAL_CFG_H_ */
/*
* File : fal_flash_stm32f1_port.c
* This file is part of FAL (Flash Abstraction Layer) package
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2018-01-26 armink the first version
*/
#include
#include
/* base address of the flash sectors */
#define FLASH_PAGE_0_ADDR ((uint32_t)0x08000000) /* 物理flash原始起始页地址 */
#define FLASH_START_PAGE_ADDR FLASH_PAGE_0_ADDR /* 分区表起始地址 */
#define CURRENT_PART_START_PAGE ((FLASH_START_PAGE_ADDR-FLASH_PAGE_0_ADDR)/FLASH_PAGE_SIZE) /* 当前分区实际物理起始页 */
#define FLASH_PAGE_NUM_MAX ((uint32_t)255+1)
/**
* Get the sector of a given address
*
* @param address flash address
*
* @return The sector of a given address
*/
static uint32_t stm32_get_sector(uint32_t Startaddress ,size_t size ,uint32_t *get_pages)
{
/*记录起始页与结束页*/
uint32_t Startpage = 0,Endpage = 0;
/*搜索完成标记*/
uint8_t start_page_flag = 0,end_page_flag = 0;
/*计算起始地址所在页*/
if(Startaddress >= stm32f1_onchip_flash.addr)
{
for(uint32_t i = 0; i < FLASH_PAGE_NUM_MAX;i++)
{
/*计算首地址所在页*/
if(start_page_flag == 0)
{
if((i*FLASH_PAGE_SIZE+FLASH_PAGE_0_ADDR+FLASH_PAGE_SIZE-1) >= Startaddress)
{
log_i("first erase page:%d",i);
Startpage = i;
start_page_flag = 1;
}
}
/*计算尾地址所在页*/
if(end_page_flag == 0)
{
if((i*FLASH_PAGE_SIZE+FLASH_PAGE_0_ADDR+FLASH_PAGE_SIZE-1) >= (Startaddress+size-1))
{
log_i("end erase page:%d",i);
Endpage = i;
end_page_flag = 1;
break;
}
}
}
}
else
{
/*地址非法*/
*get_pages = 0;
return Startpage;
}
/*计算地址范围内的页数*/
*get_pages = Endpage-Startpage+1;
return Startpage*FLASH_PAGE_SIZE+FLASH_PAGE_0_ADDR;
}
/**
* Get the sector size
*
* @param sector sector
*
* @return sector size
*/
static uint32_t stm32_get_sector_size(uint32_t pages)
{
return pages*FLASH_PAGE_SIZE; //!< 2K一页
}
static int init(void)
{
/* do nothing now */
return 0;
}
static int read(long offset, uint8_t *buf, size_t size)
{
size_t i;
uint32_t addr = stm32f1_onchip_flash.addr + offset;
for (i = 0; i < size; i++, addr++, buf++)
{
*buf = *(uint8_t *) addr;
}
return size;
}
static int write(long offset, const uint8_t *buf, size_t size)
{
size_t i;
uint32_t read_data;
uint32_t addr = stm32f1_onchip_flash.addr + offset;
HAL_FLASH_Unlock();
__HAL_FLASH_GET_FLAG(
FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR
| FLASH_FLAG_OPTVERR);
uint16_t data = 0;
for (i = 0; i < size; i++)
{
/* write data */
log_i("write addr:0x%04X",addr);
data = *(buf+i);
if((i+1) < size)
{
i++;
data |= ((uint16_t)*(buf+i)<<8);
}
else
{
data &= 0x00FF;
}
HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD ,addr, data);
read_data = *(uint16_t *) addr;
addr += 2;
/* check data */
if (read_data != data)
{
log_i("data unmatched read data:%d",read_data);
return -1;
}
}
HAL_FLASH_Lock();
return size;
}
static int erase(long offset, size_t size)
{
HAL_StatusTypeDef flash_status;
size_t erased_size = 0;
uint32_t addr = stm32f1_onchip_flash.addr + offset;
FLASH_EraseInitTypeDef erase_config = {0};
uint32_t erasepages = 0;
uint32_t error;
/* start erase */
HAL_FLASH_Unlock();
__HAL_FLASH_GET_FLAG(
FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR
| FLASH_FLAG_OPTVERR);
/* it will stop when erased size is greater than setting size */
/*擦除页地址*/
erase_config.PageAddress = stm32_get_sector(addr ,size ,&erasepages);
log_i("erase page addr:0x%08X,PAGES:%d",erase_config.PageAddress,erasepages);
/*所属BANK*/
erase_config.Banks = FLASH_BANK_1;
/*按页擦除*/
erase_config.TypeErase = FLASH_TYPEERASE_PAGES;
/*擦除页数量*/
erase_config.NbPages = erasepages;
log_i("erase page number:%d",erasepages);
flash_status = HAL_FLASHEx_Erase(&erase_config, &error);
if (flash_status != HAL_OK)
{
log_i("erase error");
return -1;
}
erased_size += stm32_get_sector_size(erasepages);
log_i("erase size:%d",erased_size);
HAL_FLASH_Lock();
return size;
}
const struct fal_flash_dev stm32f1_onchip_flash =
{
.name = "stm32_onchip",
.addr = FLASH_START_PAGE_ADDR,
.len = 512*1024,
.blk_size = FLASH_PAGE_SIZE,
.ops = {init, read, write, erase},
.write_gran = 32
};
在 msh 命令行中输入 ymodem_ota
命令后,点击鼠标右键
,然后在菜单栏找到用 YMODEM 发送
选项发送文件
bin
文件同样使用打包器打包成rpl
文件,因为打包器会对其压缩加密操作,否则无法使用rpl
文件更改后缀名为xx.bin
使用烧写器烧录进flash的factory
分区起始地址预期写入 1 2 3 4 5依次存放在8020008往后中
最后的结果可以看出,高地址 放 高位数据
地址8020009
放 数据1
为小端模式
fal erase 0 4096
fal write 8 1 2 3 4 5
fal read 0 64