How to add SD card support (SPI) to usb composite msc cdc example?

cancel
Showing results for 
Search instead for 
Did you mean: 

How to add SD card support (SPI) to usb composite msc cdc example?

1,373 Views
joe_dimaggio
Contributor II

Here my configuration:

KDS 3.2

KSDK 2.0

KL27 uC

 

My project is a datalogger based on the usb example composite msc cdc.

Thank to Kerry Zhou I ported FatFs + SD card (SPI) to my project.

Now I need to assign the SD card storage to the MSC, can anyone give me some advices?

Labels (1)
Tags (1)
4 Replies

430 Views
isaacavila
NXP Employee
NXP Employee

Hello Joe,

For KSDK 2.0, there is a middleware for memories like SD cards and MMC. If you look at: <KSDK_2_0_PATH>\middleware\sdmmc_2.0.0 there is folder that include basic support for these memories through different interfaces such as SDHC and SPI (It depends on which MCU you are using, you may be able to use any of these interfaces).

This middleware is intended to provide the layer where communication with cards is performed, however, user should create its own peripherals APIs for configuring baud rate, module/pin initialization, sending/reading data through desired interface (SPI).

Basically, you'll need to add these files for your project:

Files for SPI.jpg

Then, you will have to create functions needed for this middleware to communicate through SPI:

/*! @brief SDSPI host state. */
typedef struct _sdspi_host
{
    uint32_t busBaudRate; /*!< Bus baud rate */

    status_t (*setFrequency)(uint32_t frequency);                   /*!< Set frequency of SPI */
    status_t (*exchange)(uint8_t *in, uint8_t *out, uint32_t size); /*!< Exchange data over SPI */
    uint32_t (*getCurrentMilliseconds)(void);                       /*!< Get current time in milliseconds */
} sdspi_host_t;

These functions are:

  • One function that will configure SPI to desired frequency/baud rate.
  • One function to write/read from SPI and take/save data from/to in/out buffers.
  • One function to get current milliseconds count. Some functions for SD cards manage timeouts and it is needed a "timing" function.

After these functions are defined, driver for SD cards through SPI might be complete.

Next step is to call these APIs every time that a read/write procedure for USB MSD interface is needed. For this, you can use the USB MSD SD card example available for FRDM-K64F to check the exact procedure that application takes to initialize/communicate over USB. This example is available at: <SDK_2.0_FRDM-K64F_PATH>\boards\frdmk64f\usb_examples\usb_device_msc_sdcard\bm.

For example, there is a section where SD is initialized, and you will need to replace some functions inside this USB_DeviceMscSdhcInit to perform communication with SD through SPI interface. Some of these functions are already defined in the fsl_sdspi.h driver that were included before.

SD Card initialization.jpg

And in MSD's callback, you must replace APIs that write/read to/from SD, to those functions that are already defined in fsl_sdspi.h:

Replace SD Card APIs in USB callback.jpg

This should be the best way to implement SD card support on USB MSD + CDC example. (I'd strongly recommend to implement this on USB MSD device example before adapting to MSD + CDC example)

i hope this can help you!

Best Regards,

Isaac

0 Kudos

430 Views
joe_dimaggio
Contributor II

Thanks for the reply, I didn't use the middleware sd files you mentioned because I got to many compiler errors, as the KL27 doesn't have the sdhc component.

Now I can write to the SD card using my application and the SD card is showed in the PC.

I used following instructions for that:

Porting FatFs file system to KL26 SPI SD card code

After porting this code I modified the MSD's callback, now it works.

430 Views
arthshah
Contributor II

hello joe_

i am using FRDM KL25z, i have same problem like yours.

i want to write and  read from SD card and SD card show as a MSD.

help me for that.

Thank you.

0 Kudos

430 Views
joe_dimaggio
Contributor II

Iva Dorazinova  wrote that there is the usb_device_composite_cdc_msc_sdcard_lite example, but I didn't foud it.

There is a "composite cdc msc" and a "msc sd card" example, but there are to many differences between these two examples so I can't merge them together.

What's the best way to go further?

0 Kudos