Skip to content

Commit 354a1c7

Browse files
wuxn9999YiluMao
authored andcommitted
IssueID:1901:LittleFS and KV switch to new flash API
[Detail] LittleFS and KV switch to new flash API. [Verified Cases] Build Pass: helloworld_demo@haaseduk1 Test Pass: helloworld_demo@haaseduk1
1 parent 6a02872 commit 354a1c7

File tree

2 files changed

+134
-286
lines changed

2 files changed

+134
-286
lines changed

components/kv/src/kv_adapt.c

Lines changed: 17 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,47 @@
11
/*
22
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
33
*/
4-
#include <fcntl.h>
5-
#include <sys/types.h>
64
#include <unistd.h>
75
#include <stdio.h>
8-
#include <sys/ioctl.h>
96
#include "k_api.h"
107
#include "kv_conf.h"
118
#include "kv_adapt.h"
12-
#include "aos/hal/flash.h"
13-
#include "vfsdev/flash_dev.h"
14-
#if (CONFIG_U_FLASH_CORE > 0)
15-
#include <aos/mtd.h>
16-
#endif
9+
#include <aos/flashpart.h>
10+
#include <aos/hal/flash.h>
1711

18-
static int32_t g_kv_flash_fd = -1;
12+
static aos_flashpart_ref_t kv_flashpart_ref = AOS_DEV_REF_INIT_VAL;
1913

20-
static int32_t kv_flash_fd_open(void)
14+
static aos_status_t kv_flash_open(void)
2115
{
22-
int fd;
23-
char dev_str[16] = {0};
24-
if(g_kv_flash_fd >= 0){
25-
return g_kv_flash_fd;
26-
}
27-
28-
#if (CONFIG_U_FLASH_CORE > 0)
29-
snprintf(dev_str,16-1,"/dev/mtdblock%d",KV_PARTITION);
30-
#else
31-
snprintf(dev_str,16-1,"/dev/flash%d",KV_PARTITION);
32-
#endif
33-
34-
fd = open(dev_str, 0);
35-
if(fd < 0){
36-
return fd;
37-
}
38-
g_kv_flash_fd = fd;
39-
40-
return g_kv_flash_fd;
16+
return aos_flashpart_get(&kv_flashpart_ref, KV_PARTITION);
4117
}
4218

4319
int32_t kv_flash_read(uint32_t offset, void *buf, uint32_t nbytes)
4420
{
45-
int32_t ret;
46-
int fd;
47-
if(kv_flash_fd_open() < 0){
48-
return -1;
49-
}
50-
fd = g_kv_flash_fd;
51-
ret = lseek(fd, offset, SEEK_SET);
52-
if(ret < 0){
53-
return ret;
54-
}
55-
ret = read(fd, buf, nbytes);
56-
if(ret > 0){
57-
return 0;
58-
}else{
21+
aos_status_t r;
22+
23+
if (!aos_dev_ref_is_valid(&kv_flashpart_ref) && kv_flash_open())
5924
return -1;
60-
}
25+
26+
r = aos_flashpart_read(&kv_flashpart_ref, offset, buf, nbytes);
27+
28+
return (r >= 0 && r < AOS_FLASH_ECC_ERROR) ? 0 : -1;
6129
}
6230

6331
int32_t kv_flash_write(uint32_t offset, void *buf, uint32_t nbytes)
6432
{
65-
int32_t ret;
66-
int fd;
67-
if(kv_flash_fd_open() < 0){
33+
if (!aos_dev_ref_is_valid(&kv_flashpart_ref) && kv_flash_open())
6834
return -1;
69-
}
70-
fd = g_kv_flash_fd;
71-
ret = lseek(fd, offset, SEEK_SET);
72-
if(ret < 0){
73-
return ret;
74-
}
75-
ret = write(fd, buf, nbytes);
76-
if(ret > 0){
77-
return 0;
78-
}else{
79-
return -1;
80-
}
35+
36+
return aos_flashpart_write(&kv_flashpart_ref, offset, buf, nbytes) ? -1 : 0;
8137
}
8238

8339
int32_t kv_flash_erase(uint32_t offset, uint32_t size)
8440
{
85-
int32_t ret;
86-
int fd;
87-
#if (CONFIG_U_FLASH_CORE > 0)
88-
struct mtd_erase_info erase_arg;
89-
#endif
90-
91-
if(kv_flash_fd_open() < 0){
41+
if (!aos_dev_ref_is_valid(&kv_flashpart_ref) && kv_flash_open())
9242
return -1;
93-
}
94-
fd = g_kv_flash_fd;
95-
ret = lseek(fd, offset, SEEK_SET);
96-
if(ret < 0){
97-
return ret;
98-
}
99-
100-
#if (CONFIG_U_FLASH_CORE > 0)
101-
erase_arg.offset = offset;
102-
erase_arg.length = size;
103-
ret = ioctl(fd, IOC_MTD_ERASE, &erase_arg);
104-
#else
105-
ret = ioctl(fd, IOC_FLASH_ERASE_FLASH, size);
106-
#endif
10743

108-
return ret;
44+
return aos_flashpart_erase(&kv_flashpart_ref, offset, size) ? -1 : 0;
10945
}
11046

11147
void *kv_lock_create(void)

0 commit comments

Comments
 (0)