Using SPI flash with STM32F407VET6 board.

Dependencies:   SpiFlash25 flash-fs mbed

Fork of Dragonfly_Filesystem_Example by MultiTech

Using the on-board Winbond W25Q16 16Mbit SPI Flash memory chip with STM32F407VET6 black boards

Committer:
mfiore
Date:
Wed Sep 30 16:26:28 2015 +0000
Revision:
5:c0f6a3babedf
Parent:
0:a802928f26b6
Child:
6:11c09ef31db4
clean up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 5:c0f6a3babedf 1 /** Dragonfly Filesystem Example
mfiore 5:c0f6a3babedf 2 * Opens file in filesystem, prints contents of the file, and writes to the file.
mfiore 5:c0f6a3babedf 3 *
mfiore 5:c0f6a3babedf 4 * NOTE: This example changes the baud rate of the debug port to 115200 baud!
mfiore 5:c0f6a3babedf 5 */
mfiore 0:a802928f26b6 6 #include "mbed.h"
mfiore 0:a802928f26b6 7 #include "SpiFlash25.h"
mfiore 0:a802928f26b6 8 #include "spiffs.h"
mfiore 0:a802928f26b6 9 #include <string>
mfiore 0:a802928f26b6 10
mfiore 0:a802928f26b6 11 // this value represents the number of files you can have open at the same time
mfiore 0:a802928f26b6 12 // adjust it according to your requirements
mfiore 0:a802928f26b6 13 #define MAX_CONCURRENT_FDS 4
mfiore 0:a802928f26b6 14
mfiore 0:a802928f26b6 15 #define PAGE_SIZE 256
mfiore 0:a802928f26b6 16 #define SECTOR_SIZE 64*1024
mfiore 0:a802928f26b6 17 #define MEM_SIZE 2*1024*1024
mfiore 0:a802928f26b6 18
mfiore 0:a802928f26b6 19 static u8_t spiffs_work_buf[PAGE_SIZE * 2];
mfiore 0:a802928f26b6 20 static u8_t spiffs_fds[32 * MAX_CONCURRENT_FDS];
mfiore 0:a802928f26b6 21 static u8_t spiffs_cache_buf[(PAGE_SIZE + 32) * 4];
mfiore 0:a802928f26b6 22
mfiore 0:a802928f26b6 23 static SpiFlash25 flash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS1);
mfiore 0:a802928f26b6 24 static spiffs fs;
mfiore 0:a802928f26b6 25 static spiffs_config cfg;
mfiore 0:a802928f26b6 26
mfiore 5:c0f6a3babedf 27 // glue code between SPI driver and filesystem driver
mfiore 0:a802928f26b6 28 int spi_read(unsigned int addr, unsigned int size, unsigned char* data) {
mfiore 0:a802928f26b6 29 if (flash.read(addr, size, (char*)data))
mfiore 0:a802928f26b6 30 return SPIFFS_OK;
mfiore 0:a802928f26b6 31 return -1;
mfiore 0:a802928f26b6 32 }
mfiore 0:a802928f26b6 33 int spi_write(unsigned int addr, unsigned int size, unsigned char* data) {
mfiore 0:a802928f26b6 34 if (flash.write(addr, size, (const char*)data))
mfiore 0:a802928f26b6 35 return SPIFFS_OK;
mfiore 0:a802928f26b6 36 return -1;
mfiore 0:a802928f26b6 37 }
mfiore 0:a802928f26b6 38 int spi_erase(unsigned int addr, unsigned int size) {
mfiore 0:a802928f26b6 39 flash.clear_sector(addr);
mfiore 0:a802928f26b6 40 return SPIFFS_OK;
mfiore 0:a802928f26b6 41 }
mfiore 0:a802928f26b6 42
mfiore 0:a802928f26b6 43 int main(void) {
mfiore 0:a802928f26b6 44 int ret;
mfiore 0:a802928f26b6 45 int handle;
mfiore 0:a802928f26b6 46 char data[256];
mfiore 0:a802928f26b6 47 char msg[] = "Hello World! The sneaky cat crept around the sleeping dog.\r\n";
mfiore 0:a802928f26b6 48 char file[] = "test.txt";
mfiore 0:a802928f26b6 49 string sdata;
mfiore 0:a802928f26b6 50
mfiore 0:a802928f26b6 51 // configure the filesystem
mfiore 0:a802928f26b6 52 cfg.phys_size = MEM_SIZE;
mfiore 0:a802928f26b6 53 cfg.phys_addr = 0;
mfiore 0:a802928f26b6 54 cfg.phys_erase_block = SECTOR_SIZE;
mfiore 0:a802928f26b6 55 cfg.log_block_size = SECTOR_SIZE;
mfiore 0:a802928f26b6 56 cfg.log_page_size = PAGE_SIZE;
mfiore 0:a802928f26b6 57
mfiore 0:a802928f26b6 58 cfg.hal_read_f = &spi_read;
mfiore 0:a802928f26b6 59 cfg.hal_write_f = &spi_write;
mfiore 0:a802928f26b6 60 cfg.hal_erase_f = &spi_erase;
mfiore 0:a802928f26b6 61
mfiore 0:a802928f26b6 62 Serial pc(USBTX, USBRX);
mfiore 0:a802928f26b6 63 pc.baud(115200);
mfiore 0:a802928f26b6 64
mfiore 5:c0f6a3babedf 65 printf("Dragonfly spi flash example started\r\n");
mfiore 0:a802928f26b6 66
mfiore 0:a802928f26b6 67 // erase entire flash
mfiore 0:a802928f26b6 68 // THIS WILL ERASE THE ENTIRE FLASH! EVERYTHING ON IT WILL BE LOST AND CANNOT BE RECOVERED!
mfiore 0:a802928f26b6 69 //flash.clear_mem();
mfiore 0:a802928f26b6 70
mfiore 0:a802928f26b6 71 // mount the filesystem
mfiore 0:a802928f26b6 72 ret = SPIFFS_mount(&fs, &cfg, spiffs_work_buf, spiffs_fds, sizeof(spiffs_fds), spiffs_cache_buf, sizeof(spiffs_cache_buf), NULL);
mfiore 0:a802928f26b6 73 if (ret) {
mfiore 0:a802928f26b6 74 printf("SPIFFS_mount failed %d - can't continue\r\n", ret);
mfiore 0:a802928f26b6 75 return 1;
mfiore 0:a802928f26b6 76 }
mfiore 0:a802928f26b6 77
mfiore 0:a802928f26b6 78 // write to the file
mfiore 0:a802928f26b6 79 handle = SPIFFS_open(&fs, file, SPIFFS_CREAT | SPIFFS_RDWR | SPIFFS_APPEND, 0);
mfiore 0:a802928f26b6 80 if (handle < 0)
mfiore 0:a802928f26b6 81 printf("SPIFFS_open failed %d\r\n", SPIFFS_errno(&fs));
mfiore 0:a802928f26b6 82
mfiore 0:a802928f26b6 83 if (handle) {
mfiore 0:a802928f26b6 84 ret = SPIFFS_write(&fs, handle, msg, sizeof(msg));
mfiore 0:a802928f26b6 85 if (ret < 0)
mfiore 0:a802928f26b6 86 printf("SPIFFS_write failed %d\r\n", SPIFFS_errno(&fs));
mfiore 0:a802928f26b6 87 else
mfiore 5:c0f6a3babedf 88 printf("Wrote %d bytes\r\n", ret);
mfiore 0:a802928f26b6 89 SPIFFS_close(&fs, handle);
mfiore 0:a802928f26b6 90 }
mfiore 0:a802928f26b6 91
mfiore 0:a802928f26b6 92 // read the current file contents
mfiore 0:a802928f26b6 93 spiffs_stat stat;
mfiore 0:a802928f26b6 94 memset(&stat, 0, sizeof(stat));
mfiore 0:a802928f26b6 95 ret = SPIFFS_stat(&fs, file, &stat);
mfiore 0:a802928f26b6 96 if (ret)
mfiore 0:a802928f26b6 97 printf("SPIFFS_stat failed %d\r\n", SPIFFS_errno(&fs));
mfiore 0:a802928f26b6 98 else
mfiore 5:c0f6a3babedf 99 printf("File size: %d bytes\r\n", stat.size);
mfiore 0:a802928f26b6 100
mfiore 0:a802928f26b6 101 handle = SPIFFS_open(&fs, file, SPIFFS_RDWR, 0);
mfiore 0:a802928f26b6 102 if (handle < 0)
mfiore 0:a802928f26b6 103 printf("SPIFFS_open failed %d\r\n", SPIFFS_errno(&fs));
mfiore 0:a802928f26b6 104
mfiore 0:a802928f26b6 105 if (handle) {
mfiore 0:a802928f26b6 106 while (sdata.size() < stat.size) {
mfiore 0:a802928f26b6 107 ret = SPIFFS_read(&fs, handle, data, stat.size - sdata.size() < sizeof(data) ? stat.size - sdata.size() : sizeof(data));
mfiore 0:a802928f26b6 108 if (ret < 0) {
mfiore 0:a802928f26b6 109 printf("SPIFFS_read failed %d\r\n", SPIFFS_errno(&fs));
mfiore 0:a802928f26b6 110 continue;
mfiore 0:a802928f26b6 111 }
mfiore 5:c0f6a3babedf 112 printf("Read %d bytes\r\n", ret);
mfiore 0:a802928f26b6 113 sdata.append(data, ret);
mfiore 0:a802928f26b6 114 }
mfiore 0:a802928f26b6 115
mfiore 5:c0f6a3babedf 116 printf("Data [\r\n");
mfiore 0:a802928f26b6 117 for (int i = 0; i < sdata.size(); i++)
mfiore 0:a802928f26b6 118 printf("%c", sdata[i]);
mfiore 0:a802928f26b6 119 printf("\r\n]\r\n");
mfiore 0:a802928f26b6 120
mfiore 0:a802928f26b6 121 SPIFFS_close(&fs, handle);
mfiore 0:a802928f26b6 122 }
mfiore 0:a802928f26b6 123
mfiore 5:c0f6a3babedf 124 printf("Dragonfly spi flash example finished\r\n\r\n");
mfiore 0:a802928f26b6 125
mfiore 0:a802928f26b6 126 return 0;
mfiore 0:a802928f26b6 127 }