summaryrefslogtreecommitdiffstats
path: root/i2c.cpp
blob: 39b6b28b3afe7b27dfc815e4b057f2146e0d23b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "i2c.h"
#include "avr.h"

#include <util/twi.h>


static bool I2CStartSend(uint8_t addr);
static bool I2CStartRecv(uint8_t addr);
static void I2CStop();


static bool I2CStartSend(uint8_t addr) {
  while(true) {
    TWCR |= (1<<TWINT)|(1<<TWSTA);
    
    while(!(TWCR & (1<<TWINT)));
    
    if(TW_STATUS != TW_START && TW_STATUS != TW_REP_START) continue;
    
    TWDR = (addr&0xFE);
    TWCR = (1<<TWEN)|(1<<TWINT);
    
    while(!(TWCR & (1<<TWINT)));
    
    if(TW_STATUS == TW_MT_SLA_ACK)
      return true;
    
    I2CStop();
  }
}

static bool I2CStartRecv(uint8_t addr) {
  while(true) {
    TWCR |= (1<<TWINT)|(1<<TWSTA);
    
    while(!(TWCR & (1<<TWINT)));
        
    if(TW_STATUS != TW_START && TW_STATUS != TW_REP_START) continue;
    
    TWDR = (addr|1);
    TWCR |= (1<<TWINT);
    
    while(!(TWCR & (1<<TWINT)));
    
    if(TW_STATUS == TW_MR_SLA_ACK)
      return true;
    
    I2CStop();
  }
}

static void I2CStop() {
  TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}


void initI2C() {
  TWBR = 10;
  TWCR = (1<<TWEN);
  TWSR = 0;
}

bool I2CSendByte(uint8_t addr, uint8_t data) {
  if(!I2CStartSend(addr)) return false;
  
  TWDR=data;
  
  TWCR = (1<<TWEN)|(1<<TWINT);
  while(!(TWCR & (1<<TWINT)));
  
  if(TW_STATUS != TW_MT_DATA_ACK) {
    I2CStop();
    return false;
  }
  
  I2CStop();
  return true;
}

bool I2CSend(uint8_t addr, uint8_t *data, int length) {
  if(!I2CStartSend(addr)) return false;
  
  for(int i = 0; i < length; i++) {
    TWDR=data[i];
  
    TWCR = (1<<TWEN)|(1<<TWINT);
    while(!(TWCR & (1<<TWINT)));
    
    if(TW_STATUS != TW_MT_DATA_ACK && i < length-1) {
      I2CStop();
      return false;
    }
  }
  
  I2CStop();
  return true;
}

bool I2CRecvByte(uint8_t addr, uint8_t *data) {
  if(!I2CStartRecv(addr)) return false;
  
  TWCR = (1<<TWEN)|(1<<TWINT);
  
  while(!(TWCR & (1<<TWINT)));
  
  if(TW_STATUS != TW_MR_DATA_ACK && TW_STATUS != TW_MR_DATA_NACK) {
    I2CStop();
    return false;
  }
  
  *data = TWDR;
  TWCR = (1<<TWEN)|(1<<TWINT);
  
  I2CStop();
  
  return true;
}

int I2CRecv(uint8_t addr, uint8_t *data, int length) {
  if(!I2CStartRecv(addr)) return -1;
  
  TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWEA);
  
  for(int i = 0; i < length-1; i++) {
    while(!(TWCR & (1<<TWINT)));
    
    switch(TW_STATUS) {
      case TW_MR_DATA_ACK:
        data[i] = TWDR;
        break;
      case TW_MR_DATA_NACK:
        data[i] = TWDR;
        I2CStop();
        return i+1;
      default:
        I2CStop();
        return -1;
    }
  }
  
  TWCR = (1<<TWEN)|(1<<TWINT);
  while(!(TWCR & (1<<TWINT)));
  
  switch(TW_STATUS) {
    case TW_MR_DATA_ACK:
    case TW_MR_DATA_NACK:
      data[length-1] = TWDR;
      I2CStop();
      return length;
  }
  
  I2CStop();
  return -1;
}