Statistics
| Branch: | Revision:

scoutos / scout_avr / bom / bom.c @ a692ef82

History | View | Annotate | Download (3.87 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

    
36
  init_detect();
37

    
38
  init_timer1();
39

    
40
  sei();
41
}
42

    
43
////////////////////////////////// Receiving ///////////////////////////////////
44

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

    
50
  GIFR   |= _BV(PCIF);   // Clear any previous detections
51
  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
}
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
  disable_detect_interrupt();
67
  data = 0;
68
  count = 0;
69
  last_bit = 0;
70
  start_timer0();
71
}
72

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

    
78
ISR(PCINT0_vect) {
79
  start_detect();
80
}
81

    
82
ISR(TIMER0_COMPA_vect) {
83

    
84
  char this_bit = READ_BIT(PINB, DETECT);
85

    
86
  if (this_bit == 1) {
87
    if (last_bit == 1) {
88
      restart_detect();
89
    }
90
  } else {
91
    data = data << 1 | (last_bit & 1);
92
    count++;
93
  }
94

    
95
  last_bit = this_bit;
96

    
97
  if (count == 15) {
98
    restart_detect();
99
    smb_send_data((uint8_t*) &data, sizeof(data));
100
  }
101
}
102

    
103
////////////////////////////////////////////////////////////////////////////////
104

    
105
//////////////////////////////////// Sending ///////////////////////////////////
106

    
107
static void start_timer1() {
108
  TCNT1 = 0; // Reset timer 1 count to 0
109
  TCCR1 |= _BV(CS12); // Set prescalar to 8 and start timer1
110
}
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
static void send_next_bit() {
133
  char next_bit = sending_data >> (14 - sending_counter) & 1;
134
  char ocr_temp = next_bit ? TIME_US(2000) : TIME_US(1000);
135
  OCR1A = ocr_temp; // Compare A - Turns on transmitter
136
  OCR1C = ocr_temp; // Reset the timer when match a fires
137
}
138

    
139
// Compare A - Turns on transmitter
140
ISR(TIMER1_COMPA_vect) {
141
  SET_BIT(PORTB, EMIT); 
142
  sending_counter++;
143
  send_next_bit();
144
}
145

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

    
155
////////////////////////////////////////////////////////////////////////////////
156

    
157

    
158
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

    
170
int main() {
171
  bom_init();
172
  smb_init(slave_rx);
173
  smb_set_address(ADDRESS);
174
  for (;;);;;;;;;;;;;;;;;;; 
175
}