diff --git a/SD/picofs.c b/SD/picofs.c index 21c41bac92ced51717250d3a95f6053632455b2b..96c91204d03091deac62d655aa56ba036ab95a7e 100644 --- a/SD/picofs.c +++ b/SD/picofs.c @@ -23,22 +23,36 @@ #define FIRST_DATA_BLOCK 1040 -void readBlock(SD_info *sd, unsigned int num, int offset, unsigned char *storage, int size) { +void readBlock(SD_info *sd, unsigned int numBlock, int offset, unsigned char *storage, int size) { //lire à un endroit spécifique du bloc de la carte SD - //uint8_t * buffer_storage; - //readData(sd,num,offset, size, buffer_storage); - //storage = (unsigned char *)buffer_storage; - + uint8_t * buffer_read = malloc (sizeof(char)*BLOCK_SIZE); + readData(sd,numBlock,offset, size, buffer_read); + strcpy(storage,(unsigned char *)buffer_read); + free(buffer_read); } -void writeBlock(SD_info *sd, unsigned int num, int offset, const unsigned char *storage, int size) { +void writeBlock(SD_info *sd, unsigned int numBlock, int offset, unsigned char *source, int size) { //écrire à un endroit spécifique du bloc de la carte SD - // writeBlockSD(sd,num,offset,size, (uint8_t *)storage); - + uint8_t * buffer_write = malloc (sizeof(char)*BLOCK_SIZE); //sauve les données du bloc + if(buffer_write == NULL) printf("erreur malloc write"); + readData(sd, numBlock, 0, size, buffer_write); + if(offset + size > 512 ) + { + printf("data to write is too big"); + return; + } + for (int i = size - 1; i >= 0; i--) { + source[i + offset] = source[i]; + } + // Ajouter les zéros au début de la chaîne + for (int j = 0; j < offset; j++) { + source[j] = buffer_write[j]; + } + writeBlockSD(sd,numBlock, (uint8_t *)source); + free(buffer_write); } - void LS(SD_info *sd) { unsigned char buffer[MAX_FILENAME_LENGTH]; int fileCount = 0; @@ -467,106 +481,94 @@ void CP(SD_info *sd, const char *source_filename, const char *destination_filena int main(int argc, char *argv[]) { - - #ifdef TEST_WRITE - uint8_t blocW[BLOCK_SIZE]={0xaa,0xbb,0xcc}; - #endif - #ifdef TEST_READ - uint8_t blocR[BLOCK_SIZE]; - #endif - #ifdef DEBUG - init_printf(); - #endif + + char user_data; SD_info sd; + init_printf(); spi_init(); - uint8_t result=sd_init(&sd); - #ifdef DEBUG + uint8_t result = sd_init(&sd); printf("result=%x\n",result); printf("status=%x\n",sd.status_); printf("erreur=%x\n",sd.errorCode_); printf("type=%x\n",sd.type_); uint32_t size=cardSize(&sd); printf("taille=%ld\n",size); - #endif - #ifdef TEST_WRITE - int statutWrite=writeBlockSD(&sd,BLOC_NO,blocW); - #ifdef DEBUG - printf("writeStatus=%x\n",statutWrite); - #endif - #endif - #ifdef TEST_READ - int statutRead=readBlockSD(&sd,BLOC_NO,blocR); - #ifdef DEBUG - printf("readstatus=%x\n",statutRead); - int i; - for(i=0;i<3;i++) printf("bloc[%d]=%x ",i,blocR[i]); - printf("\n"); - #endif - #endif - return 0; - -// // Vérification du nombre d'arguments -// if (argc < 3) { -// printf("Usage: %s filesystem_path ACTION [arguments...]\n", argv[0]); -// printf("Actions:\n"); -// printf(" LS\n"); -// printf(" TYPE filename\n"); -// printf(" CAT filename\n"); -// printf(" RM filename\n"); -// printf(" MV old_filename new_filename\n"); -// printf(" CP filename copy_filename\n"); -// -// // setBlockAvailability(1040,1); -// // setBlockAvailability(1041,1); -// // setBlockAvailability(1042,1); -// // setBlockAvailability(1043,1); -// // printf("1er bloc dispo:%d\n",findAvailableBlock()); -// return 1; -// } -// -// // Récupération des arguments -// const char *filesystem_path = argv[1]; -// const char *action = argv[2]; -// -// // Exécution de l'action correspondante en fonction des arguments passés -// if (strcmp(action, "LS") == 0) { -// LS(filesystem_path); -// } else if (strcmp(action, "TYPE") == 0) { -// if (argc < 4) { -// printf("Usage: %s filesystem_path TYPE filename\n", argv[0]); -// return 1; -// } -// TYPE(filesystem_path, argv[3]); -// } else if (strcmp(action, "CAT") == 0) { -// if (argc < 4) { -// printf("Usage: %s filesystem_path CAT filename\n", argv[0]); -// return 1; -// } -// CAT(filesystem_path, argv[3]); -// } else if (strcmp(action, "RM") == 0) { -// if (argc < 4) { -// printf("Usage: %s filesystem_path RM filename\n", argv[0]); -// return 1; -// } -// RM(filesystem_path, argv[3]); -// } else if (strcmp(action, "MV") == 0) { -// if (argc < 5) { -// printf("Usage: %s filesystem_path MV filename\n", argv[0]); -// return 1; -// } -// MV(filesystem_path, argv[3], argv[4]); -// } else if (strcmp(action, "CP") == 0) { -// if (argc < 5) { -// printf("Usage: %s filesystem_path MV filename\n", argv[0]); -// return 1; -// } -// CP(filesystem_path, argv[3], argv[4]); -// -// } else { -// printf("Action non reconnue. Actions possibles : LS, TYPE, CAT, RM, MV.\n"); -// return 1; -// } - -// return 0; + + LS(&sd); + while(1) + { + _delay_ms(10); + user_data = get_serial(); + + printf("%c", user_data); + /*if(user_data == '\r' ) + { + + }*/ + + } + // // Vérification du nombre d'arguments + // if (argc < 3) { + // printf("Usage: %s filesystem_path ACTION [arguments...]\n", argv[0]); + // printf("Actions:\n"); + // printf(" LS\n"); + // printf(" TYPE filename\n"); + // printf(" CAT filename\n"); + // printf(" RM filename\n"); + // printf(" MV old_filename new_filename\n"); + // printf(" CP filename copy_filename\n"); + // + // // setBlockAvailability(1040,1); + // // setBlockAvailability(1041,1); + // // setBlockAvailability(1042,1); + // // setBlockAvailability(1043,1); + // // printf("1er bloc dispo:%d\n",findAvailableBlock()); + // return 1; + // } + // + // // Récupération des arguments + // const char *filesystem_path = argv[1]; + // const char *action = argv[2]; + // + // // Exécution de l'action correspondante en fonction des arguments passés + // if (strcmp(action, "LS") == 0) { + // LS(filesystem_path); + // } else if (strcmp(action, "TYPE") == 0) { + // if (argc < 4) { + // printf("Usage: %s filesystem_path TYPE filename\n", argv[0]); + // return 1; + // } + // TYPE(filesystem_path, argv[3]); + // } else if (strcmp(action, "CAT") == 0) { + // if (argc < 4) { + // printf("Usage: %s filesystem_path CAT filename\n", argv[0]); + // return 1; + // } + // CAT(filesystem_path, argv[3]); + // } else if (strcmp(action, "RM") == 0) { + // if (argc < 4) { + // printf("Usage: %s filesystem_path RM filename\n", argv[0]); + // return 1; + // } + // RM(filesystem_path, argv[3]); + // } else if (strcmp(action, "MV") == 0) { + // if (argc < 5) { + // printf("Usage: %s filesystem_path MV filename\n", argv[0]); + // return 1; + // } + // MV(filesystem_path, argv[3], argv[4]); + // } else if (strcmp(action, "CP") == 0) { + // if (argc < 5) { + // printf("Usage: %s filesystem_path MV filename\n", argv[0]); + // return 1; + // } + // CP(filesystem_path, argv[3], argv[4]); + // + // } else { + // printf("Action non reconnue. Actions possibles : LS, TYPE, CAT, RM, MV.\n"); + // return 1; + // } + + // return 0; }