1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/*
* Copyright (c) 2022-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-01-22 chasel first version
*/
#include <rtthread.h>
#include "hal_flash.h"
#include "drv_flash.h"
#define OCFLASH_BLK_SIZE 1024
#define OCFLASH_LEN 1024*256
#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);
|