1010#include <stdbool.h>
1111
1212#include "py/mpconfig.h"
13+ #include "py/runtime.h"
1314#include "py/obj.h"
14- #include "diskio.h" /* FatFs lower layer API */
15+ #include "diskio.h" /* FatFs lower layer API */
1516#include "sflash_diskio.h" /* Serial flash disk IO API */
16- #if MICROPY_HW_HAS_SDCARD
17- #include "sd_diskio.h" /* SDCARD disk IO API */
18- #endif
17+ #include "sd_diskio.h" /* SDCARD disk IO API */
1918#include "inc/hw_types.h"
2019#include "inc/hw_ints.h"
2120#include "inc/hw_memmap.h"
2221#include "rom_map.h"
2322#include "prcm.h"
2423#include "pybrtc.h"
2524#include "timeutils.h"
26-
27- /* Definitions of physical drive number for each drive */
28- #define SFLASH 0 /* Map SFLASH drive to drive number 0 */
29- #define SDCARD 1 /* Map SD card to drive number 1 */
25+ #include "ff.h"
26+ #include "pybsd.h"
27+ #include "moduos.h"
3028
3129
3230/*-----------------------------------------------------------------------*/
@@ -37,21 +35,20 @@ DSTATUS disk_status (
3735 BYTE pdrv /* Physical drive nmuber to identify the drive */
3836)
3937{
40- switch (pdrv ) {
41- case SFLASH :
42- return sflash_disk_status ();
43- #if MICROPY_HW_HAS_SDCARD
44- case SDCARD :
45- return sd_disk_status ();
46- #endif
47- default :
48- break ;
49- }
50- return STA_NODISK ;
38+ if (pdrv == FLASH ) {
39+ return sflash_disk_status ();
40+ } else {
41+ os_fs_mount_t * mount_obj ;
42+ if ((mount_obj = osmount_find_by_volume (pdrv ))) {
43+ if (mount_obj -> writeblocks [0 ] == MP_OBJ_NULL ) {
44+ return STA_PROTECT ;
45+ }
46+ return 0 ;
47+ }
48+ }
49+ return STA_NODISK ;
5150}
5251
53-
54-
5552/*-----------------------------------------------------------------------*/
5653/* Inidialize a Drive */
5754/*-----------------------------------------------------------------------*/
@@ -60,29 +57,22 @@ DSTATUS disk_initialize (
6057 BYTE pdrv /* Physical drive nmuber to identify the drive */
6158)
6259{
63- DSTATUS stat = 0 ;
64-
65- switch (pdrv ) {
66- case SFLASH :
67- if (RES_OK != sflash_disk_init ()) {
68- stat = STA_NOINIT ;
69- }
70- return stat ;
71- #if MICROPY_HW_HAS_SDCARD
72- case SDCARD :
73- if (RES_OK != sd_disk_init ()) {
74- stat = STA_NOINIT ;
60+ if (pdrv == FLASH ) {
61+ if (RES_OK != sflash_disk_init ()) {
62+ return STA_NOINIT ;
7563 }
76- return stat ;
77- #endif
78- default :
79- break ;
80- }
81- return STA_NOINIT ;
64+ } else {
65+ os_fs_mount_t * mount_obj ;
66+ if ((mount_obj = osmount_find_by_volume (pdrv ))) {
67+ if (mount_obj -> writeblocks [0 ] == MP_OBJ_NULL ) {
68+ return STA_PROTECT ;
69+ }
70+ return 0 ;
71+ }
72+ }
73+ return STA_NODISK ;
8274}
8375
84-
85-
8676/*-----------------------------------------------------------------------*/
8777/* Read Sector(s) */
8878/*-----------------------------------------------------------------------*/
@@ -94,22 +84,25 @@ DRESULT disk_read (
9484 UINT count /* Number of sectors to read */
9585)
9686{
97- switch (pdrv ) {
98- case SFLASH :
99- return sflash_disk_read (buff , sector , count );
100- #if MICROPY_HW_HAS_SDCARD
101- case SDCARD :
102- return sd_disk_read (buff , sector , count );
103- #endif
104- default :
105- break ;
106- }
107-
108- return RES_PARERR ;
87+ if (pdrv == FLASH ) {
88+ return sflash_disk_read (buff , sector , count );
89+ } else {
90+ os_fs_mount_t * mount_obj ;
91+ if ((mount_obj = osmount_find_by_volume (pdrv ))) {
92+ // optimization for the built-in sd card device
93+ if (mount_obj -> device == (mp_obj_t )& pybsd_obj ) {
94+ return sd_disk_read (buff , sector , count );
95+ }
96+ mount_obj -> readblocks [2 ] = MP_OBJ_NEW_SMALL_INT (sector );
97+ mount_obj -> readblocks [3 ] = mp_obj_new_bytearray_by_ref (count * 512 , buff );
98+ return mp_obj_get_int (mp_call_method_n_kw (2 , 0 , mount_obj -> readblocks ));
99+ }
100+ // nothing mounted
101+ return RES_ERROR ;
102+ }
103+ return RES_PARERR ;
109104}
110105
111-
112-
113106/*-----------------------------------------------------------------------*/
114107/* Write Sector(s) */
115108/*-----------------------------------------------------------------------*/
@@ -122,18 +115,23 @@ DRESULT disk_write (
122115 UINT count /* Number of sectors to write */
123116)
124117{
125- switch (pdrv ) {
126- case SFLASH :
127- return sflash_disk_write (buff , sector , count );
128- #if MICROPY_HW_HAS_SDCARD
129- case SDCARD :
130- return sd_disk_write (buff , sector , count );
131- #endif
132- default :
133- break ;
134- }
135-
136- return RES_PARERR ;
118+ if (pdrv == FLASH ) {
119+ return sflash_disk_write (buff , sector , count );
120+ } else {
121+ os_fs_mount_t * mount_obj ;
122+ if ((mount_obj = osmount_find_by_volume (pdrv ))) {
123+ // optimization for the built-in sd card device
124+ if (mount_obj -> device == (mp_obj_t )& pybsd_obj ) {
125+ return sd_disk_write (buff , sector , count );
126+ }
127+ mount_obj -> writeblocks [2 ] = MP_OBJ_NEW_SMALL_INT (sector );
128+ mount_obj -> writeblocks [3 ] = mp_obj_new_bytearray_by_ref (count * 512 , (void * )buff );
129+ return mp_obj_get_int (mp_call_method_n_kw (2 , 0 , mount_obj -> writeblocks ));
130+ }
131+ // nothing mounted
132+ return RES_ERROR ;
133+ }
134+ return RES_PARERR ;
137135}
138136#endif
139137
@@ -149,41 +147,47 @@ DRESULT disk_ioctl (
149147 void * buff /* Buffer to send/receive control data */
150148)
151149{
152- switch (pdrv ) {
153- case SFLASH :
150+ if (pdrv == FLASH ) {
154151 switch (cmd ) {
155152 case CTRL_SYNC :
156153 return sflash_disk_flush ();
157154 case GET_SECTOR_COUNT :
158155 * ((DWORD * )buff ) = SFLASH_SECTOR_COUNT ;
159156 return RES_OK ;
160- break ;
161157 case GET_SECTOR_SIZE :
162- * ((WORD * )buff ) = SFLASH_SECTOR_SIZE ;
158+ * ((DWORD * )buff ) = SFLASH_SECTOR_SIZE ;
163159 return RES_OK ;
164- break ;
165160 case GET_BLOCK_SIZE :
166161 * ((DWORD * )buff ) = 1 ; // high-level sector erase size in units of the block size
167162 return RES_OK ;
168163 }
169- break ;
170- #if MICROPY_HW_HAS_SDCARD
171- case SDCARD :
172- switch (cmd ) {
173- case CTRL_SYNC :
174- return RES_OK ;
175- case GET_SECTOR_COUNT :
176- * (WORD * )buff = sd_disk_info .ulNofBlock ;
177- break ;
178- case GET_SECTOR_SIZE :
179- * (WORD * )buff = SD_SECTOR_SIZE ;
180- break ;
181- case GET_BLOCK_SIZE :
182- * ((DWORD * )buff ) = 1 ; // high-level sector erase size in units of the block size
183- return RES_OK ;
164+ } else {
165+ os_fs_mount_t * mount_obj ;
166+ if ((mount_obj = osmount_find_by_volume (pdrv ))) {
167+ switch (cmd ) {
168+ case CTRL_SYNC :
169+ if (mount_obj -> sync [0 ] != MP_OBJ_NULL ) {
170+ mp_call_method_n_kw (0 , 0 , mount_obj -> sync );
171+ }
172+ return RES_OK ;
173+ case GET_SECTOR_COUNT :
174+ // optimization for the built-in sd card device
175+ if (mount_obj -> device == (mp_obj_t )& pybsd_obj ) {
176+ * ((DWORD * )buff ) = sd_disk_info .ulNofBlock * (sd_disk_info .ulBlockSize / 512 );
177+ } else {
178+ * ((DWORD * )buff ) = mp_obj_get_int (mp_call_method_n_kw (0 , 0 , mount_obj -> count ));
179+ }
180+ return RES_OK ;
181+ case GET_SECTOR_SIZE :
182+ * ((DWORD * )buff ) = SD_SECTOR_SIZE ; // Sector size is fixed to 512 bytes, as with SD cards
183+ return RES_OK ;
184+ case GET_BLOCK_SIZE :
185+ * ((DWORD * )buff ) = 1 ; // high-level sector erase size in units of the block size
186+ return RES_OK ;
187+ }
184188 }
185- break ;
186- #endif
189+ // nothing mounted
190+ return RES_ERROR ;
187191 }
188192 return RES_PARERR ;
189193}
@@ -195,7 +199,7 @@ DWORD get_fattime (
195199)
196200{
197201 timeutils_struct_time_t tm ;
198- timeutils_seconds_since_2000_to_struct_time (pybrtc_get_seconds (), & tm );
202+ timeutils_seconds_since_2000_to_struct_time (pyb_rtc_get_seconds (), & tm );
199203
200204 return ((tm .tm_year - 1980 ) << 25 ) | ((tm .tm_mon ) << 21 ) |
201205 ((tm .tm_mday ) << 16 ) | ((tm .tm_hour ) << 11 ) |
0 commit comments