Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade block protocol to use ioctl method, and support fatfs sectors > 512 bytes #1830

Merged
merged 2 commits into from
Feb 10, 2016

Conversation

dpgeorge
Copy link
Member

@dpgeorge dpgeorge commented Feb 9, 2016

This PR adds the ioctl method to the block protocol, and retains backwards compatibility with the old version (which would now be deprecated).

New protocol consists of:

  • readblocks(self, n, buf)
  • writeblocks(self, n, buf)
  • ioctl(self, cmd); 0=sync, 1=sec_count, 2=sec_size

Some open questions:

  1. What values to use for the cmd? Maybe 0 should be reserved to catch errors.
  2. For now these numbers can be "magic" but would somehow need to define them as constants on the C and Python side.
  3. Should ioctl take a third argument? If so we should do it now so that ioctl implementations don't need to be changed later.

@dpgeorge
Copy link
Member Author

dpgeorge commented Feb 9, 2016

This has been tested on pyboard with a RAMFS driver.

case GET_SECTOR_SIZE: {
vfs->u.ioctl[2] = MP_OBJ_NEW_SMALL_INT(2 /* SEC_SIZE */);
mp_obj_t ret = mp_call_method_n_kw(1, 0, vfs->u.ioctl);
*((WORD*)buff) = mp_obj_get_int(ret);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pfalcon in your PR you had this as a DWORD, but I think it should be a WORD pointer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I just copy-pasted it, thinking that return arg size is standard in all cases, but it doesn't have to.

@pfalcon
Copy link
Contributor

pfalcon commented Feb 9, 2016

Look good, thanks!

What values to use for the cmd? Maybe 0 should be reserved to catch errors.

Do you remember my idea that some well-known value should be used for "query interface" call? That can be 0, or 1 (with 0 still unused).

For now these numbers can be "magic" but would somehow need to define them as constants on the C and Python side.

Yes (can wait).

Should ioctl take a third argument? If so we should do it now so that ioctl implementations don't need to be changed later.

Yes, let's have standard ioctl format of ioctl(req_no, arg) (plus self).

@dpgeorge
Copy link
Member Author

dpgeorge commented Feb 9, 2016

Ok, I'll change it so cmd=0 is invalid, and ioctl is ioctl(self, cmd, arg).

As for the ioctl cmds, we could add an INIT command to tell the device to power up, as well as UNMOUNT so it can power down if it wants. Note that fatfs supports initialisation of drives, so INIT would be called from this location (disk_initialize).

We could make it so INIT returned a tuple of sec_size and sec_count, instead of separate cmds for getting these values.

Thoughts?

@pfalcon
Copy link
Contributor

pfalcon commented Feb 9, 2016

We could make it so INIT returned a tuple of sec_size and sec_count, instead of separate cmds for getting these values.

This kind of questions is hard to answer before we integrated a dozen of FSes. I'd say stay close to actual FS API we have at hand, and be ready to refactor it all. But even without it, I don't see why you want to tie sector number and sector size into one call. Consider for example that getting "sector number" for a type may require rewinding it, then you want that rewinding to happen twice.

The new block protocol is:
- readblocks(self, n, buf)
- writeblocks(self, n, buf)
- ioctl(self, cmd, arg)

The new ioctl method handles the old sync and count methods, as well as
a new "get sector size" method.

The old protocol is still supported, and used if the device doesn't have
the ioctl method.
If MICROPY_FATFS_MAX_SS is defined to power of 2 value between 1024 and
4096, support for dynamic sector size in FatFs will be enabled.  Note
that FatFs reserves static buffer of MICROPY_FATFS_MAX_SS size for each
filesystem in use, so that value should be set sparingly.

Initial patch provided by @pfalcon.
@dpgeorge
Copy link
Member Author

Ok, this has been updated:

  • ioctl now takes extra arg: ioctl(self, cmd, arg)
  • constant macros have been added to diskio.c for the ioctl commands
  • commands have been renumbered to start at 1, and 1 and 2 are reserved for INIT and DEINIT (because ultimately I want to be able to switch the flash and sd devices in stmhal to use this new interface).

Ok to merge?

@pfalcon
Copy link
Contributor

pfalcon commented Feb 10, 2016

Ok to merge?

Yes, thanks, please go ahead!

@dpgeorge dpgeorge merged commit 13a4c12 into micropython:master Feb 10, 2016
@dpgeorge dpgeorge deleted the block-protocol branch July 5, 2022 05:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants