]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/i2c.c
3ab222aebc50dcbf0d60822738bb80d34c11668f
3 * I2C (TWI) master interface.
7 #include <avr/interrupt.h>
8 #include <util/delay.h>
20 /* General TWI Master status codes */
21 #define TWI_START 0x08 /* START has been transmitted */
22 #define TWI_REP_START 0x10 /* Repeated START has been transmitted */
23 #define TWI_ARB_LOST 0x38 /* Arbitration lost */
25 /* TWI Master Transmitter status codes */
26 #define TWI_MTX_ADR_ACK 0x18 /* SLA+W has been transmitted and ACK received */
27 #define TWI_MTX_ADR_NACK 0x20 /* SLA+W has been transmitted and NACK received */
28 #define TWI_MTX_DATA_ACK 0x28 /* Data byte has been transmitted and ACK received */
29 #define TWI_MTX_DATA_NACK 0x30 /* Data byte has been transmitted and NACK received */
31 /* TWI Master Receiver status codes */
32 #define TWI_MRX_ADR_ACK 0x40 /* SLA+R has been transmitted and ACK received */
33 #define TWI_MRX_ADR_NACK 0x48 /* SLA+R has been transmitted and NACK received */
34 #define TWI_MRX_DATA_ACK 0x50 /* Data byte has been received and ACK transmitted */
35 #define TWI_MRX_DATA_NACK 0x58 /* Data byte has been received and NACK transmitted */
37 /* TWI Miscellaneous status codes */
38 #define TWI_NO_STATE 0xF8 /* No relevant state information available */
39 #define TWI_BUS_ERROR 0x00 /* Bus error due to an illegal START or STOP condition */
43 * TWINT: TWI Interrupt Flag
44 * TWEA: TWI Enable Acknowledge Bit
45 * TWSTA: TWI START Condition Bit
46 * TWSTO: TWI STOP Condition Bit
47 * TWEN: TWI Enable Bit
48 * TWIE: TWI Interrupt Enable
50 * (1<<TWEN)|(1<<TWIE)|(1<<TWINT)
51 * (1<<TWEN)|(1<<TWIE)|(1<<TWINT)| (1<<TWEA)
52 * (1<<TWEN)|(1<<TWIE)|(1<<TWINT)
55 * (1<<TWEN)| (1<<TWINT)| (1<<TWSTO)
61 * (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWSTA)
62 * (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWSTA)
63 * (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWSTA)
64 * (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWSTA)
67 * (1<<TWIE)|(1<<TWSTO)
73 * 0b10000000 Busy (Transmission in progress)
75 * 0b00001000 Start transmitted
76 * 0b00000100 Slave acknowledged address
77 * 0b00000010 Data byte(s) transmitted/received
78 * 0b00000001 Transmission completed
81 *----------------------------------------------------------------------
84 #define TWI_C_DISABLE 0x00
85 #define TWI_C_ENABLE (1<<TWEN)
89 typedef struct i2c_msg_s
{
91 #define XMIT_DONE (1<<0)
92 #define DATA_ACK (1<<1)
93 #define ADDR_ACK (1<<2)
95 #define TIMEOUT (1<<6)
99 uint8_t buf
[CONFIG_SYS_I2C_BUFSIZE
];
102 static volatile i2c_msg_t xmit
;
112 tmp_stat
= xmit
.stat
;
116 switch (twsr
& 0xf8) {
120 tmp_stat
= BUSY
| START
;
121 tmp_idx
= 0; /* reset xmit_buf index */
123 if (tmp_idx
< xmit
.len
) { /* all bytes transmited? */
124 TWDR
= xmit
.buf
[tmp_idx
];
126 next_twcr
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
);
128 tmp_stat
|= XMIT_DONE
;
130 next_twcr
= (1<<TWEN
)|(0<<TWIE
)|(1<<TWINT
)|(1<<TWSTO
);
134 case TWI_MTX_ADR_ACK
:
135 tmp_stat
|= ADDR_ACK
;
137 if (tmp_idx
< xmit
.len
) { /* all bytes transmited? */
138 TWDR
= xmit
.buf
[tmp_idx
];
140 next_twcr
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
);
142 tmp_stat
|= XMIT_DONE
;
144 next_twcr
= (1<<TWEN
)|(0<<TWIE
)|(1<<TWINT
)|(1<<TWSTO
);
148 case TWI_MTX_DATA_ACK
:
149 tmp_stat
|= DATA_ACK
;
151 if (tmp_idx
< xmit
.len
) { /* all bytes transmited? */
152 TWDR
= xmit
.buf
[tmp_idx
];
154 next_twcr
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
);
156 tmp_stat
|= XMIT_DONE
;
158 next_twcr
= (1<<TWEN
)|(0<<TWIE
)|(1<<TWINT
)|(1<<TWSTO
);
163 case TWI_MTX_DATA_NACK
:
164 tmp_stat
|= XMIT_DONE
;
166 next_twcr
= (1<<TWEN
)|(0<<TWIE
)|(1<<TWINT
)|(1<<TWSTO
);
169 case TWI_MRX_ADR_ACK
:
170 tmp_stat
|= ADDR_ACK
;
173 next_twcr
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
)|(1<<TWEA
);
175 next_twcr
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
);
179 case TWI_MRX_DATA_ACK
:
180 tmp_stat
|= DATA_ACK
;
181 xmit
.buf
[tmp_idx
] = TWDR
;
185 next_twcr
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
)|(1<<TWEA
);
188 next_twcr
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
);
192 case TWI_MRX_DATA_NACK
:
193 tmp_stat
|= ADDR_ACK
| DATA_ACK
;
195 xmit
.buf
[tmp_idx
] = TWDR
;
201 next_twcr
= (1<<TWEN
)|(0<<TWIE
)|(1<<TWINT
)|(1<<TWSTO
);
205 xmit
.stat
= tmp_stat
;
209 debug("|%02x", twsr
);
215 /*------------------------------------------------------------------*/
221 static void _init(void)
225 /* Disable TWI, disable TWI interrupt. */
226 /* (Reset TWI hardware state machine.) */
227 TWCR
= TWI_C_DISABLE
;
230 memset((void *) xmit
.buf
, 0xdf, sizeof(xmit
.buf
));
239 void i2c_init(uint32_t speed
)
242 uint32_t tmp_twbr
= F_CPU
/2 / speed
- 8;
244 while (tmp_twbr
> 255) {
248 debug_cond((twps
> 3), "*** TWCLK too low: %lu Hz\n", speed
);
250 twbr
= (uint8_t) tmp_twbr
;
252 debug("*** i2c_init: i2c_speed: %lu, twbr: %u, twps: %u\n",
259 int_fast8_t i2c_waitready(void)
261 uint32_t timer
= get_timer(0);
265 if (get_timer(timer
) >= 30) {
269 } while ((TWCR
& ((1<<TWIE
)|(1<<TWSTO
))) != 0 && !timeout
);
271 xmit
.stat
|= timeout
;
274 dump_ram((uint8_t *) &xmit
, 4, "=== i2c_wait ready: (done)");
281 int i2c_send(uint8_t chip
, uint16_t addr
, uint8_t alen
, uint8_t *buffer
, int8_t len
)
286 rc
= i2c_waitready();
287 if ((rc
& (BUSY
| TIMEOUT
)) != 0)
291 xmit
.buf
[0] = chip
<<1;
292 for (i
= 1; i
< alen
+1; i
++) {
293 xmit
.buf
[i
] = (uint8_t) addr
;
296 for (n
= len
+ i
; i
< n
; i
++)
297 xmit
.buf
[i
] = *buffer
++;
301 dump_ram((uint8_t *) &xmit
, 0x20, "=== i2c_send");
304 /* Enable TWI, TWI int and initiate start condition */
305 TWCR
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
)|(1<<TWSTA
);
313 int i2c_recv(uint8_t chip
, uint8_t *buffer
, int8_t len
)
317 rc
= i2c_waitready();
318 if ((rc
& (BUSY
| TIMEOUT
)) != 0)
323 xmit
.buf
[0] = (chip
<<1) | 1;
326 dump_ram((uint8_t *) &xmit
, 0x20, "=== i2c_recv: before start");
329 /* Enable TWI, TWI int and initiate start condition */
330 TWCR
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
)|(1<<TWSTA
);
331 rc
= i2c_waitready();
334 dump_ram((uint8_t *) &xmit
, 0x20, "=== i2c_recv: after completion");
338 /* at least 1 byte received */
339 for (uint8_t i
=1, n
=xmit
.idx
; i
< n
; i
++)
340 *buffer
++ = xmit
.buf
[i
];
347 * Read/Write interface:
348 * chip: I2C chip address, range 0..127
349 * addr: Memory (register) address within the chip
350 * alen: Number of bytes to use for addr (typically 1, 2 for larger
351 * memories, 0 for register type devices with only one
353 * buffer: Where to read/write the data
354 * len: How many bytes to read/write
356 * Returns: 0 on success, not 0 on failure
359 int i2c_write(uint8_t chip
, unsigned int addr
, uint_fast8_t alen
,
360 uint8_t *buffer
, uint_fast8_t len
)
364 if ((alen
> 2) || (1 + alen
+ len
> CONFIG_SYS_I2C_BUFSIZE
)) {
365 debug("** i2c_write: buffer overflow, alen: %u, len: %u\n",
370 i2c_send(chip
, addr
, alen
, buffer
, len
);
371 rc
= i2c_waitready();
372 debug("** i2c_write: result=0x%02x\n",rc
);
374 return (rc
& XMIT_DONE
) != 0;
377 int i2c_read(uint8_t chip
, unsigned int addr
, uint_fast8_t alen
,
378 uint8_t *buffer
, uint_fast8_t len
)
382 if ((alen
> 2) || (1 + len
> CONFIG_SYS_I2C_BUFSIZE
)) {
383 debug("** i2c_read: parameter error: alen: %u, len: %u\n",
389 i2c_send(chip
, addr
, alen
, NULL
, 0);
391 rc
= i2c_recv(chip
, buffer
, len
);
392 debug("** i2c_read: result=0x%02x\n",rc
);
394 return !((rc
& (XMIT_DONE
|DATA_ACK
)) == (XMIT_DONE
|DATA_ACK
));