SD connector pinout. Connecting an SD card to a microcontroller

Lesson 33

Part 1

SPI. SD card. FAT

Today we will continue our favorite topic on the SPI interface. We are done with this bus connecting the Atmega8a and ATTtiny2313 controllers to each other. And today we will try to connect a memory card to the microcontroller via this bus using this interface SD (Secure Digital).

This card can also be connected via the SDIO interface, but since such an interface is not supported in hardware by our controller, we will not touch on it in this lesson. We are interested in the type of bus connection SPI, since we already have good accumulated knowledge on this topic, as well as hardware support in the controller that we are programming.

Nevertheless, we will look at the pinout of the card legs for both types

Well, since we are interested in the second type, we will deal with it.

And there’s not much to understand here. We know all these abbreviations. All standard SPI interface pins are here and there is nothing superfluous here.

Now about the map in general. This card allows us to store data; its memory type is FLASH, which, compared to EEPROM type memory, is also non-volatile, that is, when the power is turned off, the data does not disappear anywhere, but remains stored. Also, this memory has differences, we will get to know them during the programming process. One of the main differences is that, just like in EEPROM memory, we cannot write one byte to this memory. Theoretically, of course we can, but either only ones from our byte or only zeros will be written there, depending on the type of FLASH - NOR or NAND. That is, before writing a byte, you need to erase it, and due to the organization of this memory, we can only erase in blocks, so we can also write only in blocks. But the biggest difference from EEPROM is the price. It is several times cheaper, sometimes even orders of magnitude cheaper for one stored unit of information (per megabyte, per gigabyte). Therefore, FLASH memory usually always has a much larger amount of information.

There are 4 types of SD, but we will study this a little later.

Let's connect this map still in proteus

Everything is simple here. Actually not quite like that. Need more resistors

These resistors are needed to ensure appropriate levels, since the card is powered by 3.3 volts. In general technical documentation from 2.7 to 3.6 volts.

Also, it is not indicated in the proteus, but in fact we will power our card from a separate power supply by installing a microcircuit that converts 5 volts to 3.3 volts.

Or rather, we will not install anything, but will use a ready-made module in which everything is already installed.

We also have a display connected, as in expanding the functionality of the display library.

This is how it all looks in practical terms:

This is what the module with holder looks like

You can find such a module everywhere, it costs a penny. The module that connects via SDIO costs more. We also see that the module already has a microcircuit installed to reduce the voltage to 3.3 volts. And we connect power only to the 5 volt contact, and don’t connect anything to 3.3

Also, all level dividers are installed on the module, that is, this module is designed specifically for connection to 5-volt devices.

And I dug up a flash card for testing with 32 megabytes, exactly a megabyte and not a gigabyte

This flash card was given as a gift along with some camera and it is best suited for our tests, at least we will not think that this or that glitch is due to too much memory on the card.

The entire code was also taken from the last lesson along with the display library, since we will be using the function that we created in the last lesson very actively, but of course a new project was created and named accordingly MYSD_SPI.

Let's remove unnecessary lines, in main() we will only have this

intmain( void)

unsignedinti;

Port_ini();

LCD_ini(); //initialize the display

Clearlcd(); //clear the display

Setpos(0,0);

Str_lcd( "String 1");

Setpos(2,1);

Str_lcd( "String 2");

Setpos(4,2);

Str_lcd( "String 3");

Setpos(6,3);

Str_lcd( "String 4");

Delay_ms(2000);

// for (i=0;i<=22;i++) {str80_lcd(buffer2+i*20);_delay_ms(1000);}

While(1)

Since we will not output the text character by character, we can use the char type in the variable

unsignedchar i;

Now one more nuance.

In order for us to work with an SD card in Proteus, it is not enough for us to add the holder itself with the card; we also need to attach the flash card image file in its properties.

It's not difficult to create one. One way is to create using the WinImage program.

We create a new file in it using the menu item File -> New. Select the very last item in the dialog and click "OK"

For the test in Proteus, the size of 4 megabytes is quite enough for us, so in the next dialog we will change the field with the number of sectors, and also select the FAT12/16 format, because with 32-bit file system slightly different specifics of work, and also click "OK"

In general, of course, we can leave FAT32, since we do not work with the file system yet, but in further parts of the lesson we will work with the file system and we will work with 12/16.

Then we save our created file using the File -> Save As menu item. And we save it in the folder where we have the saved proteus project. Let's name the file and click "Save"

Also, then we will need to make sure that this file is not with the “read-only” attribute, and after that we will be able to connect it in Proteus. You will have to manually enter the file name, since Proteus requires some kind of its own format and our file will simply not be visible

