An unhandled exception of type 'System.InvalidOperationException' occurred in GHIElectronics.TinyCLR.IO.dll occurred

I get this error when I try to use the Filesystem example.
The only difference is that I use a G120 module.
In my idea it worked before:

using GHIElectronics.TinyCLR.Devices.Storage;
using GHIElectronics.TinyCLR.IO;
using System;
using System.IO;
using System.Text;

namespace Filesystem
{
public class Program
{
private static void Main()
{
var sd = StorageController.FromName(@“GHIElectronics.TinyCLR.NativeApis.LPC17.SdCardStorageController\0”);
var drive = FileSystem.Mount(sd.Hdc);

        var file = new FileStream($@"{drive.Name}Test.txt", FileMode.OpenOrCreate);
        var bytes = Encoding.UTF8.GetBytes(DateTime.UtcNow.ToString() + Environment.NewLine);

        file.Write(bytes, 0, bytes.Length);

        file.Flush();

        FileSystem.Flush(sd.Hdc);
        
    }
}

}

what line do you get error?

I get the error on the following line:
var drive = FileSystem.Mount(sd.Hdc);

Try to reformat the sdcard with FAT or FAT32

Thanks for your reaction.
I formatted the card but that did not help, so I tried the same with a Panda III board and also
with a Cobra III board. These two worked well, so I am afraid that there might be a problem with my board,
although all the other options (PWM, CAN, ADC, IO’s) work well on this board.

Is it a custom board? has it ever work with SD?

Yes, it is a custom board. It is similar to the Cobra III board (it is built around the G120 module), but it has some customized stuff integrated on it like driver IC’s , a CAN transceiver, build in leds and connectors. Funny is that it worked with the previous version (preview 3), but not anymore. I am not sure if the hardware could be damaged in some way (other features work fine as indicated before). How can I load the preview 3 firmware on the board just to make sure if that makes any difference? TinyCLR just looks for the most recent version.

I think in Preview 3 pin P1.5 was low as default, in newest version it is high as default. Try to set it low before using SD Card.

Hopefully that is the reason.

var gpio1_5 = GpioController.GetDefault().OpenPin(1*32+5);
gpio1_5.SetDriveMode(GpioPinDriveMode.Output);
gpio1_5.Write(GpioPinValue.Low);

Thanks for your reaction. Unfortunately this did not help.
What SD cards are supported with which file system? I found out on the Cobra III board that the sample code worked with a micro SD of 2Gb formatted with FAT. A micro SD of 8Gb formatted as FAT32 did not work on the Cobra III either, that gave as well the same invalid operation exception. On my custom board neither of the micro SD cards work.

If you know the system is good but a specific card doesn’t work we can certainly look into the card to see why it didn’t work. You can send us the card to check or the model number so we can grab one.

I will check the system first. But since I noticed as well that not all cards are supported in the Panda III and Cobra III, I would like to know which ones are supported. What works on those boards is a 2Gb formatted as FAT. I don’t need a lot storage space, but the small ones are getting difficult to get and are even more expensive than the one with a larger capacity.

I just re-tested the official release 1.0.0 with these cases below:

  • FEZ Panda III (without adapter)
  • FEZ Cobra III (without adapter)
  • FEZ Cobra II (with adapter)
  • G80 Development (with adapter)
    With/Without VS debug.

All the case work fine to me. The card I tested Micro Center SDHC 8G - Class 10.
Try to eject - insert for sure the socket holder is working well.

I had a misunderstanding with the developer of my customized board. I had to steer a FET open for the SD interface to get power. Writing to the board works fine now, sorry for the inconvenience.

2 Likes

Hi Dat,

Again I have problems writing the SD card.
With a small test app, it works fine, but in a bigger application I get the ‘System.InvalidOperationException’ in GHIElectronics.TinyCLR.IO.dll again.
I make use of pin P1.5 for PWM.
You mentioned this pin in your answer last year, but I don’t know what the connection is between this pin and the SD card.

No on the G120 module

And still on TinyCLR 1.0

And bigger application or bigger data that written to SD Card?

bigger app !

I just ask for sure. Because I don’t see why big app effect to File System.

I am betting it’s not the size of the app, it’s the code complexity… something cleaned up with a remaining reference, or some other code complexity issue, not the fact that it ended up in aproject that’s 100x larger