/* * Copyright (c) 2006-2009 by Roland Riegel * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ #include #include #include #include "fat.h" #include "fat_config.h" #include "partition.h" #include "sd_raw.h" #include "sd_raw_config.h" #include "uart.h" #include "bool.h" #define DEBUG 1 #define BUFFERSIZE 25 #define BEG FAT_SEEK_SET #define END FAT_SEEK_END #define AKT FAT_SEEK_CUR #define CD 1 #define LS 2 #define CAT 3 #define DISK 4 #define RM 5 #define TOUCH 6 #define WRITE 7 #define MKDIR 8 #define SYNC 9 //static uint8_t read_line(char* buffer, uint8_t buffer_length); //static uint32_t strtolong(const char* str); static uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry); static struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name); static uint8_t print_disk_info(const struct fat_fs_struct* fs); int main() { char file[BUFFERSIZE]; char data[BUFFERSIZE]; uint8_t length = 0; uint32_t pos = 0; uint8_t whence = BEG; bool idle = true; uint8_t zaehler = 0; uint8_t iToDo = 0; // we will just use ordinary idle mode set_sleep_mode(SLEEP_MODE_IDLE); // setup uart uart_init(); uart_puts_p(PSTR("Start\n")); while(1) { uart_puts_p(PSTR("init\n")); // setup sd card slot if(!sd_raw_init()) { #if DEBUG uart_puts_p(PSTR("MMC/SD initialization failed\n")); #endif continue; } // open first partition struct partition_struct* partition = partition_open(sd_raw_read, sd_raw_read_interval, #if SD_RAW_WRITE_SUPPORT sd_raw_write, sd_raw_write_interval, #else 0, 0, #endif 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, #if SD_RAW_WRITE_SUPPORT sd_raw_write, sd_raw_write_interval, #else 0, 0, #endif -1 ); if(!partition) { #if DEBUG uart_puts_p(PSTR("opening partition failed\n")); #endif continue; } } // open file system struct fat_fs_struct* fs = fat_open(partition); if(!fs) { #if DEBUG uart_puts_p(PSTR("opening filesystem failed\n")); #endif continue; } // open root directory struct fat_dir_entry_struct directory; fat_get_dir_entry_of_path(fs, "/", &directory); struct fat_dir_struct* dd = fat_open_dir(fs, &directory); if(!dd) { #if DEBUG uart_puts_p(PSTR("opening root directory failed\n")); #endif continue; } // print some card information as a boot message */ print_disk_info(fs); while(1) { idle = true; iToDo = 0; while(idle) { switch(zaehler) { case 0: uart_puts_p(PSTR("schreibe:\n")); strcpy(file,"test.txt"); strcpy(data,"Hallo Test Test"); length = 15; idle = false; whence = BEG; iToDo = WRITE; zaehler ++; break; case 1: uart_puts_p(PSTR("Touch:\n")); strcpy(file,"messung1.txt"); idle = false; whence = BEG; iToDo = TOUCH; zaehler ++; break; case 2: uart_puts_p(PSTR("schreibe2:\n")); strcpy(file,"messung1.txt"); strcpy(data,"ADC: 1234567890"); length = 15; idle = false; whence = BEG; iToDo = WRITE; zaehler ++; break; case 3: uart_puts_p(PSTR("schreibe3:\n")); strcpy(file,"test.txt"); strcpy(data,"hahahahahahaha"); length = 14; idle = false; whence = END; iToDo = WRITE; zaehler ++; break; case 4: uart_puts_p(PSTR("Fertig\n")); zaehler ++; break; } } switch(iToDo) { case 0: break; case CD: // change directory { struct fat_dir_entry_struct subdir_entry; if(find_file_in_dir(fs, dd, file, &subdir_entry)) { struct fat_dir_struct* dd_new = fat_open_dir(fs, &subdir_entry); if(dd_new) { fat_close_dir(dd); dd = dd_new; continue; } } uart_puts_p(PSTR("directory not found: ")); uart_puts(file); uart_putc('\n'); } break; case LS: // print directory listing { struct fat_dir_entry_struct dir_entry; while(fat_read_dir(dd, &dir_entry)) { uint8_t spaces = sizeof(dir_entry.long_name) - strlen(dir_entry.long_name) + 4; uart_puts(dir_entry.long_name); uart_putc(dir_entry.attributes & FAT_ATTRIB_DIR ? '/' : ' '); while(spaces--) uart_putc(' '); uart_putdw_dec(dir_entry.file_size); uart_putc('\n'); } } break; case CAT: // search file in current directory and open it { struct fat_file_struct* fd = open_file_in_dir(fs, dd, file); if(!fd) { uart_puts_p(PSTR("error opening ")); uart_puts(file); uart_putc('\n'); continue; } uint8_t buffer[8]; uint32_t offset = 0; memset(data,0,sizeof(data)); int i,ii; for(i = 0; (i < length) && (fat_read_file(fd, buffer, sizeof(buffer)) > 0);i+=8) { if(ipos) && (i+8pos) { data[offset] = buffer[ii]; offset++; } } } else { for(ii = 0;i<8;i++) { data[offset] = buffer[ii]; offset++; } } for(ii = 0;i<8;i++) { data[offset] = buffer[ii]; offset++; } } fat_close_file(fd); } break; case DISK: { if(!print_disk_info(fs)) uart_puts_p(PSTR("error reading disk info\n")); } break; case RM: { struct fat_dir_entry_struct file_entry; if(find_file_in_dir(fs, dd, file, &file_entry)) { if(fat_delete_file(fs, &file_entry)) continue; } uart_puts_p(PSTR("error deleting file: ")); uart_puts(file); uart_putc('\n'); } break; case TOUCH: { struct fat_dir_entry_struct file_entry; if(!fat_create_file(dd, file, &file_entry)) { uart_puts_p(PSTR("error creating file: ")); uart_puts(file); uart_putc('\n'); } } break; case WRITE: { uart_puts_p(PSTR("in: ")); uart_puts(file); uart_puts_p(PSTR(" << ")); uart_puts(data); uart_puts_p(PSTR(" \n")); struct fat_file_struct* fd = open_file_in_dir(fs,dd,file); if(!fd) { uart_puts_p(PSTR("error opening ")); uart_puts(file); uart_putc('\n'); continue; } if(!fat_seek_file(fd, &pos, whence)) { uart_puts_p(PSTR("error seeking on ")); uart_puts(file); uart_putc('\n'); fat_close_file(fd); continue; } ; int i =0; for(i=0;i<2;i++) { if(fat_write_file(fd, (uint8_t*) data, length) != length) { uart_puts_p(PSTR("error writing to file\n")); break; } } fat_close_file(fd); uart_puts_p(PSTR("geschrieben:\n")); uart_puts_p(PSTR("in: ")); uart_puts(file); uart_puts_p(PSTR(" << ")); uart_puts(data); uart_puts_p(PSTR(" \n")); } break; case MKDIR: { struct fat_dir_entry_struct dir_entry; if(!fat_create_dir(dd, file, &dir_entry)) { uart_puts_p(PSTR("error creating directory: ")); uart_puts(file); uart_putc('\n'); } } break; case SYNC: { if(!sd_raw_sync()) uart_puts_p(PSTR("error syncing disk\n")); } break; default: { uart_puts_p(PSTR("unknown command: ")); uart_puts(file); uart_putc('\n'); } } iToDo = 0; } // close file system fat_close(fs); // close partition partition_close(partition); } return 0; } uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry) { while(fat_read_dir(dd, dir_entry)) { if(strcmp(dir_entry->long_name, name) == 0) { fat_reset_dir(dd); return 1; } } return 0; } struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name) { struct fat_dir_entry_struct file_entry; if(!find_file_in_dir(fs, dd, name, &file_entry)) return 0; return fat_open_file(fs, &file_entry); } uint8_t print_disk_info(const struct fat_fs_struct* fs) { if(!fs) return 0; struct sd_raw_info disk_info; if(!sd_raw_get_info(&disk_info)) return 0; uart_puts_p(PSTR("manuf: 0x")); uart_putc_hex(disk_info.manufacturer); uart_putc('\n'); uart_puts_p(PSTR("oem: ")); uart_puts((char*) disk_info.oem); uart_putc('\n'); uart_puts_p(PSTR("prod: ")); uart_puts((char*) disk_info.product); uart_putc('\n'); uart_puts_p(PSTR("rev: ")); uart_putc_hex(disk_info.revision); uart_putc('\n'); uart_puts_p(PSTR("serial: 0x")); uart_putdw_hex(disk_info.serial); uart_putc('\n'); uart_puts_p(PSTR("date: ")); uart_putw_dec(disk_info.manufacturing_month); uart_putc('/'); uart_putw_dec(disk_info.manufacturing_year); uart_putc('\n'); uart_puts_p(PSTR("size: ")); uart_putdw_dec(disk_info.capacity / 1024 / 1024); uart_puts_p(PSTR("MB\n")); uart_puts_p(PSTR("copy: ")); uart_putw_dec(disk_info.flag_copy); uart_putc('\n'); uart_puts_p(PSTR("wr.pr.: ")); uart_putw_dec(disk_info.flag_write_protect_temp); uart_putc('/'); uart_putw_dec(disk_info.flag_write_protect); uart_putc('\n'); uart_puts_p(PSTR("format: ")); uart_putw_dec(disk_info.format); uart_putc('\n'); uart_puts_p(PSTR("free: ")); uart_putdw_dec(fat_get_fs_free(fs)); uart_putc('/'); uart_putdw_dec(fat_get_fs_size(fs)); uart_putc('\n'); return 1; } /* uint8_t read_line(char* buffer, uint8_t buffer_length) { memset(buffer, 0, buffer_length); uint8_t read_length = 0; while(read_length < buffer_length - 1) { uint8_t c = uart_getc(); if(c == 0x08 || c == 0x7f) { if(read_length < 1) continue; --read_length; buffer[read_length] = '\0'; uart_putc(0x08); uart_putc(' '); uart_putc(0x08); continue; } uart_putc(c); if(c == '\n') { buffer[read_length] = '\0'; break; } else { buffer[read_length] = c; ++read_length; } return read_length; } uint32_t strtolong(const char* str) { uint32_t l = 0; while(*str >= '0' && *str <= '9') l = l * 10 + (*str++ - '0'); return l; } */