Micro SD connection Connecting the sd card to the microcontroller. Methods for installing and redeploying applications to SD cards

We displayed the picture on the display from the sd card, but some points were missed in it, the first - connecting the card itself, the second - only part of the library functions was considered Petit FatFs Let's take a closer look at these points.

Communication with the card is possible via one of two interfaces, SPI or SD.



I must say that the SD interface can work in one-bit and four-bit modes.

The scheme for connecting the card via SPI is standard and looks like this, unused card pins must be connected to the power supply using a 10K resistor.


But in amateur designs, pull-up resistors are often neglected, simplifying the connection diagram.

It should be noted that when connected via SPI, the card is very demanding on the supply voltage and a small drop in the supply voltage leads to the inoperability of the card, this has been verified from personal experience, there is nothing to say about the SD interface, I have not tried it yet. All this was written to for power supply, it is necessary to install capacitors. As for the inductor, it must be rated for current up to 100mA, but it is not necessary to install it.

The diagrams shown above show that the card needs 3.3 volts to work, respectively, in the data transmission lines, the voltage should not go beyond the range of 0 - 3.3 volts, and here the question arises, what if the MC is powered by 5 volts?
The answer is simple, you need to match the data lines, and this can be done using a conventional resistive divider.


The diagram shows that the MISO line does not need to be matched as data is transmitted over this line from card to MK.
In fact, few people connect the card directly to the MK, it is much more convenient to connect a card connector to the MK or buy a shield with a connector and all the necessary harness.

We figured out the connection, now let's look at how to use the library Petit FatFs, which is designed for 8-bit microcontrollers with a small memory size.

The library consists of 5 files:
integer.h- a header file that describes the main data types.

diskio.h- a header file that declares the prototypes of low-level functions for working with the disk and the status codes they return.

diskio.c- low-level functions should be implemented in this file, initially there are "stubs".

pffconf.h- configuration file.

pff.h- header file in which the prototypes of functions for interacting with the disk file system are declared.

pff.c- the file contains implementations of functions for interacting with the disk file system.

It can be seen that in order for the library to work, it is necessary to implement low-level functions. But if we are talking about AVR or PIC, for them on the site you can download an example of working with the library, which contains the file mmc, low-level functions are already implemented in it. It is also necessary to set the library configuration in the pff.h file and write the functions necessary for SPI to work.

Functions of Petit FatFs.

FRESULT pf_mount (FATFS*)- the function mounts/dismounts the disk. This function must be called before starting work with the disk, if you call the function with a null pointer, the disk is unmounted. The function can be called at any time.

Parameters
FATFS* fs- a pointer to an object of type FATFS, a description of this structure can be found in the pff.h file. We just need to declare a variable of this type.

Return values:
FR_OK (0)
FR_NOT_READY- the device could not be initialized
FR_DISK_ERR- an error occurred while reading from disk
FR_NO_FILESYSTEM- the drive does not have a valid FAT partition

FATFS fs;//declaring an object of FATFS type //mounting the disk if (pf_mount(&fs) == FR_OK) ( //mounting the disk, working with it //mounting the disk pf_mount(NULL); ) else ( //failed to mount the disk )

FRESULT pf_open (const char* path)- the function opens an existing file. After the file is opened, you can work with it, that is, read from it and write to it. You can work with an open file until another file is opened. The function can be called at any time.

Parameters
const char*path- a pointer to a string indicating the path to the file. The path must be specified completely relative to the root directory, separating the directories with a slash.

Return values:
FR_OK (0)- returned in case of successful execution of the function
FR_NO_FILE- File not found
FR_DISK_ERR- disk error
FR_NOT_ENABLED- disk has not been mounted

FATFS fs;//declaring an object of FATFS type //mounting the disk if (pf_mount(&fs) == FR_OK) ( //opening the file located in the root directory if(pf_open("hello.txt") == FR_OK) ( //doing something ) //open the file located in the folder new if(pf_open("new/hello.txt") == FR_OK) ( //do something ) //dismount the disk pf_mount(NULL); ) else ( // failed to mount disk)

