提交 2a5f5b0e 编写于 作者: 马志远

add on-chip flash driver for mm32f327x

上级 1e261287
......@@ -18,6 +18,23 @@ menu "Hardware Drivers Config"
select RT_USING_SERIAL
default y
endmenu
menu "Flash Drivers"
config BSP_USING_OCFLASH
bool "Enable On Chip Flash"
default n
config OCFLASH_USE_FAL
bool "Enable On Chip Flash FAL Driver"
depends on BSP_USING_OCFLASH
select PKG_USING_FAL
default n
config OCFLASH_USE_LFS
bool "Enable On Chip Flash DFS Driver"
depends on OCFLASH_USE_FAL
select RT_USING_DFS
select RT_USING_MTD_NOR
select PKG_USING_LITTLEFS
default n
endmenu
endmenu
endmenu
......@@ -17,6 +17,9 @@ if GetDepend('BSP_USING_UART1') or GetDepend('BSP_USING_UART2'):
if GetDepend(['BSP_USING_GPIO']):
src += ['drv_gpio.c']
# add gpio driver code
if GetDepend(['BSP_USING_OCFLASH']):
src += ['drv_flash.c']
CPPPATH = [cwd]
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH)
......
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-08-05 mazhiyuan first version
*/
#include <rtthread.h>
#include "hal_flash.h"
#include "drv_flash.h"
#define OCFLASH_BLK_SIZE 1024
#define OCFLASH_LEN 1024*512
#define OCFLASH_ADDR 0x08000000
#ifdef OCFLASH_USE_FAL
#include <fal.h>
#endif
#ifdef OCFLASH_USE_LFS
#include <dfs_fs.h>
#define FS_PARTITION_NAME "filesystem"
#endif
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 = OCFLASH_ADDR + offset;
for (i = 0; i < size; i++)
{
*buf = *(__IO uint8_t *)addr;
buf++;
addr++;
}
return size;
}
static int write(long offset, const uint8_t *buf, size_t size)
{
size_t i;
uint32_t addr = OCFLASH_ADDR + offset;
FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xCDEF89AB;
FLASH->SR = 0x00000001 | 0x00000004 | 0x00000010;
FLASH->CR |= 0x1;
i = 0;
while (i < size)
{
*(__IO uint16_t *)addr = *buf | *(buf + 1) << 8;
addr = addr + 2;
buf += 2;
i += 2;
}
//Lock flash
FLASH->CR |= 0x00000080;
return size;
}
static int erase(long offset, size_t size)
{
int len;
RT_ASSERT(offset > 0 && offset < OCFLASH_LEN);
int page_addr = (offset >> 10) << 10;
len = size + (offset - page_addr);
while (len > 0)
{
FLASH_Unlock();
FLASH_ErasePage(page_addr);
FLASH_Lock();
len -= OCFLASH_BLK_SIZE;
page_addr += OCFLASH_BLK_SIZE;
}
return size;
}
#ifdef OCFLASH_USE_FAL
const struct fal_flash_dev mm32_onchip_flash =
{
.name = "mm32_onchip",
.addr = 0x08000000,
.len = 1024 * 512,
.blk_size = 1024,
.ops = {init, read, write, erase},
.write_gran = 2
};
#endif
int flash_init(void)
{
#ifdef OCFLASH_USE_FAL
fal_init();
#endif
#ifdef OCFLASH_USE_LFS
struct rt_device *flash_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
if (flash_dev == NULL)
{
rt_kprintf("Can't create a mtd device on '%s' partition.\n", FS_PARTITION_NAME);
}
else
{
rt_kprintf("Create a mtd device on the %s partition of flash successful.\n", FS_PARTITION_NAME);
}
if (rt_device_find(FS_PARTITION_NAME) != RT_NULL)
{
if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == RT_EOK)
{
rt_kprintf("onchip lfs filesystem mount to '/'\n");
}
else
{
dfs_mkfs("lfs", FS_PARTITION_NAME);
if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == RT_EOK)
{
rt_kprintf("onchip lfs filesystem mount to '/' with mkfs\n");
}
else
{
rt_kprintf("onchip lfs filesystem mount to '/' failed!\n");
}
}
}
else
{
rt_kprintf("find filesystem portion failed\r\n");
}
#endif
return 0;
}
INIT_APP_EXPORT(flash_init);
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-08-05 mazhiyuan first version
*/
#ifndef __DRV_FLASH_H__
#define __DRV_FLASH_H__
#include <rtdevice.h>
struct spi_flash_device
{
struct rt_device flash_device;
struct rt_device_blk_geometry geometry;
struct rt_spi_device *rt_spi_device;
struct rt_mutex lock;
void *user_data;
};
int flash_init(void);
#endif
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-08-05 mazhiyuan first version
*/
#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_
#include <rtconfig.h>
#include <board.h>
/* ===================== Flash device Configuration ========================= */
extern const struct fal_flash_dev mm32_onchip_flash;
extern struct fal_flash_dev nor_flash0;
/* flash device table */
#define FAL_FLASH_DEV_TABLE \
{ \
&mm32_onchip_flash, \
}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
/* partition table */
#define FAL_PART_TABLE \
{ \
{FAL_PART_MAGIC_WORD, "bl", "mm32_onchip", 0, 128*1024, 0}, \
{FAL_PART_MAGIC_WORD, "filesystem", "mm32_onchip", 128*1024, 255*1024, 0}, \
}
#endif /* FAL_PART_HAS_TABLE_CFG */
#endif /* _FAL_CFG_H_ */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册