It show how to use storage_on_flash library. The storage_on_flash is tiny storage implementation on STM32 flash memory.

Dependencies:   mbed storage_on_flash

Committer:
hillkim7
Date:
Mon Jan 19 14:47:00 2015 +0000
Revision:
1:9e21adbf4f8e
Parent:
0:e13134122925
Function name modified.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hillkim7 0:e13134122925 1 /*
hillkim7 0:e13134122925 2 Demo application for storage_on_flash library which is for STM32F4xx series.
hillkim7 0:e13134122925 3 */
hillkim7 0:e13134122925 4 #include <stdio.h>
hillkim7 0:e13134122925 5 #include <stdlib.h>
hillkim7 0:e13134122925 6
hillkim7 0:e13134122925 7 #include "SOFBlock.h"
hillkim7 0:e13134122925 8 #include <string.h>
hillkim7 0:e13134122925 9
hillkim7 0:e13134122925 10 #include "mbed.h"
hillkim7 0:e13134122925 11
hillkim7 0:e13134122925 12 Serial pc(SERIAL_TX, SERIAL_RX);
hillkim7 0:e13134122925 13
hillkim7 0:e13134122925 14 #define TEST_BUF_SIZE 256
hillkim7 0:e13134122925 15
hillkim7 0:e13134122925 16 static char test_buf[TEST_BUF_SIZE];
hillkim7 0:e13134122925 17
hillkim7 0:e13134122925 18 static char *get_input()
hillkim7 0:e13134122925 19 {
hillkim7 0:e13134122925 20 size_t i;
hillkim7 0:e13134122925 21 int c;
hillkim7 0:e13134122925 22
hillkim7 0:e13134122925 23 for (i = 0; i < TEST_BUF_SIZE-1; ++i)
hillkim7 0:e13134122925 24 {
hillkim7 0:e13134122925 25 c = pc.getc();
hillkim7 0:e13134122925 26 printf("%c", c); // echo
hillkim7 0:e13134122925 27 if (c == '\r' || c == '\n')
hillkim7 0:e13134122925 28 {
hillkim7 0:e13134122925 29 printf("\n");
hillkim7 0:e13134122925 30 break;
hillkim7 0:e13134122925 31 }
hillkim7 0:e13134122925 32 test_buf[i] = (char)c;
hillkim7 0:e13134122925 33 }
hillkim7 0:e13134122925 34
hillkim7 0:e13134122925 35 test_buf[i] = 0;
hillkim7 0:e13134122925 36
hillkim7 0:e13134122925 37
hillkim7 0:e13134122925 38 return test_buf;
hillkim7 0:e13134122925 39 }
hillkim7 0:e13134122925 40
hillkim7 0:e13134122925 41 static int geti()
hillkim7 0:e13134122925 42 {
hillkim7 0:e13134122925 43 return atoi(get_input());
hillkim7 0:e13134122925 44 }
hillkim7 0:e13134122925 45
hillkim7 0:e13134122925 46 static uint8_t ask_sector_no()
hillkim7 0:e13134122925 47 {
hillkim7 0:e13134122925 48 printf(" sector number? ");
hillkim7 0:e13134122925 49
hillkim7 0:e13134122925 50 return (uint8_t)geti();
hillkim7 0:e13134122925 51 }
hillkim7 0:e13134122925 52
hillkim7 0:e13134122925 53 static void show_spec(uint8_t sector_index)
hillkim7 0:e13134122925 54 {
hillkim7 0:e13134122925 55 const SOF_SectorSpec_t *spec = SOF_block_get_info(sector_index);
hillkim7 0:e13134122925 56
hillkim7 0:e13134122925 57 if (spec == NULL)
hillkim7 0:e13134122925 58 {
hillkim7 0:e13134122925 59 printf("SOF_block_get_info(%u) error\r\n", sector_index);
hillkim7 0:e13134122925 60 return;
hillkim7 0:e13134122925 61 }
hillkim7 0:e13134122925 62
hillkim7 0:e13134122925 63 printf("sec_no=%u\r\n", spec->sec_no);
hillkim7 0:e13134122925 64 printf("sec_addr=%#x\r\n", spec->sec_addr);
hillkim7 0:e13134122925 65 printf("sec_size=%u\r\n", spec->sec_size);
hillkim7 0:e13134122925 66 }
hillkim7 0:e13134122925 67
hillkim7 0:e13134122925 68 static void foramt_sector(uint8_t sector_index)
hillkim7 0:e13134122925 69 {
hillkim7 0:e13134122925 70 printf("format(%u)...\r\n", sector_index);
hillkim7 0:e13134122925 71 printf(" %s\r\n", SOFBlock::format(sector_index) ? "ok" : "fail");
hillkim7 0:e13134122925 72 }
hillkim7 0:e13134122925 73
hillkim7 0:e13134122925 74 static void write_data(uint8_t sector_index)
hillkim7 0:e13134122925 75 {
hillkim7 0:e13134122925 76 printf(" input? ");
hillkim7 0:e13134122925 77 char *s = get_input();
hillkim7 0:e13134122925 78
hillkim7 0:e13134122925 79 SOFWriter writer;
hillkim7 0:e13134122925 80
hillkim7 0:e13134122925 81 if (writer.open(sector_index) != kSOF_ErrNone)
hillkim7 0:e13134122925 82 {
hillkim7 0:e13134122925 83 printf("open fail\r\n");
hillkim7 0:e13134122925 84 return;
hillkim7 0:e13134122925 85 }
hillkim7 0:e13134122925 86
hillkim7 0:e13134122925 87 size_t len = strlen(s);
hillkim7 0:e13134122925 88 size_t r;
hillkim7 0:e13134122925 89 if ((r=writer.write_data((uint8_t*)s, len)) != len)
hillkim7 0:e13134122925 90 {
hillkim7 0:e13134122925 91 printf("write_data fail: %u/%u\r\n", r, len);
hillkim7 0:e13134122925 92 printf("No free area exists, format required\r\n");
hillkim7 0:e13134122925 93 return;
hillkim7 0:e13134122925 94 }
hillkim7 0:e13134122925 95
hillkim7 0:e13134122925 96 printf("write_data ok: %u bytes\r\n", r);
hillkim7 0:e13134122925 97 }
hillkim7 0:e13134122925 98
hillkim7 0:e13134122925 99 static void read_data(uint8_t sector_index)
hillkim7 0:e13134122925 100 {
hillkim7 0:e13134122925 101 SOFReader reader;
hillkim7 0:e13134122925 102
hillkim7 0:e13134122925 103 if (reader.open(sector_index) != kSOF_ErrNone)
hillkim7 0:e13134122925 104 {
hillkim7 0:e13134122925 105 printf("open fail\r\n");
hillkim7 0:e13134122925 106 return;
hillkim7 0:e13134122925 107 }
hillkim7 0:e13134122925 108
hillkim7 1:9e21adbf4f8e 109 printf("data %d bytes at %p :\r\n", reader.get_data_size(), reader.get_physical_data_addr());
hillkim7 1:9e21adbf4f8e 110 printf("%.*s", reader.get_data_size(), reader.get_physical_data_addr());
hillkim7 0:e13134122925 111 printf("\r\n");
hillkim7 0:e13134122925 112 }
hillkim7 0:e13134122925 113
hillkim7 0:e13134122925 114 static void show_statics(uint8_t sector_index)
hillkim7 0:e13134122925 115 {
hillkim7 0:e13134122925 116 SOF_Statics_t stats;
hillkim7 0:e13134122925 117
hillkim7 0:e13134122925 118 if (SOF_block_get_statics(sector_index, &stats) != kSOF_ErrNone)
hillkim7 0:e13134122925 119 {
hillkim7 0:e13134122925 120 printf("SOF_block_get_statics(%u) error\r\n", sector_index);
hillkim7 0:e13134122925 121 return;
hillkim7 0:e13134122925 122 }
hillkim7 0:e13134122925 123
hillkim7 0:e13134122925 124 printf("data_addr=%p\r\n", stats.data_addr);
hillkim7 0:e13134122925 125 printf("data_size=%u\r\n", stats.data_size);
hillkim7 0:e13134122925 126 printf("free_size=%u\r\n", stats.free_size);
hillkim7 0:e13134122925 127 }
hillkim7 0:e13134122925 128
hillkim7 0:e13134122925 129 static void demo()
hillkim7 0:e13134122925 130 {
hillkim7 0:e13134122925 131 while (true)
hillkim7 0:e13134122925 132 {
hillkim7 0:e13134122925 133 printf("\r\n* MENU *\r\n");
hillkim7 0:e13134122925 134 printf("1. see flash sector spec\r\n");
hillkim7 0:e13134122925 135 printf("2. format sector\r\n");
hillkim7 0:e13134122925 136 printf("3. write data to sector\r\n");
hillkim7 0:e13134122925 137 printf("4. read data from sector\r\n");
hillkim7 0:e13134122925 138 printf("5. see storage status\r\n");
hillkim7 0:e13134122925 139
hillkim7 0:e13134122925 140 printf(" select? ");
hillkim7 0:e13134122925 141 switch(geti())
hillkim7 0:e13134122925 142 {
hillkim7 0:e13134122925 143 case 1:
hillkim7 0:e13134122925 144 show_spec(ask_sector_no());
hillkim7 0:e13134122925 145 break;
hillkim7 0:e13134122925 146 case 2:
hillkim7 0:e13134122925 147 foramt_sector(ask_sector_no());
hillkim7 0:e13134122925 148 break;
hillkim7 0:e13134122925 149 case 3:
hillkim7 0:e13134122925 150 write_data(ask_sector_no());
hillkim7 0:e13134122925 151 break;
hillkim7 0:e13134122925 152 case 4:
hillkim7 0:e13134122925 153 read_data(ask_sector_no());
hillkim7 0:e13134122925 154 break;
hillkim7 0:e13134122925 155 case 5:
hillkim7 0:e13134122925 156 show_statics(ask_sector_no());
hillkim7 0:e13134122925 157 break;
hillkim7 0:e13134122925 158 default:
hillkim7 0:e13134122925 159 printf("invalid choice\r\n");
hillkim7 0:e13134122925 160 break;
hillkim7 0:e13134122925 161 }
hillkim7 0:e13134122925 162 }
hillkim7 0:e13134122925 163 }
hillkim7 0:e13134122925 164
hillkim7 0:e13134122925 165 int main()
hillkim7 0:e13134122925 166 {
hillkim7 0:e13134122925 167 demo();
hillkim7 0:e13134122925 168
hillkim7 0:e13134122925 169 return 0;
hillkim7 0:e13134122925 170 }
hillkim7 0:e13134122925 171
hillkim7 0:e13134122925 172