forked from CONTROLLINO-PLC/CONTROLLINO_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControllino.cpp
479 lines (439 loc) · 10.4 KB
/
Controllino.cpp
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
/** @file Controllino.cpp
* @brief CONTROLLINO PLC extension for the Arduino IDE - implementation
*
* CONTROLLINO - First software open-source PLC (ARDUINO compatible)
*
* https://controllino.biz/
* https://github.com/CONTROLLINO-PLC/CONTROLLINO_Library
* https://www.arduino.cc/
*
* @author CONTROLLINO design team
* @version 1.1.0
* @date 2017-01-24
* @bug No known bugs.
*/
#include "Controllino.h"
boolean isRTCInitialized = false;
char Controllino_RTC_init(unsigned char aChipSelect)
{
unsigned char SPISetting; // variable to hold SPI setting
//Store current SPI setting
SPI.begin();
SPISetting = SPCR;
Controllino_RTCSSInit();
// start the SPI library:
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
// SW reset of RTC
Controllino_SetRTCSS(HIGH);
SPI.transfer(0x10); //0x10 is Control_1 register
SPI.transfer(B01011000); // SW reset command
Controllino_SetRTCSS(LOW);
//Return the SPI settings to previous state
SPCR = SPISetting;
isRTCInitialized = true;
return 0;
}
#define ARRAYSIZE 7 //Six time values and skipped index
//=====================================
char Controllino_SetTimeDate(unsigned char aDay, unsigned char aWeekDay,unsigned char aMonth, unsigned char aYear, unsigned char aHour, unsigned char aMinute, unsigned char aSecond)
{
unsigned char TimeDate [ARRAYSIZE]={aSecond,aMinute,aHour,aDay,aWeekDay,aMonth,aYear}; //we use a zero in the middle of the array as thats how the data are stored inside the the chip
unsigned char i,a,b,temp;//Help variable i is used to control for cycle and a,b are used to prepare time data for the RTC chip.
unsigned char SPISetting; // variable to hold SPI setting
if (isRTCInitialized)
{
//Store SPI setting
SPISetting = SPCR;
// start the SPI library:
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
//go through all the time values
for(i = 0; i < ARRAYSIZE;i++)
{
if (i == 0) //seconds
{
b = TimeDate[i]/40; //get the 40s
b = b<<6;
a = TimeDate[i]%40; // get the rest
temp = a/20; //get the 20s
temp = temp<<5;
b = b+temp;
a = a%20; // get the rest
temp = a/10; //get the 10s
temp = temp<<4;
b = b+temp;
a = a%10; // get the rest
}
if (i == 1) //minutes
{
b = TimeDate[i]/40; //get the 40s
b = b<<6;
a = TimeDate[i]%40; // get the rest
temp = a/20; //get the 20s
temp = temp<<5;
b = b+temp;
a = a%20; // get the rest
temp = a/10; //get the 10s
temp = temp<<4;
b = b+temp;
a = a%10; // get the rest
}
if (i == 2) //hours
{
b = TimeDate[i]/10; //get the 10s
b = b<<4;
a = TimeDate[i]%10; // get the rest
}
if (i == 3) //days
{
b = TimeDate[i]/20; //get the 20s
b = b<<5;
a = TimeDate[i]%20; // get the rest
temp = a/10; //get the 10s
temp = temp<<4;
b = b+temp;
a = a%10; // get the rest
}
if (i == 4) //weekdays
{
b = 0;
a = TimeDate[i];
}
if (i == 5) //months
{
b = TimeDate[i]/10; //get the 10s
b = b<<4;
a = TimeDate[i]%10; // get the rest
}
if (i == 6) //years
{
b = TimeDate[i]/80; //get the 80s
b = b<<7;
a = TimeDate[i]%80; // get the rest
temp = a/40; //get the 40s
temp = temp<<6;
b = b+temp;
a = TimeDate[i]%40; // get the rest
temp = a/20; //get the 20s
temp = temp<<5;
b = b+temp;
a = a%20; // get the rest
temp = a/10; //get the 10s
temp = temp<<4;
b = b+temp;
a = a%10; // get the rest
}
TimeDate[i]= a + b;
Controllino_SetRTCSS(HIGH);
SPI.transfer(i + 0x12); //0x12 is starting address for write
SPI.transfer(TimeDate[i]);
Controllino_SetRTCSS(LOW);
}
//Return the SPI settings to previous state
SPCR = SPISetting;
return 0;
}
else
{
return -1; //RTC chip was initialized properly, return -1
}
}
char Controllino_ReadTimeDate(unsigned char *aDay, unsigned char *aWeekDay, unsigned char *aMonth, unsigned char *aYear, unsigned char *aHour, unsigned char *aMinute, unsigned char *aSecond)
{
unsigned char i,a,b,c,d,e; //help variables a,b are used to store modified time information for a while during readout, and i is used for for cycle
unsigned int n; //n stores exact unmodified recieved data from the chip
int timeDate [ARRAYSIZE]; //second,minute,hour,weekday,day,month,year
unsigned char SPISetting; // variable to hold SPI setting
if (isRTCInitialized)
{
//Store SPI setting
SPISetting = SPCR;
// start the SPI library:
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
for(i = 0; i < ARRAYSIZE;i++)
{
Controllino_SetRTCSS(HIGH);
SPI.transfer(i + 0x92); //0x92 is starting address for read
n = SPI.transfer(0x00);
Controllino_SetRTCSS(LOW);
a = n & B00001111;
if(i == 2) //hours, 24 hours mode
{
c = (n & B00100000)>>5;
b = (n & B00010000)>>4;
timeDate[i] = a + (b * 10) + (c * 20);
}
else if(i == 3) //day
{
c = (n & B00100000)>>5;
b = (n & B00010000)>>4;
timeDate[i] = a + (b * 10) + (c * 20);
}
else if(i == 4) //weekday
{
timeDate[i] = a;
}
else if(i == 5) //month
{
b = (n & B00010000)>>4;
timeDate[i] = a + (b * 10);
}
else if(i == 6) //year
{
e = (n & B10000000)>>7;
d = (n & B01000000)>>6;
c = (n & B00100000)>>5;
b = (n & B00010000)>>4;
timeDate[i] = a + (b * 10) + (c * 20) + (d * 40) + (e * 80);
}
else //minute,second
{
d = (n & B01000000)>>6;
c = (n & B00100000)>>5;
b = (n & B00010000)>>4;
timeDate[i] = a + (b * 10) + (c * 20) + (d * 40);
}
}
//if supplied pointer was NOT NULL, return the recieved data
if (aDay != NULL)
{
*aDay = timeDate[3];
}
if (aWeekDay != NULL)
{
*aWeekDay = timeDate[4];
}
if (aMonth != NULL)
{
*aMonth = timeDate[5];
}
if (aYear != NULL)
{
*aYear = timeDate[6];
}
if (aHour != NULL)
{
*aHour = timeDate[2];
}
if (aMinute != NULL)
{
*aMinute = timeDate[1];
}
if (aSecond != NULL)
{
*aSecond = timeDate[0];
}
//Return the SPI settings to previous state
SPCR = SPISetting;
return 0;
}
else
{
return -1;//RTC chip was initialized properly, return -1
}
}
char Controllino_GetDay( void )
{
unsigned char day;
if (Controllino_ReadTimeDate(&day, NULL, NULL, NULL, NULL, NULL, NULL) >= 0)
{
return day;
}
else
{
return -1;//RTC chip was initialized properly, return -1
}
}
char Controllino_GetWeekDay( void )
{
unsigned char weekday;
if (Controllino_ReadTimeDate(NULL, &weekday, NULL, NULL, NULL, NULL, NULL) >= 0)
{
return weekday;
}
else
{
return -1;//RTC chip was initialized properly, return -1
}
}
char Controllino_GetMonth( void )
{
unsigned char month;
if (Controllino_ReadTimeDate(NULL, NULL, &month, NULL, NULL, NULL, NULL) >= 0)
{
return month;
}
else
{
return -1;//RTC chip was initialized properly, return -1
}
}
char Controllino_GetYear( void )
{
unsigned char year;
if (Controllino_ReadTimeDate(NULL, NULL, NULL, &year, NULL, NULL, NULL) >= 0)
{
return year;
}
else
{
return -1;//RTC chip was initialized properly, return -1
}
}
char Controllino_GetHour( void )
{
unsigned char hour;
if (Controllino_ReadTimeDate(NULL, NULL, NULL, NULL, &hour, NULL, NULL) >= 0)
{
return hour;
}
else
{
return -1;//RTC chip was initialized properly, return -1
}
}
char Controllino_GetMinute( void )
{
unsigned char minute;
if (Controllino_ReadTimeDate(NULL, NULL, NULL, NULL, NULL, &minute, NULL) >= 0)
{
return minute;
}
else
{
return -1;//RTC chip was initialized properly, return -1
}
}
char Controllino_GetSecond( void )
{
unsigned char second;
if (Controllino_ReadTimeDate(NULL, NULL, NULL, NULL, NULL, NULL, &second) >= 0)
{
return second;
}
else
{
return -1;//RTC chip was initialized properly, return -1
}
}
char Controllino_PrintTimeAndDate( void )
{
unsigned char day, weekday, month, year, hour, minute, second;
if (Controllino_ReadTimeDate(&day, &weekday, &month, &year, &hour, &minute, &second) >= 0)
{
Serial.print(day);
Serial.print("/");
Serial.print(month);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
return 0;
}
else
{
return -1;//RTC chip was initialized properly, return -1
}
}
#if defined(CONTROLLINO_MAXI) || defined(CONTROLLINO_MEGA)
char Controllino_RS485Init( void )
{
//set PORTJ pin 5,6 direction (RE,DE)
DDRJ |= B01100000;
//set RE,DE on LOW
PORTJ &= B10011111;
return 0;
}
char Controllino_SwitchRS485RE(char mode)
{
if (mode == 0)
{
// set RE on LOW
PORTJ &= B11011111;
}
else if (mode == 1)
{
// set RE on HIGH
PORTJ |= B00100000;
}
else return -1; // Unknown mode value
return 0;
}
char Controllino_SwitchRS485DE(char mode)
{
if (mode == 0)
{
// set DE on LOW
PORTJ &= B10111111;
}
else if (mode == 1)
{
// set DE on HIGH
PORTJ |= B01000000;
}
else return -1; // Unknown mode value
return 0;
}
#endif
char Controllino_RTCSSInit( void )
{
#if defined(CONTROLLINO_MAXI) || defined(CONTROLLINO_MEGA) || defined(CONTROLLINO_MAXI_AUTOMATION)
//set PORTJ pin 2 RTC SS direction and LAN SS (PORTJ pin 3) as well for safety
DDRJ |= B00001100;
// pinMode(CONTROLLINO_PIN_HEADER_DIGITAL_OUT_00,OUTPUT); // DEBUG
// set RTC SS on LOW and LAN SS on HIGH (Negated)
PORTJ &= B11111011;
PORTJ |= B00001000;
// digitalWrite(CONTROLLINO_PIN_HEADER_DIGITAL_OUT_00, LOW); // DEBUG
return 0;
#endif
#if defined(CONTROLLINO_MINI)
pinMode(CONTROLLINO_PIN_HEADER_SS,OUTPUT);
digitalWrite(CONTROLLINO_PIN_HEADER_SS, LOW);
return 0;
#endif
return -2; // No Controllino board is selected
}
char Controllino_SetRTCSS(char mode)
{
#if defined(CONTROLLINO_MAXI) || defined(CONTROLLINO_MEGA) || defined(CONTROLLINO_MAXI_AUTOMATION)
if (mode == 0)
{
// set RTC SS on LOW
PORTJ &= B11111011;
// digitalWrite(CONTROLLINO_PIN_HEADER_DIGITAL_OUT_00, LOW); // DEBUG
}
else if (mode == 1)
{
// set RTC SS on HIGH
PORTJ |= B00000100;
// digitalWrite(CONTROLLINO_PIN_HEADER_DIGITAL_OUT_00, HIGH); // DEBUG
}
else return -1; // Unknown mode value
return 0;
#endif
#if defined(CONTROLLINO_MINI)
if (mode == 0)
{
// set RTC SS on LOW
digitalWrite(CONTROLLINO_PIN_HEADER_SS, LOW);
}
else if (mode == 1)
{
// set RTC SS on HIGH
digitalWrite(CONTROLLINO_PIN_HEADER_SS, HIGH);
}
else return -1; // Unknown mode value
return 0;
#endif
return -2; // No Controllino board is selected
}