This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:examples:storage [2012/06/10 19:08] – eero.valjaots | en:examples:storage [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
- | <note important> | + | <note important> |
- | ===== Teooria ===== | + | |
- | [{{ : | ||
- | |||
- | Secure Digital (SD) is non-volatile memory card format. The Secure Digital standard is maintained by the SD Card Association (SDA) for use in portable electronic devices. SD-card working voltage is 3.3 V. Communication with a card is carried out by its SD input/ | ||
- | |||
- | SD-card is basically a large quantity of memory bits which can be modified but they are difficult to understand when interfaced with other devices. Therefore file system is used to solve this problem that enables to work with files instead of full memory matrix. The most important function of a file system is to organize files logically in hard drive. The memory volume is allocated to sectors which size is usually 512 bytes. As file system functionality is better when working with large units, sectors are organized to clusters. Clusters are some integer value of consecutive sectors. If cluster size is large, large file fragmentation decreases but wasted space increases when files are small as some clusters are only partly filled. | ||
- | |||
- | Portative memory devices and cards use usually FAT (File Allocation Table) file system which is supported by all common operation systems. FAT file system file location table includes every file starting cluster address in drive which in turn includes index to next cluster related to the file and so on, until the ending cluster of file. Clusters in FAT are indexed with 12 b(FAT12), 16 b (FAT16) or 32 b (FAT32) size table addresses depending on version. Therefore older FAT versions caused large cluster sizes which is inefficient use of space in case of large drive volumes. | ||
- | |||
- | ===== Practical example ===== | ||
- | |||
- | HomeLab controller module has a slot for Micro SD memory card. This is connected to the same SPI bus that Ethernet controller uses. | ||
- | |||
- | HomeLab library has two layers for communicating with SD-card. The first layer is directly related to communicating with drive and initialization. There are also functions to read and write to drive. The other file system layer communicates with the first layer and allows operations with files. Supported and tested are FAT12, FAT16 and FAT32 file systems. | ||
- | |||
- | Overview of FatFs file system package used in HomeLab library: | ||
- | [[http:// | ||
- | |||
- | The following example demonstrates reading and writing to a text file. If button S1 is pressed, drive and memory system is initialized. Pressing button S3 will create a directory to the drive, to where a new file with content is also created. Button S2 displays the content of created file in LCD. | ||
- | |||
- | ~~PB~~ | ||
- | |||
- | |||
- | <code c> | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | |||
- | |||
- | // 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 f_err_flag = -1; //Ketta vea lipp | ||
- | int d_err_flag = -1; // | ||
- | char f_err_buf[16]; | ||
- | char d_err_buf[16]; | ||
- | |||
- | int variableName = 0; | ||
- | |||
- | static FATFS FATFS_Obj; | ||
- | FIL fil_obj; | ||
- | |||
- | char read_buf[20]; | ||
- | |||
- | unsigned char new_value1, old_value1 = 0; | ||
- | unsigned char new_value2, old_value2 = 0; | ||
- | unsigned char new_value3, old_value3 = 0; | ||
- | |||
- | |||
- | // Seab LEDid töökorda | ||
- | pin_setup_output(led_red); | ||
- | pin_setup_output(led_yellow); | ||
- | pin_setup_output(led_green); | ||
- | |||
- | // Seab nupud töökorda | ||
- | 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 ekraani puhastamine | ||
- | lcd_gfx_clear(); | ||
- | |||
- | lcd_gfx_backlight(true); | ||
- | |||
- | // Kursori (nähtamatu) ekraani keskele viimine | ||
- | lcd_gfx_goto_char_xy(3, | ||
- | |||
- | // Programmi nime kuvamine | ||
- | lcd_gfx_write_string(" | ||
- | |||
- | |||
- | while (1) | ||
- | { | ||
- | // Loeb nuppude väärtused. | ||
- | | ||
- | new_value2 = pin_get_debounced_value(button2); | ||
- | new_value3 = pin_get_debounced_value(button3); | ||
- | |||
- | // Nupp S1 alla vajutatud. Registreeritakse ainult üks vajutus. | ||
- | if((!new_value1) && (old_value1)) | ||
- | { | ||
- | // SD kaardi initsialiseerimine. Ebaõnnestumise korral seatakse vealipp. | ||
- | d_err_flag = disk_initialize(0); | ||
- | |||
- | // Viide | ||
- | sw_delay_ms(2); | ||
- | |||
- | // Failisüsteemi initsialiseerimine. Ebaõnnestumise korral seatakse vealipp. | ||
- | f_err_flag = f_mount(0, & | ||
- | |||
- | // Viide | ||
- | sw_delay_ms(2); | ||
- | |||
- | |||
- | } | ||
- | |||
- | // Nupp S2 vajutatud. Registreeritakse ainult üks vajutus. | ||
- | if((!new_value2) && (old_value2)) | ||
- | { | ||
- | // Faili " | ||
- | f_open(& | ||
- | |||
- | // Failist esimese 14 tähemärgi lugemine. | ||
- | f_gets (read_buf, | ||
- | f_close(& | ||
- | |||
- | // Faili esimese 14 tähemärgi ekraanile kirjutamine. | ||
- | lcd_gfx_goto_char_xy(0, | ||
- | lcd_gfx_write_string(read_buf); | ||
- | |||
- | } | ||
- | |||
- | // Nupp S3 vajutatud. Registreeritakse ainult üks vajutus | ||
- | if((!new_value3) && (old_value3)) | ||
- | { | ||
- | // Suvaline muutuja, mis salvestatakse faili. | ||
- | variableName = 4; | ||
- | |||
- | // Kettale luuakse kaust " | ||
- | f_mkdir(" | ||
- | |||
- | // Loodud kausta tekitatakse tekstifail " | ||
- | f_open(& | ||
- | |||
- | // Fail avatakse kirjutusreziimis. | ||
- | f_open(& | ||
- | |||
- | // Faili kirjutamine. | ||
- | f_printf(& | ||
- | |||
- | // Faili sulgemine | ||
- | f_close(& | ||
- | } | ||
- | |||
- | |||
- | // Jätab eelmise nupu väärtuse meelde | ||
- | old_value1 = new_value1; | ||
- | old_value2 = new_value2; | ||
- | old_value3 = new_value3; | ||
- | |||
- | |||
- | // Kui SD-kaart on initsialisseritud ja töökorras, | ||
- | // ja vastupidisel juhul punane LED. | ||
- | if((f_err_flag == 0) && (d_err_flag == 0)) | ||
- | { | ||
- | pin_clear(led_green); | ||
- | pin_set(led_red); | ||
- | } | ||
- | else | ||
- | { | ||
- | pin_set(led_green); | ||
- | pin_clear(led_red); | ||
- | } | ||
- | |||
- | // Ekraanile kuvatakse vealippude olekud. Need on järgnevad: | ||
- | // -1 initsialiseerimata | ||
- | // 0 viga ei ole | ||
- | // 1 (või suurem) viga | ||
- | sprintf(f_err_buf, | ||
- | sprintf(d_err_buf, | ||
- | |||
- | lcd_gfx_goto_char_xy(0, | ||
- | lcd_gfx_write_string(f_err_buf); | ||
- | lcd_gfx_goto_char_xy(0, | ||
- | lcd_gfx_write_string(d_err_buf); | ||
- | |||
- | // Viide | ||
- | sw_delay_ms(2); | ||
- | } | ||
- | |||
- | } | ||
- | </ |