SD Card interfacing to MKL25Z128VLK4 microcontroller

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SD Card interfacing to MKL25Z128VLK4 microcontroller

3,999 Views
swathysen
Contributor I

Hi

I am using Freescale FRDM-KL25Z board and OpenSDA on the board for debugging and flashing. I would like to interface a SD card to my micro controller and send data through SPI to the SD card after creating a file in it. Can anyone provide me with a sample code for the same as I don't have any idea how to do so. I have ensured that SPI module is working fine. So I would like to have a working code for writing the data in a file in SD card. I do not use Processor Expert and hence I am not able to refer to the example given in the forum.I use Keil as the IDE.Kindly respond at earliest.

Thanks and regards

Swathy

0 Kudos
14 Replies

1,767 Views
isabelbenitez
Contributor II

Hi, I have made it yet for the MK60, I can pass you my files. I have made it with spi and it works fot FAT16, FAT32 and for SDHC too (4GBytes).

0 Kudos

1,767 Views
swathysen
Contributor I

Hi Maria Benitez,


Thanks a lot for your reply.I would really appreciate it if you could send me the files you have made. My mail id is swathysen.2690@gmail.com

0 Kudos

1,767 Views
mjbcswitzerland
Specialist V

Hi

Note that the KL family has a different SPI interface than the K family has so K60 code will not work on the FRDM-KL25Z until (at least) the interface is correspondingly modified.

Note that USB-MSD/SD-card  loaders are available for most Freescale boards according to Kinetis Boot Loader for SREC UART, SD Card and USB-MSD loading (including the FRDM-KL25Z). However, since the FRDM-KL25Z doesn't have an SD card adapter on it, the SD card function is disabled in the present one.

Please tell me how you are are connecting the SD-card and I can activate a configuration for this so that you can test it. The application also included USB-MSD to the SD card so read/write speed can be tested and the SD-card used as a mass stoarge device to a PC. If you haven't prepared any hardware yet, I suggest using the following connections which will be the same as the one that I use on this board.

[VDD J9-4 / 0V J9-14]

SPI1_CS      PTE-4 (J9-13)

SPI1_SCK    PTE-2 (J9-9)

SPI1_MOSI   PTE-1 (J2-20)

SPI1_MISO   PTE-3 (J9-11)

card detect input on PTB-8 (J9-1)

write protect input on PTB-9 (J9-3)

Regards

Mark

P.S. If you are learing about SD card interfacing via SPI there are full details about it here: http://www.utasker.com/docs/uTasker/uTasker_utFAT.PDF
The project builds with Keil (as well as GCC, IAR, Codewarrior, Rowley Crossworks and Atollic) for KL or K processors (via SPI or SDHC) - more details at http://www.uTasker.com

0 Kudos

1,767 Views
swathysen
Contributor I

Hi Mark Butcher

I have an add on board with SD card slot and all the necessary pins MISO MOSI SCK and CS.I have chosen SPI0 on MKL25 as the master and interconnected the corresponding pins on both boards together.I was trying to initialize and write commands to SD card to read and write sectors via SPI.But am still not able to communicate with the card.

0 Kudos

1,767 Views
mjbcswitzerland
Specialist V

Hi

You need to be more specific concerning the pin connections. SPI0 can be multiplexed on 3 different sets of pins and the MOSI and MISO directions can then also be defined differently on each. Which pin numbers are the 4 SPI lines connected to and do you connect write protect and card detect switches?

Regards

Mark

0 Kudos

1,767 Views
swathysen
Contributor I

Hi Mark Butcher

Thanks a lot for your help and am sorry for being vague. I have changed my SPI from SPI0 to SPI1 to achieve better speed while interacting with SD card. I am using pins PTD4, PTD5,PTD6,PTD7 as SPI1 PCS,SCK,MOSI and MISO respectively. After editing and modifying a sample code for a ST micro controller I am now able to communicate with the sd card and write into and read from raw sectors. I am now working on creating a FAT 32 file system on the card to create and write into files. So I would be really grateful if you could provide me with some sample code for KL25z or any other related microcontrollers.

