forked from kisse66/DualOptiboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c.h
264 lines (236 loc) · 7.6 KB
/
i2c.h
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Routines based on twitest.c by Joerg Wunsch
* Modified for optiboot by Krister W. <kisse66@hobbylabs.org>
*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <joerg@FreeBSD.ORG> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Joerg Wunsch
* ----------------------------------------------------------------------------
*
* This file is included from optiboot.c so that the I2C routines don't make the
* original too long.
*
* Until someone has time to optimize unfortunately these routines use
* so much space, that 1kB is no longer enough for the bootloader.
*/
#define MAX_ITER 200 //< maximum retries
uint8_t twst;
//uint8_t StartTwiTransfer(uint8_t dr)
//{
// TWDR = dr;
// TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
// while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
// return TW_STATUS;
//}
static inline void EEPROM_init()
{
/* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
#if defined(TWPS0)
/* has prescaler (mega128 & newer) */
TWSR = 0;
#endif
#if F_CPU < 4000000UL
TWBR = 10;
#else
TWBR = ((F_CPU / 100000UL) - 16) / 2;
#endif
//TWDR = 0xFF;
TWCR = (1<<TWEN) | (0<<TWEA);
}
static inline uint8_t FLASH_readByte(uint32_t eeaddr)
{
uint8_t twcr, n = 0, buf=0xFF;
/*
* Note [8]
* First cycle: master transmitter mode
*/
restart:
if (n++ >= MAX_ITER)
return 0xFF;
begin:
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* send start condition */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS)) {
case TW_REP_START: // OK, but should not happen
case TW_START:
break;
case TW_MT_ARB_LOST: // Note [9]
goto begin;
default:
return 0xFF; // error: not in start condition
// NB: do /not/ send stop condition
}
/* Note [10] */
/* send SLA+W */
TWDR = I2C_EEPROM_ADDR | TW_WRITE;
TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS)) {
case TW_MT_SLA_ACK:
break;
case TW_MT_SLA_NACK: /* nack during select: device busy writing */
/* Note [11] */
goto restart;
case TW_MT_ARB_LOST: /* re-arbitrate */
goto begin;
default:
goto error; /* must send stop condition */
}
TWDR = ((uint16_t)eeaddr >> 8); /* 16-bit word address device, send high 8 bits of addr */
TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS)) {
case TW_MT_DATA_ACK:
break;
case TW_MT_DATA_NACK:
goto quit;
case TW_MT_ARB_LOST:
goto begin;
default:
goto error; /* must send stop condition */
}
TWDR = eeaddr; /* low 8 bits of addr */
TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS)) {
case TW_MT_DATA_ACK:
break;
case TW_MT_DATA_NACK:
goto quit;
case TW_MT_ARB_LOST:
goto begin;
default:
goto error; /* must send stop condition */
}
/*
* Note [12]
* Next cycle(s): master receiver mode
*/
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* send (rep.) start condition */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS)) {
case TW_START: // OK, but should not happen
case TW_REP_START:
break;
case TW_MT_ARB_LOST:
goto begin;
default:
goto error;
}
/* send SLA+R */
TWDR = I2C_EEPROM_ADDR | TW_READ;
TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS)) {
case TW_MR_SLA_ACK:
break;
case TW_MR_SLA_NACK:
goto quit;
case TW_MR_ARB_LOST:
goto begin;
default:
goto error;
}
twcr = _BV(TWINT) | _BV(TWEN); /* send NAK this time */
TWCR = twcr; /* clear int to start transmission */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS)) {
case TW_MR_DATA_ACK:
case TW_MR_DATA_NACK:
buf = TWDR;
break;
default:
goto error;
}
quit:
/* Note [14] */
TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); /* send stop condition */
return buf;
error:
goto quit;
}
// used to invalidate EEPROM after update, writes 8 0xFFs
// (with SPI flash a page erase is used)
static inline void EEPROM_invalidate()
{
uint8_t n = 0;
restart:
if (n++ >= MAX_ITER)
return;
begin:
/* Note [15] */
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* send start condition */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS)) {
case TW_REP_START: /* OK, but should not happen */
case TW_START:
break;
case TW_MT_ARB_LOST:
goto begin;
default:
return; /* error: not in start condition */
/* NB: do /not/ send stop condition */
}
/* send SLA+W */
TWDR = I2C_EEPROM_ADDR;
TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS)) {
case TW_MT_SLA_ACK:
break;
case TW_MT_SLA_NACK: /* nack during select: device busy writing */
goto restart;
case TW_MT_ARB_LOST: /* re-arbitrate */
goto begin;
default:
goto error; /* must send stop condition */
}
TWDR = 0;
TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS)) {
case TW_MT_DATA_ACK:
break;
case TW_MT_DATA_NACK:
goto quit;
case TW_MT_ARB_LOST:
goto begin;
default:
goto error; /* must send stop condition */
}
TWDR = 0; /* low 8 bits of addr */
TWCR = _BV(TWINT) | _BV(TWEN); /* clear interrupt to start transmission */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS)) {
case TW_MT_DATA_ACK:
break;
case TW_MT_DATA_NACK:
goto quit;
case TW_MT_ARB_LOST:
goto begin;
default:
goto error; /* must send stop condition */
}
// write 8 times 0xFF
for (n=8; n ; n--) {
TWDR = 0xFF;
TWCR = _BV(TWINT) | _BV(TWEN); /* start transmission */
while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
switch ((twst = TW_STATUS)) {
case TW_MT_DATA_NACK:
goto error; /* device write protected -- Note [16] */
case TW_MT_DATA_ACK:
break;
default:
goto error;
}
}
quit:
TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); /* send stop condition */
// add delay to complete write?
return;
error:
goto quit;
}