/* * C Implementation: xtc_mmc * * Description: * * * Author: Malte Schmidt , (C) 2008 * * Copyright: Malte Schmidt, all rights reseved * */ #include "Opencharge.h" #include "fat16.h" #include "fat16_config.h" #include "partition.h" #include "sd_raw.h" #include "sd_raw_config.h" uint8_t find_file_in_dir(struct fat16_fs_struct* fs, struct fat16_dir_struct* dd, const char* name, struct fat16_dir_entry_struct* dir_entry) { while(fat16_read_dir(dd, dir_entry)) { if(strcmp(dir_entry->long_name, name) == 0) { fat16_reset_dir(dd); return 1; } } return 0; } struct fat16_file_struct* open_file_in_dir(struct fat16_fs_struct* fs, struct fat16_dir_struct* dd, const char* name) { struct fat16_dir_entry_struct file_entry; if(!find_file_in_dir(fs, dd, name, &file_entry)) return 0; return fat16_open_file(fs, &file_entry); } uint8_t write_mmc() { struct fat16_dir_entry_struct directory; struct fat16_fs_struct* fs = NULL; struct fat16_file_struct* fd = NULL; struct fat16_dir_struct* dd = NULL; struct partition_struct* partition = NULL; if (!sd_raw_init()) { printf_P(PSTR("init error")); goto err; } /* open first partition */ partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, sd_raw_write_interval, 0 ); if(!partition) { /* If the partition did not open, assume the storage device * is a "superfloppy", i.e. has no MBR. */ partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, sd_raw_write_interval, -1 ); if(!partition) { printf_P(PSTR("opening partition failed\n")); goto err; } } /* open file system */ fs = fat16_open(partition); if(!fs) { printf_P(PSTR("opening filesystem failed\n")); goto err; } /* open root directory */ fat16_get_dir_entry_of_path(fs, "/", &directory); dd = fat16_open_dir(fs, &directory); if(!dd) { printf_P(PSTR("openen directoy failed\n")); goto err; } /* if (! fat16_create_file(dd, "test.txt", &directory )) { printf("error creating file\n"); // goto err; }*/ fd = open_file_in_dir(fs, dd, "test1.txt"); if(!fd) { printf_P(PSTR("error opening file")); goto err; } fat16_resize_file(fd, 9); uint8_t buffer[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; printf_P(PSTR("w: %i"), fat16_write_file(fd, buffer, 9)); WAIT_FOR_KEY(); err: if(fd) fat16_close_file(fd); if(dd) fat16_close_dir(dd); if(fs) /* close file system */ fat16_close(fs); if(partition) /* close partition */ partition_close(partition); printf_P(PSTR("fehler")); WAIT_FOR_KEY(); return 1; }