NewAskSin
 All Classes Files Functions Variables Groups Pages
HAL.h
1 //- -----------------------------------------------------------------------------------------------------------------------
2 // AskSin driver implementation
3 // 2013-08-03 <trilu@gmx.de> Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
4 //- -----------------------------------------------------------------------------------------------------------------------
5 //- Hardware abstraction layer --------------------------------------------------------------------------------------------
6 //- -----------------------------------------------------------------------------------------------------------------------
7 
8 #ifndef _HAL_H
9  #define _HAL_H
10 
11  #if defined(ARDUINO) && ARDUINO >= 100
12  #include "Arduino.h"
13  #else
14  #include "WProgram.h"
15  #endif
16 
17  #include <avr/sleep.h>
18  #include <avr/power.h>
19  #include <avr/wdt.h>
20  #include <util/delay.h>
21  #include <util/atomic.h>
22  #include <avr/eeprom.h>
23 
24  #include "Print.h"
25 
26 
27  //- MCU dependent HAL definitions -----------------------------------------------------------------------------------------
28  #if defined(__AVR_ATmega328P__)
29  #include "HAL_atmega328P.h"
30  #elif defined(__AVR_ATmega32U4__)
31  #include "HAL_atmega32U4.h"
32  #else
33  #error "No HAL definition for current MCU available!"
34  #endif
35  //- -----------------------------------------------------------------------------------------------------------------------
36 
37  static uint16_t wdtSleep_TIME;
38 
39  //- timer functions -------------------------------------------------------------------------------------------------------
40  // https://github.com/zkemble/millis/blob/master/millis/
41  #define REG_TCCRA TCCR0A
42  #define REG_TCCRB TCCR0B
43  #define REG_TIMSK TIMSK0
44  #define REG_OCR OCR0A
45  #define BIT_OCIE OCIE0A
46  #define BIT_WGM WGM01
47  #define CLOCKSEL (_BV(CS01)|_BV(CS00))
48  #define PRESCALER 64
49  #define ISR_VECT TIMER0_COMPA_vect
50 
51  #define SET_TCCRA() (REG_TCCRA = _BV(BIT_WGM))
52  #define SET_TCCRB() (REG_TCCRB = CLOCKSEL)
53  //- -----------------------------------------------------------------------------------------------------------------------
54 
55  //- some macros and definitions -------------------------------------------------------------------------------------------
56  #define _pgmB(x) pgm_read_byte(&x) // short hand for PROGMEM read
57  #define _pgmW(x) pgm_read_word(&x)
58 
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))
65  //- -----------------------------------------------------------------------------------------------------------------------
66 
67 
68  //- timer functions -------------------------------------------------------------------------------------------------------
69  typedef uint32_t tMillis;
70  extern void initMillis(void);
71  extern tMillis getMillis(void);
72  extern void addMillis(tMillis ms);
73  //- -----------------------------------------------------------------------------------------------------------------------
74 
75  //- some macros for debugging ---------------------------------------------------------------------------------------------
76  // http://aeroquad.googlecode.com/svn/branches/pyjamasam/WIFIReceiver/Streaming.h
77  #define dbg Serial
78  template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
79 
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)
82 
83  struct _HEXB {
84  uint8_t val;
85  _HEXB(uint8_t v): val(v) {}
86  };
87  inline Print &operator <<(Print &obj, const _HEXB &arg) { obj.print(hiHexB(arg.val)); obj.print(loHexB(arg.val)); return obj; }
88 
89  struct _HEX {
90  const uint8_t *val;
91  uint8_t len;
92  _HEX(const uint8_t *v, uint8_t l): val(v), len(l) {}
93  };
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; }
95 
96  enum _eTIME { _TIME };
97  inline Print &operator <<(Print &obj, _eTIME arg) { obj.print('('); obj.print(getMillis()); obj.print(')'); return obj; }
98 
99  extern void dbgStart(void);
100  //- -----------------------------------------------------------------------------------------------------------------------
101 
102 
103  //- pin related functions -------------------------------------------------------------------------------------------------
104  void initLeds(void); // initialize leds
105  void ledRed(uint8_t stat); // function in main sketch to drive leds
106  extern void ledGrn(uint8_t stat); // stat could be 0 for off, 1 for on, 2 for toggle
107 
108  extern void initConfKey(void); // init the config key, function in user sketch
109 
110  extern void initWakeupPin(void); // init the wakeup pin
111  extern uint8_t checkWakeupPin(void); // we could setup a pin which avoid sleep mode
112  //- -----------------------------------------------------------------------------------------------------------------------
113 
114  //- pin interrupts --------------------------------------------------------------------------------------------------------
115  // http://www.protostack.com/blog/2010/09/external-interrupts-on-an-atmega168/
116 
117  #define regPCIE(PORT) (PCICR |= _BV(PORT))
118  #define regPCINT(MASK,PORT) (MASK |= _BV(PORT))
119 
120  extern void initPCINT(void);
121  extern uint8_t chkPCINT(uint8_t port, uint8_t pin);
122  //- -----------------------------------------------------------------------------------------------------------------------
123 
124 
125  //- cc1100 hardware functions ---------------------------------------------------------------------------------------------
126  extern void ccInitHw(void);
127  extern uint8_t ccSendByte(uint8_t data);
128  extern uint8_t ccGetGDO0(void);
129 
130  extern void enableGDO0Int(void);
131  extern void disableGDO0Int(void);
132 
133  extern void waitMiso(void);
134  extern void ccSelect(void);
135  extern void ccDeselect(void);
136  //- -----------------------------------------------------------------------------------------------------------------------
137 
138 
139  //- eeprom functions ------------------------------------------------------------------------------------------------------
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);
144  //- -----------------------------------------------------------------------------------------------------------------------
145 
146 
147  //- power management functions --------------------------------------------------------------------------------------------
148  extern void startWDG32ms(void);
149  extern void startWDG250ms(void);
150  extern void startWDG8000ms(void);
151  extern void setSleep(void);
152 
153  extern void startWDG();
154  extern void stopWDG();
155  extern void setSleepMode();
156  //- -----------------------------------------------------------------------------------------------------------------------
157 
158 
159  //- battery measurement functions -----------------------------------------------------------------------------------------
160  // http://jeelabs.org/2013/05/17/zero-powe-battery-measurement/
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
163 
164  extern uint16_t getAdcValue(uint8_t adcmux);
165  uint8_t getBatteryVoltage(void);
166  //- -----------------------------------------------------------------------------------------------------------------------
167 
168 #endif
Definition: HAL.h:83
Definition: HAL.h:89