static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset, ulong size, void *buf) { loff_t actread; int ret; char *filename = (char *)load->filename; ret = fat_read_file(filename, buf, file_offset, size, &actread); if (ret) return ret; return actread; }
void read_wav_header(struct fat_file_struct* file, unsigned char * header) { char field[2]; fat_read_file(file, header, 44); //Read the wav file header //Extract the Sample Rate field from the header sprintf(field, "%x%x", header[25], header[24]); str2int(field, &wave_info.sample_rate); //Extract the audio format from the header sprintf(field, "%x%x", header[21], header[20]); str2int(field, &wave_info.format); //Extract the bits per sample from the header sprintf(field, "%x%x", header[35], header[34]); str2int(field, &wave_info.bits_per_sample); return; }
// reads a file from the SD card one line at a time int fat_read_line(struct fat_file_struct* fd, uint8_t* buffer, uint8_t buffer_len) { char internal[INI_MAX_LINE]; int bytesRead, bytesToCopy; int32_t offset; char* pchar; bytesRead = fat_read_file(fd, internal, INI_MAX_LINE); #if INI_DEBUG printf("read_line start: bytesRead=%d\r\n", bytesRead); #endif if(bytesRead > 0) { // 0 on EOF, bytes read for >0 pchar = strchr(internal, '\n'); // return a pointer to the first newline if(pchar) { // if newline found, compute byte number for matching char bytesToCopy = pchar - internal + 1; } else { // if newline not found, copy all the bytes read bytesToCopy = bytesRead; } if(bytesToCopy > buffer_len) { // warn user or exit? bytesToCopy = buffer_len; } // copy the bytes to the destination buffer strncpy0(buffer, internal, bytesToCopy); // seek backwards from current file pointer to end of copied text offset = bytesToCopy - bytesRead; //offset = bytesToCopy; #if INI_DEBUG printf( "offset=%d, bytesRead=%d, bytesToCopy=%d\r\n", (int)offset, bytesRead, bytesToCopy); #endif if(!fat_seek_file(fd, &offset, FAT_SEEK_CUR)) { //if(!fat_seek_file(fd, &offset, FAT_SEEK_SET)) { #if INI_DEBUG printf("fat_seek_file: failure\r\n"); #endif } #if INI_DEBUG printf("after seek: offset=%d\r\n", offset); #endif return bytesToCopy; } return bytesRead; }
void cmd_readtest (uint64_t cluster, uint64_t size) { char buf[512]; uint64_t clustersize = size / 512; float time_before = SYSTIME; // Bytes lesen for (uint64_t b = 0; b < clustersize + 1; ++b) fat_read_file (cluster, (unsigned char *)buf, b); float duration = SYSTIME - time_before; uart_puts_P("Read "); uart_puts(num2str(size/1000, 10)); uart_puts_P(" kBytes in "); uart_puts(num2str(duration * 1000, 10)); uart_puts_P(" milliseconds.\r\n"); uart_puts_P("(Speed: "); uart_puts(num2str(((size / 1000) / duration), 10)); uart_puts_P(" kByte/s)\r\n"); }
void cmd_cat (uint64_t cluster, uint64_t size) { uart_puts_P("|-------------------------CAT--------------------------|\r\n"); char buf[512]; uint64_t clustersize = size / 512; uint16_t restsize = 512; // Datei ueber UART ausgeben for (uint64_t b = 0; b < clustersize + 1; ++b) { fat_read_file (cluster, (unsigned char *)buf, b); if (clustersize == b) restsize = size % 512; for (int a = 0; a < restsize; ++a) { uart_putc(buf[a]); if (buf[a] == '\n') uart_putc('\r'); } } uart_puts_P("\r\n|-------------------------/CAT-------------------------|\r\n"); }
int main() { /* we will just use ordinary idle mode */ set_sleep_mode(SLEEP_MODE_IDLE); /* setup uart */ uart_init(); while(1) { /* 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); /* provide a simple shell */ char buffer[24]; while(1) { /* print prompt */ uart_putc('>'); uart_putc(' '); /* read command */ char* command = buffer; if(read_line(command, sizeof(buffer)) < 1) continue; /* execute command */ if(strcmp_P(command, PSTR("init")) == 0) { break; } else if(strncmp_P(command, PSTR("cd "), 3) == 0) { command += 3; if(command[0] == '\0') continue; /* change directory */ struct fat_dir_entry_struct subdir_entry; if(find_file_in_dir(fs, dd, command, &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(command); uart_putc('\n'); } else if(strcmp_P(command, PSTR("ls")) == 0) { /* 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'); } } else if(strncmp_P(command, PSTR("cat "), 4) == 0) { command += 4; if(command[0] == '\0') continue; /* search file in current directory and open it */ struct fat_file_struct* fd = open_file_in_dir(fs, dd, command); if(!fd) { uart_puts_p(PSTR("error opening ")); uart_puts(command); uart_putc('\n'); continue; } /* print file contents */ uint8_t buffer[8]; uint32_t offset = 0; while(fat_read_file(fd, buffer, sizeof(buffer)) > 0) { uart_putdw_hex(offset); uart_putc(':'); for(uint8_t i = 0; i < 8; ++i) { uart_putc(' '); uart_putc_hex(buffer[i]); } uart_putc('\n'); offset += 8; } fat_close_file(fd); } else if(strcmp_P(command, PSTR("disk")) == 0) { if(!print_disk_info(fs)) uart_puts_p(PSTR("error reading disk info\n")); } #if FAT_WRITE_SUPPORT else if(strncmp_P(command, PSTR("rm "), 3) == 0) { command += 3; if(command[0] == '\0') continue; struct fat_dir_entry_struct file_entry; if(find_file_in_dir(fs, dd, command, &file_entry)) { if(fat_delete_file(fs, &file_entry)) continue; } uart_puts_p(PSTR("error deleting file: ")); uart_puts(command); uart_putc('\n'); } else if(strncmp_P(command, PSTR("touch "), 6) == 0) { command += 6; if(command[0] == '\0') continue; struct fat_dir_entry_struct file_entry; if(!fat_create_file(dd, command, &file_entry)) { uart_puts_p(PSTR("error creating file: ")); uart_puts(command); uart_putc('\n'); } } else if(strncmp_P(command, PSTR("write "), 6) == 0) { command += 6; if(command[0] == '\0') continue; char* offset_value = command; while(*offset_value != ' ' && *offset_value != '\0') ++offset_value; if(*offset_value == ' ') *offset_value++ = '\0'; else continue; /* search file in current directory and open it */ struct fat_file_struct* fd = open_file_in_dir(fs, dd, command); if(!fd) { uart_puts_p(PSTR("error opening ")); uart_puts(command); uart_putc('\n'); continue; } int32_t offset = strtolong(offset_value); if(!fat_seek_file(fd, &offset, FAT_SEEK_SET)) { uart_puts_p(PSTR("error seeking on ")); uart_puts(command); uart_putc('\n'); fat_close_file(fd); continue; } /* read text from the shell and write it to the file */ uint8_t data_len; while(1) { /* give a different prompt */ uart_putc('<'); uart_putc(' '); /* read one line of text */ data_len = read_line(buffer, sizeof(buffer)); if(!data_len) break; /* write text to file */ if(fat_write_file(fd, (uint8_t*) buffer, data_len) != data_len) { uart_puts_p(PSTR("error writing to file\n")); break; } } fat_close_file(fd); } else if(strncmp_P(command, PSTR("mkdir "), 6) == 0) { command += 6; if(command[0] == '\0') continue; struct fat_dir_entry_struct dir_entry; if(!fat_create_dir(dd, command, &dir_entry)) { uart_puts_p(PSTR("error creating directory: ")); uart_puts(command); uart_putc('\n'); } } #endif #if SD_RAW_WRITE_BUFFERING else if(strcmp_P(command, PSTR("sync")) == 0) { if(!sd_raw_sync()) uart_puts_p(PSTR("error syncing disk\n")); } #endif else if(strcmp_P(command, PSTR("sync")) == 0) { // vytvor adresar mereni_teploty // nekonecna smycka - pomoci RTCka kazdou minutu odmer teplotu } else { uart_puts_p(PSTR("unknown command: ")); uart_puts(command); uart_putc('\n'); } } /* close directory */ fat_close_dir(dd); /* close file system */ fat_close(fs); /* close partition */ partition_close(partition); } return 0; }
void loop() { int bytes_read=0; //Keeps track of how many bytes are read when accessing a file on the SD card. //Init Timer 1 //Used for 45uS Interrupt TCCR1A = 0; //Set Timer to normal mode TCCR1B = 0x0A; //Set Timer clock to 2 MHz. Clear timer on compare TIMSK1 = 0x02; //Enable Timer 1 Compare A Interrupt; OCR1AH = 0X00; //Count to 90 before triggering an interrupt. Counting to 90 with a 2MHz clock makes OCR1AL = 0x5A; //the interrupt trigger at 22.222kHz init_filesystem(); //Initialize the FAT16 file system on the SD card. if(get_wav_filename(dd, file_name)); //Find the first WAV file on the SD card (must be in the root directory) else while(1); //If a WAV file isn't found then the sketch is stopped here. //Open the file file_handle=open_file_in_dir(fs, dd, file_name); //Read the header information. Alternate purpose is to get to the DATA offset of the file. read_wav_header(file_handle, header); //Set the initial play buffer, and grab the initial data from the SD card. play_buffer=0; bytes_read = fat_read_file(file_handle, buffer1, BUFFERSIZE); bytes_read = fat_read_file(file_handle, buffer2, BUFFERSIZE); //Enable interrupts to start the wav playback. sei(); while(1){ if(need_new_data==1) //need_new_data flag is set by ISR to indicate a buffer is empty and should be refilled { need_new_data=0; //Clear the flag. if(play_buffer==0) //play_buffer indicates which buffer is now empty { //Get the next BUFFERSIZE bytes from the file. bytes_read = fat_read_file(file_handle, buffer1, BUFFERSIZE); } else { //Get the next BUFFERSIZE bytes from the file. bytes_read = fat_read_file(file_handle, buffer2, BUFFERSIZE); } new_buffer_ready=1; //new_buffer_ready flag tells the ISR that the buffer has been filled. //If file_read returns 0 or -1 file is over. Find the next file! if(bytes_read<=0) { cli(); //Disable interrupts to stop playback. fat_close_file(file_handle); //Close the current file //Find the next WAV file in the SD card fat_reset_dir(dd); //Make sure we start searching from the beginning of the directory find_file_in_dir(fs, dd, file_name, &dir_entry); //Navigate to the current file in the directory if(get_wav_filename(dd, file_name)); else while(1); //If we don't find another wav file, stop everything! //If we get here we've found another wav file. Open it! file_handle=open_file_in_dir(fs, dd, file_name); //Get the file header and load the initial song data. read_wav_header(file_handle, header); play_buffer=0; bytes_read = fat_read_file(file_handle, buffer1, BUFFERSIZE); bytes_read = fat_read_file(file_handle, buffer2, BUFFERSIZE); sei(); //Start playing the song } } } }
intptr_t File::read(uint8_t* buffer, uintptr_t buffer_len) { return fat_read_file(_fd, buffer, buffer_len); }
int FAT::read(char * data_buffer) { return fat_read_file(_file_handle, (uint8_t *)data_buffer, 512); }
int syscall_file_read(int fp, void* buf, unsigned int size) { return fat_read_file(fp, buf, size); }
void fetchNextByte() { int16_t read = fat_read_file(file, &next_byte, 1); has_more = read > 0; }
intptr_t SDCardFile::read(uint8_t *buf, uint8_t len) { if (fd == NULL) return -1; return fat_read_file(fd, buf, len); }
uint16_t RepRapSDCard::read_file(File f, uint8_t* buffer, uint16_t buffer_len) { return fat_read_file(f, buffer, buffer_len); }