diff options
author | Daniel Campora <daniel@wipy.io> | 2015-09-16 14:09:51 +0200 |
---|---|---|
committer | Daniel Campora <daniel@wipy.io> | 2015-09-21 22:30:32 +0200 |
commit | dffa9f6da65cd03e834b2ed3914f40428f72e49f (patch) | |
tree | 1f2e51f17c511f884db77e47d481c0f9c1b6bed2 /cc3200/fatfs | |
parent | 660f8613fd8e38863998a9758d97eada0eebc47d (diff) | |
download | micropython-dffa9f6da65cd03e834b2ed3914f40428f72e49f.tar.gz micropython-dffa9f6da65cd03e834b2ed3914f40428f72e49f.zip |
cc3200: New SD and RTC API plus os and time modules' extensions.
Diffstat (limited to 'cc3200/fatfs')
-rw-r--r-- | cc3200/fatfs/src/diskio.c | 184 | ||||
-rw-r--r-- | cc3200/fatfs/src/diskio.h | 2 | ||||
-rw-r--r-- | cc3200/fatfs/src/drivers/sd_diskio.c | 129 | ||||
-rw-r--r-- | cc3200/fatfs/src/drivers/sd_diskio.h | 1 | ||||
-rw-r--r-- | cc3200/fatfs/src/ffconf.c | 38 |
5 files changed, 173 insertions, 181 deletions
diff --git a/cc3200/fatfs/src/diskio.c b/cc3200/fatfs/src/diskio.c index cd1d2e5be5..f8a84a30c5 100644 --- a/cc3200/fatfs/src/diskio.c +++ b/cc3200/fatfs/src/diskio.c @@ -10,12 +10,11 @@ #include <stdbool.h> #include "py/mpconfig.h" +#include "py/runtime.h" #include "py/obj.h" -#include "diskio.h" /* FatFs lower layer API */ +#include "diskio.h" /* FatFs lower layer API */ #include "sflash_diskio.h" /* Serial flash disk IO API */ -#if MICROPY_HW_HAS_SDCARD -#include "sd_diskio.h" /* SDCARD disk IO API */ -#endif +#include "sd_diskio.h" /* SDCARD disk IO API */ #include "inc/hw_types.h" #include "inc/hw_ints.h" #include "inc/hw_memmap.h" @@ -23,10 +22,9 @@ #include "prcm.h" #include "pybrtc.h" #include "timeutils.h" - -/* Definitions of physical drive number for each drive */ -#define SFLASH 0 /* Map SFLASH drive to drive number 0 */ -#define SDCARD 1 /* Map SD card to drive number 1 */ +#include "ff.h" +#include "pybsd.h" +#include "moduos.h" /*-----------------------------------------------------------------------*/ @@ -37,21 +35,20 @@ DSTATUS disk_status ( BYTE pdrv /* Physical drive nmuber to identify the drive */ ) { - switch (pdrv) { - case SFLASH : - return sflash_disk_status(); -#if MICROPY_HW_HAS_SDCARD - case SDCARD : - return sd_disk_status(); -#endif - default: - break; - } - return STA_NODISK; + if (pdrv == FLASH) { + return sflash_disk_status(); + } else { + os_fs_mount_t *mount_obj; + if ((mount_obj = osmount_find_by_volume(pdrv))) { + if (mount_obj->writeblocks[0] == MP_OBJ_NULL) { + return STA_PROTECT; + } + return 0; + } + } + return STA_NODISK; } - - /*-----------------------------------------------------------------------*/ /* Inidialize a Drive */ /*-----------------------------------------------------------------------*/ @@ -60,29 +57,22 @@ DSTATUS disk_initialize ( BYTE pdrv /* Physical drive nmuber to identify the drive */ ) { - DSTATUS stat = 0; - - switch (pdrv) { - case SFLASH : - if (RES_OK != sflash_disk_init()) { - stat = STA_NOINIT; - } - return stat; -#if MICROPY_HW_HAS_SDCARD - case SDCARD : - if (RES_OK != sd_disk_init()) { - stat = STA_NOINIT; + if (pdrv == FLASH) { + if (RES_OK != sflash_disk_init()) { + return STA_NOINIT; } - return stat; -#endif - default: - break; - } - return STA_NOINIT; + } else { + os_fs_mount_t *mount_obj; + if ((mount_obj = osmount_find_by_volume(pdrv))) { + if (mount_obj->writeblocks[0] == MP_OBJ_NULL) { + return STA_PROTECT; + } + return 0; + } + } + return STA_NODISK; } - - /*-----------------------------------------------------------------------*/ /* Read Sector(s) */ /*-----------------------------------------------------------------------*/ @@ -94,22 +84,25 @@ DRESULT disk_read ( UINT count /* Number of sectors to read */ ) { - switch (pdrv) { - case SFLASH : - return sflash_disk_read(buff, sector, count); -#if MICROPY_HW_HAS_SDCARD - case SDCARD : - return sd_disk_read(buff, sector, count); -#endif - default: - break; - } - - return RES_PARERR; + if (pdrv == FLASH) { + return sflash_disk_read(buff, sector, count); + } else { + os_fs_mount_t *mount_obj; + if ((mount_obj = osmount_find_by_volume(pdrv))) { + // optimization for the built-in sd card device + if (mount_obj->device == (mp_obj_t)&pybsd_obj) { + return sd_disk_read(buff, sector, count); + } + mount_obj->readblocks[2] = MP_OBJ_NEW_SMALL_INT(sector); + mount_obj->readblocks[3] = mp_obj_new_bytearray_by_ref(count * 512, buff); + return mp_obj_get_int(mp_call_method_n_kw(2, 0, mount_obj->readblocks)); + } + // nothing mounted + return RES_ERROR; + } + return RES_PARERR; } - - /*-----------------------------------------------------------------------*/ /* Write Sector(s) */ /*-----------------------------------------------------------------------*/ @@ -122,18 +115,23 @@ DRESULT disk_write ( UINT count /* Number of sectors to write */ ) { - switch (pdrv) { - case SFLASH : - return sflash_disk_write(buff, sector, count); -#if MICROPY_HW_HAS_SDCARD - case SDCARD : - return sd_disk_write(buff, sector, count); -#endif - default: - break; - } - - return RES_PARERR; + if (pdrv == FLASH) { + return sflash_disk_write(buff, sector, count); + } else { + os_fs_mount_t *mount_obj; + if ((mount_obj = osmount_find_by_volume(pdrv))) { + // optimization for the built-in sd card device + if (mount_obj->device == (mp_obj_t)&pybsd_obj) { + return sd_disk_write(buff, sector, count); + } + mount_obj->writeblocks[2] = MP_OBJ_NEW_SMALL_INT(sector); + mount_obj->writeblocks[3] = mp_obj_new_bytearray_by_ref(count * 512, (void *)buff); + return mp_obj_get_int(mp_call_method_n_kw(2, 0, mount_obj->writeblocks)); + } + // nothing mounted + return RES_ERROR; + } + return RES_PARERR; } #endif @@ -149,41 +147,47 @@ DRESULT disk_ioctl ( void *buff /* Buffer to send/receive control data */ ) { - switch (pdrv) { - case SFLASH: + if (pdrv == FLASH) { switch (cmd) { case CTRL_SYNC: return sflash_disk_flush(); case GET_SECTOR_COUNT: *((DWORD*)buff) = SFLASH_SECTOR_COUNT; return RES_OK; - break; case GET_SECTOR_SIZE: - *((WORD*)buff) = SFLASH_SECTOR_SIZE; + *((DWORD*)buff) = SFLASH_SECTOR_SIZE; return RES_OK; - break; case GET_BLOCK_SIZE: *((DWORD*)buff) = 1; // high-level sector erase size in units of the block size return RES_OK; } - break; -#if MICROPY_HW_HAS_SDCARD - case SDCARD: - switch (cmd) { - case CTRL_SYNC: - return RES_OK; - case GET_SECTOR_COUNT: - *(WORD*)buff = sd_disk_info.ulNofBlock; - break; - case GET_SECTOR_SIZE : - *(WORD*)buff = SD_SECTOR_SIZE; - break; - case GET_BLOCK_SIZE: - *((DWORD*)buff) = 1; // high-level sector erase size in units of the block size - return RES_OK; + } else { + os_fs_mount_t *mount_obj; + if ((mount_obj = osmount_find_by_volume(pdrv))) { + switch (cmd) { + case CTRL_SYNC: + if (mount_obj->sync[0] != MP_OBJ_NULL) { + mp_call_method_n_kw(0, 0, mount_obj->sync); + } + return RES_OK; + case GET_SECTOR_COUNT: + // optimization for the built-in sd card device + if (mount_obj->device == (mp_obj_t)&pybsd_obj) { + *((DWORD*)buff) = sd_disk_info.ulNofBlock * (sd_disk_info.ulBlockSize / 512); + } else { + *((DWORD*)buff) = mp_obj_get_int(mp_call_method_n_kw(0, 0, mount_obj->count)); + } + return RES_OK; + case GET_SECTOR_SIZE: + *((DWORD*)buff) = SD_SECTOR_SIZE; // Sector size is fixed to 512 bytes, as with SD cards + return RES_OK; + case GET_BLOCK_SIZE: + *((DWORD*)buff) = 1; // high-level sector erase size in units of the block size + return RES_OK; + } } - break; -#endif + // nothing mounted + return RES_ERROR; } return RES_PARERR; } @@ -195,7 +199,7 @@ DWORD get_fattime ( ) { timeutils_struct_time_t tm; - timeutils_seconds_since_2000_to_struct_time(pybrtc_get_seconds(), &tm); + timeutils_seconds_since_2000_to_struct_time(pyb_rtc_get_seconds(), &tm); return ((tm.tm_year - 1980) << 25) | ((tm.tm_mon) << 21) | ((tm.tm_mday) << 16) | ((tm.tm_hour) << 11) | diff --git a/cc3200/fatfs/src/diskio.h b/cc3200/fatfs/src/diskio.h index 6e4a056f06..15d732b6d1 100644 --- a/cc3200/fatfs/src/diskio.h +++ b/cc3200/fatfs/src/diskio.h @@ -38,6 +38,8 @@ DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count); DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count); DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); +/* Definitions of physical drive number for each drive */ +#define FLASH 0 /* Map FLASH drive to drive number 0 */ /* Disk Status Bits (DSTATUS) */ diff --git a/cc3200/fatfs/src/drivers/sd_diskio.c b/cc3200/fatfs/src/drivers/sd_diskio.c index 3b17aa6b89..358f33fbbd 100644 --- a/cc3200/fatfs/src/drivers/sd_diskio.c +++ b/cc3200/fatfs/src/drivers/sd_diskio.c @@ -302,19 +302,6 @@ void sd_disk_deinit (void) { //***************************************************************************** // -//! Gets the disk status. -//! -//! This function gets the current status of the drive. -//! -//! \return Returns the current status of the specified drive -// -//***************************************************************************** -DSTATUS sd_disk_status (void) { - return sd_disk_info.bStatus; -} - -//***************************************************************************** -// //! Reads sector(s) from the disk drive. //! //! @@ -365,6 +352,7 @@ DRESULT sd_disk_read (BYTE* pBuffer, DWORD ulSectorNumber, UINT SectorCount) { pBuffer += 4; } CardSendCmd(CMD_STOP_TRANS, 0); + while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC)); Res = RES_OK; } } @@ -384,61 +372,62 @@ DRESULT sd_disk_read (BYTE* pBuffer, DWORD ulSectorNumber, UINT SectorCount) { // //***************************************************************************** DRESULT sd_disk_write (const BYTE* pBuffer, DWORD ulSectorNumber, UINT SectorCount) { - DRESULT Res = RES_ERROR; - unsigned long ulSize; - - if (SectorCount > 0) { - // Return if disk not initialized - if (sd_disk_info.bStatus & STA_NOINIT) { - return RES_NOTRDY; - } - - // SDSC uses linear address, SDHC uses block address - if (sd_disk_info.ulCapClass == CARD_CAP_CLASS_SDSC) { - ulSectorNumber = ulSectorNumber * SD_SECTOR_SIZE; - } - - // Set the block count - MAP_SDHostBlockCountSet(SDHOST_BASE, SectorCount); - - // Compute the number of words - ulSize = (SD_SECTOR_SIZE * SectorCount) / 4; - - // Check if 1 block or multi block transfer - if (SectorCount == 1) { - // Send single block write command - if (CardSendCmd(CMD_WRITE_SINGLE_BLK, ulSectorNumber) == 0) { - // Write the data - while (ulSize--) { - MAP_SDHostDataWrite (SDHOST_BASE, (*(unsigned long *)pBuffer)); - pBuffer += 4; - } - // Wait for data transfer complete - while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC)); - Res = RES_OK; - } - } - else { - // Set the card write block count - if (sd_disk_info.ucCardType == CARD_TYPE_SDCARD) { - CardSendCmd(CMD_APP_CMD,sd_disk_info.usRCA << 16); - CardSendCmd(CMD_SET_BLK_CNT, SectorCount); - } - - // Send multi block write command - if (CardSendCmd(CMD_WRITE_MULTI_BLK, ulSectorNumber) == 0) { - // Write the data buffer - while (ulSize--) { - MAP_SDHostDataWrite(SDHOST_BASE, (*(unsigned long *)pBuffer)); - pBuffer += 4; - } - // Wait for transfer complete - while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC)); - CardSendCmd(CMD_STOP_TRANS, 0); - Res = RES_OK; - } - } - } - - return Res; + DRESULT Res = RES_ERROR; + unsigned long ulSize; + + if (SectorCount > 0) { + // Return if disk not initialized + if (sd_disk_info.bStatus & STA_NOINIT) { + return RES_NOTRDY; + } + + // SDSC uses linear address, SDHC uses block address + if (sd_disk_info.ulCapClass == CARD_CAP_CLASS_SDSC) { + ulSectorNumber = ulSectorNumber * SD_SECTOR_SIZE; + } + + // Set the block count + MAP_SDHostBlockCountSet(SDHOST_BASE, SectorCount); + + // Compute the number of words + ulSize = (SD_SECTOR_SIZE * SectorCount) / 4; + + // Check if 1 block or multi block transfer + if (SectorCount == 1) { + // Send single block write command + if (CardSendCmd(CMD_WRITE_SINGLE_BLK, ulSectorNumber) == 0) { + // Write the data + while (ulSize--) { + MAP_SDHostDataWrite (SDHOST_BASE, (*(unsigned long *)pBuffer)); + pBuffer += 4; + } + // Wait for data transfer complete + while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC)); + Res = RES_OK; + } + } + else { + // Set the card write block count + if (sd_disk_info.ucCardType == CARD_TYPE_SDCARD) { + CardSendCmd(CMD_APP_CMD,sd_disk_info.usRCA << 16); + CardSendCmd(CMD_SET_BLK_CNT, SectorCount); + } + + // Send multi block write command + if (CardSendCmd(CMD_WRITE_MULTI_BLK, ulSectorNumber) == 0) { + // Write the data buffer + while (ulSize--) { + MAP_SDHostDataWrite(SDHOST_BASE, (*(unsigned long *)pBuffer)); + pBuffer += 4; + } + // Wait for transfer complete + while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC)); + CardSendCmd(CMD_STOP_TRANS, 0); + while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC)); + Res = RES_OK; + } + } + } + + return Res; } diff --git a/cc3200/fatfs/src/drivers/sd_diskio.h b/cc3200/fatfs/src/drivers/sd_diskio.h index f1cb2a0bfe..b5a1944ec4 100644 --- a/cc3200/fatfs/src/drivers/sd_diskio.h +++ b/cc3200/fatfs/src/drivers/sd_diskio.h @@ -21,7 +21,6 @@ extern DiskInfo_t sd_disk_info; DSTATUS sd_disk_init (void); void sd_disk_deinit (void); -DSTATUS sd_disk_status (void); DRESULT sd_disk_read (BYTE* pBuffer, DWORD ulSectorNumber, UINT bSectorCount); DRESULT sd_disk_write (const BYTE* pBuffer, DWORD ulSectorNumber, UINT bSectorCount); diff --git a/cc3200/fatfs/src/ffconf.c b/cc3200/fatfs/src/ffconf.c index e3c425e332..5726db1bc5 100644 --- a/cc3200/fatfs/src/ffconf.c +++ b/cc3200/fatfs/src/ffconf.c @@ -26,11 +26,11 @@ #include <string.h> -#include "py/mpconfig.h" -#include "py/misc.h" +#include "py/mpstate.h" #include "ff.h" #include "ffconf.h" #include "diskio.h" +#include "moduos.h" #if _FS_RPATH extern BYTE ff_CurrVol; @@ -65,31 +65,29 @@ int ff_get_ldnumber (const TCHAR **path) { } if (check_path(path, "/flash", 6)) { - return 0; + return FLASH; } -#if MICROPY_HW_HAS_SDCARD - else if (check_path(path, "/sd", 3)) { - return 1; - } -#endif else { - return -1; + for (mp_uint_t i = 0; i < MP_STATE_PORT(mount_obj_list).len; i++) { + os_fs_mount_t *mount_obj = ((os_fs_mount_t *)(MP_STATE_PORT(mount_obj_list).items[i])); + if (check_path(path, mount_obj->path, mount_obj->pathlen)) { + return mount_obj->vol; + } + } } + + return -1; } void ff_get_volname(BYTE vol, TCHAR **dest) { -#if MICROPY_HW_HAS_SDCARD - if (vol == 0) -#endif - { + if (vol == FLASH) { memcpy(*dest, "/flash", 6); *dest += 6; + } else { + os_fs_mount_t *mount_obj; + if ((mount_obj = osmount_find_by_volume(vol))) { + memcpy(*dest, mount_obj->path, mount_obj->pathlen); + *dest += mount_obj->pathlen; + } } -#if MICROPY_HW_HAS_SDCARD - else - { - memcpy(*dest, "/sd", 3); - *dest += 3; - } -#endif } |