Chameleon-Mini
AntennaLevel.h
1 /*
2  * AntennaLevel.h
3  *
4  * Created on: 24.11.2013
5  * Author: skuser
6  */
7 
8 #ifndef ANTENNALEVEL_H_
9 #define ANTENNALEVEL_H_
10 
11 #include "Common.h"
12 
13 #define ANTENNA_LEVEL_R1 47E3
14 #define ANTENNA_LEVEL_R2 1E3
15 #define ANTENNA_LEVEL_VREF 1.0
16 #define ANTENNA_LEVEL_RES 4096
17 #define ANTENNA_LEVEL_OFFSET 190 /* LSB */
18 
19 #define ANTENNA_LEVEL_MILLIVOLT 1E3
20 #define ANTENNA_LEVEL_FACTOR (ANTENNA_LEVEL_VREF * (ANTENNA_LEVEL_R1 + ANTENNA_LEVEL_R2) / (ANTENNA_LEVEL_RES * ANTENNA_LEVEL_R2) )
21 #define ANTENNA_LEVEL_SCALE ((uint32_t) 1<<16)
22 #define ANTENNA_LEVEL_NUMERATOR ((uint32_t) (ANTENNA_LEVEL_MILLIVOLT * ANTENNA_LEVEL_FACTOR * ANTENNA_LEVEL_SCALE + .5))
23 #define ANTENNA_LEVEL_DENOMINATOR (ANTENNA_LEVEL_SCALE)
24 
25 #define ANTENNA_LEVEL_LOG_RDRDETECT_INTERVAL (15)
26 extern uint8_t AntennaLevelLogReaderDetectCount;
27 
28 #define FIELD_MIN_RSSI 500
29 
30 static inline
31 void AntennaLevelInit(void) {
32  ADCA.CAL = (PRODSIGNATURES_ADCACAL1 << 8) | PRODSIGNATURES_ADCACAL0; /* Load calibration data, source: https://www.avrfreaks.net/comment/2080211#comment-2080211 */
33  ADCA.CTRLA = ADC_ENABLE_bm;
34  ADCA.CTRLB = ADC_RESOLUTION_12BIT_gc;
35  ADCA.REFCTRL = ADC_REFSEL_INT1V_gc | ADC_BANDGAP_bm;
36  ADCA.PRESCALER = ADC_PRESCALER_DIV32_gc;
37  ADCA.CH0.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc;
38  ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN1_gc;
39 
40 }
41 
42 static inline
43 uint16_t AntennaLevelGet(void) {
44  ADCA.CH0.CTRL |= ADC_CH_START_bm;
45  while (!(ADCA.CH0.INTFLAGS & ADC_CH_CHIF_bm));
46 
47  ADCA.CH0.INTFLAGS = ADC_CH_CHIF_bm;
48 
49  int16_t Result = ADCA.CH0RES - ANTENNA_LEVEL_OFFSET;
50  if (Result < 0) Result = 0;
51 
52  return (uint16_t)(((uint32_t) Result * ANTENNA_LEVEL_NUMERATOR) / ANTENNA_LEVEL_DENOMINATOR);
53 }
54 
55 void AntennaLevelTick(void);
56 void AntennaLevelResetMaxRssi(void);
57 
58 #endif /* ANTENNALEVEL_H_ */