Revision 1b05b4cc
Finish up bom code cleanup
scout_avr/bom/bom.c | ||
---|---|---|
18 | 18 |
#define TIME_US(us) (F_CPU * (us) / 1000000 / PRESCALAR) |
19 | 19 |
|
20 | 20 |
#define ADDRESS 0x3 |
21 |
#define DETECT PB2
|
|
21 |
#define DETECT PB3
|
|
22 | 22 |
#define EMIT PB4 |
23 | 23 |
|
24 |
static void bom_init(void) { |
|
25 |
|
|
26 |
// setup INT0 |
|
27 |
//EICRA |= _BV(ISC01) | _BV(ISC00); |
|
28 |
//EIMSK |= _BV(INT0); |
|
24 |
#if DETECT == PB3 |
|
25 |
#define PCINT_DETECT PCINT3 |
|
26 |
#endif |
|
27 |
#if DETECT == PB4 |
|
28 |
#define PCINT_DETECT PCINT4 |
|
29 |
#endif |
|
29 | 30 |
|
30 |
init_timer0();
|
|
31 |
start_timer0();
|
|
31 |
static void bom_init(void) {
|
|
32 |
init_detect();
|
|
32 | 33 |
|
33 | 34 |
init_timer1(); |
34 | 35 |
|
35 |
// setup output pin |
|
36 |
SET_BIT(DDRB, 4); |
|
37 |
|
|
38 | 36 |
sei(); |
39 |
bom_start(); |
|
40 | 37 |
} |
41 | 38 |
|
42 | 39 |
////////////////////////////////// Receiving /////////////////////////////////// |
43 | 40 |
|
44 |
static void init_timer0(void) {
|
|
41 |
static void init_detect(void) {
|
|
45 | 42 |
TCCROA |= _BV(WGM01); // enable reset on compare A |
46 | 43 |
TIMSK |= _BV(OCIE0A); // enable timer0 compare match A |
44 |
|
|
45 |
GIMSK |= _BV(PCIE); // Enable pin change interrupt for detection |
|
46 |
PCMSK |= _BV(PCINT_DETECT); // Enable interrupt on detect pin |
|
47 |
} |
|
48 |
|
|
49 |
// Disables the interrupt that begins the detect process while it is running |
|
50 |
static void disable_detect_interrupt(void) { |
|
51 |
PCMSK &= ~_BV(PCINT_DETECT); |
|
47 | 52 |
} |
48 | 53 |
|
49 | 54 |
static void start_timer0(void) { |
... | ... | |
52 | 57 |
} |
53 | 58 |
|
54 | 59 |
static void start_detect(void) { |
60 |
disable_detect_interrupt(); |
|
55 | 61 |
data = 0; |
56 | 62 |
count = 0; |
57 | 63 |
last_bit = 1; |
58 | 64 |
start_timer0(); |
59 | 65 |
} |
60 | 66 |
|
61 |
static void stop_detect(void) {
|
|
67 |
static void restart_detect(void) {
|
|
62 | 68 |
TCCR0B &= ~0x07; // Stop timer0 |
69 |
PCMSK |= _BV(PCINT_DETECT); // Enable interrupt on detect pin |
|
70 |
} |
|
71 |
|
|
72 |
ISR(PCINT0_vect) { |
|
73 |
start_detect(); |
|
63 | 74 |
} |
64 | 75 |
|
65 | 76 |
ISR(TIMER0_COMPA_vect) { |
... | ... | |
68 | 79 |
|
69 | 80 |
if (this_bit == 0) { |
70 | 81 |
if (last_bit == 0) { |
71 |
stop_detect();
|
|
82 |
restart_detect();
|
|
72 | 83 |
} |
73 | 84 |
} else { |
74 | 85 |
data = data << 1 | (~last_bit & 1); |
... | ... | |
78 | 89 |
last_bit = this_bit; |
79 | 90 |
|
80 | 91 |
if (count == 15) { |
81 |
stop_detect();
|
|
92 |
restart_detect();
|
|
82 | 93 |
smb_send_data(&data, sizeof(data)); |
83 | 94 |
} |
84 | 95 |
} |
... | ... | |
138 | 149 |
//////////////////////////////////////////////////////////////////////////////// |
139 | 150 |
|
140 | 151 |
|
141 |
ISR(INT0_vect) { |
|
142 |
bom_start(); |
|
143 |
} |
|
144 |
|
|
145 |
|
|
146 |
|
|
147 | 152 |
static void slave_rx(uint8_t *buf, int len) { |
148 | 153 |
if (len >= 3) { |
149 | 154 |
switch (buf[0]) { |
Also available in: Unified diff