diff --git a/bsp/mm32f327x/drivers/Kconfig b/bsp/mm32f327x/drivers/Kconfig index ae70f1800ebf1d06feca2dc40b866164edc461c6..bbffa008bd998b08eb119c73ca7c7f0671cf2990 100644 --- a/bsp/mm32f327x/drivers/Kconfig +++ b/bsp/mm32f327x/drivers/Kconfig @@ -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 diff --git a/bsp/mm32f327x/drivers/SConscript b/bsp/mm32f327x/drivers/SConscript index e7e064caf1826a982972246200c5f274652bd0ae..27aeda3f91a4dc800e01ca81e2f1b5908d46d4a5 100644 --- a/bsp/mm32f327x/drivers/SConscript +++ b/bsp/mm32f327x/drivers/SConscript @@ -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) diff --git a/bsp/mm32f327x/drivers/drv_flash.c b/bsp/mm32f327x/drivers/drv_flash.c new file mode 100644 index 0000000000000000000000000000000000000000..7ccc05ebbb88ae345429e752096dc6d5c3a82b0e --- /dev/null +++ b/bsp/mm32f327x/drivers/drv_flash.c @@ -0,0 +1,145 @@ +/* + * 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 +#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 +#endif + +#ifdef OCFLASH_USE_LFS + #include + #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); diff --git a/bsp/mm32f327x/drivers/drv_flash.h b/bsp/mm32f327x/drivers/drv_flash.h new file mode 100644 index 0000000000000000000000000000000000000000..0cdbf5d48810953398763945dd6be7c86fc0a68a --- /dev/null +++ b/bsp/mm32f327x/drivers/drv_flash.h @@ -0,0 +1,27 @@ +/* + * 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 + +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 diff --git a/bsp/mm32f327x/drivers/fal_cfg.h b/bsp/mm32f327x/drivers/fal_cfg.h new file mode 100644 index 0000000000000000000000000000000000000000..22e77992a75c86ebfdce414327b79579942f635e --- /dev/null +++ b/bsp/mm32f327x/drivers/fal_cfg.h @@ -0,0 +1,37 @@ +/* + * 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 +#include + + +/* ===================== 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_ */