Differences

This shows you the differences between two versions of the page.

Link to this comparison view

en:examples:storage:sdcard [2012/06/18 15:09] – created raivo.sellen: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>Needs translation</note> +//Required knowledge:  
-===== Teooria =====+[HW] [[et:hardware:homelab:controller]], [HW] [[et:hardware:homelab:digi]], \\ 
 +[LIB] [[et:software:homelab:library:pin]], [LIB] [[et:software:homelab:library:module:lcd_graphic]]//
  
-[{{  :et:examples:storage:dscn0633.jpg?220|Standardsuuruses SD kaart altvaates (Sinine).  Lisaks mikro SD kaart ja selle standardsuuruses adapter.}}]+===== 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. +{{ :et:examples:storage:sdcard.png?200|SD card pins}}
  
-SD kaart on iseenesest suur hulk mälupitte, mida saab küll muuta, aga muud seadmed ei oska salvestatud andmetega seljuhul midagi peale hakataProbleemi lahendamiseks kasutatakse failisüsteemi, mis annab võimaluse töötada kogu kaardi mälumaatriksi asemel failidegaFailisüsteemi üks tähtsamaid ülesandeid on organiseerida loogilisi faile füüsilisel salvestusseadmelSalvestusseadmel olev ruum on jaotatud sektoriteks, mille suurus on enamasti 512 baiti. Kuna failisüsteemil on otstarbekas töötada suuremate üksustega, grupeeritakse sektoreid klastriteks. Klaster on mingi täisarvuline hulk järjestikuliselt asuvaid sektoreidSuurema klastri kasutamisel väheneb suuremate failide puhul fragmentatsioon, kuid väiksemate failide puhul suureneb raisatud ruum, kuna osa klastreid jääb ainult osaliselt täidetuks.+Secure Digital (SD) is a memory card format developed by the SD Card Association (SDA) for portable devicesCommunication can use the card's SD interface or the SPI busSPI is a good option for simpler systemsThe SD card operating voltage is 3.3 V.
  
