-
Notifications
You must be signed in to change notification settings - Fork 26
/
KISSLoRa_sleep.cpp
296 lines (243 loc) · 6.95 KB
/
KISSLoRa_sleep.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
#include "KISSLoRa_sleep.h"
#include "KISSLoRa_io.h"
//http://playground.arduino.cc/Learning/ArduinoSleepCode
//http://playground.arduino.cc/Code/Timer1
//http://forum.arduino.cc/index.php?topic=38046.0
//http://donalmorrissey.blogspot.nl/2011/11/sleeping-arduino-part-4-wake-up-via.html
//https://oscarliang.com/arduino-timer-and-interrupt-tutorial/
//https://arduino-info.wikispaces.com/Timers-Arduino
#include <avr/power.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <Arduino.h>
//all peripherals disabled except timer0 and uart1
//supply 3.8V 100mA
//series current meter
//rotary switch at 0(0 pins high, so 0*0.33mA)
//black tape on light sensor
//3.77 V at connector
//current consumption: 13.16mA off, 16.58 max
//all peripherals disabled except timer0
//supply 3.8V 100mA
//series current meter
//rotary switch at 0(0 pins high, so 0*0.33mA)
//black tape on light sensor
//3.77 V at connector
//current consumption: 13.07mA off, 16.48 max
//all peripherals disabled except timer0, disabled on chip debugging
//supply 3.8V 100mA
//series current meter
//rotary switch at 0(0 pins high, so 0*0.33mA)
//black tape on light sensor
//3.77 V at connector
//current consumption: 13.05mA off, 16.46 max(redone measurement without on chip debugging enabled and got same results, so difference is possbily a measurement error)
//all peripherals disabled except timer1
//sleep mode: idle
//supply 3.8V 100mA
//series current meter
//rotary switch at 0(0 pins high, so 0*0.33mA)
//black tape on light sensor
//3.77 V at connector
//current consumption: 10.65mA off, 14.02 on
//all peripherals enabled
//supply 3.8V 100mA
//series current meter
//rotary switch at 0(0 pins high, so 0*0.33mA)
//black tape on light sensor
//3.77 V at connector
//current consumption: 13.80mA off, 17.22 max
//all peripherals disabled
//sleep mode: power down
//supply 3.8V 100mA
//series current meter
//rotary switch at 0(0 pins high, so 0*0.33mA)
//black tape on light sensor
//3.77 V at connector
//current consumption: 8.72mA off, 12.04 max
/*
static volatile uint8_t pin_interrupt_flag;
static void KISSLoRa_led_toggle(void){
detachInterrupt(digitalPinToInterrupt(7));
pin_interrupt_flag = 1;
}
*/
static long timeSleep = 0; // total time due to sleep
static float calibv = 0.93; // ratio of real clock with WDT clock
static volatile uint8_t isrcalled = 0; // WDT vector flag
// Internal function: Start watchdog timer
// byte psVal - Prescale mask
static void WDT_On (uint8_t psVal)
{
// prepare timed sequence first
uint8_t ps = (psVal | (1<<WDIE)) & ~(1<<WDE);
cli();
wdt_reset();
// Clear WDRF in MCUSR
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCSR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCSR = ps;
sei();
}
// Internal function. Stop watchdog timer
static void WDT_Off() {
cli();
wdt_reset();
// Clear WDRF in MCUSR
MCUSR &= ~(1<<WDRF);
// Write logical one to WDCE and WDE
// Keep old prescaler setting to prevent unintentional time-out
WDTCSR |= (1<<WDCE) | (1<<WDE);
// Turn off WDT
WDTCSR = 0x00;
sei();
}
// internal function.
static int doSleep(long timeRem) {
uint8_t WDTps = 9; // WDT Prescaler value, 9 = 8192ms
isrcalled = 0;
sleep_enable();
while(timeRem > 0) {
//work out next prescale unit to use
while ((0x10<<WDTps) > timeRem && WDTps > 0) {
WDTps--;
}
// send prescaler mask to WDT_On
WDT_On((WDTps & 0x08 ? (1<<WDP3) : 0x00) | (WDTps & 0x07));
isrcalled=0;
while (isrcalled==0) {
// turn bod off
// MCUCR |= (1<<BODS) | (1<<BODSE);
//MCUCR &= ~(1<<BODSE); // must be done right before sleep
sleep_cpu(); // sleep here
}
// calculate remaining time
timeRem -= (0x10<<WDTps);
}
sleep_disable();
return timeRem;
}
// Calibrate watchdog timer with millis() timer(timer0)
static void calibrate() {
// timer0 continues to run in idle sleep mode
set_sleep_mode(SLEEP_MODE_IDLE);
long tt1=millis();
doSleep(256);
long tt2=millis();
calibv = 256.0/(tt2-tt1);
}
// Estimated millis is real clock + calibrated sleep time
static long estMillis() {
return millis()+timeSleep;
}
// Delay function
static void sleepCPU_delay(long sleepTime) {
//ADCSRA &= ~(1<<ADEN); // adc off
//PRR = 0xEF; // modules off
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
int trem = doSleep(sleepTime*calibv);
timeSleep += (sleepTime-trem);
//PRR = 0x00; //modules on
//ADCSRA |= (1<<ADEN); // adc on
}
// wdt int service routine
ISR(WDT_vect) {
WDT_Off();
isrcalled=1;
}
//ISR(PCINT0_vect){
// LORA_LED_PORT ^= LORA_LED_BIT;
//}
/*
static void sleep_watchdog_blinky(void){
RGB_BLUE_PORT |= RGB_BLUE_BIT;
RGB_BLUE_DDR |= RGB_BLUE_BIT;
LORA_LED_PORT |= LORA_LED_BIT;
LORA_LED_DDR |= LORA_LED_BIT;
//calibrate();
//3.1: PINE6 PCINT6 arduino pin 7
//3.0: PINB7 PCINT7 arduino pin 11
//pinMode(7, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(7), KISSLoRa_led_toggle, LOW);//FALLING
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
power_usb_disable();
power_timer0_disable();
power_timer1_disable();
power_timer2_disable();
power_timer3_disable();
//output compare modulator?
power_adc_disable();
power_usart0_disable();
power_usart1_disable();
power_spi_disable();
power_twi_disable();
while(1){
sleep_cpu();
if(pin_interrupt_flag){
pin_interrupt_flag = 0;
LORA_LED_PORT ^= LORA_LED_BIT;
while((BUTTON_IN_PIN & BUTTON_IN_BIT)==0);//wait for button to be released
attachInterrupt(digitalPinToInterrupt(7), KISSLoRa_led_toggle, LOW);//FALLING
}
//sleepCPU_delay(5000-25);
//RGB_BLUE_PORT ^= RGB_BLUE_BIT;
//sleepCPU_delay(25);
// RGB_BLUE_PORT ^= RGB_BLUE_BIT;
}
}
*/
/*
void sleep_test(void){
//wake_blinky();
//sleep_blinky();
sleep_watchdog_blinky();
}
*/
//! \brief initializes sleep mode and calibrates watchdog timer
void KISSLoRa_sleep_init(void){
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
calibrate();
/*
power_usb_disable();
power_timer0_disable();
power_timer1_disable();
power_timer2_disable();
power_timer3_disable();
//output compare modulator?
power_adc_disable();
power_usart0_disable();
power_usart1_disable();
power_spi_disable();
power_twi_disable();
*/
}
//! \brief powers down peripherals and puts microcontroller in power down sleep mode, wakes up on watchdog timer
void KISSLoRa_sleep_delay_ms(long delay_ms){
power_usb_disable();
power_timer0_disable();
power_timer1_disable();
power_timer2_disable();
power_timer3_disable();
//output compare modulator?
power_adc_disable();
power_usart0_disable();
//power_usart1_disable();//lora
power_spi_disable();
power_twi_disable();
sleepCPU_delay(delay_ms);
power_usb_enable();
power_timer0_enable();
power_timer1_enable();
power_timer2_enable();
power_timer3_enable();
//output compare modulator?
power_adc_enable();
power_usart0_enable();
//power_usart1_enable();//lora
power_spi_enable();
power_twi_enable();
}