pybflash.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2017 Damien P. George
  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. //#include <stdint.h>
  27. //#include <string.h>
  28. #include "py/runtime.h"
  29. #include "lib/oofatfs/ff.h"
  30. #include "lib/oofatfs/diskio.h"
  31. #include "extmod/vfs_fat.h"
  32. #include "fatfs/src/drivers/sflash_diskio.h"
  33. #include "mods/pybflash.h"
  34. /******************************************************************************/
  35. // MicroPython bindings to expose the internal flash as an object with the
  36. // block protocol.
  37. // there is a singleton Flash object
  38. STATIC const mp_obj_base_t pyb_flash_obj = {&pyb_flash_type};
  39. 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) {
  40. // check arguments
  41. mp_arg_check_num(n_args, n_kw, 0, 0, false);
  42. // return singleton object
  43. return (mp_obj_t)&pyb_flash_obj;
  44. }
  45. STATIC mp_obj_t pyb_flash_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
  46. mp_buffer_info_t bufinfo;
  47. mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
  48. DRESULT res = sflash_disk_read(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SFLASH_SECTOR_SIZE);
  49. return MP_OBJ_NEW_SMALL_INT(res != RES_OK); // return of 0 means success
  50. }
  51. STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_readblocks_obj, pyb_flash_readblocks);
  52. STATIC mp_obj_t pyb_flash_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
  53. mp_buffer_info_t bufinfo;
  54. mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
  55. DRESULT res = sflash_disk_write(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SFLASH_SECTOR_SIZE);
  56. return MP_OBJ_NEW_SMALL_INT(res != RES_OK); // return of 0 means success
  57. }
  58. STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_writeblocks_obj, pyb_flash_writeblocks);
  59. STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in) {
  60. mp_int_t cmd = mp_obj_get_int(cmd_in);
  61. switch (cmd) {
  62. case BP_IOCTL_INIT: return MP_OBJ_NEW_SMALL_INT(sflash_disk_init() != RES_OK);
  63. case BP_IOCTL_DEINIT: sflash_disk_flush(); return MP_OBJ_NEW_SMALL_INT(0);
  64. case BP_IOCTL_SYNC: sflash_disk_flush(); return MP_OBJ_NEW_SMALL_INT(0);
  65. case BP_IOCTL_SEC_COUNT: return MP_OBJ_NEW_SMALL_INT(SFLASH_SECTOR_COUNT);
  66. case BP_IOCTL_SEC_SIZE: return MP_OBJ_NEW_SMALL_INT(SFLASH_SECTOR_SIZE);
  67. default: return mp_const_none;
  68. }
  69. }
  70. STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_ioctl_obj, pyb_flash_ioctl);
  71. STATIC const mp_rom_map_elem_t pyb_flash_locals_dict_table[] = {
  72. { MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&pyb_flash_readblocks_obj) },
  73. { MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&pyb_flash_writeblocks_obj) },
  74. { MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&pyb_flash_ioctl_obj) },
  75. };
  76. STATIC MP_DEFINE_CONST_DICT(pyb_flash_locals_dict, pyb_flash_locals_dict_table);
  77. const mp_obj_type_t pyb_flash_type = {
  78. { &mp_type_type },
  79. .name = MP_QSTR_Flash,
  80. .make_new = pyb_flash_make_new,
  81. .locals_dict = (mp_obj_t)&pyb_flash_locals_dict,
  82. };
  83. void pyb_flash_init_vfs(fs_user_mount_t *vfs) {
  84. vfs->base.type = &mp_fat_vfs_type;
  85. vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL;
  86. vfs->fatfs.drv = vfs;
  87. vfs->readblocks[0] = (mp_obj_t)&pyb_flash_readblocks_obj;
  88. vfs->readblocks[1] = (mp_obj_t)&pyb_flash_obj;
  89. vfs->readblocks[2] = (mp_obj_t)sflash_disk_read; // native version
  90. vfs->writeblocks[0] = (mp_obj_t)&pyb_flash_writeblocks_obj;
  91. vfs->writeblocks[1] = (mp_obj_t)&pyb_flash_obj;
  92. vfs->writeblocks[2] = (mp_obj_t)sflash_disk_write; // native version
  93. vfs->u.ioctl[0] = (mp_obj_t)&pyb_flash_ioctl_obj;
  94. vfs->u.ioctl[1] = (mp_obj_t)&pyb_flash_obj;
  95. }