1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| running_e machine_SD_write(bool_e ask_for_finish, char* mot,char* nom_fichier){
typedef enum
{
INIT = 0,
MOUNT,
WRITE_TEST_FILE,
READ_TEST_FILE,
DELETE_TEST_FILE,
END_WITH_ERROR,
IDLE
}state_e;
static state_e state = INIT;
FRESULT res = FR_OK;
static FIL file;
running_e ret;
ret = IN_PROGRESS;
static uint16_t size = 0;
static char write_buf[256] = {0};
if(mot != NULL)
{
size = sprintf(write_buf, mot);
}
static char current_file[20] = {0};
if(nom_fichier != NULL)
sprintf(current_file, nom_fichier);
switch(state)
{
case INIT:
if(BSP_SD_Init() == MSD_OK)///*disk_initialize(PD_SD) == 0*/)
{
debug_printf("SD Card : module initialized\n");
state = MOUNT;
}
else
{
debug_printf("Fail to initialize disk\n");
state = END_WITH_ERROR;
}
break;
case MOUNT:{
FATFS * fs;
Uint32 p2;
state = END_WITH_ERROR; //On fait la supposition que cela ne va pas marcher.... si tout se passe bien, on ira faire la suite.
if(FATFS_LinkDriver(&SD_Driver, SD_path) != 0)
{
display("SD Card : NOT mounted\n");
}
else
{
res = f_mount(&SDFatFs, (TCHAR const*)SD_path, 0);
if(res == FR_OK)
{
res = f_getfree("", (DWORD*)&p2, &fs);
//res = f_mkfs((TCHAR const*)SDPath, 0, 0);
if (res == FR_OK)
{
/*debug_printf("FAT type = %u (%s)\nBytes/Cluster = %lu\nNumber of FATs = %u\n"
"Root DIR entries = %u\nSectors/FAT = %lu\nNumber of clusters = %lu\n"
"FAT start (lba) = %lu\nDIR start (lba,clustor) = %lu\nData start (lba) = %lu\n\n",
(WORD)fs->fs_type,
(fs->fs_type==FS_FAT12) ? "FAT12" : (fs->fs_type==FS_FAT16) ? "FAT16" : "FAT32",
(DWORD)fs->csize * 512, (WORD)fs->n_fats,
fs->n_rootdir, fs->fsize, (DWORD)fs->n_fatent - 2,
fs->fatbase, fs->dirbase, fs->database
);*/
display("SD Card : mounted\n");
state = WRITE_TEST_FILE;
}
}
if(res != FR_OK){
printf("error\n");
//verbose_fresult(res);
state = END_WITH_ERROR;
}
}
break;}
case WRITE_TEST_FILE:{
UINT nb_to_write = 0;
UINT nb_written;
if(size && current_file[0]!='\0')
{
res = f_open(&file, current_file, FA_WRITE | FA_OPEN_ALWAYS);
res = f_lseek(&file, f_size(&file));
if(res == FR_OK)
{
debug_printf("File %s opened\n",current_file);
display("Testfile opened\n");
nb_to_write += size;
res = f_write(&file,write_buf,size,&nb_written);
if(res == FR_OK && size == nb_written)
debug_printf("datas wrote in file\n");
else
debug_printf("Fail to write datas in file\n");
f_close(&file);
}
else
{
debug_printf("Fail to open file %s\n",current_file);
}
size = 0;
}
state = IDLE;
break;}
case READ_TEST_FILE:{
#define READ_BUFFER_SIZE 32
UINT nb_read = 0;
uint8_t datas_read[READ_BUFFER_SIZE];
res = f_open(&file, path, FA_READ);
if(res == FR_OK)
{
res = f_read(&file,datas_read,READ_BUFFER_SIZE,&nb_read);
datas_read[nb_read] = '\0'; //Pour utiliser strcmp...
if(strcmp((char*)datas_read,mot) == 0)
debug_printf("Correct data read : %d datas : %s\n",nb_read,datas_read);
else
debug_printf("Bad data read : %d datas : %s\n",nb_read,datas_read);
f_close(&file);
}
state = IDLE;
break;}
/*case DELETE_TEST_FILE:
res = f_unlink(path);
if(res == FR_OK)
debug_printf("Test File deleted : ok\n");
else
debug_printf("Error deleting test file\n");
state = IDLE;
break;*/
case END_WITH_ERROR:
debug_printf("SD Card : error\n");
display("SD Card : ERROR\n");
state = IDLE;
break;
case IDLE:
if(size)
state = WRITE_TEST_FILE;
if(ask_for_finish)
{
state = INIT;
ret = END_OK;
}
if(mot)
{
}
break;
default:
break;
}
if(res != FR_OK)
//verbose_fresult(res);
printf("error\n");
return ret;
} |
Partager