This shows you the differences between two versions of the page.
| en:examples:storage:sdcard [2012/06/18 15:09] – created raivo.sell | en:examples:storage:sdcard [2026/02/19 11:30] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== SD Card ====== | + | ====== SD card ====== |
| - | <note important> | + | //Required knowledge: |
| - | ===== Teooria ===== | + | [HW] [[et: |
| + | [LIB] [[et: | ||
| - | [{{ : | + | ===== Theory ===== |
| - | Secure Digital (SD) on mälukaardi formaat, mille töötas välja SD Card Association (SDA) kasutamiseks kaasaskantavates seadmetes. SD kaart töötab 3,3 V tööpingega. Kaartiga suhtlemiseks saab kasutada kaarti enda SD nimelist liidest ja ka SPI siini. Viimane on hea võimalus kasutada SD kaarti lihtsamates süsteemides. | + | {{ : |
| - | SD kaart on iseenesest suur hulk mälupitte, mida saab küll muuta, aga muud seadmed ei oska salvestatud andmetega seljuhul midagi peale hakata. Probleemi lahendamiseks kasutatakse failisüsteemi, | + | Secure Digital (SD) is a memory card format developed by the SD Card Association (SDA) for portable devices. Communication can use the card's SD interface or the SPI bus. SPI is a good option for simpler systems. The SD card operating voltage is 3.3 V. |
| - | Portatiivsetel mäluseadmetel ja mälukaartidel on laialt levinud FAT (File Allocation Table) failisüsteem, mida toetavad kõik enamlevinud operatsioonisüsteemid. FAT failisüsteemi failipaigutustabel sisaldab iga kettal oleva faili algusklastri kannet, mis omakorda sisaldab viita järgmisele failiga seotud klastrile ja nii edasi, kuni faililõpu klastrini. FAT-i koral on klastrid adresseeritud n-bitiste kannetega aadressiruumi tabelisse, kus n on sõltuvalt FAT-i versioonist 12 (FAT12), 16 (FAT16) või 32 bitti (FAT32). Seega vanemate FAT versioonide korral muutusid suurte kettamahtude korral klastrid mahult suureks ja seega kettamahu kasutamine ebaeffektiivseks. | + | An SD card is basically a large set of memory bits that can be modified, but other devices cannot use the stored data without a file system. The file system enables working with files instead of the raw memory array. One of the main tasks of a file system is to organize logical files on the physical storage device. The storage space is divided into sectors, usually 512 bytes in size. Since it is more efficient to work with larger units, sectors are grouped into clusters. A cluster is a contiguous set of sectors. Larger clusters reduce fragmentation for large files, but increase wasted space for small files because some clusters remain only partially filled. |
| - | ===== Praktika ===== | + | Portable storage devices and memory cards commonly use the FAT (File Allocation Table) file system, supported by most operating systems. The FAT file allocation table contains an entry for each file's starting cluster, which points to the next cluster for the file, and so on until the end-of-file cluster. In FAT, clusters are addressed in an n-bit address table where n depends on FAT version: 12 (FAT12), 16 (FAT16), or 32 bits (FAT32). In older FAT versions, large disk sizes required large cluster sizes, resulting in inefficient space usage. |
| - | Kodulabori Kontrollermooduli plaadil on pesa Micro SD mälukaardi sisestamiseks. Ühendatud on see samale SPI siinile koos etherneti kontrolleriga. | + | SD cards are available in three physical form factors: standard SD, smaller miniSD, and smallest microSD. microSD is common in mobile phones and tablets and is also used in the HomeLab Controller module. In addition to form factor, SD cards are divided into generations: |
| + | * Generation I: SD - up to 2 GB | ||
| + | * Generation II: SDHC - up to 32 GB | ||
| + | * Generation III: SDXC - up to 2 TB | ||
| - | Kodulabori teegis on SD kaardiga suhtlemiseks kaks kihti. Esimene ketta kiht on seotud otseselt kettaga suhtlemise ja initsialiseerimisega. Lisaks paiknevad seal kettale kirjutamise ja lugemisega seotud funktsioonid. Teine failisüsteemi kiht suhtleb esimese kihiga ja võimaldab failidega seotud toiminguid. Toetatud ja testitud on FAT12, FAT16 ja FAT32 failisüsteemid. | + | < |
| - | + | ||
| - | Kodulabori teegis oleva FatFs failisüsteemi paketiga on võimalik lähemalt tutvuda | + | SD cards are available in different speeds, classified by Speed Class Rating (SCR). |
| + | Common speed classes are: | ||
| + | * 2 MB/s | ||
| + | * 4 MB/s | ||
| + | * 6 MB/s | ||
| + | * 10 MB/s | ||
| + | |||
| + | ===== Practice ===== | ||
| + | |||
| + | The HomeLab Controller module has a microSD card slot. The card is connected to the SPI bus, and on the HomeLab II Controller module it shares the SPI bus with the Ethernet controller. | ||
| + | The HomeLab library includes the FatFs file system module, which you can read about at [[http:// | ||
| - | Järgmine näide demonstreerib tekstifaili lugemist ja kirjutamist. Nupule S1 vajutades initsialiseeritakse ketas ja failissüsteem. Nupu S3 vajutamine loob kettale kausta, kuhu omakorda luuakse fail. Faili kirjutatakse ka sisu. Nupp S2 kuvab tekitatud faili sisu ekraanile. | + | For the file system to work, the function // |
| - | ~~PB~~ | + | The second file system layer is in //ff.c// and //ff.h//. It communicates with the first layer via low-level functions and provides file operations. The file system can be mounted only after the disk is initialized, |
| + | After initializing the disk and file system, you can start working with files. To open a file use //f_open// with the desired mode. There are many options, for example // | ||
| + | The next example demonstrates reading and writing a text file. Pressing S1 initializes the card and file system. Pressing S3 creates a directory on the disk and a file inside it, then writes content to the file. S2 displays the file contents on the screen. | ||
| + | \\ | ||
| + | \\ | ||
| + | \\ | ||
| <code c> | <code c> | ||
| + | // HomeLab SD card usage example program | ||
| #include < | #include < | ||
| #include < | #include < | ||
| Line 32: | Line 52: | ||
| #include < | #include < | ||
| - | + | // Main program | |
| - | // LEDide viigud | + | |
| - | pin led_red | + | |
| - | pin led_yellow = PIN(C, 4); | + | |
| - | pin led_green | + | |
| - | + | ||
| - | // Nuppude viigud | + | |
| - | pin button1 = PIN(C, 0); | + | |
| - | pin button2 = PIN(C, 1); | + | |
| - | pin button3 = PIN(C, 2); | + | |
| - | + | ||
| - | + | ||
| int main (void) | int main (void) | ||
| { | { | ||
| - | int f_err_flag = -1; //Ketta vea lipp | + | // Disk error flag |
| - | int d_err_flag = -1; // | + | int f_err_flag = -1; |
| + | // File system error flag | ||
| + | int d_err_flag = -1; | ||
| char f_err_buf[16]; | char f_err_buf[16]; | ||
| char d_err_buf[16]; | char d_err_buf[16]; | ||
| Line 61: | Line 72: | ||
| unsigned char new_value2, old_value2 = 0; | unsigned char new_value2, old_value2 = 0; | ||
| unsigned char new_value3, old_value3 = 0; | unsigned char new_value3, old_value3 = 0; | ||
| - | + | ||
| - | + | // Configure LEDs and turn them off | |
| - | // Seab LEDid töökorda | + | pin_setup_output(led_red); pin_set(led_red); |
| - | pin_setup_output(led_red); | + | pin_setup_output(led_yellow); |
| - | pin_setup_output(led_yellow); | + | pin_setup_output(led_green); |
| - | pin_setup_output(led_green); | + | |
| - | // Seab nupud töökorda | + | // LCD initialization and text |
| - | pin_setup_input_with_pullup(button1); | + | |
| - | pin_setup_input_with_pullup(button2); | + | |
| - | pin_setup_input_with_pullup(button3); | + | |
| - | + | ||
| - | // Lülitab LEDid välja | + | |
| - | pin_set(led_green); | + | |
| - | pin_set(led_yellow); | + | |
| - | pin_set(led_red); | + | |
| - | + | ||
| - | // Ekraani seadistamine | + | |
| lcd_gfx_init(); | lcd_gfx_init(); | ||
| - | |||
| - | // LCD ekraani puhastamine | ||
| - | lcd_gfx_clear(); | ||
| - | |||
| - | lcd_gfx_backlight(true); | ||
| - | |||
| - | // Kursori (nähtamatu) ekraani keskele viimine | ||
| lcd_gfx_goto_char_xy(3, | lcd_gfx_goto_char_xy(3, | ||
| - | |||
| - | // Programmi nime kuvamine | ||
| lcd_gfx_write_string(" | lcd_gfx_write_string(" | ||
| - | + | ||
| - | + | // Infinite loop | |
| while (1) | while (1) | ||
| { | { | ||
| - | // Loeb nuppude väärtused. | + | // Read button values |
| - | new_value1 = pin_get_debounced_value(button1); | + | new_value1 = pin_get_debounced_value(button1); |
| new_value2 = pin_get_debounced_value(button2); | new_value2 = pin_get_debounced_value(button2); | ||
| new_value3 = pin_get_debounced_value(button3); | new_value3 = pin_get_debounced_value(button3); | ||
| - | // Nupp S1 alla vajutatud. Registreeritakse ainult üks vajutus. | + | // S1 pressed. Register only one press. |
| if((!new_value1) && (old_value1)) | if((!new_value1) && (old_value1)) | ||
| { | { | ||
| - | // SD kaardi initsialiseerimine. Ebaõnnestumise korral seatakse vealipp. | + | // SD card initialization |
| + | // On failure, set error flag. | ||
| d_err_flag = disk_initialize(0); | d_err_flag = disk_initialize(0); | ||
| - | |||
| - | // Viide | ||
| sw_delay_ms(2); | sw_delay_ms(2); | ||
| - | // Failisüsteemi initsialiseerimine. Ebaõnnestumise korral seatakse vealipp. | + | // File system initialization |
| + | // On failure, set error flag. | ||
| f_err_flag = f_mount(0, & | f_err_flag = f_mount(0, & | ||
| - | |||
| - | // Viide | ||
| sw_delay_ms(2); | sw_delay_ms(2); | ||
| - | |||
| - | |||
| } | } | ||
| - | | + | // S2 pressed. Register only one press |
| if((!new_value2) && (old_value2)) | if((!new_value2) && (old_value2)) | ||
| { | { | ||
| - | // Faili "fail.txt" | + | // Open file "file.txt" |
| f_open(& | f_open(& | ||
| - | // Failist esimese | + | // Read first 14 characters from file |
| f_gets (read_buf, | f_gets (read_buf, | ||
| f_close(& | f_close(& | ||
| - | // Faili esimese | + | // Write the first 14 characters to the screen |
| lcd_gfx_goto_char_xy(0, | lcd_gfx_goto_char_xy(0, | ||
| lcd_gfx_write_string(read_buf); | lcd_gfx_write_string(read_buf); | ||
| Line 134: | Line 121: | ||
| } | } | ||
| - | // Nupp S3 vajutatud. Registreeritakse ainult üks vajutus | + | // S3 pressed. Register only one press |
| if((!new_value3) && (old_value3)) | if((!new_value3) && (old_value3)) | ||
| { | { | ||
| - | // Suvaline muutuja, mis salvestatakse faili. | + | // Random variable to store in file |
| variableName = 4; | variableName = 4; | ||
| - | // Kettale luuakse kaust " | + | // Create directory |
| f_mkdir(" | f_mkdir(" | ||
| - | // Loodud kausta tekitatakse tekstifail | + | // Create text file " |
| f_open(& | f_open(& | ||
| - | // Fail avatakse kirjutusreziimis. | + | // Open file for writing |
| f_open(& | f_open(& | ||
| - | // Faili kirjutamine. | + | // Write to file |
| f_printf(& | f_printf(& | ||
| - | | + | |
| f_close(& | f_close(& | ||
| } | } | ||
| - | |||
| - | // Jätab eelmise nupu väärtuse meelde | + | // Remember previous button values |
| old_value1 = new_value1; | old_value1 = new_value1; | ||
| old_value2 = new_value2; | old_value2 = new_value2; | ||
| old_value3 = new_value3; | old_value3 = new_value3; | ||
| - | + | // If SD card is initialized and OK, green LED on, | |
| - | // Kui SD-kaart on initsialisseritud ja töökorras, siis põleb roheline | + | // otherwise red LED on |
| - | // ja vastupidisel juhul punane | + | |
| if((f_err_flag == 0) && (d_err_flag == 0)) | if((f_err_flag == 0) && (d_err_flag == 0)) | ||
| { | { | ||
| Line 176: | Line 161: | ||
| } | } | ||
| - | // Ekraanile kuvatakse vealippude olekud. Need on järgnevad: | + | // Display error flag values: |
| - | // -1 initsialiseerimata | + | // -1 not initialized |
| - | // 0 viga ei ole | + | // 0 no error |
| - | // 1 (või suurem) viga | + | // 1 (or higher) error |
| sprintf(f_err_buf, | sprintf(f_err_buf, | ||
| sprintf(d_err_buf, | sprintf(d_err_buf, | ||
| Line 188: | Line 173: | ||
| lcd_gfx_write_string(d_err_buf); | lcd_gfx_write_string(d_err_buf); | ||
| - | // Viide | ||
| sw_delay_ms(2); | sw_delay_ms(2); | ||
| } | } | ||
| - | |||
| } | } | ||
| </ | </ | ||