SD CARD

From Ethersex_Wiki
Jump to: navigation, search
SD/MMC-Card Reader
Status
Stable
menuconfig I/O->Storage->SD/MMC-Card Reader
Pinning yes
Ecmd yes
Control6 yes
Depends on ECMD
Code https://github.com/ethersex/ethersex/tree/master/hardware/storage/sd_reader

The module is based on Roland Riegel's MMC/SD/SDHC card library complemented by the connection to the Ethersex Virtual File System.

Connection

SD-Card adapter circuit diagram
SD-Card adapter module
MicroSD card adapter with level-shifter from China

MMC and SD memory cards can be used in SPI mode relatively easily controlled with a microcontroller. In principle, there is not between SD Card and MMC many differences, but SD cards are more common, generally faster than MMCs, and have a better-implemented SPI interface. There are several variants (miniSD, microSD), which are largely compatible with the standard SD-card.

The card reads the adjacent data bits with the rising clock edge, as SPI modes are thus suitable Mode 0 and Mode 3 In MMCs, the SPI mode is not precisely specified, so it does happen sometimes that the SPI mode must be selected differ depending on the card.

SD cards are typically often operated with 3.3V and 5V microcontrollers. This forces a level adjustment, because the input lines to the SD card are not 5V tolerant. A level adjustment with resistors is not recommended.

In addition to the lines leading to the SD card, there are two other lines which lead to the SD card socket: namely, the card-detect line and the write-protect line. They serve as the name already say, to signal the physical presence of a card in the SD socket and the position of the write protect switch.

If hardware SPI used, only the chip select line must be configured. The card-detect line and the write-protect line are optional and MicroSD anyway not exist.

 ifdef(`conf_SD_READER', `
   /* port the sd-reader CS is attached to */
   pin(SPI_CS_SD_READER, PB2, OUTPUT)
   /* uncomment and edit this if you have connected the CD (card detect) signal */
   pin(SD_READER_AVAILABLE, PD4, INPUT)
   /* uncomment and edit this if you have connected the WP (write protected) signal */
   pin(SD_READER_WR_PROTECT, PD5, INPUT)
 ')

Another problem with the SD card is the initialization. Manages Ethersex not in a certain time to sync, so the card into the normal drops (not the SD) mode and can no longer be addressed. A solution for this is that the card receives a controllable power supply. For this purpose, we define the pinning only pin

 pin(SD_READER_POWERON, PB3, OUTPUT)

Connector

A cheap type of contact is probably cannibalizing a cheap card reader if you want to solder any cables directly to the card. Individually purchased connectors can usually considerably teuerer. Even old floppy cable can be used as cheap connectors. Parts of AT slots or connectors of 5.25-inch floppies go. If necessary, you can see the card between two rows of pin strip pinch (or solder). It's even cheaper if you use micro or mini SD cards and usually included adapter anlötet.

However, anyone who wants to give up a DIY, alternatively, for relatively little money a completely assembled SD adapter available for purchase to be connected to the AVR. With this adapter already is a bidirectional signal level between the 5V levels of the control lines of the AVR and the 3.3 V level of the SD card on board. Affordable modules sourced from China are available on ebay.

Configuration

 | |        I/O  --->                                                    | |
 ...
 │ │        Storage  --->                                                │ │ 
 ...
 | |        [*] SD/MMC-Card Reader  --->                                 | |
 ...
 | |        [ ] SDHC support                                             | |
 | |        [*] VFAT LFN support                                         | |
 | |        [ ] Read-only mode                                           | |
 | |        [*] Use read-timeout                                         | |
 | |        [*] Ping-read SD card every 10s                              | |
 | |        --- ECMD Support                                             | |
 | |        [*] info                                                     | |
 | |        [*] dir                                                      | |
 | |        [*] mkdir                                                    | |
 | |        [*] rm                                                       | |
 | |        --- Debugging Flags                                          | |
 | |        [*] FAT                                                      | |
 | |        [*] RAW                                                      | |
 | |        [*] VFS                                                      | |
   
 | |        General Setup  --->                                          | |
 ...
 | |        [*] VFS (Virtual File System) support  --->                  | |
 ...
 | |             [*] SD/MMC-Card Filesystem                              | |

ECMD

The SD CARD module implements an ECMD interface. See ECMD reference.

Control6

The following example writes the date, time and temperature by VFS_LOG_ALLOCA (VFS_LOG requires UIP) in the file "temp.log" as soon as the temperature has changed by more than a degree to the last measurement. For date and time CLOCK_SUPPORT must be enabled.

#include <stdlib.h>

int16_t Temperatur;
int16_t Temperatur_alt;

CONTROL_START

THREAD(read_temp)
  Temperatur = ONEWIRE_GET(10d85594010800eb);
  ON abs(Temperatur-Temperatur_alt)>10 DO
    uint8_t sign = Temperatur<0;
    div_t res = div(abs(Temperatur),10);
    VFS_LOG_ALLOCA("temp.log", 50, "%2d.%2d.%4d %2d:%02d %S%d.%d", CLOCK_DAY, CLOCK_MONTH, CLOCK_YEAR, CLOCK_HOUR, CLOCK_MIN, sign?PSTR("-"):PSTR(""),res.quot,res.rem)
    Temperatur_alt = Temperatur;
  END
  WAIT(15);
THREAD_END(read_temp)

ON STARTUP DO
  Temperatur = Temperatur_alt = 0;
  THREAD_START(read_temp);
END

CONTROL_END