This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Access to SD-card from µController AND from USB possible?

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.

Parents
  • 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...

Reply
  • 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...

Children