summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernard Xiong <[email protected]>2021-08-09 21:48:26 +0800
committerGitHub <[email protected]>2021-08-09 21:48:26 +0800
commitce1734fb31bbe2992967297e263de3d8f78e12a9 (patch)
treed7be2945b7bd03c7ed086e524318be19e0b03431
parent1e26128735023209f32ddbb5f8e6bb75cdfeb1ca (diff)
parent2a5f5b0e88e93fb321474a319f72c6f27bfd022c (diff)
Merge pull request #4932 from cndabai/master
add on-chip flash driver for mm32f327x
-rw-r--r--bsp/mm32f327x/drivers/Kconfig17
-rw-r--r--bsp/mm32f327x/drivers/SConscript3
-rw-r--r--bsp/mm32f327x/drivers/drv_flash.c145
-rw-r--r--bsp/mm32f327x/drivers/drv_flash.h27
-rw-r--r--bsp/mm32f327x/drivers/fal_cfg.h37
5 files changed, 229 insertions, 0 deletions
diff --git a/bsp/mm32f327x/drivers/Kconfig b/bsp/mm32f327x/drivers/Kconfig
index ae70f1800e..bbffa008bd 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 e7e064caf1..27aeda3f91 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 0000000000..7ccc05ebbb
--- /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 <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);
diff --git a/bsp/mm32f327x/drivers/drv_flash.h b/bsp/mm32f327x/drivers/drv_flash.h
new file mode 100644
index 0000000000..0cdbf5d488
--- /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 <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
diff --git a/bsp/mm32f327x/drivers/fal_cfg.h b/bsp/mm32f327x/drivers/fal_cfg.h
new file mode 100644
index 0000000000..22e77992a7
--- /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 <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_ */