summaryrefslogtreecommitdiffstats
path: root/phy/receiver.h
blob: 96fdc1d6e25bd7dc8cfc0b6a6a1e7e9135cd45cb (plain)
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
#ifndef MYRADIO_PHY_RECEIVER_H_
#define MYRADIO_PHY_RECEIVER_H_

/* Lowlevel receiver definitions.
 * These functions do poke with the actual RX hardware.
 */

#include "util.h"

#include <avr/io.h>

/* Our receiver is based on the AnalogToDigital-converter 0.
 * We basically get a binary signal from the radio. The radio
 * hardware did already demodulate the HF signal and filter
 * out any HF.
 * Our tunable is the threshold value for the logical HIGH.
 * The ADC is in freerunning mode and continuously scans the
 * input pin. The current status can be fetched by calling the
 * receiver_get_current_signal() function.
 * Note that the current signal is delayed by the ADC delay, which
 * is defined by the ADC prescaler. However, as our prescaler is 64
 * we run the ADC at a frequency of 250kHz (16Mhz crystal). That means
 * our delay is as low as 4 microseconds.
 */


//FIXME tune this to the correct value for our radio
#define RECEIVER_THRESHOLD	50 /* in ADC LSBs */


static inline bool receiver_get_current_signal(void)
{
	return (ADC > RECEIVER_THRESHOLD);
}

static inline void receiver_initialize(void)
{
	/* Multiplexer = ADC0; Reference = AVcc */
	ADMUX = (1 << REFS0);
	/* Enable ADC; Prescaler = 64 */
	ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS2);

	/* Poke the ADC once to initialize it */
	ADCSRA |= (1 << ADSC);
	while (ADCSRA & (1 << ADSC))
		mb();

	/* Now enable free-running mode. */
	ADCSRA |= (1 << ADSC) | (1 << ADFR);
}

#endif /* MYRADIO_PHY_RECEIVER_H_ */
bues.ch cgit interface