Project

General

Profile

Statistics
| Branch: | Revision:

scoutos / scout_avr / bom / bom.c @ a75b176a

History | View | Annotate | Download (4 KB)

1
#include <stdint.h>
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
4
#include <util/delay.h>
5
#include "tiny-twi.h"
6
#include "bomi2c.h"
7

    
8
char last_bit;
9
char count;
10
uint16_t data;
11
volatile char currently_sending;
12
char sending_counter;
13
uint16_t sending_data;
14

    
15
#define READ_BIT(pin, bit) (((pin) >> bit) & 1)
16
#define SET_BIT(pin, bit) ((pin) |= (1 << (bit)))
17
#define CLEAR_BIT(pin, bit) ((pin) &= ~(1 << (bit)))
18
#define PRESCALAR 8
19
#define TIME_US(us) (F_CPU * (us) / 1000000 / PRESCALAR)
20

    
21
#define ADDRESS 0x3 
22
#define DETECT PB3
23
#define EMIT PB4
24

    
25
#if DETECT == PB3
26
  #define PCINT_DETECT PCINT3
27
#endif
28
#if DETECT == PB4
29
  #define PCINT_DETECT PCINT4
30
#endif
31

    
32
static void bom_init(void) {
33

    
34
  SET_BIT(DDRB, EMIT);
35
  SET_BIT(DDRB, PB1);
36

    
37
  init_detect();
38

    
39
  init_timer1();
40

    
41
  sei();
42
}
43

    
44
////////////////////////////////// Receiving ///////////////////////////////////
45

    
46
static void init_detect(void) {
47
  TCCR0A |= _BV(WGM01);    // enable reset on compare A
48
  TIMSK  |= _BV(OCIE0A);   // enable timer0 compare match A
49
  OCR0A   = TIME_US(1000); // trigger every 1ms
50

    
51
  GIFR   |= _BV(PCIF);   // Clear any previous detections
52
  GIMSK  |= _BV(PCIE);   // Enable pin change interrupt for detection
53
  PCMSK  |= _BV(PCINT_DETECT); // Enable interrupt on detect pin
54
}
55

    
56
// Disables the interrupt that begins the detect process while it is running
57
static void disable_detect_interrupt(void) {
58
  PCMSK &= ~_BV(PCINT_DETECT);
59
}
60

    
61
static void start_timer0(void) {
62
  TCNT0 = 0; // Reset timer 0 count to 0
63
  TCCR0B |= _BV(CS01);     // setup clkio and start the timer
64
}
65

    
66
static void start_detect(void) {
67
  disable_detect_interrupt();
68
  data = 0;
69
  count = 0;
70
  last_bit = 1;
71
  _delay_us(160);
72
  start_timer0();
73
}
74

    
75
static void restart_detect(void) {
76
  TCCR0B &= ~0x07; // Stop timer0
77
  PCMSK  |= _BV(PCINT_DETECT); // Enable interrupt on detect pin
78
}
79

    
80
ISR(PCINT0_vect) {
81
  start_detect();
82
}
83

    
84
ISR(TIMER0_COMPA_vect) {
85

    
86
  char this_bit = READ_BIT(PINB, DETECT);
87
  SET_BIT(PINB, PB1); // TOGGLE THE PIN MOFOS
88
  smb_send_data((uint8_t*)&this_bit, 1);
89

    
90
  if (this_bit == 1) {
91
    if (last_bit == 1) {
92
      restart_detect();
93
    }
94
  } else {
95
    data = data << 1 | (last_bit & 1);
96
    count++;
97
  }
98

    
99
  last_bit = this_bit;
100

    
101
  if (count == 15) {
102
    restart_detect();
103
    smb_send_data((uint8_t*) &data, sizeof(data));
104
  }
105
}
106

    
107
////////////////////////////////////////////////////////////////////////////////
108

    
109
//////////////////////////////////// Sending ///////////////////////////////////
110

    
111
static void start_timer1() {
112
  TCNT1 = 0; // Reset timer 1 count to 0
113
  TCCR1 |= _BV(CS12); // Set prescalar to 8 and start timer1
114
}
115

    
116
static void stop_timer1() {
117
  TCCR1 &= ~0x0F; // Stop the timer without clearing CTC1
118
}
119

    
120
static void init_timer1() {
121
  OCR1B = TIME_US(320); // Set match b to fire every 320 us
122
  TIMSK |= _BV(OCIE1A) | _BV(OCIE1B); // Enable interrupt for match a and b
123
  TCCR1 |= _BV(CTC1);   // Enables resetting timer1 after matches match c
124
}
125

    
126
static void send_data(uint16_t data) {
127
  sending_data = data;
128
  sending_counter = 0;
129
  currently_sending = 1;
130
  SET_BIT(PORTB, EMIT);
131

    
132
  start_timer1();
133
  send_next_bit();
134
}
135

    
136
static void send_next_bit() {
137
  char next_bit = sending_data >> (14 - sending_counter) & 1;
138
  char ocr_temp = next_bit ? TIME_US(2000-40) : TIME_US(1000-20);
139
  OCR1A = ocr_temp; // Compare A - Turns on transmitter
140
  OCR1C = ocr_temp; // Reset the timer when match a fires
141
}
142

    
143
// Compare A - Turns on transmitter
144
ISR(TIMER1_COMPA_vect) {
145
  SET_BIT(PORTB, EMIT); 
146
  sending_counter++;
147
  send_next_bit();
148
}
149

    
150
// Compare B - Turns off transmitter after 320us
151
ISR(TIMER1_COMPB_vect) {
152
  CLEAR_BIT(PORTB, EMIT);
153
  if (sending_counter >= 15) {
154
    currently_sending = 0;
155
    stop_timer1();
156
  }
157
}
158

    
159
////////////////////////////////////////////////////////////////////////////////
160

    
161

    
162
static void slave_rx(uint8_t *buf, int len) {
163
  if (len >= 3) {
164
    switch (buf[0]) {
165
      case BOM_I2C_SEND:
166
        if (!currently_sending) send_data((uint16_t) buf[1] << 8 | buf[2]);  
167
        break;
168
    }
169
  }
170
}
171

    
172

    
173

    
174
int main() {
175
  bom_init();
176
  smb_init(slave_rx);
177
  smb_set_address(ADDRESS);
178
  for (;;);;;;;;;;;;;;;;;;; 
179
}