Skip to content

Commit 4e968c7

Browse files
committed
Add functions for Vcc & Freq readout
1 parent 1396bf8 commit 4e968c7

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

libraries/MySensors/core/MyHw.h

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ uint8_t hwReadConfig(int adr);
4545
int8_t hwSleep(unsigned long ms);
4646
int8_t hwSleep(uint8_t interrupt, uint8_t mode, unsigned long ms);
4747
int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms);
48+
uint16_t hwCPUVoltage();
49+
uint16_t hwCPUFrequency();
4850
#ifdef MY_DEBUG
4951
void hwDebugPrint(const char *fmt, ... );
5052
#endif

libraries/MySensors/core/MyHwATMega328.cpp

+49-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void hwInternalSleep(unsigned long ms) {
9191
if (!pinIntTrigger && ms >= 125) { hwPowerDown(SLEEP_120MS); ms -= 120; }
9292
if (!pinIntTrigger && ms >= 64) { hwPowerDown(SLEEP_60MS); ms -= 60; }
9393
if (!pinIntTrigger && ms >= 32) { hwPowerDown(SLEEP_30MS); ms -= 30; }
94-
if (!pinIntTrigger && ms >= 16) { hwPowerDown(SLEEP_15Ms); ms -= 15; }
94+
if (!pinIntTrigger && ms >= 16) { hwPowerDown(SLEEP_15MS); ms -= 15; }
9595
}
9696

9797
int8_t hwSleep(unsigned long ms) {
@@ -132,6 +132,54 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo
132132
return retVal;
133133
}
134134

135+
uint16_t hwCPUVoltage() {
136+
// Measure Vcc against 1.1V Vref
137+
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
138+
ADMUX = (_BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1));
139+
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
140+
ADMUX = (_BV(MUX5) | _BV(MUX0));
141+
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
142+
ADMUX = (_BV(MUX3) | _BV(MUX2));
143+
#else
144+
ADMUX = (_BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1));
145+
#endif
146+
// Vref settle
147+
delay(70);
148+
// Do conversion
149+
ADCSRA |= _BV(ADSC);
150+
while (bit_is_set(ADCSRA,ADSC));
151+
// return value in mV
152+
return (1125300UL) / ADC;
153+
}
154+
155+
uint16_t hwCPUFrequency() {
156+
noInterrupts();
157+
// setup timer1
158+
TIFR1 = 0xFF;
159+
TCNT1 = 0;
160+
TCCR1A = 0;
161+
TCCR1C = 0;
162+
// save WDT settings
163+
uint8_t WDTsave = WDTCSR;
164+
wdt_enable(WDTO_500MS);
165+
// enable WDT interrupt mode => first timeout WDIF, 2nd timeout reset
166+
WDTCSR |= (1 << WDIE);
167+
wdt_reset();
168+
// start timer1 with 1024 prescaling
169+
TCCR1B = _BV(CS12) | _BV(CS10);
170+
// wait until wdt interrupt
171+
while (bit_is_clear(WDTCSR,WDIF));
172+
// stop timer
173+
TCCR1B = 0;
174+
// restore WDT settings
175+
wdt_reset();
176+
WDTCSR |= (1 << WDCE) | (1 << WDE);
177+
WDTCSR = WDTsave;
178+
interrupts();
179+
// return frequency in MHz (accuracy +- 10%)
180+
return TCNT1 * 2048UL / 1000000UL;
181+
}
182+
135183

136184

137185
#ifdef MY_DEBUG

libraries/MySensors/core/MyHwATMega328.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ do { \
7979

8080
enum period_t
8181
{
82-
SLEEP_15Ms,
82+
SLEEP_15MS,
8383
SLEEP_30MS,
8484
SLEEP_60MS,
8585
SLEEP_120MS,

libraries/MySensors/core/MyHwESP8266.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo
107107
return -2;
108108
}
109109

110+
ADC_MODE(ADC_VCC);
111+
112+
uint16_t hwCPUVoltage() {
113+
return ESP.getVcc();
114+
}
115+
116+
uint16_t hwCPUFrequency() {
117+
return ESP.getCpuFreqMHz();
118+
}
119+
110120
#ifdef MY_DEBUG
111121
void hwDebugPrint(const char *fmt, ... ) {
112122
char fmtBuffer[300];

libraries/MySensors/core/MyHwSAMD.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo
135135
return -2;
136136
}
137137

138+
uint16_t hwCPUVoltage() {
139+
// TODO: not supported
140+
return 0;
141+
}
142+
143+
uint16_t hwCPUFrequency() {
144+
// TODO: not supported
145+
return 0;
146+
}
147+
138148
#ifdef MY_DEBUG
139149
void hwDebugPrint(const char *fmt, ... ) {
140150
char fmtBuffer[300];

0 commit comments

Comments
 (0)