2015-02-21 13:10:33 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2015-02-25 10:08:51 +00:00
|
|
|
* Copyright (c) 2015 Daniel Campora
|
2015-02-21 13:10:33 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-02-21 18:58:43 +00:00
|
|
|
#include "py/mpconfig.h"
|
2015-02-21 13:10:33 +00:00
|
|
|
#include MICROPY_HAL_H
|
2015-02-21 18:58:43 +00:00
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/runtime.h"
|
2015-02-21 13:10:33 +00:00
|
|
|
#include "inc/hw_types.h"
|
|
|
|
#include "inc/hw_gpio.h"
|
|
|
|
#include "inc/hw_ints.h"
|
|
|
|
#include "inc/hw_memmap.h"
|
|
|
|
#include "rom_map.h"
|
|
|
|
#include "pin.h"
|
|
|
|
#include "prcm.h"
|
|
|
|
#include "gpio.h"
|
|
|
|
#include "sdhost.h"
|
|
|
|
#include "ff.h"
|
2015-03-18 20:48:28 +00:00
|
|
|
#include "diskio.h"
|
|
|
|
#include "sd_diskio.h"
|
2015-09-16 13:09:51 +01:00
|
|
|
#include "pybsd.h"
|
2015-02-21 13:10:33 +00:00
|
|
|
#include "mpexception.h"
|
2015-03-16 12:47:30 +00:00
|
|
|
#include "pybsleep.h"
|
2015-09-16 13:09:51 +01:00
|
|
|
#include "pybpin.h"
|
|
|
|
#include "pins.h"
|
2015-02-21 13:10:33 +00:00
|
|
|
|
2015-09-16 13:09:51 +01:00
|
|
|
/******************************************************************************
|
|
|
|
DEFINE PRIVATE CONSTANTS
|
|
|
|
******************************************************************************/
|
2015-03-16 12:47:30 +00:00
|
|
|
#define PYBSD_FREQUENCY_HZ 15000000 // 15MHz
|
2015-02-21 13:10:33 +00:00
|
|
|
|
2015-02-25 10:08:51 +00:00
|
|
|
/******************************************************************************
|
2015-09-16 13:09:51 +01:00
|
|
|
DECLARE PUBLIC DATA
|
2015-02-25 10:08:51 +00:00
|
|
|
******************************************************************************/
|
2015-09-22 22:20:29 +01:00
|
|
|
pybsd_obj_t pybsd_obj;
|
2015-02-25 10:08:51 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
2015-09-16 13:09:51 +01:00
|
|
|
DECLARE PRIVATE DATA
|
2015-02-25 10:08:51 +00:00
|
|
|
******************************************************************************/
|
2015-09-16 13:09:51 +01:00
|
|
|
STATIC const mp_obj_t pyb_sd_def_pin[3] = {&pin_GP10, &pin_GP11, &pin_GP15};
|
2015-02-25 10:08:51 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
2015-09-16 13:09:51 +01:00
|
|
|
DECLARE PRIVATE FUNCTIONS
|
2015-02-25 10:08:51 +00:00
|
|
|
******************************************************************************/
|
2015-09-16 13:09:51 +01:00
|
|
|
STATIC void pyb_sd_hw_init (pybsd_obj_t *self);
|
|
|
|
STATIC mp_obj_t pyb_sd_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
|
|
|
|
STATIC mp_obj_t pyb_sd_deinit (mp_obj_t self_in);
|
2015-03-16 12:47:30 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
2015-03-28 20:46:48 +00:00
|
|
|
DEFINE PRIVATE FUNCTIONS
|
2015-03-16 12:47:30 +00:00
|
|
|
******************************************************************************/
|
2015-08-11 15:06:43 +01:00
|
|
|
/// Initalizes the sd card hardware driver
|
2015-09-16 13:09:51 +01:00
|
|
|
STATIC void pyb_sd_hw_init (pybsd_obj_t *self) {
|
|
|
|
if (self->pin_clk) {
|
|
|
|
// Configure the clock pin as output only
|
|
|
|
MAP_PinDirModeSet(((pin_obj_t *)(self->pin_clk))->pin_num, PIN_DIR_MODE_OUT);
|
|
|
|
}
|
2015-03-16 12:47:30 +00:00
|
|
|
// Enable SD peripheral clock
|
|
|
|
MAP_PRCMPeripheralClkEnable(PRCM_SDHOST, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
|
|
|
// Reset MMCHS
|
|
|
|
MAP_PRCMPeripheralReset(PRCM_SDHOST);
|
|
|
|
// Initialize MMCHS
|
|
|
|
MAP_SDHostInit(SDHOST_BASE);
|
|
|
|
// Configure the card clock
|
|
|
|
MAP_SDHostSetExpClk(SDHOST_BASE, MAP_PRCMPeripheralClockGet(PRCM_SDHOST), PYBSD_FREQUENCY_HZ);
|
2015-08-11 15:06:43 +01:00
|
|
|
// Set card rd/wr block len
|
|
|
|
MAP_SDHostBlockSizeSet(SDHOST_BASE, SD_SECTOR_SIZE);
|
2015-09-16 13:09:51 +01:00
|
|
|
self->enabled = true;
|
2015-08-11 15:06:43 +01:00
|
|
|
}
|
|
|
|
|
2015-09-16 13:09:51 +01:00
|
|
|
STATIC mp_obj_t pyb_sd_init_helper (pybsd_obj_t *self, const mp_arg_val_t *args) {
|
|
|
|
// assign the pins
|
|
|
|
mp_obj_t pins_o = args[0].u_obj;
|
|
|
|
if (pins_o != mp_const_none) {
|
|
|
|
mp_obj_t *pins;
|
|
|
|
if (pins_o == MP_OBJ_NULL) {
|
|
|
|
// use the default pins
|
|
|
|
pins = (mp_obj_t *)pyb_sd_def_pin;
|
2015-08-11 15:06:43 +01:00
|
|
|
} else {
|
2015-09-27 08:28:27 +01:00
|
|
|
mp_obj_get_array_fixed_n(pins_o, MP_ARRAY_SIZE(pyb_sd_def_pin), &pins);
|
2015-08-11 15:06:43 +01:00
|
|
|
}
|
2015-09-27 08:28:27 +01:00
|
|
|
pin_assign_pins_af (pins, MP_ARRAY_SIZE(pyb_sd_def_pin), PIN_TYPE_STD_PU, PIN_FN_SD, 0);
|
2015-09-16 13:09:51 +01:00
|
|
|
// save the pins clock
|
|
|
|
self->pin_clk = pin_find(pins[0]);
|
2015-08-11 15:06:43 +01:00
|
|
|
}
|
|
|
|
|
2015-09-16 13:09:51 +01:00
|
|
|
pyb_sd_hw_init (self);
|
|
|
|
if (sd_disk_init() != 0) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
|
2015-08-11 15:06:43 +01:00
|
|
|
}
|
2015-09-16 13:09:51 +01:00
|
|
|
|
|
|
|
// register it with the sleep module
|
2015-09-27 12:45:48 +01:00
|
|
|
pyb_sleep_add ((const mp_obj_t)self, (WakeUpCB_t)pyb_sd_hw_init);
|
2015-08-11 15:06:43 +01:00
|
|
|
return mp_const_none;
|
2015-02-21 13:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// Micro Python bindings
|
|
|
|
//
|
|
|
|
|
2015-09-16 13:09:51 +01:00
|
|
|
STATIC const mp_arg_t pyb_sd_init_args[] = {
|
|
|
|
{ MP_QSTR_id, MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_pins, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
};
|
|
|
|
STATIC mp_obj_t pyb_sd_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *all_args) {
|
|
|
|
// parse args
|
|
|
|
mp_map_t kw_args;
|
|
|
|
mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args);
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_sd_init_args)];
|
|
|
|
mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_sd_init_args, args);
|
|
|
|
|
|
|
|
// check the peripheral id
|
|
|
|
if (args[0].u_int != 0) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup and initialize the object
|
2015-08-11 15:06:43 +01:00
|
|
|
mp_obj_t self = &pybsd_obj;
|
|
|
|
pybsd_obj.base.type = &pyb_sd_type;
|
2015-09-16 13:09:51 +01:00
|
|
|
pyb_sd_init_helper (self, &args[1]);
|
2015-08-11 15:06:43 +01:00
|
|
|
return self;
|
|
|
|
}
|
2015-02-21 13:10:33 +00:00
|
|
|
|
2015-09-16 13:09:51 +01:00
|
|
|
STATIC mp_obj_t pyb_sd_init (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_sd_init_args) - 1];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &pyb_sd_init_args[1], args);
|
|
|
|
return pyb_sd_init_helper(pos_args[0], args);
|
2015-02-21 13:10:33 +00:00
|
|
|
}
|
2015-09-16 13:09:51 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_sd_init_obj, 1, pyb_sd_init);
|
2015-02-21 13:10:33 +00:00
|
|
|
|
2015-09-16 13:09:51 +01:00
|
|
|
STATIC mp_obj_t pyb_sd_deinit (mp_obj_t self_in) {
|
2015-02-25 10:08:51 +00:00
|
|
|
pybsd_obj_t *self = self_in;
|
2015-09-16 13:09:51 +01:00
|
|
|
// disable the peripheral
|
|
|
|
self->enabled = false;
|
|
|
|
MAP_PRCMPeripheralClkDisable(PRCM_SDHOST, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
|
|
|
// de-initialze the sd card at diskio level
|
|
|
|
sd_disk_deinit();
|
|
|
|
// unregister it from the sleep module
|
2015-09-27 12:45:48 +01:00
|
|
|
pyb_sleep_remove (self);
|
2015-08-11 15:06:43 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2015-09-16 13:09:51 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_sd_deinit_obj, pyb_sd_deinit);
|
2015-02-21 17:54:04 +00:00
|
|
|
|
2015-09-16 13:09:51 +01:00
|
|
|
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 },
|
2015-02-21 13:10:33 +00:00
|
|
|
};
|
2015-09-16 13:09:51 +01:00
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(pyb_sd_locals_dict, pyb_sd_locals_dict_table);
|
2015-02-21 13:10:33 +00:00
|
|
|
|
2015-02-25 10:08:51 +00:00
|
|
|
const mp_obj_type_t pyb_sd_type = {
|
2015-02-21 13:10:33 +00:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_SD,
|
2015-09-16 13:09:51 +01:00
|
|
|
.make_new = pyb_sd_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&pyb_sd_locals_dict,
|
2015-02-21 13:10:33 +00:00
|
|
|
};
|