cancel
Showing results for 
Search instead for 
Did you mean: 

FR_Invalid_Object error - STM32L4R5ZI Nucleo Board

JayDev
Senior II

I am currently trying to get an SD card working with SPI on an STM32L4R5ZI Nucleo board, using STM32CubeIDE V1.13.2 using the latest L4 embedded package. 

I currently have my code opening the card and even opening the file (I receive FR_OK for both, even tested with invalid directories/files to verify they fail with the incorrect data).  However, when I try to read from the SD card, I receive FR_INVALID_OBJECT code, which seems to be described here:

FR_INVALID_OBJECT
The file/directory object is invalid or a null pointer is given. There are some reasons as follows:
It has been closed, or the structure has been collapsed.
It has been invalidated. Open objects on the volume are invalidated by voulme mount process.
Physical drive is not ready to work due to a media removal.

I have tried changing SD card formats (originally on a 128GB FAT32 SD card and then to a 32GB exFAT formatted card), as I had read FATFS can have issues with FAT32.  I do have FS_EXFAT enabled in the options.  

With the f_mount and f_open commands, I can see the SPI commands being sent across on my scope, so I know it's communicating at the very least.  With the f_read command, no SPI commands are being set.  It seems to be setting the invalid object on this line of f_open:

FRESULT f_read (
	FIL* fp, 	/* Pointer to the file object */
	void* buff,	/* Pointer to data buffer */
	UINT btr,	/* Number of bytes to read */
	UINT* br	/* Pointer to number of bytes read */
)
{
	FRESULT res;
	FATFS *fs;
	DWORD clst, sect;
	FSIZE_t remain;
	UINT rcnt, cc, csect;
	BYTE *rbuff = (BYTE*)buff;


	*br = 0;	/* Clear read byte counter */
	res = validate(&fp->obj, &fs);				/* ERROR RETURNED ON THIS LINE - Check validity of the file object */

Is it failing before it's even tried communicating with the chip?  I'm not sure.  Currently, my main code is pretty basic so I'll just post it here for reference:

FATFS       FatFs;                //Fatfs handle
  FRESULT     fres;                 //Result after operations
  FIL 		  test_file;

  uint16_t bytes_read = 0;
  uint16_t numBytes = 4096;
//  uint8_t in_byte_array[numBytes] = {0};
  uint8_t in_byte_array[4096] = {0};

  char test_file_fn[256] ="";
  sprintf(test_file_fn, "abcd.txt");

  HAL_Delay(500);
  fres = f_mount(&FatFs, "", 1);
  fres = f_open(&test_file, test_file_fn, FA_OPEN_EXISTING | FA_READ);
  fres = f_read(&test_file, &in_byte_array, 4, &bytes_read);

Let me know if there's anything else you'd recommend checking or if I'm missing something obvious.  I think I'm pretty close to getting this working but have been struggling to find a solution for this and thought I'd check here to see if anyone has any advice.  Thanks!

2 REPLIES 2

Make sure to clear auto/local variables and structures.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..

Sorry, I'm not entirely sure I understand what you're referring to regarding local variables/structures.  Are you referring to the buffers I'm filling?

I was working on this a bit more today, took a few different approaches but I did go back to some old code I had that was writing to an SD card on a different board.  I verified I could write data to the board without issue and that the code was working as expected.  It looks like that code is able to mount, open, and write to the SD card but the same issue exists when trying to use the f_read command, so I'm not sure what the problem is.  

I know STM uses V12C of the FATFS library but I don't see any glaring issues in the change notes (and I can't be the first person who has tried using f_read).  I must be doing something wrong but I can't seem to see the issue.  I've tried building it a couple different ways (using different diskio/app_fatfs approaches) but the end result seems to be about the same.  Any idea what I might be doing wrong?  Is it staring me in the face and I'm looking right past it?  I have to think it's not a library related issue or we'd see a lot more posts on the board about it.  

Thanks for your help!