Revision cbe25c0a scout_avr/bom/bom.c

View differences:

scout_avr/bom/bom.c
1 1
#include <stdint.h>
2 2
#include <avr/io.h>
3 3
#include <avr/interrupt.h>
4
#include "twi.h"
4
#include "tiny-twi.h"
5 5
#include "bomi2c.h"
6 6

  
7
#define QUEUE_SIZE 64
8

  
9
uint16_t queue[QUEUE_SIZE];
10
int queue_idx;
11

  
12 7
char last_bit;
13 8
char count;
14 9
uint16_t data;
......
22 17
#define PRESCALAR 8
23 18
#define TIME_US(us) (F_CPU * (us) / 1000000 / PRESCALAR)
24 19

  
25
static void slave_tx(void) {
26
  twi_transmit((uint8_t*)queue, queue_idx * sizeof(queue[0]));
27
  queue_idx = 0;
28
}
29

  
30
static void slave_rx(uint8_t *buf, int len) {
31
  if (len > 0) {
32
    switch (buf[0]) {
33
      case BOM_I2C_SEND:
34
        // TODO
35
        break;
36
    }
37
  }
38
}
20
#define ADDRESS 0x3 
39 21

  
40 22
static void bom_start(void) {
41 23
  data = 0;
42 24
  count = 0;
43 25
  last_bit = 1;
44 26
  // TODO disable interrupt
45
  // TODO start timer
27
  // TODO start timer0
46 28
}
47 29

  
48 30
static void bom_stop(void) {
......
88 70
  last_bit = this_bit;
89 71

  
90 72
  if (count == 15) {
91
    if (queue_idx < QUEUE_SIZE) {
92
      queue[queue_idx] = data;
93
      queue_idx++;
94
    }
73
    smb_send_data(&data, sizeof(data));
95 74
    bom_stop();
96 75
  }
97 76
}
......
125 104
  TCCR1 |= _BV(CS12);  // Set prescalar to 8 and start timer
126 105
  SET_BIT(PORTB, PB4);
127 106
  send_next_bit();
128
  while (currently_sending) { }
129 107
}
130 108

  
131 109
ISR(INT0_vect) {
132 110
  bom_start();
133 111
}
134 112

  
135
// temporary for testing
136 113

  
137
#include <util/delay.h>
114

  
115
static void slave_rx(uint8_t *buf, int len) {
116
  if (len >= 3) {
117
    switch (buf[0]) {
118
      case BOM_I2C_SEND:
119
        if (!currently_sending) send_data((uint16_t) buf[1] << 8 | buf[2]);  
120
        break;
121
    }
122
  }
123
}
124

  
125

  
138 126

  
139 127
int main() {
140 128
  bom_init();
141
  for (;;) {
142
    send_data(4513);
143
    _delay_ms(100);
144
  }
129
  smb_init(slave_rx);
130
  smb_set_address(ADDRESS);
131
  for (;;);;;;;;;;;;;;;;;;; 
145 132
}

Also available in: Unified diff