2424 * THE SOFTWARE.
2525 */
2626
27- #include "rom_fs .h"
27+ #include "access_vfs .h"
2828
2929#include "asf/common/services/usb/class/msc/device/udi_msc.h"
30- #include "storage.h"
30+ #include "extmod/fsusermount.h"
31+ #include "lib/fatfs/diskio.h"
32+ #include "py/mpconfig.h"
33+ #include "py/mpstate.h"
34+ #include "py/misc.h"
35+
36+ #define VFS_INDEX 0
3137
3238//! This function tests memory state, and starts memory initialization
3339//! @return Ctrl_status
3440//! It is ready -> CTRL_GOOD
3541//! Memory unplug -> CTRL_NO_PRESENT
3642//! Not initialized or changed -> CTRL_BUSY
3743//! An error occurred -> CTRL_FAIL
38- Ctrl_status rom_fs_test_unit_ready (void )
44+ Ctrl_status vfs_test_unit_ready (void )
3945{
40- return CTRL_GOOD ;
46+ if (VFS_INDEX >= MP_ARRAY_SIZE (MP_STATE_PORT (fs_user_mount ))) {
47+ return CTRL_FAIL ;
48+ }
49+ DSTATUS status = disk_status (VFS_INDEX );
50+ if (status == STA_NOINIT ) {
51+ return CTRL_NO_PRESENT ;
52+ }
53+
54+ return CTRL_GOOD ;
4155}
4256
4357//! This function returns the address of the last valid sector
@@ -47,30 +61,38 @@ Ctrl_status rom_fs_test_unit_ready(void)
4761//! Memory unplug -> CTRL_NO_PRESENT
4862//! Not initialized or changed -> CTRL_BUSY
4963//! An error occurred -> CTRL_FAIL
50- Ctrl_status rom_fs_read_capacity (uint32_t * uint32_t_nb_sector )
64+ Ctrl_status vfs_read_capacity (uint32_t * uint32_t_nb_sector )
5165{
52- * uint32_t_nb_sector = storage_get_block_count ();
66+ if (disk_ioctl (VFS_INDEX , GET_SECTOR_COUNT , uint32_t_nb_sector ) != RES_OK ) {
67+ return CTRL_FAIL ;
68+ }
5369 return CTRL_GOOD ;
5470}
5571
5672//! This function returns the write-protected mode
5773//!
5874//! @return true if the memory is protected
5975//!
60- bool rom_fs_wr_protect (void )
76+ bool vfs_wr_protect (void )
6177{
62- return false;
78+ DSTATUS status = disk_status (VFS_INDEX );
79+ return status == STA_NOINIT || status == STA_PROTECT ;
6380}
6481
6582//! This function informs about the memory type
6683//!
6784//! @return true if the memory is removable
6885//!
69- bool rom_fs_removal (void )
86+ bool vfs_removal (void )
7087{
7188 return true;
7289}
7390
91+ bool vfs_unload (bool unload )
92+ {
93+ return unload ;
94+ }
95+
7496// TODO(tannewt): Transfer more than a single sector at a time if we need more
7597// speed.
7698//! This function transfers the memory data to the USB MSC interface
@@ -84,11 +106,17 @@ bool rom_fs_removal(void)
84106//! Not initialized or changed -> CTRL_BUSY
85107//! An error occurred -> CTRL_FAIL
86108//!
87- Ctrl_status rom_fs_usb_read_10 (uint32_t addr , volatile uint16_t nb_sector )
109+ Ctrl_status vfs_usb_read_10 (uint32_t addr , volatile uint16_t nb_sector )
88110{
89111 uint8_t sector_buffer [FLASH_BLOCK_SIZE ];
90112 for (uint16_t sector = 0 ; sector < nb_sector ; sector ++ ) {
91- storage_read_block (sector_buffer , addr + sector );
113+ DRESULT result = disk_read (VFS_INDEX , sector_buffer , addr + sector , 1 );
114+ if (result == RES_PARERR ) {
115+ return CTRL_NO_PRESENT ;
116+ }
117+ if (result == RES_ERROR ) {
118+ return CTRL_FAIL ;
119+ }
92120 if (!udi_msc_trans_block (true, sector_buffer , FLASH_BLOCK_SIZE , NULL )) {
93121 return CTRL_FAIL ; // transfer aborted
94122 }
@@ -108,14 +136,18 @@ Ctrl_status rom_fs_usb_read_10(uint32_t addr, volatile uint16_t nb_sector)
108136//! Not initialized or changed -> CTRL_BUSY
109137//! An error occurred -> CTRL_FAIL
110138//!
111- Ctrl_status rom_fs_usb_write_10 (uint32_t addr , uint16_t nb_sector )
139+ Ctrl_status vfs_usb_write_10 (uint32_t addr , uint16_t nb_sector )
112140{
113141 uint8_t sector_buffer [FLASH_BLOCK_SIZE ];
114142 for (uint16_t sector = 0 ; sector < nb_sector ; sector ++ ) {
115143 if (!udi_msc_trans_block (false, sector_buffer , FLASH_BLOCK_SIZE , NULL )) {
116144 return CTRL_FAIL ; // transfer aborted
117145 }
118- if (!storage_write_block (sector_buffer , addr + sector )) {
146+ DRESULT result = disk_write (VFS_INDEX , sector_buffer , addr + sector , 1 );
147+ if (result == RES_PARERR ) {
148+ return CTRL_NO_PRESENT ;
149+ }
150+ if (result == RES_ERROR ) {
119151 return CTRL_FAIL ;
120152 }
121153 }
0 commit comments