Skip to content

Commit ce1734f

Browse files
authored
Merge pull request #4932 from cndabai/master
add on-chip flash driver for mm32f327x
2 parents 1e26128 + 2a5f5b0 commit ce1734f

File tree

5 files changed

+229
-0
lines changed

5 files changed

+229
-0
lines changed

bsp/mm32f327x/drivers/Kconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ menu "Hardware Drivers Config"
1818
select RT_USING_SERIAL
1919
default y
2020
endmenu
21+
menu "Flash Drivers"
22+
config BSP_USING_OCFLASH
23+
bool "Enable On Chip Flash"
24+
default n
25+
config OCFLASH_USE_FAL
26+
bool "Enable On Chip Flash FAL Driver"
27+
depends on BSP_USING_OCFLASH
28+
select PKG_USING_FAL
29+
default n
30+
config OCFLASH_USE_LFS
31+
bool "Enable On Chip Flash DFS Driver"
32+
depends on OCFLASH_USE_FAL
33+
select RT_USING_DFS
34+
select RT_USING_MTD_NOR
35+
select PKG_USING_LITTLEFS
36+
default n
37+
endmenu
2138
endmenu
2239

2340
endmenu

bsp/mm32f327x/drivers/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ if GetDepend('BSP_USING_UART1') or GetDepend('BSP_USING_UART2'):
1717
if GetDepend(['BSP_USING_GPIO']):
1818
src += ['drv_gpio.c']
1919

20+
# add gpio driver code
21+
if GetDepend(['BSP_USING_OCFLASH']):
22+
src += ['drv_flash.c']
2023
CPPPATH = [cwd]
2124

2225
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH)

bsp/mm32f327x/drivers/drv_flash.c

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright (c) 2006-2021, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2021-08-05 mazhiyuan first version
9+
*/
10+
11+
12+
#include <rtthread.h>
13+
#include "hal_flash.h"
14+
#include "drv_flash.h"
15+
16+
#define OCFLASH_BLK_SIZE 1024
17+
#define OCFLASH_LEN 1024*512
18+
#define OCFLASH_ADDR 0x08000000
19+
20+
#ifdef OCFLASH_USE_FAL
21+
#include <fal.h>
22+
#endif
23+
24+
#ifdef OCFLASH_USE_LFS
25+
#include <dfs_fs.h>
26+
#define FS_PARTITION_NAME "filesystem"
27+
#endif
28+
29+
static int init(void)
30+
{
31+
/* do nothing now */
32+
return 0;
33+
}
34+
35+
static int read(long offset, uint8_t *buf, size_t size)
36+
{
37+
size_t i;
38+
uint32_t addr = OCFLASH_ADDR + offset;
39+
for (i = 0; i < size; i++)
40+
{
41+
*buf = *(__IO uint8_t *)addr;
42+
buf++;
43+
addr++;
44+
}
45+
return size;
46+
}
47+
48+
static int write(long offset, const uint8_t *buf, size_t size)
49+
{
50+
size_t i;
51+
uint32_t addr = OCFLASH_ADDR + offset;
52+
53+
FLASH->KEYR = 0x45670123;
54+
FLASH->KEYR = 0xCDEF89AB;
55+
FLASH->SR = 0x00000001 | 0x00000004 | 0x00000010;
56+
FLASH->CR |= 0x1;
57+
58+
i = 0;
59+
while (i < size)
60+
{
61+
*(__IO uint16_t *)addr = *buf | *(buf + 1) << 8;
62+
addr = addr + 2;
63+
buf += 2;
64+
i += 2;
65+
}
66+
//Lock flash
67+
FLASH->CR |= 0x00000080;
68+
69+
return size;
70+
}
71+
72+
static int erase(long offset, size_t size)
73+
{
74+
int len;
75+
RT_ASSERT(offset > 0 && offset < OCFLASH_LEN);
76+
int page_addr = (offset >> 10) << 10;
77+
len = size + (offset - page_addr);
78+
while (len > 0)
79+
{
80+
FLASH_Unlock();
81+
FLASH_ErasePage(page_addr);
82+
FLASH_Lock();
83+
len -= OCFLASH_BLK_SIZE;
84+
page_addr += OCFLASH_BLK_SIZE;
85+
}
86+
87+
return size;
88+
}
89+
90+
#ifdef OCFLASH_USE_FAL
91+
const struct fal_flash_dev mm32_onchip_flash =
92+
{
93+
.name = "mm32_onchip",
94+
.addr = 0x08000000,
95+
.len = 1024 * 512,
96+
.blk_size = 1024,
97+
.ops = {init, read, write, erase},
98+
.write_gran = 2
99+
};
100+
#endif
101+
102+
int flash_init(void)
103+
{
104+
#ifdef OCFLASH_USE_FAL
105+
fal_init();
106+
#endif
107+
#ifdef OCFLASH_USE_LFS
108+
struct rt_device *flash_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
109+
110+
if (flash_dev == NULL)
111+
{
112+
rt_kprintf("Can't create a mtd device on '%s' partition.\n", FS_PARTITION_NAME);
113+
}
114+
else
115+
{
116+
rt_kprintf("Create a mtd device on the %s partition of flash successful.\n", FS_PARTITION_NAME);
117+
}
118+
119+
if (rt_device_find(FS_PARTITION_NAME) != RT_NULL)
120+
{
121+
if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == RT_EOK)
122+
{
123+
rt_kprintf("onchip lfs filesystem mount to '/'\n");
124+
}
125+
else
126+
{
127+
dfs_mkfs("lfs", FS_PARTITION_NAME);
128+
if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == RT_EOK)
129+
{
130+
rt_kprintf("onchip lfs filesystem mount to '/' with mkfs\n");
131+
}
132+
else
133+
{
134+
rt_kprintf("onchip lfs filesystem mount to '/' failed!\n");
135+
}
136+
}
137+
}
138+
else
139+
{
140+
rt_kprintf("find filesystem portion failed\r\n");
141+
}
142+
#endif
143+
return 0;
144+
}
145+
INIT_APP_EXPORT(flash_init);

