123

Dependencies:   mbed

Fork of LG by igor Apu

Committer:
Diletant
Date:
Mon May 09 20:03:26 2016 +0000
Revision:
156:e68ee0bcdcda
Parent:
149:abbf7663d27d
Child:
167:bedc0a9d559a
Device & ... update. Not final!!!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Diletant 149:abbf7663d27d 1 #include "Device.h"
Diletant 149:abbf7663d27d 2 #include <stdio.h>
Diletant 149:abbf7663d27d 3
Diletant 149:abbf7663d27d 4 extern Device device;
Diletant 149:abbf7663d27d 5 extern HashParam hashParamTable[HASH_PARAM_COUNT];
Diletant 149:abbf7663d27d 6
Diletant 149:abbf7663d27d 7 //Flash memory sectors
Diletant 149:abbf7663d27d 8 #define FLASH_SECTOR0_SIZE 0x1000
Diletant 149:abbf7663d27d 9 #define FLASH_SECTOR0_START 0x00000000
Diletant 149:abbf7663d27d 10 #define FLASH_SECTOR0_END 0x00000FFF
Diletant 149:abbf7663d27d 11
Diletant 149:abbf7663d27d 12 #define FLASH_SECTOR1_SIZE 0x1000
Diletant 149:abbf7663d27d 13 #define FLASH_SECTOR1_START 0X00001000
Diletant 149:abbf7663d27d 14 #define FLASH_SECTOR1_END 0X00001FFF
Diletant 149:abbf7663d27d 15
Diletant 149:abbf7663d27d 16 #define FLASH_SECTOR2_SIZE 0x1000
Diletant 149:abbf7663d27d 17 #define FLASH_SECTOR2_START 0X00002000
Diletant 149:abbf7663d27d 18 #define FLASH_SECTOR2_END 0X00002FFF
Diletant 149:abbf7663d27d 19
Diletant 149:abbf7663d27d 20 #define FLASH_SECTOR3_SIZE 0x1000
Diletant 149:abbf7663d27d 21 #define FLASH_SECTOR3_START 0X00003000
Diletant 149:abbf7663d27d 22 #define FLASH_SECTOR3_END 0X00003FFF
Diletant 149:abbf7663d27d 23
Diletant 149:abbf7663d27d 24 #define FLASH_SECTOR4_SIZE 0x1000
Diletant 149:abbf7663d27d 25 #define FLASH_SECTOR4_START 0X00004000
Diletant 149:abbf7663d27d 26 #define FLASH_SECTOR4_END 0X00004FFF
Diletant 149:abbf7663d27d 27
Diletant 149:abbf7663d27d 28 #define FLASH_SECTOR5_SIZE 0x1000
Diletant 149:abbf7663d27d 29 #define FLASH_SECTOR5_START 0X00005000
Diletant 149:abbf7663d27d 30 #define FLASH_SECTOR5_END 0X00005FFF
Diletant 149:abbf7663d27d 31
Diletant 149:abbf7663d27d 32 #define FLASH_SECTOR6_SIZE 0x1000
Diletant 149:abbf7663d27d 33 #define FLASH_SECTOR6_START 0X00006000
Diletant 149:abbf7663d27d 34 #define FLASH_SECTOR6_END 0X00006FFF
Diletant 149:abbf7663d27d 35
Diletant 149:abbf7663d27d 36 #define FLASH_SECTOR7_SIZE 0x1000
Diletant 149:abbf7663d27d 37 #define FLASH_SECTOR7_START 0X00007000
Diletant 149:abbf7663d27d 38 #define FLASH_SECTOR7_END 0X00007FFF
Diletant 149:abbf7663d27d 39
Diletant 149:abbf7663d27d 40 #define FLASH_SECTOR8_SIZE 0x1000
Diletant 149:abbf7663d27d 41 #define FLASH_SECTOR8_START 0x00008000
Diletant 149:abbf7663d27d 42 #define FLASH_SECTOR8_END 0X00008FFF
Diletant 149:abbf7663d27d 43
Diletant 149:abbf7663d27d 44 #define FLASH_SECTOR9_SIZE 0x1000
Diletant 149:abbf7663d27d 45 #define FLASH_SECTOR9_START 0x00009000
Diletant 149:abbf7663d27d 46 #define FLASH_SECTOR9_END 0X00009FFF
Diletant 149:abbf7663d27d 47
Diletant 149:abbf7663d27d 48 #define FLASH_SECTOR10_SIZE 0x1000
Diletant 149:abbf7663d27d 49 #define FLASH_SECTOR10_START 0x0000A000
Diletant 149:abbf7663d27d 50 #define FLASH_SECTOR10_END 0X0000AFFF
Diletant 149:abbf7663d27d 51
Diletant 149:abbf7663d27d 52 #define FLASH_SECTOR11_SIZE 0x1000
Diletant 149:abbf7663d27d 53 #define FLASH_SECTOR11_START 0x0000B000
Diletant 149:abbf7663d27d 54 #define FLASH_SECTOR11_END 0X0000BFFF
Diletant 149:abbf7663d27d 55
Diletant 149:abbf7663d27d 56 #define FLASH_SECTOR12_SIZE 0x1000
Diletant 149:abbf7663d27d 57 #define FLASH_SECTOR12_START 0x0000C000
Diletant 149:abbf7663d27d 58 #define FLASH_SECTOR12_END 0X0000CFFF
Diletant 149:abbf7663d27d 59
Diletant 149:abbf7663d27d 60 #define FLASH_SECTOR13_SIZE 0x1000
Diletant 149:abbf7663d27d 61 #define FLASH_SECTOR13_START 0x0000D000
Diletant 149:abbf7663d27d 62 #define FLASH_SECTOR13_END 0X0000DFFF
Diletant 149:abbf7663d27d 63
Diletant 149:abbf7663d27d 64 #define FLASH_SECTOR14_SIZE 0x1000
Diletant 149:abbf7663d27d 65 #define FLASH_SECTOR14_START 0x0000E000
Diletant 149:abbf7663d27d 66 #define FLASH_SECTOR14_END 0X0000EFFF
Diletant 149:abbf7663d27d 67
Diletant 149:abbf7663d27d 68 #define FLASH_SECTOR15_SIZE 0x1000
Diletant 149:abbf7663d27d 69 #define FLASH_SECTOR15_START 0x0000F000
Diletant 149:abbf7663d27d 70 #define FLASH_SECTOR15_END 0X0000FFFF
Diletant 149:abbf7663d27d 71
Diletant 149:abbf7663d27d 72 #define FLASH_SECTOR16_SIZE 0x8000
Diletant 149:abbf7663d27d 73 #define FLASH_SECTOR16_START 0x00010000
Diletant 149:abbf7663d27d 74 #define FLASH_SECTOR16_END 0x00017FFF
Diletant 149:abbf7663d27d 75
Diletant 149:abbf7663d27d 76 #define FLASH_SECTOR17_SIZE 0x8000
Diletant 149:abbf7663d27d 77 #define FLASH_SECTOR17_START 0x00018000
Diletant 149:abbf7663d27d 78 #define FLASH_SECTOR17_END 0x0001FFFF
Diletant 149:abbf7663d27d 79
Diletant 149:abbf7663d27d 80 #define FLASH_SECTOR18_SIZE 0x8000
Diletant 149:abbf7663d27d 81 #define FLASH_SECTOR18_START 0x00020000
Diletant 149:abbf7663d27d 82 #define FLASH_SECTOR18_END 0x00027FFF
Diletant 149:abbf7663d27d 83
Diletant 149:abbf7663d27d 84 #define FLASH_SECTOR19_SIZE 0x8000
Diletant 149:abbf7663d27d 85 #define FLASH_SECTOR19_START 0x00028000
Diletant 149:abbf7663d27d 86 #define FLASH_SECTOR19_END 0x0002FFFF
Diletant 149:abbf7663d27d 87
Diletant 149:abbf7663d27d 88 #define FLASH_SECTOR20_SIZE 0x8000
Diletant 149:abbf7663d27d 89 #define FLASH_SECTOR20_START 0x00030000
Diletant 149:abbf7663d27d 90 #define FLASH_SECTOR20_END 0x00037FFF
Diletant 149:abbf7663d27d 91
Diletant 149:abbf7663d27d 92 #define FLASH_SECTOR21_SIZE 0x8000
Diletant 149:abbf7663d27d 93 #define FLASH_SECTOR21_START 0x00038000
Diletant 149:abbf7663d27d 94 #define FLASH_SECTOR21_END 0x0003FFFF
Diletant 149:abbf7663d27d 95
Diletant 149:abbf7663d27d 96 #define FLASH_SECTOR22_SIZE 0x8000
Diletant 149:abbf7663d27d 97 #define FLASH_SECTOR22_START 0x00040000
Diletant 149:abbf7663d27d 98 #define FLASH_SECTOR22_END 0x00047FFF
Diletant 149:abbf7663d27d 99
Diletant 149:abbf7663d27d 100 #define FLASH_SECTOR23_SIZE 0x8000
Diletant 149:abbf7663d27d 101 #define FLASH_SECTOR23_START 0x00048000
Diletant 149:abbf7663d27d 102 #define FLASH_SECTOR23_END 0x0004FFFF
Diletant 149:abbf7663d27d 103
Diletant 149:abbf7663d27d 104 #define FLASH_SECTOR24_SIZE 0x8000
Diletant 149:abbf7663d27d 105 #define FLASH_SECTOR24_START 0x00050000
Diletant 149:abbf7663d27d 106 #define FLASH_SECTOR24_END 0x00057FFF
Diletant 149:abbf7663d27d 107
Diletant 149:abbf7663d27d 108 #define FLASH_SECTOR25_SIZE 0x8000
Diletant 149:abbf7663d27d 109 #define FLASH_SECTOR25_START 0x00058000
Diletant 149:abbf7663d27d 110 #define FLASH_SECTOR25_END 0x0005FFFF
Diletant 149:abbf7663d27d 111
Diletant 149:abbf7663d27d 112 #define FLASH_SECTOR26_SIZE 0x8000
Diletant 149:abbf7663d27d 113 #define FLASH_SECTOR26_START 0x00060000
Diletant 149:abbf7663d27d 114 #define FLASH_SECTOR26_END 0x00067FFF
Diletant 149:abbf7663d27d 115
Diletant 149:abbf7663d27d 116 #define FLASH_SECTOR27_SIZE 0x8000
Diletant 149:abbf7663d27d 117 #define FLASH_SECTOR27_START 0x00068000
Diletant 149:abbf7663d27d 118 #define FLASH_SECTOR27_END 0x0006FFFF
Diletant 149:abbf7663d27d 119
Diletant 149:abbf7663d27d 120 #define FLASH_SECTOR28_SIZE 0x8000
Diletant 149:abbf7663d27d 121 #define FLASH_SECTOR28_START 0x00070000
Diletant 149:abbf7663d27d 122 #define FLASH_SECTOR28_END 0x00077FFF
Diletant 149:abbf7663d27d 123
Diletant 149:abbf7663d27d 124 #define FLASH_SECTOR29_SIZE 0x8000
Diletant 149:abbf7663d27d 125 #define FLASH_SECTOR29_START 0x00078000
Diletant 149:abbf7663d27d 126 #define FLASH_SECTOR29_END 0x0007FFFF
Diletant 149:abbf7663d27d 127
Diletant 149:abbf7663d27d 128 void InitFlashWithDefaults(void){
Diletant 156:e68ee0bcdcda 129 device.controller.flash.settings.hashSector = 22;
Diletant 156:e68ee0bcdcda 130 //device.controller.flash.settings.hashPageLength = 256;
Diletant 149:abbf7663d27d 131
Diletant 156:e68ee0bcdcda 132 device.controller.flash.settings.dataSector = 23;
Diletant 156:e68ee0bcdcda 133 //device.controller.flash.settings.dataPageLength = 256;
Diletant 149:abbf7663d27d 134 }
Diletant 149:abbf7663d27d 135
Diletant 149:abbf7663d27d 136 void InitFlash(void){
Diletant 149:abbf7663d27d 137 }
Diletant 149:abbf7663d27d 138
Diletant 149:abbf7663d27d 139 uint32_t AddressFromSector(uint8_t sector){
Diletant 149:abbf7663d27d 140 switch (sector){
Diletant 149:abbf7663d27d 141 case 0: return FLASH_SECTOR0_START;
Diletant 149:abbf7663d27d 142 case 1: return FLASH_SECTOR1_START;
Diletant 149:abbf7663d27d 143 case 2: return FLASH_SECTOR2_START;
Diletant 149:abbf7663d27d 144 case 3: return FLASH_SECTOR3_START;
Diletant 149:abbf7663d27d 145 case 4: return FLASH_SECTOR4_START;
Diletant 149:abbf7663d27d 146 case 5: return FLASH_SECTOR5_START;
Diletant 149:abbf7663d27d 147 case 6: return FLASH_SECTOR6_START;
Diletant 149:abbf7663d27d 148 case 7: return FLASH_SECTOR7_START;
Diletant 149:abbf7663d27d 149 case 8: return FLASH_SECTOR8_START;
Diletant 149:abbf7663d27d 150 case 9: return FLASH_SECTOR9_START;
Diletant 149:abbf7663d27d 151 case 10: return FLASH_SECTOR10_START;
Diletant 149:abbf7663d27d 152 case 11: return FLASH_SECTOR11_START;
Diletant 149:abbf7663d27d 153 case 12: return FLASH_SECTOR12_START;
Diletant 149:abbf7663d27d 154 case 13: return FLASH_SECTOR13_START;
Diletant 149:abbf7663d27d 155 case 14: return FLASH_SECTOR14_START;
Diletant 149:abbf7663d27d 156 case 15: return FLASH_SECTOR15_START;
Diletant 149:abbf7663d27d 157 case 16: return FLASH_SECTOR16_START;
Diletant 149:abbf7663d27d 158 case 17: return FLASH_SECTOR17_START;
Diletant 149:abbf7663d27d 159 case 18: return FLASH_SECTOR18_START;
Diletant 149:abbf7663d27d 160 case 19: return FLASH_SECTOR19_START;
Diletant 149:abbf7663d27d 161 case 20: return FLASH_SECTOR20_START;
Diletant 149:abbf7663d27d 162 case 21: return FLASH_SECTOR21_START;
Diletant 149:abbf7663d27d 163 case 22: return FLASH_SECTOR22_START;
Diletant 149:abbf7663d27d 164 case 23: return FLASH_SECTOR23_START;
Diletant 149:abbf7663d27d 165 case 24: return FLASH_SECTOR24_START;
Diletant 149:abbf7663d27d 166 case 25: return FLASH_SECTOR25_START;
Diletant 149:abbf7663d27d 167 case 26: return FLASH_SECTOR26_START;
Diletant 149:abbf7663d27d 168 case 27: return FLASH_SECTOR27_START;
Diletant 149:abbf7663d27d 169 case 28: return FLASH_SECTOR28_START;
Diletant 149:abbf7663d27d 170 case 29: return FLASH_SECTOR29_START;
Diletant 149:abbf7663d27d 171 default: return 0;
Diletant 149:abbf7663d27d 172 }
Diletant 149:abbf7663d27d 173 }
Diletant 149:abbf7663d27d 174
Diletant 149:abbf7663d27d 175 uint16_t LengthFromSector(uint8_t sector){
Diletant 149:abbf7663d27d 176 switch (sector){
Diletant 149:abbf7663d27d 177 case 0: return FLASH_SECTOR0_SIZE;
Diletant 149:abbf7663d27d 178 case 1: return FLASH_SECTOR1_SIZE;
Diletant 149:abbf7663d27d 179 case 2: return FLASH_SECTOR2_SIZE;
Diletant 149:abbf7663d27d 180 case 3: return FLASH_SECTOR3_SIZE;
Diletant 149:abbf7663d27d 181 case 4: return FLASH_SECTOR4_SIZE;
Diletant 149:abbf7663d27d 182 case 5: return FLASH_SECTOR5_SIZE;
Diletant 149:abbf7663d27d 183 case 6: return FLASH_SECTOR6_SIZE;
Diletant 149:abbf7663d27d 184 case 7: return FLASH_SECTOR7_SIZE;
Diletant 149:abbf7663d27d 185 case 8: return FLASH_SECTOR8_SIZE;
Diletant 149:abbf7663d27d 186 case 9: return FLASH_SECTOR9_SIZE;
Diletant 149:abbf7663d27d 187 case 10: return FLASH_SECTOR10_SIZE;
Diletant 149:abbf7663d27d 188 case 11: return FLASH_SECTOR11_SIZE;
Diletant 149:abbf7663d27d 189 case 12: return FLASH_SECTOR12_SIZE;
Diletant 149:abbf7663d27d 190 case 13: return FLASH_SECTOR13_SIZE;
Diletant 149:abbf7663d27d 191 case 14: return FLASH_SECTOR14_SIZE;
Diletant 149:abbf7663d27d 192 case 15: return FLASH_SECTOR15_SIZE;
Diletant 149:abbf7663d27d 193 case 16: return FLASH_SECTOR16_SIZE;
Diletant 149:abbf7663d27d 194 case 17: return FLASH_SECTOR17_SIZE;
Diletant 149:abbf7663d27d 195 case 18: return FLASH_SECTOR18_SIZE;
Diletant 149:abbf7663d27d 196 case 19: return FLASH_SECTOR19_SIZE;
Diletant 149:abbf7663d27d 197 case 20: return FLASH_SECTOR20_SIZE;
Diletant 149:abbf7663d27d 198 case 21: return FLASH_SECTOR21_SIZE;
Diletant 149:abbf7663d27d 199 case 22: return FLASH_SECTOR22_SIZE;
Diletant 149:abbf7663d27d 200 case 23: return FLASH_SECTOR23_SIZE;
Diletant 149:abbf7663d27d 201 case 24: return FLASH_SECTOR24_SIZE;
Diletant 149:abbf7663d27d 202 case 25: return FLASH_SECTOR25_SIZE;
Diletant 149:abbf7663d27d 203 case 26: return FLASH_SECTOR26_SIZE;
Diletant 149:abbf7663d27d 204 case 27: return FLASH_SECTOR27_SIZE;
Diletant 149:abbf7663d27d 205 case 28: return FLASH_SECTOR28_SIZE;
Diletant 149:abbf7663d27d 206 case 29: return FLASH_SECTOR29_SIZE;
Diletant 149:abbf7663d27d 207 default: return 0;
Diletant 149:abbf7663d27d 208 }
Diletant 149:abbf7663d27d 209 }
Diletant 149:abbf7663d27d 210 /*
Diletant 149:abbf7663d27d 211 uint8_t sectorFromAddress(uint32_t address){
Diletant 149:abbf7663d27d 212 if (address >= FLASH_SECTOR29_START) return 29;
Diletant 149:abbf7663d27d 213 else if (address >= FLASH_SECTOR28_START) return 28;
Diletant 149:abbf7663d27d 214 else if (address >= FLASH_SECTOR27_START) return 27;
Diletant 149:abbf7663d27d 215 else if (address >= FLASH_SECTOR26_START) return 26;
Diletant 149:abbf7663d27d 216 else if (address >= FLASH_SECTOR25_START) return 25;
Diletant 149:abbf7663d27d 217 else if (address >= FLASH_SECTOR24_START) return 24;
Diletant 149:abbf7663d27d 218 else if (address >= FLASH_SECTOR23_START) return 23;
Diletant 149:abbf7663d27d 219 else if (address >= FLASH_SECTOR22_START) return 22;
Diletant 149:abbf7663d27d 220 else if (address >= FLASH_SECTOR21_START) return 21;
Diletant 149:abbf7663d27d 221 else if (address >= FLASH_SECTOR20_START) return 20;
Diletant 149:abbf7663d27d 222 else if (address >= FLASH_SECTOR19_START) return 19;
Diletant 149:abbf7663d27d 223 else if (address >= FLASH_SECTOR18_START) return 18;
Diletant 149:abbf7663d27d 224 else if (address >= FLASH_SECTOR17_START) return 17;
Diletant 149:abbf7663d27d 225 else if (address >= FLASH_SECTOR16_START) return 16;
Diletant 149:abbf7663d27d 226 else if (address >= FLASH_SECTOR15_START) return 15;
Diletant 149:abbf7663d27d 227 else if (address >= FLASH_SECTOR14_START) return 14;
Diletant 149:abbf7663d27d 228 else if (address >= FLASH_SECTOR13_START) return 13;
Diletant 149:abbf7663d27d 229 else if (address >= FLASH_SECTOR12_START) return 12;
Diletant 149:abbf7663d27d 230 else if (address >= FLASH_SECTOR11_START) return 11;
Diletant 149:abbf7663d27d 231 else if (address >= FLASH_SECTOR10_START) return 10;
Diletant 149:abbf7663d27d 232 else if (address >= FLASH_SECTOR9_START) return 9;
Diletant 149:abbf7663d27d 233 else if (address >= FLASH_SECTOR8_START) return 8;
Diletant 149:abbf7663d27d 234 else if (address >= FLASH_SECTOR7_START) return 7;
Diletant 149:abbf7663d27d 235 else if (address >= FLASH_SECTOR6_START) return 6;
Diletant 149:abbf7663d27d 236 else if (address >= FLASH_SECTOR5_START) return 5;
Diletant 149:abbf7663d27d 237 else if (address >= FLASH_SECTOR4_START) return 4;
Diletant 149:abbf7663d27d 238 else if (address >= FLASH_SECTOR3_START) return 3;
Diletant 149:abbf7663d27d 239 else if (address >= FLASH_SECTOR2_START) return 2;
Diletant 149:abbf7663d27d 240 else if (address >= FLASH_SECTOR1_START) return 1;
Diletant 149:abbf7663d27d 241 else return 0;
Diletant 149:abbf7663d27d 242 }
Diletant 149:abbf7663d27d 243 */
Diletant 149:abbf7663d27d 244
Diletant 149:abbf7663d27d 245 DeviceFlashHash * FlashFindByHash(uint32_t hash){
Diletant 156:e68ee0bcdcda 246 uint16_t sectorLength = LengthFromSector(device.controller.flash.settings.hashSector);
Diletant 149:abbf7663d27d 247 uint16_t hashCapacity = sectorLength / sizeof(DeviceFlashHash);
Diletant 156:e68ee0bcdcda 248 uint32_t sectorAddress = AddressFromSector(device.controller.flash.settings.hashSector);
Diletant 149:abbf7663d27d 249 DeviceFlashHash * item = (DeviceFlashHash *)sectorAddress;
Diletant 149:abbf7663d27d 250 for (uint16_t i = 0; i < hashCapacity; i++){
Diletant 149:abbf7663d27d 251 if (item->hash == 0) break;
Diletant 149:abbf7663d27d 252 if (item->hash == hash) return item;
Diletant 149:abbf7663d27d 253 item++;
Diletant 149:abbf7663d27d 254 }
Diletant 149:abbf7663d27d 255 return 0;
Diletant 149:abbf7663d27d 256 }
Diletant 149:abbf7663d27d 257
Diletant 156:e68ee0bcdcda 258 void DeviceFlashReadAll(void){
Diletant 149:abbf7663d27d 259 uint16_t count = 0;
Diletant 156:e68ee0bcdcda 260 uint16_t sectorLength = LengthFromSector(device.controller.flash.settings.hashSector);
Diletant 149:abbf7663d27d 261 uint16_t hashCapacity = sectorLength / sizeof(DeviceFlashHash);
Diletant 156:e68ee0bcdcda 262 uint32_t sectorAddress = AddressFromSector(device.controller.flash.settings.hashSector);
Diletant 149:abbf7663d27d 263 DeviceFlashHash * item = (DeviceFlashHash *)sectorAddress;
Diletant 149:abbf7663d27d 264 for (uint16_t i = 0; i < hashCapacity; i++){
Diletant 149:abbf7663d27d 265 if (item->hash == 0) break; //Cleared
Diletant 149:abbf7663d27d 266 if (item->hash == 0xffffffff) break; //Erased
Diletant 149:abbf7663d27d 267 for (uint32_t j = 0; j < HASH_PARAM_COUNT; j++){
Diletant 149:abbf7663d27d 268 if (item->hash == hashParamTable[j].hash){
Diletant 149:abbf7663d27d 269 //if (item->size == hashParamTable[j].size){
Diletant 149:abbf7663d27d 270 uint8_t * src = (uint8_t *)item->address;
Diletant 149:abbf7663d27d 271 uint8_t * dst = hashParamTable[j].ref;
Diletant 149:abbf7663d27d 272 for (uint16_t k = 0; k < hashParamTable[j].size; k++){
Diletant 149:abbf7663d27d 273 *dst = *src; src++; dst++;
Diletant 149:abbf7663d27d 274 }
Diletant 149:abbf7663d27d 275 //}
Diletant 149:abbf7663d27d 276 }
Diletant 149:abbf7663d27d 277 }
Diletant 149:abbf7663d27d 278 item++;
Diletant 149:abbf7663d27d 279 count++;
Diletant 149:abbf7663d27d 280 }
Diletant 156:e68ee0bcdcda 281 sprintf(device.service.buffer,"FlashReadAll(): %d\r\n",count); WriteConcole();
Diletant 149:abbf7663d27d 282 }
Diletant 149:abbf7663d27d 283
Diletant 149:abbf7663d27d 284 void FlashOperationWritePage(DeviceFlashWriteOperation * op){
Diletant 149:abbf7663d27d 285 uint32_t res;
Diletant 149:abbf7663d27d 286 if (op->sectorPosition == 0){
Diletant 149:abbf7663d27d 287 //Prepare new sector for write operation
Diletant 149:abbf7663d27d 288 res = u32IAP_PrepareSectors(op->sector, op->sector);
Diletant 156:e68ee0bcdcda 289 sprintf(device.service.buffer,"PrepareSectors(): %02d %08x\r\n",op->sector, res); WriteConcole();
Diletant 149:abbf7663d27d 290 //Erase new sector
Diletant 149:abbf7663d27d 291 res = u32IAP_EraseSectors(op->sector, op->sector);
Diletant 156:e68ee0bcdcda 292 sprintf(device.service.buffer,"EraseSectors(): %02d %08x\r\n",op->sector, res); WriteConcole();
Diletant 149:abbf7663d27d 293 }
Diletant 149:abbf7663d27d 294 if (op->bytes < op->maxBytes){
Diletant 149:abbf7663d27d 295 //Prepare sector for write operation
Diletant 149:abbf7663d27d 296 res = u32IAP_PrepareSectors(op->sector, op->sector);
Diletant 156:e68ee0bcdcda 297 sprintf(device.service.buffer,"PrepareSectors(): %02d %08x\r\n",op->sector, res); WriteConcole();
Diletant 149:abbf7663d27d 298 //Copy page buffer to flash
Diletant 149:abbf7663d27d 299 res = u32IAP_CopyRAMToFlash(op->sectorAddress + op->sectorPosition, (uint32_t)&op->pageBuffer[0], FLASH_PAGE_SIZE);
Diletant 156:e68ee0bcdcda 300 sprintf(device.service.buffer,"CopyRAMToFlash(): %8x %08x\r\n", op->sectorAddress + op->sectorPosition, res); WriteConcole();
Diletant 149:abbf7663d27d 301 //Update bytes count
Diletant 149:abbf7663d27d 302 op->bytes += FLASH_PAGE_SIZE;
Diletant 149:abbf7663d27d 303 //Update sector position
Diletant 149:abbf7663d27d 304 op->sectorPosition += FLASH_PAGE_SIZE;
Diletant 149:abbf7663d27d 305 if (op->sectorPosition == op->sectorLength){
Diletant 149:abbf7663d27d 306 //Update sector address
Diletant 149:abbf7663d27d 307 op->sectorAddress += op->sectorLength;
Diletant 149:abbf7663d27d 308 //Clear sector position
Diletant 149:abbf7663d27d 309 op->sectorPosition = 0;
Diletant 149:abbf7663d27d 310 //Update sector
Diletant 149:abbf7663d27d 311 op->sector += 1;
Diletant 149:abbf7663d27d 312 }
Diletant 149:abbf7663d27d 313 //Update page position
Diletant 149:abbf7663d27d 314 op->pagePosition = 0;
Diletant 149:abbf7663d27d 315 }
Diletant 149:abbf7663d27d 316 }
Diletant 149:abbf7663d27d 317
Diletant 149:abbf7663d27d 318 void FlashOperationAppend(DeviceFlashWriteOperation * op, void * src, uint16_t count){
Diletant 149:abbf7663d27d 319 uint8_t * ptr = (uint8_t*) src;
Diletant 149:abbf7663d27d 320 for (uint16_t i = 0; i < count; i++){
Diletant 149:abbf7663d27d 321 op->pageBuffer[op->pagePosition] = *ptr;
Diletant 149:abbf7663d27d 322 op->pagePosition++;
Diletant 149:abbf7663d27d 323 ptr++;
Diletant 149:abbf7663d27d 324 //Write page buffer
Diletant 149:abbf7663d27d 325 if (op->pagePosition == FLASH_PAGE_SIZE){
Diletant 149:abbf7663d27d 326 FlashOperationWritePage(op);
Diletant 149:abbf7663d27d 327 }
Diletant 149:abbf7663d27d 328 }
Diletant 149:abbf7663d27d 329 }
Diletant 149:abbf7663d27d 330
Diletant 149:abbf7663d27d 331 void FlashOperationComplete(DeviceFlashWriteOperation * op){
Diletant 149:abbf7663d27d 332 for (uint16_t i = op->pagePosition; i < FLASH_PAGE_SIZE; i++){
Diletant 149:abbf7663d27d 333 op->pageBuffer[op->pagePosition] = 0xff;
Diletant 149:abbf7663d27d 334 op->pagePosition++;
Diletant 149:abbf7663d27d 335 }
Diletant 149:abbf7663d27d 336 FlashOperationWritePage(op);
Diletant 149:abbf7663d27d 337 }
Diletant 149:abbf7663d27d 338
Diletant 156:e68ee0bcdcda 339 void DeviceFlashWriteAll(void){
Diletant 149:abbf7663d27d 340 //#pragma pack(push) /* push current alignment to stack */
Diletant 149:abbf7663d27d 341 //#pragma pack(4) /* set alignment to 4 byte boundary */
Diletant 149:abbf7663d27d 342 DeviceFlashWriteOperation hash;
Diletant 149:abbf7663d27d 343 //#pragma pack(pop) /* restore original alignment from stack */
Diletant 156:e68ee0bcdcda 344 hash.sector = device.controller.flash.settings.hashSector;
Diletant 149:abbf7663d27d 345 hash.sectorAddress = AddressFromSector(hash.sector);
Diletant 149:abbf7663d27d 346 hash.sectorLength = LengthFromSector(hash.sector);
Diletant 149:abbf7663d27d 347 hash.sectorPosition = 0;
Diletant 149:abbf7663d27d 348 hash.pagePosition = 0;
Diletant 149:abbf7663d27d 349 //hash.pageLength = device.flash.settings.hashPageLength;
Diletant 149:abbf7663d27d 350 hash.bytes = 0;
Diletant 149:abbf7663d27d 351 hash.maxBytes = hash.sectorLength;
Diletant 149:abbf7663d27d 352
Diletant 149:abbf7663d27d 353 DeviceFlashWriteOperation data;
Diletant 156:e68ee0bcdcda 354 data.sector = device.controller.flash.settings.dataSector;
Diletant 149:abbf7663d27d 355 data.sectorAddress = AddressFromSector(data.sector);
Diletant 149:abbf7663d27d 356 data.sectorLength = LengthFromSector(data.sector);
Diletant 149:abbf7663d27d 357 data.sectorPosition = 0;
Diletant 149:abbf7663d27d 358 data.pagePosition = 0;
Diletant 149:abbf7663d27d 359 //data.pageLength = device.flash.settings.dataPageLength;
Diletant 149:abbf7663d27d 360 data.bytes = 0;
Diletant 149:abbf7663d27d 361 data.maxBytes = FLASH_SECTOR29_END - data.sectorAddress;
Diletant 149:abbf7663d27d 362
Diletant 149:abbf7663d27d 363 NVIC_DisableIRQ(TIMER1_IRQn);
Diletant 149:abbf7663d27d 364 NVIC_DisableIRQ(TIMER2_IRQn);
Diletant 149:abbf7663d27d 365 NVIC_DisableIRQ(EINT3_IRQn);
Diletant 149:abbf7663d27d 366 //SystemInitDef();
Diletant 149:abbf7663d27d 367
Diletant 149:abbf7663d27d 368 for (uint32_t i = 0; i < HASH_PARAM_COUNT; i++){
Diletant 149:abbf7663d27d 369 FlashOperationAppend(&hash, &hashParamTable[i].hash, 4);
Diletant 149:abbf7663d27d 370 uint32_t address = data.sectorAddress + data.sectorPosition + data.pagePosition;
Diletant 149:abbf7663d27d 371 FlashOperationAppend(&hash, &address, 4);
Diletant 149:abbf7663d27d 372 FlashOperationAppend(&data, hashParamTable[i].ref, hashParamTable[i].size);
Diletant 149:abbf7663d27d 373 }
Diletant 149:abbf7663d27d 374 FlashOperationComplete(&hash);
Diletant 149:abbf7663d27d 375 FlashOperationComplete(&data);
Diletant 149:abbf7663d27d 376
Diletant 149:abbf7663d27d 377 NVIC_EnableIRQ(TIMER1_IRQn);
Diletant 149:abbf7663d27d 378 NVIC_EnableIRQ(TIMER2_IRQn);
Diletant 149:abbf7663d27d 379 NVIC_EnableIRQ(EINT3_IRQn);
Diletant 149:abbf7663d27d 380 }