0 Kudos

1,767 Views
mjbcswitzerland
Specialist V

Hi

I set up the interface as you specified for this board and built an application with USB-MSD to operate with the connected SD card. This means that connecting the board via USB to the PC (with formatted SD card inserted) causes it to appear as a hard disk to the PC (files can be read and written)

In addition, I put a local FAT32 interface (DOS-like) on the UART (via the OpenSDA virtual COM port) so that the directories on the SD card can be browsed directly. The details of the card can be seen and individual sectors read.

The SREC is here so that you can load it to your FRDM-KL25Z to verify that it operates suitably: http://www.uTasker.com/freescale/uTasker_V1.4_FRDM-KL25Z_SD_CARD.srec

Good luck!

Regards

Mark

1,767 Views
swathysen
Contributor I

Hi

I have loaded the srec file into FRDM-KL25z as you have said and I could see  my SD card as a hard disk on PC.Thanks a lot. My actual requirement is something different. I would like to store some data read by the TSI sensors into the SD card. I was able to communicate with the SD card via SPI by writing a code and could read and write into raw sectors of the SD card. Now I would like to create a file system on the SD card and create a file on the SD card and store the data from my TSI sensors in it by writing a code. So if you could provide me with a sample code of how to create a file system on the SD card and to create files and write in it, I can integrate it with my code. I use Keil as the IDE,CMSIS-DAP as the debugger and use Open SDA on board for debugging.

Thanks and regards

Swathy

0 Kudos

1,767 Views
mjbcswitzerland
Specialist V

Hi

The SREC contains FAT and an interface to use it, as well as the USB-MSD functionality. The USB-MSD functionality is generally useful for retrieving files later to a PC, rather than having to remove the physical SD card and inserting it into a reader so that the file(s) can be transfered to the PC.

If you use the OpenSDA's virtual COM port and connect at 115k with a terminal emulator you communicate with the interface. Below is a recording of moving to the SD card menu, creating a file, adding some data to it and printing the content.

Project code to save to a file (such as your requirement to store data read by TSI sensors) requires a file to be opened (or created), the data written to it each time something is to be saved and, when finished, the file to be closed (possibly optional). Therefore the application interface is straight-forward.

In my case I built the project with uTasker, which contains everything needed to do these things and configured for your board and tested in its simulator. Since I played around with the FRDM-KL25Z yesterday I also posted some details here: Having fun with the FRDM-K25Z

It took about 10 minutes to configure and test the project and then I could generate the SREC with any of a number of compilers.

If you prefer to build each layer up yourself you can either use the Microsoft FAT specification http://www.microsoft.com/whdc/system/platform/firmw are/fatgen.mspx to develop the FAT (there are also a lot of practical FAT detaisl in http://www.utasker.com/docs/uTasker/uTasker_utFAT.PDF) or start with an open source project such as FsFAT http://elm-chan.org/fsw/ff/00index_e.html, add the HW interface and port to your environment.

Regards

Mark

Example of command line interface and FAT menu use:

Serial number: 00-00

Software version V1.4.004

Device identification: KINETIS

     Main menu

===================

1              Configure LAN interface

2              Configure serial interface

3              Go to I/O menu

4              Go to administration menu

5              Go to overview/statistics menu

6              Go to USB menu

7              Go to I2C menu

8              Go to SD-Card disk interface

9              FTP client commands

a              CAN commands

help           Display menu specific help

quit           Leave command mode

8

  Disk interface

===================

up           go to main menu

info         utFAT/card info

dir          [path] show directory content

dird         [path] show deleted directory content

dirc         [path] show corrupted directory content

infof        [path] show file info

infod        [path] show deleted info

