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