EEPROM emulation (STM algorithm described in the application notes: AN4061, AN3969, AN2594, AN3390, AN4056) with added multipage possibility. For Nucleo-F030 and others boards with similar microcontrolers.
Description in AN4061 from STM.
Changed (compared with the original code AN4061):
- possibility of a larger size of emulated EEPROM (using multiple Flash pages)
- dummy variables prevent overwrite code in Flash by algorithm of EEPROM emulation
Macro PAGE_NB_PVP (in eeprom.h) defines the size of the virtual page.
Eg. For F030 where Flash page are 1kB value 4 gives 4kB.
Size 4kB virtual page gives you the ability to use max. approx. 1k of 16-bit variables.
main.cpp@0:2eda09ff61d0, 2016-09-24 (annotated)
- Committer:
- mega64
- Date:
- Sat Sep 24 00:52:55 2016 +0000
- Revision:
- 0:2eda09ff61d0
Added possibility to increase size of virtual pages of emulation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mega64 | 0:2eda09ff61d0 | 1 | |
mega64 | 0:2eda09ff61d0 | 2 | #include "mbed.h" |
mega64 | 0:2eda09ff61d0 | 3 | #include "eeprom.h" |
mega64 | 0:2eda09ff61d0 | 4 | #include <ctype.h> |
mega64 | 0:2eda09ff61d0 | 5 | |
mega64 | 0:2eda09ff61d0 | 6 | |
mega64 | 0:2eda09ff61d0 | 7 | |
mega64 | 0:2eda09ff61d0 | 8 | uint16_t n = 0; |
mega64 | 0:2eda09ff61d0 | 9 | /* Virtual address defined by the user: 0xFFFF value is prohibited */ |
mega64 | 0:2eda09ff61d0 | 10 | uint16_t VirtAddVarTab[NB_OF_VAR] = {0x5555, 0x6666, 0x7777}; |
mega64 | 0:2eda09ff61d0 | 11 | uint16_t VarDataTab[] = {0, 0, 0}; |
mega64 | 0:2eda09ff61d0 | 12 | |
mega64 | 0:2eda09ff61d0 | 13 | |
mega64 | 0:2eda09ff61d0 | 14 | |
mega64 | 0:2eda09ff61d0 | 15 | int main() |
mega64 | 0:2eda09ff61d0 | 16 | { |
mega64 | 0:2eda09ff61d0 | 17 | |
mega64 | 0:2eda09ff61d0 | 18 | |
mega64 | 0:2eda09ff61d0 | 19 | |
mega64 | 0:2eda09ff61d0 | 20 | /* Unlock the Flash Program Erase controller */ |
mega64 | 0:2eda09ff61d0 | 21 | HAL_FLASH_Unlock(); |
mega64 | 0:2eda09ff61d0 | 22 | |
mega64 | 0:2eda09ff61d0 | 23 | EE_Init(); |
mega64 | 0:2eda09ff61d0 | 24 | |
mega64 | 0:2eda09ff61d0 | 25 | |
mega64 | 0:2eda09ff61d0 | 26 | printf("EEPROM emulation test\n\n"); |
mega64 | 0:2eda09ff61d0 | 27 | printf("\n\rType one of the following commands and press space or enter"); |
mega64 | 0:2eda09ff61d0 | 28 | printf("\n\r READ - read all variables\n\r WRITE1 <n> - write first variable\n\r WRITE2 <n> - write second variable\n\r WRITE3 <n> - write third variable"); |
mega64 | 0:2eda09ff61d0 | 29 | printf("\n\rwhere <n> is uint16_t value (0-65535)"); |
mega64 | 0:2eda09ff61d0 | 30 | printf("\n\r"); |
mega64 | 0:2eda09ff61d0 | 31 | |
mega64 | 0:2eda09ff61d0 | 32 | |
mega64 | 0:2eda09ff61d0 | 33 | char command[20]; |
mega64 | 0:2eda09ff61d0 | 34 | char arg[10]; |
mega64 | 0:2eda09ff61d0 | 35 | int i; |
mega64 | 0:2eda09ff61d0 | 36 | |
mega64 | 0:2eda09ff61d0 | 37 | while(1) { |
mega64 | 0:2eda09ff61d0 | 38 | scanf("%s", command); |
mega64 | 0:2eda09ff61d0 | 39 | i=0; |
mega64 | 0:2eda09ff61d0 | 40 | while(command[i]) |
mega64 | 0:2eda09ff61d0 | 41 | { |
mega64 | 0:2eda09ff61d0 | 42 | command[i]=toupper(command[i]); |
mega64 | 0:2eda09ff61d0 | 43 | i++; |
mega64 | 0:2eda09ff61d0 | 44 | } |
mega64 | 0:2eda09ff61d0 | 45 | |
mega64 | 0:2eda09ff61d0 | 46 | |
mega64 | 0:2eda09ff61d0 | 47 | if (strcmp("READ", command)==0) { |
mega64 | 0:2eda09ff61d0 | 48 | for (i=0; i<3; i++) { |
mega64 | 0:2eda09ff61d0 | 49 | EE_ReadVariable(VirtAddVarTab[i], &VarDataTab[i]); |
mega64 | 0:2eda09ff61d0 | 50 | }; |
mega64 | 0:2eda09ff61d0 | 51 | printf("\nvar1=%6u, var2=%6u, var3=%6u",VarDataTab[0],VarDataTab[1],VarDataTab[2]); |
mega64 | 0:2eda09ff61d0 | 52 | } |
mega64 | 0:2eda09ff61d0 | 53 | /////////////////////////////////////////////////////////////////////////////////////////////////////// |
mega64 | 0:2eda09ff61d0 | 54 | else if (strcmp("WRITE1", command)==0) { |
mega64 | 0:2eda09ff61d0 | 55 | scanf("%s", arg); |
mega64 | 0:2eda09ff61d0 | 56 | printf("\nok"); |
mega64 | 0:2eda09ff61d0 | 57 | n=atoi(arg); |
mega64 | 0:2eda09ff61d0 | 58 | EE_WriteVariable(VirtAddVarTab[0], n); |
mega64 | 0:2eda09ff61d0 | 59 | } |
mega64 | 0:2eda09ff61d0 | 60 | //////////////////////////////////////////////////////////////////////////////////// |
mega64 | 0:2eda09ff61d0 | 61 | else if (strcmp("WRITE2", command)==0) { |
mega64 | 0:2eda09ff61d0 | 62 | scanf("%s", arg); |
mega64 | 0:2eda09ff61d0 | 63 | printf("\nok"); |
mega64 | 0:2eda09ff61d0 | 64 | n=atoi(arg); |
mega64 | 0:2eda09ff61d0 | 65 | EE_WriteVariable(VirtAddVarTab[1], n); |
mega64 | 0:2eda09ff61d0 | 66 | } |
mega64 | 0:2eda09ff61d0 | 67 | //////////////////////////////////////////////////////////////////////////////////// |
mega64 | 0:2eda09ff61d0 | 68 | else if (strcmp("WRITE3", command)==0) { |
mega64 | 0:2eda09ff61d0 | 69 | scanf("%s", arg); |
mega64 | 0:2eda09ff61d0 | 70 | printf("\nok"); |
mega64 | 0:2eda09ff61d0 | 71 | n=atoi(arg); |
mega64 | 0:2eda09ff61d0 | 72 | EE_WriteVariable(VirtAddVarTab[2], n); |
mega64 | 0:2eda09ff61d0 | 73 | } |
mega64 | 0:2eda09ff61d0 | 74 | //////////////////////////////////////////////////////////////////////////////////// |
mega64 | 0:2eda09ff61d0 | 75 | else if (strcmp("T1", command)==0) { |
mega64 | 0:2eda09ff61d0 | 76 | // multiple write test. After test var1 = 1000 |
mega64 | 0:2eda09ff61d0 | 77 | printf("\nok"); |
mega64 | 0:2eda09ff61d0 | 78 | for (n=0;n<1001;n++) |
mega64 | 0:2eda09ff61d0 | 79 | { |
mega64 | 0:2eda09ff61d0 | 80 | EE_WriteVariable(VirtAddVarTab[0], n); |
mega64 | 0:2eda09ff61d0 | 81 | }; |
mega64 | 0:2eda09ff61d0 | 82 | } |
mega64 | 0:2eda09ff61d0 | 83 | //////////////////////////////////////////////////////////////////////////////////// |
mega64 | 0:2eda09ff61d0 | 84 | else if (strcmp("T2", command)==0) { |
mega64 | 0:2eda09ff61d0 | 85 | // multiple write test. After test var2 = 1001 |
mega64 | 0:2eda09ff61d0 | 86 | printf("\nok"); |
mega64 | 0:2eda09ff61d0 | 87 | for (n=0;n<1002;n++) |
mega64 | 0:2eda09ff61d0 | 88 | { |
mega64 | 0:2eda09ff61d0 | 89 | EE_WriteVariable(VirtAddVarTab[1], n); |
mega64 | 0:2eda09ff61d0 | 90 | }; |
mega64 | 0:2eda09ff61d0 | 91 | } |
mega64 | 0:2eda09ff61d0 | 92 | //////////////////////////////////////////////////////////////////////////////////// |
mega64 | 0:2eda09ff61d0 | 93 | else if (strcmp("T3", command)==0) { |
mega64 | 0:2eda09ff61d0 | 94 | // multiple write test. After test var3 = 1002 |
mega64 | 0:2eda09ff61d0 | 95 | printf("\nok"); |
mega64 | 0:2eda09ff61d0 | 96 | for (n=0;n<1003;n++) |
mega64 | 0:2eda09ff61d0 | 97 | { |
mega64 | 0:2eda09ff61d0 | 98 | EE_WriteVariable(VirtAddVarTab[2], n); |
mega64 | 0:2eda09ff61d0 | 99 | }; |
mega64 | 0:2eda09ff61d0 | 100 | } |
mega64 | 0:2eda09ff61d0 | 101 | //////////////////////////////////////////////////////////////////////////////////// |
mega64 | 0:2eda09ff61d0 | 102 | |
mega64 | 0:2eda09ff61d0 | 103 | else perror("\nThere is no command matching. Try again"); |
mega64 | 0:2eda09ff61d0 | 104 | |
mega64 | 0:2eda09ff61d0 | 105 | command[0] = arg[0] = 0; |
mega64 | 0:2eda09ff61d0 | 106 | printf("\n\r"); |
mega64 | 0:2eda09ff61d0 | 107 | } |
mega64 | 0:2eda09ff61d0 | 108 | |
mega64 | 0:2eda09ff61d0 | 109 | |
mega64 | 0:2eda09ff61d0 | 110 | } |