FRESULT pf_read(void* buff, WORD btr, WORD* br)- the function reads the specified number of bytes from the file and saves them to the buffer. If the number of bytes read is less than specified, then the end of the file has been reached.
#define _USE_READ 1

Parameters:
void*buff- pointer to the buffer in which the read data is stored
WORD btr- number of bytes to be read
WORD*br- a pointer to a variable that stores the number of bytes read.

Return values:
FR_OK (0)- returned in case of successful execution of the function
FR_DISK_ERR- disk error
FR_NOT_OPENED- the file was not opened
FR_NOT_ENABLED- disk has not been mounted

FATFS fs;//declare an object of type FATFS BYTE buff;//buffer for reading a file WORD br; // counter of bytes read // mount the disk if (pf_mount(&fs) == FR_OK) ( // open the file located in the root directory if(pf_open("hello.txt") == FR_OK) ( // read 10 bytes from it pf_read(buff, 10, &br); if(br != 10) ( //if br is not equal to 10 //it means we have reached the end of the file ) ) )

FRESULT pf_write(const void* buff, WORD btw, WORD* bw)- the function allows you to write data to an open file. In order for the function to work in the pffconf.h file, you need to write
#define _USE_WRITE 1

Parameters:
void*buff- pointer to the buffer we want to write, zero value finalizes the write
WORD btw- the number of bytes we want to write
WORD*bw- a pointer to a variable that stores the number of bytes that could be written. By analyzing this variable, you can find out whether the end of the file has been reached.

Return values:
FR_OK (0)- returned in case of successful execution of the function
FR_DISK_ERR- disk error
FR_NOT_OPENED- the file was not opened
FR_NOT_ENABLED- disk has not been mounted

Due to the fact that the library is designed for microcontrollers with a small amount of memory, this function has a number of limitations:

  • you cannot create new files, and you can only write to existing ones
  • file size cannot be increased
  • can't update timestamp
  • a write operation can only be started/stopped at a sector boundary
  • read-only file attribute cannot prevent writing

In order to understand the penultimate point, you need to know that the memory of the card is divided into blocks (sectors) of 512 bytes and recording can only be started from the beginning of the sector. Thus, if we want to write 1000 bytes, then the first sector will be written completely, and only 488 bytes will be written to the second, and the remaining 24 bytes will be filled with zeros.

To write to an open file, do the following:

  • set the pointer to the sector boundary, if set not to the boundary, the pointer will be rounded to the lower sector boundary
  • call the write function the desired number of times
  • finalize the entry by calling the function with a null pointer

In order to give an example of the work of the recording function, it is necessary to consider one more function.

FRESULT pf_lseek(DWORD offset)- sets the read/write pointer in the open file. You can set the pointer with an absolute or relative offset; for an absolute offset, you must pass a number to the function
pf_lseek(5000);
for relative, pass the value of the pointer to the current position fs.fptr and the amount of displacement
pf_lseek(fs.fptr + 3000);
In order for the function to work in the pffconf.h file, you need to write
#define _USE_LSEEK 1

Parameters:
DWORD offset is the number of bytes to shift the pointer to.

Return values:
FR_OK (0)- returned in case of successful execution of the function
FR_DISK_ERR- disk error
FR_NOT_OPENED- the file was not opened

You can write data to a file in the following way.
FATFS fs;//declare an object of type FATFS BYTE buff;//buffer for reading a file WORD br; // counter of bytes read // mount the disk if (pf_mount(&fs) == FR_OK) ( // open the file located in the root directory if(pf_open("hello.txt") == FR_OK) ( // set the pointer to the first sector pf_lseek(0); //write pf_write(buff, 10, &br); //finalize the write pf_write(0, 0, &br); ) )

