forked from deividAlfa/stm32_soldering_iron_controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.c
1042 lines (914 loc) · 36 KB
/
settings.c
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__
#define __BASE_FILE__ "settings.c"
#endif
///temperature saving variables
//#define TEMP_SETTINGS (PROFILE_SETTINGS - FLASH_PAGE_SIZE) //0x801EC00
#define TEMP_SETTINGS 0x801EC00
// 0x801e800
#define PROFILE_TIP_SETTINGS (TEMP_SETTINGS-FLASH_PAGE_SIZE)
//#define TEMP_SETTINGS (0x801F000+3840+128)
const uint32_t increments[]={1, 2, 4, 8}; //values at [0] won't be used but added it for better clearance
const uint64_t mask[]={0xff, 0xffff, 0xffffffff, 0xffffffffffffffff}; //the same here
static uint32_t tempCnt=0;
static uint16_t tempRam=0;
static uint32_t temperaturePrev = 0u;
static uint32_t temperatureNew = 0u;
static uint32_t profileTipCnt=0;
static profileTipSettings_t profileTipRam={.profileTip=0};
static profileTipSettings_t profileTipPrev={.profileTip=0};
//static profileTipSettings_t profileTipNew={.profileTip=0};
static bool tempChangeTrigger=false;
static bool profileTipChangeTrigger=false;
static uint32_t lastCheckTimeOfTemperature=0;
static uint32_t lastCheckTimeOfProfileTip=0;
///temperature saving variables end
const settings_t defaultSettings = {
.version = (~((uint32_t)SETTINGS_VERSION<<16)&0xFFFF0000) | SETTINGS_VERSION, // Higher 16bit is 1s complement to make detection stronger
#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,
.rememberLastProfile = true,
.rememberLastTemp = false,
.rememberLastTip = true,
.lvp = 110, // 11.0V Low voltage
.bootProfile = profile_None,
.initMode = mode_sleep, // Safer to boot in sleep mode by default!
.buzzerMode = disable,
.buttonWakeMode = wake_all,
.shakeWakeMode = wake_all,
.EncoderMode = RE_Mode_Forward,
.debugEnabled = disable,
.language = lang_english,
.clone_fix = disable,
.state = initialized,
};
#ifdef ENABLE_ADDONS
const addonSettings_t defaultAddonSettings = {
.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
__attribute__((aligned(4))) typedef struct{
settings_t settings;
uint32_t settingsChecksum;
} flashSettingsSettings_t;
__attribute__((section(".globalSettings"))) flashSettingsSettings_t flashGlobalSettings;
__attribute__((aligned(4))) typedef struct{
profile_t Profile[NUM_PROFILES];
uint32_t ProfileChecksum[NUM_PROFILES];
} flashSettingsProfiles_t;
__attribute__((section(".profileSettings"))) flashSettingsProfiles_t flashProfilesSettings;
#ifdef ENABLE_ADDONS
__attribute__((aligned(4))) typedef struct{
addonSettings_t addonSettings;
uint32_t addonSettingsChecksum;
} flashSettingsAddons_t;
__attribute__((section(".addonSettings"))) flashSettingsAddons_t flashAddonSettings;
#endif
systemSettings_t systemSettings;
static void saveSettings(uint8_t mode);
static void checksumError(uint8_t mode);
static void Flash_error(void);
static void Button_reset(void);
static void ErrCountDown(uint8_t Start,uint8_t xpos, uint8_t ypos);
static uint32_t ChecksumSettings(settings_t* settings);
static uint32_t ChecksumProfile(profile_t* profile);
static void resetSystemSettings(void);
static void resetCurrentProfile(void);
static uint64_t readTempInit(uint32_t addr, uint32_t * cnt, uint32_t size); //call it only once during init
static void writeTemp(uint64_t tempArg, uint32_t addr, uint32_t * cnt, uint32_t size);
#ifdef ENABLE_ADDONS
static void loadAddonSettings(void);
static void resetAddonSettings();
static uint32_t ChecksumAddons(addonSettings_t* addonSettings);
#endif
#ifdef HAS_BATTERY
#define BACKUP_RAM_SIZE_IN_BYTES 20u
#define NUM_BACKUP_RAM_REGISTERS (BACKUP_RAM_SIZE_IN_BYTES / 2u) // each register holds 2 byte of data in the lower nibble
typedef struct
{
// max (BACKUP_RAM_SIZE_IN_BYTES - 4) byte of data in total
uint16_t lastTipTemp[NUM_PROFILES];
uint8_t lastSelTip[NUM_PROFILES];
uint8_t lastProfile;
} backupRamValues_t;
typedef union
{
__attribute__((aligned(4))) struct
{
backupRamValues_t values;
uint32_t crc;
};
uint8_t bytes[BACKUP_RAM_SIZE_IN_BYTES];
} backupRamData_t;
static void loadSettingsFromBackupRam(void);
static uint32_t ChecksumBackupRam();
static void readBackupRam();
static void writeBackupRam();
static backupRamData_t bkpRamData;
#endif
extern int __SETTINGS_SECTION_START; /* defined by the linker, only its address which is the target value */
#define SETTINGS_SECTION_START ((uint32_t)&__SETTINGS_SECTION_START)
extern int __SETTINGS_SECTION_LENGTH; /* defined by the linker, only its address which is the target value */
#define SETTINGS_SECTION_LENGTH ((uint32_t)&__SETTINGS_SECTION_LENGTH)
void checkSettings(void){
#ifdef NOSAVESETTINGS
return;
#endif
static uint32_t prevSysChecksum = 0u;
static uint32_t newSysChecksum = 0u;
static uint32_t prevTipChecksum = 0u;
static uint32_t newProfileChecksum = 0u;
#ifdef ENABLE_ADDONS
static uint32_t prevAddonChecksum = 0u;
static uint32_t newAddonChecksum = 0u;
#endif
static uint32_t lastCheckTime=0;
static uint32_t lastChangeTime=0;
uint32_t CurrentTime = HAL_GetTick();
uint8_t scr_index=current_screen->index;
// To reduce heap usage, only allow saving in smaller screens.
// Content change detection will be still active, but saving will postponed upon returning to a smaller screen.
// This is done to ensure compatibility with 10KB RAM devices yet allowing the firmware to grow unconstrained by ram usage
uint8_t allowSave = (scr_index == screen_boot ||
scr_index == screen_main ||
scr_index == screen_settings ||
scr_index == screen_calibration ||
scr_index == screen_reset_confirmation);
//#ifndef HAS_BATTERY
// if(systemSettings.settings.rememberLastProfile)
// {
// systemSettings.settings.bootProfile = systemSettings.currentProfile;
// }
// if(systemSettings.settings.rememberLastTip)
// {
// systemSettings.Profile.defaultTip = systemSettings.currentTip;
// }
//#endif
// Save from menu
if(systemSettings.save_Flag && allowSave){
switch(systemSettings.save_Flag & reboot_mask){
case save_Settings:
saveSettings(keepProfiles);
break;
case reset_Profiles:
systemSettings.currentProfile=profile_None;
saveSettings(wipeProfiles);
break;
case reset_Profile:
resetCurrentProfile();
saveSettings(keepProfiles);
break;
case reset_Settings:
{
uint8_t currentProfile=systemSettings.currentProfile;
resetSystemSettings();
systemSettings.currentProfile=currentProfile;
saveSettings(keepProfiles);
break;
}
case reset_All:
resetSystemSettings();
saveSettings(wipeProfiles);
break;
default:
Error_Handler();
}
if(systemSettings.save_Flag>=reboot_after_save){ // If save flag indicates any resetting mode, reboot
NVIC_SystemReset();
}
systemSettings.save_Flag=0;
return;
}
//auto save user temperature after 10s
temperatureNew=getUserTemperature();
if(temperatureNew!=temperaturePrev) //poll for temprature change
{
temperaturePrev=temperatureNew;
lastCheckTimeOfTemperature = CurrentTime; //start time counting from now
tempChangeTrigger=true;
}
if((CurrentTime-lastCheckTimeOfTemperature)>9999)
{
lastCheckTimeOfTemperature = CurrentTime; //reset time counting
if(tempChangeTrigger==true)
{
if(tempRam != temperatureNew) //make sure not to write the same value twice
writeTemp(tempRam=temperatureNew,TEMP_SETTINGS,&tempCnt, FLASH_TYPEPROGRAM_HALFWORD);
tempChangeTrigger=false;
}
}
profileTipRam.bootProfile=systemSettings.currentProfile;
profileTipRam.bootTip[systemSettings.currentProfile]=systemSettings.currentTip;
if(profileTipRam.profileTip!=profileTipPrev.profileTip) //poll for temprature change
{
profileTipPrev.profileTip=profileTipRam.profileTip;
lastCheckTimeOfProfileTip = CurrentTime; //start time counting from now
profileTipChangeTrigger=true;
}
if((CurrentTime-lastCheckTimeOfProfileTip)>999)
{
lastCheckTimeOfProfileTip = CurrentTime; //reset time counting
if(profileTipChangeTrigger==true)
{
writeTemp(profileTipRam.profileTip,PROFILE_TIP_SETTINGS,&profileTipCnt, FLASH_TYPEPROGRAM_WORD);
profileTipChangeTrigger=false;
}
}
// Auto save on content change
if( (systemSettings.setupMode==enable) || (getIronCalibrationMode()) || (getIronErrorFlags().safeMode) || ((CurrentTime-lastCheckTime)<999)){
return;
}
lastCheckTime = CurrentTime; // Store current time
newSysChecksum = ChecksumSettings(&systemSettings.settings); // Calculate system checksum
newProfileChecksum = ChecksumProfile(&systemSettings.Profile); // Calculate profile checksum
#ifdef ENABLE_ADDONS
newAddonChecksum = ChecksumAddons(&(systemSettings.addonSettings)); // Calculate the addon checksum
#endif
// If anything was changed (Checksum mismatch)
if( (systemSettings.settingsChecksum != newSysChecksum )
|| (systemSettings.ProfileChecksum != newProfileChecksum)
#ifdef ENABLE_ADDONS
|| (systemSettings.addonSettingsChecksum != newAddonChecksum )
#endif
){
if( (prevSysChecksum != newSysChecksum)
|| (prevTipChecksum != newProfileChecksum)
#ifdef ENABLE_ADDONS
|| (prevAddonChecksum != newAddonChecksum)
#endif
){
// If different from the previous calculated checksum (settings are being changed quickly, don't save every time).
prevSysChecksum = newSysChecksum; // Store last computed checksum
prevTipChecksum = newProfileChecksum;
#ifdef ENABLE_ADDONS
prevAddonChecksum = newAddonChecksum;
#endif
lastChangeTime = CurrentTime; // Reset timer (we don't save anything until we pass a certain time without changes)
}
else if(allowSave && ((CurrentTime-lastChangeTime)>5000)){ // If different from the previous calculated checksum, and timer expired (No changes in the last 5 sec)
saveSettings(save_Settings); // Data was saved (so any pending interrupt knows this)
}
}
#ifdef HAS_BATTERY
bkpRamData.values.lastProfile = systemSettings.currentProfile;
if(!getIronCalibrationMode() && scr_index != screen_debug) // don't persist the temperature while calibration is in progress or in the debug screen
{
bkpRamData.values.lastTipTemp[systemSettings.currentProfile] = getUserSetTemperature();
}
bkpRamData.values.lastSelTip[systemSettings.currentProfile] = systemSettings.currentTip;
writeBackupRam();
#endif
}
//This is done to avoid huge stack build up. Trigger saving using checkSettings with a flag instead direct call from menu.
// Thus, the flags stays after the screen exits. The code handling settings saving decides when it's ok to store them.
void saveSettingsFromMenu(uint8_t mode){
systemSettings.save_Flag=mode;
if(mode>=reset_Profiles){ // Force safe mode (disable iron power) in any resetting mode, the station will reboot when done.
setSafeMode(enable);
}
}
static void eraseFlashPages(uint32_t pageAddress, uint32_t numPages)
{
uint32_t error = 0;
FLASH_EraseInitTypeDef erase = {0};
__disable_irq();
configurePWMpin(output_Low);
HAL_FLASH_Unlock();
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)){
Flash_error();
}
HAL_FLASH_Lock();
__enable_irq();
// Ensure flash was erased
for (uint32_t i = 0u; i < (numPages * FLASH_PAGE_SIZE / sizeof(int32_t)); i++) {
if( *((uint32_t*)pageAddress+i) != 0xFFFFFFFF){
Flash_error();
}
}
}
static void writeFlash(uint32_t* src, uint32_t len, uint32_t dstAddr)
{
uint32_t const numWordsToWrite = (len + sizeof(uint32_t) - 1u) / sizeof(uint32_t);
uint32_t* srcData = src;
__disable_irq();
HAL_FLASH_Unlock();
__enable_irq();
// written = number of 32-bit values written
for(uint32_t written=0; written < numWordsToWrite; written++){
__disable_irq();
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, dstAddr, *srcData ) != HAL_OK)
{
Flash_error();
}
dstAddr += sizeof(uint32_t); // increase pointers
srcData++;
__enable_irq();
}
__disable_irq();
HAL_FLASH_Lock();
__enable_irq();
}
static void saveSettings(uint8_t mode){
#ifdef NOSAVESETTINGS
return;
#else
uint8_t const profile = systemSettings.currentProfile;
flashSettingsProfiles_t* flashBufferProfiles = _malloc(sizeof(flashSettingsProfiles_t));
flashSettingsSettings_t* flashBufferSettings = _malloc(sizeof(flashSettingsSettings_t));
#ifdef ENABLE_ADDONS
flashSettingsAddons_t* flashBufferAddons = _malloc(sizeof(flashSettingsAddons_t));
#endif
// check if malloc succeeded or not
if( (flashBufferProfiles == NULL)
|| (flashBufferSettings == NULL)
#ifdef ENABLE_ADDONS
|| (flashBufferAddons == NULL)
#endif
)
{ Error_Handler(); }
*flashBufferProfiles = flashProfilesSettings; // Save current profiles into the temp buffer
while(ADC_Status != ADC_Idle);
__disable_irq();
systemSettings.isSaving = 1;
configurePWMpin(output_Low);
__enable_irq();
if( (systemSettings.settings.state!=initialized) || (systemSettings.Profile.state!=initialized) ){
Error_Handler();
}
systemSettings.settingsChecksum = ChecksumSettings(&systemSettings.settings);
flashBufferSettings->settingsChecksum = systemSettings.settingsChecksum;
flashBufferSettings->settings = systemSettings.settings;
#ifdef ENABLE_ADDONS
systemSettings.addonSettingsChecksum = ChecksumAddons(&(systemSettings.addonSettings));
flashBufferAddons->addonSettingsChecksum = systemSettings.addonSettingsChecksum;
flashBufferAddons->addonSettings = systemSettings.addonSettings;
#endif
if((mode==keepProfiles) && (profile<=profile_C210) && (systemSettings.Profile.ID == profile )){
systemSettings.ProfileChecksum = ChecksumProfile(&systemSettings.Profile);
flashBufferProfiles->ProfileChecksum[profile] = systemSettings.ProfileChecksum;
flashBufferProfiles->Profile[profile] = systemSettings.Profile;
}
else{
mode = wipeProfiles;
for(uint8_t x=0;x<NUM_PROFILES;x++){
flashBufferProfiles->Profile[x].state = 0xFF;
flashBufferProfiles->ProfileChecksum[x] = 0xFFFFFFFF;
memset(&flashBufferProfiles->Profile[x],0xFF,sizeof(profile_t));
}
}
eraseFlashPages(SETTINGS_SECTION_START, (SETTINGS_SECTION_LENGTH+FLASH_PAGE_SIZE-1) / FLASH_PAGE_SIZE);
writeFlash((uint32_t*)flashBufferProfiles, sizeof(flashSettingsProfiles_t), (uint32_t)&flashProfilesSettings);
writeFlash((uint32_t*)flashBufferSettings, sizeof(flashSettingsSettings_t), (uint32_t)&flashGlobalSettings);
#ifdef ENABLE_ADDONS
writeFlash((uint32_t*)flashBufferAddons, sizeof(flashSettingsAddons_t), (uint32_t)&flashAddonSettings);
#endif
if(mode==keepProfiles){
uint32_t ProfileFlash = ChecksumProfile(&flashProfilesSettings.Profile[profile]);
uint32_t ProfileRam = ChecksumProfile(&systemSettings.Profile);
if(ProfileFlash != ProfileRam){
Flash_error();
}
}
// Check flash and system settings have same checksum
uint32_t SettingsFlash = ChecksumSettings(&flashGlobalSettings.settings);
uint32_t SettingsRam = ChecksumSettings(&systemSettings.settings);
if(SettingsFlash != SettingsRam){
Flash_error();
}
#ifdef ENABLE_ADDONS
// Verify addon crc
uint32_t addonsFlashCrc = ChecksumAddons(&flashAddonSettings.addonSettings);
uint32_t addonsRamCrc = ChecksumAddons(&systemSettings.addonSettings);
if(addonsFlashCrc != addonsRamCrc){
Flash_error();
}
#endif
#ifdef ENABLE_ADDONS
_free(flashBufferAddons);
#endif
_free(flashBufferSettings);
_free(flashBufferProfiles);
__disable_irq();
systemSettings.isSaving = 0;
__enable_irq();
#endif
}
void restoreSettings() {
#ifdef NOSAVESETTINGS // Stop erasing the flash while in debug mode
__disable_irq();
resetSystemSettings();
systemSettings.settings.currentProfile = profile_T12;
resetCurrentProfile();
loadProfile(systemSettings.settings.currentProfile);
__enable_irq();
return;
#endif
if(flashGlobalSettings.settings.state != initialized){
resetSystemSettings();
#ifdef ENABLE_ADDONS
resetAddonSettings();
#endif
saveSettings(wipeProfiles);
}
else{
Button_reset();
}
if(flashGlobalSettings.settings.version!=defaultSettings.version){ // Silent reset if version mismatch
resetSystemSettings();
saveSettings(wipeProfiles);
resetAddonSettings();
}
else{
systemSettings.settings = flashGlobalSettings.settings;
systemSettings.settingsChecksum = flashGlobalSettings.settingsChecksum;
}
if(ChecksumSettings(&systemSettings.settings)!=systemSettings.settingsChecksum){ // Show error message if bad checksum
checksumError(reset_All);
}
//read selected profile and tip from FLASH
profileTipRam.profileTip=readTempInit(PROFILE_TIP_SETTINGS, &profileTipCnt, FLASH_TYPEPROGRAM_WORD); //read tip and profile from flash
if(profileTipRam.bootProfile>=NUM_PROFILES)
profileTipRam.profileTip=0;
profileTipPrev.profileTip=profileTipRam.profileTip;
// assume the boot profile
loadProfile(profileTipRam.bootProfile);
//read temperature saved in flash
tempRam=readTempInit(TEMP_SETTINGS, &tempCnt, FLASH_TYPEPROGRAM_HALFWORD);
if((tempRam > systemSettings.Profile.MaxSetTemperature) || (tempRam < systemSettings.Profile.MinSetTemperature))
tempRam=systemSettings.Profile.defaultTemperature;
else
setUserTemperature(tempRam);
#ifdef ENABLE_ADDONS
loadAddonSettings();
#endif
#ifdef HAS_BATTERY
loadSettingsFromBackupRam();
if(systemSettings.settings.rememberLastProfile)
{
loadProfile(bkpRamData.values.lastProfile);
}
if(systemSettings.settings.rememberLastTip)
{
setCurrentTip(bkpRamData.values.lastSelTip[systemSettings.currentProfile]);
ironSchedulePwmUpdate();
}
#endif
}
#ifdef HAS_BATTERY
void loadSettingsFromBackupRam(void)
{
// assert on backup ram size
if(sizeof(backupRamValues_t) + 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())
{
// restore defaults, show error
memset((void*)&bkpRamData,0, sizeof(backupRamData_t));
for(uint8_t i = 0; i < NUM_PROFILES; i++)
{
bkpRamData.values.lastTipTemp[i] = flashProfilesSettings.Profile[i].defaultTemperature;
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 = systemSettings.settings.bootProfile;
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);
NVIC_SystemReset();
}
}
void restoreLastSessionSettings(void)
{
if(systemSettings.settings.rememberLastTemp)
{
setUserTemperature(bkpRamData.values.lastTipTemp[systemSettings.currentProfile]);
}
}
static void readBackupRam()
{
for(uint8_t i = 0; i < NUM_BACKUP_RAM_REGISTERS; i++)
{
uint16_t const data = (uint16_t)*(&(BKP->DR1) + i);
bkpRamData.bytes[i*2 ] = data;
bkpRamData.bytes[i*2+1] = data >> 8u;
}
}
static void writeBackupRam()
{
bkpRamData.crc = ChecksumBackupRam(bkpRamData);
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);
}
}
static uint32_t ChecksumBackupRam(){
uint32_t checksum;
checksum = HAL_CRC_Calculate(&hcrc, (uint32_t*)bkpRamData.bytes, sizeof(backupRamValues_t)/sizeof(uint32_t) );
return checksum;
}
#endif
static uint32_t ChecksumSettings(settings_t* settings){
uint32_t checksum;
checksum = HAL_CRC_Calculate(&hcrc, (uint32_t*)settings, sizeof(settings_t)/sizeof(uint32_t) );
return checksum;
}
static uint32_t ChecksumProfile(profile_t* profile){
uint32_t checksum;
checksum = HAL_CRC_Calculate(&hcrc, (uint32_t*)profile, sizeof(profile_t)/sizeof(uint32_t));
return checksum;
}
#ifdef ENABLE_ADDONS
static void loadAddonSettings(void)
{
systemSettings.addonSettings = flashAddonSettings.addonSettings;
systemSettings.addonSettingsChecksum = ChecksumAddons(&(systemSettings.addonSettings));
if((systemSettings.addonSettings.enabledAddons != defaultAddonSettings.enabledAddons) || // list of addons changed
(systemSettings.addonSettingsChecksum != flashAddonSettings.addonSettingsChecksum)) // crc mismatch
{
checksumError(reset_Addons);
}
}
static void resetAddonSettings()
{
__disable_irq();
systemSettings.addonSettings = defaultAddonSettings;
systemSettings.addonSettingsChecksum = 0u;
__enable_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;
}
#endif
static void resetSystemSettings(void) {
__disable_irq();
systemSettings.settings = defaultSettings;
__enable_irq();
}
static void resetCurrentProfile(void){
__disable_irq();
if(systemSettings.currentProfile==profile_T12){
systemSettings.Profile.ID = profile_T12;
for(uint8_t x = 0; x < NUM_TIPS; x++) {
systemSettings.Profile.tip[x].calADC_At_250 = T12_Cal250;
systemSettings.Profile.tip[x].calADC_At_400 = T12_Cal400; // These values are way lower, but better to be safe than sorry
systemSettings.Profile.tip[x].active = 1; // All tips active
systemSettings.Profile.tip[x].PID.Kp = 4000; // val = /1.000.000
systemSettings.Profile.tip[x].PID.Ki = 5500; // val = /1.000.000
systemSettings.Profile.tip[x].PID.Kd = 700; // val = /1.000.000
systemSettings.Profile.tip[x].PID.maxI = 70; // val = /100
systemSettings.Profile.tip[x].PID.minI = 0; // val = /100
strcpy(systemSettings.Profile.tip[x].name, _BLANK_TIP); // Empty name
}
strcpy(systemSettings.Profile.tip[0].name, "T12-BC3"); // Put some generic name.
systemSettings.Profile.currentNumberOfTips = 1;
systemSettings.Profile.defaultTip = 0;
systemSettings.Profile.impedance = 80; // 8.0 Ohms
systemSettings.Profile.power = 80; // 80W
systemSettings.Profile.noIronValue = 4000;
systemSettings.Profile.Cal250_default = T12_Cal250;
systemSettings.Profile.Cal400_default = T12_Cal400;
}
else if(systemSettings.currentProfile==profile_C245){
systemSettings.Profile.ID = profile_C245;
for(uint8_t x = 0; x < NUM_TIPS; x++) {
systemSettings.Profile.tip[x].calADC_At_250 = C245_Cal250;
systemSettings.Profile.tip[x].calADC_At_400 = C245_Cal400;
systemSettings.Profile.tip[x].PID.Kp = 4000; // val = /1.000.000
systemSettings.Profile.tip[x].PID.Ki = 5500; // val = /1.000.000
systemSettings.Profile.tip[x].PID.Kd = 700; // val = /1.000.000
systemSettings.Profile.tip[x].PID.maxI = 70; // val = /100
systemSettings.Profile.tip[x].PID.minI = 0;
strcpy(systemSettings.Profile.tip[x].name, _BLANK_TIP);
}
strcpy(systemSettings.Profile.tip[0].name, "C245-963");
systemSettings.Profile.currentNumberOfTips = 1;
systemSettings.Profile.defaultTip = 0;
systemSettings.Profile.impedance = 26;
systemSettings.Profile.power = 150;
systemSettings.Profile.noIronValue = 4000;
systemSettings.Profile.Cal250_default = C245_Cal250;
systemSettings.Profile.Cal400_default = C245_Cal400;
}
else if(systemSettings.currentProfile==profile_C210){
systemSettings.Profile.ID = profile_C210;
for(uint8_t x = 0; x < NUM_TIPS; x++) {
systemSettings.Profile.tip[x].calADC_At_250 = C210_Cal250;
systemSettings.Profile.tip[x].calADC_At_400 = C210_Cal400;
systemSettings.Profile.tip[x].PID.Kp = 4000; // val = /1.000.000
systemSettings.Profile.tip[x].PID.Ki = 5500; // val = /1.000.000
systemSettings.Profile.tip[x].PID.Kd = 700; // val = /1.000.000
systemSettings.Profile.tip[x].PID.maxI = 70; // val = /100
systemSettings.Profile.tip[x].PID.minI = 0;
strcpy(systemSettings.Profile.tip[x].name, _BLANK_TIP);
}
strcpy(systemSettings.Profile.tip[0].name, "C210-018");
systemSettings.Profile.currentNumberOfTips = 1;
systemSettings.Profile.defaultTip = 0;
systemSettings.Profile.power = 80;
systemSettings.Profile.impedance = 21;
systemSettings.Profile.noIronValue = 1200;
systemSettings.Profile.Cal250_default = C210_Cal250;
systemSettings.Profile.Cal400_default = C210_Cal400;
}
else{
Error_Handler(); // We shouldn't get here!
}
systemSettings.Profile.calADC_At_0 = 0;
systemSettings.Profile.tipFilter.coefficient = 75; // % of old data (more %, more filtering)
systemSettings.Profile.tipFilter.threshold = 50;
systemSettings.Profile.tipFilter.min = 50; // Don't go below x% when decreasing after exceeding threshold limits
systemSettings.Profile.tipFilter.count_limit = 0;
systemSettings.Profile.tipFilter.step = -3; // -5% less everytime the reading diff exceeds threshold_limit and the counter is greater than count_limit
systemSettings.Profile.tipFilter.reset_threshold = 600; // Any diff over 500 reset the filter (Tip removed or connected)
#ifdef USE_NTC
systemSettings.Profile.ntc.enabled = enable;
#ifdef PULLUP
systemSettings.Profile.ntc.pullup = 1;
#elif defined PULLDOWN
systemSettings.Profile.ntc.pullup = 0;
#else
#error NO PULL MODE DEFINED
#endif
systemSettings.Profile.ntc.detection = 0;
systemSettings.Profile.ntc.NTC_res = NTC_RES/100;
systemSettings.Profile.ntc.NTC_beta = NTC_BETA;
systemSettings.Profile.ntc.high_NTC_res = 1000; // 100.0K
systemSettings.Profile.ntc.low_NTC_res = 100; // 10.0K
systemSettings.Profile.ntc.high_NTC_beta = NTC_BETA;
systemSettings.Profile.ntc.low_NTC_beta = NTC_BETA;
systemSettings.Profile.ntc.pull_res = PULL_RES/100;
#else
systemSettings.Profile.ntc.enabled = 0;
#endif
systemSettings.Profile.errorTimeout = 500; // ms
systemSettings.Profile.errorResumeMode = error_resume;
systemSettings.Profile.sleepTimeout = (uint32_t)5*60000; // ms
systemSettings.Profile.standbyTimeout = (uint32_t)5*60000;
systemSettings.Profile.standbyTemperature = 180;
systemSettings.Profile.defaultTemperature = 320;
systemSettings.Profile.MaxSetTemperature = 450;
systemSettings.Profile.MinSetTemperature = 180;
systemSettings.Profile.boostTimeout = 60000; // ms
systemSettings.Profile.boostTemperature = 50;
systemSettings.Profile.pwmMul = 1;
systemSettings.Profile.readPeriod = (200*200)-1; // 200ms * 200 because timer period is 5us
systemSettings.Profile.readDelay = (20*200)-1; // 20ms (Also uses 5us clock)
systemSettings.Profile.tempUnit = mode_Celsius;
systemSettings.Profile.shakeFiltering = disable;
systemSettings.Profile.WakeInputMode = mode_shake;
systemSettings.Profile.smartActiveEnabled = disable;
systemSettings.Profile.smartActiveLoad = 30;
systemSettings.Profile.standDelay = 0;
systemSettings.Profile.StandMode = mode_sleep;
systemSettings.Profile.state = initialized;
}
void loadProfile(uint8_t profile){
while(ADC_Status!=ADC_Idle);
__disable_irq();
HAL_IWDG_Refresh(&hiwdg);
uint16_t oldTemp=getUserTemperature();
systemSettings.currentProfile=profile;
if(profile==profile_None){ // If profile not initialized yet, use T12 values until the system is configured
systemSettings.currentProfile=profile_T12; // Force T12 profile
resetCurrentProfile(); // Load default data
systemSettings.currentProfile=profile_None; // Revert to none to trigger setup screen
}
else if(profile<=profile_C210){ // If valid profile
if(flashProfilesSettings.Profile[profile].state!=initialized){ // If flash profile not initialized
resetCurrentProfile(); // Load defaults
systemSettings.ProfileChecksum = ChecksumProfile(&systemSettings.Profile);
}
else{
systemSettings.Profile = flashProfilesSettings.Profile[profile];
systemSettings.ProfileChecksum = flashProfilesSettings.ProfileChecksum[profile];
}
// Calculate data checksum and compare with stored checksum, also ensure the stored ID is the same as the requested profile
if( (profile!=systemSettings.Profile.ID) || (systemSettings.ProfileChecksum != ChecksumProfile(&systemSettings.Profile)) ){
checksumError(reset_Profile);
}
}
else{
Error_Handler();
}
setSystemTempUnit(getSystemTempUnit()); // Ensure the profile uses the same temperature unit as the system
setUserTemperature(systemSettings.Profile.defaultTemperature);
if((oldTemp > systemSettings.Profile.MaxSetTemperature) || (oldTemp < systemSettings.Profile.MinSetTemperature) || systemSettings.settings.loadDefTemp) //load default temp from profile if its not initialized or loadDefTemp is active
setUserTemperature(systemSettings.Profile.defaultTemperature);
else
setUserTemperature(oldTemp);
setCurrentTip(profileTipRam.bootTip[systemSettings.currentProfile]);
TIP.filter=systemSettings.Profile.tipFilter;
ironSchedulePwmUpdate();
__enable_irq();
}
static void Flash_error(void){
__disable_irq();
HAL_FLASH_Lock();
__enable_irq();
fatalError(error_FLASH);
}
static void checksumError(uint8_t mode){
Oled_error_init();
putStrAligned("BAD CHECKSUM!", 0, align_center);
putStrAligned("RESTORING THE", 20, align_center);
if(mode==reset_Profile){
putStrAligned("PROFILE", 36, align_center);
}
#ifdef ENABLE_ADDONS
else if(mode == reset_Addons)
{
putStrAligned("ADDONS", 36, align_center);
}
#endif
else{
putStrAligned("SYSTEM", 36, align_center);
}
update_display();
ErrCountDown(3,117,50);
if(mode == reset_Profile){
resetCurrentProfile();
saveSettings(keepProfiles);
}
#ifdef ENABLE_ADDONS
else if(mode == reset_Addons)
{
resetAddonSettings();
saveSettings(keepProfiles);
}
#endif
else{
resetSystemSettings();
saveSettings(wipeProfiles);
}
NVIC_SystemReset();
}
static void Button_reset(void){
uint16_t ResetTimer= HAL_GetTick();
if(!BUTTON_input()){
Oled_error_init();
putStrAligned("HOLD BUTTON", 10, align_center);
putStrAligned("TO RESTORE", 26, align_center);
putStrAligned("DEFAULTS", 42, align_center);
update_display();
while(!BUTTON_input()){
HAL_IWDG_Refresh(&hiwdg);
if((HAL_GetTick()-ResetTimer)>5000){
fillBuffer(BLACK,fill_dma);
putStrAligned("RELEASE", 16, align_center);
putStrAligned("BUTTON NOW", 32, align_center);
update_display();
while(!BUTTON_input()){
HAL_IWDG_Refresh(&hiwdg);
}
resetSystemSettings();
#ifdef ENABLE_ADDONS
resetAddonSettings();
#endif
saveSettings(wipeProfiles);
NVIC_SystemReset();
}
}
}
}
bool isCurrentProfileChanged(void)
{
return ChecksumProfile(&systemSettings.Profile) != systemSettings.ProfileChecksum;
}
//Max 99 seconds countdown.
static void ErrCountDown(uint8_t Start,uint8_t xpos, uint8_t ypos){
uint32_t timErr = 0;
char str[5];
uint8_t length;
if(Start>99){Start=99;}
if(Start>9){
length=2;
}
else{
length=1;
}
do{
HAL_IWDG_Refresh(&hiwdg);
}while(oled.status!=oled_idle);
while(Start){
timErr=HAL_GetTick();
u8g2_SetDrawColor(&u8g2, BLACK);
u8g2_DrawBox(&u8g2,xpos,ypos,u8g2_GetUTF8Width(&u8g2,str),u8g2_GetMaxCharHeight(&u8g2));
u8g2_SetDrawColor(&u8g2, WHITE);
sprintf(&str[0],"%*u",length-1,Start);
u8g2_DrawUTF8(&u8g2,xpos,ypos,str);
update_display();
while( (HAL_GetTick()-timErr)<999 ){
HAL_IWDG_Refresh(&hiwdg);
}
Start--;
}
}
static uint64_t readTempInit(uint32_t addr, uint32_t * cnt, uint32_t size) //call it only once during init
{
if(size>3 || size<1) return 0;
uint64_t valFlash=0, valRam=0;