We don’t need any path, since the file is located in the project folder. Click "OK".

We don’t need to initialize the bus, since our SPI will be software-based; not all flash cards work correctly with hardware, so we won’t need to use any registers. Hardware is, of course, better, but in order to thoroughly understand the operation of the protocol, you also need to work with the software, that is, twitch the legs of the ports. In general, looking at the diagram, it may seem that we have everything in hardware, since I chose exactly these legs, this is because I just chose it that way, so that later, maybe someone will try to work with the hardware implementation tires.

Let's add macro substitutions for the port legs

#include"main.h"

#defineMOSI3

#defineMISO4

#defineSCK5

#defineSS2

Let's add code to initialize the legs to the port initialization function

voidport_ini( void)

PORTD=0x00;

DDRD=0xFF;

PORTB|=(1<< SS)|(1<< MISO)|(1<< MOSI);

DDRB|=(1<< SS)|(1<< MOSI)|(1<< SCK);

We leave the MISO pin at the input, since by default all the bits in the register are zero, and we simply do not touch it. We also immediately turn on the high level in MOSI and SS, and pull up a resistor to MISO.

Let's write a function for transferring a byte over the SPI bus

voidSPI_SendByte( unsignedcharbyte)

{

}

Let's add a variable for the loop and the loop itself

voidSPI_SendByte( unsignedcharbyte)

unsignedchari;

for( i=0; i<8; i++) //move by bits of the byte

{

}

I think it’s clear why we count to 8, since we are transmitting exactly 8 bits.

Well, let’s start transmitting them little by little.

Let's first check the leftmost bit, isolating it from the entire byte by masking, and if it is equal to 1, then we will set it to 1 on the MOSI bus, and if it is 0, then we will not touch the bus

for( i=0; i<8; i++) //move by bits of the byte

A few years ago in May 2011, the creator of the cult game “Elite” David Braben presented the first concept of a single-board computer Raspberry Pi. This moment became a turning point in my life. The idea of ​​making a computer the size of a flash drive was on the surface, but it only gained momentum with the help of the Raspberry Pi Foundation.

Already on July 25, 2011, the alpha version of the computer was put into production. Unfortunately, the concept of the project has changed, and it is now positioned as a computer the size of a credit card. Despite this circumstance, millions of people followed him. I too obeyed the crowd effect, checking the official project page every day. A long and painful wait began for the “miracle”, which happened on February 29, 2012 - the start of sales.

The Raspberry Pi could be purchased through the Farnell network or from RS Components. As it turned out, on February 29 it was only possible to make a pre-order. None of the offices had these boards in stock. The first batch of devices was only 10,000 copies, so given the hype around the project, placing an order was very difficult. However, having overcome all difficulties, at 14:06 on the same day the computer was purchased for £46.73 from Farnell.

None of my foreign orders were fulfilled for so long. I was extremely upset that Farnell, charging £20 for delivery, sent the parcel on May 29, 2012 (2 months later) by regular mail without a tracking number. Surprisingly, the symbiosis of the Royal and Russian Mail delivered the parcel safe and sound on June 22. This was the most desired package in the last few months, so, unable to withstand the stress, I had to take time off from work and run to the post office.

There is no point in talking about how to configure the Raspberry Pi for the first launch. I’m a couple of years late with an article on this topic; many lines about this have already been written on other resources, and on Youtube A sufficient amount of video material has been posted. I want to talk about a significant design flaw for me - the inconvenient location of the SD card slot. When the card is inserted, it protrudes greatly beyond the board, which spoils the appearance of the homemade case.

There are two options for solving this problem: solder an SD->microSD adapter parallel to the connector installed on the Raspberry Pi (you can read how to do this operation in the article on Habré), or use a Low-profile MicroSD->SD adapter. The first option is simply unacceptable for me - well, I don’t dare to solder the board, because... I'm afraid of ruining the presentation of my Raspberry. I think that the best choice is to use a Low-profile adapter.

Initially, it was decided to purchase such an adapter in one of the foreign online stores. There is a choice, but the cost of such trinkets is simply prohibitive. The cheapest copies cost $10, and some samples frankly look homemade. The final decision to make the adapter yourself was made after visiting the DIYGadget website; note how easy it is to repeat their creation.

Are you ready? Let's move from words to action. To make the adapter correctly, let’s study the specifications for SD and microSD cards. I tried to systematize everything that is necessary for production in tables 1, 2.

Table 2: SD memory card pinout

