Using SPI flash with STM32F407VET6 board.

Dependencies:   SpiFlash25 flash-fs mbed

Fork of Dragonfly_Filesystem_Example by MultiTech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /** SPI Flash Filesystem Example
00002  * Opens file in filesystem, prints contents of the file, and writes to the file.
00003  *
00004  */
00005 #include "mbed.h"
00006 #include "SpiFlash25.h"
00007 #include "spiffs.h"
00008 #include <string>
00009 
00010 // this value represents the number of files you can have open at the same time
00011 // adjust it according to your requirements
00012 #define MAX_CONCURRENT_FDS      2
00013 
00014 #define PAGE_SIZE               256
00015 #define SECTOR_SIZE             64*1024
00016 #define MEM_SIZE                2*1024*1024
00017 
00018 static u8_t spiffs_work_buf[PAGE_SIZE * 2];
00019 static u8_t spiffs_fds[32 * MAX_CONCURRENT_FDS];
00020 static u8_t spiffs_cache_buf[(PAGE_SIZE + 32) * 4];
00021 
00022 static SpiFlash25 flash(PB_5, PB_4, PB_3, PB_0);
00023 static spiffs fs;
00024 static spiffs_config cfg;
00025 
00026 // glue code between SPI driver and filesystem driver
00027 int spi_read(unsigned int addr, unsigned int size, unsigned char* data) {
00028     if (flash.read(addr, size, (char*)data))
00029         return SPIFFS_OK;
00030     return -1;
00031 }
00032 int spi_write(unsigned int addr, unsigned int size, unsigned char* data) {
00033     if (flash.write(addr, size, (const char*)data))
00034         return SPIFFS_OK;
00035     return -1;
00036 }
00037 int spi_erase(unsigned int addr, unsigned int size) {
00038     flash.clear_sector(addr);
00039     return SPIFFS_OK;
00040 }
00041 
00042 int main(void) {
00043     int ret;
00044     int handle;
00045     char data[256];
00046     char msg[] = "Hello World!\r\n";
00047     char file[] = "test.txt";
00048     string sdata;
00049 
00050     // configure the filesystem
00051     cfg.phys_size = MEM_SIZE;
00052     cfg.phys_addr = 0;
00053     cfg.phys_erase_block = SECTOR_SIZE;
00054     cfg.log_block_size = SECTOR_SIZE;
00055     cfg.log_page_size = PAGE_SIZE;
00056 
00057     cfg.hal_read_f = &spi_read;
00058     cfg.hal_write_f = &spi_write;
00059     cfg.hal_erase_f = &spi_erase;
00060 
00061     printf("\r\n\r\nSPI flash example started.\r\n\r\n");
00062 
00063     // erase entire flash
00064     // THIS WILL ERASE THE ENTIRE FLASH! EVERYTHING ON IT WILL BE LOST AND CANNOT BE RECOVERED!
00065     flash.clear_mem();
00066 
00067     printf("Mounting SPI flash file system ... ");
00068     // mount the filesystem
00069     ret = SPIFFS_mount(&fs, &cfg, spiffs_work_buf, spiffs_fds, sizeof(spiffs_fds), spiffs_cache_buf, sizeof(spiffs_cache_buf), NULL);
00070     if (ret) {
00071         printf("Failed.\r\n");
00072         return ret;
00073     }
00074     else
00075         printf("OK.\r\n");
00076 
00077     // write to the file
00078     printf("Opening file 'test.txt' for writing ... ");
00079     handle = SPIFFS_open(&fs, file, SPIFFS_CREAT | SPIFFS_RDWR | SPIFFS_APPEND, 0);
00080     if (handle < 0) {
00081         printf("Failed.\r\n");
00082         return SPIFFS_errno(&fs);
00083     }
00084     else
00085         printf("OK.\r\n");
00086 
00087     printf("Writing data to the file ... ");
00088     if (handle) {
00089         ret = SPIFFS_write(&fs, handle, msg, sizeof(msg));
00090         if (ret < 0) {
00091             printf("Failed.\r\n");
00092             return SPIFFS_errno(&fs);
00093         }
00094         else
00095             printf("OK. \r\nWrote %d bytes to the file.\r\n", ret);
00096         SPIFFS_close(&fs, handle);
00097         printf("File closed.\r\n");
00098     }
00099 
00100     // read the current file contents
00101     printf("Getting size of 'test.tx' file ... ");
00102     spiffs_stat stat;
00103     memset(&stat, 0, sizeof(stat));
00104     ret = SPIFFS_stat(&fs, file, &stat);
00105     if (ret) {
00106         printf("Failed.\r\n");
00107         return SPIFFS_errno(&fs);
00108     }
00109     else
00110         printf("OK.\r\nFile size: %d bytes.\r\n", stat.size);
00111 
00112     printf("Opening file 'text.txt' for reading ... ");
00113     handle = SPIFFS_open(&fs, file, SPIFFS_RDWR, 0);
00114     if (handle < 0) {
00115         printf("Failed.\r\n");
00116         return SPIFFS_errno(&fs);
00117     }
00118     else
00119         printf("OK.\r\n");
00120 
00121     printf("Reading data from the file ... ");
00122     if (handle) {
00123         while (sdata.size() < stat.size) {
00124             ret = SPIFFS_read(&fs, handle, data, stat.size - sdata.size() < sizeof(data) ? stat.size - sdata.size() : sizeof(data));
00125             if (ret < 0) {
00126                 printf("\r\nRead failed. Error: %d\r\n", SPIFFS_errno(&fs));
00127                 continue;
00128             }
00129             printf("OK.\r\nRead %d bytes.\r\n", ret);
00130             sdata.append(data, ret);
00131         }
00132 
00133         printf("File content: ");
00134         for (int i = 0; i < sdata.size(); i++)
00135             printf("%c", sdata[i]);
00136         printf("\r\n");
00137 
00138         SPIFFS_close(&fs, handle);
00139         printf("File closed.\r\n");
00140     }
00141 
00142     printf("\r\nSPI flash example finished\r\n\r\n");
00143 
00144     return 0;
00145 }