← Back to dashboard

File view

plc_configuration.lib

1
/*** BeginHeader */
2
#ifndef ISM_CONFIGURATION_LIB
3
#define ISM_CONFIGURATION_LIB
4
/*** EndHeader */
5
6
/*** BeginHeader
7
	InitFileSystem,
8
	GetConfig,
9
	CheckConfig,
10
   WriteConfig
11
   ClearConfig,
12
	BatteryNotOK,
13
	SetTestWord,
14
	ClearTestWord
15
	InitConfig
16
	InitProduct
17
	InitChannel
18
*/
19
20
// Global function declorations
21
22
int  InitFileSystem	(void);
23
int  GetConfig			(void);
24
int  CheckConfig		(void);
25
int  WriteConfig		(void);
26
int  ClearConfig		(void);
27
void InitConfig		(void);
28
void InitProduct		(uint8_t i);
29
void InitChannel		(uint8_t i);
30
31
/*** EndHeader */
32
33
root configStruct 		tmpConfig;
34
root productStruct      tmpProduct[N_PROD];
35
36
// Local Declaration ***********************************************************
37
38
static char FileBuffer[256];
39
40
// InitFileSystem **************************************************************
41
42
int InitFileSystem(void)
43
{
44
	FSLXnum 	flash1;                   // logical extent number
45
	Ms			Lxfree;
46
	int 		result;
47
	
48
	result = 0;
49
	flash1 = fs_get_flash_lx();
50
   if (flash1) {
51
   	result = fs_init(0, 0);
52
   	if (result) 
53
   		printf("Flash Init Error: %d\n", result);
54
   	else {
55
	      printf("File System Init - %d\n\n",result);
56
	      printf("Flash File System Enabled\n");
57
	      printf("# of Max Devices - %d\n", FS_MAX_DEVICES);
58
	      printf("# of Max Logical - %d\n", FS_MAX_LX);
59
	      printf("# of Max Files   - %d\n", FS_MAX_FILES);
60
	      printf("XMEM Res Size    - %u\n", XMEM_RESERVE_SIZE);
61
	      printf("Program Size XB  - %d\n", FS2_USE_PROGRAM_FLASH);
62
	      printf("lx capacity      - %u\n", fs_get_lx_size(flash1, 1, 0));
63
	      Lxfree = (Ms)fs_get_lx_size(flash1, 0, 0);
64
	      printf("lx free          - %u\n\n", Lxfree);
65
			printf("Log Records Free - %u\n\n", Lxfree / 8);	
66
		}     
67
   }
68
   else {
69
   	printf("Flash Init Error: No Flash System\n", FS_MAX_DEVICES);
70
   	result = 10;
71
   }
72
   
73
	return result;
74
}
75
76
// GetConfig *********************************************************************
77
78
int GetConfig(void) {
79
   int fileResult;
80
   int readResult;
81
   int configSize;
82
   int productSize;
83
   File configFile;
84
   FSLXnum flash1;                   // logical extent number
85
86
   fileResult  = 0;
87
   readResult  = 0;
88
   configSize  = sizeof(Config );
89
   productSize = sizeof(Product);
90
91
   #if FLASH_DATA
92
    	printf("Reading config file ... ");
93
      flash1 = fs_get_flash_lx();
94
      fileResult = fopen_rd(&configFile, CONFIG_FILE);
95
      if (fileResult)
96
	      printf("Config file not found - settings defaults | Error: %d\n", fileResult);
97
	   else {
98
	      readResult  = fread(&configFile, (char*)&tmpConfig,  configSize);
99
	      readResult += fread(&configFile, (char*)&tmpProduct, productSize);
100
	      fileResult  = fclose(&configFile);
101
102
	      if (readResult != configSize + productSize) {
103
	         printf("Read config file error. Bytes: %d | %d\n", readResult, configSize + productSize);
104
	         readResult = readResult - (configSize + productSize);
105
	      }
106
         else {
107
         	memcpy(&Config,  &tmpConfig,  sizeof(tmpConfig ));
108
	         memcpy(&Product, &tmpProduct, sizeof(tmpProduct));
109
            printf("Successful\n");
110
         	readResult = 0;	
111
         }
112
	   }
113
   #else
114
		fileResult = BatteryNotOK();
115
   #endif
116
117
   return fileResult + readResult;
118
}
119
120
// Check Config ****************************************************************
121
122
int CheckConfig(void) {
123
	int result;
124
	uint8_t i;
125
	uint8_t lastProductFlag;
126
127
	result = 1;
128
	lastProductFlag = 0;
129
130
	#if ISM4711_Default
131
	   Config.PresRetries = 2;
132
	   for (i = 0 ; i < 3 ; i++) {
133
	      Product[i].N_Turbines = 1;
134
	      Product[i].CV.SCV = 0;
135
	      Product[i].CV.PCV = 0;
136
	      Product[i].CV.DCV = 0;
137
	      Product[i].N_LeadTurbines = 1;
138
	      Product[i].N_RunTurbines = 1;
139
	      Product[i].N_PistonSwitches = 1;
140
	      Product[i].ProdName = i+1;
141
	      Product[i].RotateFlag = 0;
142
	      Product[i].AutoAuthFlag = 0;
143
	   }
144
	#endif
145
146
   for (i = 0 ; i < N_PROD ; i++) {
147
      if ((Product[i].N_Turbines < 1 || Product[i].N_Turbines > N_ISM) || lastProductFlag) {
148
         InitProduct(i);
149
         lastProductFlag = 1;
150
      }
151
   }
152
153
	if (Product[0].N_Turbines == 0)
154
		result = 0;
155
156
	return result;
157
}
158
159
// Write Config ********************************************************************
160
161
int WriteConfig(void) {
162
   int fileResult;
163
   int writeResult;
164
   int configSize;
165
   int productSize;
166
   File configFile;
167
   FSLXnum flash1;                   // logical extent number
168
169
   fileResult = 0;
170
   writeResult = 0;
171
   configSize  = sizeof(Config );
172
   productSize = sizeof(Product);
173
   
174
   #if FLASH_DATA
175
    	printf("Writing config file ... ");
176
   	flash1 = fs_get_flash_lx();
177
	   fileResult = fdelete(CONFIG_FILE);
178
		fileResult = fcreate(&configFile, CONFIG_FILE) && fopen_wr(&configFile, CONFIG_FILE);
179
      if(fileResult)
180
      	printf("Not able to open:  %d\n", fileResult);
181
		else {
182
      	memcpy(&tmpConfig,  &Config,  configSize);
183
	      memcpy(&tmpProduct, &Product, productSize);
184
                                                                                                                                       
185
	      writeResult  = fwrite(&configFile, (char*)&tmpConfig,  configSize);
186
	      writeResult += fwrite(&configFile, (char*)&tmpProduct, productSize);
187
         fileResult   = fclose(&configFile);
188
189
	      if (writeResult != configSize + productSize) {
190
	         printf("Write config file error. Bytes: %d | %d\n", writeResult, configSize + productSize);
191
	         writeResult = writeResult - (configSize + productSize);
192
	      }
193
         else {
194
            printf("Successful\n");
195
         	writeResult = 0;	
196
         }
197
	   }
198
   #else
199
   	SetTestWord();
200
   	fileResult = 0;
201
   #endif
202
203
   return fileResult + writeResult;
204
}
205
206
// Clear Config *********************************************************************
207
208
int ClearConfig(void)
209
{
210
	int fileResult;
211
	uint8_t i;
212
   File configFile;
213
214
	// Clear Files
215
   #if FLASH_DATA
216
	   fileResult = fdelete(CONFIG_FILE);
217
      if (!fileResult)
218
      	printf("Config File Deleted\n");
219
      #if FORMAT_FS
220
	      fileResult = fs_format(0, 0, 1);
221
	      if (!fileResult)
222
	         printf("Formated\n");
223
      #endif
224
	#else
225
	   ClearTestWord();
226
      fileResult = 0;
227
	#endif
228
	
229
	//Clear Structures
230
   printf("Initializing Structures\n");
231
   InitConfig();
232
233
   for (i = 0 ; i < N_PROD ; i++)
234
      InitProduct(i);
235
      
236
	for (i = 0 ; i < N_ISM  ; i++)
237
      InitChannel(i);
238
	
239
   return fileResult;
240
}
241
242
// Config Initilazation ******************************************************
243
244
void InitConfig (void) {
245
	uint8_t i; 
246
	Config.SiteConfigFlag		 = 0;
247
	Config.MenuFlag				 = 0;
248
   Config.AlarmSilenceFlag		 = 0;
249
   Config.TestCnl					 = -1;
250
   Config.ViewCnl					 = -1;
251
   Config.DspInputStatus		 = 0;
252
   Config.DspNetworkStatus		 = 0;
253
	Config.N_Products				 = 0;
254
	Config.N_Turbines				 = 0;
255
   Config.N_CVs					 = 0;
256
   Config.N_PS						 = 0;
257
   Config.UpdateFlag				 = 0;
258
   Config.PresRetries			 = 2;
259
   Config.RePresRetries			 = 2;
260
   Config.TestMode				 = 0;
261
   Config.ShutDownMode			 = 2;
262
   Config.FlowDelay				 = 30;
263
   Config.PresCheckDelay		 = 60;
264
   
265
	Config.Sump.NoSumpFlag 		= 1;
266
	Config.Sump.AlarmPos 		= SUMP_OPEN;
267
	Config.Sump.ShutDnFlag 		= 0;
268
	Config.Sump.PerProdFlag 	= 0;
269
	for (i = 0 ; i < N_INPUTS  ; i++)
270
		Config.Sump.Status[i] 	= 0;	
271
		
272
	Config.ModBus.Address 		 = 1;
273
	Config.ModBus.Baudidx 		 = 6;
274
	Config.ModBus.Offsetidx 	 = 0;
275
	Config.ModBus.ActivityTimer = 0;
276
}
277
278
// Channel Initilazation ******************************************************
279
280
void InitChannel(uint8_t i) {
281
   Channel[i].StartTime       = SEC_TIMER;
282
   Channel[i].DurationTime    = DEFAULT_RUN;
283
   Channel[i].DelayTime       = DEFAULT_DELAY;
284
   Channel[i].OnDly           = 0;
285
   Channel[i].OffDly          = 0;
286
   Channel[i].FlowTime        = 0;
287
   Channel[i].Turbine         = 0;
288
   Channel[i].RdyFlag         = 0;
289
   Channel[i].ShuntValve      = 0;
290
   Channel[i].State           = 0;
291
   Channel[i].PreviousState   = -1;
292
   Channel[i].Authorize       = 0;
293
   Channel[i].PresFlag        = 0;
294
   Channel[i].PresOkFlag      = 0;
295
   Channel[i].PresAlmFlag     = 0;
296
   Channel[i].SumpAlmFlag     = 0;
297
   Channel[i].ResetPbFlag     = 0;
298
   Channel[i].DisableFlag     = 0;
299
   Channel[i].FlowFlag        = 0;
300
   Channel[i].SensorOutFlag   = 0;
301
   Channel[i].NeverLeadFlag   = 0;
302
   Channel[i].ByPass          = 0;
303
   Channel[i].OverRideFlag    = 0;
304
   Channel[i].IOChannel       = i;
305
   Channel[i].IOPS            = i;
306
   Channel[i].Idx 				= i;
307
   Channel[i].ID              = i + 1;
308
   Channel[i].ProductID       = i + 1;
309
   Channel[i].LeadFlag        = 0;
310
   Channel[i].NextStage       = 0;
311
   Channel[i].AuthOff         = 0;
312
   Channel[i].Retries         = 1;
313
   Channel[i].ResilVal        = DEFAULT_RESIL;
314
   Channel[i].NoReadIS        = 0;
315
   Channel[i].WarnFlag        = 0;
316
   Channel[i].AlarmFlag       = 0;
317
   Channel[i].lastState       = 0;             
318
   Channel[i].lastPres        = 0;             
319
   Channel[i].RunPressure     = DEFAULT_HIGH_PRES;
320
   Channel[i].HighPressure    = DEFAULT_HIGH_PRES;
321
   Channel[i].LowPressure     = DEFAULT_LOW_PRES;
322
   Channel[i].Event.Time		= 0;
323
   
324
   Channel[i].Prod				= &Product[i];
325
   Channel[i].LDN					= &LDNS[i];
326
   
327
   InitEvent	  (&Channel[i].Event);
328
   TestInitialize(&Channel[i].GPH3Test);
329
   TestInitialize(&Channel[i].PrecTest);
330
}
331
332
// Product Initilazation ******************************************************
333
334
void InitProduct (uint8_t i) {
335
   Product[i].CheckTime				= MS_TIMER;
336
   Product[i].LeadTurbine			= 0;
337
	Product[i].Authorize				= 0;
338
	Product[i].N_Turbines			= 0;
339
   Product[i].N_LeadTurbines		= 0;
340
   Product[i].N_RunTurbines		= 0;
341
   Product[i].N_PistonSwitches	= 0;
342
	Product[i].FirstTurbine			= 0;
343
   Product[i].RotateFlag			= 1;
344
   Product[i].AutoAuthFlag			= 1;
345
   Product[i].FilterFlag			= 0;
346
   Product[i].ProdName				= i + 1;
347
   Product[i].ResilVal				= DEFAULT_RESIL;
348
	Product[i].DisStartTimeHours	= 0;
349
   Product[i].DisStartTimeMin		= 0;
350
	Product[i].DisEndTimeHours		= 0;
351
   Product[i].DisEndTimeMin		= 0;
352
   Product[i].RunPressure			= DEFAULT_HIGH_PRES;	
353
   Product[i].HighPressure			= DEFAULT_HIGH_PRES;
354
   Product[i].LowPressure			= DEFAULT_LOW_PRES;
355
   Product[i].RePresCount			= 0;
356
   Product[i].CheckPres				= 0;
357
   Product[i].ReturnFuel			= 0;
358
	Product[i].Idx						= i;
359
								
360
	Product[i].CV.OnDly 				= 0;
361
	Product[i].CV.OffDly 			= 0;
362
	Product[i].CV.SCV 				= 0;
363
	Product[i].CV.scvIOChannel 	= 0;
364
	Product[i].CV.scvOnDly 			= 0;
365
	Product[i].CV.scvOffDly 		= 0;
366
	Product[i].CV.PCV 				= 0;
367
	Product[i].CV.pcvIOChannel 	= 0;
368
	Product[i].CV.pcvOnDly 			= 0;
369
	Product[i].CV.pcvOffDly 		= 0;
370
	Product[i].CV.DCV 				= 0;
371
	Product[i].CV.dcvIOChannel 	= 0;
372
	Product[i].CV.dcvOnDly 			= 0;
373
	Product[i].CV.dcvOffDly 		= 0;
374
	Product[i].CV.firstAuthFlag 	= 0;
375
	Product[i].CV.authStart 		= 0;
376
	Product[i].CV.CVLock 			= 0;
377
}
378
379
// Test Battery ********************************************************************
380
381
int  BatteryNotOK (void) {return (TestWord != 0x55);}
382
void SetTestWord  (void) {TestWord = 0x55;}			// set test word to known pattern
383
void ClearTestWord(void) {TestWord++;}      			// needs to be root because it is
384
																	//   called indirectly from keyboard.lib
385
386
/*** BeginHeader */
387
#endif
388
/*** EndHeader */