diff options
author | Damien George <damien.p.george@gmail.com> | 2016-02-10 16:21:38 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2016-02-10 23:40:10 +0000 |
commit | 3770cd2e7062d0844cee7b69ec9d9f80a2ad1206 (patch) | |
tree | ceb2161c79c2c09552e4eeaf2b36db07c2b292a5 /stmhal/storage.c | |
parent | f7e5e677df120122eeaac18b81d3fccfe0965180 (diff) | |
download | micropython-3770cd2e7062d0844cee7b69ec9d9f80a2ad1206.tar.gz micropython-3770cd2e7062d0844cee7b69ec9d9f80a2ad1206.zip |
stmhal: Expose flash and SD card as proper objects with block protocol.
You can now create (singleton) objects representing the flash and SD
card, using:
flash = pyb.Flash()
sdcard = pyb.SDCard()
These objects provide the block protocol.
Diffstat (limited to 'stmhal/storage.c')
-rw-r--r-- | stmhal/storage.c | 83 |
1 files changed, 82 insertions, 1 deletions
diff --git a/stmhal/storage.c b/stmhal/storage.c index 29d373e60f..7bd55a2b20 100644 --- a/stmhal/storage.c +++ b/stmhal/storage.c @@ -26,9 +26,12 @@ #include <stdint.h> #include <string.h> -#include STM32_HAL_H #include "py/obj.h" +#include "py/runtime.h" +#include "lib/fatfs/ff.h" +#include "extmod/fsusermount.h" + #include "systick.h" #include "led.h" #include "flash.h" @@ -306,3 +309,81 @@ bool storage_write_block(const uint8_t *src, uint32_t block) { return true; } } + +mp_uint_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) { + for (size_t i = 0; i < num_blocks; i++) { + if (!storage_read_block(dest + i * FLASH_BLOCK_SIZE, block_num + i)) { + return 1; // error + } + } + return 0; // success +} + +mp_uint_t storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) { + for (size_t i = 0; i < num_blocks; i++) { + if (!storage_write_block(src + i * FLASH_BLOCK_SIZE, block_num + i)) { + return 1; // error + } + } + return 0; // success +} + +/******************************************************************************/ +// MicroPython bindings +// +// Expose the flash as an object with the block protocol. + +// there is a singleton Flash object +STATIC const mp_obj_base_t pyb_flash_obj = {&pyb_flash_type}; + +STATIC mp_obj_t pyb_flash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + // check arguments + mp_arg_check_num(n_args, n_kw, 0, 0, false); + + // return singleton object + return (mp_obj_t)&pyb_flash_obj; +} + +STATIC mp_obj_t pyb_flash_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE); + mp_uint_t ret = storage_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FLASH_BLOCK_SIZE); + return MP_OBJ_NEW_SMALL_INT(ret); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_readblocks_obj, pyb_flash_readblocks); + +STATIC mp_obj_t pyb_flash_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); + mp_uint_t ret = storage_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FLASH_BLOCK_SIZE); + return MP_OBJ_NEW_SMALL_INT(ret); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_writeblocks_obj, pyb_flash_writeblocks); + +STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in) { + mp_int_t cmd = mp_obj_get_int(cmd_in); + switch (cmd) { + case 1: storage_init(); return MP_OBJ_NEW_SMALL_INT(0); // INIT + case 2: storage_flush(); return MP_OBJ_NEW_SMALL_INT(0); // DEINIT; TODO properly + case 3: storage_flush(); return MP_OBJ_NEW_SMALL_INT(0); // SYNC + case 4: return MP_OBJ_NEW_SMALL_INT(storage_get_block_count()); // SEC_COUNT + case 5: return MP_OBJ_NEW_SMALL_INT(storage_get_block_size()); // SEC_SIZE + default: return mp_const_none; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_ioctl_obj, pyb_flash_ioctl); + +STATIC const mp_map_elem_t pyb_flash_locals_dict_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR_readblocks), (mp_obj_t)&pyb_flash_readblocks_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_writeblocks), (mp_obj_t)&pyb_flash_writeblocks_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ioctl), (mp_obj_t)&pyb_flash_ioctl_obj }, +}; + +STATIC MP_DEFINE_CONST_DICT(pyb_flash_locals_dict, pyb_flash_locals_dict_table); + +const mp_obj_type_t pyb_flash_type = { + { &mp_type_type }, + .name = MP_QSTR_Flash, + .make_new = pyb_flash_make_new, + .locals_dict = (mp_obj_t)&pyb_flash_locals_dict, +}; |