I also leave here a piece of really working code that uses all the functions described above.
#define F_CPU 8000000UL #define buff_size 10 #include #include #include "diskio.h" #include "pff.h" #include "spi.h" FATFS fs;//declaring an object of type FATFS BYTE read_buff;//buffer for reading the file BYTE write_buff = "hello word";/// /buffer to write to file UINT br; // counter of bytes read int main(void) ( // mount the disk if (pf_mount(&fs) == FR_OK) ( // open the file in the folder new if(pf_open("new/hello.txt") == FR_OK) ( //set write pointer pf_lseek(0); //write pf_write(write_buff, buff_size, &br); //finalize write pf_write(0, 0, &br); //set read pointer pf_lseek(0); //read that that we wrote pf_read(read_buff, buff_size, &br); if(br != buff_size) ( //if br is not equal to buff_size //it means we have reached the end of the file ) ) //mount the disk pf_mount(NULL); ) while(1) ( ) )

FRESULT pf_opendir(DIR* dp, const char * path)- the function opens an existing directory and creates a pointer to an object of type DIR, which will be used to get a list of files in the open directory.
In order for the function to work in the pffconf.h file, you need to write
#define _USE_DIR 1

Parameters:
DIR *dp- pointer to a variable of type DIR.

const char *path- pointer to a string that contains the path to the directory, directories are separated by a slash

Return values:
FR_OK (0)- returned in case of successful execution of the function
FR_NO_PATH- could not find path
FR_NOT_READY- Failed to initialize disk
FR_DISK_ERR- disk error
FR_NOT_ENABLED- disk has not been mounted

//declaring variables FATFS fs; DIR dir; //mount disk pf_mount(&fs); //open directory pf_opendir(&dir, "MY_FOLDER");

FRESULT pf_readdir(DIR* dp, FILINFO* fno)- function allows you to read the contents of the directory. To do this, open a directory with the pf_opendir() function and call pf_readdir(). Each time the function is called, it will return the name of the object (folder/file) located in the specified directory. When it has gone through all the objects, it will return the null string in the fno.fname array element.
In order for the function to work in the pffconf.h file, you need to write
#define _USE_DIR 1

Parameters:
DIR *dp- pointer to a variable of type DIR, which must be previously declared

FILINFO *fno- a pointer to a variable of type FILINFO, which must be previously declared.

Return values:
FR_OK- successful completion of the function
FR_DISK_ERR- disk error
FR_NOT_OPENED- directory not open

FATFS fs; FRESULT res; FILINFO fno; DIR dir; //mount disk pf_mount(&fs); //open directory res = pf_opendir(&dir, MY_FOLDER); //read the contents of the directory for(;;)( res = pf_readdir(&dir, &fno); //check if there were any errors while reading // and if there are still files in the specified directory if ((res != FR_OK) || ( fno.fname == 0))( break; ) //display in a convenient way fno.fname usart_sendStr(fno.name); usart_sendStr(/r); )

And finally, I'll leave the working project here

Today we will talk about automatic installation of applications on a memory card for tablets running Android. Apple devices, due to the lack of a MicroSD slot, immediately disappear - they are limited by the amount of internal memory, so you have to store part of the data on the cloud. Whereas in most Android tablets this slot is present. Let's say more, recently gadgets have begun to support memory cards up to two terabytes in size! And no, we did not seal it - it really is.

If the method below does not work for you, then try the one we wrote recently.

Why are applications not saved to the memory card?

We hasten to disappoint you - in some devices, software will not be able to allow automatic installation on MicroSD. In particular, this applies to devices on Android 4.4.2 and higher - up to the marshmallow. Fortunately, there is third-party software that allows you to do this. But let's not rush things - you will learn about everything in order.

Find out the version of Android
Well, now let's put everything on the shelves. First we need to find out the version of Android.

