FAL

关于FAL移植

发布于 2019-01-15 10:32:40
git提供的fal例程是STM32F2标准库的,移植到我的F4开发板上,read,write都正常,但是erase 会崩溃,只能重新下载程序,不知道是代码问题还是硬件问题,求大神相助:(


/*
* File : fal_flash_stm32f2_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 ADDR_FLASH_SECTOR_0 ((u32)0x08000000) //ÉÈÇø0ÆðʼµØÖ·, 16 Kbytes
#define ADDR_FLASH_SECTOR_1 ((u32)0x08004000) //ÉÈÇø1ÆðʼµØÖ·, 16 Kbytes
#define ADDR_FLASH_SECTOR_2 ((u32)0x08008000) //ÉÈÇø2ÆðʼµØÖ·, 16 Kbytes
#define ADDR_FLASH_SECTOR_3 ((u32)0x0800C000) //ÉÈÇø3ÆðʼµØÖ·, 16 Kbytes
#define ADDR_FLASH_SECTOR_4 ((u32)0x08010000) //ÉÈÇø4ÆðʼµØÖ·, 64 Kbytes
#define ADDR_FLASH_SECTOR_5 ((u32)0x08020000) //ÉÈÇø5ÆðʼµØÖ·, 128 Kbytes
#define ADDR_FLASH_SECTOR_6 ((u32)0x08040000) //ÉÈÇø6ÆðʼµØÖ·, 128 Kbytes
#define ADDR_FLASH_SECTOR_7 ((u32)0x08060000) //ÉÈÇø7ÆðʼµØÖ·, 128 Kbytes
#define ADDR_FLASH_SECTOR_8 ((u32)0x08080000) //ÉÈÇø8ÆðʼµØÖ·, 128 Kbytes
#define ADDR_FLASH_SECTOR_9 ((u32)0x080A0000) //ÉÈÇø9ÆðʼµØÖ·, 128 Kbytes
#define ADDR_FLASH_SECTOR_10 ((u32)0x080C0000) //ÉÈÇø10ÆðʼµØÖ·,128 Kbytes
#define ADDR_FLASH_SECTOR_11 ((u32)0x080E0000) //ÉÈÇø11ÆðʼµØÖ·,128 Kbytes

/**
* 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 address)
{
if(address else if(address else if(address else if(address else if(address else if(address else if(address else if(address else if(address else if(address else if(address return FLASH_Sector_11;


}

/**
* Get the sector size
*
* @param sector sector
*
* @return sector size
*/
static uint32_t stm32_get_sector_size(uint32_t sector) {
assert(IS_FLASH_SECTOR(sector));

switch (sector) {
case FLASH_Sector_0: return 16 * 1024;
case FLASH_Sector_1: return 16 * 1024;
case FLASH_Sector_2: return 16 * 1024;
case FLASH_Sector_3: return 16 * 1024;
case FLASH_Sector_4: return 64 * 1024;
case FLASH_Sector_5: return 128 * 1024;
case FLASH_Sector_6: return 128 * 1024;
case FLASH_Sector_7: return 128 * 1024;
case FLASH_Sector_8: return 128 * 1024;
case FLASH_Sector_9: return 128 * 1024;
case FLASH_Sector_10: return 128 * 1024;
case FLASH_Sector_11: return 128 * 1024;
default : return 128 * 1024;
}
}
static int init(void)
{
/* do nothing now */
}

static int read(long offset, uint8_t *buf, size_t size)
{
size_t i;
uint32_t addr = stm32f4_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 = stm32f4_onchip_flash.addr + offset;

FLASH_Unlock();
FLASH_ClearFlag(
FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR
| FLASH_FLAG_PGSERR);
for (i = 0; i < size; i++, buf++, addr++)
{
/* write data */
FLASH_ProgramByte(addr, *buf);
read_data = *(uint8_t *) addr;
/* check data */
if (read_data != *buf)
{
return -1;
}
}
FLASH_Lock();

return size;
}

static int erase(long offset, size_t size)
{
FLASH_Status flash_status;
size_t erased_size = 0;
uint32_t cur_erase_sector;
uint32_t addr = stm32f4_onchip_flash.addr + offset;

/* start erase */
FLASH_Unlock();
FLASH_ClearFlag(
FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR
| FLASH_FLAG_PGSERR);
/* it will stop when erased size is greater than setting size */
while (erased_size < size)
{
cur_erase_sector = stm32_get_sector(addr + erased_size);
//cur_erase_sector=FLASH_Sector_8;
flash_status = FLASH_EraseSector(cur_erase_sector, VoltageRange_3);
if (flash_status != FLASH_COMPLETE)
{
return -1;
}
erased_size += stm32_get_sector_size(cur_erase_sector);
}
FLASH_Lock();

return size;
}

//const struct fal_flash_dev stm32f2_onchip_flash = { "stm32_onchip", 0x08000000, 1024*1024, 128*1024, {init, read, write, erase} };


const struct fal_flash_dev stm32f4_onchip_flash = { "onchip_flash", 0x08000000, (1024 * 1024), 128*1024, {NULL, read, write, erase} };
//#endif /* FAL_FLASH_PORT_DRIVER_STM32F2 */


查看更多

关注者
0
被浏览
1.5k
2 个回答
armink
armink 2019-01-15
直接用新重构的 STM32 BSP 嘛,里面有现成的 f4 flash 驱动,并且对接好的 fal

JASON1101
JASON1101 2019-01-15
armink 发表于 2019-1-15 10:35
直接用新重构的 STM32 BSP 嘛,里面有现成的 f4 flash 驱动,并且对接好的 fal

https://github.com/RT-Thr ...


好的,谢谢:handshake

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友