Project

General

Profile

Revision cbe25c0a

IDcbe25c0a432251e97d5895a3eb89b89f0bbb60ce
Parent 68845e8e
Child b2554c5c, f5548f32

Added by Thomas Mullins about 10 years ago

Wrote main bom code function

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
}
scout_avr/bom/tiny-twi.c
6 6
#define SDA PB0
7 7
#define SCL PB2
8 8

  
9
#define RECV_BUF_SIZE 2
9
#define RECV_BUF_SIZE 3
10 10
#define SEND_BUF_SIZE 100
11 11

  
12 12
uint8_t address;
......
168 168
  address = addr;
169 169
}
170 170

  
171
void send_data(uint8_t* data, uint8_t length) {
171
void smb_send_data(uint8_t* data, uint8_t length) {
172 172
  uint8_t i;
173 173
  for (i=0; i<length && !send_queue.full; i++) {
174 174
    send_queue.data[send_queue.end++] = data[i];
......
178 178
      send_queue.full = 1;
179 179
  }
180 180
}
181

  
182
void testCB(uint8_t* data, int length) {
183
  send_data(data, length);
184
}
185

  
186

  
187
int main() {
188
  sei();
189
  smb_set_address(5);
190
  DDRB |= _BV(DDB4);
191
  smb_init(testCB);
192
  while(1);
193
}
scout_avr/bom/tiny-twi.h
7 7

  
8 8
void smb_init(slave_rx_t slave_rx);
9 9
void smb_set_address(uint8_t addr);
10
void smb_send_data(uint8_t* data, uint8_t length); 
10 11

  
11 12
#endif

Also available in: Unified diff