We go to the menu;
- Go to "Settings";
- Scroll to the very bottom and click on the item “About phone”;
- In the submenu that opens, we are looking for information on the version;

In this case, it's Android 5.1.1. This method is suitable for both smartphones and tablets. As a matter of fact, on this device, without “external” intervention, it will not be possible to make sure that all applications are automatically installed on the card. But, as you have already noticed, we have a third-party firmware with built-in Root rights.

With their help, you can easily install additional software, which, working in the background, will “scatter” all files from programs and games on a flash drive.

Saving applications to a memory card for Android 2.2 - 4.2.2

Here everything is extremely simple and banal:

1. We also go to the menu and look for “Settings” there - the icon, as a rule, resembles a gear in its appearance - there should be no problems with its search;

2. Next, look for the sub-item “Memory”. In our case, it is located between the “Screen” and “Battery”. The menu may be different depending on the manufacturer of the device. In the screenshot, this is an example of a clean version of Android, without shells that are pre-installed at manufacturers;

3. And now the most important thing - tap once on the “SD card” item, located below the inscription: “Default recording disk”. On the contrary, a circle or a checkmark should appear;

4. Profit! Now all applications downloaded through the Play Market will be automatically installed on external memory.

By the way, if the flash drive is slow, and there are some, then applications may not work correctly. So get a good SD card - don't be stingy.

What to do with devices on Android KitKat and above?

Unfortunately, without obtaining Root-rights, it will not work. Google has officially dropped support for this feature in new versions of the operating system. The fact is that cloud services are becoming more widespread and, as a result, problems with lack of memory should not arise. But in our country there is no such high-speed Internet as in the USA, and traffic is not cheap, so clouds are not in demand.

Is it possible to somehow make the applications automatically installed on the memory card? As we said above, this is real.

If you have a tablet of one of the Chinese companies, then there probably already have built-in Root-rights, but you will have to tinker with other manufacturers. Naturally, in the course of this article, we cannot talk about how to get them, because the process for each gadget is unique - the instructions can fit only in a multi-volume book. But oh well, it doesn't matter.

You can contact a specialist with a request to install Root-rights or do it yourself at home. The last option is the most risky, there is a chance to turn your gadget into a so-called “brick” and only one of the service centers can restore it. However, if you have already carried out a similar procedure, there should be no problems. In extreme cases, on the Internet, in addition, you can also find solutions to these same problems. So go ahead and experiment!

  • Something we have moved far enough away from the main topic of the article. So, back to the instructions: In any of the possible ways (flashing, unlocking the bootloader, and so on), we get Root rights;
  • We go to Google Play;
  • In the search bar we write: “SDFix: KitKat Writable MicroSD” - this is the very assistant application that will come in handy in the future. We install it. We hope that this does not need to be explained to anyone? Just tap once on the “Install” button and the process will take place automatically, after which a shortcut to launch will appear in the menu;
  • We open it and see a bunch of, most likely incomprehensible, inscriptions in English. You do not need to translate them - everything is solved in a few clicks;
  • Click on “Continue”, as shown in the first screenshot;
  • We tick off our agreement that by pressing the “Continue” button the device will be slightly modified;
  • We wait literally a couple of minutes until the orange screen changes to green.
  • On the green screen, we are informed that the automatic installation of applications on the SD card has been successfully enabled.
Actually, that's all. And the most difficult thing in this whole business is getting Root rights. Fortunately, on the Internet there are step-by-step instructions for all gadgets that have ever hit store shelves, and there are enough instructions on our website.

So, you are a happy owner of a tablet. Almost all tablet computers are equipped with a microSD card slot for . Often, the built-in memory is usually not enough, especially if you are a fan or watch your favorite TV shows on the road. And now, imagine, there was a nuisance - the tablet does not see the memory card. You should not panic, you still have time to run to the service center. Let's first try to solve the problem on our own.

In most cases, the problem can be solved by yourself.

