pybsd.c 8.5 KB

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