Hello,
I use the Evalboard MCB4300 and µVision 5.14 MDK-ARM Professional. Is it possible to initialize MCI0 to read/write to an SD-Card using fopen()..fwrite()..fclose(), AND also allow the user to use the SD-card as a mass storage device via USB? (access not at the same time, user selects when the USB-connection is alive) ?
I'm trying to do this, but currently it does not work, but I also do not get any errors from the functions I call. I call fopen(), fwrite() fputc(), fclose() without errors, but afterwards the file on the sdcard has not changed. All function calls return without an error (fwrite(), USBD_Initialize(0),USBD_Connect(0),...) Access to the card from USB works fine.
Am I doing something wrong, or is this basically not supported?
What's wrong with this:
finit("M0:"); fmount("M0:"); f=fopen("M0:test1.txt","a"); fputs("Hello!", f); fclose (f); funmount ("M0:"); funinit("M0:"); => file is correct on SDCard
stat=finit ("M0:"); // init again => stat=7(fsDriverError) : why?
Later in the code there would follow calls to ustat=USBD_Initialize (0); /* USB Device 0 Initialization */ ustat=USBD_Connect (0);
Thank you.
Hello, I tried this in a slightly different way, still without success. Access to the SD-Card via USB works as expected.
I took the example "USB device Mass Storage (MBC4300)" and modified the mail loop:
finit ("M0:"); /* Initialize SD Card 0 */ fmount ("M0:"); /* Mount SD Card 0 */ USBD_Initialize (0); /* USB Device 0 Initialization */ USBD_Connect (0); /* USB Device 0 Connect */ while (1) { osDelay(10); // added code start: if(button_pressed() ) { USBD_Disconnect (0); USBD_Uninitialize(0); // this fails, stays in an endless loop, see below f=fopen("M0:test4.txt","a"); fprintf(f,"data written\n"); fclose (f); USBD_Initialize(0); USBD_Connect(0); } // added code end }
USBD_Uninitialize(0) never returns: in file USBD0_LPC43xx.c:
// Disable USB0 register interface clock LPC_CCU1->CLK_M4_USB1_CFG &= ~1; while (LPC_CCU1->CLK_M4_USB0_STAT & 1); // endless loop here
If USBD_Uninitialize(0) would not fail: Could this method (disconnect/disable USB to allow write access to SD-Card from firmware) work?
Any idea why USBD_Uninitialize() fails?
Without the surrounding USBD_Uninitialize()/USBD_Initialize() the code is executed, fopen() and fprintf() and fclose() do not return any errors, but the file on the SD-card is not modified.
Hello, I observe your post, because I am interested in getting an answer, too.
My proposal without testing it: Perhaps it is not neccessary to call funinit and USBD_Uninitialize(0). Did you try following code?
// added code start: if(button_pressed() ) { USBD_Disconnect (0); fmount("M0:") f=fopen("M0:test4.txt","a"); fprintf(f,"data written\n"); fclose (f); funmount("M0:"); USBD_Initialize(0); USBD_Connect(0); }
Hello, several problem were solved after installation of yesterday's update (Middleware 6.3.0, CMSIS 4.3.0, Compiler 1.0.0)
My testcode is based on the latest USB Device Mass storage example. I added the Board Support "Buttons" and modified the main.c function. At poweron a file test1.txt is generated (overwritten in case it was already present) Each time button P4.0 is pressed another file test2.txt is modified (one line added) USB-Connection can be present or absent. If a file is updated while the usb-cable is plugged in, it is required to remove/add the usb-cable to see the change in the file system. This is what my current test code looks like and it seems to work correctly (although I'm not sure that every code line is required...:-) ):
int main (void) { GLCD_Initialize (); GLCD_SetBackgroundColor (GLCD_COLOR_BLUE); GLCD_SetForegroundColor (GLCD_COLOR_WHITE); GLCD_ClearScreen (); GLCD_SetFont (&GLCD_Font_16x24); GLCD_DrawString (0, 0*24, " USB Device "); GLCD_DrawString (0, 1*24, " MSC Class "); GLCD_DrawString (0, 2*24, "Mass Storage Example"); GLCD_DrawString (0, 4*24, "USB0: SD Card Disk "); GLCD_DrawString (0, 5*24, "USB1: Memory Disk "); GLCD_DrawString (0, 8*24, " Keil Tools by ARM "); GLCD_DrawString (0, 9*24, " http://www.keil.com "); Buttons_Initialize(); finit ("M0:"); fmount ("M0:"); USBD_Initialize (0); USBD_Connect (0); /* USB Device 0 Connect */ funmount ("M0:"); // x1 finit ("M0:"); fmount ("M0:"); f=fopen("M0:test1.txt","w"); // erzeuge leere datei fprintf(f,"Hello SD-Card!\n"); fflush(f); fclose (f); funmount ("M0:"); // x2 finit ("M0:"); fmount ("M0:"); while (1) { static int button; osDelay(10); button<<=1; button|=(Buttons_GetState()&1); if(button==1) { funmount ("M0:"); // x3 finit ("M0:"); fmount ("M0:"); f=fopen("M0:test2.txt","a"); fprintf(f,"Hello Test %d!\n",n++); fflush(f); fclose (f); funmount ("M0:"); // x4 finit ("M0:"); fmount ("M0:"); } } }
x1: I think this unmout(), init(), mount() sequence might be required because: "USBD_Initialize()" calles the function "USBD_MSC0_Initialize()". In this file I find the following code line:
usbd_msc0_media_own = MEDIA_OWN_USB; // Initially media is owned by USB
I want to access the sdcard via fopen(), fprintf() fclose() therefor I want to totally "disconnect" the usb-connection.
x2: If I omit this, the following fprintf-sequence (when the button is pressed) writes another file "test2.txt", but the first file "test1.txt" gets sometimes corrupted.
x3: same reason as x1 x4: not sure if it is required...
Thanks for the detail Information,
At this Moment I solely use the filesystem for access. But I am going to implement shared access to sdCard in the future. Perhaps with Ethernet instead of usb, or both.
It is good to have a working starting point, and to know that other users are confused with Keils Middleware, too. :-)
Please keep update this post.
Gruß Thomas
Hi,
I found an Application Note 273, Creating a USB Data Logger Application using Middleware and CMSIS.
Here I found 2 functions: USBD_MSC0_SetMediaOwnerUSB() and USBD_MSC0_SetMediaOwnerFS() Using something like this seems to be the correct was to swap the ownership and access the memory card either via USB MSC or file system.
Gunnar, you found the solution to that problem. Please note that we have implemented the Media Ownership control in the Middleware 6.3.0
Refer also to: www.keil.com/.../group__usbd__msc_functions.html under headline: Media Ownership
Let us know if you have further questions. Reinhard