So, what to do if suddenly the tablet stopped seeing the memory card. This happens sometimes. The first step is to reboot the device, that is, turn it off and on again. With some degree of probability, everything will be back to normal after that.

What to do if the restart did not work

We remove the drive from the device and check it on another tablet or mobile phone. If everything works fine in another device, it means that everything is in order with the memory card and in the MicroSD slot on your tablet. In this case, you have a direct road to the service center.

Let's assume that your drive is not detected in another mobile device. Then, using a card reader or an adapter from MicroSD to SD, we connect the memory card to. If he does not see it either, then the drive is probably out of order and you need to purchase another one. Fortunately, the cost of microSD cards is now quite democratic.


If other devices do not see the media, then the problem is in it

But consider a more positive situation - your computer has detected the card. There are two options here:

  • Windows sees the memory card, but can't access it
  • The map opens correctly and all your information is present on it.

In both options, the further actions are approximately the same - since the computer works with the card, but the tablet does not, then there is a possibility that the matter is in incorrect formatting. In this case, the drive must be reformatted. The only caveat is that if the card still opens, then you need all the information that is on it (just create a folder somewhere, albeit on the "Desktop", and copy all the files and folders into it).

In order to format your MicroSD card, you need to right-click on its icon and select the "Format" context menu item. A dialog box will open in which you need to set the cluster size to "Default" and the FAT32 file system. Then uncheck the "Quick (cleaning the table of contents)" checkbox and click the "Start" button. You will have to wait for some time, usually no more than ten minutes, but it depends on the size of the drive.

Check after formatting

We check the card after formatting, if the explorer displays it normally and allows you to enter it, then the next step will be the correct (safe) removal of the memory card from the computer.

We insert the MicroSD back into the tablet computer and check. In most cases, the above procedure restores the health of your tablet-drive bundle and you can back. If the tablet does not open the memory card even after formatting, then there is most likely a problem with the MicroSD slot and you still have to go to the service center.

Video on what to do if the Android device does not see the storage device:

Here are some simple manipulations, if the tablet does not read the memory card. As you can see, the recipe is quite simple and does not require any special knowledge from the user. But it can save you from going to a service center, where in the vast majority of cases you will be required to pay some amount of money (sometimes quite tangible, depending on the impudence of the masters). Let your memory cards serve happily ever after!

The problem of lack of memory is one of the fundamental problems for both PCs and mobile devices. With a small amount of free memory, the system usually starts to slow down, freeze, work unstably and unreliably. This is especially true for Android devices, many of which initially have a rather small amount of main memory (the so-called "Internal Storage"). In such a situation, some users may have the idea to try using an external SD card as the main storage on their Android device. In this article, I will tell you how to make an SD card the main memory on Android gadgets, and what methods will help us with this.

We analyze how to make an SD card the main memory on Android

What you need to install an SD card as main memory on Android

To accomplish this task, you will need a high-speed SD card (preferably class 10 or faster). Cards 6, and especially 4 and 2 classes are not suitable for such purposes, your system will significantly slow down its work due to their use, which is unlikely to please any of the users.

It is also important to understand that the validity period of such an SD card, due to the active load on it, will be significantly less than if the card was loaded in standard mode.


Method number 1. Change the contents of the Vold.fstab file

The first of the described methods involves changing the contents of the system settings file "Vold.fstab". After making these changes, the Android OS will consider your SD card as the internal memory of the device, while keeping in mind that a number of previously installed applications may stop working.

It is important to know that this method only works on rooted devices running Android OS below (!) than version 4.4.2. In Android OS versions 4.4.2 and above, most likely you simply will not find the specified file.

Also note that an error in the implementation of this method (in particular, adding extra characters to the necessary lines) can most sadly affect the performance of your device. Therefore, carefully weigh the possible risks, and if, nevertheless, you have made a decision, then proceed with its implementation.

So, to implement this method, do the following:

