element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Freedom development platform
  • Products
  • Dev Tools
  • Freedom development platform
  • More
  • Cancel
Freedom development platform
Documents Interfacing FRDM-KL25Z with Arduino Ethernet Shield R3 on Processor Expert, Part 1: SD Card
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Freedom development platform to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Engagement
  • Author Author: bheemarao
  • Date Created: 11 Mar 2014 7:33 AM Date Created
  • Last Updated Last Updated: 18 Mar 2014 1:27 PM
  • Views 909 views
  • Likes 0 likes
  • Comments 0 comments
Related
Recommended

Interfacing FRDM-KL25Z with Arduino Ethernet Shield R3 on Processor Expert, Part 1: SD Card

This tutorial was extracted from Erich Styger blog http://mcuoneclipse.wordpress.com with his agreement.

Sometimes it takes a very long time to realize a project. Adding the Arduino Ethernet Shield R3Arduino Ethernet Shield R3 to one of my Freescale FRDM boards is one of it: it took me a year until I have found a few days to work on using the Ethernet Shield with my FRDM-KL25ZFRDM-KL25Z.

 

                                                           FRDM-KL25Z with Ethernet Shield

                                                                                                    FRDM-KL25Z with Ethernet Shield

I have not everything in place yet, so I decided to publish things in parts. So this is about part one: using the Micro SD Card on the Shield.

 

List of Tutorials

  1. FRDM with Arduino Ethernet Shield R3, Part 1: SD Card
  2. FRDM with Arduino Ethernet Shield R3, Part 2: Ping
  3. FRDM with Arduino Ethernet Shield R3, Part 3: Embedded Web Server

Arduino Ethernet Shield

The Arduino Ethernet Shield R3 has two main features:

 

                                                    Arduino Ethernet Shield with FRDM-KL25Z

 

                                                                             Arduino Ethernet Shield with FRDM-KL25Z

 

  1. The Wiznet W5100 Ethernet Chip which makes an Ethernet connection possible for small microcontrollers. The W5100 has a Ethernet PHY and a microcontroller can use a simple socket API over SPI to communicate over Ethernet.
  2. A Micro-SD card socket which is on the same SPI bus as the W5100 chip. The SD card can be used for data logging or to host web pages/etc.

There might be better or less expensive solutions to connect a microcontroller to the Internet, but that Arduino Ethernet Shield is very affordable and easy to use. The shield is available for less than $15 from many online vendors. There is the option to add a PoE (Power over Ethernet) to the shield too (I don’t have this option).The schematics and Eagle files of the shield are available on the official Arduino site here. There are different versions of the shield: earlier versions had problems with the W5100 and SPI signals. I’m using the ’06 R3′ version which to my knowledge is the latest version.


Hardware Modification

Both the SD card and the W5100 are connected with SPI to the microcontroller. The shield is using the Arduino programming (SPI) adapter, and the SPI MISO/MOSI/CLK signals are *not* connected to the Arduino headers! So to have it working with my FRDM boards, I have to route the signals to the header.


Removal of SPI Programming Header

First, I removed the 2×3 SPI programming socket on the bottom side of the board:

 

                                                       image

                                                       Remove SPI Programming Header

 

 

Connecting SPI Signals to Header

Next, I wired the SPI Signals MISO, MOSI and CLK to the Arduino header pins:


                                                         image

                                                                                                  Routing SPI Signals

Pin Assignments

With this, I have all the SPI Signals on the header:


  • Pin 13: CLK (SPI Clock) (to FRDM-KL2Z: PTD1)
  • Pin 12: MISO (to FRDM-KL2Z: PTD3)
  • Pin 11: MOSI (to FRDM-KL2Z: PTD2)
  • Pin 10: ETH CS (Chip Select of W5100) (to FRDM-KL2Z: PTD0)
  • Pin: 4: SC CS (Chip Select of SD Card) (to FRDM-KL2Z: PTA4)

 

With this, I can nicely check the signals with a logic analyzer too:

                                                       SPI Signal Connections

                                                                                                                        SPI Signal Connections

Application with SD Card Driver

For my project I’m using FatFS with FreeRTOS. Using multiple Processor Expert components the project is easily built up like with Lego bricks:

                                                                      Processor Expert Components for Ethernet Shield

                                                                                Processor Expert Components for Ethernet Shield

Concurrent Access to SPI Bus

