forked from trilu2000/NewAskSin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HAL.h
168 lines (129 loc) · 7.63 KB
/
HAL.h
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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin driver implementation
// 2013-08-03 <[email protected]> Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
//- Hardware abstraction layer --------------------------------------------------------------------------------------------
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef _HAL_H
#define _HAL_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <util/delay.h>
#include <util/atomic.h>
#include <avr/eeprom.h>
#include "Print.h"
//- MCU dependent HAL definitions -----------------------------------------------------------------------------------------
#if defined(__AVR_ATmega328P__)
#include "HAL_atmega328P.h"
#elif defined(__AVR_ATmega32U4__)
#include "HAL_atmega32U4.h"
#else
#error "No HAL definition for current MCU available!"
#endif
//- -----------------------------------------------------------------------------------------------------------------------
static uint16_t wdtSleep_TIME;
//- timer functions -------------------------------------------------------------------------------------------------------
// https://github.com/zkemble/millis/blob/master/millis/
#define REG_TCCRA TCCR0A
#define REG_TCCRB TCCR0B
#define REG_TIMSK TIMSK0
#define REG_OCR OCR0A
#define BIT_OCIE OCIE0A
#define BIT_WGM WGM01
#define CLOCKSEL (_BV(CS01)|_BV(CS00))
#define PRESCALER 64
#define ISR_VECT TIMER0_COMPA_vect
#define SET_TCCRA() (REG_TCCRA = _BV(BIT_WGM))
#define SET_TCCRB() (REG_TCCRB = CLOCKSEL)
//- -----------------------------------------------------------------------------------------------------------------------
//- some macros and definitions -------------------------------------------------------------------------------------------
#define _pgmB(x) pgm_read_byte(&x) // short hand for PROGMEM read
#define _pgmW(x) pgm_read_word(&x)
#define pinOutput(PORT,PIN) ((PORT) |= _BV(PIN)) // pin functions
#define pinInput(PORT,PIN) ((PORT) &= ~_BV(PIN))
#define setPinHigh(PORT,PIN) ((PORT) |= _BV(PIN))
#define setPinLow(PORT,PIN) ((PORT) &= ~_BV(PIN))
#define setPinCng(PORT,PIN) ((PORT) ^= _BV(PIN))
#define getPin(PORT,PIN) ((PORT) & _BV(PIN))
//- -----------------------------------------------------------------------------------------------------------------------
//- timer functions -------------------------------------------------------------------------------------------------------
typedef uint32_t tMillis;
extern void initMillis(void);
extern tMillis getMillis(void);
extern void addMillis(tMillis ms);
//- -----------------------------------------------------------------------------------------------------------------------
//- some macros for debugging ---------------------------------------------------------------------------------------------
// http://aeroquad.googlecode.com/svn/branches/pyjamasam/WIFIReceiver/Streaming.h
#define dbg Serial
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
#define hiHexB(x) char((x>>4)>9?(x>>4)+55:(x>>4)+48)
#define loHexB(x) char((x&0xF)>9?(x&0xF)+55:(x&0xF)+48)
struct _HEXB {
uint8_t val;
_HEXB(uint8_t v): val(v) {}
};
inline Print &operator <<(Print &obj, const _HEXB &arg) { obj.print(hiHexB(arg.val)); obj.print(loHexB(arg.val)); return obj; }
struct _HEX {
uint8_t *val;
uint8_t len;
_HEX(uint8_t *v, uint8_t l): val(v), len(l) {}
};
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; }
enum _eTIME { _TIME };
inline Print &operator <<(Print &obj, _eTIME arg) { obj.print('('); obj.print(getMillis()); obj.print(')'); return obj; }
extern void dbgStart(void);
//- -----------------------------------------------------------------------------------------------------------------------
//- pin related functions -------------------------------------------------------------------------------------------------
void initLeds(void); // initialize leds
void ledRed(uint8_t stat); // function in main sketch to drive leds
extern void ledGrn(uint8_t stat); // stat could be 0 for off, 1 for on, 2 for toggle
extern void initConfKey(void); // init the config key, function in user sketch
extern void initWakeupPin(void); // init the wakeup pin
extern uint8_t checkWakeupPin(void); // we could setup a pin which avoid sleep mode
//- -----------------------------------------------------------------------------------------------------------------------
//- pin interrupts --------------------------------------------------------------------------------------------------------
// http://www.protostack.com/blog/2010/09/external-interrupts-on-an-atmega168/
#define regPCIE(PORT) (PCICR |= _BV(PORT))
#define regPCINT(MASK,PORT) (MASK |= _BV(PORT))
extern void initPCINT(void);
extern uint8_t chkPCINT(uint8_t port, uint8_t pin, uint8_t debounce);
//- -----------------------------------------------------------------------------------------------------------------------
//- cc1100 hardware functions ---------------------------------------------------------------------------------------------
extern void ccInitHw(void);
extern uint8_t ccSendByte(uint8_t data);
extern uint8_t ccGetGDO0(void);
extern void enableGDO0Int(void);
extern void disableGDO0Int(void);
extern void waitMiso(void);
extern void ccSelect(void);
extern void ccDeselect(void);
//- -----------------------------------------------------------------------------------------------------------------------
//- eeprom functions ------------------------------------------------------------------------------------------------------
extern void initEEProm(void);
extern void getEEPromBlock(uint16_t addr,uint8_t len,void *ptr);
extern void setEEPromBlock(uint16_t addr,uint8_t len,void *ptr);
extern void clearEEPromBlock(uint16_t addr, uint16_t len);
//- -----------------------------------------------------------------------------------------------------------------------
//- power management functions --------------------------------------------------------------------------------------------
extern void startWDG32ms(void);
extern void startWDG250ms(void);
extern void startWDG8000ms(void);
extern void setSleep(void);
extern void startWDG();
extern void stopWDG();
extern void setSleepMode();
//- -----------------------------------------------------------------------------------------------------------------------
//- battery measurement functions -----------------------------------------------------------------------------------------
// http://jeelabs.org/2013/05/17/zero-powe-battery-measurement/
#define BAT_NUM_MESS_ADC 20 // real measures to get the best average measure
#define BAT_DUMMY_NUM_MESS_ADC 40 // dummy measures to get the ADC working
extern uint16_t getAdcValue(uint8_t adcmux);
uint8_t getBatteryVoltage(void);
//- -----------------------------------------------------------------------------------------------------------------------
#endif