For example, these lines could be:

  • dev_mount sdcard/storage/sdcard0 [email protected]
  • dev_mount sdcard2/storage/sdcard1 auto/xxxxxx

To make the necessary changes, we need to change the path in the specified lines, that is, in other words, instead of 0, put one in the first line, and in the second, put 0 instead of 1.

After the changes, these lines will look like:

  • dev_mount sdcard/storage/sdcard1 [email protected]
  • dev_mount sdcard2/storage/sdcard0 auto/xxxxx

Save your changes and then reboot your gadget.

Another option on how to make a memory card the main one on android:


Method number 2. We use the settings of Android OS 6.0 and higher

In addition to the first method, in which I looked at how to switch the phone's memory to a memory card, there is another method that works only on Android 6.0 (Marshmallow) or higher OS settings and allows you to use the SD card as the main one for saving files and working with them . To implement it, I recommend making a copy of the data from your SD card (if any), since this card will be formatted by the system.

Do the following:

Conclusion

In this article, I have considered options for how to make an SD card the main memory on Android. It is important to consider that these methods do not always work - the first one requires root rights and the Android OS is lower than 4.4. which the implementation of the latter method is impossible for internal reasons). Also note that the implementation of these methods is carried out by you at your own peril and risk, and it is hardly possible to guarantee a 100% result in this case.

Starting with androil 6.0, it became possible to use a flash card as internal storage device data. Now the device, after certain actions, can use the memory available on SD as freely as the internal one. The article will talk about how to connect an SD card in this capacity and what restrictions are imposed on it.

How to connect a flash drive as internal memory

Before connecting the drive, you must transfer from it all important information. During the setup process, it will be completely cleared and you will not be able to return the data.

First of all, you need to go to Settings and then go to the " Storage and drive”, where you should click on the SD card.

Next, choose " Tune» and click « Inner memory". Immediately after that, the device will warn the user that all information will be deleted and it will become unreadable on other devices without full formatting.

Here you need to select the item " Clean and Format” and wait for the memory cleaning process to complete. You may then see a message that the media is running slowly. As a rule, this means that the flash drive used is not of very good quality and its use as device storage may also affect the performance of the smartphone itself. For good and fast work recommended to use UHS Speed ​​Class 3 (U3) drives.

After formatting is completed, the smartphone will require you to transfer the information, you should agree with this and wait for the work to complete. After the transfer, the work of turning the flash drive into internal memory will be almost completed, all that remains is to reboot the device.

Features and limitations of using an SD card

There are a few things to be aware of before using a flash drive in this way.

  1. After conversion, all data, except for some applications and system updates, will be placed on the SD drive.
  2. When connected to a computer, only this part of the memory will also be available for interaction.

In fact, all actions are performed only with a flash drive, the real internal storage of the phone not available for interaction and, practically, is not used in any way. Firstly, this means that when you remove the drive, almost all data, photos and applications will be lost. Secondly, if the volume of the flash drive is less than the actual storage capacity of the smartphone, then the amount of available memory will decrease, not increase.

Format the card with ADB for use as internal storage

On some devices, the function is not available, but it is possible to connect a USB flash drive as storage in another way. However, it should be noted that this method is very laborious and can harm the device, therefore, if you are not confident in your abilities, then it is better not to do this on your own.

To use this method, you will need to perform a lot of actions. You need to download from the site and install Android SDK, then download and install from the official website device drivers, and you also need to enable " debug mode byUSB» on the device.

  • adb shell
  • sm list-disks (after execution, the id will be given in the form disk:XXX,XX it should be written down and entered in the next line)
  • sm partition disk:XXX,XX private

Then it will take turn off the phone, go to settings and click on sd, select menu and click " Transfer data". Everything, on this action is completed.

How to put a memory card in standard mode

To return the flash drive to standard mode, you just need to go to its settings, as in the first option, and select " Portable media". Before that, all important information should be transferred to another location, because the drive will be formatted in the process.