By connecting the corresponding contacts on the memory cards, and combining Vss1, Vss2, we get the electrical circuit diagram of the adapter.

To make an adapter we need:
1) Holder for microSD (CF TFC-WPCE-08 MICROSD CARD) – 52.22 rub.
2) A piece of double-sided foil fiberglass laminate with an area of ​​​​about 4 cm 2 (2% of the position FOIL GLASS TEXTOLITE 1.5MM 2 SIDES) – 3 rubles.
3) Materials for installation (ferric chloride, tin, flux) – 10 rubles.

Then the cost of the project will be 65 rubles 22 kopecks and a certain amount of free time. In order to reduce the cost of the design, you can replace the microSD card holder with a CF TFC-WPCER-08 MICROSD CARD. Unfortunately, this item was not available in the Promelektronika JSC warehouse, so I purchased a more expensive option. I draw your attention to the fact that if you change the type of holder, you may not be able to use my template for LUT (laser ironing technology).

I used Autocad to design the PCB because... my favorite SprintLayout could not please me with the presence of the required template. For those who like to modify a printed circuit board, you can download the source in DWG format, and if there is no such need, then a blank in PDF format (I recommend checking the dimensions before applying the PDF template).

After the template is transferred to the board, I recommend drilling holes with a diameter of 0.5 mm to transition from one layer to another.

I make the transition from layer to layer using a thin wire, having previously irradiated the tracks of the future adapter. In those places where the holes are located under the microSD holder, it is necessary to grind off drops of tin with a file so that it is installed without distortion. Lastly, we install the holder. If various fluxes were used during the manufacturing process of the board, be sure to wash the board before pushing it into your Raspberry Pi.

Make an adapter yourself or buy it – the choice is yours. To make your choice more informed, I have selected several links for purchase especially for you:
1) Raspberry Pi Premium Low-profile MicroSD (TF) to SD Card Adapter. Protect Board.
2) Raspberry Pi Low-profile MicroSD to SD Card Adapter, SD card won't get damaged!!
3) MicroSD to "short" SD / RS-MMC adapter. For Everdrive carts, Raspberry Pi, etc.
4) Low-profile MicroSD to SD Card Adapter for Raspberry Pi.
5) SD card adapter for Raspberry pi REV2 +free shipping.

In conclusion, I would like to say that all material is intended for informational purposes only. The use of developments for commercial purposes is strictly prohibited. Reproduction of materials is possible only with my permission and in compliance with references to the original source. Those who want to support not only in word, but also in deed, and also thank me, please email me.

To assemble the device yourself, you need to download the following files:
1. Source board for LUT in DWG format
2. Payment for LUT in PDF format

Good luck in your endeavors!!!

As long as I can remember, I have always loved to read, but this is the 21st century and sometimes the necessary literature can only be found on the Internet. Yes, and you can read e-books on an electronic device such as a tablet, computer or e-reader. The result is a small device that can read text files from an SD or microSD card and display their contents on the screen.

The brain of the device is the Atmega32 microcontroller operating at a frequency of 8 MHz. The MK is clocked from an external quartz at 8 MHz; I used a small WH1604A LCD indicator on the HD44780 controller with a resolution of 4 lines of 16 characters each as the device screen. I used regular clock buttons; as for the SD card, to connect it to the microcontroller, I used resistor dividers to match the logical levels.

Schematic diagram of the device:

The pinout in the diagram is correct only for an SD card or SD adapter; to connect other cards, use their pinout!

The device supports SD, miniSD and microSD memory cards up to 4GB in size formatted in the FAT, FAT16 file system. It should be remembered that the device does not support directories, so all files need to be written only to the root of the flash drive. Text files must be in regular txt format and without formatting, file names must be no longer than 8 characters (not counting the extension).

When you turn on the device, a splash screen will appear on the display:

If the SD card is not installed in the device, is connected incorrectly, or something else, the following message will appear:

If everything is in order, the main menu will appear:

Using the buttons, you can enter the “Browse files” item where you can select the file you need to read.

In the "Settings" item you can choose which extension to display files in the "File Browser".

And in the last paragraph “About the system...” you can read information about the device, its author, etc.

I wrote the firmware for the device in the BASCOM-AVR environment using the AVRDOS library, the firmware takes up only 30% of the microcontroller's program memory, so there is room for creativity. Inside, the device is assembled on two printed circuit boards: on one there is an MK with a body kit, on the other there is a connector for an SD card and matching chains of logical levels.

Here is a photo of the device inside:

For power supply I used a 4.8V, 600mAh Ni-Cd battery. After flashing the microcontroller, you need to set the following fuse bits:

