-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c.c
210 lines (168 loc) · 5.07 KB
/
i2c.c
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// i2c.h
//
//
// Created by NullWolf on 09/07/17.
// Copyright © 2017 NullWolf. All rights reserved.
//
#include <avr/io.h>
#include "i2c.h"
static bool masterMode;
void i2c_init(void)
{
TWSR &= ~((1 << TWPS1) | (1 << TWPS0)); // pre-scalar 1
TWBR = ((F_CPU / F_I2C) - 16) / 2; // baud rate factor 12
}
uint8_t i2c_tx_start(bool mode)
{
int8_t status = 0;
masterMode = mode; // set global state of R/W bit
/* clear interrupt flag, issue start command (gain control of bus as
master), enable I2C (SCL and SDA are automatically reconfigured) */
TWCR |= (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
/* wait until start transmitted */
while (!(TWCR & (1 << TWINT)));
switch (TWSR & 0xF8) {
/* start condition sent from master */
case 0x08:
status = TRANSMISSION_SUCCESS;
break;
/* repeat start condition sent from master */
case 0x10:
status = TRANSMISSION_SUCCESS;
break;
default:
status = TRANSMISSION_ERROR;
break;
}
return status;
}
uint8_t i2c_tx_address(uint8_t address)
{
int8_t status = 0;
TWDR = (address << 1) | masterMode;
/* clear start command to release bus as master */
TWCR &= ~(1 << TWSTA);
/* clear interrupt flag */
TWCR |= (1 << TWINT);
/* wait until address transmitted */
while (!(TWCR & (1 << TWINT)));
if (masterMode == MASTER_TRANSMITTER) {
switch (TWSR & 0xF8) {
/* address|write sent and ACK returned */
case 0x18:
status = TRANSMISSION_SUCCESS;
break;
/* address|write sent and NACK returned slave */
case 0x20:
status = TRANSMISSION_ERROR;
break;
/* address|write sent and bus failure detected */
case 0x38:
status = TRANSMISSION_ERROR;
break;
default:
status = TRANSMISSION_ERROR;
break;
}
} else if (masterMode == MASTER_RECEIVER) {
switch (TWSR & 0xF8) {
/* address|read sent and ACK returned */
case 0x40:
status = TRANSMISSION_SUCCESS;
break;
/* address|read sent and NACK returned */
case 0x48:
status = TRANSMISSION_ERROR;
break;
case 0x38:
status = TRANSMISSION_ERROR;
break;
default:
status = TRANSMISSION_ERROR;
break;
}
}
return status;
}
uint8_t i2c_tx_byte(uint8_t byteData)
{
int8_t status = 0;
TWDR = byteData; // load data buffer with data to be transmitted
TWCR |= (1 << TWINT); // clear interrupt flag
/* wait until data transmitted */
while (!(TWCR & (1 << TWINT)));
/* retrieve transmission status codes */
switch (TWSR & 0xF8) {
/* byte sent and ACK returned */
case 0x28:
status = TRANSMISSION_SUCCESS;
break;
/* byte sent and NACK returned */
case 0x30:
status = TRANSMISSION_ERROR;
break;
/* byte sent and bus failure detected */
case 0x38:
status = TRANSMISSION_ERROR;
break;
default:
status = TRANSMISSION_ERROR;
break;
}
return status;
}
bool i2c_timeout(void)
{
uint8_t time = TIMEOUT;
int8_t status = BUS_DISCONNECTED;
while (time-- > 0) {
/* check to see if bus is ready */
if ((TWCR & (1 << TWINT))) {
status = BUS_CONNECTED;
break;
}
}
return status;
}
uint8_t i2c_rx_byte(bool response)
{
int8_t status;
if (response == ACK) {
TWCR |= (1 << TWEA); // generate ACK
} else {
TWCR &= ~(1 << TWEA); // generate NACK
}
/* clear interrupt flag */
TWCR |= (1 << TWINT);
/* detect bus time-out */
if (i2c_timeout() != BUS_DISCONNECTED) {
/* retrieve transmission status codes or received data */
switch (TWSR & 0xF8) {
/* data byte read and ACK returned by master */
case 0x50:
status = TWDR;
break;
/* data byte read and NACK returned by master */
case 0x58:
status = TWDR;
break;
/* bus failure detected */
case 0x38:
status = TRANSMISSION_ERROR;
break;
default:
status = TRANSMISSION_ERROR;
break;
}
} else {
status = TRANSMISSION_ERROR;
}
return status;
}
void i2c_tx_stop(void)
{
/* clear interrupt flag, issue stop command (cleared automatically) */
TWCR |= (1 << TWINT) | (1 << TWSTO);
while (!(TWCR & (1 << TWSTO))); // wait until stop transmitted
}