forked from corygrant/dingoFW
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cpp
More file actions
211 lines (181 loc) · 6.63 KB
/
Copy pathconfig.cpp
File metadata and controls
211 lines (181 loc) · 6.63 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
#include "config.h"
#include "error.h"
#include "crc.h"
MB85RC fram(I2CD1, MB85RC_I2CADDR_DEFAULT);
bool ReadConfig(){
// Read config
if(!fram.Read(0x0, (uint8_t*)&stConfig, sizeof(stConfig))) {
return false;
}
// Check version number
if(stConfig.stDevConfig.nConfigVersion != CONFIG_VERSION) {
return false;
}
// Read stored data CRC
uint32_t storedCrc = 0;
if(!fram.Read(sizeof(stConfig), (uint8_t*)&storedCrc, sizeof(storedCrc))) {
return false;
}
// Calculate CRC of the data we just read
uint32_t calculatedCrc = CalculateCRC32(&stConfig, sizeof(stConfig));
// Compare CRCs to verify data integrity
if(storedCrc != calculatedCrc) {
return false; // Data corrupt or changed
}
return true;
}
bool WriteConfig(){
if(!fram.CheckId()) {
return false;
}
// Make sure the version is current
stConfig.stDevConfig.nConfigVersion = CONFIG_VERSION;
// Write config
if(!fram.Write(0x0, (uint8_t*)&stConfig, sizeof(stConfig))) {
return false;
}
// Calculate config CRC
uint32_t dataCrc = CalculateCRC32(&stConfig, sizeof(stConfig));
// Write CRC after config
if(!fram.Write(sizeof(stConfig), (uint8_t*)&dataCrc, sizeof(dataCrc))) {
return false;
}
return true;
}
void SetDefaultConfig()
{
stConfig.stDevConfig.nConfigVersion = CONFIG_VERSION;
stConfig.stDevConfig.eCanSpeed = CanBitrate::Bitrate_500K;
stConfig.stDevConfig.bCanFilterEnabled = false;
stConfig.stDevConfig.bSleepEnabled = true;
#if PDM_NUM_INPUTS > 0
for(uint8_t i = 0; i < PDM_NUM_INPUTS; i++)
{
stConfig.stInput[i].bEnabled = false;
stConfig.stInput[i].eMode = InputMode::Momentary;
stConfig.stInput[i].bInvert = true;
stConfig.stInput[i].nDebounceTime = 20;
stConfig.stInput[i].ePull = InputPull::Up;
}
#endif
for(uint8_t i = 0; i < PDM_NUM_VIRT_INPUTS; i++)
{
stConfig.stVirtualInput[i].bEnabled = false;
stConfig.stVirtualInput[i].bNot0 = false;
stConfig.stVirtualInput[i].nVar0 = 0;
stConfig.stVirtualInput[i].eCond0 = BoolOperator::And;
stConfig.stVirtualInput[i].bNot1 = false;
stConfig.stVirtualInput[i].nVar1 = 0;
stConfig.stVirtualInput[i].eCond1 = BoolOperator::And;
stConfig.stVirtualInput[i].bNot2 = false;
stConfig.stVirtualInput[i].nVar2 = 0;
stConfig.stVirtualInput[i].eMode = InputMode::Momentary;
}
for(uint8_t i = 0; i < PDM_NUM_OUTPUTS; i++)
{
stConfig.stOutput[i].bEnabled = false;
stConfig.stOutput[i].nInput = 0;
stConfig.stOutput[i].nCurrentLimit = 20;
stConfig.stOutput[i].nInrushLimit = 30;
stConfig.stOutput[i].nInrushTime = 1000;
stConfig.stOutput[i].eResetMode = ProfetResetMode::None;
stConfig.stOutput[i].nResetTime = 1000;
stConfig.stOutput[i].nResetLimit = 0;
}
stConfig.stWiper.bEnabled = false;
stConfig.stWiper.eMode = WiperMode::DigIn;
stConfig.stWiper.nSlowInput = 0;
stConfig.stWiper.nFastInput = 0;
stConfig.stWiper.nInterInput = 0;
stConfig.stWiper.nOnInput = 0;
stConfig.stWiper.nSpeedInput = 0;
stConfig.stWiper.nParkInput = 0;
stConfig.stWiper.bParkStopLevel = false;
stConfig.stWiper.nSwipeInput = 0;
stConfig.stWiper.nWashInput = 0;
stConfig.stWiper.nWashWipeCycles = 0;
stConfig.stWiper.eSpeedMap[0] = WiperSpeed::Intermittent1;
stConfig.stWiper.eSpeedMap[1] = WiperSpeed::Intermittent2;
stConfig.stWiper.eSpeedMap[2] = WiperSpeed::Intermittent3;
stConfig.stWiper.eSpeedMap[3] = WiperSpeed::Intermittent4;
stConfig.stWiper.eSpeedMap[4] = WiperSpeed::Intermittent5;
stConfig.stWiper.eSpeedMap[5] = WiperSpeed::Intermittent6;
stConfig.stWiper.eSpeedMap[6] = WiperSpeed::Slow;
stConfig.stWiper.eSpeedMap[7] = WiperSpeed::Fast;
stConfig.stWiper.nIntermitTime[0] = 1000;
stConfig.stWiper.nIntermitTime[1] = 2000;
stConfig.stWiper.nIntermitTime[2] = 3000;
stConfig.stWiper.nIntermitTime[3] = 4000;
stConfig.stWiper.nIntermitTime[4] = 5000;
stConfig.stWiper.nIntermitTime[5] = 6000;
for(uint8_t i = 0; i < PDM_NUM_FLASHERS; i++)
{
stConfig.stFlasher[i].bEnabled = false;
stConfig.stFlasher[i].nInput = 0;
stConfig.stFlasher[i].nFlashOnTime = 500;
stConfig.stFlasher[i].nFlashOffTime = 500;
stConfig.stFlasher[i].bSingleCycle = false;
}
stConfig.stStarter.bEnabled = false;
stConfig.stStarter.nInput = 0;
for(uint8_t i = 0; i < PDM_NUM_OUTPUTS; i++)
{
stConfig.stStarter.bDisableOut[i] = false;
}
for(uint8_t i = 0; i < PDM_NUM_CAN_INPUTS; i++)
{
stConfig.stCanInput[i].bEnabled = false;
stConfig.stCanInput[i].bTimeoutEnabled = true;
stConfig.stCanInput[i].nTimeout = 2000;
stConfig.stCanInput[i].nIDE = 0;
stConfig.stCanInput[i].nSID = 0;
stConfig.stCanInput[i].nEID = 0;
stConfig.stCanInput[i].nDLC = 0;
stConfig.stCanInput[i].nStartingByte = 0;
stConfig.stCanInput[i].eOperator = Operator::Equal;
stConfig.stCanInput[i].nOnVal = 0;
stConfig.stCanInput[i].eMode = InputMode::Momentary;
}
stConfig.stCanOutput.nBaseId = 2000;
for(uint8_t i=0; i < PDM_NUM_COUNTERS; i++)
{
stConfig.stCounter[i].bEnabled = false;
stConfig.stCounter[i].nIncInput = 0;
stConfig.stCounter[i].nDecInput = 0;
stConfig.stCounter[i].nResetInput = 0;
stConfig.stCounter[i].nMaxCount = 4;
stConfig.stCounter[i].nMinCount = 0;
stConfig.stCounter[i].bWrapAround = false;
}
for(uint8_t i = 0; i < PDM_NUM_CONDITIONS; i++)
{
stConfig.stCondition[i].bEnabled = false;
stConfig.stCondition[i].eOperator = Operator::Equal;
stConfig.stCondition[i].nInput = 0;
stConfig.stCondition[i].nArg = 0;
}
}
void InitConfig()
{
if(!fram.CheckId())
Error::SetFatalError(FatalErrorType::ErrFRAM, MsgSrc::Config);
if(!ReadConfig())
{
if(fram.GetErrors() != 0)
Error::SetFatalError(FatalErrorType::ErrFRAM, MsgSrc::Config);
//Write default for next power cycle
SetDefaultConfig();
if(!WriteConfig()){
//Couldn't write default config
//FRAM issue
if(fram.GetErrors() != 0)
Error::SetFatalError(FatalErrorType::ErrFRAM, MsgSrc::Config);
}
else
{
//Wrote default config
//Error to force power cycle
Error::SetFatalError(FatalErrorType::ErrConfig, MsgSrc::Config);
}
}
}