Added Write Protection Pin to FatFsMemSDHC

What was missing in the FatFsMemSDHC component presented here is support for a ‘write protection’ pin. Well, that write protection is not present on micro-SD cards, and on normal SD cards it is a simple plastic thing with no real hardware meaning: it is all up to the software to respect it. While my other SD card components have support for such a write protection detection, it was lacking for the FatFsMemSDHC (for Kinetis) component. Time to fix this!

SD Card Lock

SD Card Lock: an SD-Card, a micro-SD Card and a micro-SD card adapter, both with write-enabled

As you can see from above picture: the writing on the card can be very confusing about which position is the ‘lock’ (write protected).

Write Protect Pin

The component has now an optional property for a write protection pin:

Write Protect Pin in FatFsMemSDHC

Write Protect Pin in FatFsMemSDHC

I considered to replace the GPIO_LDD interface of the Card Detect Pin with the better BitIO interface (I used for the Write Protect Pin). For now I kept the existing interface for compatibility reasons. And because that would be an extra effort to change it.

Because some SD card sockets have the write protection pin with a pull-up, others have it with a pull-down, there is a ‘Low Active’ property to invert the logic.

💡 if the pin needs an internal pull-up or pull-down, you need to configure this in an extra pin Init component. See this post (section Init_GPIO) for details how to do this.

This adds a pin to the component, and the status can be checked with isWriteProtected():

Write Protection Pin Enabled

Write Protection Pin Enabled

The CDE (Component Developement Code) is very simple:

bool %'ModuleName'%.%isWriteProtected(void)
{
%if defined(WP)
  %if %WPLowActive='yes'
  return inherited.WP.GetVal()==0;
  %else
  return inherited.WP.GetVal()!=0;
  %endif
%else
  return FALSE; /* no card write protection pin available */
%endif
}

TWR-K60N512

I tested the function with the TWR-K60N512 board which has a SD card socket with a write protection pin:

TWR-K60N512 with SD Card

TWR-K60N512 with SD Card

This pin is connected to PTE27:

SD_CARD_WP on TWR-K60N512 (Source: Freescale TWR-K60N512 Schematics)

SD_CARD_WP on TWR-K60N512 (Source: Freescale TWR-K60N512 Schematics)

Unfortunately, the schematic does not tell us if that pin will be low or high if the card is inserted :-(. For this it would be good to have a reference to the data sheet of the SD card socket used, but this was not provided (or I have not found it).

So time to make a wild guess: I assume that the WP switch is making a connection to GND, and is floating otherwise. That means that I need a pull-up, and as there is no pull-up resistor on the board schematic, I need to add an internal pull-up to the pin.

For this, I add an Init_GPIO component to the project to enable a pull-up on PTE27:

Initialization Settings for PTE27

Initialization Settings for PTE27

This creates a conflict as the PTE27 is used twice. I need to enable Pin Sharing:

Enabled Pin Sharing

Enabled Pin Sharing

❗ Note: there is a bug in Processor Expert that Pin Sharing is not propagated from the ‘master’ component to the ‘slave’ component. So I need to enable Pin Sharing in the BitIO_LDD, and *not* in the BitIO component!

With this, the status of the protection tab is properly reported in the shell:

SD Card Protection Status

SD Card Protection Status

Summary and Sources

With this, I have added write protection pin support to the FatFsMemSDHC component. I hope this is useful for everyone.

The new component is available on GitHub. The TWR-K60N512 project is here.

Happy Writing 🙂

Advertisements

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s