List of radioelements

Designation Type Denomination Quantity NoteShopMy notepad
U1 MK AVR 8-bit

ATmega32

1 To notepad
D1, D2 Rectifier diode

1N4001

2 To notepad
C1, C2 Capacitor22 pF2 To notepad
C3 Electrolytic capacitor100 µF1 To notepad
C4 Capacitor100 nF1 To notepad
R1 Resistor

10 kOhm

1 To notepad
R2-R4 Resistor

4.7 kOhm

3 To notepad
R5-R7 Resistor

1 kOhm

3 To notepad
LCD1 LCD displayLM014L1 To notepad
X1 Quartz8 MHz1 To notepad
Button 4 To notepad
Switch 1

Artem Makarov aka Robin

27.09.2014

Recently, flash drives made on a monocrystal basis, so-called monoliths, are increasingly being brought in for data recovery. Today we will talk about the process of recovering data from such a monolith - an SD memory card sent by a partner from the city of Kemerovo. The video of the wedding was recorded on the card, and when the celebration was successfully completed and it was time to start editing and releasing gift DVDs, the flash drive died for a long time.

Recovery of monolithic SD memory cards

It is noteworthy that from the outside it is not clear whether this is a “classic” SD card, with a PCB board, NAND memory and a controller, or a single crystal. Until the plastic case is opened. Most often, the failure of such memory cards is due to a failure in the translation tables. Less often - electromechanical damage.

To recover files from such a card, the first thing you need to do is read dumps from the crystal. To do this, the protective varnish that hides the tracks and contact pads of the monolith is removed mechanically (cleaning and grinding). After which the flash drive starts to look like this:

Tracks and pinout of a monolithic SD card

The contact pads to which the data bus, chip enable, read/write busy, power, etc. are connected are visible. Of course, nothing is marked, and datasheets detailing what to connect where are also not freely available. The pinout can be found either by taking exactly the same working flash drive (and there are a great many types of them, and having found the same conventional Kingston SD in appearance, you can get a completely different device inside) and, armed with a logic analyzer, painstakingly find out what goes where and why. Or by purchasing the pinout from a person/office that has already done such work for you.

The result is something like this:

Or this:

Now we need to eliminate internal transformations in the resulting dumps. The first step is to remove the XOR mask that the flash drive controller applied when writing information to NAND cells. With this mask the sector looks like this:

and when the required XOR mask is selected and applied, the sector takes on a meaningful appearance:

After eliminating XOR transformations, you need to set the correct sector geometry, describe the markers and the ECC data correction area. Correct bit errors using the ECC algorithm. Find out in what sequence the blocks were located and their size. Since the controller type is unknown (it’s a monolith!), it is necessary to determine which collector to use in this particular case. Will it be assembling the final image using a sector marker or using the rest of the translation tables?

After the image is assembled, check conflict blocks that have the same marker for relevance and substitute into the image those with which the final result will be the best. Having received a familiar image with a file system, you can open it in any disk editor and upload the files the user needs.

Of course, many operations are quite automated, but nevertheless the amount of work involved in recovering data from monoliths (single crystals) is very large. Not every engineer or company that recovers information is eager to get involved in such work. And the price tag for this kind of restoration is very far from the concept of “budget”.

Here is another case using the example of SD Sandisk recovery - the same monolith, only made a little differently inside:

Ready to Read

Recovery of MicroSD flash drives

And here is what the contact pads look like on a Micro SD card. It should be noted right away that these are just a few examples of many layout options.

And here is the pinout option for a monolithic Memory Stick Pro Duo memory card

This is not to say that it is a monolith, but it is not an ordinary USB flash drive either. The memory chip (crystal) is filled with compound (glue).

And here’s what a monolithic Olympus XD Picture card looks like, from which it was necessary to restore photographs:

Restoring broken Micro SDs

Separately, it is worth mentioning the successful completion of tasks to recover information from MicroSD flash drives that were broken into pieces, with a broken piece, with cracks in the body, etc. Some examples in the pictures below:

In all cases, when we are talking about a flash drive broken into pieces, with a broken part, etc. It is possible to restore information if the NAND crystal remains intact. For example, in the Sandisk micro-flash drive from the example below, as a result of careless operation, a piece broke off and damaged the tracks marked with a red oval.

The Hardmaster laboratory is one of the few that have experience and qualifications in data recovery from monolithic USB, SD, microSD, Memory Stick, etc. memory cards. If there are important files on your monolithic broken flash drive that you would like to return, please contact us!

