配置
board/Kconfig
config BSP_USING_ON_CHIP_FLASHbool "Enable On Chip Flash"default n
cp rt-thread/components/fal/samples/porting/fal_cfg.h board/
fal_cfg.h
/** Copyright (c) 2006-2018, RT-Thread Development Team** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date Author Notes* 2018-05-17 armink the first version*/#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_#include <rtconfig.h>
#include <board.h>// 芯片型號:STM32407VET6 512Kb
// 起始地址
#define STM32_FLASH_START_ADRESS_16K ADDR_FLASH_SECTOR_0
#define STM32_FLASH_START_ADRESS_64K ADDR_FLASH_SECTOR_4
#define STM32_FLASH_START_ADRESS_128K ADDR_FLASH_SECTOR_5// 大小
#define FLASH_SIZE_GRANULARITY_16K (ADDR_FLASH_SECTOR_4 - ADDR_FLASH_SECTOR_0) // 4個16K, 總共64K
#define FLASH_SIZE_GRANULARITY_64K (ADDR_FLASH_SECTOR_5 - ADDR_FLASH_SECTOR_4) // 1個64K
#define FLASH_SIZE_GRANULARITY_128K (ADDR_FLASH_SECTOR_8 - ADDR_FLASH_SECTOR_5) // 3個128K, 總共384K/* ===================== Flash device Configuration ========================= */
extern const struct fal_flash_dev stm32_onchip_flash_16k;
extern const struct fal_flash_dev stm32_onchip_flash_64k;
extern const struct fal_flash_dev stm32_onchip_flash_128k;/* flash device table */
#define FAL_FLASH_DEV_TABLE \{ \&stm32_onchip_flash_16k, \&stm32_onchip_flash_64k, \&stm32_onchip_flash_128k, \}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
/* partition table */
#define FAL_PART_TABLE \{ \{FAL_PART_MAGIC_WORD, "bl_a", "onchip_flash_16k", 0, 64 * 1024, 0}, \{FAL_PART_MAGIC_WORD, "bl_b", "onchip_flash_64k", 0, 64 * 1024, 0}, \{FAL_PART_MAGIC_WORD, "app", "onchip_flash_128k", 0, 256 * 1024, 0}, \{FAL_PART_MAGIC_WORD, "backup", "onchip_flash_128k", 256 * 1024, 384 * 1024, 0}, \}
#endif /* FAL_PART_HAS_TABLE_CFG */#endif /* _FAL_CFG_H_ */
main.c
/** Copyright (c) 2006-2018, RT-Thread Development Team** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date Author Notes* 2018-11-06 SummerGift first version*/#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <fal.h>/* defined the LED0 pin: PB1 */
#define LED0_PIN GET_PIN(B, 1)int main(void)
{fal_init();int count = 1;/* set LED0 pin mode to output */rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);while (count++){rt_pin_write(LED0_PIN, PIN_HIGH);rt_thread_mdelay(500);rt_pin_write(LED0_PIN, PIN_LOW);rt_thread_mdelay(500);}return RT_EOK;
}
燒錄
scons -c
scons -j8
bash flash.bash
flash.bash
#!/bin/bash# Use first argument as ELF file, default to rtthread.elf
ELF_FILE="${1:-rt-thread.elf}"# OpenOCD configuration files
INTERFACE_CFG="/usr/local/share/openocd/scripts/interface/stlink-v2.cfg"
TARGET_CFG="/usr/local/share/openocd/scripts/target/stm32f4x.cfg"# Print info in English
echo "Flashing firmware using OpenOCD: $ELF_FILE"
echo "Interface: CMSIS-DAP"
echo "Target MCU: STM32F4xx"# Flash command
openocd \-f "$INTERFACE_CFG" \-f "$TARGET_CFG" \-c "init; program $ELF_FILE verify; reset run; exit"
\ | /
- RT - Thread Operating System/ | \ 5.1.0 build Jul 15 2025 22:48:222006 - 2024 Copyright by RT-Thread team
[D/FAL] (fal_flash_init:47) Flash device | onchip_flash_16k | addr: 0x08000000 | len: 0x00010000 | blk_size: 0x00004000 |initialized finish.
[D/FAL] (fal_flash_init:47) Flash device | onchip_flash_64k | addr: 0x08010000 | len: 0x00010000 | blk_size: 0x00010000 |initialized finish.
[D/FAL] (fal_flash_init:47) Flash device | onchip_flash_128k | addr: 0x08020000 | len: 0x00060000 | blk_size: 0x00020000 |initialized finish.
[I/FAL] ==================== FAL partition table ====================
[I/FAL] | name | flash_dev | offset | length |
[I/FAL] -------------------------------------------------------------
[I/FAL] | bl_a | onchip_flash_16k | 0x00000000 | 0x00010000 |
[I/FAL] | bl_b | onchip_flash_64k | 0x00000000 | 0x00010000 |
[I/FAL] | app | onchip_flash_128k | 0x00000000 | 0x00040000 |
[I/FAL] | backup | onchip_flash_128k | 0x00040000 | 0x00060000 |
[I/FAL] =============================================================
[I/FAL] RT-Thread Flash Abstraction Layer initialize success.