This file gives an example of using the FS access, through POSIX commands.
Definition in file fsaccess_example.c.
#include <string.h>
#include "compiler.h"
#include "preprocessor.h"
#include "board.h"
#include "print_funcs.h"
#include "power_clocks_lib.h"
#include "gpio.h"
#include "usart.h"
#include "spi.h"
#include "conf_at45dbx.h"
#include "fsaccess.h"
Go to the source code of this file.
Defines | |
#define | ABORT_CHAR CTRL_C |
#define | AT45DBX_ENABLE_NPCS_PIN(NPCS, unused) {AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION}, |
#define | BKSPACE_CHAR '\b' |
#define | CR '\r' |
#define | CTRL_C 0x03 |
#define | CTRL_Q 0x11 |
#define | LF '\n' |
#define | MSG_FILENAME "Enter the filename : " |
#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 : " |
#define | MSG_WELCOME "\r\n ---------- Welcome to FSACCESS Example ---------- \r\n" |
#define | NB_SECTOR_TO_SEND 4 |
#define | QUIT_APPEND CTRL_Q |
#define | TEST_FAIL "\t[FAIL]\n" |
#define | TEST_SUCCESS "\t[PASS]\n" |
Functions | |
static void | at45dbx_resources_init (void) |
Initialize daflash memory AT45DBX resources: GPIO, SPI and AT45DBX. | |
void | fsaccess_example_get_filename (char *buf) |
Wait for user to enter a filename. | |
int | fsaccess_example_get_mode (void) |
Wait for user to select the mode he wants to test. | |
int | fsaccess_example_read (char *path) |
Read a file and display it to USART. | |
int | fsaccess_example_write (char *source, char *destination, int flags) |
Copy a file to another one. | |
int | main (void) |
Main function, execution starts here. RS232 is used to input/output information. The example lets you issue the following commands on RS232 using POSIX interfaces (open, read, write, close):
|
#define ABORT_CHAR CTRL_C |
#define AT45DBX_ENABLE_NPCS_PIN | ( | NPCS, | |||
unused | ) | {AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION}, |
Referenced by at45dbx_resources_init().
#define BKSPACE_CHAR '\b' |
#define CR '\r' |
#define CTRL_C 0x03 |
Definition at line 129 of file fsaccess_example.c.
#define CTRL_Q 0x11 |
Definition at line 128 of file fsaccess_example.c.
#define LF '\n' |
Definition at line 127 of file fsaccess_example.c.
Referenced by fsaccess_example_get_filename(), fsaccess_example_get_mode(), and fsaccess_example_read().
#define MSG_FILENAME "Enter the filename : " |
#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 : " |
#define MSG_WELCOME "\r\n ---------- Welcome to FSACCESS Example ---------- \r\n" |
#define NB_SECTOR_TO_SEND 4 |
Definition at line 135 of file fsaccess_example.c.
Referenced by fsaccess_example_read(), and fsaccess_example_write().
#define QUIT_APPEND CTRL_Q |
Definition at line 132 of file fsaccess_example.c.
#define TEST_FAIL "\t[FAIL]\n" |
Definition at line 124 of file fsaccess_example.c.
#define TEST_SUCCESS "\t[PASS]\n" |
Definition at line 123 of file fsaccess_example.c.
static void at45dbx_resources_init | ( | void | ) | [static] |
Initialize daflash memory AT45DBX resources: GPIO, SPI and AT45DBX.
Definition at line 149 of file fsaccess_example.c.
References AT45DBX_ENABLE_NPCS_PIN, AT45DBX_MEM_CNT, AT45DBX_SPI_BITS, AT45DBX_SPI_FIRST_NPCS, and AT45DBX_SPI_MASTER_SPEED.
Referenced by main().
00150 { 00151 static const gpio_map_t AT45DBX_SPI_GPIO_MAP = 00152 { 00153 {AT45DBX_SPI_SCK_PIN, AT45DBX_SPI_SCK_FUNCTION }, // SPI Clock. 00154 {AT45DBX_SPI_MISO_PIN, AT45DBX_SPI_MISO_FUNCTION }, // MISO. 00155 {AT45DBX_SPI_MOSI_PIN, AT45DBX_SPI_MOSI_FUNCTION }, // MOSI. 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 // SPI options. 00163 spi_options_t spiOptions = 00164 { 00165 .reg = AT45DBX_SPI_FIRST_NPCS, // Defined in conf_at45dbx.h. 00166 .baudrate = AT45DBX_SPI_MASTER_SPEED, // Defined in conf_at45dbx.h. 00167 .bits = AT45DBX_SPI_BITS, // Defined in conf_at45dbx.h. 00168 .spck_delay = 0, 00169 .trans_delay = 0, 00170 .stay_act = 1, 00171 .spi_mode = 0, 00172 .modfdis = 1 00173 }; 00174 00175 // Assign I/Os to SPI. 00176 gpio_enable_module(AT45DBX_SPI_GPIO_MAP, 00177 sizeof(AT45DBX_SPI_GPIO_MAP) / sizeof(AT45DBX_SPI_GPIO_MAP[0])); 00178 00179 // Initialize as master. 00180 spi_initMaster(AT45DBX_SPI, &spiOptions); 00181 00182 // Set selection mode: variable_ps, pcs_decode, delay. 00183 spi_selectionMode(AT45DBX_SPI, 0, 0, 0); 00184 00185 // Enable SPI. 00186 spi_enable(AT45DBX_SPI); 00187 00188 // Initialize data flash with SPI clock Osc0. 00189 at45dbx_init(spiOptions, FOSC0); 00190 }
void fsaccess_example_get_filename | ( | char * | buf | ) |
Wait for user to enter a filename.
Definition at line 298 of file fsaccess_example.c.
References ABORT_CHAR, BKSPACE_CHAR, CR, LF, and MSG_FILENAME.
Referenced by main().
00299 { 00300 unsigned long i_str = 3; 00301 unsigned short file; 00302 int c; 00303 00304 // Display asking message 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 // If something new in the USART : 00314 usart_reset_status(DBG_USART); 00315 if (usart_read_char(DBG_USART, &c) == USART_SUCCESS) 00316 { 00317 // Regarding the received char : 00318 switch (c) 00319 { 00320 // This is a carriage return 00321 case CR: 00322 // Add LF 00323 print_dbg_char(LF); 00324 // Add NUL char 00325 buf[i_str] = '\0'; 00326 // End the loop 00327 file = TRUE; 00328 // Reset string length 00329 i_str = 0; 00330 break; 00331 // This is a CTRL+C 00332 case ABORT_CHAR: 00333 i_str = 0; 00334 print_dbg_char(LF); // Add LF 00335 print_dbg(MSG_FILENAME); 00336 break; 00337 // This is a backspace 00338 case BKSPACE_CHAR: 00339 if (i_str > 0) 00340 { 00341 // Echo the backspace 00342 print_dbg_char(c); 00343 // Echo a space 00344 print_dbg_char(' '); 00345 // Echo the backspace 00346 print_dbg_char(c); 00347 // Decrease string length 00348 i_str--; 00349 } 00350 break; 00351 // This is a char 00352 default: 00353 // Echo it 00354 print_dbg_char(c); 00355 // Append to cmd line and increase string length 00356 buf[i_str++] = c; 00357 break; 00358 } 00359 } 00360 } 00361 }
int fsaccess_example_get_mode | ( | void | ) |
Wait for user to select the mode he wants to test.
Definition at line 275 of file fsaccess_example.c.
Referenced by main().
00276 { 00277 int c; 00278 00279 // Display asking message 00280 print_dbg(MSG_MODE); 00281 while (TRUE) 00282 { 00283 // If something new in the USART : 00284 usart_reset_status(DBG_USART); 00285 if (usart_read_char(DBG_USART, &c) == USART_SUCCESS) 00286 { 00287 // Echo char 00288 print_dbg_char(c); 00289 // Add LF 00290 print_dbg_char(LF); 00291 return (c); 00292 } 00293 } 00294 }
int fsaccess_example_read | ( | char * | path | ) |
Read a file and display it to USART.
Definition at line 365 of file fsaccess_example.c.
References close(), FS_SIZE_OF_SECTOR, fsaccess_file_get_size(), LF, NB_SECTOR_TO_SEND, O_RDONLY, open(), and read().
Referenced by main().
00366 { 00367 char * ptrFile; 00368 int fd, i; 00369 long size; 00370 00371 // Try to open the file 00372 if ((fd = open(path, O_RDONLY)) < 0) 00373 { 00374 // Display error message 00375 print_dbg(path); 00376 print_dbg(" : Open failed\n"); 00377 return (-1); 00378 } 00379 00380 // Get file size 00381 size = fsaccess_file_get_size(fd); 00382 // Display file size 00383 print_dbg("File size: "); 00384 print_dbg_ulong(size); 00385 print_dbg_char(LF); 00386 00387 // Allocate a buffer 00388 ptrFile = malloc((NB_SECTOR_TO_SEND * FS_SIZE_OF_SECTOR) + 1); 00389 00390 // Allocation fails 00391 if (ptrFile == NULL) 00392 { 00393 // Display error message 00394 print_dbg("Malloc failed\n"); 00395 } 00396 else 00397 { 00398 // Try to perform a single access 00399 if ( size < (NB_SECTOR_TO_SEND * FS_SIZE_OF_SECTOR) ) 00400 { 00401 if( read(fd, ptrFile, size) != size) 00402 { 00403 // Display error message 00404 print_dbg("Reading entire file failed\n"); 00405 } 00406 else 00407 { 00408 // Add a null terminating char 00409 ptrFile[size] = '\0'; 00410 // Display the buffer to user 00411 print_dbg(ptrFile); 00412 } 00413 } 00414 else 00415 { 00416 // Try to send the biggest frame contained in the file 00417 for (i = NB_SECTOR_TO_SEND ; i > 0 ; i--) 00418 { 00419 // Get sectors of maximum size 00420 while(size > i * FS_SIZE_OF_SECTOR) 00421 { 00422 // Get the data from file 00423 if( read(fd, ptrFile, i * FS_SIZE_OF_SECTOR) != i * FS_SIZE_OF_SECTOR) 00424 { 00425 // Display error message 00426 print_dbg("Reading file block failed\n"); 00427 // Close file 00428 close(fd); 00429 return (-1); 00430 } 00431 // Add a null terminating character 00432 ptrFile[i * FS_SIZE_OF_SECTOR] = '\0'; 00433 // Display buffer content to user 00434 print_dbg(ptrFile); 00435 // Decrease remaining size 00436 size -= (i * FS_SIZE_OF_SECTOR); 00437 } 00438 } 00439 // Finish with the few data remaining (less than 1 sector) 00440 if ( size > 0 ) 00441 { 00442 // Get the data from filesystem 00443 if( read(fd, ptrFile, size) != size) 00444 { 00445 // Display error message 00446 print_dbg("Reading file end failed\n"); 00447 // Close file 00448 close(fd); 00449 return (-1); 00450 } 00451 else 00452 { 00453 // Add a null terminating char 00454 ptrFile[size] = '\0'; 00455 // Display the buffer to user 00456 print_dbg(ptrFile); 00457 } 00458 } 00459 } 00460 // Free the buffer 00461 free(ptrFile); 00462 } 00463 // Close file 00464 close(fd); 00465 return (0); 00466 }
int fsaccess_example_write | ( | char * | source, | |
char * | destination, | |||
int | flags | |||
) |
Copy a file to another one.
Definition at line 470 of file fsaccess_example.c.
References close(), FS_SIZE_OF_SECTOR, fsaccess_file_get_size(), NB_SECTOR_TO_SEND, O_RDONLY, open(), read(), and write().
Referenced by main().
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 // Display error message 00480 print_dbg(source); 00481 print_dbg(": Open failed\n"); 00482 return (ErrorCode); 00483 } 00484 if ((fd2 = open(destination, flags)) < 0) 00485 { 00486 // Display error message 00487 print_dbg(destination); 00488 print_dbg(": Open failed\n"); 00489 return (ErrorCode); 00490 } 00491 00492 // Get file size 00493 size = fsaccess_file_get_size(fd1); 00494 // Display file size 00495 print_dbg("Copying "); 00496 print_dbg_ulong(size); 00497 print_dbg(" Bytes\n"); 00498 00499 // Allocate a buffer 00500 ptrFile = malloc(NB_SECTOR_TO_SEND * FS_SIZE_OF_SECTOR); 00501 00502 // Allocation fails 00503 if (ptrFile == NULL) 00504 { 00505 // Display error message 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 // Display error message 00516 print_dbg("Reading entire file failed\n"); 00517 // Escape 00518 goto close_end; 00519 } 00520 if ( write(fd2, ptrFile, size) != size ) 00521 { 00522 // Display error message 00523 print_dbg("Writing entire file failed\n"); 00524 // Escape 00525 goto close_end; 00526 } 00527 } 00528 else 00529 { 00530 // Try to send the biggest frame contained in the file 00531 for (i = NB_SECTOR_TO_SEND ; i > 0 ; i--) 00532 { 00533 // Get sectors of maximum size 00534 while(size > i * FS_SIZE_OF_SECTOR) 00535 { 00536 // Clear previous buffer 00537 memset(ptrFile, 0, NB_SECTOR_TO_SEND * FS_SIZE_OF_SECTOR); 00538 // Read the data from source file 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 // Escape 00543 goto close_end; 00544 } 00545 // Write the data to destination file 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 // Escape 00550 goto close_end; 00551 } 00552 // Decrease remaining size 00553 size -= (i * FS_SIZE_OF_SECTOR); 00554 } 00555 } 00556 // Finish with the few data remaining (less than 1 sector) 00557 if ( size > 0 ) 00558 { 00559 // Get the data from filesystem 00560 if( read(fd1, ptrFile, size) != size) 00561 { 00562 print_dbg("Reading file end failed\n"); 00563 // Escape 00564 goto close_end; 00565 } 00566 if ( write(fd2, ptrFile, size) != size ) 00567 { 00568 print_dbg("Writing file end failed\n"); 00569 // Escape 00570 goto close_end; 00571 } 00572 } 00573 } 00574 // No error occurs here 00575 ErrorCode = 0; 00576 } 00577 00578 close_end: 00579 // Free the buffer 00580 free(ptrFile); 00581 // Close files 00582 close(fd1); 00583 close(fd2); 00584 return (ErrorCode); 00585 }
int main | ( | void | ) |
Main function, execution starts here. RS232 is used to input/output information. The example lets you issue the following commands on RS232 using POSIX interfaces (open, read, write, close):
Connect USART_1 to your serial port via a standard RS-232 D-SUB9 cable Set the following settings in your terminal of choice: 57600 8N1
Definition at line 204 of file fsaccess_example.c.
References at45dbx_resources_init(), b_fsaccess_init(), fsaccess_example_get_filename(), fsaccess_example_get_mode(), fsaccess_example_read(), fsaccess_example_write(), MSG_WELCOME, O_APPEND, and O_CREAT.
00205 { 00206 char filename1[90]; 00207 char filename2[90]; 00208 00209 // switch to external oscillator 0 00210 pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); 00211 00212 // initialize RS232 debug text output 00213 init_dbg_rs232(FOSC0); 00214 00215 // initialize AT45DBX resources: GPIO, SPI and AT45DBX 00216 at45dbx_resources_init(); 00217 00218 // initialize FSACCESS mutex and navigators 00219 b_fsaccess_init(); 00220 00221 // Try to init data flash 00222 if (at45dbx_mem_check()) 00223 { 00224 // display welcome message 00225 print_dbg(MSG_WELCOME); 00226 } 00227 else 00228 { 00229 // display error message 00230 print_dbg("Initialization failed\r\n"); 00231 return (-1); 00232 } 00233 00234 while (TRUE) 00235 { 00236 // check the user command 00237 switch (fsaccess_example_get_mode()) 00238 { 00239 case '1': 00240 // Wait for a filename 00241 fsaccess_example_get_filename(filename1); 00242 // Read it and display the content 00243 fsaccess_example_read(filename1); 00244 break; 00245 case '2': 00246 // Wait for source filename 00247 print_dbg("Source : "); 00248 fsaccess_example_get_filename(filename1); 00249 // Wait for destination filename 00250 print_dbg("Destination : "); 00251 fsaccess_example_get_filename(filename2); 00252 // Write from source to destination (append to the existing file) 00253 fsaccess_example_write(filename1, filename2, O_APPEND); 00254 break; 00255 case '3': 00256 // Wait for source filename 00257 print_dbg("Source : "); 00258 fsaccess_example_get_filename(filename1); 00259 // Wait for destination filename 00260 print_dbg("Destination : "); 00261 fsaccess_example_get_filename(filename2); 00262 // Write from source to destination (append to the unexisting file) 00263 fsaccess_example_write(filename1, filename2, (O_CREAT | O_APPEND)); 00264 break; 00265 default: 00266 // Display error message 00267 print_dbg("Wrong choice\r\n"); 00268 break; 00269 } 00270 } 00271 }