Commit f382f444 authored by danicampora's avatar danicampora
Browse files

cc3200: Fixes and improvements to the SD card driver.

parent 963d7c7e
...@@ -88,7 +88,7 @@ DiskInfo_t sd_disk_info = {CARD_TYPE_UNKNOWN, CARD_VERSION_1, CARD_CAP_CLASS_SD ...@@ -88,7 +88,7 @@ DiskInfo_t sd_disk_info = {CARD_TYPE_UNKNOWN, CARD_VERSION_1, CARD_CAP_CLASS_SD
static unsigned int CardSendCmd (unsigned int ulCmd, unsigned int ulArg) { static unsigned int CardSendCmd (unsigned int ulCmd, unsigned int ulArg) {
unsigned long ulStatus; unsigned long ulStatus;
// Clear interrupt status // Clear the interrupt status
MAP_SDHostIntClear(SDHOST_BASE,0xFFFFFFFF); MAP_SDHostIntClear(SDHOST_BASE,0xFFFFFFFF);
// Send command // Send command
...@@ -286,6 +286,22 @@ DSTATUS sd_disk_init (void) { ...@@ -286,6 +286,22 @@ DSTATUS sd_disk_init (void) {
return sd_disk_info.bStatus; return sd_disk_info.bStatus;
} }
//*****************************************************************************
//
//! De-initializes the physical drive
//!
//! This function de-initializes the physical drive
//*****************************************************************************
void sd_disk_deinit (void) {
sd_disk_info.ucCardType = CARD_TYPE_UNKNOWN;
sd_disk_info.ulVersion = CARD_VERSION_1;
sd_disk_info.ulCapClass = CARD_CAP_CLASS_SDSC;
sd_disk_info.ulNofBlock = 0;
sd_disk_info.ulBlockSize = 0;
sd_disk_info.bStatus = STA_NOINIT;
sd_disk_info.usRCA = 0;
}
//***************************************************************************** //*****************************************************************************
// //
//! Gets the disk status. //! Gets the disk status.
...@@ -360,8 +376,6 @@ DRESULT sd_disk_read (BYTE* pBuffer, DWORD ulSectorNumber, UINT SectorCount) { ...@@ -360,8 +376,6 @@ DRESULT sd_disk_read (BYTE* pBuffer, DWORD ulSectorNumber, UINT SectorCount) {
pBuffer += 4; pBuffer += 4;
} }
CardSendCmd(CMD_STOP_TRANS, 0); CardSendCmd(CMD_STOP_TRANS, 0);
// Wait for the command to complete
while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC));
Res = RES_OK; Res = RES_OK;
} }
} }
...@@ -432,8 +446,6 @@ DRESULT sd_disk_write (const BYTE* pBuffer, DWORD ulSectorNumber, UINT SectorCou ...@@ -432,8 +446,6 @@ DRESULT sd_disk_write (const BYTE* pBuffer, DWORD ulSectorNumber, UINT SectorCou
// Wait for transfer complete // Wait for transfer complete
while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC)); while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC));
CardSendCmd(CMD_STOP_TRANS, 0); CardSendCmd(CMD_STOP_TRANS, 0);
// Wait for the command to complete
while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC));
Res = RES_OK; Res = RES_OK;
} }
} }
......
...@@ -20,6 +20,7 @@ typedef struct ...@@ -20,6 +20,7 @@ typedef struct
extern DiskInfo_t sd_disk_info; extern DiskInfo_t sd_disk_info;
DSTATUS sd_disk_init (void); DSTATUS sd_disk_init (void);
void sd_disk_deinit (void);
DSTATUS sd_disk_status (void); DSTATUS sd_disk_status (void);
bool sd_disk_ready (void); bool sd_disk_ready (void);
DRESULT sd_disk_read (BYTE* pBuffer, DWORD ulSectorNumber, UINT bSectorCount); DRESULT sd_disk_read (BYTE* pBuffer, DWORD ulSectorNumber, UINT bSectorCount);
......
...@@ -40,6 +40,8 @@ ...@@ -40,6 +40,8 @@
#include "pybpin.h" #include "pybpin.h"
#include "pybsd.h" #include "pybsd.h"
#include "ff.h" #include "ff.h"
#include "diskio.h"
#include "sd_diskio.h"
#include "simplelink.h" #include "simplelink.h"
#include "debug.h" #include "debug.h"
#include "mpexception.h" #include "mpexception.h"
...@@ -54,10 +56,8 @@ typedef struct { ...@@ -54,10 +56,8 @@ typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
FATFS *fatfs; FATFS *fatfs;
pin_obj_t *pin_clk; pin_obj_t *pin_clk;
pin_obj_t *pin_detect;
bool pinsset; bool pinsset;
bool enabled; bool enabled;
bool inpath;
} pybsd_obj_t; } pybsd_obj_t;
/****************************************************************************** /******************************************************************************
...@@ -81,16 +81,8 @@ void pybsd_init0 (void) { ...@@ -81,16 +81,8 @@ void pybsd_init0 (void) {
ASSERT ((pybsd_obj.fatfs = mem_Malloc(sizeof(FATFS))) != NULL); ASSERT ((pybsd_obj.fatfs = mem_Malloc(sizeof(FATFS))) != NULL);
} }
bool pybsd_is_present(void) {
if (pybsd_obj.pin_detect) {
return pybsd_obj.enabled && MAP_GPIOPinRead(pybsd_obj.pin_detect->port, pybsd_obj.pin_detect->bit);
}
return pybsd_obj.enabled;
}
void pybsd_deinit (void) { void pybsd_deinit (void) {
pybsd_disable ((mp_obj_t)&pybsd_obj); pybsd_disable ((mp_obj_t)&pybsd_obj);
pybsd_obj.pin_detect = NULL;
} }
/****************************************************************************** /******************************************************************************
...@@ -100,7 +92,6 @@ void pybsd_deinit (void) { ...@@ -100,7 +92,6 @@ void pybsd_deinit (void) {
STATIC void pybsd_init (pybsd_obj_t *self) { STATIC void pybsd_init (pybsd_obj_t *self) {
// Configure the clock pin as output only // Configure the clock pin as output only
MAP_PinDirModeSet(self->pin_clk->pin_num, PIN_DIR_MODE_OUT); MAP_PinDirModeSet(self->pin_clk->pin_num, PIN_DIR_MODE_OUT);
// Enable SD peripheral clock // Enable SD peripheral clock
MAP_PRCMPeripheralClkEnable(PRCM_SDHOST, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK); MAP_PRCMPeripheralClkEnable(PRCM_SDHOST, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
// Reset MMCHS // Reset MMCHS
...@@ -109,8 +100,6 @@ STATIC void pybsd_init (pybsd_obj_t *self) { ...@@ -109,8 +100,6 @@ STATIC void pybsd_init (pybsd_obj_t *self) {
MAP_SDHostInit(SDHOST_BASE); MAP_SDHostInit(SDHOST_BASE);
// Configure the card clock // Configure the card clock
MAP_SDHostSetExpClk(SDHOST_BASE, MAP_PRCMPeripheralClockGet(PRCM_SDHOST), PYBSD_FREQUENCY_HZ); MAP_SDHostSetExpClk(SDHOST_BASE, MAP_PRCMPeripheralClockGet(PRCM_SDHOST), PYBSD_FREQUENCY_HZ);
self->enabled = true;
} }
/******************************************************************************/ /******************************************************************************/
...@@ -119,22 +108,17 @@ STATIC void pybsd_init (pybsd_obj_t *self) { ...@@ -119,22 +108,17 @@ STATIC void pybsd_init (pybsd_obj_t *self) {
/// \classmethod \constructor() /// \classmethod \constructor()
/// Configure the pins used for the sd card. /// Configure the pins used for the sd card.
/// May receive 0, 6 or 7 arguments. The 7th optional argument is the card detect pin. /// May receive 0, or 6 arguments.
/// this pin needs no external pull-up and must be low when the sdcard is inserted.
/// ///
/// Usage: /// Usage:
/// sd = pyb.SD() /// sd = pyb.SD()
//// ////
/// sd = pyb.SD(d0_pin, d0_af, clk_pin, clk_af, cmd_pin, cmd_af) /// sd = pyb.SD(d0_pin, d0_af, clk_pin, clk_af, cmd_pin, cmd_af)
/// ///
/// and:
///
/// sd = pyb.SD(d0_pin, d0_af, clk_pin, clk_af, cmd_pin, cmd_af, card_detect_pin)
///
STATIC mp_obj_t pybsd_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 pybsd_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 7, false); mp_arg_check_num(n_args, n_kw, 0, 6, false);
if (n_args >= 6) { if (n_args == 6) {
// save the clock pin for later use // save the clock pin for later use
pybsd_obj.pin_clk = (pin_obj_t *)pin_find(args[2]); pybsd_obj.pin_clk = (pin_obj_t *)pin_find(args[2]);
...@@ -145,20 +129,10 @@ STATIC mp_obj_t pybsd_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_ ...@@ -145,20 +129,10 @@ STATIC mp_obj_t pybsd_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_
// configure the command pin with pull-up enabled // configure the command pin with pull-up enabled
pin_config ((pin_obj_t *)pin_find(args[4]), mp_obj_get_int(args[5]), 0, PIN_TYPE_STD_PU, PIN_STRENGTH_4MA); pin_config ((pin_obj_t *)pin_find(args[4]), mp_obj_get_int(args[5]), 0, PIN_TYPE_STD_PU, PIN_STRENGTH_4MA);
// card detect pin was provided
if (n_args == 7) {
pybsd_obj.pin_detect = (pin_obj_t *)pin_find(args[6]);
pin_config (pybsd_obj.pin_detect, PIN_MODE_0, GPIO_DIR_MODE_IN, PIN_TYPE_STD_PU, PIN_STRENGTH_4MA);
}
pybsd_obj.pinsset = true; pybsd_obj.pinsset = true;
pybsd_obj.base.type = &pyb_sd_type; pybsd_obj.base.type = &pyb_sd_type;
} }
else if (n_args == 0) { else if (!pybsd_obj.pinsset) {
if (!pybsd_obj.pinsset) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments));
}
}
else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments)); nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments));
} }
...@@ -170,20 +144,22 @@ STATIC mp_obj_t pybsd_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_ ...@@ -170,20 +144,22 @@ STATIC mp_obj_t pybsd_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_
STATIC mp_obj_t pybsd_enable (mp_obj_t self_in) { STATIC mp_obj_t pybsd_enable (mp_obj_t self_in) {
pybsd_obj_t *self = self_in; pybsd_obj_t *self = self_in;
// do the init first if (!self->enabled) {
pybsd_init (self); // do the init first
pybsd_init (self);
// try to mount the sd card on /SD // try to mount the sd card on /SD
if (FR_OK != f_mount(self->fatfs, "/SD", 1)) { if (FR_OK != f_mount(self->fatfs, "/SD", 1)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
} }
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_SD)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_SD));
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_SD_slash_LIB)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_SD_slash_LIB));
self->inpath = true;
// register it with the sleep module // register it with the sleep module
pybsleep_add ((const mp_obj_t)&pybsd_obj, (WakeUpCB_t)pybsd_init); pybsleep_add ((const mp_obj_t)&pybsd_obj, (WakeUpCB_t)pybsd_init);
self->enabled = true;
}
return mp_const_none; return mp_const_none;
} }
...@@ -198,16 +174,20 @@ STATIC mp_obj_t pybsd_disable (mp_obj_t self_in) { ...@@ -198,16 +174,20 @@ STATIC mp_obj_t pybsd_disable (mp_obj_t self_in) {
// unmount the sd card // unmount the sd card
f_mount (NULL, "/SD", 1); f_mount (NULL, "/SD", 1);
// remove sd paths from mp_sys_path // remove sd paths from mp_sys_path
if (self->inpath) { mp_obj_list_remove(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_SD));
mp_obj_list_remove(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_SD)); mp_obj_list_remove(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_SD_slash_LIB));
mp_obj_list_remove(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_SD_slash_LIB));
self->inpath = false;
}
// disable the peripheral // disable the peripheral
MAP_PRCMPeripheralClkDisable(PRCM_SDHOST, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK); MAP_PRCMPeripheralClkDisable(PRCM_SDHOST, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
// unregister with the sleep module // de-initialze de sd card at diskio level
sd_disk_deinit();
// unregister it with the sleep module
pybsleep_remove (self); pybsleep_remove (self);
// change the drive in case it was /SD
f_chdrive("/SFLASH");
} }
return mp_const_none; return mp_const_none;
} }
......
...@@ -27,11 +27,10 @@ ...@@ -27,11 +27,10 @@
#define PYBSD_H_ #define PYBSD_H_
#if MICROPY_HW_HAS_SDCARD #if MICROPY_HW_HAS_SDCARD
extern const mp_obj_type_t pyb_sd_type;
void pybsd_init0 (void); void pybsd_init0 (void);
bool pybsd_is_present(void);
void pybsd_deinit (void); void pybsd_deinit (void);
extern const mp_obj_type_t pyb_sd_type;
#endif #endif
#endif // PYBSD_H_ #endif // PYBSD_H_
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment