1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2015 Daniel Campora
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 
27 #include "py/mpconfig.h"
28 #include "py/obj.h"
29 #include "py/runtime.h"
30 #include "py/mperrno.h"
31 #include "lib/oofatfs/ff.h"
32 #include "lib/oofatfs/diskio.h"
33 #include "extmod/vfs_fat.h"
34 #include "inc/hw_types.h"
35 #include "inc/hw_gpio.h"
36 #include "inc/hw_ints.h"
37 #include "inc/hw_memmap.h"
38 #include "rom_map.h"
39 #include "pin.h"
40 #include "prcm.h"
41 #include "gpio.h"
42 #include "sdhost.h"
43 #include "sd_diskio.h"
44 #include "pybsd.h"
45 #include "pybsleep.h"
46 #include "pybpin.h"
47 #include "pins.h"
48 
49 /******************************************************************************
50  DEFINE PRIVATE CONSTANTS
51  ******************************************************************************/
52 #define PYBSD_FREQUENCY_HZ              15000000    // 15MHz
53 
54 /******************************************************************************
55  DECLARE PUBLIC DATA
56  ******************************************************************************/
57 pybsd_obj_t pybsd_obj;
58 
59 /******************************************************************************
60  DECLARE PRIVATE DATA
61  ******************************************************************************/
62 STATIC const mp_obj_t pyb_sd_def_pin[3] = {&pin_GP10, &pin_GP11, &pin_GP15};
63 
64 /******************************************************************************
65  DECLARE PRIVATE FUNCTIONS
66  ******************************************************************************/
67 STATIC void pyb_sd_hw_init (pybsd_obj_t *self);
68 STATIC mp_obj_t pyb_sd_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
69 STATIC mp_obj_t pyb_sd_deinit (mp_obj_t self_in);
70 
71 /******************************************************************************
72  DEFINE PRIVATE FUNCTIONS
73  ******************************************************************************/
74 /// Initalizes the sd card hardware driver
pyb_sd_hw_init(pybsd_obj_t * self)75 STATIC void pyb_sd_hw_init (pybsd_obj_t *self) {
76     if (self->pin_clk) {
77         // Configure the clock pin as output only
78         MAP_PinDirModeSet(((pin_obj_t *)(self->pin_clk))->pin_num, PIN_DIR_MODE_OUT);
79     }
80     // Enable SD peripheral clock
81     MAP_PRCMPeripheralClkEnable(PRCM_SDHOST, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
82     // Reset MMCHS
83     MAP_PRCMPeripheralReset(PRCM_SDHOST);
84     // Initialize MMCHS
85     MAP_SDHostInit(SDHOST_BASE);
86     // Configure the card clock
87     MAP_SDHostSetExpClk(SDHOST_BASE, MAP_PRCMPeripheralClockGet(PRCM_SDHOST), PYBSD_FREQUENCY_HZ);
88     // Set card rd/wr block len
89     MAP_SDHostBlockSizeSet(SDHOST_BASE, SD_SECTOR_SIZE);
90     self->enabled = true;
91 }
92 
pyb_sd_init_helper(pybsd_obj_t * self,const mp_arg_val_t * args)93 STATIC mp_obj_t pyb_sd_init_helper (pybsd_obj_t *self, const mp_arg_val_t *args) {
94     // assign the pins
95     mp_obj_t pins_o = args[0].u_obj;
96     if (pins_o != mp_const_none) {
97         mp_obj_t *pins;
98         if (pins_o == MP_OBJ_NULL) {
99             // use the default pins
100             pins = (mp_obj_t *)pyb_sd_def_pin;
101         } else {
102             mp_obj_get_array_fixed_n(pins_o, MP_ARRAY_SIZE(pyb_sd_def_pin), &pins);
103         }
104         pin_assign_pins_af (pins, MP_ARRAY_SIZE(pyb_sd_def_pin), PIN_TYPE_STD_PU, PIN_FN_SD, 0);
105         // save the pins clock
106         self->pin_clk = pin_find(pins[0]);
107     }
108 
109     pyb_sd_hw_init (self);
110     if (sd_disk_init() != 0) {
111         mp_raise_OSError(MP_EIO);
112     }
113 
114     // register it with the sleep module
115     pyb_sleep_add ((const mp_obj_t)self, (WakeUpCB_t)pyb_sd_hw_init);
116     return mp_const_none;
117 }
118 
119 /******************************************************************************/
120 // MicroPython bindings
121 //
122 
123 STATIC const mp_arg_t pyb_sd_init_args[] = {
124     { MP_QSTR_id,                          MP_ARG_INT, {.u_int = 0} },
125     { MP_QSTR_pins,                        MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
126 };
pyb_sd_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * all_args)127 STATIC mp_obj_t pyb_sd_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
128     // parse args
129     mp_map_t kw_args;
130     mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args);
131     mp_arg_val_t args[MP_ARRAY_SIZE(pyb_sd_init_args)];
132     mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_sd_init_args, args);
133 
134     // check the peripheral id
135     if (args[0].u_int != 0) {
136         mp_raise_OSError(MP_ENODEV);
137     }
138 
139     // setup and initialize the object
140     mp_obj_t self = &pybsd_obj;
141     pybsd_obj.base.type = &pyb_sd_type;
142     pyb_sd_init_helper (self, &args[1]);
143     return self;
144 }
145 
pyb_sd_init(size_t n_args,const mp_obj_t * pos_args,mp_map_t * kw_args)146 STATIC mp_obj_t pyb_sd_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
147     // parse args
148     mp_arg_val_t args[MP_ARRAY_SIZE(pyb_sd_init_args) - 1];
149     mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &pyb_sd_init_args[1], args);
150     return pyb_sd_init_helper(pos_args[0], args);
151 }
152 STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_sd_init_obj, 1, pyb_sd_init);
153 
pyb_sd_deinit(mp_obj_t self_in)154 STATIC mp_obj_t pyb_sd_deinit (mp_obj_t self_in) {
155     pybsd_obj_t *self = self_in;
156     // disable the peripheral
157     self->enabled = false;
158     MAP_PRCMPeripheralClkDisable(PRCM_SDHOST, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
159     // de-initialze the sd card at diskio level
160     sd_disk_deinit();
161     // unregister it from the sleep module
162     pyb_sleep_remove (self);
163     return mp_const_none;
164 }
165 STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_sd_deinit_obj, pyb_sd_deinit);
166 
pyb_sd_readblocks(mp_obj_t self,mp_obj_t block_num,mp_obj_t buf)167 STATIC mp_obj_t pyb_sd_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
168     mp_buffer_info_t bufinfo;
169     mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
170     DRESULT res = sd_disk_read(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SD_SECTOR_SIZE);
171     return MP_OBJ_NEW_SMALL_INT(res != RES_OK); // return of 0 means success
172 }
173 STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sd_readblocks_obj, pyb_sd_readblocks);
174 
pyb_sd_writeblocks(mp_obj_t self,mp_obj_t block_num,mp_obj_t buf)175 STATIC mp_obj_t pyb_sd_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
176     mp_buffer_info_t bufinfo;
177     mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
178     DRESULT res = sd_disk_write(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SD_SECTOR_SIZE);
179     return MP_OBJ_NEW_SMALL_INT(res != RES_OK); // return of 0 means success
180 }
181 STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sd_writeblocks_obj, pyb_sd_writeblocks);
182 
pyb_sd_ioctl(mp_obj_t self,mp_obj_t cmd_in,mp_obj_t arg_in)183 STATIC mp_obj_t pyb_sd_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in) {
184     mp_int_t cmd = mp_obj_get_int(cmd_in);
185     switch (cmd) {
186         case MP_BLOCKDEV_IOCTL_INIT:
187         case MP_BLOCKDEV_IOCTL_DEINIT:
188         case MP_BLOCKDEV_IOCTL_SYNC:
189             // nothing to do
190             return MP_OBJ_NEW_SMALL_INT(0); // success
191 
192         case MP_BLOCKDEV_IOCTL_BLOCK_COUNT:
193             return MP_OBJ_NEW_SMALL_INT(sd_disk_info.ulNofBlock * (sd_disk_info.ulBlockSize / 512));
194 
195         case MP_BLOCKDEV_IOCTL_BLOCK_SIZE:
196             return MP_OBJ_NEW_SMALL_INT(SD_SECTOR_SIZE);
197 
198         default: // unknown command
199             return MP_OBJ_NEW_SMALL_INT(-1); // error
200     }
201 }
202 STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sd_ioctl_obj, pyb_sd_ioctl);
203 
204 STATIC const mp_rom_map_elem_t pyb_sd_locals_dict_table[] = {
205     { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_sd_init_obj) },
206     { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pyb_sd_deinit_obj) },
207     // block device protocol
208     { MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&pyb_sd_readblocks_obj) },
209     { MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&pyb_sd_writeblocks_obj) },
210     { MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&pyb_sd_ioctl_obj) },
211 };
212 
213 STATIC MP_DEFINE_CONST_DICT(pyb_sd_locals_dict, pyb_sd_locals_dict_table);
214 
215 const mp_obj_type_t pyb_sd_type = {
216     { &mp_type_type },
217     .name = MP_QSTR_SD,
218     .make_new = pyb_sd_make_new,
219     .locals_dict = (mp_obj_t)&pyb_sd_locals_dict,
220 };
221