Because both the SD card and the W5100 are using the same SPI bus, I need to make sure that access to it is protected with a critical section. For this I use a FreeRTOS semaphore.

First, I enable in FatFS reentrant and protected access to the file system:

                                                       Reentrant FatFS

                                                                                                              Reentrant FatFS

This not only gives me fully reentrant access to the file system (multiple tasks can access the file system without reentrancy problems). It creates as well events for activating/deactivating the bus:

                                                                           Activate and Deactivate of SD Card

                                                                                                    Activate and Deactivate of SD Card

These events I can route to my W5100 driver which there could lock/unlock a semaphore to protect access to the SPI bus, both for the SD card driver and for the Ethernet/W5100 driver:

 

1

 

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

      19

 

static xSemaphoreHandle SPImutex = NULL; /* Semaphore to protect shell SCI access */

 

 

void W5100_RequestSPIBus(void) {

  (void)xSemaphoreTakeRecursive(SPImutex, portMAX_DELAY);

}

 

void W5100_ReleaseSPIBus(void) {

  (void)xSemaphoreGiveRecursive(SPImutex);

}

 

void W5100_GetBus(void) {

  W5100_RequestSPIBus();

  W5100_CS_ENABLE();

}

 

void W5100_ReleaseBus(void) {

  W5100_CS_DISABLE();

  W5100_ReleaseSPIBus();

}

More about this in a future post.

Mounting File System

Because there is no ‘card detect’ pin available with the SD card socket, I have added the ‘mount’ and ‘unmount’ commands to the application shell command handler:

 

1

 

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

 

static bool cardMounted = FALSE;

 

static FAT1_FATFS fileSystemObject;

 

uint8_t APP_ParseCommand(const unsigned char *cmd, bool *handled, CLS1_ConstStdIOType *io) {

  if (UTIL1_strcmp((char*)cmd, CLS1_CMD_HELP)==0 || UTIL1_strcmp((char*)cmd, "app help")==0) {

    CLS1_SendHelpStr((unsigned char*)"app", (const unsigned char*)"Group of CLS1 commands\r\n", io->stdOut);

    CLS1_SendHelpStr((unsigned char*)"  mount|unmount", (const unsigned char*)"Print help or status information\r\n", io->stdOut);

    *handled = TRUE;

    return ERR_OK;

  } else if (UTIL1_strcmp((char*)cmd, CLS1_CMD_STATUS)==0 || UTIL1_strcmp((char*)cmd, "app status")==0) {

    CLS1_SendStatusStr((unsigned char*)"app", (unsigned char*)"\r\n", io->stdOut);

    CLS1_SendStatusStr((unsigned char*)"  mounted", cardMounted?(unsigned char*)"yes\r\n":(unsigned char*)"no\r\n", io->stdOut);

    *handled = TRUE;

    return ERR_OK;

  } else if (UTIL1_strcmp((char*)cmd, "app mount")==0) {

    *handled = TRUE;

    if (cardMounted) {

      CLS1_SendStr((unsigned char*)"card already mounted!\r\n", io->stdErr);

      return ERR_FAILED;

    }

    if (FAT1_MountFileSystem(&fileSystemObject, 0, io)!=ERR_OK) {

      CLS1_SendStr((unsigned char*)"failed!\r\n", io->stdErr);

      return ERR_FAILED;

    } else {

      cardMounted = TRUE;

    }

  } else if (UTIL1_strcmp((char*)cmd, "app unmount")==0) {

    *handled = TRUE;

    if (!cardMounted) {

      CLS1_SendStr((unsigned char*)"card not mounted!\r\n", io->stdErr);

      return ERR_FAILED;

    }

    if (FAT1_UnMountFileSystem(0, io)!=ERR_OK) {

      CLS1_SendStr((unsigned char*)"failed!\r\n", io->stdErr);

      return ERR_FAILED;

    } else {

      cardMounted = FALSE;

    }

  }

  return ERR_OK; /* no error */

}

With the Shell I have a command line interface to my application running on the board:

                                        Shell Interface

                                                                                               Shell Interface

With a card inserted, I can mount the file system.

                                             Mounting File System

                                                                                          Mounting File System

Summary

In this part I have covered how to use the SD card of the Arduino Ethernet Shield. The software project is available on GitHub here. That project already has an first version of the W5100 driver, and this will be subject of a future post.

Happy SD-Carding  image

  • Share
  • History
  • More
  • Cancel
  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube