8000 zephyr: Implement block device protocol via zephyr disk access api. · jeremyherbert/micropython@cc19cf2 · GitHub
[go: up one dir, main page]

Skip to content

Commit cc19cf2

Browse files
MaureenHelmdpgeorge
authored andcommitted
zephyr: Implement block device protocol via zephyr disk access api.
Introduces a new zephyr.DiskAccess class that uses the zephyr disk access api to implement the uos.AbstractBlockDev protocol. This can be used with any type of zephyr disk access driver, which currently includes SDHC, RAM, and FLASH implementations. The SDHC driver is enabled on the mimxrt1050_evk board. Only the standard block device protocol (without the offset parameter) can be supported with the zephyr disk access api, therefore this class cannot be used with file systems like littlefs which require the extended interface. In the future it may be possible to implement the extended interface in a new class using the zephyr flash api.
1 parent dbed8f5 commit cc19cf2

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed

ports/zephyr/Makefile

Lines changed: 1 addition & 0 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ SRC_C = main.c \
4646
machine_i2c.c \
4747
machine_pin.c \
4848
uart_core.c \
49+
zephyr_storage.c \
4950
lib/utils/stdout_helpers.c \
5051
lib/utils/printf.c \
5152
lib/utils/pyexec.c \

ports/zephyr/modzephyr.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* The MIT License (MIT)
55
*
66
* Copyright (c) 2017 Linaro Limited
7+
* Copyright (c) 2019 NXP
78
*
89
* Permission is hereby granted, free of charge, to any person obtaining a copy
910
* of this software and associated documentation files (the "Software"), to deal
@@ -31,6 +32,7 @@
3132
#include <zephyr.h>
3233
#include <debug/stack.h>
3334

35+
#include "modzephyr.h"
3436
#include "py/runtime.h"
3537

