Skip to content

Commit

Permalink
mimxrt/sdcard: Fixed review findings.
Browse files Browse the repository at this point in the history
  • Loading branch information
alphaFred committed Aug 5, 2021
1 parent 4084cac commit 2616459
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 23 deletions.
4 changes: 2 additions & 2 deletions ports/mimxrt/boards/make-pins.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ def print_header(self, hdr_filename):
hdr_file.write("extern const mp_obj_dict_t machine_pin_board_pins_locals_dict;\n")

hdr_file.write("\n// Defines\n")
usdhcInstanceFactory(self.cpu_pins, hdr_file)
usdhc_instance_factory(self.cpu_pins, hdr_file)


def usdhcInstanceFactory(pins, output_file):
def usdhc_instance_factory(pins, output_file):
usdhc_pins = filter(lambda p: any([af for af in p.alt_fn if "USDHC" in af.af_str]), pins)

usdhc_instances = dict()
Expand Down
28 changes: 14 additions & 14 deletions ports/mimxrt/machine_sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
enum { SDCARD_INIT_ARG_ID };


STATIC const mp_arg_t allowed_args[] = {
[SDCARD_INIT_ARG_ID] { MP_QSTR_id, MP_ARG_INT, {.u_int = 1} },
STATIC const mp_arg_t sdcard_init_allowed_args[] = {
[SDCARD_INIT_ARG_ID] = { MP_QSTR_id, MP_ARG_INT, {.u_int = 1} },
};


Expand Down Expand Up @@ -80,8 +80,8 @@ STATIC mp_obj_t sdcard_obj_make_new(const mp_obj_type_t *type, size_t n_args, si
mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args);

// Parse args
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_arg_val_t args[MP_ARRAY_SIZE(sdcard_init_allowed_args)];
mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(sdcard_init_allowed_args), sdcard_init_allowed_args, args);

// Extract arguments
mp_int_t sdcard_id = args[SDCARD_INIT_ARG_ID].u_int;
Expand All @@ -103,8 +103,8 @@ STATIC mp_obj_t sdcard_obj_make_new(const mp_obj_type_t *type, size_t n_args, si
// init()
STATIC mp_obj_t machine_sdcard_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
// Parse args
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args) - 1];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_arg_val_t args[MP_ARRAY_SIZE(sdcard_init_allowed_args) - 1];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(sdcard_init_allowed_args), sdcard_init_allowed_args, args);

// Todo: make sure that there is a valid reason that a call to init would not change anything for an already initialized hsot
machine_sdcard_init_helper(pos_args[0], args);
Expand All @@ -119,15 +119,15 @@ STATIC mp_obj_t machine_sdcard_deinit(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_sdcard_deinit_obj, machine_sdcard_deinit);

// readblocks(block_num, buf, [offset])
STATIC mp_obj_t machine_sdcard_readblocks(mp_obj_t self_in, mp_obj_t _block_num, mp_obj_t _buf) {
// readblocks(block_num, buf)
STATIC mp_obj_t machine_sdcard_readblocks(mp_obj_t self_in, mp_obj_t block_num, mp_obj_t buf) {
mp_buffer_info_t bufinfo;
mimxrt_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_get_buffer_raise(_buf, &bufinfo, MP_BUFFER_WRITE);
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
// ---

if (self->initialized &&
sdcard_read(self, bufinfo.buf, mp_obj_get_int(_block_num), bufinfo.len / SDCARD_DEFAULT_BLOCK_SIZE)) {
sdcard_read(self, bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SDCARD_DEFAULT_BLOCK_SIZE)) {
return MP_OBJ_NEW_SMALL_INT(0);
} else {
// Todo: fix upper layer instead of throwing exception here
Expand All @@ -136,15 +136,15 @@ STATIC mp_obj_t machine_sdcard_readblocks(mp_obj_t self_in, mp_obj_t _block_num,
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(machine_sdcard_readblocks_obj, machine_sdcard_readblocks);

// writeblocks(block_num, buf, [offset])
STATIC mp_obj_t machine_sdcard_writeblocks(mp_obj_t self_in, mp_obj_t _block_num, mp_obj_t _buf) {
// writeblocks(block_num, buf)
STATIC mp_obj_t machine_sdcard_writeblocks(mp_obj_t self_in, mp_obj_t block_num, mp_obj_t buf) {
mp_buffer_info_t bufinfo;
mimxrt_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_get_buffer_raise(_buf, &bufinfo, MP_BUFFER_WRITE);
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
// ---

if (self->initialized &&
sdcard_write(self, bufinfo.buf, mp_obj_get_int(_block_num), bufinfo.len / SDCARD_DEFAULT_BLOCK_SIZE)) {
sdcard_write(self, bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SDCARD_DEFAULT_BLOCK_SIZE)) {
return MP_OBJ_NEW_SMALL_INT(0);
} else {
// Todo: fix upper layer instead of throwing exception here
Expand Down
12 changes: 6 additions & 6 deletions ports/mimxrt/modules/_boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
import os
import sys
import mimxrt
from machine import SDCard

bdev = mimxrt.Flash()
try:
vfs = os.VfsLfs2(bdev, progsize=256)
except:
os.VfsLfs2.mkfs(bdev, progsize=256)
vfs = os.VfsLfs2(bdev, progsize=256)
finally:
os.mount(vfs, "/flash")
os.chdir("/flash")
sys.path.append("/flash")
os.mount(vfs, "/flash")
os.chdir("/flash")
sys.path.append("/flash")

# do not mount the SD card if SKIPSD exists.
# do not mount the SD card if SKIPSD exists and SDCard module is available.
try:
from machine import SDCard

os.stat("SKIPSD")
except:
sdcard = SDCard(1)
Expand Down
26 changes: 26 additions & 0 deletions ports/mimxrt/sdcard.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Philipp Ebensberger
*
* 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 "sdcard.h"
#include "ticks.h"
#include "fsl_iomuxc.h"
Expand Down
2 changes: 1 addition & 1 deletion ports/mimxrt/sdcard.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Philipp Ebensberger
* Copyright (c) 2021 Philipp Ebensberger
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down

0 comments on commit 2616459

Please sign in to comment.