Using SPI flash with STM32F407VET6 board.
Dependencies: SpiFlash25 flash-fs mbed
Fork of Dragonfly_Filesystem_Example by
Using the on-board Winbond W25Q16 16Mbit SPI Flash memory chip with STM32F407VET6 black boards
main.cpp@7:afdf19fe2341, 2018-04-28 (annotated)
- Committer:
- hudakz
- Date:
- Sat Apr 28 15:59:04 2018 +0000
- Revision:
- 7:afdf19fe2341
- Parent:
- 6:11c09ef31db4
Initial reelase.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 7:afdf19fe2341 | 1 | /** SPI Flash 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 | */ |
mfiore | 0:a802928f26b6 | 5 | #include "mbed.h" |
mfiore | 0:a802928f26b6 | 6 | #include "SpiFlash25.h" |
mfiore | 0:a802928f26b6 | 7 | #include "spiffs.h" |
mfiore | 0:a802928f26b6 | 8 | #include <string> |
mfiore | 0:a802928f26b6 | 9 | |
mfiore | 0:a802928f26b6 | 10 | // this value represents the number of files you can have open at the same time |
mfiore | 0:a802928f26b6 | 11 | // adjust it according to your requirements |
hudakz | 7:afdf19fe2341 | 12 | #define MAX_CONCURRENT_FDS 2 |
mfiore | 0:a802928f26b6 | 13 | |
mfiore | 0:a802928f26b6 | 14 | #define PAGE_SIZE 256 |
mfiore | 0:a802928f26b6 | 15 | #define SECTOR_SIZE 64*1024 |
mfiore | 0:a802928f26b6 | 16 | #define MEM_SIZE 2*1024*1024 |
mfiore | 0:a802928f26b6 | 17 | |
mfiore | 0:a802928f26b6 | 18 | static u8_t spiffs_work_buf[PAGE_SIZE * 2]; |
mfiore | 0:a802928f26b6 | 19 | static u8_t spiffs_fds[32 * MAX_CONCURRENT_FDS]; |
mfiore | 0:a802928f26b6 | 20 | static u8_t spiffs_cache_buf[(PAGE_SIZE + 32) * 4]; |
mfiore | 0:a802928f26b6 | 21 | |
hudakz | 7:afdf19fe2341 | 22 | static SpiFlash25 flash(PB_5, PB_4, PB_3, PB_0); |
mfiore | 0:a802928f26b6 | 23 | static spiffs fs; |
mfiore | 0:a802928f26b6 | 24 | static spiffs_config cfg; |
mfiore | 0:a802928f26b6 | 25 | |
mfiore | 5:c0f6a3babedf | 26 | // glue code between SPI driver and filesystem driver |
mfiore | 0:a802928f26b6 | 27 | int spi_read(unsigned int addr, unsigned int size, unsigned char* data) { |
mfiore | 0:a802928f26b6 | 28 | if (flash.read(addr, size, (char*)data)) |
mfiore | 0:a802928f26b6 | 29 | return SPIFFS_OK; |
mfiore | 0:a802928f26b6 | 30 | return -1; |
mfiore | 0:a802928f26b6 | 31 | } |
mfiore | 0:a802928f26b6 | 32 | int spi_write(unsigned int addr, unsigned int size, unsigned char* data) { |
mfiore | 0:a802928f26b6 | 33 | if (flash.write(addr, size, (const char*)data)) |
mfiore | 0:a802928f26b6 | 34 | return SPIFFS_OK; |
mfiore | 0:a802928f26b6 | 35 | return -1; |
mfiore | 0:a802928f26b6 | 36 | } |
mfiore | 0:a802928f26b6 | 37 | int spi_erase(unsigned int addr, unsigned int size) { |
mfiore | 0:a802928f26b6 | 38 | flash.clear_sector(addr); |
mfiore | 0:a802928f26b6 | 39 | return SPIFFS_OK; |
mfiore | 0:a802928f26b6 | 40 | } |
mfiore | 0:a802928f26b6 | 41 | |
mfiore | 0:a802928f26b6 | 42 | int main(void) { |
mfiore | 0:a802928f26b6 | 43 | int ret; |
mfiore | 0:a802928f26b6 | 44 | int handle; |
mfiore | 0:a802928f26b6 | 45 | char data[256]; |
hudakz | 7:afdf19fe2341 | 46 | char msg[] = "Hello World!\r\n"; |
mfiore | 0:a802928f26b6 | 47 | char file[] = "test.txt"; |
mfiore | 0:a802928f26b6 | 48 | string sdata; |
mfiore | 0:a802928f26b6 | 49 | |
mfiore | 0:a802928f26b6 | 50 | // configure the filesystem |
mfiore | 0:a802928f26b6 | 51 | cfg.phys_size = MEM_SIZE; |
mfiore | 0:a802928f26b6 | 52 | cfg.phys_addr = 0; |
mfiore | 0:a802928f26b6 | 53 | cfg.phys_erase_block = SECTOR_SIZE; |
mfiore | 0:a802928f26b6 | 54 | cfg.log_block_size = SECTOR_SIZE; |
mfiore | 0:a802928f26b6 | 55 | cfg.log_page_size = PAGE_SIZE; |
mfiore | 0:a802928f26b6 | 56 | |
mfiore | 0:a802928f26b6 | 57 | cfg.hal_read_f = &spi_read; |
mfiore | 0:a802928f26b6 | 58 | cfg.hal_write_f = &spi_write; |
mfiore | 0:a802928f26b6 | 59 | cfg.hal_erase_f = &spi_erase; |
mfiore | 0:a802928f26b6 | 60 | |
hudakz | 7:afdf19fe2341 | 61 | printf("\r\n\r\nSPI flash example started.\r\n\r\n"); |
mfiore | 0:a802928f26b6 | 62 | |
mfiore | 0:a802928f26b6 | 63 | // erase entire flash |
mfiore | 0:a802928f26b6 | 64 | // THIS WILL ERASE THE ENTIRE FLASH! EVERYTHING ON IT WILL BE LOST AND CANNOT BE RECOVERED! |
hudakz | 7:afdf19fe2341 | 65 | flash.clear_mem(); |
mfiore | 0:a802928f26b6 | 66 | |
hudakz | 7:afdf19fe2341 | 67 | printf("Mounting SPI flash file system ... "); |
mfiore | 0:a802928f26b6 | 68 | // mount the filesystem |
mfiore | 0:a802928f26b6 | 69 | ret = SPIFFS_mount(&fs, &cfg, spiffs_work_buf, spiffs_fds, sizeof(spiffs_fds), spiffs_cache_buf, sizeof(spiffs_cache_buf), NULL); |
mfiore | 0:a802928f26b6 | 70 | if (ret) { |
hudakz | 7:afdf19fe2341 | 71 | printf("Failed.\r\n"); |
hudakz | 7:afdf19fe2341 | 72 | return ret; |
mfiore | 0:a802928f26b6 | 73 | } |
hudakz | 7:afdf19fe2341 | 74 | else |
hudakz | 7:afdf19fe2341 | 75 | printf("OK.\r\n"); |
mfiore | 0:a802928f26b6 | 76 | |
mfiore | 0:a802928f26b6 | 77 | // write to the file |
hudakz | 7:afdf19fe2341 | 78 | printf("Opening file 'test.txt' for writing ... "); |
mfiore | 0:a802928f26b6 | 79 | handle = SPIFFS_open(&fs, file, SPIFFS_CREAT | SPIFFS_RDWR | SPIFFS_APPEND, 0); |
hudakz | 7:afdf19fe2341 | 80 | if (handle < 0) { |
hudakz | 7:afdf19fe2341 | 81 | printf("Failed.\r\n"); |
hudakz | 7:afdf19fe2341 | 82 | return SPIFFS_errno(&fs); |
hudakz | 7:afdf19fe2341 | 83 | } |
hudakz | 7:afdf19fe2341 | 84 | else |
hudakz | 7:afdf19fe2341 | 85 | printf("OK.\r\n"); |
mfiore | 0:a802928f26b6 | 86 | |
hudakz | 7:afdf19fe2341 | 87 | printf("Writing data to the file ... "); |
mfiore | 0:a802928f26b6 | 88 | if (handle) { |
mfiore | 0:a802928f26b6 | 89 | ret = SPIFFS_write(&fs, handle, msg, sizeof(msg)); |
hudakz | 7:afdf19fe2341 | 90 | if (ret < 0) { |
hudakz | 7:afdf19fe2341 | 91 | printf("Failed.\r\n"); |
hudakz | 7:afdf19fe2341 | 92 | return SPIFFS_errno(&fs); |
hudakz | 7:afdf19fe2341 | 93 | } |
mfiore | 0:a802928f26b6 | 94 | else |
hudakz | 7:afdf19fe2341 | 95 | printf("OK. \r\nWrote %d bytes to the file.\r\n", ret); |
mfiore | 0:a802928f26b6 | 96 | SPIFFS_close(&fs, handle); |
hudakz | 7:afdf19fe2341 | 97 | printf("File closed.\r\n"); |
mfiore | 0:a802928f26b6 | 98 | } |
mfiore | 0:a802928f26b6 | 99 | |
mfiore | 0:a802928f26b6 | 100 | // read the current file contents |
hudakz | 7:afdf19fe2341 | 101 | printf("Getting size of 'test.tx' file ... "); |
mfiore | 0:a802928f26b6 | 102 | spiffs_stat stat; |
mfiore | 0:a802928f26b6 | 103 | memset(&stat, 0, sizeof(stat)); |
mfiore | 0:a802928f26b6 | 104 | ret = SPIFFS_stat(&fs, file, &stat); |
hudakz | 7:afdf19fe2341 | 105 | if (ret) { |
hudakz | 7:afdf19fe2341 | 106 | printf("Failed.\r\n"); |
hudakz | 7:afdf19fe2341 | 107 | return SPIFFS_errno(&fs); |
hudakz | 7:afdf19fe2341 | 108 | } |
mfiore | 0:a802928f26b6 | 109 | else |
hudakz | 7:afdf19fe2341 | 110 | printf("OK.\r\nFile size: %d bytes.\r\n", stat.size); |
mfiore | 0:a802928f26b6 | 111 | |
hudakz | 7:afdf19fe2341 | 112 | printf("Opening file 'text.txt' for reading ... "); |
mfiore | 0:a802928f26b6 | 113 | handle = SPIFFS_open(&fs, file, SPIFFS_RDWR, 0); |
hudakz | 7:afdf19fe2341 | 114 | if (handle < 0) { |
hudakz | 7:afdf19fe2341 | 115 | printf("Failed.\r\n"); |
hudakz | 7:afdf19fe2341 | 116 | return SPIFFS_errno(&fs); |
hudakz | 7:afdf19fe2341 | 117 | } |
hudakz | 7:afdf19fe2341 | 118 | else |
hudakz | 7:afdf19fe2341 | 119 | printf("OK.\r\n"); |
mfiore | 0:a802928f26b6 | 120 | |
hudakz | 7:afdf19fe2341 | 121 | printf("Reading data from the file ... "); |
mfiore | 0:a802928f26b6 | 122 | if (handle) { |
mfiore | 0:a802928f26b6 | 123 | while (sdata.size() < stat.size) { |
mfiore | 0:a802928f26b6 | 124 | ret = SPIFFS_read(&fs, handle, data, stat.size - sdata.size() < sizeof(data) ? stat.size - sdata.size() : sizeof(data)); |
mfiore | 0:a802928f26b6 | 125 | if (ret < 0) { |
hudakz | 7:afdf19fe2341 | 126 | printf("\r\nRead failed. Error: %d\r\n", SPIFFS_errno(&fs)); |
mfiore | 0:a802928f26b6 | 127 | continue; |
mfiore | 0:a802928f26b6 | 128 | } |
hudakz | 7:afdf19fe2341 | 129 | printf("OK.\r\nRead %d bytes.\r\n", ret); |
mfiore | 0:a802928f26b6 | 130 | sdata.append(data, ret); |
mfiore | 0:a802928f26b6 | 131 | } |
mfiore | 0:a802928f26b6 | 132 | |
hudakz | 7:afdf19fe2341 | 133 | printf("File content: "); |
mfiore | 0:a802928f26b6 | 134 | for (int i = 0; i < sdata.size(); i++) |
mfiore | 0:a802928f26b6 | 135 | printf("%c", sdata[i]); |
hudakz | 7:afdf19fe2341 | 136 | printf("\r\n"); |
mfiore | 0:a802928f26b6 | 137 | |
mfiore | 0:a802928f26b6 | 138 | SPIFFS_close(&fs, handle); |
hudakz | 7:afdf19fe2341 | 139 | printf("File closed.\r\n"); |
mfiore | 0:a802928f26b6 | 140 | } |
mfiore | 0:a802928f26b6 | 141 | |
hudakz | 7:afdf19fe2341 | 142 | printf("\r\nSPI flash example finished\r\n\r\n"); |
mfiore | 0:a802928f26b6 | 143 | |
mfiore | 0:a802928f26b6 | 144 | return 0; |
mfiore | 0:a802928f26b6 | 145 | } |