bool sdApaga(char *nome) { SDSPI_Handle sdspiHandle; SDSPI_Params sdspiParams; char arquivo[20] = "fat:"STR(SD_DRIVE_NUM)":"; strcat(arquivo, nome); strcat(arquivo, ".txt"); /* Mount and register the SD Card */ SDSPI_Params_init(&sdspiParams); sdspiHandle = SDSPI_open(Board_SDSPI0, SD_DRIVE_NUM, &sdspiParams); if (sdspiHandle == NULL) { printf("Erro ao iniciar o SD card!\n"); return false; } if (remove(arquivo) != 0) { printf("Nao foi possivel apagar o arquivo!\n"); Task_sleep(10); SDSPI_close(sdspiHandle); return false; } else { printf("\t\tArquivo apagado!\n"); } SDSPI_close(sdspiHandle); return true; }
void sdEscreve(char *conteudo, int tamanho) { SDSPI_Handle sdspiHandle; SDSPI_Params sdspiParams; FILE *src; const char outputfile[] = "fat:"STR(SD_DRIVE_NUM)":input.txt"; /* Mount and register the SD Card */ SDSPI_Params_init(&sdspiParams); sdspiHandle = SDSPI_open(Board_SDSPI0, SD_DRIVE_NUM, &sdspiParams); if (sdspiHandle == NULL) { printf("Erro ao iniciar o SD card!\n"); return; } // else { // printf("Drive %u montado!\n", SD_DRIVE_NUM); // } /* Tenta abrir o arquivo */ src = fopen(outputfile, "ab"); if (!src) { printf("Nao foi possivel criar o arquivo!\n"); Task_sleep(1); SDSPI_close(sdspiHandle); return; } else { printf("Arquivo criado!\n\n"); /* escreve do arquivo */ fputs(conteudo, src); //fflush(src); fclose(src); } SDSPI_close(sdspiHandle); }
void sdLe() { SDSPI_Handle sdspiHandle; SDSPI_Params sdspiParams; FILE *src; // unsigned int bytesRead = 0; const char inputfile[] = "fat:"STR(SD_DRIVE_NUM)":input.txt"; char read_buff[BUFF_SIZE + 1]; /* Mount and register the SD Card */ SDSPI_Params_init(&sdspiParams); sdspiHandle = SDSPI_open(Board_SDSPI0, SD_DRIVE_NUM, &sdspiParams); if (sdspiHandle == NULL) { printf("Erro ao iniciar o SD card!\n"); return; } // else { // printf("Drive %u montado!\n", SD_DRIVE_NUM); // } /* Tenta abrir o arquivo */ src = fopen(inputfile, "rb"); if (!src) { printf("Nao foi possivel abrir o arquivo!\n"); Task_sleep(1); SDSPI_close(sdspiHandle); return; } else { printf("\t\tArquivo aberto!\n"); while (true) { /* Le blocos do arquivo arquivo */ //bytesRead = fread(read_buff,sizeof(char), sizeof(read_buff), src); //TODO: Está lendo duas vezes a última linha! if (!feof(src)) { fgets(read_buff, sizeof(read_buff), src); //le linhas do arquivo printf("%s", read_buff); fflush(stdout); } else { printf("\t\tFim do arquivo\n"); break; } // if (bytesRead == 0) { // printf("\t\tFim do arquivo\n"); // break; /* Error or EOF */ // } } fclose(src); } SDSPI_close(sdspiHandle); }
/* * ======== taskFxn ======== * Task to perform a file copy * * Task tries to open an existing file inputfile[]. If the file doesn't * exist, create one and write some known content into it. * The contents of the inputfile[] are then copied to an output file * outputfile[]. Once completed, the contents of the output file are * printed onto the system console (stdout). * * Task for this function is created statically. See the project's .cfg file. */ Void taskFxn(UArg arg0, UArg arg1) { FRESULT fresult; SDSPI_Handle sdspiHandle; SDSPI_Params sdspiParams; /* Variables to keep track of the file copy progress */ unsigned int bytesRead = 0; unsigned int bytesWritten = 0; unsigned int filesize; unsigned int totalBytesCopied = 0; /* Mount and register the SD Card */ SDSPI_Params_init(&sdspiParams); sdspiHandle = SDSPI_open(Board_SDSPI0, DRIVE_NUM, &sdspiParams); if (sdspiHandle == NULL) { System_abort("Error starting the SD card\n"); } else { System_printf("Drive %u is mounted\n", DRIVE_NUM); } printDrive(STR(DRIVE_NUM), &(dst.fs)); /* Try to open the source file */ fresult = f_open(&src, inputfile, FA_READ); if (fresult != FR_OK) { System_printf("Creating a new file \"%s\"...", inputfile); /* Open file for both reading and writing */ fresult = f_open(&src, inputfile, FA_CREATE_NEW|FA_READ|FA_WRITE); if (fresult != FR_OK) { System_printf("Error: \"%s\" could not be created.\n" "Please check the Getting Started Guide " "if additional jumpers are necessary.\n", inputfile); System_abort("Aborting...\n"); } f_write(&src, textarray, strlen(textarray), &bytesWritten); f_sync(&src); /* Reset the internal file pointer */ f_lseek(&src, 0); System_printf("done\n"); } else { System_printf("Using existing copy of \"%s\"\n", inputfile); } /* Create a new file object for the file copy */ fresult = f_open(&dst, outputfile, FA_CREATE_ALWAYS|FA_WRITE); if (fresult != FR_OK) { System_printf("Error opening \"%s\"\n", outputfile); System_abort("Aborting...\n"); } else { System_printf("Starting file copy\n"); } /* Copy the contents from the src to the dst */ while (true) { /* Read from source file */ fresult = f_read(&src, cpy_buff, CPY_BUFF_SIZE, &bytesRead); if (fresult || bytesRead == 0) { break; /* Error or EOF */ } /* Write to dst file */ fresult = f_write(&dst, cpy_buff, bytesRead, &bytesWritten); if (fresult || bytesWritten < bytesRead) { System_printf("Disk Full\n"); break; /* Error or Disk Full */ } /* Update the total number of bytes copied */ totalBytesCopied += bytesWritten; } f_sync(&dst); /* Get the filesize of the source file */ filesize = f_size(&src); /* Close both inputfile[] and outputfile[] */ f_close(&src); f_close(&dst); System_printf("File \"%s\" (%u B) copied to \"%s\" (Wrote %u B)\n", inputfile, filesize, outputfile, totalBytesCopied); /* Now output the outputfile[] contents onto the console */ fresult = f_open(&dst, outputfile, FA_READ); if (fresult != FR_OK) { System_printf("Error opening \"%s\"\n", outputfile); System_abort("Aborting...\n"); } /* Print file contents */ while (true) { /* Read from output file */ fresult = f_read(&dst, cpy_buff, CPY_BUFF_SIZE, &bytesRead); if (fresult || bytesRead == 0) { break; /* Error or EOF */ } cpy_buff[bytesRead] = '\0'; /* Write output */ System_printf("%s", cpy_buff); System_flush(); } /* Close the file */ f_close(&dst); printDrive(STR(DRIVE_NUM), &(dst.fs)); /* Stopping the SDCard */ SDSPI_close(sdspiHandle); System_printf("Drive %u unmounted\n", DRIVE_NUM); BIOS_exit(0); }
/* * ======== taskFxn ======== * Task to perform a file copy * * Task tries to open an existing file inputfile[]. If the file doesn't * exist, create one and write some known content into it. * The contents of the inputfile[] are then copied to an output file * outputfile[]. Once completed, the contents of the output file are * printed onto the system console (stdout). * * Task for this function is created statically. See the project's .cfg file. */ Void taskFxn(UArg arg0, UArg arg1) { SDSPI_Handle sdspiHandle; SDSPI_Params sdspiParams; USBMSCHFatFs_Handle usbmschfatfsHandle; USBMSCHFatFs_Params usbmschfatfsParams; /* Variables for the CIO functions */ FILE *src, *dst; /* Variables to keep track of the file copy progress */ unsigned int bytesRead = 0; unsigned int bytesWritten = 0; unsigned int filesize; unsigned int totalBytesCopied = 0; /* Mount and register the USB Drive */ SDSPI_Params_init(&sdspiParams); sdspiHandle = SDSPI_open(Board_SDSPI0, SD_DRIVE_NUM, &sdspiParams); if (sdspiHandle == NULL) { System_abort("Error starting the SD card\n"); } else { System_printf("Drive %u is mounted\n", SD_DRIVE_NUM); } /* Mount and register the USB Drive */ USBMSCHFatFs_Params_init(&usbmschfatfsParams); usbmschfatfsParams.serviceTaskStackPtr = usbServiceTaskStack; usbmschfatfsParams.serviceTaskStackSize = sizeof(usbServiceTaskStack); usbmschfatfsHandle = USBMSCHFatFs_open(Board_USBMSCHFatFs0, USB_DRIVE_NUM, &usbmschfatfsParams); if (usbmschfatfsHandle == NULL) { System_abort("Error starting the USB Drive\n"); } else { System_printf("Drive %u is mounted\n", USB_DRIVE_NUM); } /* Need to block until a USB Drive has been enumerated */ if (!USBMSCHFatFs_waitForConnect(usbmschfatfsHandle, 10000)) { System_abort("No USB drive present, aborting...\n"); } /* Try to open the source file */ src = fopen(inputfilesd, "r"); if (!src) { System_printf("Creating a new file \"%s\"...", inputfilesd); /* Open file for both reading and writing */ src = fopen(inputfilesd, "w+"); if ( !src ) { System_printf("Error: \"%s\" could not be created.\n" "Please check the Getting Started Guide " "if additional jumpers are necessary.\n", inputfilesd); System_abort("Aborting...\n"); } fwrite(textarray, 1, strlen(textarray), src); fflush(src); /* Reset the internal file pointer */ rewind(src); System_printf("done\n"); } else { System_printf("Using existing copy of \"%s\"\n", inputfilesd); } /* Create a new file object for the file copy */ dst = fopen(outputfileusb, "w"); if (!dst) { System_printf("Error opening \"%s\"\n", outputfileusb); System_abort("Aborting...\n"); } else { System_printf("Starting file copy\n"); } /* Copy the contents from the src to the dst */ while (true) { /* Read from source file */ bytesRead = fread(cpy_buff, 1, CPY_BUFF_SIZE, src); if (bytesRead == 0) { break; /* Error or EOF */ } /* Write to dst file */ bytesWritten = fwrite(cpy_buff, 1, bytesRead, dst); if (bytesWritten < bytesRead) { System_printf("Disk Full\n"); break; /* Error or Disk Full */ } /* Update the total number of bytes copied */ totalBytesCopied += bytesWritten; } fflush(dst); /* Get the filesize of the source file */ fseek(src, 0, SEEK_END); filesize = ftell(src); rewind(src); /* Close both inputfile[] and outputfile[] */ fclose(src); fclose(dst); System_printf("File \"%s\" (%u B) copied to \"%s\" (Wrote %u B)\n", inputfilesd, filesize, outputfileusb, totalBytesCopied); /* Now output the outputfile[] contents onto the console */ dst = fopen(outputfileusb, "r"); if (!dst) { System_printf("Error opening \"%s\"\n", outputfileusb); System_abort("Aborting...\n"); } /* Print file contents */ while (true) { /* Read from output file */ bytesRead = fread(cpy_buff, 1, CPY_BUFF_SIZE, dst); if (bytesRead == 0) { break; /* Error or EOF */ } /* Write output */ System_printf("%s", cpy_buff); } /* Close the file */ fclose(dst); /* Stopping the SDCard */ SDSPI_close(sdspiHandle); System_printf("Drive %u unmounted\n", SD_DRIVE_NUM); /* Stopping the USB Drive */ USBMSCHFatFs_close(usbmschfatfsHandle); System_printf("Drive %u unmounted\n", USB_DRIVE_NUM); BIOS_exit(0); }