3638
STATIC mp_obj_t mod_is_preempt_thread(void) {
@@ -90,6 +92,9 @@ STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = {
9092
#ifdef CONFIG_NET_SHELL
9193
{ MP_ROM_QSTR(MP_QSTR_shell_net_iface), MP_ROM_PTR(&mod_shell_net_iface_obj) },
9294
#endif
95+
#ifdef CONFIG_DISK_ACCESS
96+
{ MP_ROM_QSTR(MP_QSTR_DiskAccess), MP_ROM_PTR(&zephyr_disk_access_type) },
97+
#endif
9398
};
9499

95100
STATIC MP_DEFINE_CONST_DICT(mp_module_time_globals, mp_module_time_globals_table);

ports/zephyr/modzephyr.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 NXP
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ZEPHYR_MODZEPHYR_H
28+
#define MICROPY_INCLUDED_ZEPHYR_MODZEPHYR_H
29+
30+
#include "py/obj.h"
31+
32+
#ifdef CONFIG_DISK_ACCESS
33+
extern const mp_obj_type_t zephyr_disk_access_type;
34+
#endif
35+
36+
#endif // MICROPY_INCLUDED_ZEPHYR_MODZEPHYR_H

ports/zephyr/prj_mimxrt1050_evk.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Required for zephyr.DiskAccess block devices
2+
CONFIG_DISK_ACCESS=y
3+
CONFIG_DISK_ACCESS_SDHC=y

ports/zephyr/zephyr_storage.c

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 NXP
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT O 3262 R OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "modzephyr.h"
28+
#include "py/runtime.h"
29+
30+
#if MICROPY_VFS
31+ D966
#include "extmod/vfs.h"
32+
#endif
33+
34+
#ifdef CONFIG_DISK_ACCESS
35+
#include <disk/disk_access.h>
36+
#endif
37+
38+
#ifdef CONFIG_DISK_ACCESS
39+
typedef struct _zephyr_disk_access_obj_t {
40+
mp_obj_base_t base;
41+
const char *pdrv;
42+
int block_size;
43+
int block_count;
44+
} zephyr_disk_access_obj_t;
45+
46+
STATIC void zephyr_disk_access_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
47+
zephyr_disk_access_obj_t *self = self_in;
48+
mp_printf(print, "DiskAccess(%s)", self->pdrv);
49+
}
50+
51+
STATIC mp_obj_t zephyr_disk_access_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
52+
mp_arg_check_num(n_args, n_kw, 1, 1, false);
53+
zephyr_disk_access_obj_t *self = m_new_obj(zephyr_disk_access_obj_t);
54+
self->base.type = type;
55+
self->pdrv = mp_obj_str_get_str(args[0]);
56+
57+
if (disk_access_init(self->pdrv) != 0) {
58+
mp_raise_ValueError("disk not found");
59+
}
60+
61+
if (disk_access_ioctl(self->pdrv, DISK_IOCTL_GET_SECTOR_SIZE, &self->block_size)) {
62+
mp_raise_ValueError("unable to get sector size");
63+
}
64+
65+
if (disk_access_ioctl(self->pdrv, DISK_IOCTL_GET_SECTOR_COUNT, &self->block_count)) {
66+
mp_raise_ValueError("unable to get block count");
67+
}
68+
69+
return MP_OBJ_FROM_PTR(self);
70+
}
71+
72+
STATIC mp_obj_t zephyr_disk_access_readblocks(mp_obj_t self_in, mp_obj_t block_num, mp_obj_t buf) {
73+
zephyr_disk_access_obj_t *self = self_in;
74+
mp_buffer_info_t bufinfo;
75+
int ret;
76+
77+
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
78+
ret = disk_access_read(self->pdrv, bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / self->block_size);
79+
return MP_OBJ_NEW_SMALL_INT(ret);
80+
}
81+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(zephyr_disk_access_readblocks_obj, zephyr_disk_access_readblocks);
82+
83+
STATIC mp_obj_t zephyr_disk_access_writeblocks(mp_obj_t self_in, mp_obj_t block_num, mp_obj_t buf) {
84+
zephyr_disk_access_obj_t *self = self_in;
85+
mp_buffer_info_t bufinfo;
86+
int ret;
87+
88+
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
89+
ret = disk_access_write(self->pdrv, bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / self->block_size);
90+
return MP_OBJ_NEW_SMALL_INT(ret);
91+
}
92+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(zephyr_disk_access_writeblocks_obj, zephyr_disk_access_writeblocks);
93+
94+
STATIC mp_obj_t zephyr_disk_access_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_in) {
95+
zephyr_disk_access_obj_t *self = self_in;
96+
mp_int_t cmd = mp_obj_get_int(cmd_in);
97+
int buf;
98+
int ret;
99+
100+
switch (cmd) {
101+
case MP_BLOCKDEV_IOCTL_INIT:
102+
case MP_BLOCKDEV_IOCTL_DEINIT:
103+
return MP_OBJ_NEW_SMALL_INT(0);
104+
105+
case MP_BLOCKDEV_IOCTL_SYNC:
106+
ret = disk_access_ioctl(self->pdrv, DISK_IOCTL_CTRL_SYNC, &buf);
107+
return MP_OBJ_NEW_SMALL_INT(ret);
108+
109+
case MP_BLOCKDEV_IOCTL_BLOCK_COUNT:
110+
return MP_OBJ_NEW_SMALL_INT(self->block_count);
111+
112+
case MP_BLOCKDEV_IOCTL_BLOCK_SIZE:
113+
return MP_OBJ_NEW_SMALL_INT(self->block_size);
114+
115+
default:
116+
return MP_OBJ_NEW_SMALL_INT(-1);
117+
}
118+
}
119+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(zephyr_disk_access_ioctl_obj, zephyr_disk_access_ioctl);
120+
121+
STATIC const mp_rom_map_elem_t zephyr_disk_access_locals_dict_table[] = {
122+
{ MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&zephyr_disk_access_readblocks_obj) },
123+
{ MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&zephyr_disk_access_writeblocks_obj) },
124+
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&zephyr_disk_access_ioctl_obj) },
125+
};
126+
STATIC MP_DEFINE_CONST_DICT(zephyr_disk_access_locals_dict, zephyr_disk_access_locals_dict_table);
127+
128+
const mp_obj_type_t zephyr_disk_access_type = {
129+
{ &mp_type_type },
130+
.name = MP_QSTR_DiskAccess,
131+
.print = zephyr_disk_access_print,
132+
.make_new = zephyr_disk_access_make_new,
133+
.locals_dict = (mp_obj_dict_t*)&zephyr_disk_access_locals_dict,
134+
};
135+
#endif // CONFIG_DISK_ACCESS

0 commit comments

Comments
 (0)
0