-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtwi_master.c
231 lines (171 loc) · 3.52 KB
/
twi_master.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "twi_master.h"
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
static uint8_t status(void)
{
uint8_t twsr;
twsr = TWSR & 0xf8;
return(twsr >> 3);
}
static void wait(void)
{
while(!(TWCR & _BV(TWINT)))
(void)0;
}
static void send_stop(void)
{
PORTD &= ~_BV(4);
TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
while(TWCR & _BV(TWSTO))
(void)0;
}
static uint8_t send_start(void)
{
uint8_t stat;
PORTD |= _BV(4);
TWCR = _BV(TWINT) | _BV(TWEN) | _BV(TWSTA);
wait();
stat = status();
if(stat != tms_start_sent)
{
send_stop();
return(tme_send_start | (stat << 4));
}
return(tme_ok);
}
static uint8_t send_address(uint8_t address, uint8_t request_write)
{
uint8_t stat;
TWDR = (address << 1) | (request_write ? 0 : 1);
TWCR = _BV(TWINT) | _BV(TWEN);
wait();
stat = status();
if(request_write)
{
if(stat != tms_addr_w_ack)
{
send_stop();
return(tme_send_address_w | (stat << 4));
}
}
else
{
if(stat != tms_addr_r_ack)
{
send_stop();
return(tme_send_address_r | (stat << 4));
}
}
return(tme_ok);
}
static uint8_t send_byte(uint8_t data)
{
uint8_t stat;
TWDR = data;
TWCR = _BV(TWINT) | _BV(TWEN);
wait();
stat = status();
if(stat != tms_data_sent_ack)
{
send_stop();
return(tme_send_byte | (stat << 4));
}
return(tme_ok);
}
static uint8_t receive_byte(uint8_t *data)
{
uint8_t stat;
TWCR = _BV(TWINT) | _BV(TWEN) | _BV(TWEA);
wait();
stat = status();
if((stat != tms_data_recvd_ack) && (stat != tms_data_recvd_nack))
{
send_stop();
return(tme_receive_byte | (stat << 4));
}
if(stat == tms_data_recvd_nack)
*data = 0xfe;
else
*data = TWDR;
return(tme_ok);
}
static uint8_t send_nack(void)
{
uint8_t stat;
TWCR = _BV(TWINT) | _BV(TWEN);
wait();
stat = status();
if((stat != tms_data_recvd_ack) && (stat != tms_data_recvd_nack))
{
send_stop();
return(tme_send_nack | (stat << 4));
}
return(tme_ok);
}
void twi_master_init(void)
{
PRR |= _BV(PRTWI);
TWCR = 0x00;
DDRC &= ~(_BV(5) | _BV(6)); // SCL, SDA HiZ
PRR &= ~_BV(PRTWI);
TWBR = 32;
TWSR = 0;
TWDR = 0xff;
TWCR = _BV(TWEN);
}
void twi_master_recover(void)
{
uint8_t ix;
// try to send stop condition
TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
_delay_ms(1);
TWCR = _BV(TWINT) | _BV(TWEN);
_delay_ms(1);
// try to resolve bus locked by slave
TWCR = 0x00; // disable TWI, make ports available
DDRC |= _BV(5); // SCL -> output
DDRC &= ~_BV(6); // SDA -> HiZ
for(ix = 16; ix > 0; --ix)
{
PORTC &= ~_BV(5); // SCL low
_delay_ms(1);
PORTC |= _BV(5); // SCL high
_delay_ms(1);
}
return(twi_master_init());
}
uint8_t twi_master_send(uint8_t address, uint8_t length, const uint8_t *buffer)
{
uint8_t rv;
if((rv = send_start()) != tme_ok)
return(rv);
if((rv = send_address(address, 1)) != tme_ok)
return(rv);
for(; length > 0; length--, buffer++)
if((rv = send_byte(*buffer)) != tme_ok)
return(rv);
send_stop();
return(tme_ok);
}
uint8_t twi_master_receive(uint8_t address, uint8_t size, uint8_t *buffer)
{
uint8_t rv, ix;
if((rv = send_start()) != tme_ok)
return(rv);
if((rv = send_address(address, 0)) != tme_ok)
return(rv);
for(ix = 0; ix < size; ix++)
if((rv = receive_byte(&buffer[ix])) != tme_ok)
return(rv);
if((rv = send_nack()) != tme_ok)
return(rv);
send_stop();
return(tme_ok);
}
void twi_master_error(uint8_t *dst, uint16_t size, uint8_t error)
{
static const __flash char format_string[] = "TWI error: %x, state %x\n";
snprintf_P((char *)dst, (size_t)size, format_string,
error & 0x0f, (error & 0xf0) >> 4);
}