-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
294 lines (251 loc) · 7.21 KB
/
main.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
#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include "spi.h"
#include "nRF24L01.h"
#include "RF24.h"
#include "hdc1080.h"
#include "battery.h"
#include <string.h>
#include "utils.h"
#include "packets.h"
#include "build.h"
// Build Defines
#define RADIO_ENABLE 1
//#define WAKEUP_TIMER 1
//#define SENSOR_ENABLE 1
#define PIN_IRQ 1
// Wake period defines
#define SLEEP_PERIOD_RELOAD 0x70 // Appx 30 sec (was 0x70)
#define WAKE_COUNT 9 // 5 mins appx
#define BATTERY_SAMPLE_COUNT 12 // 1 Hours appx
/********************************************************************************************************/
// System clock defines
#define SYS_CLK_EN 0
#define SYS_SEL 2
#define SYS_DIV_EN 0 //0: Fsys=Fosc, 1: Fsys = Fosc/(2*CKDIV)
#define SYS_DIV 1
#define I2C_CLOCK 2
// Module data pipes
__xdata uint8_t pipeTx[5] = {0xc7, 0xc7, 0xc7, 0xc7, 0xc7};
__xdata uint8_t pipeRx[5] = {0xe7, 0xe7, 0xe7, 0xe7, 0xe7};
uint8_t wake_count, battery_count;
#if SENSOR_ENABLE
__xdata sDataPacket SensorPacket;
#endif
__xdata sIdentPacket IdentPacket;
#if PIN_IRQ
__xdata sTriggerPacket TriggerPacket;
#endif
volatile struct {
unsigned triggerflag :1;
unsigned triggerstate :1;
} flags;
#if PIN_IRQ
//==============================================
//
// Interrupt handler INT0
//
//==============================================
void pin_int07(void) __interrupt 0x3B
{
// Check which pin interrupted.
if (PIF & 0x08 )
{
flags.triggerflag = 1;
flags.triggerstate = 1;
}
//clear interrupt flag
PIF = 0x00;
}
#endif
#if WAKEUP_TIMER
//==============================================
//
// Interrupt handler for deeps sleep wakeup
//
//==============================================
void WakeUp_Timer_ISR(void) __interrupt 17
{
clr_WKTF; //clear interrupt flag
}
//==============================================
//
// Powerdown and sleep.
//
//==============================================
void powerdown_and_sleep(void)
{
//clr_TR3;
#if RADIO_ENABLE
powerDown();
#endif
clr_SPIEN;
while (wake_count--)
{
ADCCON1 = 0x00; // Turn off the ADC.
clr_BODEN; // Disable undervoltage detection.
set_PD; // Enter power-down mode.
}
wake_count = WAKE_COUNT;
// Sample battery periodically
if (!battery_count--)
{
// SensorPacket.mHeader.mBatteryVolts = read_battery_voltage() / 100;
battery_count = BATTERY_SAMPLE_COUNT;
}
set_SPIEN;
#if RADIO_ENABLE
powerUp();
#endif
}
#endif
//==============================================
//
// Send a powerup packet ot indicate device online.
//
//==============================================
void startup_packet_send(void)
{
// Initial SensorPacket content
IdentPacket.mHeader.mStart = PACKET_START;
IdentPacket.mHeader.mVersion = PACKET_VERSION;
IdentPacket.mHeader.mPacketType = ePacketTypeIdent;
IdentPacket.mHeader.mNodeId = get_device_id();
IdentPacket.mHeader.mCounter =0x00;
IdentPacket.mHeader.mBatteryVolts = read_battery_voltage() / 100;
IdentPacket.BuildData.mDay = BUILD_DAY;
IdentPacket.BuildData.mMonth = BUILD_MONTH;
IdentPacket.BuildData.mYear = BUILD_YEAR;
IdentPacket.BuildData.mHour = BUILD_HOUR;
IdentPacket.BuildData.mMmin = BUILD_MIN;
IdentPacket.BuildData.mSec = BUILD_SEC;
IdentPacket.BuildData.mVersioHi = BUILD_VER_HI;
IdentPacket.BuildData.mVersionLo = BUILD_VER_LO;
IdentPacket.mChecksum = make_packet_checksum((uint8_t *)&IdentPacket, sizeof(IdentPacket));
write((uint8_t *)&IdentPacket, sizeof(IdentPacket));
}
//==============================================
//
// Fill the reqiured contents and send.
//
//==============================================
#if SENSOR_ENABLE
void sensor_packet_send(void)
{
SensorPacket.mHeader.mCounter += 1;
Hdc1080Read(&SensorPacket.mSensorData.mTemperature, &SensorPacket.mSensorData.mHumidity);
SensorPacket.mHeader.mBatteryVolts = read_battery_voltage() / 100;
SensorPacket.mChecksum = make_packet_checksum((uint8_t *)&SensorPacket, sizeof(SensorPacket));
write((uint8_t *)&SensorPacket, sizeof(SensorPacket));
}
#endif
#if PIN_IRQ
void trigger_packet_send(void)
{
TriggerPacket.mHeader.mCounter += 1;
TriggerPacket.mTriggerData.triggerstate = P13;// (uint8_t) flags.triggerstate;
TriggerPacket.mHeader.mBatteryVolts = read_battery_voltage() / 100;
TriggerPacket.mChecksum = make_packet_checksum((uint8_t *)&TriggerPacket, sizeof(TriggerPacket));
write((uint8_t *)&TriggerPacket, sizeof(TriggerPacket));
}
#endif
//==============================================
//
// Main function loop
//
//==============================================
void main(void)
{
clr_BODEN; // Kill brown out
ADCCON1 = 0x00; // turn off the ADC
Set_All_GPIO_Quasi_Mode; // Set port drivers
#if PIN_IRQ
//-----------------------------------------------------
// Intialise the external irq pin P07
//-----------------------------------------------------
P13_Input_Mode; // Input mode.
//P07 =1;
PICON |= 0x21; // Edge triggered.
set_PINEN3; // Neg edge detection.
set_PIPEN3; // Pos edge detection.
// Enable Port 0 irq.
//Enable_BIT3_LowLevel_Trig;
//Enable_BIT3_HighLevel_Trig;
set_EPI;
#endif
#if WAKEUP_TIMER
wake_count = WAKE_COUNT;
battery_count = BATTERY_SAMPLE_COUNT;
//-----------------------------------------------------
// WKT initial
//-----------------------------------------------------
WKCON = 0x07; //timer base 10k, Pre-scale = 1/2048
RWK = SLEEP_PERIOD_RELOAD; // Roughly 30's
set_EWKT; // enable WKT interrupt
set_WKTR; // Wake-up timer run
#endif
EA = 1; // Enable irq's
#if SENSOR_ENABLE
// Initialise the sensor
Stimer_10u(1000);
Hdc1080Init();
#endif
spi_init();
#if RADIO_ENABLE
/* Initialise the radio */
begin();
enableDynamicPayloads();
openWritingPipe(pipeTx);
openReadingPipe(1, pipeRx);
powerUp();
Stimer_10u(10);
startup_packet_send();
#if PIN_IRQ
// Initial TriggerPacket content
TriggerPacket.mHeader.mStart = PACKET_START;
TriggerPacket.mHeader.mVersion = PACKET_VERSION;
TriggerPacket.mHeader.mPacketType = ePacketTypeTrigger;
TriggerPacket.mHeader.mNodeId = get_device_id();
TriggerPacket.mHeader.mCounter = 0;
TriggerPacket.mHeader.mBatteryVolts = read_battery_voltage() / 100;
#endif
#if SENSOR_ENABLE
// Initial SensorPacket content
SensorPacket.mHeader.mStart = PACKET_START;
SensorPacket.mHeader.mVersion = PACKET_VERSION;
SensorPacket.mHeader.mPacketType = ePacketTypeTemperatureHumidity;
SensorPacket.mHeader.mNodeId = get_device_id();
SensorPacket.mHeader.mCounter = 0;
SensorPacket.mHeader.mBatteryVolts = read_battery_voltage() / 100;
#endif
#endif
/* Main process loop */
while (1)
{
#if SENSOR_ENABLE
sensor_packet_send();
powerdown_and_sleep();
#endif
#if PIN_IRQ
#if RADIO_ENABLE
powerDown();
#endif
clr_SPIEN;
ADCCON1 = 0x00; // Turn off the ADC.
clr_BODEN; // Disable undervoltage detection.
PIF = 0x00;
set_PD; // Enter power-down mode.
set_SPIEN;
#if RADIO_ENABLE
powerUp();
#endif
#if PIN_IRQ
trigger_packet_send();
#endif
#endif
}
/* =================== */
}