summaryrefslogtreecommitdiffstatshomepage
path: root/cc3200/mods/pybsd.c
diff options
context:
space:
mode:
Diffstat (limited to 'cc3200/mods/pybsd.c')
-rw-r--r--cc3200/mods/pybsd.c51
1 files changed, 47 insertions, 4 deletions
diff --git a/cc3200/mods/pybsd.c b/cc3200/mods/pybsd.c
index b8d5b4cc1a..306baea8ba 100644
--- a/cc3200/mods/pybsd.c
+++ b/cc3200/mods/pybsd.c
@@ -27,6 +27,10 @@
#include "py/mpconfig.h"
#include "py/obj.h"
#include "py/runtime.h"
+#include "py/mperrno.h"
+#include "lib/oofatfs/ff.h"
+#include "lib/oofatfs/diskio.h"
+#include "extmod/vfs_fat.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_ints.h"
@@ -36,8 +40,6 @@
#include "prcm.h"
#include "gpio.h"
#include "sdhost.h"
-#include "ff.h"
-#include "diskio.h"
#include "sd_diskio.h"
#include "pybsd.h"
#include "mpexception.h"
@@ -107,7 +109,7 @@ STATIC mp_obj_t pyb_sd_init_helper (pybsd_obj_t *self, const mp_arg_val_t *args)
pyb_sd_hw_init (self);
if (sd_disk_init() != 0) {
- mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed);
+ mp_raise_OSError(MP_EIO);
}
// register it with the sleep module
@@ -132,7 +134,7 @@ STATIC mp_obj_t pyb_sd_make_new(const mp_obj_type_t *type, size_t n_args, size_t
// check the peripheral id
if (args[0].u_int != 0) {
- mp_raise_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable);
+ mp_raise_OSError(MP_ENODEV);
}
// setup and initialize the object
@@ -163,9 +165,50 @@ STATIC mp_obj_t pyb_sd_deinit (mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_sd_deinit_obj, pyb_sd_deinit);
+STATIC mp_obj_t pyb_sd_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);
+ DRESULT res = sd_disk_read(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SD_SECTOR_SIZE);
+ return MP_OBJ_NEW_SMALL_INT(res != RES_OK); // return of 0 means success
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sd_readblocks_obj, pyb_sd_readblocks);
+
+STATIC mp_obj_t pyb_sd_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);
+ DRESULT res = sd_disk_write(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SD_SECTOR_SIZE);
+ return MP_OBJ_NEW_SMALL_INT(res != RES_OK); // return of 0 means success
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sd_writeblocks_obj, pyb_sd_writeblocks);
+
+STATIC mp_obj_t pyb_sd_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 BP_IOCTL_INIT:
+ case BP_IOCTL_DEINIT:
+ case BP_IOCTL_SYNC:
+ // nothing to do
+ return MP_OBJ_NEW_SMALL_INT(0); // success
+
+ case BP_IOCTL_SEC_COUNT:
+ return MP_OBJ_NEW_SMALL_INT(sd_disk_info.ulNofBlock * (sd_disk_info.ulBlockSize / 512));
+
+ case BP_IOCTL_SEC_SIZE:
+ return MP_OBJ_NEW_SMALL_INT(SD_SECTOR_SIZE);
+
+ default: // unknown command
+ return MP_OBJ_NEW_SMALL_INT(-1); // error
+ }
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sd_ioctl_obj, pyb_sd_ioctl);
+
STATIC const mp_map_elem_t pyb_sd_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_sd_init_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&pyb_sd_deinit_obj },
+ // block device protocol
+ { MP_OBJ_NEW_QSTR(MP_QSTR_readblocks), (mp_obj_t)&pyb_sd_readblocks_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_writeblocks), (mp_obj_t)&pyb_sd_writeblocks_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_ioctl), (mp_obj_t)&pyb_sd_ioctl_obj },
};
STATIC MP_DEFINE_CONST_DICT(pyb_sd_locals_dict, pyb_sd_locals_dict_table);