-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathsettings.c
More file actions
1218 lines (1078 loc) · 49.7 KB
/
settings.c
File metadata and controls
1218 lines (1078 loc) · 49.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* settings.c
*
* Created on: Jan 12, 2021
* Author: David Original work by Jose Barros (PTDreamer), 2017
*/
#include "settings.h"
#include "pid.h"
#include "iron.h"
#include "gui.h"
#include "display.h"
#include "tempsensors.h"
#include "main.h"
#ifdef __BASE_FILE__
#undef __BASE_FILE__
#endif
#define __BASE_FILE__ "settings.c"
const systemSettings_t defaultSystemSettings = {
.version = SYSTEM_SETTINGS_VERSION,
#ifdef ST7565
.contrastOrBrightness = 34,
#else
.contrastOrBrightness = 255,
#endif
.dim_mode = dim_always,
.dim_Timeout = 10000, // ms
.dim_inSleep = enable,
.displayStartColumn = DISPLAY_START_COLUMN,
.displayStartLine = DISPLAY_START_LINE,
.displayXflip = 1,
#ifdef SSD1306
.displayClk = 0xF0,
.displayVcom = 0x44,
.displayPrecharge = 0x3C,
.displayYflip = 1,
#elif defined ST7565
.displayYflip = 0,
.displayResRatio = 5, // For ST7565 only
#endif
.guiUpdateDelay = 200, // ms
.guiTempDenoise = 5, // ±5°C
.tempUnit = mode_Celsius,
.tempStep = 5, // 5º steps
.tempBigStep = 20, // 20º big steps
.activeDetection = true,
.hasBattery = false,
.lvp = 110, // 11.0V Low voltage
.initMode = mode_sleep, // Safer to boot in sleep mode by default!
.buzzerMode = disable,
.beepDuration = 500,
.buttonWakeMode = wake_all,
.shakeWakeMode = wake_all,
.EncoderMode = RE_Mode_Forward,
.debugEnabled = disable,
.language = lang_english,
};
#ifdef ENABLE_ADDONS
const addonSettings_t defaultAddons = {
.version = ADDONS_SETTINGS_VERSION,
.enabledAddons = 0
#ifdef ENABLE_ADDON_FUME_EXTRACTOR
+ 0b1
#endif
#ifdef ENABLE_ADDON_SWITCH_OFF_REMINDER
+ 0b10
#endif
,
#ifdef ENABLE_ADDON_FUME_EXTRACTOR
.fumeExtractorMode = fume_extractor_mode_auto,
.fumeExtractorAfterrun = 2,
#endif
#ifdef ENABLE_ADDON_SWITCH_OFF_REMINDER
.swOffReminderEnabled = disable,
.swOffReminderInactivityDelay = 30,
.swOffReminderBeepType = switch_off_reminder_short_beep,
.swOffReminderPeriod = 5,
#endif
};
#endif
const profile_settings_t defaultProfileSettings = {
.calADC_At_0 = 0,
.tipFilter.coefficient = 75, // % of old data (more %, more filtering)
.tipFilter.threshold = 50,
.tipFilter.min = 50, // Don't go below x% when decreasing after exceeding threshold limits
.tipFilter.count_limit = 0,
.tipFilter.step = -3, // -5% less everytime the reading diff exceeds threshold_limit and the counter is greater than count_limit
.tipFilter.reset_threshold = 600, // Any diff over 500 reset the filter (Tip removed or connected)
#ifdef USE_NTC
.ntc.enabled = enable,
#ifdef PULLUP
.ntc.pullup = 1,
#elif defined PULLDOWN
.ntc.pullup = 0,
#else
#error NO PULL MODE DEFINED
#endif
.ntc.detection = 0,
.ntc.NTC_res = NTC_RES/100,
.ntc.NTC_beta = NTC_BETA,
.ntc.high_NTC_res = 1000, // 100.0K
.ntc.low_NTC_res = 100, // 10.0K
.ntc.high_NTC_beta = NTC_BETA,
.ntc.low_NTC_beta = NTC_BETA,
.ntc.pull_res = PULL_RES/100,
#else
.ntc.enabled = 0,
#endif
.errorTimeout = 500, // ms
.errorResumeMode = error_resume,
.sleepTimeout = (uint32_t)5*60000, // ms
.standbyTimeout = (uint32_t)5*60000,
.standbyTemperature = 180,
.MaxSetTemperature = 450,
.MinSetTemperature = 180,
.boostTimeout = 60000, // ms
.boostTemperature = 50,
.coldBoostEnabled = true,
.coldBoostTimeout = 10000, // ms
.coldBoostTemperature = 150,
.pwmMul = 1,
.readPeriod = (200*200)-1, // 200ms * 200 because timer period is 5us
.readDelay = (20*200)-1, // 20ms (Also uses 5us clock)
.tempUnit = mode_Celsius,
.shakeFiltering = disable,
.WakeInputMode = mode_shake,
.smartActiveEnabled = disable,
.smartActiveLoad = 30,
.standDelay = 0,
.StandMode = mode_sleep,
.version = PROFILE_SETTINGS_VERSION,
};
const tipData_t defaultTipData[NUM_PROFILES] = {
[profile_T12] = {
.calADC_At_250 = T12_Cal250,
.calADC_At_400 = T12_Cal400, // These values are way lower, but better to be safe than sorry
.PID.Kp = 4500, // val = /1.000.000
.PID.Ki = 1500, // val = /1.000.000
.PID.Kd = 600, // val = /1.000.000
.PID.maxI = 70, // val = /100
.PID.minI = 0, // val = /100
.name = "T12-", // Put some generic name
},
[profile_C245] = {
.calADC_At_250 = C245_Cal250,
.calADC_At_400 = C245_Cal400,
.PID.Kp = 4500, // val = /1.000.000
.PID.Ki = 1500, // val = /1.000.000
.PID.Kd = 600, // val = /1.000.000
.PID.maxI = 70, // val = /100
.PID.minI = 0,
.name = "C245-",
},
[profile_C210] = {
.calADC_At_250 = C210_Cal250,
.calADC_At_400 = C210_Cal400,
.PID.Kp = 4500, // val = /1.000.000
.PID.Ki = 1500, // val = /1.000.000
.PID.Kd = 600, // val = /1.000.000
.PID.maxI = 70, // val = /100
.PID.minI = 0,
.name = "C210-",
},
};
const char * defaultTipName[NUM_PROFILES] = { "T12-BC3", "C245-963", "C210-018" };
__attribute__((section(".globalSettings"))) flashSettings_t flashGlobalSettings;
__attribute__((section(".tempSettings"))) temp_settings_t flashTempSettings;
__attribute__((section(".tips"))) flashTipSlot_t flashTips[3];
settings_t settings;
static flashTemp_t flashTemp;
static uint8_t flashPages_GlobalSettings, flashPages_TempSettings, flashPages_TipSlot;
static uint16_t flashPageSize;
static void checksumError(uint8_t mode);
static void Button_reset(void);
static void ErrCountDown(uint8_t Start,uint8_t xpos, uint8_t ypos);
static uint32_t ChecksumSystemSettings(systemSettings_t* system);
static uint32_t ChecksumProfileSettings(profile_settings_t* profile_settings);
static uint32_t ChecksumTipData(flashTipData_t* tipData);
static void resetSystemSettings(systemSettings_t * system) ;
static void resetProfileSettings(profile_settings_t * data, uint8_t profile);
static void sortTips(flashTipData_t * data, uint8_t numberOfTips);
static void flashTempSettingsErase(void);
static void flashTempSettingsInit(void);
static void flashTempWrite(void);
static void flashTipsWrite(void);
static void flashProfileWrite(void);
static void loadSettingsFromBackupRam(void);
static uint32_t ChecksumBackupRam(void);
static void readBackupRam(void);
static void writeBackupRam(void);
#ifdef ENABLE_ADDONS
static void resetAddonSettings(addonSettings_t *addons);
static uint32_t ChecksumAddons(addonSettings_t* addonSettings);
#endif
static backupRamData_t bkpRamData;
#ifdef ENABLE_DBG_SAVE
uint8_t save_prevstate_debug, save_laststate_debug;
#define DBG_SAVE(n) save_prevstate_debug = save_laststate_debug;save_laststate_debug=n
#else
#define DBG_SAVE(n)
#endif
void updateTempData(bool force){
uint32_t CurrentTime = HAL_GetTick();
uint8_t scr_index=current_screen->index;
if(getIronCalibrationMode() || getSettings()->setupMode || scr_index == screen_debug) // Don't update while calibration is in progress or in the debug screen
return;
// Always update the data, whether the batery option is enabled or not
if(getSystemSettings()->hasBattery == true){
bkpRamData.values.lastSelTip[getCurrentProfile()] = getCurrentTip();
bkpRamData.values.lastProfile = getCurrentProfile();
bkpRamData.values.lastTipTemp[getCurrentProfile()] = getUserTemperature();
writeBackupRam();
}
else if ((getSystemSettings()->hasBattery == false) || force){ // No battery or forced
uint16_t currentTemp = getUserTemperature();
uint8_t currentTip = getCurrentTip();
uint8_t currentProfile = getCurrentProfile();
if(force){
flashTemp.prevTemp = currentTemp;
flashTemp.prevTip[currentProfile] = currentTip;
flashTemp.prevProfile = currentProfile;
}
if(flashTemp.temp != currentTemp) { // Compare with stored in flash
if(flashTemp.prevTemp != currentTemp) { // Store if different to last check
flashTemp.prevTemp = currentTemp;
flashTemp.tempCheckTime = CurrentTime; // Start timeout
}
if(force || ((CurrentTime-flashTemp.tempCheckTime)>TEMPSETTINGS_SAVE_TIME)) { // Different than flash and timeout is over
flashTemp.temp = currentTemp;
flashTempWrite(); // Update temperature in flash
}
}
if(flashTemp.tip[currentProfile] != currentTip) {
if(flashTemp.prevTip[currentProfile] != currentTip) { // Store if different to last check
flashTemp.prevTip[currentProfile] = currentTip;
flashTemp.tipCheckTime = CurrentTime; // Start timeout
}
if(force || ((CurrentTime-flashTemp.tipCheckTime)>4999)) { // Different than flash and timeout is over
for(uint8_t i=0;i<NUM_PROFILES;i++){ // Update all tips
flashTemp.tip[i] = flashTemp.prevTip[i];
}
flashTipsWrite(); // Update tip data in flash
}
}
if(flashTemp.profile != currentProfile) {
if(flashTemp.prevProfile != currentProfile) { // Store if different to last check
flashTemp.prevProfile = currentProfile;
flashTemp.profileCheckTime = CurrentTime; // Start timeout
}
if(force || ((CurrentTime-flashTemp.profileCheckTime)>4999)) { // Different than flash and timeout is over
flashTemp.profile = currentProfile;
flashProfileWrite(); // Update tip data in flash
}
}
}
}
static void eraseFlashPages(uint32_t pageAddress, uint32_t numPages, uint8_t verify)
{
uint32_t error = 0;
FLASH_EraseInitTypeDef erase = {0};
uint32_t _irq = __get_PRIMASK();
__disable_irq();
configurePWMpin(output_Low);
HAL_FLASH_Unlock();
__set_PRIMASK(_irq);
erase.NbPages = numPages;
erase.PageAddress = pageAddress;
erase.TypeErase = FLASH_TYPEERASE_PAGES;
HAL_IWDG_Refresh(&hiwdg);
if((HAL_FLASHEx_Erase(&erase, &error)!=HAL_OK) || (error!=0xFFFFFFFF)){
Error_Handler();
}
HAL_FLASH_Lock();
if(verify){
for (uint32_t i = 0u; i < (numPages * (flashPageSize / sizeof(int32_t))); i++) { // Ensure flash was erased
if( *((uint32_t*)pageAddress+i) != 0xFFFFFFFF){
Error_Handler();
}
}
}
}
static void writeFlash(uint32_t* src, uint32_t len, uint32_t dstAddr)
{
uint32_t numWordsToWrite = (len + sizeof(uint32_t) - 1u) / sizeof(uint32_t);
uint32_t* srcData = src;
uint32_t _irq = __get_PRIMASK();
__disable_irq();
HAL_FLASH_Unlock();
__set_PRIMASK(_irq);
// written = number of 32-bit values written
for(uint32_t written=0; written < numWordsToWrite; written++){
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, dstAddr, *srcData ) != HAL_OK)
{
Error_Handler();
}
dstAddr += sizeof(uint32_t); // increase pointers
srcData++;
}
HAL_FLASH_Lock();
}
static void resetflashTipData(flashTipData_t *tipData, uint8_t profile){
tipData->tip[0] = defaultTipData[profile]; // Load default tip
strcpy(tipData->tip[0].name, defaultTipName[profile]); // Load default tip name
tipData->currentNumberOfTips = 1; // Set current tips to 1
tipData->version = TIP_SETTINGS_VERSION;
}
void saveTip(uint8_t save_mode, uint8_t tip_index){
#ifndef DEBUG
struct mallinfo mi = mallinfo();
#endif
if(mi.uordblks > 128) // Check that heap usage is low
Error_Handler(); // We got there from a working screen (Heap must be free)
uint8_t profile = getCurrentProfile();
flashTipSlot_t* flashBufferTipBlock = _malloc(sizeof(flashTipSlot_t));
if((profile>=NUM_PROFILES) || (getProfileSettings()->ID != profile )) // Sanity check
Error_Handler();
if(flashBufferTipBlock == NULL)
Error_Handler();
for(uint8_t i=0; i<NUM_PROFILES; i++){
if( ((save_mode == reset_tips) && (i == profile)) || (save_mode == reset_allTips) || // Reset current tip slot, reset all tips
(ChecksumTipData(&flashTips[i].data) != flashTips[i].crc)){ // Perform check in others and reset if wrong
resetflashTipData(&flashBufferTipBlock->data, i);
flashBufferTipBlock->crc = ChecksumTipData(&flashBufferTipBlock->data);
DBG_SAVE(i+1);
eraseFlashPages((uint32_t)&flashTips[i], flashPages_TipSlot, 1);
DBG_SAVE(i+2);
writeFlash((uint32_t*)flashBufferTipBlock, sizeof(flashTipSlot_t), (uint32_t)&flashTips[i]);
}
}
if(save_mode == perform_scanFix || save_mode == reset_tips || save_mode == reset_allTips){
_free(flashBufferTipBlock);
return;
}
flashBufferTipBlock->data = flashTips[profile].data; // Backup flash tips
if(save_mode == save_tip){
flashBufferTipBlock->data.tip[tip_index] = *getCurrentTipData(); // Save current tip
}
else if(save_mode == add_tip){
flashBufferTipBlock->data.tip[flashBufferTipBlock->data.currentNumberOfTips++] = *getCurrentTipData(); // Add new tip, increase count
}
else if(save_mode == delete_tip){ // Delete tip
for(uint8_t i=tip_index; i<getCurrentNumberOfTips()-1; i++) // Overwrite selected tip with the next one, move the rest one position backwards
flashBufferTipBlock->data.tip[i] = flashBufferTipBlock->data.tip[i+1];
flashBufferTipBlock->data.currentNumberOfTips--;
}
else
Error_Handler(); // Save mode = Save_Tip, but tip_modenot defined, something went wrong}
sortTips(&flashBufferTipBlock->data, flashBufferTipBlock->data.currentNumberOfTips); // Sort tips
flashBufferTipBlock->crc = ChecksumTipData(&flashBufferTipBlock->data);
DBG_SAVE(7);
eraseFlashPages((uint32_t)&flashTips[profile], flashPages_TipSlot, 1);
DBG_SAVE(8);
writeFlash((uint32_t*)flashBufferTipBlock, sizeof(flashTipSlot_t), (uint32_t)&flashTips[profile]);
uint32_t flashChecksum, ramChecksum;
// Check flash and profile have same checksum
ramChecksum = ChecksumTipData(&flashBufferTipBlock->data);
flashChecksum = ChecksumTipData(&flashTips[profile].data);
if( (flashChecksum != ramChecksum) ||
(flashChecksum != flashTips[profile].crc)){
Error_Handler();
}
_free(flashBufferTipBlock);
DBG_SAVE(0);
loadTipDataFromFlash(getCurrentTip()); // Reload tip, sortTips will have updated the number to keep the same tip, or the new one
}
static void sortTips(flashTipData_t * data, uint8_t numberOfTips){
uint8_t min;
char current[TIP_LEN+1];
tipData_t backupTip;
strcpy (current, getCurrentTipData()->name); // Copy tip name being used in the system
for(uint8_t j=0; j<numberOfTips; j++){ // Sort alphabetically
min = j; // Min is start position
for(uint8_t i=j+1; i<numberOfTips; i++){
if(strcmp(data->tip[min].name, data->tip[i].name) > 0) // If Tip[Min] > Tip[i]
min = i; // Update min
}
if(min!=j){ // If j is not the min
backupTip = data->tip[j]; // Swap j and min
data->tip[j] = data->tip[min];
data->tip[min] = backupTip;
}
}
setCurrentTip(0); // Just in case it's not found
for(uint8_t i=0; i<numberOfTips; i++){ // Find the same tip after sorting
if(strcmp(current, data->tip[i].name) == 0){ // If matching name
setCurrentTip(i); // Update the tip to be reloaded later
break;
}
}
}
void saveSettings(uint8_t save_mode, uint8_t reboot_mode){
if(reboot_mode == do_reboot) // Force safe save_mode (disable iron power) if rebooting.
setIronSafeMode(enable);
uint8_t needs_saving = save_mode;
uint32_t _irq = __get_PRIMASK();
if( ((save_mode & save_settings) && (save_mode & reset_settings)) || // Sanity check
((save_mode & save_profile) && (save_mode & reset_profile)) ||
((save_mode & save_profile) && (save_mode & reset_profiles)) ||
((save_mode & save_addons) && (save_mode & reset_addons)) ){
Error_Handler();
}
uint8_t profile = getCurrentProfileID();
if((profile>=NUM_PROFILES) || (getProfileSettings()->ID != profile )) // Sanity check
Error_Handler();
if(save_mode == reset_all)
saveTip(reset_allTips, 0);
flashSettings_t* flashBufferSettings = _malloc(sizeof(flashSettings_t));
// check if malloc succeeded or not
if(flashBufferSettings == NULL)
Error_Handler();
while(ADC_Status != ADC_Idle);
__disable_irq();
getSettings()->isSaving = 1;
configurePWMpin(output_Low);
__set_PRIMASK(_irq);
#ifdef ENABLE_ADDONS // ADDONS
if((save_mode&(save_addons | reset_addons)) == 0){ // No addon save_mode specified
if( (ChecksumAddons(&flashGlobalSettings.addons) != flashGlobalSettings.addonsChecksum) || // Check existing flash data is valid
(flashGlobalSettings.addons.version) != ADDONS_SETTINGS_VERSION ){
resetAddonSettings(&flashBufferSettings->addons); // Load defaults if wrong
needs_saving=1;
}
else
flashBufferSettings->addons = flashGlobalSettings.addons; // Keep existing addons
}
if(save_mode & reset_addons) // Reset Addons
resetAddonSettings(&flashBufferSettings->addons);
else if(save_mode & save_addons) // Save Addons
flashBufferSettings->addons = *getAddons();
flashBufferSettings->addonsChecksum = ChecksumAddons(&flashBufferSettings->addons); // Update addon checksum
#endif
// SYSTEM SETTINGS
if((save_mode&(save_settings | reset_settings)) == 0){ // No settings save_mode specified
if( (ChecksumSystemSettings(&flashGlobalSettings.system) != flashGlobalSettings.systemChecksum) ||// Check existing flash data is valid
(flashGlobalSettings.system.version) != SYSTEM_SETTINGS_VERSION ){
resetSystemSettings(&flashBufferSettings->system);
needs_saving=1;
} // Load defaults if wrong
else // Keep existing settings
flashBufferSettings->system = flashGlobalSettings.system; // Keep existing data if ok
}
if(save_mode & reset_settings){ // Reset settings
resetSystemSettings(&flashBufferSettings->system); // Load defaults
if(save_mode == reset_all){
flashBufferSettings->system.version = UINT16_MAX; // To trigger setup screen
}
}
else if(save_mode & save_settings){ // Save current settings
flashBufferSettings->system = *getSystemSettings();
}
flashBufferSettings->systemChecksum = ChecksumSystemSettings(&flashBufferSettings->system); // Update checksum
for(uint8_t i=0;i<NUM_PROFILES;i++){ // PROFILE SETTINGS
uint8_t bad_data = (ChecksumProfileSettings(&flashGlobalSettings.profile[i]) != flashGlobalSettings.profileChecksum[i]) || // check if existing flash data is valid
(flashGlobalSettings.profile[i].version != PROFILE_SETTINGS_VERSION);
if( bad_data || ((save_mode & reset_profile) && (i == profile)) || (save_mode & reset_profiles) ){ // Reset data if wrong, of if resetting profiles.
resetProfileSettings(&flashBufferSettings->profile[i], i);
needs_saving=1;
}
else{
if((save_mode & save_profile) && (i == profile))
flashBufferSettings->profile[i] = *getProfileSettings(); // Save profile, copy profile settings from system
else
flashBufferSettings->profile[i] = flashGlobalSettings.profile[i]; // Not saving this profile, backup existing flash profile settings
}
flashBufferSettings->profileChecksum[i] = ChecksumProfileSettings(&flashBufferSettings->profile[i]); // Generate profile checksums
}
if(!needs_saving){ // Nothing to save, return
_free(flashBufferSettings);
getSettings()->isSaving = 0;
return;
}
DBG_SAVE(9);
eraseFlashPages((uint32_t)&flashGlobalSettings, flashPages_GlobalSettings, 1);
DBG_SAVE(10);
writeFlash((uint32_t*)flashBufferSettings, sizeof(flashSettings_t), (uint32_t)&flashGlobalSettings);
uint32_t flashChecksum, ramChecksum;
// Check flash and profile have same checksum
for(uint8_t i=0; i<NUM_PROFILES; i++){
ramChecksum = ChecksumProfileSettings(&flashGlobalSettings.profile[i]);
flashChecksum = ChecksumProfileSettings(&flashGlobalSettings.profile[i]);
if( (flashChecksum != ramChecksum) ||
(flashChecksum != flashGlobalSettings.profileChecksum[i])){
Error_Handler();
}
}
// Check flash and settings buffer have same checksum
flashChecksum = ChecksumSystemSettings(&flashGlobalSettings.system);
ramChecksum = ChecksumSystemSettings(&flashBufferSettings->system);
if(flashChecksum != ramChecksum)
Error_Handler();
#ifdef ENABLE_ADDONS
// Verify addon crc
flashChecksum = ChecksumAddons(&flashGlobalSettings.addons);
ramChecksum = ChecksumAddons(&flashBufferSettings->addons);
if(flashChecksum != ramChecksum)
Error_Handler();
#endif
_free(flashBufferSettings);
DBG_SAVE(0);
getSettings()->isSaving = 0;
if(reboot_mode == do_reboot)
NVIC_SystemReset();
}
static void update_flash_sizes(void){
flashPages_GlobalSettings = (sizeof(flashSettings_t) + flashPageSize - 1) / flashPageSize;
flashPages_TempSettings = (sizeof(temp_settings_t) + flashPageSize - 1) / flashPageSize;
flashPages_TipSlot = (sizeof(flashTipSlot_t) + flashPageSize - 1) / flashPageSize;
}
static void flash_sector_detect(void){
uint32_t s=0;
uint8_t *p = (uint8_t*)&flashGlobalSettings;
flashSettings_t* flashBufferSettings = _calloc(1, 2048); // Allocate 2KB buffer, fill with zeros
if(flashBufferSettings == NULL){
Error_Handler();
}
writeFlash((uint32_t*)flashBufferSettings, 2048, (uint32_t)&flashGlobalSettings); // Write buffer to flash
free(flashBufferSettings); // Free temporal buffer
eraseFlashPages((uint32_t)&flashGlobalSettings, 1, 0); // Erase only one page, don't verify (We can't, page size is still unknown)
while(*p++ == 0xFF && ++s<2048){} // Scan the flash and count the bytes erased to 0xFF
if(s!=1024 && s!=2048){ // Only 1KB/2KB page sizes are supported
fatalError(error_UNSUPPORTED_FLASH);
}
flashPageSize = s; // Store value
update_flash_sizes(); // And update flash sizes
}
void restoreSettings(void) {
bool setup = (flashGlobalSettings.system.version == UINT16_MAX) || (flashGlobalSettings.system.sector_size!=1024 && flashGlobalSettings.system.sector_size!=2048); // 0xFFFF = erased flash, trigger setup mode
#ifndef STM32F072xB
RCC->APB1ENR |= RCC_APB1ENR_PWREN | RCC_APB1ENR_BKPEN; // power the BKP peripheral
PWR->CR |= PWR_CR_DBP; // enable access to the BKP registers
BKP->CR = 0u; // disable tamper pin, just to be sure
#else
RCC->APB1ENR |= RCC_APB1ENR_PWREN; // power the BKP peripheral
RCC->BDCR |= RCC_BDCR_RTCEN;
PWR->CR |= PWR_CR_DBP; // enable access to the BKP registers
RTC->TAFCR = 0u; // disable tamper pin, just to be sure
#endif
if(setup){ // Setup mode, so flash data is not initialized
getSettings()->setupMode = enable; // Load setup state for boot screen
setIronSafeMode(enable); // Safe mode just in case
flash_sector_detect(); // Detect flash sector erase size
resetSystemSettings(getSystemSettings()); // Load default system settings
#ifdef ENABLE_ADDONS
resetAddonSettings(getAddons()); // Load default addon settings
#endif
resetProfileSettings(getProfileSettings(), profile_T12); // Load default profile settings
flashTempSettingsInit(); // Always init this to set default/safe values at boot. Initializes flashTemp index
loadSettingsFromBackupRam(); // Also read backup ram. loadProfile wil take the correct values depending on hasBattery variable.
setCurrentProfile(profile_T12); // Force profile T12, we can't call loadProfile, there's nothing in flash, let boot screen finish it
}
else{
flashPageSize = getFlashSystemSettings()->sector_size; // Restore value from flash settings
update_flash_sizes(); // Update flash sizes
Button_reset(); // Button-triggered reset routine
// [ CHECK SYSTEM SETTINGS ]
if( (flashGlobalSettings.system.version==SYSTEM_SETTINGS_VERSION) && // System version correct
(flashGlobalSettings.systemChecksum != ChecksumSystemSettings(&flashGlobalSettings.system))){ // But bad cheksum
checksumError(reset_settings); // Show checksum error
}
// [ CHECK PROFILE SETTINGS ]
for(uint8_t i=0;i<NUM_PROFILES;i++){
if( (flashGlobalSettings.profile[i].version==PROFILE_SETTINGS_VERSION) && // Profile version correct
(flashGlobalSettings.profileChecksum[i] != ChecksumProfileSettings(&flashGlobalSettings.profile[i])) ){ // But bad cheksum
checksumError(reset_profile); // Show checksum error
break;
}
}
// [ CHECK TIPS ]
for(uint8_t i=0;i<NUM_PROFILES;i++){
if( (flashTips[i].data.version==TIP_SETTINGS_VERSION) && // Tips version correct
(flashTips[i].crc != ChecksumTipData(&flashTips[i].data)) ){ // But bad cheksum
checksumError(reset_tips); // Show checksum error
break;
}
}
#ifdef ENABLE_ADDONS // [ CHECK ADDONS SETTINGS ]
if( (flashGlobalSettings.addons.version == ADDONS_SETTINGS_VERSION) && // Addons version correct
(flashGlobalSettings.addonsChecksum != ChecksumAddons(&flashGlobalSettings.addons))){ // But bad cheksum
checksumError(reset_addons); // Show checksum error
}
#endif
saveSettings(perform_scanFix, no_reboot); // Scan current settings, reset any bad area
saveTip(perform_scanFix, 0);
*getSystemSettings() = flashGlobalSettings.system;
#ifdef ENABLE_ADDONS
*getAddons() = flashGlobalSettings.addons; // Load current addons
#endif
flashTempSettingsInit(); // These initialize currentProfile, needed later by loadProfile
loadSettingsFromBackupRam();
loadProfile(getCurrentProfile());
}
}
void loadSettingsFromBackupRam(void) {
if(getSystemSettings()->hasBattery == 0)
return;
// assert on backup ram size
if(sizeof(bkpRamData.values) + sizeof(uint32_t) > BACKUP_RAM_SIZE_IN_BYTES)
{
Error_Handler(); // can't put this much data into the backup ram
}
readBackupRam();
// check crc
if(bkpRamData.crc != ChecksumBackupRam() || bkpRamData.values.lastProfile >= NUM_PROFILES )
{
// restore defaults, show error
memset((void*)&bkpRamData,0, sizeof(backupRamData_t));
for(uint8_t i = 0; i < NUM_PROFILES; i++)
{
bkpRamData.values.lastTipTemp[i] = 0; // To be resetted by loadProfile on loading
if(bkpRamData.values.lastTipTemp[i] == UINT16_MAX) // just a sanity check to handle uninitialized data
{
bkpRamData.values.lastTipTemp[i] = 0u;
}
bkpRamData.values.lastSelTip[i] = 0u;
}
bkpRamData.values.lastProfile = profile_T12;
writeBackupRam();
Oled_error_init();
putStrAligned("New/low batt?", 0, align_center);
putStrAligned("Forgot last", 16, align_center);
putStrAligned("used settings.", 32, align_center);
putStrAligned("Restored dflt.", 48, align_left);
update_display();
ErrCountDown(3,117,50);
}
setCurrentProfile(bkpRamData.values.lastProfile);
}
static void readBackupRam()
{
#ifndef STM32F072xB
for(uint8_t i = 0; i < NUM_BACKUP_RAM_REGISTERS; i++) // STM32F10x has 16.bit backup registers
{
uint16_t data = (uint16_t)*(&(BKP->DR1) + i);
bkpRamData.bytes[i*2 ] = data;
bkpRamData.bytes[i*2+1] = data >> 8u;
}
#else
memcpy(bkpRamData.bytes, (uint8_t*)&RTC->BKP0R, BACKUP_RAM_SIZE_IN_BYTES); // STM32F072 has 32bit backup registers
#endif
}
static void writeBackupRam()
{
bkpRamData.crc = ChecksumBackupRam();
#ifndef STM32F072xB
for(uint8_t i = 0; i < NUM_BACKUP_RAM_REGISTERS; i++)
{
*(&(BKP->DR1) + i) = bkpRamData.bytes[i*2] + ((uint32_t)bkpRamData.bytes[i*2+1] << 8u);
}
#else
memcpy((uint8_t*)&RTC->BKP0R, bkpRamData.bytes, BACKUP_RAM_SIZE_IN_BYTES);
#endif
}
static uint32_t ChecksumBackupRam(){
uint32_t checksum;
checksum = HAL_CRC_Calculate(&hcrc, (uint32_t*)bkpRamData.bytes, sizeof(bkpRamData.values)/sizeof(uint32_t) );
return checksum;
}
void flashTempSettingsInitialSetup(void){ // To be called after initial setup screen
flashTempSettingsErase();
for(uint8_t i=0;i<NUM_PROFILES;i++)
flashTemp.tip[i]= 0;
flashTemp.temp = DEFAULT_TEMPERATURE;
flashTemp.profile=profile_T12;
flashTempWrite();
flashTipsWrite();
flashProfileWrite();
setCurrentProfile(profile_T12); // Just in case
setCurrentTip(0);
setUserTemperature(DEFAULT_TEMPERATURE);
}
void flashTempSettingsInit(void) //call it only once during init
{
bool setDefault=0;
uint16_t i;
for(i=0; i<(sizeof(flashTempSettings.temperature)/2)-1 && flashTempSettings.temperature[i+1] != UINT16_MAX; i++); // Seek through the array
if(i<(sizeof(flashTempSettings.temperature)/2)-1){ // Free slot found
for(uint16_t j=i+1; j<(sizeof(flashTempSettings.temperature)/2)-1; j++) { // Ensure rest of array is erased
if(flashTempSettings.temperature[j] != UINT16_MAX){ // Found unexpected data
setDefault=1; // Reset
break;
}
}
if(!setDefault){
flashTemp.tempIndex = i;
flashTemp.temp = flashTempSettings.temperature[i];
}
} // No free slot found, reset
else
setDefault=1;
if(!setDefault){
for(i=0; i<(sizeof(flashTempSettings.profile)/2)-1 && flashTempSettings.profile[i+1] != UINT16_MAX; i++); // Same operation with profile
if(i<(sizeof(flashTempSettings.profile)/2)-1){
for(uint16_t j=i+1; j<(sizeof(flashTempSettings.profile)/2)-1; j++) {
if(flashTempSettings.profile[j] != UINT16_MAX){
setDefault=1;
break;
}
}
if(!setDefault){
flashTemp.profile=flashTempSettings.profile[i];
flashTemp.profileIndex = i;
}
}
else
setDefault=1;
}
if(!setDefault){
for(uint8_t n=0; n<NUM_PROFILES; n++){
for(i=0; i<(sizeof(flashTempSettings.tip[0])/2)-1 && flashTempSettings.tip[n][i+1] != UINT16_MAX; i++); // Same operation with tips
if(i<(sizeof(flashTempSettings.tip[0])/2)-1){
for(uint16_t j=i+1; j<(sizeof(flashTempSettings.tip[0])/2)-1; j++) {
if(flashTempSettings.tip[n][j] != UINT16_MAX){
setDefault=1;
break;
}
}
if(!setDefault){
flashTemp.tipIndex[n] = i;
flashTemp.tip[n] = flashTempSettings.tip[n][i];
}
}
else
setDefault=1;
}
}
if(setDefault)
flashTempSettingsInitialSetup();
else{
setCurrentProfile(flashTemp.profile);
if(flashTempSettings.temperature[flashTemp.tempIndex] != UINT16_MAX)
flashTemp.tempIndex++; // Increase index if current slot is not empty (Only required when flash is completely erased)
if(flashTempSettings.profile[flashTemp.profileIndex] != UINT16_MAX)
flashTemp.profileIndex++;
for(uint8_t n=0; n<NUM_PROFILES; n++)
if(flashTempSettings.tip[n][flashTemp.tipIndex[n]] != UINT16_MAX)
flashTemp.tipIndex[n]++;
}
}
void flashTempWrite(void)
{
if(flashTemp.tempIndex>=(sizeof(flashTempSettings.temperature)/2)-1) // All positions used
{
flashTempSettingsErase();
flashTipsWrite();
flashProfileWrite();
}
HAL_IWDG_Refresh(&hiwdg);
uint32_t _irq = __get_PRIMASK();
__disable_irq();
HAL_FLASH_Unlock();
__set_PRIMASK(_irq);
DBG_SAVE(12);
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)&flashTempSettings.temperature[flashTemp.tempIndex], flashTemp.temp) != HAL_OK) { // Store new temp
Error_Handler();
}
HAL_FLASH_Lock();
flashTemp.tempIndex++; // Increase index for next time
DBG_SAVE(0);
}
void flashProfileWrite(void)
{
if(flashTemp.profileIndex>=(sizeof(flashTempSettings.profile)/2)-1) // All positions used
{
flashTempSettingsErase();
flashTipsWrite();
flashTempWrite();
}
HAL_IWDG_Refresh(&hiwdg);
uint32_t _irq = __get_PRIMASK();
__disable_irq();
HAL_FLASH_Unlock();
__set_PRIMASK(_irq);
DBG_SAVE(13);
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)&flashTempSettings.profile[flashTemp.profileIndex], flashTemp.profile) != HAL_OK) { // Store new tip/profile data
Error_Handler();
}
HAL_FLASH_Lock();
flashTemp.profileIndex++; // Increase index for next time
DBG_SAVE(0);
}
void flashTipsWrite(void)
{
uint8_t p = getCurrentProfile();
if(flashTemp.tipIndex[p]>=(sizeof(flashTempSettings.tip[0])/2)-1) // All positions used
{
flashTempSettingsErase();
flashTempWrite();
flashProfileWrite();
}
HAL_IWDG_Refresh(&hiwdg);
uint32_t _irq = __get_PRIMASK();
__disable_irq();
HAL_FLASH_Unlock();
__set_PRIMASK(_irq);
DBG_SAVE(14);
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)&flashTempSettings.tip[p][flashTemp.tipIndex[p]], flashTemp.tip[p]) != HAL_OK) { // Store new tip/profile data
Error_Handler();
}
HAL_FLASH_Lock();
DBG_SAVE(0);
flashTemp.tipIndex[p]++; // Increase index for next time
}
void flashTempSettingsErase(void){
DBG_SAVE(11);
eraseFlashPages((uint32_t)&flashTempSettings, flashPages_TempSettings, 1);
flashTemp.tempIndex = 0; // Reset index
flashTemp.profileIndex = 0;
for(uint8_t i=0; i<NUM_PROFILES;i++)
flashTemp.tipIndex[i] = 0;
flashTemp.temp=0xFFFF;
flashTemp.profile=0xFF;
DBG_SAVE(0);
}
static uint32_t ChecksumSystemSettings(systemSettings_t* system){ // Check system settings block
return (HAL_CRC_Calculate(&hcrc, (uint32_t*)system, sizeof(systemSettings_t)/sizeof(uint32_t)) );
}
static uint32_t ChecksumTipData(flashTipData_t* tipData){ // Check whole profile block (Including tips)
return (HAL_CRC_Calculate(&hcrc, (uint32_t*)tipData, sizeof(flashTipData_t)/sizeof(uint32_t)) );
}
static uint32_t ChecksumProfileSettings(profile_settings_t* profile_settings){ // Check only profile settings (Not tips)
return (HAL_CRC_Calculate(&hcrc, (uint32_t*)profile_settings, sizeof(profile_settings_t)/sizeof(uint32_t)) );
}
#ifdef ENABLE_ADDONS
static void resetAddonSettings(addonSettings_t *addons){
uint32_t _irq = __get_PRIMASK();
__disable_irq();
*addons = defaultAddons;
__set_PRIMASK(_irq);
}
static uint32_t ChecksumAddons(addonSettings_t* addonSettings){
uint32_t checksum;
checksum = HAL_CRC_Calculate(&hcrc, (uint32_t*)addonSettings, sizeof(addonSettings_t)/sizeof(uint32_t));
return checksum;
}
addonSettings_t * getAddons(void){
return &getSettings()->addons;
}
bool isAddonSettingsChanged(void) {
return ChecksumAddons(getAddons()) != flashGlobalSettings.addonsChecksum;
}
#endif
static void resetSystemSettings(systemSettings_t * system) {
uint32_t _irq = __get_PRIMASK();
__disable_irq();
*system = defaultSystemSettings;
system->sector_size = flashPageSize; // Keep existing value
flashTempSettingsInitialSetup();
__set_PRIMASK(_irq);
}
static void resetProfileSettings(profile_settings_t * p, uint8_t profile){
uint32_t _irq = __get_PRIMASK();
*p = defaultProfileSettings;
__disable_irq();
if(profile==profile_T12){
p->ID = profile_T12;
p->impedance = 80; // 8.0 Ohms
p->power = 80; // 80W
p->noIronValue = 4000;
p->Cal250_default = T12_Cal250;
p->Cal400_default = T12_Cal400;
}
else if(profile==profile_C245){
p->ID = profile_C245;
p->impedance = 26;
p->power = 150;
p->noIronValue = 4000;
p->Cal250_default = C245_Cal250;
p->Cal400_default = C245_Cal400;
}
else if(profile==profile_C210){
p->ID = profile_C210;
p->power = 80;
p->impedance = 21;
p->noIronValue = 1200;
p->Cal250_default = C210_Cal250;
p->Cal400_default = C210_Cal400;
}
else{
Error_Handler(); // We shouldn't get here!
}
__set_PRIMASK(_irq);
}
profile_settings_t * getFlashProfileSettings(void){
return &getFlashSettings()->profile[getCurrentProfile()];
}