As you know, SD memory cards are compatible with the SPI interface, so they can easily be connected to a microcontroller and communicate with them. Adapters for microSD cards are also available, from such an adapter we can make a microSD card slot for our layout. The photographs below show the appearance of the manufactured adapter for connecting to a breadboard.

The project initially used a 1 GB microSD memory card. The microcontroller is ATmega8 or ATmega32, operating at 8 MHz from an internal RC oscillator. In addition, an RS-232 interface was used to connect the prototype to a personal computer for data monitoring. To convert the logical levels of the interface, the MAX232 chip is used. To power the circuit, a stabilized 3.3 V power supply is required (the MAX232 chip is designed for a supply voltage of 5 V, however, as practice has shown, it remains operational at 3.3 V). Connecting the memory card using a 7-wire circuit, according to the pinout (see figure).

Schematic diagram for the ATmega8 microcontroller.

Pull-up resistors R1, R2 with a nominal value of 51 kOhm of the SPI interface provide better stability when working with various cards. Zener diodes D1, D2 are designed to protect the memory card during operation of the in-circuit programmer (ISP). The pins of the MAX232 VCC and GND chip are not indicated on the diagrams, but they must be connected to the corresponding points in the diagram.

Circuit diagram for microcontroller ATmega32

Schematic diagram for the ATmega32 microcontroller (added real-time clock on the DS1307 chip)

As you noticed, the latest version of the device is powered from a 12 V source, and the board has two voltage regulators 5.0 V (LM7805) and 3.3 V (LM1117-3.3). To power the SD card interface, 3.3 V is used, the rest of the circuit is powered by a 5.0 V source. The DS1307 real-time clock chip is in the standard configuration and is connected to the I2C interface of the microcontroller.

First, the “raw” data transfer format was studied, using the example of the operations of reading any data block, reading and writing several blocks of data, erasing several blocks, writing data to any SD memory block. The device, assembled on a breadboard, was connected to the computer via the RS-232 interface. To display read data from a memory card, as well as to enter and write data to the card, use the HyperTerminal (or similar) program on a computer.

After successful implementation of data exchange without specification, the memory card was formatted (FAT32) in the Windows XP operating system, then several text files, directories and other types of files were written to the card (in the root directory of the card). After this, routines and functions were written to work with the FAT32 file system to read files, to obtain a list of files on a memory card (using HiperTerminal), to obtain information about the full and free amount of memory.

View of the HiperTerminal program window with functions for working with an SD memory card:

The user is offered over 10 options for working with the memory card (for the version with a clock).

Options 0 - 4 These are low-level functions. After using options 0 - 3, you will need to reformat the card before using the FAT32 routines.
Options 5 - 9- refer to the FAT32 file system. At the moment, only short file names are supported (8 Bytes - file name, 3 Bytes - file extension). If files with long names are written, they will be displayed in the terminal program in a short format. To test these options, do not forget to format the card in the FAT32 file system and write down several directories and text files.

Description of options:

0 - Erase Blocks- erasing the selected number of blocks starting from the specified one.
1 - Write Single Block- writing data to a block with a specific address. Data is entered from the keyboard in the Hiperterminal program;
2 - Read Single Block- reading data from a block with a specific address. The read data is displayed in the terminal program window;
3 - Writing multiple blocks- recording several blocks starting from a specific address;
4 - Reading multiple blocks- reading several blocks starting from a specific address.

Note. Here, the multi-block features (options 3 and 4) are disabled due to insufficient memory on the ATmega8 microcontroller, since these features are not needed for testing the FAT32 file system. To enable these options, you must remove the macro in the SD_routines.h file (#define FAT_TESTING_ONLY). And, if you are using ATmega8, while testing options 3 and 4, the FAT32 library can be deleted in order to free up the microcontroller memory.

5 - Get File List- displays a list of available directories and files with the amount of memory they occupy (in the root directory of the card);
6 - Read File- reading the specified file and displaying the contents in the terminal program window;
7 - Create File- create/add a file with the specified name;
8 - Delete File- delete all files file with the specified name;
9 - Read SD Memory Capacity- information about the full and free capacity of the memory card (the FSinfo sector of the SD card is used).

In the terminal program, the serial port is configured for a baud rate of 19200, with no flow control and no parity check.

For the version with a real-time clock (DS1307) on the ATmega32 microcontroller, the properties of created or updated files are tied to date and time (date of creation/change), these properties are registered in the file table and can be checked using a computer, and the clock can be useful when data collection. Three options have been added to the options menu in the terminal program.

Share: