Subversion Repositories group.electronics

Rev

Rev 128 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
123 pfowler 1
#include <avr/io.h>
2
#include <stdlib.h>
3
#include "wire.h"
4
#include "avrutil.h"
5
#include <util/delay.h>
6
 
7
#include "e2p.h"
8
 
9
void e2p_init() {
10
  e2p.i2c_addr = 0x50;
11
}
12
 
13
void e2p_init_addr(uint8_t i2c_addr) {
14
  e2p.i2c_addr = i2c_addr;
15
}
16
 
17
uint8_t e2p_readByte(uint16_t address) {
18
  if (!e2p.i2c_addr) return 0xFF;
19
 
20
  uint8_t buffer = 0xFF;
21
  e2p_readBytes(address, &buffer, 1);
22
  return buffer;
23
}
24
 
25
uint8_t e2p_busy() {
26
  i2c_beginTransmission(e2p.i2c_addr);
27
  return i2c_endTransmission(1);
28
}
29
 
30
void e2p_flush() {
31
 while (e2p_busy()) {
32
    _delay_us(500);
33
 }
34
}
35
 
36
void e2p_readBytes(uint16_t address, uint8_t* buffer, uint8_t length) {
37
  if (!e2p.i2c_addr) return;
38
 
39
  i2c_beginTransmission(e2p.i2c_addr);
40
  i2c_writeByte((int) address >> 8);	// MSB
41
  i2c_writeByte((int) address & 0xFF);	// LSB
42
  i2c_endTransmission(0);
43
 
44
 
45
  i2c_requestFrom(e2p.i2c_addr, length, 1);
46
  uint8_t c;
47
  for ( c = 0; c < length; c++)
48
    if (i2c_available()) buffer[c] = i2c_read();
49
}
50
 
51
void e2p_writeByte(uint16_t address, uint8_t data) {
52
    if (!e2p.i2c_addr) return;
53
 
54
    i2c_beginTransmission(e2p.i2c_addr);
55
    i2c_writeByte((int) address >> 8);	// MSB
56
    i2c_writeByte((int) address & 0xFF);	// LSB
57
    i2c_writeByte(data);
58
    i2c_endTransmission(1);
59
}
60
 
61
void e2p_writeBytes(uint16_t address, uint8_t* data, uint8_t length) {
62
    if (!e2p.i2c_addr) return;
63
 
64
    i2c_beginTransmission(e2p.i2c_addr);
65
    i2c_writeByte((int) address >> 8);	// MSB
66
    i2c_writeByte((int) address & 0xFF);	// LSB
67
    uint8_t c;
68
    for ( c = 0; c < length; c++)
69
      i2c_writeByte(data[c]);
70
    i2c_endTransmission(1);  
71
}