This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
174 lines (143 loc) · 3.99 KB
/
main.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
/*
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Version: 3.0
* Design: David Gascon
* Implementation: Yuri Carmona
*/
#include <Waspmote.h>
//define global variable for Waspmote serial id
volatile uint8_t _serial_id[8];
//define global variable for Waspmote bootloader version
volatile uint8_t _boot_version;
int main(void)
{
init();
// switch on main power supply
pinMode(POWER_3V3, OUTPUT);
digitalWrite(POWER_3V3,HIGH);
// get bootloader version
_boot_version = Utils.getBootVersion();
// proceed depending on the bootloader version
if (_boot_version >= 'E')
{
pinMode(RTC_SLEEP, OUTPUT);
digitalWrite(RTC_SLEEP, HIGH);
}
uint8_t rtc_hibernate_triggered = digitalRead(RTC_INT_PIN_MON);
// proceed depending on the bootloader version
if (_boot_version >= 'G')
{
RTC.ON();
RTC.disableSQW();
RTC.OFF();
}
// Check OTA EEPROM flag and mark flag in Waspmote
// Control Register if corresponds to
if (Utils.readEEPROM(0x01) == 0x01)
{
// set register flag
WaspRegister |= REG_OTA;
// clear eeprom flag
eeprom_write_byte((unsigned char *) 0x01, 0x00);
}
// validate Hibernate interruption when both RTC interruption
// signal and hibernate EEPROM flag are active
if (rtc_hibernate_triggered && (Utils.readEEPROM(HIB_ADDR)==HIB_VALUE))
{
// get RTC time and last almarm setting
RTC.ON();
RTC.getAlarm1();
RTC.getTime();
// when the interruption and startup has been produced within 3 seconds
// then we validate the Hibernate interruption. If not, maybe the
// Waspmote startup conditions may show the hibernate startup conditions
// when it is not true
int total_diff = RTC.second - RTC.second_alarm1;
// check seconds zero crossing
if (RTC.minute - RTC.minute_alarm1 > 0)
{
total_diff = total_diff + (RTC.minute - RTC.minute_alarm1)*60;
}
// check minute zero crossing
if (RTC.minute - RTC.minute_alarm1 < 0)
{
total_diff = total_diff + (RTC.minute-RTC.minute_alarm1+60)*60;
}
if ((total_diff < 3) && (total_diff >= 0))
{
intFlag |= HIB_INT;
}
}
// disable both RTC alarms
RTC.ON();
RTC.disableAlarm1();
RTC.disableAlarm2();
RTC.OFF();
// Improve consumption if there is an XBee in SOCKET0 and
// also a SPI sensor board is connected
pinMode(SOCKET0_SS, INPUT);
delay(3);
if (WaspRegister & REG_SX)
{
delay(3);
// Powering the module
PWR.powerSocket(SOCKET0, HIGH);
delay(3);
//Configure the MISO, MOSI, CS, SPCR.
SPI.begin();
delay(3);
//Set Most significant bit first
SPI.setBitOrder(MSBFIRST);
delay(3);
//Divide the clock frequency
SPI.setClockDivider(SPI_CLOCK_DIV2);
delay(3);
//Set data mode
SPI.setDataMode(SPI_MODE0);
delay(3);
//finish
SPI.end();
delay(3);
// disable all SPI slaves
SPI.setSPISlave(ALL_DESELECTED);
delay(3);
// Powering the module
PWR.powerSocket(SOCKET0, LOW);
delay(3);
}
delay(3);
// get serial id
Utils.readSerialID();
// set random seed
unsigned int seed = 0;
seed += _serial_id[6] * 0x100;
seed += _serial_id[7];
srand(seed);
// check for XBee modules in SOCKET0
//PWR.checkPeripherals();
// close UART
closeSerial(0);
// switch off mux on uart0
if (_boot_version >= 'G')
{
Utils.muxOFF0();
}
setup();
for (;;) {
loop();
}
return 0;
}