Skip to content
Snippets Groups Projects
Commit 7d3787d4 authored by chayrai's avatar chayrai
Browse files

avancee systeme de fichiers pour carte mere

parent ee403771
Branches
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ export TARGET_ARCH = -mmcu=$(MCU) ...@@ -6,7 +6,7 @@ export TARGET_ARCH = -mmcu=$(MCU)
export CFLAGS = -Wall -I. -DF_CPU=16000000 -Os -DDEBUG -DTEST_WRITE -DTEST_READ #-g export CFLAGS = -Wall -I. -DF_CPU=16000000 -Os -DDEBUG -DTEST_WRITE -DTEST_READ #-g
export LDFLAGS = -g $(TARGET_ARCH) -lm -Wl,--gc-sections # -Os export LDFLAGS = -g $(TARGET_ARCH) -lm -Wl,--gc-sections # -Os
TARGET = fileSystem TARGET = picofs
TERM = /dev/ttyUSB0 TERM = /dev/ttyUSB0
CPPFLAGS = -mmcu=$(MCU) CPPFLAGS = -mmcu=$(MCU)
PGMER = -c stk500v1 -b 57600 -P $(TERM) PGMER = -c stk500v1 -b 57600 -P $(TERM)
......
...@@ -294,7 +294,7 @@ info->partialBlockRead_ = value; ...@@ -294,7 +294,7 @@ info->partialBlockRead_ = value;
*/ */
uint8_t readData(SD_info *info, uint32_t block, uint16_t offset, uint16_t count, uint8_t* dst) { uint8_t readData(SD_info *info, uint32_t block, uint16_t offset, uint16_t count, uint8_t* dst) {
if(count==0) return true; if(count==0) return true;
if((count+offset)>512){ goto fail; } if((count+offset)>256){ goto fail; }
if(!info->inBlock_||block!=info->block_||offset<info->offset_){ if(!info->inBlock_||block!=info->block_||offset<info->offset_){
info->block_=block; info->block_=block;
// use address if not SDHC card // use address if not SDHC card
...@@ -315,7 +315,7 @@ uint16_t i; ...@@ -315,7 +315,7 @@ uint16_t i;
for(i=0;i<count;i++) dst[i]=spi_exch(0xff); for(i=0;i<count;i++) dst[i]=spi_exch(0xff);
info->offset_ += count; info->offset_ += count;
if(!info->partialBlockRead_||info->offset_>=512){ if(!info->partialBlockRead_||info->offset_>=256){
// read rest of data, checksum and set chip select high // read rest of data, checksum and set chip select high
readEnd(info); readEnd(info);
} }
...@@ -336,8 +336,8 @@ return false; ...@@ -336,8 +336,8 @@ return false;
* \return The value one, true, is returned for success and * \return The value one, true, is returned for success and
* the value zero, false, is returned for failure. * the value zero, false, is returned for failure.
*/ */
uint8_t readBlock(SD_info *info, uint32_t block, uint8_t* dst) { uint8_t readBlockSD(SD_info *info, uint32_t block, uint8_t* dst) {
return readData(info, block, 0, 512, dst); return readData(info, block, 0, 256, dst);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
...@@ -345,7 +345,7 @@ return readData(info, block, 0, 512, dst); ...@@ -345,7 +345,7 @@ return readData(info, block, 0, 512, dst);
static uint8_t writeData_(SD_info *info, uint8_t token, const uint8_t* src) { static uint8_t writeData_(SD_info *info, uint8_t token, const uint8_t* src) {
spi_exch(token); spi_exch(token);
uint16_t i; uint16_t i;
for(i=0;i<512;i++) spi_exch(src[i]); for(i=0;i<256;i++) spi_exch(src[i]);
spi_exch(0xff); // dummy crc spi_exch(0xff); // dummy crc
spi_exch(0xff); // dummy crc spi_exch(0xff); // dummy crc
info->status_=spi_exch(0xff); info->status_=spi_exch(0xff);
...@@ -378,7 +378,7 @@ return writeData_(info, WRITE_MULTIPLE_TOKEN, src); ...@@ -378,7 +378,7 @@ return writeData_(info, WRITE_MULTIPLE_TOKEN, src);
* \return The value one, true, is returned for success and * \return The value one, true, is returned for success and
* the value zero, false, is returned for failure. * the value zero, false, is returned for failure.
*/ */
uint8_t writeBlock(SD_info *info, uint32_t blockNumber, const uint8_t* src) { uint8_t writeBlockSD(SD_info *info, uint32_t blockNumber, const uint8_t* src) {
// use address if not SDHC card // use address if not SDHC card
if(info->type_!=SD_CARD_TYPE_SDHC) blockNumber <<= 9; if(info->type_!=SD_CARD_TYPE_SDHC) blockNumber <<= 9;
if(cardCommand(info, CMD24, blockNumber)){ if(cardCommand(info, CMD24, blockNumber)){
......
...@@ -68,8 +68,8 @@ uint8_t erase(SD_info *info, uint32_t firstBlock, uint32_t lastBlock); ...@@ -68,8 +68,8 @@ uint8_t erase(SD_info *info, uint32_t firstBlock, uint32_t lastBlock);
uint8_t sd_init(SD_info *info); uint8_t sd_init(SD_info *info);
void partialBlockRead(SD_info *info, uint8_t value); void partialBlockRead(SD_info *info, uint8_t value);
uint8_t readData(SD_info *info, uint32_t block, uint16_t offset, uint16_t count, uint8_t* dst); uint8_t readData(SD_info *info, uint32_t block, uint16_t offset, uint16_t count, uint8_t* dst);
uint8_t readBlock(SD_info *info, uint32_t block, uint8_t* dst); uint8_t readBlockSD(SD_info *info, uint32_t block, uint8_t* dst);
uint8_t writeData(SD_info *info, const uint8_t* src); uint8_t writeData(SD_info *info, const uint8_t* src);
uint8_t writeBlock(SD_info *info, uint32_t blockNumber, const uint8_t* src); uint8_t writeBlockSD(SD_info *info, uint32_t blockNumber, const uint8_t* src);
uint8_t writeStart(SD_info *info, uint32_t blockNumber, uint32_t eraseCount); uint8_t writeStart(SD_info *info, uint32_t blockNumber, uint32_t eraseCount);
uint8_t writeStop(SD_info *info); uint8_t writeStop(SD_info *info);
...@@ -2,17 +2,20 @@ ...@@ -2,17 +2,20 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
//inclusion des fonctions faites précédemment #include "avr/io.h"
#include "util/delay.h"
#include "serial.h" #include "serial.h"
#include <avr/io.h> #include "spi.h"
#include <spi.h>
#include "SdInfo.h" #include "SdInfo.h"
#include "Sd2Card.h" #include "Sd2Card.h"
#define CHUNK_SIZE 64 #define CHUNK_SIZE 64
//#define BLOCK_SIZE 256 #define BLOCK_SIZE 256
#define MAX_FILENAME_LENGTH 16 #define MAX_FILENAME_LENGTH 16
#define MAX_BLOCKS_PER_FILE 2040 #define MAX_BLOCKS_PER_FILE 16
#define MAX_FILES_IN_DIRECTORY 64 #define MAX_FILES_IN_DIRECTORY 64
#define FILESYSTEM_SIZE (8 * 1024 * 1024) // 8 Mo #define FILESYSTEM_SIZE (8 * 1024 * 1024) // 8 Mo
#define FIRST_DISPONIBILITY_CARD_BLOCK 1024 #define FIRST_DISPONIBILITY_CARD_BLOCK 1024
...@@ -20,14 +23,39 @@ ...@@ -20,14 +23,39 @@
#define FIRST_DATA_BLOCK 1040 #define FIRST_DATA_BLOCK 1040
void readBlock(SD_info *sd, unsigned int num, int offset, unsigned char *storage, int size) {
// FILE *file = fopen("filesystem.bin", "rb+"); // Utiliser "rb+" pour ouvrir le fichier en lecture et écriture
// if (file == NULL) {
// perror("Erreur lors de l'ouverture du fichier");
// return;
// }
readData(sd,num,offset, size, storage);
// fseek(file, num * BLOCK_SIZE + offset, SEEK_SET);
// fread(storage, 1, size, file);
// //fclose(file);
}
void writeBlock(SD_info *sd, unsigned int num, int offset, const unsigned char *storage, int size) {
// FILE *file = fopen("filesystem.bin", "rb+"); // Utiliser "rb+" pour ouvrir le fichier en lecture et écriture
// if (file == NULL) {
// perror("Erreur lors de l'ouverture du fichier");
// return;
// }
writeData(sd,num,offset,size,storage);
// fseek(file, num * BLOCK_SIZE + offset, SEEK_SET);
// fwrite(storage, 1, size, file);
// fclose(file);
}
void LS(SD_info *info) { void LS(SD_info *sd) {
unsigned char buffer[MAX_FILENAME_LENGTH]; unsigned char buffer[MAX_FILENAME_LENGTH];
int fileCount = 0; int fileCount = 0;
// Parcourir les blocs multiples de 16 à partir de 0 jusqu'à MAX_FILES_IN_DIRECTORY // Parcourir les blocs multiples de 16 à partir de 0 jusqu'à MAX_FILES_IN_DIRECTORY
for (int blockNum = 0; blockNum < MAX_FILES_IN_DIRECTORY; blockNum += 16) { for (int blockNum = 0; blockNum < MAX_FILES_IN_DIRECTORY; blockNum += 16) {
readBlock(info, blockNum, buffer); readBlock(sd,blockNum, 0, buffer, MAX_FILENAME_LENGTH);
// Vérifier si le nom de fichier est vide // Vérifier si le nom de fichier est vide
if (buffer[0] != 0) { if (buffer[0] != 0) {
...@@ -43,7 +71,7 @@ void LS(SD_info *info) { ...@@ -43,7 +71,7 @@ void LS(SD_info *info) {
} }
void setBlockAvailability(int blockNum, int availability) { void setBlockAvailability(SD_info *sd, int blockNum, int availability) {
int byteIndexInCard = blockNum / 8; //Numéro d'octet dans la carte des blocs libres int byteIndexInCard = blockNum / 8; //Numéro d'octet dans la carte des blocs libres
int blockIndexInCard = byteIndexInCard / 256; //Numéro du bloc dans la carte des blocs libres int blockIndexInCard = byteIndexInCard / 256; //Numéro du bloc dans la carte des blocs libres
...@@ -55,7 +83,7 @@ void setBlockAvailability(int blockNum, int availability) { ...@@ -55,7 +83,7 @@ void setBlockAvailability(int blockNum, int availability) {
// Lire l'octet existant dans le bloc de disponibilité du super bloc // Lire l'octet existant dans le bloc de disponibilité du super bloc
unsigned char buffer; unsigned char buffer;
readBlock(availabilityBlockNum, byteIndexInBlock, &buffer, 1); readBlock(sd, availabilityBlockNum, byteIndexInBlock, &buffer, 1);
// Mettre à jour le bit d'offset correspondant : // Mettre à jour le bit d'offset correspondant :
if (availability==1) { // indisponible if (availability==1) { // indisponible
...@@ -66,14 +94,14 @@ void setBlockAvailability(int blockNum, int availability) { ...@@ -66,14 +94,14 @@ void setBlockAvailability(int blockNum, int availability) {
} }
// Écrire l'octet mis à jour dans le bloc de disponibilité du super bloc // Écrire l'octet mis à jour dans le bloc de disponibilité du super bloc
writeBlock(availabilityBlockNum, byteIndexInBlock, &buffer, 1); writeBlock(sd,availabilityBlockNum, byteIndexInBlock, &buffer, 1);
} }
int min(int a, int b) { int min(int a, int b) {
return a < b ? a : b; return a < b ? a : b;
} }
void RM(const char *filesystem_path, const char *filename) { void RM(SD_info *sd, const char *filename) {
int fileFound = -1; int fileFound = -1;
int offset; int offset;
unsigned char fileBuffer[BLOCK_SIZE]; unsigned char fileBuffer[BLOCK_SIZE];
...@@ -81,13 +109,13 @@ void RM(const char *filesystem_path, const char *filename) { ...@@ -81,13 +109,13 @@ void RM(const char *filesystem_path, const char *filename) {
// Parcourir les blocs réservés pour la description des fichiers (superbloc) // Parcourir les blocs réservés pour la description des fichiers (superbloc)
for (int blockNum = 0; blockNum < MAX_FILES_IN_DIRECTORY; blockNum += 16) { for (int blockNum = 0; blockNum < MAX_FILES_IN_DIRECTORY; blockNum += 16) {
unsigned char filenameBuffer[MAX_FILENAME_LENGTH]; unsigned char filenameBuffer[MAX_FILENAME_LENGTH];
readBlock(blockNum, 0, filenameBuffer, MAX_FILENAME_LENGTH); readBlock(sd,blockNum, 0, filenameBuffer, MAX_FILENAME_LENGTH);
// Vérifier si le bloc contient le nom du fichier recherché // Vérifier si le bloc contient le nom du fichier recherché
if (strncmp((const char *)filenameBuffer, filename, MAX_FILENAME_LENGTH) == 0) { if (strncmp((const char *)filenameBuffer, filename, MAX_FILENAME_LENGTH) == 0) {
// Effacer le nom du fichier dans le superbloc // Effacer le nom du fichier dans le superbloc
memset(filenameBuffer, 0, MAX_FILENAME_LENGTH); memset(filenameBuffer, 0, MAX_FILENAME_LENGTH);
writeBlock(blockNum, 0, filenameBuffer, MAX_FILENAME_LENGTH); writeBlock(sd,blockNum, 0, filenameBuffer, MAX_FILENAME_LENGTH);
fileFound = 1; fileFound = 1;
offset = blockNum; offset = blockNum;
break; break;
...@@ -109,22 +137,22 @@ void RM(const char *filesystem_path, const char *filename) { ...@@ -109,22 +137,22 @@ void RM(const char *filesystem_path, const char *filename) {
for (int chunkStart = startOffset; chunkStart < BLOCK_SIZE; chunkStart += CHUNK_SIZE) { for (int chunkStart = startOffset; chunkStart < BLOCK_SIZE; chunkStart += CHUNK_SIZE) {
int chunkSize = min(CHUNK_SIZE, BLOCK_SIZE - chunkStart); int chunkSize = min(CHUNK_SIZE, BLOCK_SIZE - chunkStart);
readBlock(blockNum, chunkStart, fileBuffer, chunkSize); readBlock(sd,blockNum, chunkStart, fileBuffer, chunkSize);
for (int i = 0; i < chunkSize; i += 2) { for (int i = 0; i < chunkSize; i += 2) {
int blockNumData = (fileBuffer[i] << 8) | fileBuffer[i + 1]; int blockNumData = (fileBuffer[i] << 8) | fileBuffer[i + 1];
printf("blockNumData=%d\n",blockNumData); printf("blockNumData=%d\n",blockNumData);
if (blockNumData == 0) { if (blockNumData == 0) {
writeBlock(blockNum, chunkStart, fileBuffer, chunkSize); writeBlock(sd,blockNum, chunkStart, fileBuffer, chunkSize);
printf("Le fichier \"%s\" a été supprimé avec succès.\n", filename); printf("Le fichier \"%s\" a été supprimé avec succès.\n", filename);
return; // Sortir des boucles return; // Sortir des boucles
} }
setBlockAvailability(blockNumData, 0); // Marquer le bloc comme disponible setBlockAvailability(sd,blockNumData, 0); // Marquer le bloc comme disponible
fileBuffer[i] = 0; fileBuffer[i] = 0;
fileBuffer[i + 1] = 0; fileBuffer[i + 1] = 0;
} }
writeBlock(blockNum, chunkStart, fileBuffer, chunkSize); writeBlock(sd,blockNum, chunkStart, fileBuffer, chunkSize);
} }
} }
...@@ -132,7 +160,7 @@ void RM(const char *filesystem_path, const char *filename) { ...@@ -132,7 +160,7 @@ void RM(const char *filesystem_path, const char *filename) {
} }
// Fonction qui modifie le nom du fichier // Fonction qui modifie le nom du fichier
void MV(const char *filesystem_path, const char *old_filename, const char *new_filename) { void MV(SD_info *sd, const char *old_filename, const char *new_filename) {
size_t sizeNew_filename = strlen(new_filename); size_t sizeNew_filename = strlen(new_filename);
size_t sizeOld_filename = strlen(old_filename); size_t sizeOld_filename = strlen(old_filename);
unsigned char filenameBuffer[MAX_FILENAME_LENGTH]; unsigned char filenameBuffer[MAX_FILENAME_LENGTH];
...@@ -141,7 +169,7 @@ void MV(const char *filesystem_path, const char *old_filename, const char *new_f ...@@ -141,7 +169,7 @@ void MV(const char *filesystem_path, const char *old_filename, const char *new_f
// Parcourir les blocs réservés pour la description des fichiers (superbloc) // Parcourir les blocs réservés pour la description des fichiers (superbloc)
for (int blockNum = 0; blockNum < MAX_FILES_IN_DIRECTORY; blockNum += 16) { for (int blockNum = 0; blockNum < MAX_FILES_IN_DIRECTORY; blockNum += 16) {
readBlock(blockNum, 0, filenameBuffer, MAX_FILENAME_LENGTH); readBlock(sd,blockNum, 0, filenameBuffer, MAX_FILENAME_LENGTH);
// Vérifier si le bloc contient le nom du fichier recherché // Vérifier si le bloc contient le nom du fichier recherché
if (strncmp((const char*)filenameBuffer, old_filename, MAX_FILENAME_LENGTH) == 0) { if (strncmp((const char*)filenameBuffer, old_filename, MAX_FILENAME_LENGTH) == 0) {
...@@ -151,7 +179,7 @@ void MV(const char *filesystem_path, const char *old_filename, const char *new_f ...@@ -151,7 +179,7 @@ void MV(const char *filesystem_path, const char *old_filename, const char *new_f
} }
// Écrire le nom du fichier dans l'emplacement // Écrire le nom du fichier dans l'emplacement
writeBlock(blockNum, 0, (const unsigned char *)new_filename, sizeNew_filename); writeBlock(sd,blockNum, 0, (const unsigned char *)new_filename, sizeNew_filename);
fileFound=1; fileFound=1;
...@@ -166,13 +194,13 @@ void MV(const char *filesystem_path, const char *old_filename, const char *new_f ...@@ -166,13 +194,13 @@ void MV(const char *filesystem_path, const char *old_filename, const char *new_f
} }
// Renvoie le premier bloc de donné disponible // Renvoie le premier bloc de donné disponible
int findAvailableBlock() { int findAvailableBlock(SD_info *sd) {
unsigned char availabilityBuffer[BLOCK_SIZE]; unsigned char availabilityBuffer[BLOCK_SIZE];
int offset; int offset;
// Lire la carte de disponibilité (à partir du bloc 1) // Lire la carte de disponibilité (à partir du bloc 1)
for (int blockNum = FIRST_DISPONIBILITY_CARD_BLOCK; blockNum < FIRST_DATA_BLOCK; blockNum++) { for (int blockNum = FIRST_DISPONIBILITY_CARD_BLOCK; blockNum < FIRST_DATA_BLOCK; blockNum++) {
readBlock(blockNum, 0, availabilityBuffer, BLOCK_SIZE); readBlock(sd,blockNum, 0, availabilityBuffer, BLOCK_SIZE);
if (blockNum==FIRST_DISPONIBILITY_CARD_BLOCK) { if (blockNum==FIRST_DISPONIBILITY_CARD_BLOCK) {
offset=FIRST_DATA_BLOCK/8; offset=FIRST_DATA_BLOCK/8;
...@@ -199,7 +227,7 @@ int findAvailableBlock() { ...@@ -199,7 +227,7 @@ int findAvailableBlock() {
// Aucun bloc disponible trouvé // Aucun bloc disponible trouvé
return -1; return -1;
} }
/*
// Fonction qui reconstitue le numéro de bloc à partir du tableau // Fonction qui reconstitue le numéro de bloc à partir du tableau
int reconsituteNumber(unsigned char blockNumberBuffer[2]) { int reconsituteNumber(unsigned char blockNumberBuffer[2]) {
unsigned char octet1 = blockNumberBuffer[0]; unsigned char octet1 = blockNumberBuffer[0];
...@@ -214,8 +242,8 @@ void createNumberBuffer(int dataBlockNum, unsigned char blockNumberBuffer[2]) { ...@@ -214,8 +242,8 @@ void createNumberBuffer(int dataBlockNum, unsigned char blockNumberBuffer[2]) {
blockNumberBuffer[0] = (dataBlockNum >> 8) & 0xFF; blockNumberBuffer[0] = (dataBlockNum >> 8) & 0xFF;
blockNumberBuffer[1] = dataBlockNum & 0xFF; blockNumberBuffer[1] = dataBlockNum & 0xFF;
} }
*/
void TYPE(const char *filesystem_path, const char *filename) { void TYPE(SD_info *sd, const char *filename) {
size_t sizeFilename = strlen(filename); size_t sizeFilename = strlen(filename);
unsigned char buffer[MAX_FILENAME_LENGTH]; unsigned char buffer[MAX_FILENAME_LENGTH];
int placeFound = -1; int placeFound = -1;
...@@ -232,18 +260,18 @@ void TYPE(const char *filesystem_path, const char *filename) { ...@@ -232,18 +260,18 @@ void TYPE(const char *filesystem_path, const char *filename) {
// Parcours des blocs réservés pour la description des fichiers (superbloc) // Parcours des blocs réservés pour la description des fichiers (superbloc)
for (int blockNum = 0; blockNum < MAX_FILES_IN_DIRECTORY; blockNum += 16) { for (int blockNum = 0; blockNum < MAX_FILES_IN_DIRECTORY; blockNum += 16) {
readBlock(blockNum, 0, buffer, MAX_FILENAME_LENGTH); readBlock(sd,blockNum, 0, buffer, MAX_FILENAME_LENGTH);
// Vérifier si le bloc est vide (pas de nom de fichier) // Vérifier si le bloc est vide (pas de nom de fichier)
if (buffer[0] == 0) { if (buffer[0] == 0) {
// Écrire le nom du fichier dans l'emplacement vide du superbloc // Écrire le nom du fichier dans l'emplacement vide du superbloc
if (sizeFilename < MAX_FILENAME_LENGTH) { if (sizeFilename < MAX_FILENAME_LENGTH) {
sizeFilename += 1; // Ajouter '\0' s'il y a de la place sizeFilename += 1; // Ajouter '\0' s'il y a de la place
} }
writeBlock(blockNum, 0, (const unsigned char *)filename, sizeFilename); writeBlock(sd,blockNum, 0, (const unsigned char *)filename, sizeFilename);
placeFound = blockNum; placeFound = blockNum;
// Lire les données depuis l'entrée standard et écrire dans le fichier // Lire les données depuis l'entrée standard et écrire dans le fichier
int dataBlockNum = findAvailableBlock(); // Premier bloc de données à partir du bloc 1040 int dataBlockNum = findAvailableBlock(sd); // Premier bloc de données à partir du bloc 1040
int blockSizeUsed = 0; // Compteur d'octets dans le bloc actuel int blockSizeUsed = 0; // Compteur d'octets dans le bloc actuel
unsigned char chunkBuffer[CHUNK_SIZE]; unsigned char chunkBuffer[CHUNK_SIZE];
...@@ -252,8 +280,8 @@ void TYPE(const char *filesystem_path, const char *filename) { ...@@ -252,8 +280,8 @@ void TYPE(const char *filesystem_path, const char *filename) {
while ((bytesRead = fread(chunkBuffer, 1, CHUNK_SIZE, stdin)) > 0) { while ((bytesRead = fread(chunkBuffer, 1, CHUNK_SIZE, stdin)) > 0) {
// Écrire le chunk dans le bloc de données // Écrire le chunk dans le bloc de données
writeBlock(dataBlockNum, blockSizeUsed, chunkBuffer, bytesRead); writeBlock(sd,dataBlockNum, blockSizeUsed, chunkBuffer, bytesRead);
setBlockAvailability(dataBlockNum, 1); setBlockAvailability(sd,dataBlockNum, 1);
// Écrire le numéro du bloc actuel dans la description du fichier // Écrire le numéro du bloc actuel dans la description du fichier
unsigned char blockNumberBuffer[2]; unsigned char blockNumberBuffer[2];
...@@ -272,10 +300,10 @@ void TYPE(const char *filesystem_path, const char *filename) { ...@@ -272,10 +300,10 @@ void TYPE(const char *filesystem_path, const char *filename) {
} else { } else {
offset = 0; offset = 0;
} }
writeBlock(placeFound + index_description_block, offset + index_in_descrBlock*2, blockNumberBuffer, 2); writeBlock(sd,placeFound + index_description_block, offset + index_in_descrBlock*2, blockNumberBuffer, 2);
index_in_descrBlock++; index_in_descrBlock++;
dataBlockNum = findAvailableBlock(); // Passer au bloc suivant dataBlockNum = findAvailableBlock(sd); // Passer au bloc suivant
blockSizeUsed = 0; // Réinitialiser la taille utilisée blockSizeUsed = 0; // Réinitialiser la taille utilisée
// Passage au bloc de description suivant // Passage au bloc de description suivant
...@@ -307,7 +335,7 @@ void TYPE(const char *filesystem_path, const char *filename) { ...@@ -307,7 +335,7 @@ void TYPE(const char *filesystem_path, const char *filename) {
} }
void CAT(const char *filesystem_path, const char *filename) { void CAT(SD_info *sd, const char *filename) {
unsigned char filenameBuffer[MAX_FILENAME_LENGTH]; unsigned char filenameBuffer[MAX_FILENAME_LENGTH];
unsigned char byteBuffer[2]; unsigned char byteBuffer[2];
unsigned char dataBuffer[CHUNK_SIZE]; unsigned char dataBuffer[CHUNK_SIZE];
...@@ -315,7 +343,7 @@ void CAT(const char *filesystem_path, const char *filename) { ...@@ -315,7 +343,7 @@ void CAT(const char *filesystem_path, const char *filename) {
// Parcours des blocs réservés pour la description des fichiers (superbloc) // Parcours des blocs réservés pour la description des fichiers (superbloc)
for (int blockNum = 0; blockNum < MAX_FILES_IN_DIRECTORY; blockNum += 16) { for (int blockNum = 0; blockNum < MAX_FILES_IN_DIRECTORY; blockNum += 16) {
readBlock(blockNum, 0, filenameBuffer, MAX_FILENAME_LENGTH); readBlock(sd,blockNum, 0, filenameBuffer, MAX_FILENAME_LENGTH);
// Vérifier si le bloc contient le nom du fichier recherché // Vérifier si le bloc contient le nom du fichier recherché
if (strncmp((const char *)filenameBuffer, filename, MAX_FILENAME_LENGTH) == 0) { if (strncmp((const char *)filenameBuffer, filename, MAX_FILENAME_LENGTH) == 0) {
...@@ -331,7 +359,7 @@ void CAT(const char *filesystem_path, const char *filename) { ...@@ -331,7 +359,7 @@ void CAT(const char *filesystem_path, const char *filename) {
// Lecture des octets deux par deux // Lecture des octets deux par deux
for (int i=0; i<BLOCK_SIZE;i+=2) { for (int i=0; i<BLOCK_SIZE;i+=2) {
readBlock(descrBlockNum+blockNum, offset+i, byteBuffer, 2); readBlock(sd,descrBlockNum+blockNum, offset+i, byteBuffer, 2);
// Lire les numéros de blocs associés à ce fichier depuis les blocs de description // Lire les numéros de blocs associés à ce fichier depuis les blocs de description
int dataBlockNum = reconsituteNumber(byteBuffer); int dataBlockNum = reconsituteNumber(byteBuffer);
...@@ -343,7 +371,7 @@ void CAT(const char *filesystem_path, const char *filename) { ...@@ -343,7 +371,7 @@ void CAT(const char *filesystem_path, const char *filename) {
for (int chunkStart = 0; chunkStart < BLOCK_SIZE; chunkStart += CHUNK_SIZE) { for (int chunkStart = 0; chunkStart < BLOCK_SIZE; chunkStart += CHUNK_SIZE) {
// Lire et afficher le contenu du bloc de données // Lire et afficher le contenu du bloc de données
readBlock(dataBlockNum, chunkStart, dataBuffer, CHUNK_SIZE); readBlock(sd,dataBlockNum, chunkStart, dataBuffer, CHUNK_SIZE);
fwrite(dataBuffer, 1, CHUNK_SIZE, stdout); fwrite(dataBuffer, 1, CHUNK_SIZE, stdout);
} }
} }
...@@ -359,7 +387,7 @@ void CAT(const char *filesystem_path, const char *filename) { ...@@ -359,7 +387,7 @@ void CAT(const char *filesystem_path, const char *filename) {
// Fonction pour copier un fichier existant du système de fichier // Fonction pour copier un fichier existant du système de fichier
void CP(const char *filesystem_path, const char *source_filename, const char *destination_filename) { void CP(SD_info *sd, const char *source_filename, const char *destination_filename) {
unsigned char source_filenameBuffer[MAX_FILENAME_LENGTH]; unsigned char source_filenameBuffer[MAX_FILENAME_LENGTH];
unsigned char destination_filenameBuffer[MAX_FILENAME_LENGTH]; unsigned char destination_filenameBuffer[MAX_FILENAME_LENGTH];
unsigned char descriptionBuffer[CHUNK_SIZE]; unsigned char descriptionBuffer[CHUNK_SIZE];
...@@ -373,7 +401,7 @@ void CP(const char *filesystem_path, const char *source_filename, const char *de ...@@ -373,7 +401,7 @@ void CP(const char *filesystem_path, const char *source_filename, const char *de
// Recherche du fichier source // Recherche du fichier source
source_offset = -1; source_offset = -1;
for (int blockNum = 0; blockNum < MAX_FILES_IN_DIRECTORY; blockNum += 16) { for (int blockNum = 0; blockNum < MAX_FILES_IN_DIRECTORY; blockNum += 16) {
readBlock(blockNum, 0, source_filenameBuffer, MAX_FILENAME_LENGTH); readBlock(sd,blockNum, 0, source_filenameBuffer, MAX_FILENAME_LENGTH);
// Vérifier si le bloc contient le nom du fichier source // Vérifier si le bloc contient le nom du fichier source
if (strncmp((const char *)source_filenameBuffer, source_filename, MAX_FILENAME_LENGTH) == 0) { if (strncmp((const char *)source_filenameBuffer, source_filename, MAX_FILENAME_LENGTH) == 0) {
...@@ -390,7 +418,7 @@ void CP(const char *filesystem_path, const char *source_filename, const char *de ...@@ -390,7 +418,7 @@ void CP(const char *filesystem_path, const char *source_filename, const char *de
// Recherche d'un emplacement libre pour le fichier destination // Recherche d'un emplacement libre pour le fichier destination
destination_offset = -1; destination_offset = -1;
for (int blockNum = 0; blockNum < MAX_FILES_IN_DIRECTORY; blockNum += 16) { for (int blockNum = 0; blockNum < MAX_FILES_IN_DIRECTORY; blockNum += 16) {
readBlock(blockNum, 0, destination_filenameBuffer, MAX_FILENAME_LENGTH); readBlock(sd,blockNum, 0, destination_filenameBuffer, MAX_FILENAME_LENGTH);
// Vérifier si le bloc est vide (pas de nom de fichier) // Vérifier si le bloc est vide (pas de nom de fichier)
if (destination_filenameBuffer[0] == 0) { if (destination_filenameBuffer[0] == 0) {
...@@ -405,7 +433,7 @@ void CP(const char *filesystem_path, const char *source_filename, const char *de ...@@ -405,7 +433,7 @@ void CP(const char *filesystem_path, const char *source_filename, const char *de
} }
// Copie du nom de la copie dans le bloc de description // Copie du nom de la copie dans le bloc de description
writeBlock(destination_offset, 0, (const unsigned char *)destination_filename, strlen(destination_filename)); writeBlock(sd,destination_offset, 0, (const unsigned char *)destination_filename, strlen(destination_filename));
// Copier les blocs de données associés au fichier source dans de nouveaux blocs de données pour le fichier destination // Copier les blocs de données associés au fichier source dans de nouveaux blocs de données pour le fichier destination
for (int i = 0; i < MAX_BLOCKS_PER_FILE_DESCRIPTION; i++) { for (int i = 0; i < MAX_BLOCKS_PER_FILE_DESCRIPTION; i++) {
...@@ -414,7 +442,7 @@ void CP(const char *filesystem_path, const char *source_filename, const char *de ...@@ -414,7 +442,7 @@ void CP(const char *filesystem_path, const char *source_filename, const char *de
// Lecture des numéros de bloc dans les blocs de description // Lecture des numéros de bloc dans les blocs de description
for (int byteNum=0; byteNum<BLOCK_SIZE; byteNum+=2) { for (int byteNum=0; byteNum<BLOCK_SIZE; byteNum+=2) {
readBlock(source_offset + i, offset + byteNum, numBuffer, 2); readBlock(sd,source_offset + i, offset + byteNum, numBuffer, 2);
numDataBlock = reconsituteNumber(numBuffer); numDataBlock = reconsituteNumber(numBuffer);
...@@ -425,22 +453,22 @@ void CP(const char *filesystem_path, const char *source_filename, const char *de ...@@ -425,22 +453,22 @@ void CP(const char *filesystem_path, const char *source_filename, const char *de
for (int chunkStart = 0; chunkStart < BLOCK_SIZE; chunkStart += CHUNK_SIZE) { for (int chunkStart = 0; chunkStart < BLOCK_SIZE; chunkStart += CHUNK_SIZE) {
// On stocke le bloc de données associé au fichier source dans descriptionBuffer // On stocke le bloc de données associé au fichier source dans descriptionBuffer
readBlock(numDataBlock, chunkStart, descriptionBuffer, CHUNK_SIZE); readBlock(sd,numDataBlock, chunkStart, descriptionBuffer, CHUNK_SIZE);
if (chunkStart == 0) { if (chunkStart == 0) {
// Trouver un nouveau bloc de données disponible // Trouver un nouveau bloc de données disponible
newDataBlock = findAvailableBlock(); newDataBlock = findAvailableBlock(sd);
// Mise à jour de la carte de disponibilités // Mise à jour de la carte de disponibilités
setBlockAvailability(newDataBlock, 1); setBlockAvailability(sd,newDataBlock, 1);
// Ecriture du numéro de bloc dans la description du fichier // Ecriture du numéro de bloc dans la description du fichier
createNumberBuffer(newDataBlock,numBuffer); createNumberBuffer(newDataBlock,numBuffer);
writeBlock(destination_offset+i, offset+byteNum, numBuffer, 2); writeBlock(sd,destination_offset+i, offset+byteNum, numBuffer, 2);
} }
// Ecriture du bloc de données dans le premier bloc disponible // Ecriture du bloc de données dans le premier bloc disponible
writeBlock(newDataBlock, chunkStart, descriptionBuffer, CHUNK_SIZE); writeBlock(sd,newDataBlock, chunkStart, descriptionBuffer, CHUNK_SIZE);
} }
} }
} }
...@@ -450,21 +478,108 @@ void CP(const char *filesystem_path, const char *source_filename, const char *de ...@@ -450,21 +478,108 @@ void CP(const char *filesystem_path, const char *source_filename, const char *de
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
#ifdef TEST_WRITE
uint8_t blocW[BLOCK_SIZE]={0xac,0xbc,0xcc};
#endif
#ifdef TEST_READ
uint8_t blocR[BLOCK_SIZE];
#endif
#ifdef DEBUG
init_printf(); init_printf();
#endif
SD_info sd; SD_info sd;
spi_init(); spi_init();
uint8_t result=sd_init(&sd); uint8_t result=sd_init(&sd);
#ifdef DEBUG
printf("result=%x\n",result); printf("result=%x\n",result);
printf("status=%x\n",sd.status_); printf("status=%x\n",sd.status_);
printf("erreur=%x\n",sd.errorCode_); printf("erreur=%x\n",sd.errorCode_);
printf("type=%x\n",sd.type_); printf("type=%x\n",sd.type_);
uint32_t size=cardSize(&sd); uint32_t size=cardSize(&sd);
printf("taille=%ld\n",size); printf("taille=%ld\n",size);
#endif
#ifdef TEST_WRITE
int statutWrite=writeBlockSD(&sd,16,blocW); //écriture dans bloc 16
#endif
#ifdef DEBUG
printf("statutWrite=%x\n",statutWrite);
#endif
#ifdef TEST_READ
int statutRead=readBlockSD(&sd,16,blocR);
#endif
#ifdef DEBUG
printf("statutRead=%x\n",statutRead);
int i;
for(i=0;i<3;i++) printf("blocR[%d]=%x ",i,blocR[i]);
printf("\n");
#endif
return 0; 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;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment