scoutos / scout_avr / bom / bom.c @ a692ef82
History | View | Annotate | Download (3.87 KB)
1 | f572eaeb | Tom Mullins | #include <stdint.h> |
---|---|---|---|
2 | #include <avr/io.h> |
||
3 | #include <avr/interrupt.h> |
||
4 | 5f7a1707 | Tom Mullins | #include <util/delay.h> |
5 | cbe25c0a | tmullins | #include "tiny-twi.h" |
6 | f572eaeb | Tom Mullins | #include "bomi2c.h" |
7 | |||
8 | char last_bit;
|
||
9 | char count;
|
||
10 | uint16_t data; |
||
11 | f8c1522b | Aaron Perley | volatile char currently_sending; |
12 | char sending_counter;
|
||
13 | uint16_t sending_data; |
||
14 | f572eaeb | Tom Mullins | |
15 | #define READ_BIT(pin, bit) (((pin) >> bit) & 1) |
||
16 | #define SET_BIT(pin, bit) ((pin) |= (1 << (bit))) |
||
17 | c7445989 | Tom Mullins | #define CLEAR_BIT(pin, bit) ((pin) &= ~(1 << (bit))) |
18 | f8c1522b | Aaron Perley | #define PRESCALAR 8 |
19 | #define TIME_US(us) (F_CPU * (us) / 1000000 / PRESCALAR) |
||
20 | f572eaeb | Tom Mullins | |
21 | cbe25c0a | tmullins | #define ADDRESS 0x3 |
22 | 1b05b4cc | Aaron Perley | #define DETECT PB3
|
23 | b2554c5c | Aaron Perley | #define EMIT PB4
|
24 | f572eaeb | Tom Mullins | |
25 | 1b05b4cc | Aaron Perley | #if DETECT == PB3
|
26 | #define PCINT_DETECT PCINT3
|
||
27 | #endif
|
||
28 | #if DETECT == PB4
|
||
29 | #define PCINT_DETECT PCINT4
|
||
30 | #endif
|
||
31 | f572eaeb | Tom Mullins | |
32 | 1b05b4cc | Aaron Perley | static void bom_init(void) { |
33 | 5f7a1707 | Tom Mullins | |
34 | SET_BIT(DDRB, EMIT); |
||
35 | |||
36 | 1b05b4cc | Aaron Perley | init_detect(); |
37 | f8c1522b | Aaron Perley | |
38 | b2554c5c | Aaron Perley | init_timer1(); |
39 | f572eaeb | Tom Mullins | |
40 | c7445989 | Tom Mullins | sei(); |
41 | f572eaeb | Tom Mullins | } |
42 | |||
43 | b2554c5c | Aaron Perley | ////////////////////////////////// Receiving ///////////////////////////////////
|
44 | |||
45 | 1b05b4cc | Aaron Perley | static void init_detect(void) { |
46 | e4d78d85 | Julian Binder | TCCR0A |= _BV(WGM01); // enable reset on compare A
|
47 | b2554c5c | Aaron Perley | TIMSK |= _BV(OCIE0A); // enable timer0 compare match A
|
48 | 5f7a1707 | Tom Mullins | OCR0A = TIME_US(1000); // trigger every 1ms |
49 | 1b05b4cc | Aaron Perley | |
50 | 5f7a1707 | Tom Mullins | GIFR |= _BV(PCIF); // Clear any previous detections
|
51 | 1b05b4cc | Aaron Perley | GIMSK |= _BV(PCIE); // Enable pin change interrupt for detection
|
52 | PCMSK |= _BV(PCINT_DETECT); // Enable interrupt on detect pin
|
||
53 | } |
||
54 | |||
55 | // Disables the interrupt that begins the detect process while it is running
|
||
56 | static void disable_detect_interrupt(void) { |
||
57 | PCMSK &= ~_BV(PCINT_DETECT); |
||
58 | b2554c5c | Aaron Perley | } |
59 | |||
60 | static void start_timer0(void) { |
||
61 | TCNT0 = 0; // Reset timer 0 count to 0 |
||
62 | TCCR0B |= _BV(CS01); // setup clkio and start the timer
|
||
63 | } |
||
64 | |||
65 | static void start_detect(void) { |
||
66 | 1b05b4cc | Aaron Perley | disable_detect_interrupt(); |
67 | b2554c5c | Aaron Perley | data = 0;
|
68 | count = 0;
|
||
69 | a692ef82 | roboclub | last_bit = 0;
|
70 | b2554c5c | Aaron Perley | start_timer0(); |
71 | } |
||
72 | |||
73 | 1b05b4cc | Aaron Perley | static void restart_detect(void) { |
74 | b2554c5c | Aaron Perley | TCCR0B &= ~0x07; // Stop timer0 |
75 | 1b05b4cc | Aaron Perley | PCMSK |= _BV(PCINT_DETECT); // Enable interrupt on detect pin
|
76 | } |
||
77 | |||
78 | ISR(PCINT0_vect) { |
||
79 | start_detect(); |
||
80 | b2554c5c | Aaron Perley | } |
81 | |||
82 | f572eaeb | Tom Mullins | ISR(TIMER0_COMPA_vect) { |
83 | |||
84 | b2554c5c | Aaron Perley | char this_bit = READ_BIT(PINB, DETECT);
|
85 | f572eaeb | Tom Mullins | |
86 | 5f7a1707 | Tom Mullins | if (this_bit == 1) { |
87 | if (last_bit == 1) { |
||
88 | 1b05b4cc | Aaron Perley | restart_detect(); |
89 | f572eaeb | Tom Mullins | } |
90 | } else {
|
||
91 | 5f7a1707 | Tom Mullins | data = data << 1 | (last_bit & 1); |
92 | f572eaeb | Tom Mullins | count++; |
93 | } |
||
94 | |||
95 | last_bit = this_bit; |
||
96 | |||
97 | if (count == 15) { |
||
98 | 1b05b4cc | Aaron Perley | restart_detect(); |
99 | e4d78d85 | Julian Binder | smb_send_data((uint8_t*) &data, sizeof(data));
|
100 | f572eaeb | Tom Mullins | } |
101 | } |
||
102 | |||
103 | b2554c5c | Aaron Perley | ////////////////////////////////////////////////////////////////////////////////
|
104 | |||
105 | //////////////////////////////////// Sending ///////////////////////////////////
|
||
106 | |||
107 | static void start_timer1() { |
||
108 | TCNT1 = 0; // Reset timer 1 count to 0 |
||
109 | e4d78d85 | Julian Binder | TCCR1 |= _BV(CS12); // Set prescalar to 8 and start timer1
|
110 | b2554c5c | Aaron Perley | } |
111 | |||
112 | static void stop_timer1() { |
||
113 | TCCR1 &= ~0x0F; // Stop the timer without clearing CTC1 |
||
114 | } |
||
115 | |||
116 | static void init_timer1() { |
||
117 | OCR1B = TIME_US(320); // Set match b to fire every 320 us |
||
118 | TIMSK |= _BV(OCIE1A) | _BV(OCIE1B); // Enable interrupt for match a and b
|
||
119 | TCCR1 |= _BV(CTC1); // Enables resetting timer1 after matches match c
|
||
120 | } |
||
121 | |||
122 | static void send_data(uint16_t data) { |
||
123 | sending_data = data; |
||
124 | sending_counter = 0;
|
||
125 | currently_sending = 1;
|
||
126 | SET_BIT(PORTB, EMIT); |
||
127 | |||
128 | start_timer1(); |
||
129 | send_next_bit(); |
||
130 | } |
||
131 | |||
132 | f8c1522b | Aaron Perley | static void send_next_bit() { |
133 | c7445989 | Tom Mullins | char next_bit = sending_data >> (14 - sending_counter) & 1; |
134 | a692ef82 | roboclub | char ocr_temp = next_bit ? TIME_US(2000) : TIME_US(1000); |
135 | b2554c5c | Aaron Perley | OCR1A = ocr_temp; // Compare A - Turns on transmitter
|
136 | OCR1C = ocr_temp; // Reset the timer when match a fires
|
||
137 | f572eaeb | Tom Mullins | } |
138 | |||
139 | b2554c5c | Aaron Perley | // Compare A - Turns on transmitter
|
140 | f8c1522b | Aaron Perley | ISR(TIMER1_COMPA_vect) { |
141 | b2554c5c | Aaron Perley | SET_BIT(PORTB, EMIT); |
142 | c7445989 | Tom Mullins | sending_counter++; |
143 | send_next_bit(); |
||
144 | } |
||
145 | |||
146 | b2554c5c | Aaron Perley | // Compare B - Turns off transmitter after 320us
|
147 | c7445989 | Tom Mullins | ISR(TIMER1_COMPB_vect) { |
148 | b2554c5c | Aaron Perley | CLEAR_BIT(PORTB, EMIT); |
149 | c7445989 | Tom Mullins | if (sending_counter >= 15) { |
150 | f8c1522b | Aaron Perley | currently_sending = 0;
|
151 | b2554c5c | Aaron Perley | stop_timer1(); |
152 | f572eaeb | Tom Mullins | } |
153 | } |
||
154 | |||
155 | b2554c5c | Aaron Perley | ////////////////////////////////////////////////////////////////////////////////
|
156 | |||
157 | c7445989 | Tom Mullins | |
158 | cbe25c0a | tmullins | static void slave_rx(uint8_t *buf, int len) { |
159 | if (len >= 3) { |
||
160 | switch (buf[0]) { |
||
161 | case BOM_I2C_SEND:
|
||
162 | if (!currently_sending) send_data((uint16_t) buf[1] << 8 | buf[2]); |
||
163 | break;
|
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | |||
168 | |||
169 | c7445989 | Tom Mullins | |
170 | f572eaeb | Tom Mullins | int main() {
|
171 | bom_init(); |
||
172 | cbe25c0a | tmullins | smb_init(slave_rx); |
173 | smb_set_address(ADDRESS); |
||
174 | for (;;);;;;;;;;;;;;;;;;;
|
||
175 | f572eaeb | Tom Mullins | } |