|
7 | 7 |
|
8 | 8 | #include <avr/io.h>
|
9 | 9 |
|
| 10 | +// Device SPI driver |
| 11 | +#include "spi/spi_master.h" |
10 | 12 |
|
11 |
| -int main(void) |
12 |
| -{ |
13 |
| - /* Replace with your application code */ |
14 |
| - while (1) |
15 |
| - { |
16 |
| - } |
| 13 | +// Library supplied SD card driver |
| 14 | +#include "sd-driver/sd_driver.h" |
| 15 | + |
| 16 | +// SlimFAT file system driver |
| 17 | +#include "slimfat/slimfat.h" |
| 18 | + |
| 19 | +// Device buffer |
| 20 | +uint8_t buffer[SECTOR_SIZE] = { 0 }; |
| 21 | + |
| 22 | +int main(void) { |
| 23 | + spi_master_init(); |
| 24 | + |
| 25 | + // Create handle for SD card connected to SPI |
| 26 | + sd_card_t sd_card = GET_SD_HANDLE(spi_master_transfer, spi_slave_sd_select); |
| 27 | + |
| 28 | + // Create handle for generic storage device with sector buffer |
| 29 | + fs_storage_device storage_dev = GET_DEV_HANDLE(buffer, &sd_card, sd_card_read, sd_card_write); |
| 30 | + |
| 31 | + // Create handle for partition to be mounted |
| 32 | + fs_partition_t partition = GET_PART_HANDLE(storage_dev); |
| 33 | + |
| 34 | + // Initialize SD card |
| 35 | + sd_card_err err = sd_card_init(&sd_card); |
| 36 | + if(SD_SUCCESS == err) { |
| 37 | + // Mount selected partition (0 - 3) |
| 38 | + if (FS_SUCCESS == fs_mount(&partition, 0)) { |
| 39 | + uint8_t line_buff[100] = { 0 }; |
| 40 | + |
| 41 | + /* Read existing files on mounted partition */ |
| 42 | + fs_file_t read_file = GET_FILE_HANDLE(partition); |
| 43 | + if (FS_SUCCESS == fs_fopen(&read_file, "read.txt", READ)) { |
| 44 | + /* Access data stored in file */ |
| 45 | + fs_fgetc(&read_file); |
| 46 | + fs_fgets(&read_file, line_buff, sizeof(line_buff)); |
| 47 | + fs_fread(&read_file, line_buff, 100); |
| 48 | + |
| 49 | + /* Navigate file */ |
| 50 | + fs_fseek(&read_file, 300, FS_SEEK_SET); |
| 51 | + } |
| 52 | + |
| 53 | + /* Write existing files or create new on mounted partition */ |
| 54 | + fs_file_t write_file = GET_FILE_HANDLE(partition); |
| 55 | + if (FS_SUCCESS == fs_fopen(&write_file, "write.txt", WRITE)) { |
| 56 | + /* Store data to file */ |
| 57 | + fs_fputc(&write_file, 'a'); |
| 58 | + fs_fputs(&write_file, "Hello, world!"); |
| 59 | + fs_fflush(&write_file); |
| 60 | + fs_fwrite(&write_file, line_buff, 100); |
| 61 | + |
| 62 | + /* Close file to make sure data is flushed */ |
| 63 | + fs_fclose(&write_file); |
| 64 | + } |
| 65 | + |
| 66 | + /* Append existing file on mounted partition */ |
| 67 | + fs_file_t append_file = GET_FILE_HANDLE(partition); |
| 68 | + if (FS_SUCCESS == fs_fopen(&append_file, "append.txt", APPEND)) { |
| 69 | + /* Store data to file */ |
| 70 | + fs_fputs(&append_file, "This is simple append."); |
| 71 | + fs_fclose(&append_file); |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + } |
| 76 | + while (1); |
17 | 77 | }
|
18 | 78 |
|
0 commit comments