11 #if defined(ARDUINO) && ARDUINO >= 100
17 #include <avr/sleep.h>
18 #include <avr/power.h>
20 #include <util/delay.h>
21 #include <util/atomic.h>
22 #include <avr/eeprom.h>
28 #if defined(__AVR_ATmega328P__)
29 #include "HAL_atmega328P.h"
30 #elif defined(__AVR_ATmega32U4__)
31 #include "HAL_atmega32U4.h"
33 #error "No HAL definition for current MCU available!"
37 static uint16_t wdtSleep_TIME;
41 #define REG_TCCRA TCCR0A
42 #define REG_TCCRB TCCR0B
43 #define REG_TIMSK TIMSK0
45 #define BIT_OCIE OCIE0A
47 #define CLOCKSEL (_BV(CS01)|_BV(CS00))
49 #define ISR_VECT TIMER0_COMPA_vect
51 #define SET_TCCRA() (REG_TCCRA = _BV(BIT_WGM))
52 #define SET_TCCRB() (REG_TCCRB = CLOCKSEL)
56 #define _pgmB(x) pgm_read_byte(&x) // short hand for PROGMEM read
57 #define _pgmW(x) pgm_read_word(&x)
59 #define pinOutput(PORT,PIN) ((PORT) |= _BV(PIN)) // pin functions
60 #define pinInput(PORT,PIN) ((PORT) &= ~_BV(PIN))
61 #define setPinHigh(PORT,PIN) ((PORT) |= _BV(PIN))
62 #define setPinLow(PORT,PIN) ((PORT) &= ~_BV(PIN))
63 #define setPinCng(PORT,PIN) ((PORT) ^= _BV(PIN))
64 #define getPin(PORT,PIN) ((PORT) & _BV(PIN))
69 typedef uint32_t tMillis;
70 extern void initMillis(
void);
71 extern tMillis getMillis(
void);
72 extern void addMillis(tMillis ms);
78 template<
class T>
inline Print &operator <<(Print &obj, T arg) { obj.print(arg);
return obj; }
80 #define hiHexB(x) char((x>>4)>9?(x>>4)+55:(x>>4)+48)
81 #define loHexB(x) char((x&0xF)>9?(x&0xF)+55:(x&0xF)+48)
85 _HEXB(uint8_t v): val(v) {}
87 inline Print &operator <<(Print &obj,
const _HEXB &arg) { obj.print(hiHexB(arg.val)); obj.print(loHexB(arg.val));
return obj; }
92 _HEX(
const uint8_t *v, uint8_t l): val(v), len(l) {}
94 inline Print &operator <<(Print &obj,
const _HEX &arg) {
for(uint8_t i=0;i<arg.len;i++) { obj.print(hiHexB(arg.val[i])); obj.print(loHexB(arg.val[i]));
if(i+1<arg.len) obj.print(
' '); };
return obj; }
96 enum _eTIME { _TIME };
97 inline Print &operator <<(Print &obj, _eTIME arg) { obj.print(
'('); obj.print(getMillis()); obj.print(
')');
return obj; }
99 extern void dbgStart(
void);
105 void ledRed(uint8_t stat);
106 extern void ledGrn(uint8_t stat);
108 extern void initConfKey(
void);
110 extern void initWakeupPin(
void);
111 extern uint8_t checkWakeupPin(
void);
117 #define regPCIE(PORT) (PCICR |= _BV(PORT))
118 #define regPCINT(MASK,PORT) (MASK |= _BV(PORT))
120 extern void initPCINT(
void);
121 extern uint8_t chkPCINT(uint8_t port, uint8_t pin);
126 extern void ccInitHw(
void);
127 extern uint8_t ccSendByte(uint8_t data);
128 extern uint8_t ccGetGDO0(
void);
130 extern void enableGDO0Int(
void);
131 extern void disableGDO0Int(
void);
133 extern void waitMiso(
void);
134 extern void ccSelect(
void);
135 extern void ccDeselect(
void);
140 extern void initEEProm(
void);
141 extern void getEEPromBlock(uint16_t addr,uint8_t len,
void *ptr);
142 extern void setEEPromBlock(uint16_t addr,uint8_t len,
void *ptr);
143 extern void clearEEPromBlock(uint16_t addr, uint16_t len);
148 extern void startWDG32ms(
void);
149 extern void startWDG250ms(
void);
150 extern void startWDG8000ms(
void);
151 extern void setSleep(
void);
153 extern void startWDG();
154 extern void stopWDG();
155 extern void setSleepMode();
161 #define BAT_NUM_MESS_ADC 20 // real measures to get the best average measure
162 #define BAT_DUMMY_NUM_MESS_ADC 40 // dummy measures to get the ADC working
164 extern uint16_t getAdcValue(uint8_t adcmux);
165 uint8_t getBatteryVoltage(
void);