4343#include "simplelink.h"
4444#include "debug.h"
4545#include "mpexception.h"
46+ #include "pybsleep.h"
4647
4748
4849#if MICROPY_HW_HAS_SDCARD
4950
50- #define PYBSD_FREQUENCY_HZ 15000000 // 15MHz
51+ #define PYBSD_FREQUENCY_HZ 15000000 // 15MHz
5152
5253typedef struct {
5354 mp_obj_base_t base ;
5455 FATFS * fatfs ;
55- pin_obj_t * pin_d0 ;
5656 pin_obj_t * pin_clk ;
57- pin_obj_t * pin_cmd ;
58- pin_obj_t * pin_sd_detect ;
59- byte af_d0 ;
60- byte af_clk ;
61- byte af_cmd ;
57+ pin_obj_t * pin_detect ;
6258 bool pinsset ;
6359 bool enabled ;
6460 bool inpath ;
@@ -86,15 +82,35 @@ void pybsd_init0 (void) {
8682}
8783
8884bool pybsd_is_present (void ) {
89- if (pybsd_obj .pin_sd_detect ) {
90- return pybsd_obj .enabled && MAP_GPIOPinRead (pybsd_obj .pin_sd_detect -> port , pybsd_obj .pin_sd_detect -> bit );
85+ if (pybsd_obj .pin_detect ) {
86+ return pybsd_obj .enabled && MAP_GPIOPinRead (pybsd_obj .pin_detect -> port , pybsd_obj .pin_detect -> bit );
9187 }
9288 return pybsd_obj .enabled ;
9389}
9490
9591void pybsd_deinit (void ) {
9692 pybsd_disable ((mp_obj_t )& pybsd_obj );
97- pybsd_obj .pin_sd_detect = NULL ;
93+ pybsd_obj .pin_detect = NULL ;
94+ }
95+
96+ /******************************************************************************
97+ DECLARE PRIVATE FUNCTIONS
98+ ******************************************************************************/
99+ /// Initalizes the sd card driver
100+ STATIC void pybsd_init (pybsd_obj_t * self ) {
101+ // Configure the clock pin as output only
102+ MAP_PinDirModeSet (self -> pin_clk -> pin_num , PIN_DIR_MODE_OUT );
103+
104+ // Enable SD peripheral clock
105+ MAP_PRCMPeripheralClkEnable (PRCM_SDHOST , PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK );
106+ // Reset MMCHS
107+ MAP_PRCMPeripheralReset (PRCM_SDHOST );
108+ // Initialize MMCHS
109+ MAP_SDHostInit (SDHOST_BASE );
110+ // Configure the card clock
111+ MAP_SDHostSetExpClk (SDHOST_BASE , MAP_PRCMPeripheralClockGet (PRCM_SDHOST ), PYBSD_FREQUENCY_HZ );
112+
113+ self -> enabled = true;
98114}
99115
100116/******************************************************************************/
@@ -119,35 +135,23 @@ STATIC mp_obj_t pybsd_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_
119135 mp_arg_check_num (n_args , n_kw , 0 , 7 , false);
120136
121137 if (n_args >= 6 ) {
122- // get pins and afs
123- pybsd_obj .pin_d0 = (pin_obj_t * )pin_find (args [0 ]);
124- pybsd_obj .af_d0 = mp_obj_get_int (args [1 ]);
125- pin_verify_af (pybsd_obj .af_d0 );
138+ // save the clock pin for later use
126139 pybsd_obj .pin_clk = (pin_obj_t * )pin_find (args [2 ]);
127- pybsd_obj .af_clk = mp_obj_get_int (args [3 ]);
128- pin_verify_af (pybsd_obj .af_clk );
129- pybsd_obj .pin_cmd = (pin_obj_t * )pin_find (args [4 ]);
130- pybsd_obj .af_cmd = mp_obj_get_int (args [5 ]);
131- pin_verify_af (pybsd_obj .af_cmd );
132-
133- // configure the sdhost pins
134- // TODO: all pin configs must go through pin_config()
135- MAP_PinTypeSDHost (pybsd_obj .pin_d0 -> pin_num , pybsd_obj .af_d0 );
136- MAP_PinTypeSDHost (pybsd_obj .pin_clk -> pin_num , pybsd_obj .af_clk );
137- MAP_PinDirModeSet (pybsd_obj .pin_clk -> pin_num , PIN_DIR_MODE_OUT );
138- MAP_PinTypeSDHost (pybsd_obj .pin_cmd -> pin_num , pybsd_obj .af_cmd );
139-
140- // enable pull-ups on data and cmd
141- // TODO: all pin configs must go through pin_config()
142- MAP_PinConfigSet (pybsd_obj .pin_d0 -> pin_num , PIN_STRENGTH_4MA , PIN_TYPE_STD_PU );
143- MAP_PinConfigSet (pybsd_obj .pin_cmd -> pin_num , PIN_STRENGTH_4MA , PIN_TYPE_STD_PU );
140+
141+ // configure the data pin with pull-up enabled
142+ pin_config ((pin_obj_t * )pin_find (args [0 ]), mp_obj_get_int (args [1 ]), 0 , PIN_TYPE_STD_PU , PIN_STRENGTH_4MA );
143+ // configure the clock pin
144+ pin_config (pybsd_obj .pin_clk , mp_obj_get_int (args [3 ]), 0 , PIN_TYPE_STD , PIN_STRENGTH_4MA );
145+ // configure the command pin with pull-up enabled
146+ pin_config ((pin_obj_t * )pin_find (args [4 ]), mp_obj_get_int (args [5 ]), 0 , PIN_TYPE_STD_PU , PIN_STRENGTH_4MA );
144147
145148 // card detect pin was provided
146149 if (n_args == 7 ) {
147- pybsd_obj .pin_sd_detect = (pin_obj_t * )pin_find (args [6 ]);
148- pin_config (pybsd_obj .pin_sd_detect , PIN_MODE_0 , GPIO_DIR_MODE_IN , PIN_TYPE_STD_PU , PIN_STRENGTH_4MA );
150+ pybsd_obj .pin_detect = (pin_obj_t * )pin_find (args [6 ]);
151+ pin_config (pybsd_obj .pin_detect , PIN_MODE_0 , GPIO_DIR_MODE_IN , PIN_TYPE_STD_PU , PIN_STRENGTH_4MA );
149152 }
150153 pybsd_obj .pinsset = true;
154+ pybsd_obj .base .type = & pyb_sd_type ;
151155 }
152156 else if (n_args == 0 ) {
153157 if (!pybsd_obj .pinsset ) {
@@ -158,26 +162,16 @@ STATIC mp_obj_t pybsd_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_
158162 nlr_raise (mp_obj_new_exception_msg (& mp_type_TypeError , mpexception_num_type_invalid_arguments ));
159163 }
160164
161- // TODO: register with the sleep module!!
162-
163- pybsd_obj .base .type = & pyb_sd_type ;
164165 return & pybsd_obj ;
165166}
166167
167168/// \method enable()
168169/// Enables the sd card and mounts the file system
169170STATIC mp_obj_t pybsd_enable (mp_obj_t self_in ) {
170171 pybsd_obj_t * self = self_in ;
171- // Enable SD peripheral clock
172- MAP_PRCMPeripheralClkEnable (PRCM_SDHOST , PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK );
173- // Reset MMCHS
174- MAP_PRCMPeripheralReset (PRCM_SDHOST );
175- // Initialize MMCHS
176- MAP_SDHostInit (SDHOST_BASE );
177- // Configure the card clock
178- MAP_SDHostSetExpClk (SDHOST_BASE , MAP_PRCMPeripheralClockGet (PRCM_SDHOST ), PYBSD_FREQUENCY_HZ );
179172
180- self -> enabled = true;
173+ // do the init first
174+ pybsd_init (self );
181175
182176 // try to mount the sd card on /SD
183177 if (FR_OK != f_mount (self -> fatfs , "/SD" , 1 )) {
@@ -188,6 +182,9 @@ STATIC mp_obj_t pybsd_enable (mp_obj_t self_in) {
188182 mp_obj_list_append (mp_sys_path , MP_OBJ_NEW_QSTR (MP_QSTR__slash_SD_slash_LIB ));
189183 self -> inpath = true;
190184
185+ // register it with the sleep module
186+ pybsleep_add ((const mp_obj_t )& pybsd_obj , (WakeUpCB_t )pybsd_init );
187+
191188 return mp_const_none ;
192189}
193190STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pybsd_enable_obj , pybsd_enable );
@@ -208,6 +205,9 @@ STATIC mp_obj_t pybsd_disable (mp_obj_t self_in) {
208205 }
209206 // disable the peripheral
210207 MAP_PRCMPeripheralClkDisable (PRCM_SDHOST , PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK );
208+
209+ // unregister with the sleep module
210+ pybsleep_remove (self );
211211 }
212212 return mp_const_none ;
213213}
0 commit comments