-Portatiivsetel mäluseadmetel ja mälukaartidel on laialt levinud FAT (File Allocation Table) failisüsteemmida toetavad kõik enamlevinud operatsioonisüsteemidFAT 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 klastriniFAT-i koral on klastrid adresseeritud n-bitiste kannetega aadressiruumi tabelissekus 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 modifiedbut other devices cannot use the stored data without a file systemThe file system enables working with files instead of the raw memory arrayOne of the main tasks of a file system is to organize logical files on the physical storage device. The storage space is divided into sectorsusually 512 bytes in size. Since it is more efficient to work with larger unitssectors are grouped into clustersA 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 microSDmicroSD is common in mobile phones and tablets and is also used in the HomeLab Controller moduleIn 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 kihtiEsimene ketta kiht on seotud otseselt kettaga suhtlemise ja initsialiseerimisegaLisaks 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 FAT12FAT16  ja FAT32 failisüsteemid+<pagebreak> 
-  + 
-Kodulabori teegis oleva FatFs failisüsteemi paketiga on võimalik lähemalt tutvuda [[http://elm-chan.org/fsw/ff/00index_e.html|siit]].+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 slotThe card is connected to the SPI busand 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://elm-chan.org/fsw/ff/00index_e.html|http://elm-chan.org/fsw/ff/00index_e.html]]. In FatFs, there are two layers for SD card communication. The first disk layer is in //diskio.h// and //mmc.c//. It handles direct communication with the disk and its initialization, including low-level read/write functions. For the end user, the most important function is //disk_initialize//, which initializes the selected card.
  
-Järgmine näide demonstreerib tekstifaili lugemist ja kirjutamistNupule S1 vajutades initsialiseeritakse ketas ja failissüsteemNupu S3 vajutamine loob kettale kaustakuhu omakorda luuakse fail. Faili kirjutatakse ka sisu. Nupp S2 kuvab tekitatud faili sisu ekraanile.+For the file system to work, the function //disk_timerproc// must be called at 100 HzThis is handled by a timer interruptThe HomeLab Controller module does not have a real-time clockso file timestamps are constant
  
-~~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, using //f_mount//. The function takes a file system object and returns a result code.
  
 +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 //FA_WRITE// opens the file for writing. Then you can write to the file with //f_write// or read with //f_read//. When done, close the file with //f_close//. FAT12, FAT16, and FAT32 file systems are supported and tested.
  
 +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 <stdio.h> #include <stdio.h>
 #include <homelab/module/ff.h> #include <homelab/module/ff.h>
Line 32: Line 52:
 #include <homelab/module/lcd_gfx.h> #include <homelab/module/lcd_gfx.h>
  
- +// Main program
-// LEDide viigud +
-pin led_red    = PIN(C, 5); +
-pin led_yellow = PIN(C, 4); +
-pin led_green  = PIN(C, 3); +
- +
-// 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; //Failisüsteemi vea lipp+ 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_set(led_yellow); 
- pin_setup_output(led_yellow); + pin_setup_output(led_green); pin_set(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, 2);  lcd_gfx_goto_char_xy(3, 2);
-  
- // Programmi nime kuvamine 
  lcd_gfx_write_string("SD Card");  lcd_gfx_write_string("SD Card");
- +  
- + // 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 vajutatudRegistreeritakse ainult üks vajutus.+ // S1 pressedRegister 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, &FATFS_Obj);  f_err_flag = f_mount(0, &FATFS_Obj);
- 
- // Viide 
  sw_delay_ms(2);  sw_delay_ms(2);
-  
-  
  
   
-     // Nupp S2 vajutatud. Registreeritakse ainult üks vajutus.+ // S2 pressedRegister only one press
  if((!new_value2) && (old_value2))  if((!new_value2) && (old_value2))
  {  {
- // Faili "fail.txt" avamine lugemisreziimis.+ // Open file "file.txt" for read
  f_open(&fil_obj, "/Homelab/fail.txt", FA_READ);  f_open(&fil_obj, "/Homelab/fail.txt", FA_READ);
  
- // Failist esimese 14 tähemärgi lugemine.+ // Read first 14 characters from file
  f_gets (read_buf,14, &fil_obj);  f_gets (read_buf,14, &fil_obj);
  f_close(&fil_obj);  f_close(&fil_obj);
  
- // Faili esimese 14 tähemärgi ekraanile kirjutamine.+ // Write the first 14 characters to the screen
  lcd_gfx_goto_char_xy(0, 0);  lcd_gfx_goto_char_xy(0, 0);
  lcd_gfx_write_string(read_buf);  lcd_gfx_write_string(read_buf);
Line 134: Line 121:
  }  }
    
- // Nupp S3 vajutatudRegistreeritakse ainult üks vajutus+ // S3 pressedRegister 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 "Homelab".+ // Create directory "Homelab" on disk
  f_mkdir("Homelab");   f_mkdir("Homelab");
   
- // Loodud kausta tekitatakse tekstifail "fail.txt".+ // Create text file "fail.txt" in the directory
  f_open(&fil_obj, "/Homelab/fail.txt", FA_CREATE_NEW);  f_open(&fil_obj, "/Homelab/fail.txt", FA_CREATE_NEW);
  
- // Fail avatakse kirjutusreziimis.+ // Open file for writing
  f_open(&fil_obj, "/Homelab/fail.txt", FA_WRITE);   f_open(&fil_obj, "/Homelab/fail.txt", FA_WRITE);
  
- // Faili kirjutamine.+ // Write to file
  f_printf(&fil_obj, "Variable: %d", variableName);  f_printf(&fil_obj, "Variable: %d", variableName);
    
-  // Faili sulgemine+  // Close file
  f_close(&fil_obj);  f_close(&fil_obj);
  }  }
-  
   
- // 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 OKgreen LED on, 
- // Kui SD-kaart on initsialisseritud ja töökorrassiis põleb roheline LED + // otherwise red LED on
- // ja vastupidisel juhul punane LED.+
  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 suuremviga + //  1 (or highererror
  sprintf(f_err_buf, "Error_f: %02d", f_err_flag );  sprintf(f_err_buf, "Error_f: %02d", f_err_flag );
  sprintf(d_err_buf, "Error_d: %02d", d_err_flag );  sprintf(d_err_buf, "Error_d: %02d", d_err_flag );
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);
  }  }
- 
 } }
 </code> </code>
  
en/examples/storage/sdcard.1340021358.txt.gz · Last modified: (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0