bsp/mm32f327x/drivers/drv_flash.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2006-2021, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2021-08-05 mazhiyuan first version
9+
*/
10+
11+
#ifndef __DRV_FLASH_H__
12+
#define __DRV_FLASH_H__
13+
14+
#include <rtdevice.h>
15+
16+
struct spi_flash_device
17+
{
18+
struct rt_device flash_device;
19+
struct rt_device_blk_geometry geometry;
20+
struct rt_spi_device *rt_spi_device;
21+
struct rt_mutex lock;
22+
void *user_data;
23+
};
24+
25+
int flash_init(void);
26+
27+
#endif

bsp/mm32f327x/drivers/fal_cfg.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2006-2021, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2021-08-05 mazhiyuan first version
9+
*/
10+
11+
#ifndef _FAL_CFG_H_
12+
#define _FAL_CFG_H_
13+
14+
#include <rtconfig.h>
15+
#include <board.h>
16+
17+
18+
/* ===================== Flash device Configuration ========================= */
19+
extern const struct fal_flash_dev mm32_onchip_flash;
20+
extern struct fal_flash_dev nor_flash0;
21+
22+
/* flash device table */
23+
#define FAL_FLASH_DEV_TABLE \
24+
{ \
25+
&mm32_onchip_flash, \
26+
}
27+
/* ====================== Partition Configuration ========================== */
28+
#ifdef FAL_PART_HAS_TABLE_CFG
29+
/* partition table */
30+
#define FAL_PART_TABLE \
31+
{ \
32+
{FAL_PART_MAGIC_WORD, "bl", "mm32_onchip", 0, 128*1024, 0}, \
33+
{FAL_PART_MAGIC_WORD, "filesystem", "mm32_onchip", 128*1024, 255*1024, 0}, \
34+
}
35+
#endif /* FAL_PART_HAS_TABLE_CFG */
36+
37+
#endif /* _FAL_CFG_H_ */

0 commit comments

Comments
 (0)