NewAskSin
 All Classes Files Functions Variables Groups Pages
HAL_extern.h
1 //- cc1100 hardware functions ---------------------------------------------------------------------------------------------
2 void ccInitHw(void) {
3  pinOutput( CC_CS_DDR, CC_CS_PIN ); // set chip select as output
4  pinOutput( SPI_DDR, SPI_MOSI ); // set MOSI as output
5  pinInput ( SPI_DDR, SPI_MISO ); // set MISO as input
6  pinOutput( SPI_DDR, SPI_SCLK ); // set SCK as output
7  pinInput ( CC_GDO0_DDR, CC_GDO0_PIN ); // set GDO0 as input
8 
9  SPCR = _BV(SPE) | _BV(MSTR); // SPI enable, master, speed = CLK/4
10 
11  CC_GDO0_PCICR |= _BV(CC_GDO0_PCIE); // set interrupt in mask active
12 }
13 uint8_t ccSendByte(uint8_t data) {
14  SPDR = data; // send byte
15  while (!(SPSR & _BV(SPIF))); // wait until transfer finished
16  return SPDR;
17 }
18 uint8_t ccGetGDO0() {
19  uint8_t x = chkPCINT(CC_GDO0_PCIE, CC_GDO0_INT);
20  //if (x>1) dbg << "x:" << x << '\n';
21 
22  if (x == 2 ) return 1; // falling edge detected
23  else return 0;
24 }
25 
26 void enableGDO0Int(void) {
27  //dbg << "enable int\n";
28  CC_GDO0_PCMSK |= _BV(CC_GDO0_INT);
29 }
30 void disableGDO0Int(void) {
31  //dbg << "disable int\n";
32  CC_GDO0_PCMSK &= ~_BV(CC_GDO0_INT);
33 }
34 
35 void waitMiso(void) {
36  while(SPI_PORT & _BV(SPI_MISO));
37 }
38 void ccSelect(void) {
39  setPinLow( CC_CS_PORT, CC_CS_PIN);
40 }
41 void ccDeselect(void) {
42  setPinHigh( CC_CS_PORT, CC_CS_PIN);
43 }
44 //- -----------------------------------------------------------------------------------------------------------------------
45 
46 
47 //- status led related functions -------------------------------------------------------------------------------------------------
48 void initLeds(void) {
49  pinOutput(LED_RED_DDR, LED_RED_PIN); // set the led pins in port
50  pinOutput(LED_GRN_DDR, LED_GRN_PIN);
51  if (LED_ACTIVE_LOW) {
52  setPinHigh(LED_RED_PORT, LED_RED_PIN);
53  setPinHigh(LED_GRN_PORT, LED_GRN_PIN);
54  }
55 }
56 void ledRed(uint8_t stat) {
57  stat ^= LED_ACTIVE_LOW;
58  if (stat == 1) setPinHigh(LED_RED_PORT, LED_RED_PIN);
59  else if (stat == 0) setPinLow(LED_RED_PORT, LED_RED_PIN);
60  else setPinCng(LED_RED_PORT, LED_RED_PIN);
61 }
62 void ledGrn(uint8_t stat) {
63  stat ^= LED_ACTIVE_LOW;
64  if (stat == 1) setPinHigh(LED_GRN_PORT, LED_GRN_PIN);
65  else if (stat == 0) setPinLow(LED_GRN_PORT, LED_GRN_PIN);
66  else setPinCng(LED_GRN_PORT, LED_GRN_PIN);
67 }
68 
69 
70 
71 struct s_pcINT {
72  uint8_t cur;
73  uint8_t prev;
74  uint32_t time;
75 } static volatile pcInt[3];
76 
77 //- pin related functions -------------------------------------------------------------------------------------------------
78 
79 void initPCINT(void) {
80  memset((uint8_t*)pcInt, 0x00, sizeof(pcInt));
81  //dbg << "a: " << pcInt[2].cur << '\n';
82 }
83 uint8_t chkPCINT(uint8_t port, uint8_t pin) {
84  // returns pin status while no interrupt had happened for the pin, 2 for falling and 3 for rising edge
85 
86  uint8_t cur = pcInt[port].cur & _BV(pin);
87  uint8_t prev = pcInt[port].prev & _BV(pin);
88 
89  if ((cur == prev) || ( (getMillis() - pcInt[port].time) < DEBOUNCE )) { // old and new bit is similar, or DEBOUNCE time is running
90  return (pcInt[port].cur & _BV(pin))?1:0;
91  }
92 
93  //if ( (getMillis() - pcInt[port].time) < DEBOUNCE ) {
94  // dbg << "xxxx\n";
95  //}
96 
97  // detect rising or falling edge
98  //dbg << pcInt[port].cur << ' ' << pcInt[port].prev << ' ';
99  if (cur) { // pin is 1
100  pcInt[port].prev |= _BV(pin); // set bit bit in prev
101  //dbg << "y3\n";
102  return 3;
103  } else { // pin is 0
104  //dbg << "y2\n";
105  pcInt[port].prev &= ~_BV(pin); // clear bit in prev
106  return 2;
107  }
108 }
109 
110 
111 /************************************
112  *** Config key related functions ***
113  ************************************/
114 
119 void initConfKey(void) {
120  // set port pin and register pin interrupt
121  pinInput(CONFIG_KEY_DDR, CONFIG_KEY_PIN); // init the config key pin
122  setPinHigh(CONFIG_KEY_PORT,CONFIG_KEY_PIN);
123 
124  initPCINT(); // some sanity on interrupts
125  regPCIE(CONFIG_KEY_PCIE); // set the pin change interrupt
126  regPCINT(CONFIG_KEY_PCMSK, CONFIG_KEY_INT); // description is in hal.h
127 
128  //dbg << "pb:" << PINB << " pc:" << PINC << " pd:" << PIND << "\n";
129  //dbg << "ckDDR:" << CONFIG_KEY_DDR << "ckPort:" << CONFIG_KEY_PORT << " ckpin:" << CONFIG_KEY_PIN << "\n";
130 
131  pcInt[0].cur = PINB;
132  pcInt[1].cur = PINC;
133  pcInt[2].cur = PIND;
134 }
135 
136 //- -----------------------------------------------------------------------------------------------------------------------
137 ISR (PCINT0_vect) {
138  pcInt[0].prev = pcInt[0].cur;
139  pcInt[0].cur = PINB;
140  pcInt[0].time = getMillis();
141  //dbg << "i1:" << PINB << "\n";
142 }
143 ISR (PCINT1_vect) {
144  pcInt[1].prev = pcInt[1].cur;
145  pcInt[1].cur = PINC;
146  pcInt[1].time = getMillis();
147  //dbg << "i2:" << PINC << "\n";
148 }
149 ISR (PCINT2_vect) {
150  pcInt[2].prev = pcInt[2].cur;
151  pcInt[2].cur = PIND;
152  pcInt[2].time = getMillis();
153  //dbg << "i3:" << PIND << "\n";
154 }
155 //- -----------------------------------------------------------------------------------------------------------------------
156 
157 
158 /*************************************
159  *** Battery measurement functions ***
160  *************************************/
161 void initExtBattMeasurement(void);
162 void switchExtBattMeasurement(uint8_t stat);
163 
167 uint8_t getBatteryVoltage(void) {
168  #if defined EXT_BATTERY_MEASUREMENT
169  initExtBattMeasurement();
170  switchExtBattMeasurement(1);
171  #endif
172 
173  #if defined EXT_BATTERY_MEASUREMENT
174  uint16_t adcValue = getAdcValue( // Voltage Reference = Internal 1.1V; Input Channel = external battery measure pin
175  (1 << REFS1) | (1 << REFS0) | BATT_MEASURE_PIN
176  );
177 
178  adcValue = adcValue * AVR_BANDGAP_VOLTAGE / 1023 / BATTERY_FACTOR; // calculate battery voltage in V/10
179  switchExtBattMeasurement(0);
180  #else
181  uint16_t adcValue = getAdcValue( // Voltage Reference = AVCC with external capacitor at AREF pin; Input Channel = 1.1V (V BG)
182  (0 << REFS1) | (1 << REFS0) | (1 << MUX3) | (1 << MUX2) | (1 << MUX1) | (0 << MUX0)
183  );
184 
185  adcValue = AVR_BANDGAP_VOLTAGE * 1023 / adcValue / 100; // calculate battery voltage in V/10
186  #endif
187 
188  return (uint8_t)adcValue;
189 }
190 
194 void initExtBattMeasurement(void) {
195  pinInput(BATT_MEASURE_DDR, BATT_MEASURE_PIN); // set the ADC pin as input
196  pinInput(BATT_ENABLE_DDR, BATT_ENABLE_PIN); // set the measurement enable pin as input, otherwise we waste energy over the resistor network against VCC
197 }
198 
203 void switchExtBattMeasurement(uint8_t stat) {
204  if (stat == 1) {
205  pinOutput(BATT_ENABLE_DDR, BATT_ENABLE_PIN); // set pin as out put
206  setPinLow(BATT_ENABLE_PORT, BATT_ENABLE_PIN); // set low to measure the resistor network
207  setPinLow(BATT_MEASURE_PORT, BATT_MEASURE_PIN);
208  } else {
209  pinInput(BATT_ENABLE_DDR, BATT_ENABLE_PIN);
210 
211  // todo: check
212  setPinHigh(BATT_MEASURE_PORT, BATT_MEASURE_PIN); // switch on pull up, otherwise we waste energy over the resistor network against VCC
213  }
214 }
Definition: HAL_extern.h:71