Skip to main content
Associate II
May 12, 2024
Question

USB host MSC read sector count

Hello everyone.

I am working on a project which needs low level access to USB mass storage class such as reading sector size/count of an attached storage device but thus far I could not get anything out of my board which is nucleo-f446re.

I have initialized my board as a USB host mass storage class and I am abled to write on specific sectors such as sector number 10 and it DOES write it down using msc_scsi.c functions but I can't find any solution to determine sector count of attached device. I just can't find the appropriate function/data structure for it,

Can anyone please help me with it?

This topic has been closed for replies.

11 replies

AScha.3
Super User
May 12, 2024

Hi,

i just can tell you, how i do it :

using the fatfs , start with mount ; then can use : 

f_getfree("0:", &free_cluster, &fs); // to get free clusters

free_cluster = (free_cluster * USBfs.csize ) / 2000000UL ;  // calculate free GB on device

or get/print total volume size :

sprintf(TempChar, "USB:%d GB",(int) ((USBfs.n_fatent * USBfs.csize + 500000UL) / 2000000UL));

+

If you want do everything without any filesystem (why ? cannot use card in PC anymore...)

look at fatfs functions, how the information is collected from filesystem on card:

http://elm-chan.org/fsw/ff/00index_e.html

If you feel a post has answered your question, please click on " Best Answer ".
Associate II
May 12, 2024

Thanks for your reply but I am trying to avoid any file system layer whatsoever.

I am doing it for educational purposes, I know how to use file system layer functions to get info I need but what about device layer?

Let's say you have a device with corrupted filesystem which data cannot be relied on, obviously you cannot read sector size of storage from file system layer in this scenario so there must be some other ways to read that info

AScha.3
Super User
May 12, 2024

Right, there is an info block on the device, because if you want format a corrupted file system, the format needs some info, what it should format then.

So search for this basic info - or just look at fatfs -> mkfs (= format) , what its reading from device, to get the info.

http://elm-chan.org/fsw/ff/doc/mkfs.html

here part of mkfs source :

 

 

	/* Determine where the volume to be located (b_vol, sz_vol) */
	if (_MULTI_PARTITION && part != 0) {
		/* Get partition information from partition table in the MBR */
		if (disk_read(pdrv, buf, 0, 1) != RES_OK) return FR_DISK_ERR;	/* Load MBR */
		if (ld_word(buf + BS_55AA) != 0xAA55) return FR_MKFS_ABORTED;	/* Check if MBR is valid */
		pte = buf + (MBR_Table + (part - 1) * SZ_PTE);
		if (!pte[PTE_System]) return FR_MKFS_ABORTED;	/* No partition? */
		b_vol = ld_dword(pte + PTE_StLba);		/* Get volume start sector */
		sz_vol = ld_dword(pte + PTE_SizLba);	/* Get volume size */

 

http://elm-chan.org/fsw/ff/doc/filename.html#vol

	/* Create a single-partition in this function */
		if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_vol) != RES_OK) return FR_DISK_ERR;
		b_vol = (opt & FM_SFD) ? 0 : 63;		/* Volume start sector */
		if (sz_vol < b_vol) return FR_MKFS_ABORTED;
		sz_vol -= b_vol;						/* Volume size */

 

If you feel a post has answered your question, please click on " Best Answer ".
Pavel A.
May 12, 2024

The disk layer should implement ioctl GET_SECTOR_COUNT, and also  GET_SECTOR_SIZE, GET_BLOCK_SIZE. This is how fatfs knows it.

Associate II
May 12, 2024

Thanks pal.

I guess those commands are low level USB MSC commands, right?

How can I invoke them? By what function call I mean

Tesla DeLorean
Guru
May 12, 2024

SCSI command READ CAPACITY, there should be a 10 or 16 byte CDB version

The IDENTIFY command should get Block Size

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Associate II
May 12, 2024

SCSI command READ CAPACITY doesn't work as it always returns 0.

Should I initialize something first? And if yes, how to do so?

I'm not using fatfs middleware so I guess lots of data structures are not initialized automatically.

Associate II
May 12, 2024

Also it might be worth noting that phost->pActiveClass->pData is equal to 0 which means null pointer and when I make a call to any function that uses this particular data, the micro hangs completely. That's why I think I'm missing some initialization steps.

However phost->pData is set and can be referenced, but it doesn't contain the valid data I'm looking for.