scoutos / scout_avr / bom / bom.c @ f572eaeb
History | View | Annotate | Download (1.86 KB)
1 | f572eaeb | Tom Mullins | #include <stdint.h> |
---|---|---|---|
2 | #include <avr/io.h> |
||
3 | #include <avr/interrupt.h> |
||
4 | #include <util/delay.h> |
||
5 | #include "twi.h" |
||
6 | #include "bomi2c.h" |
||
7 | |||
8 | #define QUEUE_SIZE 64 |
||
9 | |||
10 | uint16_t queue[QUEUE_SIZE]; |
||
11 | int queue_idx;
|
||
12 | |||
13 | char last_bit;
|
||
14 | char count;
|
||
15 | uint16_t data; |
||
16 | |||
17 | #define READ_BIT(pin, bit) (((pin) >> bit) & 1) |
||
18 | #define SET_BIT(pin, bit) ((pin) |= (1 << (bit))) |
||
19 | #define RESET_BIT(pin, bit) ((pin) &= ~(1 << (bit))) |
||
20 | |||
21 | static void send_bit(char bit); |
||
22 | |||
23 | static void slave_tx(void) { |
||
24 | twi_transmit((uint8_t*)queue, queue_idx * sizeof(queue[0])); |
||
25 | queue_idx = 0;
|
||
26 | } |
||
27 | |||
28 | static void slave_rx(uint8_t *buf, int len) { |
||
29 | if (len > 0) { |
||
30 | switch (buf[0]) { |
||
31 | case BOM_I2C_SEND:
|
||
32 | // TODO
|
||
33 | break;
|
||
34 | } |
||
35 | } |
||
36 | } |
||
37 | |||
38 | static void bom_start(void) { |
||
39 | data = 0;
|
||
40 | count = 0;
|
||
41 | last_bit = 1;
|
||
42 | // TODO disable interrupt
|
||
43 | // TODO start timer
|
||
44 | } |
||
45 | |||
46 | static void bom_stop(void) { |
||
47 | // TODO stop timer
|
||
48 | // TODO restart INT0
|
||
49 | } |
||
50 | |||
51 | static void bom_init(void) { |
||
52 | |||
53 | // setup INT0
|
||
54 | //EICRA |= _BV(ISC01) | _BV(ISC00);
|
||
55 | //EIMSK |= _BV(INT0);
|
||
56 | |||
57 | // setup timer 0
|
||
58 | //TCCR0B |= _BV(CS01);
|
||
59 | |||
60 | // setup output pin
|
||
61 | SET_BIT(DDRD, 5);
|
||
62 | |||
63 | //sei();
|
||
64 | bom_start(); |
||
65 | } |
||
66 | |||
67 | ISR(TIMER0_COMPA_vect) { |
||
68 | |||
69 | char this_bit = READ_BIT(PIND, PD2);
|
||
70 | |||
71 | if (this_bit == 0) { |
||
72 | if (last_bit == 0) { |
||
73 | bom_stop(); |
||
74 | } |
||
75 | } else {
|
||
76 | data = data << 1 | (~last_bit & 1); |
||
77 | count++; |
||
78 | } |
||
79 | |||
80 | last_bit = this_bit; |
||
81 | |||
82 | if (count == 15) { |
||
83 | if (queue_idx < QUEUE_SIZE) {
|
||
84 | queue[queue_idx] = data; |
||
85 | queue_idx++; |
||
86 | } |
||
87 | bom_stop(); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | static void send_data(uint16_t data) { |
||
92 | for (int i = 14; i >= 0; i--) { |
||
93 | send_bit((data >> i) & 1);
|
||
94 | } |
||
95 | send_bit(0);
|
||
96 | } |
||
97 | |||
98 | static void send_bit(char bit) { |
||
99 | SET_BIT(PORTD, PD5); |
||
100 | _delay_us(320);
|
||
101 | RESET_BIT(PORTD, PD5); |
||
102 | if (bit) {
|
||
103 | _delay_us(1680);
|
||
104 | } else {
|
||
105 | _delay_us(680);
|
||
106 | } |
||
107 | } |
||
108 | |||
109 | int main() {
|
||
110 | bom_init(); |
||
111 | for (;;) {
|
||
112 | send_data(45243);
|
||
113 | _delay_ms(100);
|
||
114 | } |
||
115 | } |
||
116 | |||
117 | ISR(INT0_vect) { |
||
118 | bom_start(); |
||
119 | } |