-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
vfs: add littlefs v1 and v2 bindings #5228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dpgeorge
merged 13 commits into
micropython:master
from
dpgeorge:extmod-add-littlefs-v2
Oct 29, 2019
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9aabb6c
extmod: Factor out block-device struct to make independent of fatfs.
dpgeorge e1c7b1c
extmod/vfs_blockdev: Factor out block device interface code.
dpgeorge 669d1d2
lib/littlefs: Add littlefs v1.7.2 source.
dpgeorge 2e66d83
lib/littlefs: Add littlefs v2.1.3 source.
dpgeorge 22bfc47
lib/littlefs: Add README describing origin and how to gen lfs1/lfs2.
dpgeorge 98beea9
extmod/vfs_blockdev: Add extended read/write methods.
dpgeorge a099505
extmod: Add VFS littlefs bindings.
dpgeorge 62d5659
unix: Enable uos.VfsLfs1, uos.VfsLfs2 on coverage build.
dpgeorge 73fddb8
tests/extmod: Add littlefs tests.
dpgeorge 7c8fb27
tests/extmod: Add test for blockdev with standard and extended protocol.
dpgeorge cfe1c5a
extmod/vfs: Rename BP_IOCTL_xxx constants to MP_BLOCKDEV_IOCTL_xxx.
dpgeorge 4cf054a
extmod/vfs: Add MP_BLOCKDEV_IOCTL_BLOCK_ERASE constant.
dpgeorge 7a24b7f
docs/library: Add documentation for extended block device protocol.
dpgeorge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2013-2019 Damien P. George | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#include "py/runtime.h" | ||
#include "py/binary.h" | ||
#include "py/objarray.h" | ||
#include "py/mperrno.h" | ||
#include "extmod/vfs.h" | ||
|
||
#if MICROPY_VFS | ||
|
||
void mp_vfs_blockdev_init(mp_vfs_blockdev_t *self, mp_obj_t bdev) { | ||
mp_load_method(bdev, MP_QSTR_readblocks, self->readblocks); | ||
mp_load_method_maybe(bdev, MP_QSTR_writeblocks, self->writeblocks); | ||
mp_load_method_maybe(bdev, MP_QSTR_ioctl, self->u.ioctl); | ||
if (self->u.ioctl[0] != MP_OBJ_NULL) { | ||
// Device supports new block protocol, so indicate it | ||
self->flags |= MP_BLOCKDEV_FLAG_HAVE_IOCTL; | ||
} else { | ||
// No ioctl method, so assume the device uses the old block protocol | ||
mp_load_method_maybe(bdev, MP_QSTR_sync, self->u.old.sync); | ||
mp_load_method(bdev, MP_QSTR_count, self->u.old.count); | ||
} | ||
} | ||
|
||
int mp_vfs_blockdev_read(mp_vfs_blockdev_t *self, size_t block_num, size_t num_blocks, uint8_t *buf) { | ||
if (self->flags & MP_BLOCKDEV_FLAG_NATIVE) { | ||
mp_uint_t (*f)(uint8_t*, uint32_t, uint32_t) = (void*)(uintptr_t)self->readblocks[2]; | ||
return f(buf, block_num, num_blocks); | ||
} else { | ||
mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, num_blocks * self->block_size, buf}; | ||
self->readblocks[2] = MP_OBJ_NEW_SMALL_INT(block_num); | ||
self->readblocks[3] = MP_OBJ_FROM_PTR(&ar); | ||
mp_call_method_n_kw(2, 0, self->readblocks); | ||
// TODO handle error return | ||
return 0; | ||
} | ||
} | ||
|
||
int mp_vfs_blockdev_read_ext(mp_vfs_blockdev_t *self, size_t block_num, size_t block_off, size_t len, uint8_t *buf) { | ||
mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, len, buf}; | ||
self->readblocks[2] = MP_OBJ_NEW_SMALL_INT(block_num); | ||
self->readblocks[3] = MP_OBJ_FROM_PTR(&ar); | ||
self->readblocks[4] = MP_OBJ_NEW_SMALL_INT(block_off); | ||
mp_obj_t ret = mp_call_method_n_kw(3, 0, self->readblocks); | ||
if (ret == mp_const_none) { | ||
return 0; | ||
} else { | ||
return MP_OBJ_SMALL_INT_VALUE(ret); | ||
} | ||
} | ||
|
||
int mp_vfs_blockdev_write(mp_vfs_blockdev_t *self, size_t block_num, size_t num_blocks, const uint8_t *buf) { | ||
if (self->writeblocks[0] == MP_OBJ_NULL) { | ||
// read-only block device | ||
return -MP_EROFS; | ||
} | ||
|
||
if (self->flags & MP_BLOCKDEV_FLAG_NATIVE) { | ||
mp_uint_t (*f)(const uint8_t*, uint32_t, uint32_t) = (void*)(uintptr_t)self->writeblocks[2]; | ||
return f(buf, block_num, num_blocks); | ||
} else { | ||
mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, num_blocks * self->block_size, (void*)buf}; | ||
self->writeblocks[2] = MP_OBJ_NEW_SMALL_INT(block_num); | ||
self->writeblocks[3] = MP_OBJ_FROM_PTR(&ar); | ||
mp_call_method_n_kw(2, 0, self->writeblocks); | ||
// TODO handle error return | ||
return 0; | ||
} | ||
} | ||
|
||
int mp_vfs_blockdev_write_ext(mp_vfs_blockdev_t *self, size_t block_num, size_t block_off, size_t len, const uint8_t *buf) { | ||
if (self->writeblocks[0] == MP_OBJ_NULL) { | ||
// read-only block device | ||
return -MP_EROFS; | ||
} | ||
|
||
mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, len, (void*)buf}; | ||
self->writeblocks[2] = MP_OBJ_NEW_SMALL_INT(block_num); | ||
self->writeblocks[3] = MP_OBJ_FROM_PTR(&ar); | ||
self->writeblocks[4] = MP_OBJ_NEW_SMALL_INT(block_off); | ||
mp_obj_t ret = mp_call_method_n_kw(3, 0, self->writeblocks); | ||
if (ret == mp_const_none) { | ||
return 0; | ||
} else { | ||
return MP_OBJ_SMALL_INT_VALUE(ret); | ||
} | ||
} | ||
|
||
mp_obj_t mp_vfs_blockdev_ioctl(mp_vfs_blockdev_t *self, uintptr_t cmd, uintptr_t arg) { | ||
if (self->flags & MP_BLOCKDEV_FLAG_HAVE_IOCTL) { | ||
// New protocol with ioctl | ||
self->u.ioctl[2] = MP_OBJ_NEW_SMALL_INT(cmd); | ||
self->u.ioctl[3] = MP_OBJ_NEW_SMALL_INT(arg); | ||
return mp_call_method_n_kw(2, 0, self->u.ioctl); | ||
} else { | ||
// Old protocol with sync and count | ||
switch (cmd) { | ||
case MP_BLOCKDEV_IOCTL_SYNC: | ||
if (self->u.old.sync[0] != MP_OBJ_NULL) { | ||
mp_call_method_n_kw(0, 0, self->u.old.sync); | ||
} | ||
break; | ||
|
||
case MP_BLOCKDEV_IOCTL_BLOCK_COUNT: | ||
return mp_call_method_n_kw(0, 0, self->u.old.count); | ||
|
||
case MP_BLOCKDEV_IOCTL_BLOCK_SIZE: | ||
// Old protocol has fixed sector size of 512 bytes | ||
break; | ||
|
||
case MP_BLOCKDEV_IOCTL_INIT: | ||
// Old protocol doesn't have init | ||
break; | ||
} | ||
return mp_const_none; | ||
} | ||
} | ||
|
||
#endif // MICROPY_VFS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This blockdev.readblocks offset argument is new isn't it, I don't think any of the other existing block device impl's include it so far?
I presume from this arg name it's intended to be an offset in units of blocks? The
RAMBlockDevice
implementation below looks like it's using it as an address bytes offset.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes offset is a new argument, only needed for littlefs.
It's an offset in bytes, within the block. So it can read/write smaller chunks than an erase block.