RP2040 USB host question

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Post Reply
User avatar
heliophagus
 
Posts: 27
Joined: Sun May 27, 2012 8:59 pm

RP2040 USB host question

Post by heliophagus »

Hi! I want to use the RP2040 USB host as part of a datalogger with a USB thumb drive & have an embarrassingly n00b question.

In the datalogger example, it seems that fatfs is defined as a FatVolume from SdFat, & f_log is defined as File32. f_log is then opened so:

Code: Select all

f_log = fatfs.open(LOG_FILE, O_WRITE | O_APPEND | O_CREAT);
and then closed with

Code: Select all

f_log.close();
Unfortunately I can't find a list of available commands & their parameters (other than the above code) anywhere. I've Googled & Kagi'd for over a day with no results. Basically, I'd like to search for a filename on the USB, & if it exists, choose another candidate filename until I find a unique name. Then, open that & write (append) to it for a few minutes or hours. Then, finally, close the file. Very simple!

I'd HUGELY appreciate a link to the available commands - for example, a Boolean function that returns whether a given filename exists.

BTW, Am I correct in assuming that filenames are limited to the 8.3 format?

Thanks in advance! --- John

User avatar
T_Mo
 
Posts: 289
Joined: Thu Mar 15, 2018 7:10 pm

Re: RP2040 USB host question

Post by T_Mo »

Are you referring to this product?
https://www.adafruit.com/product/5723

User avatar
heliophagus
 
Posts: 27
Joined: Sun May 27, 2012 8:59 pm

Re: RP2040 USB host question

Post by heliophagus »

Yes indeed! I was lucky enough to get one before they ran out of stock. It sees the supply chain issues are still with us.

It's been a while since I've been down in the trenches with coding, & had lost track of the fact that the Adafruit TinyUSB library inherits its commands etc. from the SdFat library, so I'm wading through the swamps of the SdFile.h include. It would be great if an informative text intro to the commands existed, with examples. But if that's not the case, I'll eventually get things working. Though at the cost of many hours of trial-and-error.

If anyone knows of a good intro to the SD/USB file commands with examples, please let me know! I'm certainly not the only person who could use a guide to this area.

User avatar
adafruit_support_mike
 
Posts: 67734
Joined: Thu Feb 11, 2010 2:51 pm

Re: RP2040 USB host question

Post by adafruit_support_mike »

In CircuitPython, the SDFat interface lives in the 'storage' module:

https://docs.circuitpython.org/en/lates ... age.VfsFat

To test if a file exists, you can use the 'os' module and the .stat() command:

https://docs.circuitpython.org/en/lates ... ml#os.stat

If the file exists you'll get a data structure of metadata about it. If it doesn't exist the interpreter will thrown an OSError exception:

Code: Select all

import os

def file_existsP( f ):
	try:
		e = os.stat( f )
		result = True
	except:
		result = False
	return result
The function name ends with P because I code with a slight LISP.. test functions like that are formally known as 'predicates' and the captial P is as close as most languages will let you get to putting a question mark in a symbol name.

User avatar
heliophagus
 
Posts: 27
Joined: Sun May 27, 2012 8:59 pm

Re: RP2040 USB host question

Post by heliophagus »

Thanks, but I should have made it clearer that I am using the standard Arduino language.

User avatar
adafruit_support_mike
 
Posts: 67734
Joined: Thu Feb 11, 2010 2:51 pm

Re: RP2040 USB host question

Post by adafruit_support_mike »

In that case you'll want the SDFat library documentation:

https://github.com/greiman/SdFat

Post Reply
Please be positive and constructive with your questions and comments.

Return to “Arduino”