Mercurial > public > ostc4
annotate Discovery/Src/externLogbookFlash.c @ 427:b1091e183d52 ImprovmentNVM_2
Activated ringbuffer for settings:
In previous versions the settings have always been writte to the ring start address causing additional ~200ms for sector erase. The settings are now continously written (~8ms). At shutdown the settings are written to ring start for compability reasons.
In case of a reset the SW will scan the ringbuffer for the latest available block and restore it.
author | ideenmodellierer |
---|---|
date | Sun, 16 Feb 2020 22:04:52 +0100 |
parents | 86fcac4cc43a |
children | 7f351c25608a |
rev | line source |
---|---|
38 | 1 /** |
2 ****************************************************************************** | |
3 * @copyright heinrichs weikamp | |
4 * @file externLogbookFlash.c | |
5 * @author heinrichs weikamp gmbh | |
6 * @date 07-Aug-2014 | |
7 * @version V0.0.4 | |
8 * @since 29-Sept-2015 | |
9 * @brief Main File to access the new 1.8 Volt Spansion S25FS256S 256 Mbit (32 Mbyte) | |
10 * @bug | |
11 * @warning | |
12 * | |
13 @verbatim | |
14 ============================================================================== | |
15 ##### Logbook Header (TOC) ##### | |
16 ============================================================================== | |
17 [..] Memory useage: | |
18 NEW: Spansion S25FS-S256S | |
19 | |
20 only 8x 4KB and 1x 32KB, remaining is 64KB or 256KB | |
21 Sector Size (kbyte) Sector Count Sector Range Address Range (Byte Address) Notes | |
22 4 8 SA00 00000000h-00000FFFh | |
23 : : | |
24 SA07 00007000h-00007FFFh | |
25 | |
26 32 1 SA08 00008000h-0000FFFFh | |
27 | |
28 64 511 SA09 00010000h-0001FFFFh | |
29 : : | |
30 SA519 01FF0000h-01FFFFFFh | |
31 OLD: | |
32 1kB each header | |
33 with predive header at beginning | |
34 and postdive header with 0x400 HEADER2OFFSET | |
35 4kB (one erase) has two dives with 4 headers total | |
36 total of 512 kB (with 256 header ids (8 bit)) | |
37 Size is 280 Byte (as of 25.Nov. 2014) | |
38 | |
39 [..] Output to PC / UART is postdive header | |
40 | |
41 [..] Block Protection Lock-Down is to erase logbook only | |
42 | |
43 [..] Timing (see page 137 of LOGBOOK_V3_S25FS-S_00-271247.pdf | |
44 bulk erase is 2 minutes typ., 6 minutes max. | |
45 | |
46 ============================================================================== | |
47 ##### DEMOMODE ##### | |
48 ============================================================================== | |
49 151215: ext_flash_write_settings() is DISABLED! | |
50 | |
51 ============================================================================== | |
52 ##### bug fixes ##### | |
53 ============================================================================== | |
54 150917: end in header and length of sample was one byte too long | |
55 as stated by Jef Driesen email 15.09.2015 | |
56 | |
57 @endverbatim | |
58 ****************************************************************************** | |
59 * @attention | |
60 * | |
61 * <h2><center>© COPYRIGHT(c) 2015 heinrichs weikamp</center></h2> | |
62 * | |
63 ****************************************************************************** | |
64 */ | |
65 | |
66 /* Includes ------------------------------------------------------------------*/ | |
67 #include "stm32f4xx_hal.h" | |
68 #include "externLogbookFlash.h" | |
69 #include "ostc.h" | |
70 #include "settings.h" | |
71 #include "gfx_engine.h" | |
72 | |
73 #ifndef BOOTLOADER_STANDALONE | |
74 #include "logbook.h" | |
75 #endif | |
76 | |
77 /* Private types -------------------------------------------------------------*/ | |
78 #define FLASHSTART 0x000000 | |
79 //#define FLASHSTOP 0x01FFFFFF all 32 MB with 4byte addressing | |
80 #define FLASHSTOP 0x00FFFFFF | |
81 //#define FLASHSTOP 0x3FFFFF | |
82 #define RELEASE 1 | |
83 #define HOLDCS 0 | |
84 | |
85 #define HEADER2OFFSET 0x400 | |
86 | |
87 typedef enum{ | |
88 EF_HEADER, | |
89 EF_SAMPLE, | |
90 EF_DEVICEDATA, | |
91 EF_VPMDATA, | |
92 EF_SETTINGS, | |
93 EF_FIRMWARE, | |
94 EF_FIRMWARE2, | |
95 }which_ring_enum; | |
96 | |
97 | |
98 typedef struct{ | |
99 uint8_t IsBusy:1; | |
100 uint8_t IsWriteEnabled:1; | |
101 uint8_t BlockProtect0:1; | |
102 uint8_t BlockProtect1:1; | |
103 uint8_t BlockProtect2:1; | |
104 uint8_t BlockProtect3:1; | |
105 uint8_t IsAutoAddressIncMode:1; | |
106 uint8_t BlockProtectL:1; | |
107 } extFlashStatusUbit8_t; | |
108 | |
109 typedef union{ | |
110 extFlashStatusUbit8_t ub; | |
111 uint8_t uw; | |
112 } extFlashStatusBit8_Type; | |
113 | |
114 | |
115 /* Exported variables --------------------------------------------------------*/ | |
116 | |
117 /* Private variables ---------------------------------------------------------*/ | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
118 static uint32_t actualAddress = 0; |
423
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
119 static uint32_t preparedPageAddress = 0; |
425
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
120 static uint32_t closeSectorAddress = 0; |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
121 static uint32_t entryPoint = 0; |
38 | 122 |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
123 static uint32_t actualPointerHeader = 0; |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
124 static uint32_t actualPointerSample = 0; |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
125 static uint32_t LengthLeftSampleRead = 0; |
421
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
126 static uint32_t actualPointerDevicedata = DDSTART; |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
127 static uint32_t actualPointerDevicedata_Read = DDSTART; |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
128 static uint32_t actualPointerVPM = 0; |
427 | 129 static uint32_t actualPointerSettings = SETTINGSSTART; |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
130 static uint32_t actualPointerFirmware = 0; |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
131 static uint32_t actualPointerFirmware2 = 0; |
38 | 132 |
133 /* Private function prototypes -----------------------------------------------*/ | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
134 static void chip_unselect(void); |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
135 static void chip_select(void); |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
136 static void error_led_on(void); |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
137 static void error_led_off(void); |
38 | 138 |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
139 static void write_spi(uint8_t data, uint8_t unselect_CS_afterwards); |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
140 static uint8_t read_spi(uint8_t unselect_CS_afterwards); |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
141 static void write_address(uint8_t unselect_CS_afterwards); |
38 | 142 static void Error_Handler_extflash(void); |
143 static void wait_chip_not_busy(void); | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
144 static void ext_flash_incf_address(uint8_t type); |
38 | 145 //void ext_flash_incf_address_ring(void); |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
146 static void ext_flash_decf_address_ring(uint8_t type); |
38 | 147 |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
148 static void ext_flash_erase4kB(void); |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
149 static void ext_flash_erase32kB(void); |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
150 static void ext_flash_erase64kB(void); |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
151 static uint8_t ext_flash_erase_if_on_page_start(void); |
38 | 152 |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
153 static void ef_write_block(uint8_t * sendByte, uint32_t length, uint8_t type, uint8_t do_not_erase); |
38 | 154 |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
155 static void ext_flash_read_block(uint8_t *getByte, uint8_t type); |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
156 static void ext_flash_read_block_multi(void *getByte, uint32_t size, uint8_t type); |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
157 static void ext_flash_read_block_stop(void); |
38 | 158 |
159 static void ef_hw_rough_delay_us(uint32_t delayUs); | |
160 static void ef_erase_64K(uint32_t blocks); | |
161 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
162 static void ext_flash_overwrite_sample_without_erase(uint8_t *pSample, uint16_t length); |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
163 |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
164 static void ext_flash_disable_protection(void); |
38 | 165 |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
166 static _Bool ext_flash_test_remaining_space_of_page_empty(uint32_t pointer, uint16_t length); |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
167 static void ext_flash_set_to_begin_of_next_page(uint32_t *pointer, uint8_t type); |
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
168 static void ext_flash_find_start(void); |
38 | 169 |
170 | |
171 /* Exported functions --------------------------------------------------------*/ | |
172 | |
173 void ext_flash_write_firmware(uint8_t *pSample1, uint32_t length1)//, uint8_t *pSample2, uint32_t length2) | |
174 { | |
175 general32to8_Type lengthTransform; | |
176 | |
177 lengthTransform.u32 = length1; | |
178 | |
179 actualPointerFirmware = FWSTART; | |
180 ef_write_block(lengthTransform.u8,4, EF_FIRMWARE, 1); | |
181 ef_write_block(pSample1,length1, EF_FIRMWARE, 1); | |
182 | |
183 // if(length2) | |
184 // ef_write_block(pSample2,length2, EF_FIRMWARE, 1); | |
185 } | |
186 | |
187 uint8_t ext_flash_read_firmware_version(char *text) | |
188 { | |
189 uint32_t backup = actualAddress; | |
190 uint8_t buffer[4]; | |
191 | |
192 // + 4 for length data, see ext_flash_write_firmware | |
193 actualAddress = FWSTART + 4 + 0x10000; | |
194 ext_flash_read_block_start(); | |
195 ext_flash_read_block(&buffer[0], EF_FIRMWARE); | |
196 ext_flash_read_block(&buffer[1], EF_FIRMWARE); | |
197 ext_flash_read_block(&buffer[2], EF_FIRMWARE); | |
198 ext_flash_read_block(&buffer[3], EF_FIRMWARE); | |
199 | |
200 ext_flash_read_block_stop(); | |
201 actualAddress = backup; | |
202 | |
203 uint8_t ptr = 0; | |
204 text[ptr++] = 'V'; | |
205 ptr += gfx_number_to_string(2,0,&text[ptr],buffer[0] & 0x3F); | |
206 text[ptr++] = '.'; | |
207 ptr += gfx_number_to_string(2,0,&text[ptr],buffer[1] & 0x3F); | |
208 text[ptr++] = '.'; | |
209 ptr += gfx_number_to_string(2,0,&text[ptr],buffer[2] & 0x3F); | |
210 text[ptr++] = ' '; | |
211 if(buffer[3]) | |
212 { | |
213 text[ptr++] = 'b'; | |
214 text[ptr++] = 'e'; | |
215 text[ptr++] = 't'; | |
216 text[ptr++] = 'a'; | |
217 text[ptr++] = ' '; | |
218 } | |
219 return ptr; | |
220 } | |
221 | |
222 | |
223 uint32_t ext_flash_read_firmware(uint8_t *pSample1, uint32_t max_length, uint8_t *magicByte) | |
224 { | |
225 uint32_t backup = actualAddress; | |
226 general32to8_Type lengthTransform; | |
227 | |
228 actualAddress = FWSTART; | |
229 ext_flash_read_block_start(); | |
230 | |
231 ext_flash_read_block(&lengthTransform.u8[0], EF_FIRMWARE); | |
232 ext_flash_read_block(&lengthTransform.u8[1], EF_FIRMWARE); | |
233 ext_flash_read_block(&lengthTransform.u8[2], EF_FIRMWARE); | |
234 ext_flash_read_block(&lengthTransform.u8[3], EF_FIRMWARE); | |
235 | |
236 | |
237 if(lengthTransform.u32 == 0xFFFFFFFF) | |
238 { | |
239 lengthTransform.u32 = 0xFFFFFFFF; | |
240 } | |
241 else | |
242 if(lengthTransform.u32 > max_length) | |
243 { | |
244 lengthTransform.u32 = 0xFF000000; | |
245 } | |
246 else | |
247 { | |
248 for(uint32_t i = 0; i<lengthTransform.u32; i++) | |
249 { | |
250 ext_flash_read_block(&pSample1[i], EF_FIRMWARE); | |
251 } | |
252 | |
253 } | |
254 | |
255 ext_flash_read_block_stop(); | |
256 | |
257 if(magicByte) | |
258 { | |
259 *magicByte = pSample1[0x10000 + 0x3E]; // 0x3E == 62 | |
260 } | |
261 | |
262 actualAddress = backup; | |
263 return lengthTransform.u32; | |
264 } | |
265 | |
266 | |
267 void ext_flash_write_firmware2(uint32_t offset, uint8_t *pSample1, uint32_t length1, uint8_t *pSample2, uint32_t length2) | |
268 { | |
269 general32to8_Type lengthTransform, offsetTransform; | |
270 | |
271 lengthTransform.u32 = length1 + length2; | |
272 offsetTransform.u32 = offset; | |
273 | |
274 actualPointerFirmware2 = FWSTART2; | |
275 ef_write_block(lengthTransform.u8,4, EF_FIRMWARE2, 1); | |
276 ef_write_block(offsetTransform.u8,4, EF_FIRMWARE2, 1); | |
277 ef_write_block(pSample1,length1, EF_FIRMWARE2, 1); | |
278 if(length2) | |
279 ef_write_block(pSample2,length2, EF_FIRMWARE2, 1); | |
280 } | |
281 | |
282 | |
283 uint32_t ext_flash_read_firmware2(uint32_t *offset, uint8_t *pSample1, uint32_t max_length1, uint8_t *pSample2, uint32_t max_length2) | |
284 { | |
285 uint32_t backup = actualAddress; | |
286 uint32_t length1, length2; | |
287 general32to8_Type lengthTransform, offsetTransform; | |
288 | |
289 actualAddress = FWSTART2; | |
290 ext_flash_read_block_start(); | |
291 | |
292 ext_flash_read_block(&lengthTransform.u8[0], EF_FIRMWARE2); | |
293 ext_flash_read_block(&lengthTransform.u8[1], EF_FIRMWARE2); | |
294 ext_flash_read_block(&lengthTransform.u8[2], EF_FIRMWARE2); | |
295 ext_flash_read_block(&lengthTransform.u8[3], EF_FIRMWARE2); | |
296 | |
297 ext_flash_read_block(&offsetTransform.u8[0], EF_FIRMWARE2); | |
298 ext_flash_read_block(&offsetTransform.u8[1], EF_FIRMWARE2); | |
299 ext_flash_read_block(&offsetTransform.u8[2], EF_FIRMWARE2); | |
300 ext_flash_read_block(&offsetTransform.u8[3], EF_FIRMWARE2); | |
301 | |
302 *offset = offsetTransform.u32; | |
303 | |
304 if(lengthTransform.u32 == 0xFFFFFFFF) | |
305 { | |
306 lengthTransform.u32 = 0xFFFFFFFF; | |
307 } | |
308 else | |
309 if(lengthTransform.u32 > max_length1 + max_length2) | |
310 { | |
311 lengthTransform.u32 = 0xFF000000; | |
312 } | |
313 else | |
314 { | |
315 if(lengthTransform.u32 < max_length1) | |
316 { | |
317 length1 = lengthTransform.u32; | |
318 length2 = 0; | |
319 } | |
320 else | |
321 { | |
322 length1 = max_length1; | |
323 length2 = lengthTransform.u32 - max_length1; | |
324 } | |
325 | |
326 if(pSample1) | |
327 { | |
328 for(uint32_t i = 0; i<length1; i++) | |
329 { | |
330 ext_flash_read_block(&pSample1[i], EF_FIRMWARE2); | |
331 } | |
332 if(pSample2) | |
333 { | |
334 for(uint32_t i = 0; i<length2; i++) | |
335 { | |
336 ext_flash_read_block(&pSample2[i], EF_FIRMWARE2); | |
337 } | |
338 } | |
339 } | |
340 else if(pSample2) | |
341 { | |
342 actualAddress += length1; | |
343 for(uint32_t i = 0; i<length2; i++) | |
344 { | |
345 ext_flash_read_block(&pSample2[i], EF_FIRMWARE2); | |
346 } | |
347 } | |
348 } | |
349 ext_flash_read_block_stop(); | |
350 actualAddress = backup; | |
351 return lengthTransform.u32; | |
352 } | |
353 | |
354 | |
355 void ext_flash_read_fixed_16_devicedata_blocks_formated_128byte_total(uint8_t *buffer) | |
356 { | |
357 SDeviceLine data[16]; | |
358 uint8_t tempLengthIngnore; | |
359 uint16_t count; | |
360 uint8_t transfer; | |
361 | |
362 RTC_DateTypeDef Sdate; | |
363 RTC_TimeTypeDef Stime; | |
364 | |
365 actualAddress = DDSTART; | |
366 | |
367 ext_flash_read_block_start(); | |
368 ext_flash_read_block(&tempLengthIngnore, EF_DEVICEDATA); | |
369 ext_flash_read_block(&tempLengthIngnore, EF_DEVICEDATA); | |
370 | |
371 ext_flash_read_block_multi((uint8_t *)data,16*3*4, EF_DEVICEDATA); | |
372 ext_flash_read_block_stop(); | |
373 | |
374 count = 0; | |
375 for(int i=0;i<16;i++) | |
376 { | |
377 transfer = (data[i].value_int32 >> 24) & 0xFF; | |
378 buffer[count++] = transfer; | |
379 transfer = (data[i].value_int32 >> 16) & 0xFF; | |
380 buffer[count++] = transfer; | |
381 transfer = (data[i].value_int32 >> 8) & 0xFF; | |
382 buffer[count++] = transfer; | |
383 transfer = (data[i].value_int32) & 0xFF; | |
384 buffer[count++] = transfer; | |
385 | |
386 translateDate(data[i].date_rtc_dr, &Sdate); | |
387 translateTime(data[i].time_rtc_tr, &Stime); | |
388 buffer[count++] = Sdate.Year; | |
389 buffer[count++] = Sdate.Month; | |
390 buffer[count++] = Sdate.Date; | |
391 buffer[count++] = Stime.Hours; | |
392 } | |
393 } | |
394 | |
395 | |
396 #ifndef BOOTLOADER_STANDALONE | |
397 | |
421
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
398 void ext_flash_write_devicedata(uint8_t resetRing) |
38 | 399 { |
400 uint8_t *pData; | |
401 const uint16_t length = sizeof(SDevice); | |
402 uint8_t length_lo, length_hi; | |
403 uint8_t dataLength[2] = { 0 }; | |
421
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
404 uint32_t tmpBlockStart; |
38 | 405 |
406 ext_flash_disable_protection(); | |
407 | |
408 pData = (uint8_t *)stateDeviceGetPointer(); | |
409 | |
421
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
410 /* Reset the Ring to the start address if requested (e.g. because we write the default block during shutdown) */ |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
411 if((resetRing) || ((actualPointerDevicedata + length) >= DDSTOP)) |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
412 { |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
413 actualPointerDevicedata = DDSTART; |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
414 } |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
415 tmpBlockStart = actualPointerDevicedata; |
38 | 416 |
417 length_lo = (uint8_t)(length & 0xFF); | |
418 length_hi = (uint8_t)(length >> 8); | |
419 dataLength[0] = length_lo; | |
420 dataLength[1] = length_hi; | |
421 | |
422 ef_write_block(dataLength,2, EF_DEVICEDATA, 0); | |
423 ef_write_block(pData,length, EF_DEVICEDATA, 0); | |
421
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
424 |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
425 actualPointerDevicedata_Read = tmpBlockStart; |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
426 |
38 | 427 } |
428 | |
429 | |
430 uint16_t ext_flash_read_devicedata(uint8_t *buffer, uint16_t max_length) | |
431 { | |
432 uint16_t length; | |
433 uint8_t length_lo, length_hi; | |
434 | |
421
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
435 actualAddress = actualPointerDevicedata_Read; |
38 | 436 |
421
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
437 length = 0; |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
438 length_lo = 0; |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
439 length_hi = 0; |
38 | 440 ext_flash_read_block_start(); |
421
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
441 |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
442 |
38 | 443 ext_flash_read_block(&length_lo, EF_DEVICEDATA); |
444 ext_flash_read_block(&length_hi, EF_DEVICEDATA); | |
445 | |
421
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
446 while ((length_lo != 0xFF) && (length_hi != 0xFF)) |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
447 { |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
448 length = (length_hi * 256) + length_lo; |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
449 |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
450 if(length > max_length) |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
451 return 0; |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
452 |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
453 ext_flash_read_block_multi(buffer,length,EF_DEVICEDATA); |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
454 |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
455 ext_flash_read_block(&length_lo, EF_DEVICEDATA); /* check if another devicedata set is available */ |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
456 ext_flash_read_block(&length_hi, EF_DEVICEDATA); /* length will be 0xFFFF if a empty memory is read */ |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
457 } |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
458 ext_flash_decf_address_ring(EF_DEVICEDATA); /* set pointer back to empty address */ |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
459 ext_flash_decf_address_ring(EF_DEVICEDATA); |
38 | 460 ext_flash_read_block_stop(); |
421
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
461 |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
462 if(actualAddress > actualPointerDevicedata) /* the write pointer has not yet been set up probably */ |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
463 { |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
464 actualPointerDevicedata = actualAddress; |
3f7d80f37bfc
Enable sequentionel writing of device data:
ideenmodellierer
parents:
420
diff
changeset
|
465 } |
38 | 466 return length; |
467 } | |
468 | |
469 | |
470 void ext_flash_write_vpm(SVpm *vpmInput) | |
471 { | |
472 uint8_t *pData; | |
473 const uint16_t length = sizeof(SVpm); | |
474 | |
475 uint8_t length_lo, length_hi; | |
476 uint8_t dataLength[2] = { 0 }; | |
477 | |
478 pData = (uint8_t *)vpmInput; | |
479 | |
480 actualPointerVPM = VPMSTART; | |
481 | |
482 length_lo = (uint8_t)(length & 0xFF); | |
483 length_hi = (uint8_t)(length >> 8); | |
484 dataLength[0] = length_lo; | |
485 dataLength[1] = length_hi; | |
486 | |
487 ef_write_block(dataLength,2, EF_VPMDATA, 0); | |
488 ef_write_block(pData,length, EF_VPMDATA, 0); | |
489 } | |
490 | |
491 | |
492 int ext_flash_read_vpm(SVpm *vpmOutput) | |
493 { | |
494 uint8_t *pData; | |
495 const uint16_t length = sizeof(SVpm); | |
496 uint8_t length_lo, length_hi; | |
497 int output; | |
498 | |
499 actualAddress = VPMSTART; | |
500 | |
501 ext_flash_read_block_start(); | |
502 ext_flash_read_block(&length_lo, EF_VPMDATA); | |
503 ext_flash_read_block(&length_hi, EF_VPMDATA); | |
504 if((length_lo == (uint8_t)(length & 0xFF)) | |
505 &&(length_hi == (uint8_t)(length >> 8))) | |
506 { | |
507 pData = (uint8_t *)vpmOutput; | |
508 for(uint16_t i = 0; i < length; i++) | |
509 ext_flash_read_block(&pData[i], EF_VPMDATA); | |
510 output = length; | |
511 } | |
512 else | |
513 output = 0; | |
514 | |
515 ext_flash_read_block_stop(); | |
516 return output; | |
517 } | |
518 | |
519 #ifdef DEMOMODE | |
520 void ext_flash_write_settings(void) | |
521 { | |
522 return; | |
523 } | |
524 #else | |
427 | 525 void ext_flash_write_settings(uint8_t resetRing) |
38 | 526 { |
527 uint8_t *pData; | |
528 const uint16_t length = sizeof(SSettings); | |
529 uint8_t length_lo, length_hi; | |
530 uint8_t dataLength[2] = { 0 }; | |
531 | |
532 ext_flash_disable_protection(); | |
533 | |
534 if(stateRealGetPointer()->lastKnownBatteryPercentage) | |
535 { | |
536 settingsGetPointer()->lastKnownBatteryPercentage = stateRealGetPointer()->lastKnownBatteryPercentage; | |
537 } | |
538 settingsGetPointer()->backup_localtime_rtc_tr = stateRealGetPointer()->lifeData.timeBinaryFormat; | |
539 settingsGetPointer()->backup_localtime_rtc_dr = stateRealGetPointer()->lifeData.dateBinaryFormat; | |
540 | |
541 pData = (uint8_t *)settingsGetPointer(); | |
542 | |
427 | 543 /* Reset the Ring to the start address if requested (e.g. because we write the default block during shutdown) */ |
544 if((resetRing) || ((actualPointerSettings + length) >= SETTINGSSTOP)) | |
545 { | |
546 actualPointerSettings = SETTINGSSTART; | |
547 } | |
38 | 548 |
549 length_lo = (uint8_t)(length & 0xFF); | |
550 length_hi = (uint8_t)(length >> 8); | |
551 dataLength[0] = length_lo; | |
552 dataLength[1] = length_hi; | |
553 | |
554 ef_write_block(dataLength,2, EF_SETTINGS, 0); | |
555 ef_write_block(pData,length, EF_SETTINGS, 0); | |
556 // ext_flash_enable_protection(); | |
557 } | |
558 #endif | |
559 | |
560 | |
561 /* CHANGES 150929 hw | |
562 * this now allows to read old settings too | |
563 * but make sure that everything is fixed in | |
564 * set_new_settings_missing_in_ext_flash | |
565 * new settings should be fine as they are added | |
566 * and loaded before calling this function | |
567 */ | |
568 uint8_t ext_flash_read_settings(void) | |
569 { | |
570 uint8_t returnValue = HAL_BUSY; | |
571 uint8_t *pData; | |
572 const uint16_t lengthStandardNow = sizeof(SSettings); | |
573 uint8_t length_lo, length_hi; | |
574 uint16_t lengthOnEEPROM; | |
575 uint32_t header; | |
576 SSettings *pSettings = settingsGetPointer(); | |
577 | |
578 actualAddress = SETTINGSSTART; | |
579 | |
580 ext_flash_read_block_start(); | |
581 ext_flash_read_block(&length_lo, EF_SETTINGS); | |
582 ext_flash_read_block(&length_hi, EF_SETTINGS); | |
583 | |
427 | 584 while ((length_lo != 0xFF) && (length_hi != 0xFF)) /* get the latest stored setting block */ |
38 | 585 { |
427 | 586 lengthOnEEPROM = length_hi * 256; |
587 lengthOnEEPROM += length_lo; | |
588 if(lengthOnEEPROM <= lengthStandardNow) | |
38 | 589 { |
427 | 590 ext_flash_read_block_multi(&header, 4, EF_SETTINGS); |
591 if((header <= pSettings->header) && (header >= pSettings->updateSettingsAllowedFromHeader)) | |
592 { | |
593 returnValue = HAL_OK; | |
594 pSettings->header = header; | |
595 pData = (uint8_t *)pSettings + 4; /* header */ | |
596 for(uint16_t i = 0; i < (lengthOnEEPROM-4); i++) | |
597 ext_flash_read_block(&pData[i], EF_SETTINGS); | |
598 } | |
599 else | |
600 { | |
601 returnValue = HAL_ERROR; | |
602 } | |
38 | 603 } |
427 | 604 ext_flash_read_block(&length_lo, EF_SETTINGS); |
605 ext_flash_read_block(&length_hi, EF_SETTINGS); | |
606 } | |
607 ext_flash_decf_address_ring(EF_SETTINGS); /* set pointer back to empty address */ | |
608 ext_flash_decf_address_ring(EF_SETTINGS); | |
609 ext_flash_read_block_stop(); | |
610 | |
611 if(actualAddress > actualPointerSettings) /* the write pointer has not yet been set up probably */ | |
612 { | |
613 actualPointerSettings = actualAddress; | |
38 | 614 } |
615 ext_flash_read_block_stop(); | |
616 return returnValue; | |
617 } | |
618 | |
619 | |
620 | |
621 | |
622 /* ext_flash_start_new_dive_log_and_set_actualPointerSample | |
623 * prepares the write sample pointer | |
624 * to be used by ext_flash_write_sample() | |
625 * to be set in the * pHeaderPreDive | |
626 * for write with ext_flash_create_new_dive_log() and ext_flash_close_new_dive_log() | |
627 */ | |
628 void ext_flash_start_new_dive_log_and_set_actualPointerSample(uint8_t *pHeaderPreDive) | |
629 { | |
630 convert_Type data; | |
631 SSettings *settings = settingsGetPointer(); | |
632 | |
633 /* new 5. Jan. 2015 */ | |
634 actualPointerSample = settings->logFlashNextSampleStartAddress; | |
635 | |
636 if(!ext_flash_test_remaining_space_of_page_empty(actualPointerSample, 4)) | |
637 ext_flash_set_to_begin_of_next_page(&actualPointerSample, EF_SAMPLE); | |
638 | |
639 if((actualPointerSample < SAMPLESTART) || (actualPointerSample > SAMPLESTOP)) | |
640 actualPointerSample = SAMPLESTART; | |
641 | |
642 data.u32bit = actualPointerSample; | |
643 pHeaderPreDive[2] = data.u8bit.byteLow; | |
644 pHeaderPreDive[3] = data.u8bit.byteMidLow; | |
645 pHeaderPreDive[4] = data.u8bit.byteMidHigh; | |
646 /* to start sample writing and header etc. pp. */ | |
647 ext_flash_disable_protection_for_logbook(); | |
648 } | |
649 | |
650 | |
651 /* ext_flash_create_new_dive_log | |
652 * uses the first header without HEADER2OFFSET | |
653 * for the header it is not important to be complete | |
654 * and can be reconstructed | |
655 * ext_flash_start_new_dive_log_and_set_actualPointerSample() | |
656 * has to be called before to set the actualPointerSample | |
657 * in the header | |
658 * the following func writes to header to the ext_flash | |
659 */ | |
660 void ext_flash_create_new_dive_log(uint8_t *pHeaderPreDive) | |
661 { | |
662 SSettings *settings; | |
663 uint8_t id, id_next; | |
664 uint8_t header1, header2; | |
665 | |
666 settings = settingsGetPointer(); | |
667 id = settings->lastDiveLogId; | |
668 | |
669 actualAddress = HEADERSTART + (0x800 * id); | |
670 ext_flash_read_block_start(); | |
671 ext_flash_read_block(&header1, EF_SAMPLE); | |
672 ext_flash_read_block(&header2, EF_SAMPLE); | |
673 ext_flash_read_block_stop(); | |
674 | |
675 if((header1 == 0xFA) && (header2 == 0xFA)) | |
676 { | |
677 id += 1; /* 0-255, auto rollover */ | |
678 if(id & 1) | |
679 { | |
680 actualAddress = HEADERSTART + (0x800 * id); | |
681 ext_flash_read_block_start(); | |
682 ext_flash_read_block(&header1, EF_SAMPLE); | |
683 ext_flash_read_block(&header2, EF_SAMPLE); | |
684 ext_flash_read_block_stop(); | |
685 if((header1 == 0xFA) && (header2 == 0xFA)) | |
686 id += 1; | |
687 } | |
688 } | |
689 else | |
690 { | |
691 id = 0; | |
692 } | |
693 | |
694 /* delete next header */ | |
695 id_next = id + 1; | |
696 actualPointerHeader = HEADERSTART + (0x800 * id_next); | |
697 ef_write_block(0,0, EF_HEADER, 0); | |
698 | |
699 settings->lastDiveLogId = id; | |
700 actualPointerHeader = HEADERSTART + (0x800 * id); | |
701 | |
702 if(pHeaderPreDive != 0) | |
703 ef_write_block(pHeaderPreDive,HEADERSIZE, EF_HEADER, 0); | |
704 } | |
705 | |
706 | |
707 void ext_flash_close_new_dive_log(uint8_t *pHeaderPostDive ) | |
708 { | |
709 SSettings * settings = settingsGetPointer(); | |
710 uint8_t id; | |
711 convert_Type startAddress; | |
712 convert_Type data; | |
713 uint32_t backup; | |
714 | |
715 uint8_t sampleData[3]; | |
716 actualAddress = actualPointerSample; | |
717 sampleData[0] = 0xFD; | |
718 sampleData[1] = 0xFD; | |
719 ext_flash_write_sample(sampleData, 2); | |
720 | |
721 /* end of sample data, pointing to the last sample 0xFD | |
722 */ | |
723 actualAddress = actualPointerSample; // change hw 17.09.2015 | |
724 ext_flash_decf_address_ring(EF_SAMPLE); // 17.09.2015: this decf actualAddress only!! | |
725 actualPointerSample = actualAddress; // change hw 17.09.2015 | |
726 data.u32bit = actualPointerSample; | |
727 | |
728 pHeaderPostDive[5] = data.u8bit.byteLow; | |
729 pHeaderPostDive[6] = data.u8bit.byteMidLow; | |
730 pHeaderPostDive[7] = data.u8bit.byteMidHigh; | |
731 | |
732 /* take data written before, calculate length and write | |
733 SLogbookHeader has different order: length (byte# 8,9,10) prior to profile version (byte# 11) | |
734 */ | |
735 startAddress.u8bit.byteLow = pHeaderPostDive[2]; | |
736 startAddress.u8bit.byteMidLow = pHeaderPostDive[3]; | |
737 startAddress.u8bit.byteMidHigh = pHeaderPostDive[4]; | |
738 startAddress.u8bit.byteHigh = 0; | |
739 | |
740 if(startAddress.u32bit < actualPointerSample) | |
741 data.u32bit = 1 + actualPointerSample - startAddress.u32bit; | |
742 else | |
743 data.u32bit = 2 + (actualPointerSample - SAMPLESTART) + (SAMPLESTOP - startAddress.u32bit); | |
744 | |
745 pHeaderPostDive[8] = data.u8bit.byteLow; | |
746 pHeaderPostDive[9] = data.u8bit.byteMidLow; | |
747 pHeaderPostDive[10] = data.u8bit.byteMidHigh; | |
748 | |
749 /* set id and write post-dive-header | |
750 */ | |
751 id = settings->lastDiveLogId; | |
752 actualPointerHeader = HEADERSTART + (0x800 * id) + HEADER2OFFSET; | |
753 | |
754 ef_write_block(pHeaderPostDive,HEADERSIZE, EF_HEADER, 0); | |
755 | |
756 /* write length at beginning of sample | |
757 and write proper beginning for next dive to actualPointerSample | |
758 */ | |
759 backup = actualPointerSample; | |
760 actualPointerSample = startAddress.u32bit; // is still 0xFF | |
761 sampleData[0] = data.u8bit.byteLow; | |
762 sampleData[1] = data.u8bit.byteMidLow; | |
763 sampleData[2] = data.u8bit.byteMidHigh; | |
764 ext_flash_overwrite_sample_without_erase(sampleData, 3); | |
765 | |
766 actualAddress = backup; | |
767 ext_flash_incf_address(EF_SAMPLE); | |
768 actualPointerSample = actualAddress; | |
769 ext_flash_enable_protection(); | |
770 } | |
771 | |
772 | |
773 void ext_flash_write_sample(uint8_t *pSample, uint16_t length) | |
774 { | |
423
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
775 uint32_t actualAdressBackup = 0; |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
776 |
38 | 777 ef_write_block(pSample,length, EF_SAMPLE, 0); |
778 | |
779 SSettings *settings = settingsGetPointer(); | |
780 settings->logFlashNextSampleStartAddress = actualPointerSample; | |
423
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
781 |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
782 if(0xFFFF - (actualAddress & 0x0000FFFF) < 255) /* are we close to a sector border? */ |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
783 { |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
784 if (((actualAddress & 0x0000FFFF) != 0) && (preparedPageAddress == 0)) /* only prepare if not already at start of sector */ |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
785 { |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
786 actualAdressBackup = actualAddress; |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
787 actualAddress = (actualAddress & 0xFFFF0000) + 0x00010000; /* Set to start of next 64k sector */ |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
788 if(actualAddress >= SAMPLESTOP) |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
789 { |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
790 actualAddress = SAMPLESTART; |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
791 } |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
792 preparedPageAddress = actualAddress; |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
793 ext_flash_erase64kB(); |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
794 actualAddress = actualAdressBackup; |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
795 } |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
796 } |
38 | 797 } |
798 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
799 static void ext_flash_overwrite_sample_without_erase(uint8_t *pSample, uint16_t length) |
38 | 800 { |
801 ef_write_block(pSample,length, EF_SAMPLE, 1); | |
802 } | |
803 | |
804 | |
805 uint8_t ext_flash_count_dive_headers(void) | |
806 { | |
807 uint8_t id = 0; | |
808 uint8_t counter = 0; | |
809 uint16_t headerStartData = 0x0000; | |
810 | |
811 id = settingsGetPointer()->lastDiveLogId; | |
812 | |
813 do | |
814 { | |
815 actualAddress = HEADERSTART + (0x800 * id) + HEADER2OFFSET; | |
816 ext_flash_read_block_start(); | |
817 ext_flash_read_block_multi((uint8_t *)&headerStartData, 2, EF_HEADER); | |
818 ext_flash_read_block_stop(); | |
819 counter++; | |
820 id -=1; | |
821 } while((headerStartData == 0xFAFA) && (counter < 255)); | |
822 return (counter - 1); | |
823 } | |
824 | |
825 | |
826 void ext_flash_read_dive_header(uint8_t *pHeaderToFill, uint8_t StepBackwards) | |
827 { | |
828 SSettings *settings; | |
829 uint8_t id; | |
830 uint16_t i; | |
831 | |
832 settings = settingsGetPointer(); | |
833 id = settings->lastDiveLogId; | |
834 id -= StepBackwards; /* 0-255, auto rollover */ | |
835 | |
836 actualAddress = HEADERSTART + (0x800 * id) + HEADER2OFFSET; | |
837 ext_flash_read_block_start(); | |
838 for(i = 0; i < HEADERSIZE; i++) | |
839 ext_flash_read_block(&pHeaderToFill[i], EF_HEADER); | |
840 ext_flash_read_block_stop(); | |
841 | |
842 } | |
843 | |
844 void ext_flash_read_dive_header2(uint8_t *pHeaderToFill, uint8_t id, _Bool bOffset) | |
845 { | |
846 | |
847 uint16_t i; | |
848 actualAddress = HEADERSTART + (0x800 * id) ; | |
849 | |
850 if(bOffset) | |
851 actualAddress += HEADER2OFFSET; | |
852 ext_flash_read_block_start(); | |
853 for(i = 0; i < HEADERSIZE; i++) | |
854 ext_flash_read_block(&pHeaderToFill[i], EF_HEADER); | |
855 ext_flash_read_block_stop(); | |
856 } | |
857 | |
858 | |
859 uint32_t ext_flash_read_dive_raw_with_double_header_1K(uint8_t *data, uint32_t max_size, uint8_t StepBackwards) | |
860 { | |
861 if(max_size < 0x800) | |
862 return 0; | |
863 | |
864 uint8_t id; | |
865 convert_Type dataStart, dataEnd; | |
866 uint32_t LengthAll = 0; | |
867 | |
868 id = settingsGetPointer()->lastDiveLogId; | |
869 id -= StepBackwards; /* 0-255, auto rollover */ | |
870 | |
871 // clear data | |
872 for(int i=0;i<0x800;i++) | |
873 data[i] = 0xFF; | |
874 | |
875 // copy primary/pre-dive | |
876 actualAddress = HEADERSTART + (0x800 * id); | |
877 ext_flash_read_block_start(); | |
878 for(int i = 0; i < HEADERSIZE; i++) | |
879 ext_flash_read_block(&data[i], EF_HEADER); | |
880 ext_flash_read_block_stop(); | |
881 | |
882 // copy main/secondary/post-dive | |
883 actualAddress = HEADERSTART + (0x800 * id) + HEADER2OFFSET; | |
884 ext_flash_read_block_start(); | |
885 for(int i = 0x400; i < HEADERSIZE+0x400; i++) | |
886 ext_flash_read_block(&data[i], EF_HEADER); | |
887 ext_flash_read_block_stop(); | |
888 | |
889 // data | |
890 | |
891 dataStart.u8bit.byteHigh = 0; | |
892 dataStart.u8bit.byteLow = data[0x402]; | |
893 dataStart.u8bit.byteMidLow = data[0x403]; | |
894 dataStart.u8bit.byteMidHigh = data[0x404]; | |
895 | |
896 dataEnd.u8bit.byteHigh = 0; | |
897 dataEnd.u8bit.byteLow = data[0x405]; | |
898 dataEnd.u8bit.byteMidLow = data[0x406]; | |
899 dataEnd.u8bit.byteMidHigh = data[0x407]; | |
900 | |
901 actualPointerSample = dataStart.u32bit; | |
902 if(dataEnd.u32bit >= dataStart.u32bit) | |
903 LengthAll = 1 + dataEnd.u32bit - dataStart.u32bit; | |
904 else | |
905 LengthAll = 2 + (dataStart.u32bit - SAMPLESTART) + (SAMPLESTOP - dataEnd.u32bit); | |
906 | |
907 LengthAll += 0x800; | |
908 | |
909 if(LengthAll > max_size) | |
910 return 0x800; | |
911 | |
912 actualAddress = actualPointerSample; | |
913 ext_flash_read_block_start(); | |
914 for(uint32_t i = 0x800; i < LengthAll; i++) | |
915 ext_flash_read_block(&data[i], EF_SAMPLE); | |
916 ext_flash_read_block_stop(); | |
917 return LengthAll; | |
918 } | |
919 | |
920 void ext_flash_write_dive_raw_with_double_header_1K(uint8_t *data, uint32_t length) | |
921 { | |
922 convert_Type dataStart, dataEnd; | |
923 SLogbookHeader headerTemp; | |
924 | |
925 // set actualPointerSample and get pointer to sample storage and disable flash write protect | |
926 ext_flash_start_new_dive_log_and_set_actualPointerSample((uint8_t *)&headerTemp); | |
927 | |
928 dataStart.u8bit.byteHigh = 0; | |
929 dataStart.u8bit.byteLow = headerTemp.pBeginProfileData[0]; | |
930 dataStart.u8bit.byteMidLow = headerTemp.pBeginProfileData[1]; | |
931 dataStart.u8bit.byteMidHigh = headerTemp.pBeginProfileData[2]; | |
932 | |
933 dataEnd.u32bit = dataStart.u32bit + length - 0x801; | |
934 if(dataEnd.u32bit > SAMPLESTOP) | |
935 dataEnd.u32bit -= SAMPLESTOP + SAMPLESTART - 1; | |
936 | |
937 data[0x002] = data[0x402] = dataStart.u8bit.byteLow; | |
938 data[0x003] = data[0x403] = dataStart.u8bit.byteMidLow; | |
939 data[0x004] = data[0x404] = dataStart.u8bit.byteMidHigh; | |
940 data[0x005] = data[0x405] = dataEnd.u8bit.byteLow; | |
941 data[0x006] = data[0x406] = dataEnd.u8bit.byteMidLow; | |
942 data[0x007] = data[0x407] = dataEnd.u8bit.byteMidHigh; | |
943 | |
944 // set actualPointerHeader to next free header and update lastDiveLogId | |
945 ext_flash_create_new_dive_log(0); | |
946 | |
947 // copy header data | |
948 ef_write_block(data,0x800,EF_HEADER, 1); | |
949 | |
950 // copy sample data | |
951 ef_write_block(&data[0x800], length-0x800, EF_SAMPLE, 1); | |
952 | |
953 // update logFlashNextSampleStartAddress | |
954 settingsGetPointer()->logFlashNextSampleStartAddress = actualPointerSample; | |
955 } | |
956 | |
957 | |
958 // =============================================================================== | |
959 // ext_flash_read_header_memory | |
960 /// @brief This function returns the entire header space 1:1 | |
961 /// @date 04-April-2016 | |
962 /// | |
963 /// @param *data 256KB output | |
964 // =============================================================================== | |
965 void ext_flash_read_header_memory(uint8_t *data) | |
966 { | |
967 actualAddress = HEADERSTART; | |
968 actualPointerHeader = actualAddress; | |
969 ext_flash_read_block_start(); | |
970 for(int i=0;i<8;i++) | |
971 ext_flash_read_block_multi(&data[0x8000 * i], 0x8000, EF_HEADER); | |
972 ext_flash_read_block_stop(); | |
973 } | |
974 | |
975 | |
976 // =============================================================================== | |
977 // ext_flash_read_header_memory | |
978 /// @brief This function erases and overwrites the entire logbook header block | |
979 /// @date 04-April-2016 | |
980 /// | |
981 /// @param *data 256KB input of header memory 1:1 | |
982 // =============================================================================== | |
983 void ext_flash_write_header_memory(uint8_t *data) | |
984 { | |
985 actualAddress = HEADERSTART; | |
986 actualPointerHeader = actualAddress; | |
987 ef_write_block(data, 0x40000, EF_HEADER, 0); | |
988 } | |
989 | |
990 | |
991 void ext_flash_open_read_sample(uint8_t StepBackwards, uint32_t *totalNumberOfBytes) | |
992 { | |
993 SSettings *settings = settingsGetPointer(); | |
994 uint8_t id; | |
995 convert_Type dataStart, dataEnd; | |
996 uint8_t header1, header2; | |
997 | |
998 id = settings->lastDiveLogId; | |
999 id -= StepBackwards; /* 0-255, auto rollover */ | |
1000 # | |
1001 actualAddress = HEADERSTART + (0x800 * id) + HEADER2OFFSET; | |
1002 actualPointerHeader = actualAddress; | |
1003 | |
1004 ext_flash_read_block_start(); | |
1005 /* little endian */ | |
1006 ext_flash_read_block(&header1, EF_HEADER); | |
1007 ext_flash_read_block(&header2, EF_HEADER); | |
1008 dataStart.u8bit.byteHigh = 0; | |
1009 ext_flash_read_block(&dataStart.u8bit.byteLow, EF_HEADER); | |
1010 ext_flash_read_block(&dataStart.u8bit.byteMidLow, EF_HEADER); | |
1011 ext_flash_read_block(&dataStart.u8bit.byteMidHigh, EF_HEADER); | |
1012 dataEnd.u8bit.byteHigh = 0; | |
1013 ext_flash_read_block(&dataEnd.u8bit.byteLow, EF_HEADER); | |
1014 ext_flash_read_block(&dataEnd.u8bit.byteMidLow, EF_HEADER); | |
1015 ext_flash_read_block(&dataEnd.u8bit.byteMidHigh, EF_HEADER); | |
1016 ext_flash_read_block_stop(); | |
1017 | |
1018 actualPointerSample = dataStart.u32bit; | |
1019 if(dataEnd.u32bit >= dataStart.u32bit) | |
1020 LengthLeftSampleRead = 1 + dataEnd.u32bit - dataStart.u32bit; | |
1021 else | |
1022 LengthLeftSampleRead = 2 + (dataStart.u32bit - SAMPLESTART) + (SAMPLESTOP - dataEnd.u32bit); | |
1023 *totalNumberOfBytes = LengthLeftSampleRead; | |
1024 | |
1025 actualAddress = actualPointerSample; | |
1026 ext_flash_read_block_start(); | |
1027 } | |
1028 | |
1029 | |
1030 void ext_flash_read_next_sample_part(uint8_t *pSample, uint8_t length) | |
1031 { | |
1032 for(uint16_t i = 0; i < length; i++) | |
1033 ext_flash_read_block(&pSample[i], EF_SAMPLE); | |
1034 } | |
1035 | |
1036 | |
1037 void ext_flash_close_read_sample(void) | |
1038 { | |
1039 actualPointerSample = actualAddress; | |
1040 ext_flash_read_block_stop(); | |
1041 } | |
1042 | |
1043 | |
1044 void ext_flash_set_entry_point(void) | |
1045 { | |
1046 entryPoint = actualAddress; | |
1047 } | |
1048 | |
1049 | |
1050 void ext_flash_reopen_read_sample_at_entry_point(void) | |
1051 { | |
1052 error_led_on(); | |
1053 chip_unselect(); | |
1054 wait_chip_not_busy(); | |
1055 | |
1056 actualAddress = entryPoint; | |
1057 ext_flash_read_block_start(); | |
1058 error_led_off(); | |
1059 } | |
1060 | |
1061 /* | |
1062 uint8_t ext_flash_point_to_64k_block_in_headerSpace(uint8_t logId) | |
1063 { | |
1064 uint32_t pointerToData = logId * 0x800; | |
1065 | |
1066 return pointerToData / 0x10000; | |
1067 } | |
1068 */ | |
1069 | |
1070 | |
1071 // =============================================================================== | |
1072 // ext_flash_repair_dive_numbers_starting_count_helper | |
1073 /// @brief | |
1074 /// @date 22-June-2016 | |
1075 | |
1076 // =============================================================================== | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1077 static uint16_t ext_flash_repair_dive_numbers_starting_count_helper(uint8_t *data, uint8_t *change64k, uint16_t startNumber, uint8_t lastLogId) |
38 | 1078 { |
1079 const uint32_t headerStep = 0x800; | |
1080 uint8_t actualLogId = 0; | |
1081 uint16_t oldNumber = 0; | |
1082 uint16_t actualNumber = 0; | |
1083 SLogbookHeader *ptrLogbookHeader = 0; | |
1084 | |
1085 if(startNumber == 0) | |
1086 return 0; | |
1087 | |
1088 actualNumber = startNumber - 1; | |
1089 | |
282 | 1090 // where is the oldest dive (Which is now getting startNumber) |
38 | 1091 // use first header for ease (without HEADER2OFFSET for end of dive header) |
1092 // compare for lastLogId to prevent endless loop | |
1093 | |
1094 if(*(uint16_t*)&data[lastLogId * headerStep] != 0xFAFA) | |
1095 return 0; | |
1096 | |
1097 actualLogId = lastLogId - 1; | |
1098 while((*(uint16_t*)&data[actualLogId * headerStep] == 0xFAFA) && (actualLogId != lastLogId)) | |
1099 { | |
1100 actualLogId--; | |
1101 } | |
1102 | |
1103 // now pointing to one behind the last | |
1104 while(actualLogId != lastLogId) | |
1105 { | |
1106 actualLogId++; | |
1107 actualNumber++; | |
1108 ptrLogbookHeader = (SLogbookHeader *)&data[actualLogId * headerStep]; | |
1109 | |
1110 oldNumber = ptrLogbookHeader->diveNumber; | |
1111 if(oldNumber != actualNumber) | |
1112 { | |
1113 // change64k[ext_flash_point_to_64k_block_in_headerSpace(actualLogId )] = 1; | |
1114 change64k[(actualLogId * 0x800)/0x10000] = 1; | |
1115 ptrLogbookHeader->diveNumber = actualNumber; | |
1116 ptrLogbookHeader = (SLogbookHeader *)(&data[actualLogId * headerStep] + HEADER2OFFSET); | |
1117 ptrLogbookHeader->diveNumber = actualNumber; | |
1118 } | |
1119 } | |
1120 | |
1121 return actualNumber; | |
1122 } | |
1123 | |
1124 // =============================================================================== | |
1125 // ext_flash_repair_SPECIAL_dive_numbers_starting_count_with | |
1126 /// @brief This function | |
1127 /// @date 04-April-2016 | |
282 | 1128 /// problem (160621): 64K blocks (32 dives) in the new flash memory chip |
1129 /// This block needs to be deleted | |
1130 /// these where only 4KB block before | |
38 | 1131 /// @output endCount, last diveNumber |
1132 | |
1133 // =============================================================================== | |
1134 uint16_t ext_flash_repair_SPECIAL_dive_numbers_starting_count_with(uint16_t startCount) | |
1135 { | |
1136 uint32_t logCopyDataPtr = 0; | |
1137 uint8_t *data; | |
1138 uint16_t lastCount; | |
282 | 1139 uint8_t listOfChanged64kBlocks[8]; // 32 dives each 64K |
38 | 1140 |
1141 logCopyDataPtr = getFrame(97); | |
1142 data = (uint8_t *)logCopyDataPtr; | |
1143 | |
1144 for(int i=0;i<8;i++) | |
1145 listOfChanged64kBlocks[i] = 0; | |
1146 | |
1147 actualAddress = HEADERSTART; | |
1148 ext_flash_read_block_start(); | |
1149 ext_flash_read_block_multi(data,0x100000,EF_HEADER); | |
1150 ext_flash_read_block_stop(); | |
1151 | |
1152 lastCount = ext_flash_repair_dive_numbers_starting_count_helper(data, listOfChanged64kBlocks, startCount, settingsGetPointer()->lastDiveLogId); | |
1153 | |
1154 for(int i=0;i<8;i++) | |
1155 { | |
1156 if(listOfChanged64kBlocks[i] != 0) | |
1157 { | |
1158 actualPointerHeader = HEADERSTART + (i * 0x10000); | |
1159 ef_write_block(&data[i * 0x10000], 0x10000, EF_HEADER, 0); | |
1160 } | |
1161 } | |
1162 | |
1163 releaseFrame(97,logCopyDataPtr); | |
1164 if(settingsGetPointer()->totalDiveCounter < lastCount) | |
1165 { | |
1166 settingsGetPointer()->totalDiveCounter = lastCount; | |
1167 } | |
1168 return lastCount; | |
1169 } | |
1170 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1171 /* |
38 | 1172 void OLD_ext_flash_repair_SPECIAL_dive_numbers_starting_count_with(uint16_t startCount) |
1173 { | |
1174 uint16_t counterStorage[256]; | |
1175 uint8_t start = 0xFF; | |
1176 uint32_t logCopyDataPtr = 0; | |
1177 uint8_t *data; | |
1178 uint8_t startAbsolute = 0; | |
1179 int16_t count = 0; | |
1180 _Bool repair = 0; | |
1181 uint8_t startBackup = 0; | |
1182 | |
1183 SLogbookHeader tempLogbookHeader; | |
1184 SLogbookHeader *ptrHeaderInData1a; | |
1185 SLogbookHeader *ptrHeaderInData1b; | |
1186 SLogbookHeader *ptrHeaderInData2a; | |
1187 SLogbookHeader *ptrHeaderInData2b; | |
1188 | |
1189 logCopyDataPtr = getFrame(97); | |
1190 data = (uint8_t *)logCopyDataPtr; | |
1191 ptrHeaderInData1a = (SLogbookHeader *)logCopyDataPtr; | |
1192 ptrHeaderInData1b = (SLogbookHeader *)(logCopyDataPtr + HEADER2OFFSET); | |
1193 ptrHeaderInData2a = (SLogbookHeader *)(logCopyDataPtr + 0x800); | |
1194 ptrHeaderInData2b = (SLogbookHeader *)(logCopyDataPtr + 0x800 + HEADER2OFFSET); | |
1195 | |
1196 // get data | |
1197 for(int StepBackwards = 0; StepBackwards < 255; StepBackwards++) | |
1198 { | |
1199 logbook_getHeader(StepBackwards, &tempLogbookHeader); | |
1200 counterStorage[StepBackwards+1] = tempLogbookHeader.diveNumber; | |
1201 if(tempLogbookHeader.diveHeaderStart == 0xFAFA) | |
1202 start = StepBackwards; | |
1203 else | |
1204 break; | |
1205 } | |
1206 | |
1207 if(start == 0xFF) | |
1208 return; | |
1209 | |
1210 count = start + 1; | |
1211 startAbsolute = settingsGetPointer()->lastDiveLogId; | |
1212 | |
1213 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1214 |
38 | 1215 if(start%2) |
1216 { | |
1217 if(counterStorage[start] != startCount) | |
1218 { | |
1219 // clear data | |
1220 for(int i=0;i<0x800*2;i++) | |
1221 data[i] = 0xFF; | |
1222 | |
1223 uint8_t id = settingsGetPointer()->lastDiveLogId; | |
1224 id -= start; // 0-255, auto rollover | |
1225 | |
1226 // copy primary/pre-dive | |
1227 actualAddress = HEADERSTART + (0x800 * id); | |
1228 ext_flash_read_block_start(); | |
1229 for(int i = 0; i < HEADERSIZE; i++) | |
1230 ext_flash_read_block(&data[i], EF_HEADER); | |
1231 ext_flash_read_block_stop(); | |
1232 | |
1233 // copy main/secondary/post-dive | |
1234 actualAddress = HEADERSTART + (0x800 * id) + HEADER2OFFSET; | |
1235 ext_flash_read_block_start(); | |
1236 for(int i = 0x400; i < HEADERSIZE+0x400; i++) | |
1237 ext_flash_read_block(&data[i], EF_HEADER); | |
1238 ext_flash_read_block_stop(); | |
1239 | |
1240 // repair | |
1241 ptrHeaderInData2a->diveNumber = startCount; | |
1242 ptrHeaderInData2b->diveNumber = startCount; | |
1243 startCount++; | |
1244 | |
1245 // write | |
1246 actualAddress = HEADERSTART + (0x800 * (id-1)); | |
1247 ef_write_block(data,0x800*2,EF_HEADER, 0); | |
1248 } | |
1249 start--; | |
1250 } | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1251 |
38 | 1252 // for(int count = start; count > -1; count -= 2) |
1253 | |
1254 while(count > 0) | |
1255 { | |
1256 // clear data | |
1257 for(int i=0;i<0x1000;i++) | |
1258 data[i] = 0xFF; | |
1259 | |
1260 repair = 0; | |
1261 | |
1262 startBackup = startAbsolute; | |
1263 | |
1264 if(startAbsolute%2) // 0x800 to 0x1000 | |
1265 { | |
1266 // copy second pre-dive | |
1267 actualAddress = HEADERSTART + (0x800 * startAbsolute); | |
1268 ext_flash_read_block_start(); | |
1269 for(int i = 0x800; i < HEADERSIZE+0x800; i++) | |
1270 ext_flash_read_block(&data[i], EF_HEADER); | |
1271 ext_flash_read_block_stop(); | |
1272 | |
1273 // copy second post-dive | |
1274 actualAddress = HEADERSTART + HEADER2OFFSET + (0x800 * startAbsolute); | |
1275 ext_flash_read_block_start(); | |
1276 for(int i = 0xC00; i < HEADERSIZE+0xC00; i++) | |
1277 ext_flash_read_block(&data[i], EF_HEADER); | |
1278 ext_flash_read_block_stop(); | |
1279 | |
1280 if(counterStorage[count] != startCount) | |
1281 { | |
1282 ptrHeaderInData2a->diveNumber = startCount; | |
1283 ptrHeaderInData2b->diveNumber = startCount; | |
1284 repair = 1; | |
1285 } | |
1286 startCount += 1; | |
1287 | |
1288 startAbsolute -= 1; | |
1289 count -= 1; | |
1290 | |
1291 if(count > 0) | |
1292 { | |
1293 // copy first pre-dive | |
1294 actualAddress = HEADERSTART + (0x800 * startAbsolute); | |
1295 ext_flash_read_block_start(); | |
1296 for(int i = 0; i < HEADERSIZE; i++) | |
1297 ext_flash_read_block(&data[i], EF_HEADER); | |
1298 ext_flash_read_block_stop(); | |
1299 | |
1300 // copy first post-dive | |
1301 actualAddress = HEADERSTART + (0x800 * startAbsolute); | |
1302 ext_flash_read_block_start(); | |
1303 for(int i = 0x400; i < HEADERSIZE+0x400; i++) | |
1304 ext_flash_read_block(&data[i], EF_HEADER); | |
1305 ext_flash_read_block_stop(); | |
1306 | |
1307 if(counterStorage[count] != startCount) | |
1308 { | |
1309 ptrHeaderInData1a->diveNumber = startCount; | |
1310 ptrHeaderInData1b->diveNumber = startCount; | |
1311 repair = 1; | |
1312 } | |
1313 startCount += 1; | |
1314 | |
1315 startAbsolute -= 1; | |
1316 count -= 1; | |
1317 } | |
1318 } | |
1319 else | |
1320 { | |
1321 // copy first pre-dive | |
1322 actualAddress = HEADERSTART + (0x800 * startAbsolute); | |
1323 ext_flash_read_block_start(); | |
1324 for(int i = 0; i < HEADERSIZE; i++) | |
1325 ext_flash_read_block(&data[i], EF_HEADER); | |
1326 ext_flash_read_block_stop(); | |
1327 | |
1328 // copy first post-dive | |
1329 actualAddress = HEADERSTART + (0x800 * startAbsolute); | |
1330 ext_flash_read_block_start(); | |
1331 for(int i = 0x400; i < HEADERSIZE+0x400; i++) | |
1332 ext_flash_read_block(&data[i], EF_HEADER); | |
1333 ext_flash_read_block_stop(); | |
1334 | |
1335 if(counterStorage[count] != startCount) | |
1336 { | |
1337 ptrHeaderInData1a->diveNumber = startCount; | |
1338 ptrHeaderInData1b->diveNumber = startCount; | |
1339 repair = 1; | |
1340 } | |
1341 startCount += 1; | |
1342 | |
1343 startAbsolute -= 1; | |
1344 count -= 1; | |
1345 } | |
1346 | |
1347 // write | |
1348 if(repair) | |
1349 { | |
1350 actualPointerHeader = HEADERSTART + (0x1000 * startBackup%2); | |
1351 ef_write_block(data,0x1000,EF_HEADER, 0); | |
1352 } | |
1353 } | |
1354 releaseFrame(97,logCopyDataPtr); | |
1355 settingsGetPointer()->totalDiveCounter = startCount; | |
1356 } | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1357 */ |
38 | 1358 |
1359 // =============================================================================== | |
1360 // ext_flash_repair_dive_log | |
1361 /// @brief This function | |
1362 /// does set | |
1363 /// logFlashNextSampleStartAddress | |
1364 /// and | |
1365 /// lastDiveLogId | |
1366 /// | |
1367 void ext_flash_repair_dive_log(void) | |
1368 { | |
1369 uint8_t header1, header2; | |
1370 convert_Type dataStart; | |
1371 | |
1372 for(int id = 0; id < 255;id++) | |
1373 { | |
1374 actualAddress = HEADERSTART + (0x800 * id); | |
1375 ext_flash_read_block_start(); | |
1376 ext_flash_read_block(&header1, EF_HEADER); | |
1377 ext_flash_read_block(&header2, EF_HEADER); | |
1378 dataStart.u8bit.byteHigh = 0; | |
1379 ext_flash_read_block(&dataStart.u8bit.byteLow, EF_HEADER); | |
1380 ext_flash_read_block(&dataStart.u8bit.byteMidLow, EF_HEADER); | |
1381 ext_flash_read_block(&dataStart.u8bit.byteMidHigh, EF_HEADER); | |
1382 ext_flash_read_block_stop(); | |
425
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
1383 if((header1 == 0xFA) && (header2 == 0xFA)) /* Header is indicating the start of a dive */ |
38 | 1384 { |
1385 actualAddress = HEADERSTART + (0x800 * id) + HEADER2OFFSET; | |
1386 ext_flash_read_block_start(); | |
1387 ext_flash_read_block(&header1, EF_HEADER); | |
1388 ext_flash_read_block(&header2, EF_HEADER); | |
1389 ext_flash_read_block_stop(); | |
425
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
1390 if((header1 != 0xFA) || (header2 != 0xFA)) /* Secondary header was not written at the end of a dive */ |
38 | 1391 { |
425
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
1392 actualPointerSample = dataStart.u32bit; /* Set datapointer to position stored in header written at beginning of dive */ |
38 | 1393 actualAddress = actualPointerSample; |
1394 logbook_recover_brokenlog(id); | |
1395 SSettings *settings = settingsGetPointer(); | |
1396 settings->logFlashNextSampleStartAddress = actualPointerSample; | |
1397 } | |
1398 } | |
1399 } | |
1400 ext_flash_find_start(); | |
1401 } | |
1402 | |
1403 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1404 static void ext_flash_find_start(void) |
38 | 1405 { |
1406 uint8_t id; | |
1407 uint8_t header1, header2; | |
1408 convert_Type dataStart, dataEnd; | |
1409 | |
1410 for(id = 0; id < 255;id++) | |
1411 { | |
1412 actualAddress = HEADERSTART + (0x800 * id) + HEADER2OFFSET; | |
1413 ext_flash_read_block_start(); | |
1414 ext_flash_read_block(&header1, EF_HEADER); | |
1415 ext_flash_read_block(&header2, EF_HEADER); | |
1416 dataStart.u8bit.byteHigh = 0; | |
1417 ext_flash_read_block(&dataStart.u8bit.byteLow, EF_HEADER); | |
1418 ext_flash_read_block(&dataStart.u8bit.byteMidLow, EF_HEADER); | |
1419 ext_flash_read_block(&dataStart.u8bit.byteMidHigh, EF_HEADER); | |
1420 ext_flash_read_block_stop(); | |
1421 if((header1 == 0xFF) && (header2 == 0xFF)) | |
1422 { | |
1423 break; | |
1424 } | |
1425 } | |
1426 id--; | |
1427 SSettings *settings = settingsGetPointer(); | |
1428 settings->lastDiveLogId = id; | |
1429 | |
1430 actualAddress = HEADERSTART + (0x800 * id) + HEADER2OFFSET; | |
1431 actualPointerHeader = actualAddress; | |
1432 | |
1433 ext_flash_read_block_start(); | |
1434 | |
1435 ext_flash_read_block(&header1, EF_HEADER); | |
1436 ext_flash_read_block(&header2, EF_HEADER); | |
1437 dataStart.u8bit.byteHigh = 0; | |
1438 ext_flash_read_block(&dataStart.u8bit.byteLow, EF_HEADER); | |
1439 ext_flash_read_block(&dataStart.u8bit.byteMidLow, EF_HEADER); | |
1440 ext_flash_read_block(&dataStart.u8bit.byteMidHigh, EF_HEADER); | |
1441 dataEnd.u8bit.byteHigh = 0; | |
1442 ext_flash_read_block(&dataEnd.u8bit.byteLow, EF_HEADER); | |
1443 ext_flash_read_block(&dataEnd.u8bit.byteMidLow, EF_HEADER); | |
1444 ext_flash_read_block(&dataEnd.u8bit.byteMidHigh, EF_HEADER); | |
1445 ext_flash_read_block_stop(); | |
1446 | |
1447 //Find free space | |
1448 if((header1 == 0xFA) && (header2 == 0xFA)) | |
1449 { | |
1450 uint8_t uiRead = 0; | |
1451 int countFF = 0; | |
1452 //End of last complete dive | |
1453 actualPointerSample = dataEnd.u32bit ; | |
1454 actualAddress = actualPointerSample; | |
1455 //Check if there are samples of dives with less than half a minute | |
1456 while(true) | |
1457 { | |
1458 ext_flash_read_block_start(); | |
1459 ext_flash_read_block(&uiRead, EF_SAMPLE); | |
1460 if(uiRead == 0xFF) | |
1461 countFF++; | |
1462 else | |
1463 countFF = 0; | |
1464 | |
1465 | |
1466 | |
1467 if(countFF == 10) | |
1468 { | |
1469 actualAddress -= 10; | |
1470 break; | |
1471 } | |
1472 | |
1473 //New page: clear | |
1474 if(ext_flash_erase_if_on_page_start()) | |
1475 break; | |
1476 } | |
1477 // Set new start address | |
1478 actualPointerSample = actualAddress; | |
1479 settings->logFlashNextSampleStartAddress = actualPointerSample; | |
1480 } | |
1481 else | |
1482 { | |
1483 settings->logFlashNextSampleStartAddress = SAMPLESTART; | |
1484 } | |
1485 } | |
1486 | |
1487 | |
1488 #endif | |
1489 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1490 static void ext_flash_disable_protection(void) |
38 | 1491 { |
1492 /* | |
1493 extFlashStatusBit8_Type status; | |
1494 | |
1495 status.uw = 0; | |
1496 | |
1497 wait_chip_not_busy(); | |
1498 write_spi(0x50,RELEASE); // EWSR | |
1499 write_spi(0x01,HOLDCS); // WRSR | |
1500 write_spi(status.uw,RELEASE); // new status | |
1501 */ | |
1502 } | |
1503 | |
1504 | |
1505 void ext_flash_disable_protection_for_logbook(void) | |
1506 { | |
1507 /* | |
1508 extFlashStatusBit8_Type status; | |
1509 | |
1510 status.uw = 0; | |
1511 status.ub.BlockProtect0 = 1; | |
1512 status.ub.BlockProtect1 = 0; | |
1513 status.ub.BlockProtect2 = 1; | |
1514 status.ub.BlockProtect3 = 0; // not set in OSTC3. Why? | |
1515 | |
1516 wait_chip_not_busy(); | |
1517 write_spi(0x50,RELEASE); // EWSR | |
1518 write_spi(0x01,HOLDCS); // WRSR | |
1519 write_spi(status.uw,RELEASE); // new status | |
1520 */ | |
1521 } | |
1522 | |
1523 | |
1524 void ext_flash_enable_protection(void) | |
1525 { | |
1526 /* | |
1527 extFlashStatusBit8_Type status; | |
1528 | |
1529 status.uw = 0; | |
1530 status.ub.BlockProtect0 = 1; | |
1531 status.ub.BlockProtect1 = 1; | |
1532 status.ub.BlockProtect2 = 1; | |
1533 status.ub.BlockProtect3 = 1; // not set in OSTC3. Why? | |
1534 | |
1535 wait_chip_not_busy(); | |
1536 write_spi(0x50,RELEASE); // EWSR | |
1537 write_spi(0x01,HOLDCS); // WRSR | |
1538 write_spi(status.uw,RELEASE); // new status | |
1539 */ | |
1540 } | |
1541 | |
1542 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1543 /*void ext_flash_erase_chip(void) |
38 | 1544 { |
1545 wait_chip_not_busy(); | |
1546 write_spi(0x06,RELEASE); | |
1547 write_spi(0x60,RELEASE); | |
1548 wait_chip_not_busy(); | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1549 }*/ |
38 | 1550 |
1551 void ext_flash_erase_firmware(void) | |
1552 { | |
1553 uint32_t size, blocks_64k; | |
1554 | |
1555 actualAddress = FWSTART; | |
1556 size = 1 + FWSTOP - FWSTART; | |
1557 blocks_64k = size / 0x10000; | |
1558 ef_erase_64K(blocks_64k); | |
1559 } | |
1560 | |
1561 void ext_flash_erase_firmware2(void) | |
1562 { | |
1563 uint32_t size, blocks_64k; | |
1564 | |
1565 actualAddress = FWSTART2; | |
1566 size = 1 + FWSTOP2 - FWSTART2; | |
1567 blocks_64k = size / 0x10000; | |
1568 ef_erase_64K(blocks_64k); | |
1569 } | |
1570 | |
1571 | |
1572 | |
1573 void ext_flash_erase_logbook(void) | |
1574 { | |
1575 uint32_t size, blocks_64k; | |
1576 | |
1577 ext_flash_disable_protection_for_logbook(); | |
1578 | |
1579 actualAddress = SAMPLESTART; | |
1580 size = 1 + SAMPLESTOP - SAMPLESTART; | |
1581 blocks_64k = size / 0x10000; | |
1582 ef_erase_64K(blocks_64k); | |
1583 | |
1584 actualAddress = HEADERSTART; | |
1585 size = 1 + HEADERSTOP - HEADERSTART; | |
1586 blocks_64k = size / 0x10000; | |
1587 ef_erase_64K(blocks_64k); | |
1588 | |
1589 ext_flash_enable_protection(); | |
1590 } | |
1591 | |
1592 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1593 static void ext_flash_erase4kB(void) |
38 | 1594 { |
1595 wait_chip_not_busy(); | |
1596 write_spi(0x06,RELEASE);/* WREN */ | |
1597 write_spi(0x20,HOLDCS);/* sector erase cmd */ | |
1598 write_address(RELEASE); | |
1599 } | |
1600 | |
282 | 1601 /* be careful - might not work with entire family and other products |
38 | 1602 * see page 14 of LOGBOOK_V3_S25FS-S_00-271247.pdf |
1603 */ | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1604 static void ext_flash_erase32kB(void) |
38 | 1605 { |
1606 uint32_t actualAddress_backup; | |
1607 | |
1608 actualAddress_backup = actualAddress; | |
1609 actualAddress = 0; | |
1610 wait_chip_not_busy(); | |
1611 write_spi(0x06,RELEASE);/* WREN */ | |
1612 write_spi(0xD8,HOLDCS);/* sector erase cmd */ | |
1613 write_address(RELEASE); | |
1614 actualAddress = actualAddress_backup; | |
1615 } | |
1616 | |
1617 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1618 static void ext_flash_erase64kB(void) |
38 | 1619 { |
1620 wait_chip_not_busy(); | |
1621 write_spi(0x06,RELEASE);/* WREN */ | |
1622 write_spi(0xD8,HOLDCS);/* sector erase cmd */ | |
1623 write_address(RELEASE); | |
1624 } | |
1625 | |
1626 | |
1627 void ext_flash_read_block_start(void) | |
1628 { | |
1629 wait_chip_not_busy(); | |
1630 write_spi(0x03,HOLDCS); /* WREN */ | |
1631 write_address(HOLDCS); | |
1632 } | |
1633 | |
1634 /* 4KB, 32KB, 64 KB, not the upper 16 MB with 4 Byte address at the moment */ | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1635 static uint8_t ext_flash_erase_if_on_page_start(void) |
38 | 1636 { |
1637 if(actualAddress < 0x00008000) | |
1638 { | |
1639 /* 4K Byte is 0x1000 */ | |
1640 if((actualAddress & 0xFFF) == 0) | |
1641 { | |
1642 ext_flash_erase4kB(); | |
1643 return 1; | |
1644 } | |
423
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
1645 } |
38 | 1646 else |
1647 if(actualAddress < 0x00010000) | |
1648 { | |
1649 /* 32K Byte is only one page */ | |
1650 if(actualAddress == 0x00010000) | |
1651 { | |
1652 ext_flash_erase32kB(); | |
1653 return 1; | |
1654 } | |
423
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
1655 } |
38 | 1656 else |
1657 { | |
1658 /* 64K Byte is 0x10000 */ | |
1659 if((actualAddress & 0xFFFF) == 0) | |
1660 { | |
423
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
1661 if(preparedPageAddress == actualAddress) /* has page already been prepared before? (at the moment for samples only) */ |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
1662 { |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
1663 preparedPageAddress = 0; |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
1664 |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
1665 } |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
1666 else |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
1667 { |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
1668 ext_flash_erase64kB(); |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
1669 } |
38 | 1670 return 1; |
1671 } | |
423
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
1672 } |
a560afdaadbf
ext_Flash_write_sample erase sector optimization:
ideenmodellierer
parents:
421
diff
changeset
|
1673 |
38 | 1674 return 0; |
1675 } | |
1676 | |
1677 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1678 static void ext_flash_read_block(uint8_t *getByte, uint8_t type) |
38 | 1679 { |
1680 *getByte = read_spi(HOLDCS);/* read data */ | |
1681 ext_flash_incf_address(type); | |
1682 } | |
1683 | |
1684 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1685 static void ext_flash_read_block_multi(void *getByte, uint32_t size, uint8_t type) |
38 | 1686 { |
1687 uint8_t *data; | |
1688 data = getByte; | |
1689 | |
1690 for(uint32_t i=0;i<size;i++) | |
1691 { | |
1692 data[i] = read_spi(HOLDCS);/* read data */ | |
1693 ext_flash_incf_address(type); | |
1694 } | |
1695 } | |
1696 | |
1697 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1698 static void ext_flash_read_block_stop(void) |
38 | 1699 { |
1700 chip_unselect(); | |
1701 } | |
1702 | |
1703 | |
1704 /* Private functions ---------------------------------------------------------*/ | |
1705 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1706 static void ef_write_block(uint8_t * sendByte, uint32_t length, uint8_t type, uint8_t do_not_erase) |
38 | 1707 { |
1708 uint32_t remaining_page_size, remaining_length, remaining_space_to_ring_end; | |
420 | 1709 uint32_t i=0; |
1710 | |
38 | 1711 if(!length) |
1712 return; | |
1713 | |
1714 uint32_t ringStart, ringStop; | |
1715 | |
1716 switch(type) | |
1717 { | |
1718 case EF_HEADER: | |
1719 actualAddress = actualPointerHeader; | |
1720 ringStart = HEADERSTART; | |
1721 ringStop = HEADERSTOP; | |
1722 break; | |
1723 case EF_SAMPLE: | |
1724 actualAddress = actualPointerSample; | |
1725 ringStart = SAMPLESTART; | |
1726 ringStop = SAMPLESTOP; | |
1727 break; | |
1728 case EF_DEVICEDATA: | |
1729 actualAddress = actualPointerDevicedata; | |
1730 ringStart = DDSTART; | |
1731 ringStop = DDSTOP; | |
1732 break; | |
1733 case EF_VPMDATA: | |
1734 actualAddress = actualPointerVPM; | |
1735 ringStart = VPMSTART; | |
1736 ringStop = VPMSTOP; | |
1737 break; | |
1738 case EF_SETTINGS: | |
1739 actualAddress = actualPointerSettings; | |
1740 ringStart = SETTINGSSTART; | |
1741 ringStop = SETTINGSSTOP; | |
1742 break; | |
1743 case EF_FIRMWARE: | |
1744 actualAddress = actualPointerFirmware; | |
1745 ringStart = FWSTART; | |
1746 ringStop = FWSTOP; | |
1747 break; | |
1748 case EF_FIRMWARE2: | |
1749 actualAddress = actualPointerFirmware2; | |
1750 ringStart = FWSTART2; | |
1751 ringStop = FWSTOP2; | |
1752 break; | |
1753 default: | |
1754 ringStart = FLASHSTART; | |
1755 ringStop = FLASHSTOP; | |
1756 break; | |
1757 } | |
1758 /* safety */ | |
1759 if(actualAddress < ringStart) | |
1760 actualAddress = ringStart; | |
1761 | |
1762 if(do_not_erase == 0) | |
1763 ext_flash_erase_if_on_page_start(); | |
1764 | |
420 | 1765 while( i<length) |
38 | 1766 { |
1767 ef_hw_rough_delay_us(5); | |
1768 wait_chip_not_busy(); | |
1769 write_spi(0x06,RELEASE); /* WREN */ | |
1770 write_spi(0x02,HOLDCS); /* write cmd */ | |
1771 write_address(HOLDCS); | |
1772 | |
1773 remaining_length = length - i; | |
420 | 1774 remaining_page_size = 0xFF - (uint8_t)(actualAddress & 0xFF) +1; |
38 | 1775 remaining_space_to_ring_end = ringStop - actualAddress; |
1776 | |
420 | 1777 if(remaining_length >= 256) |
1778 { | |
1779 remaining_length = 255; /* up to 256 bytes may be written in one burst. Last byte is written with release */ | |
1780 } | |
1781 else | |
38 | 1782 { |
420 | 1783 remaining_length--; /* last byte needed for release */ |
1784 } | |
1785 if(remaining_length >= (remaining_page_size) ) /* use 256 byte page and calculate number of bytes left */ | |
1786 { | |
1787 remaining_length = remaining_page_size - 1; | |
1788 } | |
1789 if( (remaining_space_to_ring_end >= 256)) | |
1790 { | |
1791 for(int j=0; j<remaining_length; j++) | |
38 | 1792 { |
1793 write_spi(sendByte[i],HOLDCS);/* write data */ | |
1794 actualAddress++; | |
1795 i++; | |
1796 } | |
1797 } | |
1798 /* byte with RELEASE */ | |
1799 write_spi(sendByte[i],RELEASE);/* write data */ | |
1800 actualAddress++; | |
420 | 1801 i++; |
1802 | |
38 | 1803 if(actualAddress > ringStop) |
1804 actualAddress = ringStart; | |
420 | 1805 |
38 | 1806 if(do_not_erase == 0) |
1807 ext_flash_erase_if_on_page_start(); | |
1808 } | |
1809 switch(type) | |
1810 { | |
1811 case EF_HEADER: | |
1812 actualPointerHeader = actualAddress; | |
1813 break; | |
1814 case EF_SAMPLE: | |
1815 actualPointerSample = actualAddress; | |
1816 break; | |
1817 case EF_DEVICEDATA: | |
1818 actualPointerDevicedata = actualAddress; | |
1819 break; | |
1820 case EF_VPMDATA: | |
1821 actualPointerVPM = actualAddress; | |
1822 break; | |
1823 case EF_SETTINGS: | |
1824 actualPointerSettings = actualAddress; | |
1825 break; | |
1826 case EF_FIRMWARE: | |
1827 actualPointerFirmware = actualAddress; | |
1828 break; | |
1829 case EF_FIRMWARE2: | |
1830 actualPointerFirmware2 = actualAddress; | |
1831 break; | |
1832 default: | |
1833 break; | |
1834 } | |
1835 } | |
1836 | |
1837 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1838 static _Bool ext_flash_test_remaining_space_of_page_empty(uint32_t pointer, uint16_t length) |
38 | 1839 { |
1840 if((pointer & 0xFFF) == 0) | |
1841 return 1; | |
1842 | |
1843 uint32_t backup = actualAddress; | |
1844 uint8_t data; | |
1845 uint32_t size_to_page_end; | |
1846 | |
1847 size_to_page_end = 0x1000 - (pointer & 0xFFF); | |
1848 if(length > size_to_page_end) | |
1849 length = size_to_page_end; | |
1850 | |
1851 actualAddress = pointer; | |
1852 ext_flash_read_block_start(); | |
1853 | |
1854 for(uint16_t i = 0; i<length; i++) | |
1855 { | |
1856 ext_flash_read_block(&data, 255); // 255 = ENTIRE FLASH | |
1857 if(data != 0xFF) | |
1858 { | |
1859 ext_flash_read_block_stop(); | |
1860 actualAddress = backup; | |
1861 return 0; | |
1862 } | |
1863 } | |
1864 ext_flash_read_block_stop(); | |
1865 actualAddress = backup; | |
1866 return 1; | |
1867 } | |
1868 | |
1869 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1870 static void ext_flash_set_to_begin_of_next_page(uint32_t *pointer, uint8_t type) |
38 | 1871 { |
1872 uint32_t ringStart, ringStop; | |
1873 | |
1874 switch(type) | |
1875 { | |
1876 case EF_HEADER: | |
1877 ringStart = HEADERSTART; | |
1878 ringStop = HEADERSTOP; | |
1879 break; | |
1880 case EF_SAMPLE: | |
1881 ringStart = SAMPLESTART; | |
1882 ringStop = SAMPLESTOP; | |
1883 break; | |
1884 case EF_DEVICEDATA: | |
1885 ringStart = DDSTART; | |
1886 ringStop = DDSTOP; | |
1887 break; | |
1888 case EF_VPMDATA: | |
1889 ringStart = VPMSTART; | |
1890 ringStop = VPMSTOP; | |
1891 break; | |
1892 case EF_SETTINGS: | |
1893 ringStart = SETTINGSSTART; | |
1894 ringStop = SETTINGSSTOP; | |
1895 break; | |
1896 default: | |
1897 ringStart = FLASHSTART; | |
1898 ringStop = FLASHSTOP; | |
1899 break; | |
1900 } | |
1901 | |
1902 *pointer = (*pointer & 0xFFF) + 0x1000; | |
1903 | |
1904 if((*pointer < ringStart) || (*pointer >= ringStop)) | |
1905 *pointer = ringStart; | |
1906 } | |
1907 | |
1908 | |
1909 static void ef_erase_64K(uint32_t blocks) | |
1910 { | |
1911 for(uint32_t i = 0; i < blocks; i++) | |
1912 { | |
1913 wait_chip_not_busy(); | |
1914 write_spi(0x06,RELEASE);/* WREN */ | |
1915 write_spi(0xD8,HOLDCS);/* 64k erase cmd */ | |
1916 write_address(RELEASE); | |
1917 actualAddress += 0x10000; | |
1918 HAL_Delay(25); | |
1919 } | |
1920 } | |
1921 | |
1922 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1923 static void chip_unselect(void) |
38 | 1924 { |
1925 HAL_GPIO_WritePin(EXTFLASH_CSB_GPIO_PORT,EXTFLASH_CSB_PIN,GPIO_PIN_SET); // chip select | |
1926 } | |
1927 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1928 static void chip_select(void) |
38 | 1929 { |
1930 HAL_GPIO_WritePin(EXTFLASH_CSB_GPIO_PORT,EXTFLASH_CSB_PIN,GPIO_PIN_RESET); // chip select | |
1931 } | |
1932 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1933 static void error_led_on(void) |
38 | 1934 { |
1935 HAL_GPIO_WritePin(OSCILLOSCOPE_GPIO_PORT,OSCILLOSCOPE_PIN,GPIO_PIN_SET); | |
1936 } | |
1937 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1938 static void error_led_off(void) |
38 | 1939 { |
1940 HAL_GPIO_WritePin(OSCILLOSCOPE_GPIO_PORT,OSCILLOSCOPE_PIN,GPIO_PIN_RESET); | |
1941 } | |
1942 | |
1943 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1944 static uint8_t read_spi(uint8_t unselect_CS_afterwards) |
38 | 1945 { |
1946 uint8_t byte; | |
1947 | |
1948 chip_select(); | |
1949 | |
1950 if(HAL_SPI_Receive(&hspiDisplay, &byte, 1, 10000) != HAL_OK) | |
1951 Error_Handler_extflash(); | |
1952 | |
1953 while (HAL_SPI_GetState(&hspiDisplay) != HAL_SPI_STATE_READY) | |
1954 { | |
1955 } | |
1956 if(unselect_CS_afterwards) | |
1957 chip_unselect(); | |
1958 | |
1959 return byte; | |
1960 } | |
1961 | |
1962 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1963 static void write_spi(uint8_t data, uint8_t unselect_CS_afterwards) |
38 | 1964 { |
1965 chip_select(); | |
1966 | |
1967 if(HAL_SPI_Transmit(&hspiDisplay, &data, 1, 10000) != HAL_OK) | |
1968 Error_Handler_extflash(); | |
1969 | |
1970 while (HAL_SPI_GetState(&hspiDisplay) != HAL_SPI_STATE_READY) | |
1971 { | |
1972 } | |
1973 if(unselect_CS_afterwards) | |
1974 chip_unselect(); | |
1975 } | |
1976 | |
1977 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
1978 static void write_address(uint8_t unselect_CS_afterwards) |
38 | 1979 { |
1980 uint8_t hi, med ,lo; | |
1981 | |
1982 hi = (actualAddress >> 16) & 0xFF; | |
1983 med = (actualAddress >> 8) & 0xFF; | |
1984 lo = actualAddress & 0xFF; | |
1985 | |
1986 write_spi(hi, HOLDCS); | |
1987 write_spi(med, HOLDCS); | |
1988 write_spi(lo, unselect_CS_afterwards); | |
1989 } | |
1990 | |
1991 | |
1992 static void wait_chip_not_busy(void) | |
1993 { | |
1994 uint8_t status; | |
1995 | |
1996 chip_unselect(); | |
1997 | |
1998 write_spi(0x05,HOLDCS); /* RDSR */ | |
1999 status = read_spi(HOLDCS);/* read status */ | |
2000 while(status & 0x01) | |
2001 { | |
2002 HAL_Delay(1); | |
2003 status = read_spi(HOLDCS);/* read status */ | |
2004 } | |
2005 chip_unselect(); | |
2006 } | |
2007 | |
2008 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
2009 static void ext_flash_incf_address(uint8_t type) |
38 | 2010 { |
2011 uint32_t ringStart, ringStop; | |
2012 | |
2013 actualAddress += 1; | |
2014 | |
2015 switch(type) | |
2016 { | |
2017 case EF_HEADER: | |
2018 ringStart = HEADERSTART; | |
2019 ringStop = HEADERSTOP; | |
2020 break; | |
2021 case EF_SAMPLE: | |
2022 ringStart = SAMPLESTART; | |
2023 ringStop = SAMPLESTOP; | |
2024 break; | |
2025 case EF_DEVICEDATA: | |
2026 ringStart = DDSTART; | |
2027 ringStop = DDSTOP; | |
2028 break; | |
2029 case EF_VPMDATA: | |
2030 ringStart = VPMSTART; | |
2031 ringStop = VPMSTOP; | |
2032 break; | |
2033 case EF_SETTINGS: | |
2034 ringStart = SETTINGSSTART; | |
2035 ringStop = SETTINGSSTOP; | |
2036 break; | |
2037 case EF_FIRMWARE: | |
2038 ringStart = FWSTART; | |
2039 ringStop = FWSTOP; | |
2040 break; | |
2041 case EF_FIRMWARE2: | |
2042 ringStart = FWSTART2; | |
2043 ringStop = FWSTOP2; | |
2044 break; | |
2045 default: | |
2046 ringStart = FLASHSTART; | |
2047 ringStop = FLASHSTOP; | |
2048 break; | |
2049 } | |
2050 | |
2051 if((actualAddress < ringStart) || (actualAddress > ringStop)) | |
2052 actualAddress = ringStart; | |
2053 } | |
2054 | |
2055 | |
268
1b9847d40e81
cleanup: make things static where possible.
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
2056 static void ext_flash_decf_address_ring(uint8_t type) |
38 | 2057 { |
2058 uint32_t ringStart, ringStop; | |
2059 | |
2060 switch(type) | |
2061 { | |
2062 case EF_HEADER: | |
2063 ringStart = HEADERSTART; | |
2064 ringStop = HEADERSTOP; | |
2065 break; | |
2066 case EF_SAMPLE: | |
2067 ringStart = SAMPLESTART; | |
2068 ringStop = SAMPLESTOP; | |
2069 break; | |
2070 case EF_DEVICEDATA: | |
2071 ringStart = DDSTART; | |
2072 ringStop = DDSTOP; | |
2073 break; | |
2074 case EF_VPMDATA: | |
2075 ringStart = VPMSTART; | |
2076 ringStop = VPMSTOP; | |
2077 break; | |
2078 case EF_SETTINGS: | |
2079 ringStart = SETTINGSSTART; | |
2080 ringStop = SETTINGSSTOP; | |
2081 break; | |
2082 case EF_FIRMWARE: | |
2083 ringStart = FWSTART; | |
2084 ringStop = FWSTOP; | |
2085 break; | |
2086 case EF_FIRMWARE2: | |
2087 ringStart = FWSTART2; | |
2088 ringStop = FWSTOP2; | |
2089 break; | |
2090 default: | |
2091 ringStart = FLASHSTART; | |
2092 ringStop = FLASHSTOP; | |
2093 break; | |
2094 } | |
2095 | |
2096 if((actualAddress <= ringStart) || (actualAddress > ringStop)) | |
2097 actualAddress = ringStop; | |
2098 else | |
2099 actualAddress -= 1; | |
2100 } | |
2101 | |
2102 | |
2103 static void ef_hw_rough_delay_us(uint32_t delayUs) | |
2104 { | |
2105 if(!delayUs) | |
2106 return; | |
2107 delayUs*= 12; | |
2108 while(delayUs--); | |
2109 return; | |
2110 } | |
2111 | |
2112 static void Error_Handler_extflash(void) | |
2113 { | |
2114 while(1) | |
2115 { | |
2116 } | |
2117 } | |
425
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2118 |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2119 void ext_flash_CloseSector(void) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2120 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2121 uint32_t actualAddressBackup = actualAddress; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2122 int i=0; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2123 |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2124 if(closeSectorAddress != 0) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2125 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2126 /* write some dummy bytes to the sector which is currently used for storing samples. This is done to "hide" problem if function is calles again */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2127 actualAddress = closeSectorAddress; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2128 |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2129 wait_chip_not_busy(); |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2130 write_spi(0x06,RELEASE); /* WREN */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2131 write_spi(0x02,HOLDCS); /* write cmd */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2132 write_address(HOLDCS); |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2133 for(i = 0; i<8; i++) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2134 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2135 write_spi(0xA5,HOLDCS);/* write data */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2136 actualAddress++; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2137 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2138 /* byte with RELEASE */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2139 write_spi(0xA5,RELEASE);/* write data */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2140 actualAddress = actualAddressBackup; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2141 closeSectorAddress = 0; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2142 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2143 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2144 |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2145 uint32_t ext_flash_AnalyseSampleBuffer(char *pstrResult) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2146 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2147 uint8_t sectorState[16]; /* samples are stored in 16 sector / 64k each */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2148 uint32_t curAddress = SAMPLESTART; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2149 uint8_t curSector = 0; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2150 uint8_t samplebuffer[10]; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2151 uint32_t actualAddressBackup = actualAddress; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2152 uint8_t emptyCellCnt = 0; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2153 uint32_t i = 0; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2154 uint8_t startedSectors = 0; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2155 uint8_t lastSectorInuse = 0; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2156 |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2157 /* check if a sector is used till its end */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2158 for(curSector = 0; curSector < 16; curSector++) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2159 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2160 sectorState[curSector] = 0; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2161 emptyCellCnt = 0; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2162 curAddress = SAMPLESTART + (curSector * 0x10000); /* set address to begin of sector and check if it is used */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2163 actualAddress = curAddress; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2164 ext_flash_read_block_start(); |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2165 for(uint32_t i=0;i<10;i++) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2166 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2167 samplebuffer[i] = read_spi(HOLDCS);/* read data */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2168 if(samplebuffer[i] == 0xFF) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2169 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2170 emptyCellCnt++; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2171 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2172 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2173 ext_flash_read_block_stop(); |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2174 if(emptyCellCnt == 10) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2175 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2176 sectorState[curSector] = SECTOR_NOTUSED; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2177 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2178 emptyCellCnt = 0; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2179 curAddress = SAMPLESTART + (curSector * 0x10000) + 0xFFF5; /* set address to end of sector and check if it is used */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2180 actualAddress = curAddress; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2181 ext_flash_read_block_start(); |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2182 for(i=0;i<10;i++) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2183 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2184 samplebuffer[i] = read_spi(HOLDCS);/* read data */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2185 if(samplebuffer[i] == 0xFF) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2186 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2187 emptyCellCnt++; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2188 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2189 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2190 ext_flash_read_block_stop(); |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2191 if(emptyCellCnt == 10) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2192 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2193 sectorState[curSector] |= SECTOR_INUSE; /* will become SECTOR_EMPTY if start is NOTUSED */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2194 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2195 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2196 |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2197 for(i=0;i<16;i++) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2198 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2199 if( sectorState[i] == SECTOR_INUSE) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2200 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2201 startedSectors++; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2202 lastSectorInuse = i; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2203 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2204 *(pstrResult+i) = sectorState[i] + 48; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2205 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2206 |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2207 if(startedSectors > 1) /* more than one sector is in used => ring buffer corrupted */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2208 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2209 if(startedSectors == 2) /* only fix issue if only two sectors are in used. Otherwise fixing will cause more worries than help */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2210 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2211 /* the logic behind healing of the problem is that the larger address is the oldest one => restore the largest address */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2212 curAddress = SAMPLESTART + (lastSectorInuse * 0x10000); |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2213 emptyCellCnt = 0; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2214 actualAddress = curAddress; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2215 ext_flash_read_block_start(); |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2216 while((emptyCellCnt < 10) && (actualAddress < curAddress + 0x10000)) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2217 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2218 samplebuffer[0] = read_spi(HOLDCS);/* read data */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2219 if(samplebuffer[0] == 0xFF) |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2220 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2221 emptyCellCnt++; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2222 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2223 else |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2224 { |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2225 emptyCellCnt = 0; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2226 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2227 actualAddress++; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2228 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2229 ext_flash_read_block_stop(); |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2230 actualAddress -= 10; /* step 10 bytes back to the start of free bytes */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2231 actualPointerSample = actualAddress; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2232 |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2233 closeSectorAddress = settingsGetPointer()->logFlashNextSampleStartAddress & 0xFFFF0000; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2234 closeSectorAddress += 0xFFF5; /* to be used once next time a dive is logged. Needed because NextSampleID is derived at every startup */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2235 settingsGetPointer()->logFlashNextSampleStartAddress = actualPointerSample; /* store new position to be used for next dive */ |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2236 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2237 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2238 actualAddress = actualAddressBackup; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2239 *(pstrResult+i) = 0; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2240 return startedSectors; |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2241 } |
86fcac4cc43a
Added function to analyse the sampel ringbuffer:
ideenmodellierer
parents:
423
diff
changeset
|
2242 |
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
2243 /* |
38 | 2244 uint8_t ext_flash_erase_firmware_if_not_empty(void) |
2245 { | |
2246 const uint8_t TESTSIZE_FW = 4; | |
2247 | |
2248 uint8_t data[TESTSIZE_FW]; | |
2249 uint8_t notEmpty = 0; | |
2250 | |
2251 actualAddress = FWSTART; | |
2252 ext_flash_read_block_start(); | |
2253 for(int i = 0; i < TESTSIZE_FW; i++) | |
2254 { | |
2255 ext_flash_read_block(&data[i], EF_FIRMWARE); | |
2256 if(data[i] != 0xFF) | |
2257 notEmpty = 1; | |
2258 } | |
2259 ext_flash_read_block_stop(); | |
2260 | |
2261 if(notEmpty) | |
2262 { | |
2263 ext_flash_erase_firmware(); | |
2264 return 1; | |
2265 } | |
2266 else | |
2267 return 0; | |
2268 } | |
2269 | |
2270 uint8_t ext_flash_erase_firmware2_if_not_empty(void) | |
2271 { | |
2272 const uint8_t TESTSIZE_FW = 4; | |
2273 | |
2274 uint8_t data[TESTSIZE_FW]; | |
2275 uint8_t notEmpty = 0; | |
2276 | |
2277 actualAddress = FWSTART2; | |
2278 ext_flash_read_block_start(); | |
2279 for(int i = 0; i < TESTSIZE_FW; i++) | |
2280 { | |
2281 ext_flash_read_block(&data[i], EF_FIRMWARE2); | |
2282 if(data[i] != 0xFF) | |
2283 notEmpty = 1; | |
2284 } | |
2285 ext_flash_read_block_stop(); | |
2286 | |
2287 if(notEmpty) | |
2288 { | |
2289 ext_flash_erase_firmware2(); | |
2290 return 1; | |
2291 } | |
2292 else | |
2293 return 0; | |
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
2294 }*/ |