cd           [path] change dir. (.. for up)

file         [path] new empty file

write        [path] test write to file

mkdir        new empty dir

rename       [from] [to] rename

trunc        truncate to [length] [path]

print        [path] print file content

del          [path] delete file or dir.

format       [-16] [label] format (unformatted) disk

re-format    [-16] [label] reformat disk!!!!!

sect         [hex number] display sector

sectw        [hex no.] [patt.] [cnt]

help         Display menu specific help

quit         Leave command mode

>info

SD-card NO NAME (4124049408 bytes) SDHC

Bytes per sector: 512

Cluster size: 4096

Directory base: 0x00000002

FAT start: 0x0000043b

FAT size: 0x00001e02

Number of FATs: 2

LBA: 0x0000403f

Total clusters: 0x000f00f8

Info sect: 0x00000040

Free clusters: 0x000ec7fd

Next free: 0x00001184

CSD: 0x40 0x0e 0x00 0x32 0x5b 0x59 0x00 0x00 0x1e 0xb9 0x7f 0x80 0x0a 0x40 0x00

0xc7

>dir

Directory D:\

---A 03.11.2013  22:35             1370 cache.manifest

---- 03.11.2013  22:35 <DIR>            Run

---- 14.01.2014  14:27 <DIR>            setup

---- 17.01.2014  02:05 <DIR>            utFAT

---A 17.01.2014  01:16            80700 software3.bin

---A 18.01.2014  03:19            80684 software.bin

---- 07.03.2014  01:52 <DIR>            test

4 files with 263455 bytes

4 directories, 3967799296 bytes free

D:\>

D:\>file myFile.txt

D:\>write myFile.txt

New file length = 256

D:\>print myFile.txt

UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU

UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU

UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU

UUUUUUUUUUUUUUUU

D:\>dir

Directory D:\

---A 03.11.2013  22:35             1370 cache.manifest

---- 03.11.2013  22:35 <DIR>            Run

---- 14.01.2014  14:27 <DIR>            setup

---- 17.01.2014  02:05 <DIR>            utFAT

---A 03.11.2013  22:35              256 myFile.txt

---A 17.01.2014  01:16            80700 software3.bin

---A 18.01.2014  03:19            80684 software.bin

---- 07.03.2014  01:52 <DIR>            test

5 files with 263711 bytes

4 directories, 3967795200 bytes free

D:\>

1,767 Views
swathysen
Contributor I

Hi

I am finally able to implement a FAT32 module in SD card with the help of a code i got from a site.The Microsoft FAT specification document from  http://www.microsoft.com/whdc/system/platform/firmw are/fatgen.mspx was really helpful in understanding the code. Thanks a lot for bringing the document to my notice.

Thanks and Regards

Swathy

0 Kudos

1,767 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Swathy Sen,

Actually there is no ready SD card demo based on FRDM-KL25Z, but the MQX 4.0.2 has similar examples for such kind of application. Please kindly refer to C:\Program Files\Freescale\MQX_4_0_2\mfs\examples\sdcard\uv4 for details. You will find SDCARD demos for Keil.

1.PNG.png

Hope that helps,

B.R

Kan

0 Kudos

1,767 Views
swathysen
Contributor I

Hi

Thank you for your reply. I do not have such a folder or file path in my system. I have installed Keil in Disk D. I have a path D:\Keil\ARM\Boards\Freescale\KWIKSTIK\. In the same path instead there is a FlexMem_Cfg folder. Are they by any chance the same?

0 Kudos

1,767 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Swathy Sen,

You have to download MQX 4.0.2 firstly, and will find such a folder like that after installation . Please kindly refer to MQX: Freescale MQX™ Software Solutions for more details.

Hope that helps,

B.R

Kan

0 Kudos

1,767 Views
swathysen
Contributor I

Hi Kan_Li

I will download MQX 4.0.2 and see once.Thanks a lot.

0 Kudos