Run a filesystem on the Dragonfly's SPI flash.

Dependencies:   SpiFlash25 flash-fs mbed

Fork of flash-fs-example by MultiTech

Committer:
mfiore
Date:
Fri Feb 26 16:53:58 2016 +0000
Revision:
6:11c09ef31db4
Parent:
5:c0f6a3babedf
Updated mbed library to revision 112, disable regulator's battery charger

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