00001
00103 #include <string.h>
00104 #include "compiler.h"
00105 #include "preprocessor.h"
00106 #include "board.h"
00107 #include "print_funcs.h"
00108 #include "power_clocks_lib.h"
00109 #include "gpio.h"
00110 #include "usart.h"
00111 #include "spi.h"
00112 #include "conf_at45dbx.h"
00113 #include "fsaccess.h"
00114
00115
00116
00117 #define MSG_WELCOME "\r\n ---------- Welcome to FSACCESS Example ---------- \r\n"
00118 #define MSG_FILENAME "Enter the filename : "
00119 #define MSG_MODE "\r\nWhat do you want to do ?\r\n\t1 - Read file\r\n\t2 - Concatenate a file to an existing one\r\n\t3 - Copy the content of a file to a new one\r\nenter your choice : "
00120
00121
00122
00123 #define TEST_SUCCESS "\t[PASS]\n"
00124 #define TEST_FAIL "\t[FAIL]\n"
00125
00126 #define CR '\r'
00127 #define LF '\n'
00128 #define CTRL_Q 0x11
00129 #define CTRL_C 0x03
00130 #define BKSPACE_CHAR '\b'
00131 #define ABORT_CHAR CTRL_C
00132 #define QUIT_APPEND CTRL_Q
00133
00134
00135 #define NB_SECTOR_TO_SEND 4
00136
00137
00138 int fsaccess_example_get_mode (void);
00139 void fsaccess_example_get_filename (char * buf);
00140 int fsaccess_example_read(char * path);
00141 int fsaccess_example_write(char * source, char * destination, int flags);
00142
00143
00144
00145
00149 static void at45dbx_resources_init(void)
00150 {
00151 static const gpio_map_t AT45DBX_SPI_GPIO_MAP =
00152 {
00153 {AT45DBX_SPI_SCK_PIN, AT45DBX_SPI_SCK_FUNCTION },
00154 {AT45DBX_SPI_MISO_PIN, AT45DBX_SPI_MISO_FUNCTION },
00155 {AT45DBX_SPI_MOSI_PIN, AT45DBX_SPI_MOSI_FUNCTION },
00156 #define AT45DBX_ENABLE_NPCS_PIN(NPCS, unused) \
00157 {AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION}, // Chip Select NPCS.
00158 MREPEAT(AT45DBX_MEM_CNT, AT45DBX_ENABLE_NPCS_PIN, ~)
00159 #undef AT45DBX_ENABLE_NPCS_PIN
00160 };
00161
00162
00163 spi_options_t spiOptions =
00164 {
00165 .reg = AT45DBX_SPI_FIRST_NPCS,
00166 .baudrate = AT45DBX_SPI_MASTER_SPEED,
00167 .bits = AT45DBX_SPI_BITS,
00168 .spck_delay = 0,
00169 .trans_delay = 0,
00170 .stay_act = 1,
00171 .spi_mode = 0,
00172 .modfdis = 1
00173 };
00174
00175
00176 gpio_enable_module(AT45DBX_SPI_GPIO_MAP,
00177 sizeof(AT45DBX_SPI_GPIO_MAP) / sizeof(AT45DBX_SPI_GPIO_MAP[0]));
00178
00179
00180 spi_initMaster(AT45DBX_SPI, &spiOptions);
00181
00182
00183 spi_selectionMode(AT45DBX_SPI, 0, 0, 0);
00184
00185
00186 spi_enable(AT45DBX_SPI);
00187
00188
00189 at45dbx_init(spiOptions, FOSC0);
00190 }
00191
00192
00204 int main(void)
00205 {
00206 char filename1[90];
00207 char filename2[90];
00208
00209
00210 pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP);
00211
00212
00213 init_dbg_rs232(FOSC0);
00214
00215
00216 at45dbx_resources_init();
00217
00218
00219 b_fsaccess_init();
00220
00221
00222 if (at45dbx_mem_check())
00223 {
00224
00225 print_dbg(MSG_WELCOME);
00226 }
00227 else
00228 {
00229
00230 print_dbg("Initialization failed\r\n");
00231 return (-1);
00232 }
00233
00234 while (TRUE)
00235 {
00236
00237 switch (fsaccess_example_get_mode())
00238 {
00239 case '1':
00240
00241 fsaccess_example_get_filename(filename1);
00242
00243 fsaccess_example_read(filename1);
00244 break;
00245 case '2':
00246
00247 print_dbg("Source : ");
00248 fsaccess_example_get_filename(filename1);
00249
00250 print_dbg("Destination : ");
00251 fsaccess_example_get_filename(filename2);
00252
00253 fsaccess_example_write(filename1, filename2, O_APPEND);
00254 break;
00255 case '3':
00256
00257 print_dbg("Source : ");
00258 fsaccess_example_get_filename(filename1);
00259
00260 print_dbg("Destination : ");
00261 fsaccess_example_get_filename(filename2);
00262
00263 fsaccess_example_write(filename1, filename2, (O_CREAT | O_APPEND));
00264 break;
00265 default:
00266
00267 print_dbg("Wrong choice\r\n");
00268 break;
00269 }
00270 }
00271 }
00272
00275 int fsaccess_example_get_mode (void)
00276 {
00277 int c;
00278
00279
00280 print_dbg(MSG_MODE);
00281 while (TRUE)
00282 {
00283
00284 usart_reset_status(DBG_USART);
00285 if (usart_read_char(DBG_USART, &c) == USART_SUCCESS)
00286 {
00287
00288 print_dbg_char(c);
00289
00290 print_dbg_char(LF);
00291 return (c);
00292 }
00293 }
00294 }
00295
00298 void fsaccess_example_get_filename (char * buf)
00299 {
00300 unsigned long i_str = 3;
00301 unsigned short file;
00302 int c;
00303
00304
00305 print_dbg(MSG_FILENAME);
00306 file = FALSE;
00307 buf[0] = 'A';
00308 buf[1] = ':';
00309 buf[2] = '/';
00310
00311 while (file == FALSE)
00312 {
00313
00314 usart_reset_status(DBG_USART);
00315 if (usart_read_char(DBG_USART, &c) == USART_SUCCESS)
00316 {
00317
00318 switch (c)
00319 {
00320
00321 case CR:
00322
00323 print_dbg_char(LF);
00324
00325 buf[i_str] = '\0';
00326
00327 file = TRUE;
00328
00329 i_str = 0;
00330 break;
00331
00332 case ABORT_CHAR:
00333 i_str = 0;
00334 print_dbg_char(LF);
00335 print_dbg(MSG_FILENAME);
00336 break;
00337
00338 case BKSPACE_CHAR:
00339 if (i_str > 0)
00340 {
00341
00342 print_dbg_char(c);
00343
00344 print_dbg_char(' ');
00345
00346 print_dbg_char(c);
00347
00348 i_str--;
00349 }
00350 break;
00351
00352 default:
00353
00354 print_dbg_char(c);
00355
00356 buf[i_str++] = c;
00357 break;
00358 }
00359 }
00360 }
00361 }
00362
00365 int fsaccess_example_read(char * path)
00366 {
00367 char * ptrFile;
00368 int fd, i;
00369 long size;
00370
00371
00372 if ((fd = open(path, O_RDONLY)) < 0)
00373 {
00374
00375 print_dbg(path);
00376 print_dbg(" : Open failed\n");
00377 return (-1);
00378 }
00379
00380
00381 size = fsaccess_file_get_size(fd);
00382
00383 print_dbg("File size: ");
00384 print_dbg_ulong(size);
00385 print_dbg_char(LF);
00386
00387
00388 ptrFile = malloc((NB_SECTOR_TO_SEND * FS_SIZE_OF_SECTOR) + 1);
00389
00390
00391 if (ptrFile == NULL)
00392 {
00393
00394 print_dbg("Malloc failed\n");
00395 }
00396 else
00397 {
00398
00399 if ( size < (NB_SECTOR_TO_SEND * FS_SIZE_OF_SECTOR) )
00400 {
00401 if( read(fd, ptrFile, size) != size)
00402 {
00403
00404 print_dbg("Reading entire file failed\n");
00405 }
00406 else
00407 {
00408
00409 ptrFile[size] = '\0';
00410
00411 print_dbg(ptrFile);
00412 }
00413 }
00414 else
00415 {
00416
00417 for (i = NB_SECTOR_TO_SEND ; i > 0 ; i--)
00418 {
00419
00420 while(size > i * FS_SIZE_OF_SECTOR)
00421 {
00422
00423 if( read(fd, ptrFile, i * FS_SIZE_OF_SECTOR) != i * FS_SIZE_OF_SECTOR)
00424 {
00425
00426 print_dbg("Reading file block failed\n");
00427
00428 close(fd);
00429 return (-1);
00430 }
00431
00432 ptrFile[i * FS_SIZE_OF_SECTOR] = '\0';
00433
00434 print_dbg(ptrFile);
00435
00436 size -= (i * FS_SIZE_OF_SECTOR);
00437 }
00438 }
00439
00440 if ( size > 0 )
00441 {
00442
00443 if( read(fd, ptrFile, size) != size)
00444 {
00445
00446 print_dbg("Reading file end failed\n");
00447
00448 close(fd);
00449 return (-1);
00450 }
00451 else
00452 {
00453
00454 ptrFile[size] = '\0';
00455
00456 print_dbg(ptrFile);
00457 }
00458 }
00459 }
00460
00461 free(ptrFile);
00462 }
00463
00464 close(fd);
00465 return (0);
00466 }
00467
00470 int fsaccess_example_write(char * source, char * destination, int flags)
00471 {
00472 char * ptrFile;
00473 int fd1, fd2, i;
00474 long size;
00475 int ErrorCode = -1;
00476
00477 if ((fd1 = open(source, O_RDONLY)) < 0)
00478 {
00479
00480 print_dbg(source);
00481 print_dbg(": Open failed\n");
00482 return (ErrorCode);
00483 }
00484 if ((fd2 = open(destination, flags)) < 0)
00485 {
00486
00487 print_dbg(destination);
00488 print_dbg(": Open failed\n");
00489 return (ErrorCode);
00490 }
00491
00492
00493 size = fsaccess_file_get_size(fd1);
00494
00495 print_dbg("Copying ");
00496 print_dbg_ulong(size);
00497 print_dbg(" Bytes\n");
00498
00499
00500 ptrFile = malloc(NB_SECTOR_TO_SEND * FS_SIZE_OF_SECTOR);
00501
00502
00503 if (ptrFile == NULL)
00504 {
00505
00506 print_dbg("Malloc failed\n");
00507 return (ErrorCode);
00508 }
00509 else
00510 {
00511 if ( size <= (NB_SECTOR_TO_SEND * FS_SIZE_OF_SECTOR) )
00512 {
00513 if ( read(fd1, ptrFile, size) != size )
00514 {
00515
00516 print_dbg("Reading entire file failed\n");
00517
00518 goto close_end;
00519 }
00520 if ( write(fd2, ptrFile, size) != size )
00521 {
00522
00523 print_dbg("Writing entire file failed\n");
00524
00525 goto close_end;
00526 }
00527 }
00528 else
00529 {
00530
00531 for (i = NB_SECTOR_TO_SEND ; i > 0 ; i--)
00532 {
00533
00534 while(size > i * FS_SIZE_OF_SECTOR)
00535 {
00536
00537 memset(ptrFile, 0, NB_SECTOR_TO_SEND * FS_SIZE_OF_SECTOR);
00538
00539 if( read(fd1, ptrFile, i * FS_SIZE_OF_SECTOR) != i * FS_SIZE_OF_SECTOR)
00540 {
00541 print_dbg("Reading file block failed\n");
00542
00543 goto close_end;
00544 }
00545
00546 if ( write(fd2, ptrFile, i * FS_SIZE_OF_SECTOR) != i * FS_SIZE_OF_SECTOR )
00547 {
00548 print_dbg("Writing file block failed\n");
00549
00550 goto close_end;
00551 }
00552
00553 size -= (i * FS_SIZE_OF_SECTOR);
00554 }
00555 }
00556
00557 if ( size > 0 )
00558 {
00559
00560 if( read(fd1, ptrFile, size) != size)
00561 {
00562 print_dbg("Reading file end failed\n");
00563
00564 goto close_end;
00565 }
00566 if ( write(fd2, ptrFile, size) != size )
00567 {
00568 print_dbg("Writing file end failed\n");
00569
00570 goto close_end;
00571 }
00572 }
00573 }
00574
00575 ErrorCode = 0;
00576 }
00577
00578 close_end:
00579
00580 free(ptrFile);
00581
00582 close(fd1);
00583 close(fd2);
00584 return (ErrorCode);
00585 }