4949
5050#define PYBSD_FREQUENCY_HZ 15000000 // 15MHz
5151
52- static byte pybsd_pin_d0 , pybsd_pin_clk , pybsd_pin_cmd ;
53- static byte pybsd_af_d0 , pybsd_af_clk , pybsd_af_cmd ;
5452static const pin_obj_t * pybsd_pin_sd_detect ;
55- static bool pybsd_pin_config_set ;
5653static bool pybsd_is_enabled ;
5754static bool pybsd_in_path ;
5855static FATFS * sd_fatfs ;
@@ -63,20 +60,16 @@ void pybsd_init0 (void) {
6360 ASSERT ((sd_fatfs = mem_Malloc (sizeof (FATFS ))) != NULL );
6461}
6562
66-
6763bool pybsd_is_present (void ) {
6864 if (pybsd_pin_sd_detect ) {
6965 return pybsd_is_enabled && MAP_GPIOPinRead (pybsd_pin_sd_detect -> port , pybsd_pin_sd_detect -> bit );
7066 }
71- return true ;
67+ return pybsd_is_enabled ;
7268}
7369
7470/******************************************************************************/
7571// Micro Python bindings
7672//
77- // Note: these function are a bit ad-hoc at the moment and are mainly intended
78- // for testing purposes. In the future SD should be a proper class with a
79- // consistent interface and methods to mount/unmount it.
8073
8174/// \method sd_config([value])
8275/// Configure the pins used for the sd card.
@@ -91,9 +84,12 @@ bool pybsd_is_present(void) {
9184///
9285/// pyb.SDcard.sd_config_pins (d0_pin, d0_af, clk_pin, clk_af, cmd_pin, cmd_af, card_detect_pin)
9386///
94- STATIC mp_obj_t pybsd_config (mp_uint_t n_args , const mp_obj_t * args ) {
95- const pin_obj_t * pin = NULL ;
87+ STATIC mp_obj_t pybsd_config_pins (mp_uint_t n_args , const mp_obj_t * args ) {
88+ byte pybsd_pin_d0 , pybsd_pin_clk , pybsd_pin_cmd ;
89+ byte pybsd_af_d0 , pybsd_af_clk , pybsd_af_cmd ;
90+ const pin_obj_t * pin ;
9691
92+ // get all the arguments
9793 pin = pin_find (args [1 ]);
9894 pybsd_pin_d0 = pin -> pin_num ;
9995 pybsd_af_d0 = mp_obj_get_int (args [2 ]);
@@ -106,42 +102,32 @@ STATIC mp_obj_t pybsd_config (mp_uint_t n_args, const mp_obj_t *args) {
106102 pybsd_pin_cmd = pin -> pin_num ;
107103 pybsd_af_cmd = mp_obj_get_int (args [6 ]);
108104 pin_verify_af (pybsd_af_cmd );
105+
106+ // configure the sdhost pins
107+ MAP_PinTypeSDHost (pybsd_pin_d0 , pybsd_af_d0 );
108+ MAP_PinTypeSDHost (pybsd_pin_clk , pybsd_af_clk );
109+ MAP_PinTypeSDHost (pybsd_pin_cmd , pybsd_af_cmd );
110+ MAP_PinDirModeSet (pybsd_pin_clk , PIN_DIR_MODE_OUT );
111+ // configure the card detect pin if provided
109112 if (n_args == 8 ) {
110113 pybsd_pin_sd_detect = pin_find (args [7 ]);
114+ pin_config (pybsd_pin_sd_detect , PIN_MODE_0 , GPIO_DIR_MODE_IN , PIN_TYPE_STD_PU , PIN_STRENGTH_4MA );
111115 }
112- pybsd_pin_config_set = true;
113116
114117 return mp_const_none ;
115118}
116- STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (pybsd_config_obj , 7 , 8 , pybsd_config );
119+ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (pybsd_config_pins_obj , 7 , 8 , pybsd_config_pins );
117120
118121STATIC mp_obj_t pybsd_enable (mp_obj_t self ) {
119- if (pybsd_pin_config_set ) {
120- // Configure the D0 pin
121- MAP_PinTypeSDHost (pybsd_pin_d0 , pybsd_af_d0 );
122- // Configure the CLK pin
123- MAP_PinTypeSDHost (pybsd_pin_clk , pybsd_af_clk );
124- // Configure the CMD pin
125- MAP_PinTypeSDHost (pybsd_pin_cmd , pybsd_af_cmd );
126- // Set the SD card clock as an output pin
127- MAP_PinDirModeSet (pybsd_pin_clk , PIN_DIR_MODE_OUT );
128- // Enable SD peripheral clock
129- MAP_PRCMPeripheralClkEnable (PRCM_SDHOST , PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK );
130- // Reset MMCHS
131- MAP_PRCMPeripheralReset (PRCM_SDHOST );
132- // Initialize MMCHS
133- MAP_SDHostInit (SDHOST_BASE );
134- // Configure the card clock
135- MAP_SDHostSetExpClk (SDHOST_BASE , MAP_PRCMPeripheralClockGet (PRCM_SDHOST ), PYBSD_FREQUENCY_HZ );
136-
137- // Configure the card detect pin (if available)
138- if (pybsd_pin_sd_detect ) {
139- pin_config (pybsd_pin_sd_detect , PIN_MODE_0 , GPIO_DIR_MODE_IN , PIN_TYPE_STD_PU , PIN_STRENGTH_4MA );
140- }
141- }
142- else {
143- nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_resource_not_avaliable ));
144- }
122+ // Enable SD peripheral clock
123+ MAP_PRCMPeripheralClkEnable (PRCM_SDHOST , PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK );
124+ // Reset MMCHS
125+ MAP_PRCMPeripheralReset (PRCM_SDHOST );
126+ // Initialize MMCHS
127+ MAP_SDHostInit (SDHOST_BASE );
128+ // Configure the card clock
129+ MAP_SDHostSetExpClk (SDHOST_BASE , MAP_PRCMPeripheralClockGet (PRCM_SDHOST ), PYBSD_FREQUENCY_HZ );
130+
145131 pybsd_is_enabled = true;
146132
147133 // try to mount the sd card on /SD
@@ -176,7 +162,7 @@ STATIC mp_obj_t pybsd_disable(mp_obj_t self) {
176162STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pybsd_disable_obj , pybsd_disable );
177163
178164STATIC const mp_map_elem_t pybsd_locals_dict_table [] = {
179- { MP_OBJ_NEW_QSTR (MP_QSTR_config ), (mp_obj_t )& pybsd_config_obj },
165+ { MP_OBJ_NEW_QSTR (MP_QSTR_config_pins ), (mp_obj_t )& pybsd_config_pins_obj },
180166 { MP_OBJ_NEW_QSTR (MP_QSTR_enable ), (mp_obj_t )& pybsd_enable_obj },
181167 { MP_OBJ_NEW_QSTR (MP_QSTR_disable ), (mp_obj_t )& pybsd_disable_obj },
182168};
0 commit comments