int main(void) { char buffer_name[10]; short int handler; alt_up_sd_card_dev *device_reference = NULL; int connected = 0; device_reference = alt_up_sd_card_open_dev("/dev/SD_Card"); if (device_reference != NULL) { while(1) { if ((connected == 0) && (alt_up_sd_card_is_Present())) { printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()) { printf("FAT16 file system detected.\n"); //Print out the list in the sd card handler = alt_up_sd_card_find_first("", buffer_name); printf("%s \n", buffer_name); while ((handler = alt_up_sd_card_find_next(buffer_name)) != -1) printf("%s \n", buffer_name); }else{ printf("Unknown file system.\n"); } connected = 1; } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) { printf("Card disconnected.\n"); connected = 0; } } } return 0; }
/* * Overview: exercise 3 reads sd_card and list its file. * */ void sd_card(void){ char file_name[10]; alt_up_sd_card_dev *device_reference = NULL; int connected = 0; device_reference = alt_up_sd_card_open_dev(ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME); if (device_reference != NULL) { while(1) { if ((connected == 0) && (alt_up_sd_card_is_Present())) { //printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()) { //printf("FAT16 file system detected.\n"); if(alt_up_sd_card_find_first(".", (char*) file_name)==0){ //printf("%s\n",file_name); while(alt_up_sd_card_find_next((char*)file_name)==0) { //printf("%s\n", file_name); } } } else { //printf("Unknown file system.\n"); } connected = 1; } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) { //printf("Card disconnected.\n"); connected = 0; } break; } } else { //printf("Card could not be opened\n"); } //printf("sd_card function finished\n"); }
void load_file(char* filename, void (*func)(short)){ bool found_file = false; if (get_device_reference() == NULL || !alt_up_sd_card_is_Present() || !alt_up_sd_card_is_FAT16()){ printf("Can't find device, or device not configured properly\n"); return; } char filename_all_caps[strlen(filename)]; to_caps(filename, filename_all_caps); char found_file_name[13]; if (alt_up_sd_card_find_first(".", found_file_name) != 0){ printf("Couldn't find root dir\n"); return; } do { if (strcmp(found_file_name, filename_all_caps)== 0){ short int file = alt_up_sd_card_fopen(found_file_name, false); if (file >= 0){ printf("found file %s in SD\n", filename_all_caps); (*func)(file); found_file = true; //want to close file, so use this rather than returning } alt_up_sd_card_fclose(file); } }while(!found_file && alt_up_sd_card_find_next(found_file_name) == 0); }
void findDatabaseVersion(){ if(alt_up_sd_card_find_first("",database_name)==0){ while(alt_up_sd_card_find_next(database_name)==0){ //printf("%s \n",database_name); version++; } } else if (alt_up_sd_card_find_first("",database_name)== -1) printf("No files. \n"); }
//Initializes SD card, outputs filenames on sd card int init_sd(char * filenames[]){ int j; int i = 0; char thepath[] = AUD_DIRECT; //Directory for filenames we want to read short int check_file_name; for (j = 0; j < MAXFILES; j++) { filenames[j] = (char *)malloc(sizeof(char) * MAX_NAME_LEN); if (filenames[j] == NULL) printf("Error, out of memory (2)\n"); } alt_up_sd_card_dev * sd_card = NULL; int connected = 0; sd_card = alt_up_sd_card_open_dev("/dev/Interface"); if (sd_card != NULL) { while(1) { if ((connected == 0) && (alt_up_sd_card_is_Present())) { printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()) { printf("FAT16 file system detected.\n"); check_file_name = alt_up_sd_card_find_first(thepath,filenames[i]); if (check_file_name == 1) printf("Invalid Directory.\n"); else if (check_file_name == 2) printf("Invalid SD Card.\n"); else if (check_file_name == -1) printf("No files found.\n"); else { i++; } while(alt_up_sd_card_find_next(filenames[i]) != -1){ i++; } arr = (unsigned int ***)malloc(sizeof(unsigned int) * i); if (arr == NULL){ printf("Error, arr null\n"); } return i; //If card connected, returns amount of files } else { printf("Unknown file system.\n"); } connected = 1; } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) { printf("Card disconnected.\n"); connected = 0; } } } else { printf("Error.\n"); return -1; } }
void printSD() { char* fileName; if (alt_up_sd_card_is_Present() && alt_up_sd_card_is_FAT16()) { if (alt_up_sd_card_find_first("", fileName) == 0) { printf("Files detected: \n"); do { printf("%s \n", fileName); } while (alt_up_sd_card_find_next(fileName) == 0); } else if (alt_up_sd_card_find_first("", fileName) == -1) printf("No files. \n"); } }
int init_sd(char * filenames[]){ int i = 0; char thepath[] = "/"; short int check_file_name; alt_up_sd_card_dev * sd_card = NULL; int connected = 0; sd_card = alt_up_sd_card_open_dev("/dev/Interface"); if (sd_card != NULL) { while(1) { if ((connected == 0) && (alt_up_sd_card_is_Present())) { printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()) { printf("FAT16 file system detected.\n"); check_file_name = alt_up_sd_card_find_first(thepath,filenames[i]); if (check_file_name == 1) printf("Invalid Directory.\n"); else if (check_file_name == 2) printf("Invalid SD Card.\n"); else if (check_file_name == -1) printf("No files found.\n"); else { // printf("First file name = %s.\n", filenames[i]); i++; } while(alt_up_sd_card_find_next(filenames[i]) != -1){ // printf("Next file name = %s.\n", filenames[i]); i++; } return i; } else { printf("Unknown file system.\n"); } connected = 1; } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) { printf("Card disconnected.\n"); connected = 0; } } } else { printf("Error.\n"); return -1; } }
/* * Overview: Helper function to test SD card. * Check connectivity, file system, and any present files. * */ void testsdcard(alt_up_sd_card_dev *device_reference) { char file_name[10]; int connected = 0; if (device_reference != NULL) { while(1) { if ((connected == 0) && (alt_up_sd_card_is_Present())) { printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()) { printf("FAT16 file system detected.\n"); if(alt_up_sd_card_find_first(".", (char*) file_name)==0) { printf("%s\n",file_name); while(alt_up_sd_card_find_next((char*)file_name)==0) { printf("%s\n", file_name); alt_up_sd_card_read(file_name); } } break; } else { printf("Unknown file system.\n"); } connected = 1; } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) { printf("Card disconnected.\n"); connected = 0; } } } else { printf("Card could not be opened\n"); } }
int filenames_read(char* filename, int charmax){ //Test if the SD card is inserted and is in a valid file format for reading if ((alt_up_sd_card_is_Present())) { if (alt_up_sd_card_is_FAT16()) { int offset = 0; int filecount = 0; int returnval = alt_up_sd_card_find_first(0x0, filename); if (returnval < 0){ printf("the read failed"); return -2; } /* * Read the first filename within the SD card as long as there is sufficient space within the character buffer */ if((returnval == 0)) { filecount++; while(*(filename+offset) != '.') offset++; offset+=4; *(filename+offset) = ';'; offset++; } /* As long as there is sufficient space within the character buffer, will read every name within the * SD card to a buffer. */ while (1){ returnval = alt_up_sd_card_find_next((filename+offset)); if (returnval == -1 ){ break; } filecount++; if((charmax-offset) <= 12){ break; } while(*(filename+offset) != '.'){ offset++; } offset+=4; *(filename+offset) = ';'; offset++; } return filecount; } } return -1; }
/** * Uses the directory used in sdcard_get_first_file. * Return: Same as sdcard_FirstFile, with the addition of 3, which means that sdcard_FirstFile must be called first */ short int sdcard_NextFile(char *file_name) { short int result = alt_up_sd_card_find_next(file_name); return result; }
int main(void) { device_reference = alt_up_sd_card_open_dev("/dev/SD_Card"); if (device_reference != NULL) { while(1) { if ((connected == 0) && (alt_up_sd_card_is_Present())) { printf("Card connected.\n"); if (alt_up_sd_card_is_FAT16()) { printf("FAT16 file system detected.\n"); //Print out the list in the sd card handler = alt_up_sd_card_find_first("", buffer_name); printf("%s \n", buffer_name); //Read a file in SD card -needs to speed up(10 sec?) - needs array of int sd_fileh = alt_up_sd_card_fopen("TEST.WAV", false); if (sd_fileh < 0) printf("Problem reading file. Error %i\n", sd_fileh); else { printf("Reading file...\n"); audio_dev = alt_up_audio_open_dev (AUDIO_NAME); if ( audio_dev == NULL){ return; } av_config = alt_up_av_config_open_dev(AUDIO_AND_VIDEO_CONFIG_NAME); while(!alt_up_av_config_read_ready(av_config)); alt_up_audio_disable_read_interrupt(audio_dev); alt_up_audio_enable_write_interrupt(audio_dev); /* // Get information of number of channels //count = 0; char a, b; int d; short c; int index = 0; while (buffer_name[index] != "\0"){ a = alt_up_sd_card_read(sd_fileh); b = alt_up_sd_card_read(sd_fileh); c = ((unsigned char) a <<8) | (unsigned char) b; d = c; d = alt_up_audio_write_fifo(device_reference,d,) index++; } */ /* // printf("NumChannels "); byte1 = 0;byte2 = 0;total = 0; byte1 = alt_up_sd_card_read(sd_fileh); byte2 = alt_up_sd_card_read(sd_fileh); total = (byte1 & 0x00ff) | (byte2 << 8); NumChannels = total; // printf("%d\n",total); // printf("SampleRate "); byte1 = 0;byte2 = 0;byte3 = 0;byte4 = 0;long_total = 0; byte1 = alt_up_sd_card_read(sd_fileh); byte2 = alt_up_sd_card_read(sd_fileh); byte3 = alt_up_sd_card_read(sd_fileh); byte4 = alt_up_sd_card_read(sd_fileh); long_total = (byte4 << 24) | (byte3 << 16) | (byte2 << 8) | ( byte1 & 0x000000ff); sampleRate = long_total; // printf("%ld\n",long_total); */ /* count = 0; while (count < 16) // 44-byte header { alt_up_sd_card_read(sd_fileh); count++; } */ } //find next file while ((handler = alt_up_sd_card_find_next(buffer_name)) != -1) printf("%s \n", buffer_name); }else{ printf("Unknown file system.\n"); } connected = 1; } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) { printf("Card disconnected.\n"); connected = 0; } } } return 0; }
int main() { int i; //1 for receiving file from middleman, 2 for sending to middleman int mode; //Num char in filename array int numFileName; //Num bytes in file int numBytesFile; //Num characters in file array int numFile; char* listName; //File to send or receive (likely a piece of it) unsigned char fileName[MAX_FILENAME]; //variable to hold data received from uart unsigned char data; //parity bit for reading (not using parity atm, but still need the bit) unsigned char parity; short int handle; //handle for the file to create/access int connected = 0; //Variable to keep track of whether the SD CARD is connected or not. while (1) { alt_up_sd_card_dev *device_reference = NULL; device_reference = alt_up_sd_card_open_dev(ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME); printf("UART Initialization\n"); alt_up_rs232_dev* uart = alt_up_rs232_open_dev("/dev/rs232_0"); if(!alt_up_sd_card_is_FAT16()){ printf("SD CARD is not FAT16 Format\n"); } if (device_reference != NULL) { while (alt_up_sd_card_is_Present()) { printf("Clearing read buffer to start\n"); while (alt_up_rs232_get_used_space_in_read_FIFO(uart)) { alt_up_rs232_read_data(uart, &data, &parity); } // Now receive the instruction from the Middleman printf("Waiting for instruction to come from the Middleman\n"); while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0) ; // First byte is the mode, 1 for receiving file from middleman, 2 for sending to middleman alt_up_rs232_read_data(uart, &data, &parity); mode = (int) data; //mode -= 48; printf("Mode:%d\n", mode); //Receive file from middleman and save to SD if (mode == 1) { printf("Waiting for num char:\n"); // The second byte is the number of characters in the file name while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); numFileName = (int) data; //numFileName -= 48; //Now receive the file name printf("About to receive %d characters:\n\n", numFileName); printf("Filename received:"); for (i = 0; i < numFileName; i++) { while (alt_up_rs232_get_used_space_in_read_FIFO(uart)== 0); alt_up_rs232_read_data(uart, &data, &parity); fileName[i] = data; printf("%c", data); } printf("\n"); fileName[i] = '.'; fileName[i+1] = 't'; fileName[i+2] = 'x'; fileName[i+3] = 't'; fileName[i+4]= '\0'; // // TODO: // USE THAT FILENAME TO MAKE A NEW FILE ON SD CARD HERE handle = alt_up_sd_card_fopen(fileName, 1); if(handle < 0){ //TODO: File can't be opened, do something about it printf("send had a neg handle \n\n"); } else{ // The 4 bytes after filename is the number of bytes in the file // // SHIFT BYTES LEFT AND CONCATENATE TO ACCOUNT FOR SEVERAL BYTES WORTH OF FILE // WRITE FIRST 4 BYTES OF FILE AS SIZE OF FILE IN BYTES while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); numBytesFile = (int) data; numBytesFile = numBytesFile << 8; while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); numBytesFile += (int) data; numBytesFile = numBytesFile << 8; while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); numBytesFile += (int) data; numBytesFile = numBytesFile << 8; while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); numBytesFile += (int) data; //WRITE BYTES TO SD CARD printf("About to receive %d file chars:\n\n",numBytesFile); for (i = 0; i < numBytesFile; i++) { while (alt_up_rs232_get_used_space_in_read_FIFO(uart)== 0); alt_up_rs232_read_data(uart, &data, &parity); alt_up_sd_card_write(handle, data); } printf("File done\n"); alt_up_sd_card_fclose(handle); } //This bracket ends receiving a file } //Send file to middleman from SD else if (mode == 2) { printf("Waiting for num char:\n"); // The second byte is the number of characters in the file name while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0) ; alt_up_rs232_read_data(uart, &data, &parity); numFileName = (int) data; //numFileName -= 48; //Now receive the file name printf("About to receive %d characters:\n\n", numFileName); printf("Filename received:"); for (i = 0; i < numFileName; i++) { while (alt_up_rs232_get_used_space_in_read_FIFO(uart) == 0) ; alt_up_rs232_read_data(uart, &data, &parity); fileName[i] = data; printf("%c", data); } printf("\n"); fileName[i] = '.'; fileName[i+1] = 't'; fileName[i+2] = 'x'; fileName[i+3] = 't'; fileName[i+4]= '\0'; handle = alt_up_sd_card_fopen(fileName, 0); if (handle == -1) { //SEND ANDROID AN ASCII 2 TO LET THEM KNOW TO RECEIVE FILE NAME alt_up_rs232_write_data(uart, 50); printf("neg handle"); if(alt_up_sd_card_find_first(".",listName) ==-1){ //SEND ANDROID AN ASCII 2 TO LET THEM KNOW THERES NO FILE NAMES alt_up_rs232_write_data(uart, 50); printf("no files"); } else{ //SEND ANDROID LIST OF FILE NAMES printf("some files"); i=0; for(i = 0; listName[i] != '.'; i++){ alt_up_rs232_write_data(uart, listName[i]); } alt_up_rs232_write_data(uart, 32); while(alt_up_sd_card_find_next(listName)!=-1){ i=0; for(i = 0; listName[i] != '.'; i++){ alt_up_rs232_write_data(uart, listName[i]); } alt_up_rs232_write_data(uart, 32); } alt_up_rs232_write_data(uart, 1); printf("done files"); } } else { //SEND ANDROID AN ASCII 1 TO LET THEM KNOW TO RECEIVE A FILE alt_up_rs232_write_data(uart, 49); // SHIFT BYTES LEFT AND CONCATENATE TO ACCOUNT FOR SEVERAL BYTES WORTH OF FILE // WRITE FIRST 4 BYTES OF FILE AS SIZE OF FILE IN BYTES data = alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile = (int) data; numFile = numFile << 8; data = (int) alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile += (int) data; numFile = numFile << 8; data = (int) alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile += (int) data; numFile = numFile << 8; data = (int) alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile += (int) data; printf("About to send %d file bytes\n", numFile); while (numFile > 0) { while(alt_up_rs232_get_available_space_in_write_FIFO(uart) == 0); data = alt_up_sd_card_read(handle); alt_up_rs232_write_data(uart, data); numFile--; } // WRITE A "FILE DONE" STRING OR WHATEVER WE DECIDE printf("sending end bits\n"); // alt_up_rs232_write_data(uart, 1); // // // alt_up_sd_card_fclose(handle); } //This bracket ends sending a file } //Something broke else { printf("Wrong mode, something broke, starting over\n"); } } } else { printf("Card Reader is not working\n"); } } return 0; //end main }