Project

General

Profile

Statistics
| Branch: | Revision:

root / scout_avr / bom / bom.c @ 5f7a1707

History | View | Annotate | Download (3.93 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
  last_bit = 1;
70 5f7a1707 Tom Mullins
  _delay_us(160);
71 b2554c5c Aaron Perley
  start_timer0();
72
}
73
74 1b05b4cc Aaron Perley
static void restart_detect(void) {
75 b2554c5c Aaron Perley
  TCCR0B &= ~0x07; // Stop timer0
76 1b05b4cc Aaron Perley
  PCMSK  |= _BV(PCINT_DETECT); // Enable interrupt on detect pin
77
}
78
79
ISR(PCINT0_vect) {
80
  start_detect();
81 b2554c5c Aaron Perley
}
82
83 f572eaeb Tom Mullins
ISR(TIMER0_COMPA_vect) {
84
85 b2554c5c Aaron Perley
  char this_bit = READ_BIT(PINB, DETECT);
86 5f7a1707 Tom Mullins
  smb_send_data((uint8_t*)&this_bit, 1);
87 f572eaeb Tom Mullins
88 5f7a1707 Tom Mullins
  if (this_bit == 1) {
89
    if (last_bit == 1) {
90 1b05b4cc Aaron Perley
      restart_detect();
91 f572eaeb Tom Mullins
    }
92
  } else {
93 5f7a1707 Tom Mullins
    data = data << 1 | (last_bit & 1);
94 f572eaeb Tom Mullins
    count++;
95
  }
96
97
  last_bit = this_bit;
98
99
  if (count == 15) {
100 1b05b4cc Aaron Perley
    restart_detect();
101 e4d78d85 Julian Binder
    smb_send_data((uint8_t*) &data, sizeof(data));
102 f572eaeb Tom Mullins
  }
103
}
104
105 b2554c5c Aaron Perley
////////////////////////////////////////////////////////////////////////////////
106
107
//////////////////////////////////// Sending ///////////////////////////////////
108
109
static void start_timer1() {
110
  TCNT1 = 0; // Reset timer 1 count to 0
111 e4d78d85 Julian Binder
  TCCR1 |= _BV(CS12); // Set prescalar to 8 and start timer1
112 b2554c5c Aaron Perley
}
113
114
static void stop_timer1() {
115
  TCCR1 &= ~0x0F; // Stop the timer without clearing CTC1
116
}
117
118
static void init_timer1() {
119
  OCR1B = TIME_US(320); // Set match b to fire every 320 us
120
  TIMSK |= _BV(OCIE1A) | _BV(OCIE1B); // Enable interrupt for match a and b
121
  TCCR1 |= _BV(CTC1);   // Enables resetting timer1 after matches match c
122
}
123
124
static void send_data(uint16_t data) {
125
  sending_data = data;
126
  sending_counter = 0;
127
  currently_sending = 1;
128
  SET_BIT(PORTB, EMIT);
129
130
  start_timer1();
131
  send_next_bit();
132
}
133
134 f8c1522b Aaron Perley
static void send_next_bit() {
135 c7445989 Tom Mullins
  char next_bit = sending_data >> (14 - sending_counter) & 1;
136
  char ocr_temp = next_bit ? TIME_US(2000-40) : TIME_US(1000-20);
137 b2554c5c Aaron Perley
  OCR1A = ocr_temp; // Compare A - Turns on transmitter
138
  OCR1C = ocr_temp; // Reset the timer when match a fires
139 f572eaeb Tom Mullins
}
140
141 b2554c5c Aaron Perley
// Compare A - Turns on transmitter
142 f8c1522b Aaron Perley
ISR(TIMER1_COMPA_vect) {
143 b2554c5c Aaron Perley
  SET_BIT(PORTB, EMIT); 
144 c7445989 Tom Mullins
  sending_counter++;
145
  send_next_bit();
146
}
147
148 b2554c5c Aaron Perley
// Compare B - Turns off transmitter after 320us
149 c7445989 Tom Mullins
ISR(TIMER1_COMPB_vect) {
150 b2554c5c Aaron Perley
  CLEAR_BIT(PORTB, EMIT);
151 c7445989 Tom Mullins
  if (sending_counter >= 15) {
152 f8c1522b Aaron Perley
    currently_sending = 0;
153 b2554c5c Aaron Perley
    stop_timer1();
154 f572eaeb Tom Mullins
  }
155
}
156
157 b2554c5c Aaron Perley
////////////////////////////////////////////////////////////////////////////////
158
159 c7445989 Tom Mullins
160 cbe25c0a tmullins
static void slave_rx(uint8_t *buf, int len) {
161
  if (len >= 3) {
162
    switch (buf[0]) {
163
      case BOM_I2C_SEND:
164
        if (!currently_sending) send_data((uint16_t) buf[1] << 8 | buf[2]);  
165
        break;
166
    }
167
  }
168
}
169
170
171 c7445989 Tom Mullins
172 f572eaeb Tom Mullins
int main() {
173
  bom_init();
174 cbe25c0a tmullins
  smb_init(slave_rx);
175
  smb_set_address(ADDRESS);
176
  for (;;);;;;;;;;;;;;;;;;; 
177 f572eaeb Tom Mullins
}