]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/i2c.c
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0+
8 * I2C (TWI) master interface.
12 #include <avr/interrupt.h>
22 #define debug_i2c(fmt, args...) \
23 debug_cond(DEBUG_I2C, fmt, ##args)
26 /* General TWI Master status codes */
27 #define TWI_START 0x08 /* START has been transmitted */
28 #define TWI_REP_START 0x10 /* Repeated START has been transmitted */
29 #define TWI_ARB_LOST 0x38 /* Arbitration lost */
31 /* TWI Master Transmitter status codes */
32 #define TWI_MTX_ADR_ACK 0x18 /* SLA+W has been transmitted and ACK received */
33 #define TWI_MTX_ADR_NACK 0x20 /* SLA+W has been transmitted and NACK received */
34 #define TWI_MTX_DATA_ACK 0x28 /* Data byte has been transmitted and ACK received */
35 #define TWI_MTX_DATA_NACK 0x30 /* Data byte has been transmitted and NACK received */
37 /* TWI Master Receiver status codes */
38 #define TWI_MRX_ADR_ACK 0x40 /* SLA+R has been transmitted and ACK received */
39 #define TWI_MRX_ADR_NACK 0x48 /* SLA+R has been transmitted and NACK received */
40 #define TWI_MRX_DATA_ACK 0x50 /* Data byte has been received and ACK transmitted */
41 #define TWI_MRX_DATA_NACK 0x58 /* Data byte has been received and NACK transmitted */
43 /* TWI Miscellaneous status codes */
44 #define TWI_NO_STATE 0xF8 /* No relevant state information available */
45 #define TWI_BUS_ERROR 0x00 /* Bus error due to an illegal START or STOP condition */
49 * TWINT: TWI Interrupt Flag
50 * TWEA: TWI Enable Acknowledge Bit
51 * TWSTA: TWI START Condition Bit
52 * TWSTO: TWI STOP Condition Bit
53 * TWEN: TWI Enable Bit
54 * TWIE: TWI Interrupt Enable
56 * (1<<TWEN)|(1<<TWIE)|(1<<TWINT)
57 * (1<<TWEN)|(1<<TWIE)|(1<<TWINT)| (1<<TWEA)
58 * (1<<TWEN)|(1<<TWIE)|(1<<TWINT)
61 * (1<<TWEN)| (1<<TWINT)| (1<<TWSTO)
67 * (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWSTA)
68 * (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWSTA)
69 * (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWSTA)
70 * (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWSTA)
73 * (1<<TWIE)|(1<<TWSTO)
79 * 0b10000000 Busy (Transmission in progress)
81 * 0b00001000 Start transmitted
82 * 0b00000100 Slave acknowledged address
83 * 0b00000010 Data byte(s) transmitted/received
84 * 0b00000001 Transmission completed
87 *----------------------------------------------------------------------
90 #define TWI_C_DISABLE 0x00
91 #define TWI_C_ENABLE (1<<TWEN)
95 typedef struct i2c_msg_s
{
97 #define XMIT_DONE (1<<0)
98 #define DATA_ACK (1<<1)
99 #define ADDR_ACK (1<<2)
101 #define TIMEOUT (1<<6)
105 uint8_t buf
[CONFIG_SYS_I2C_BUFSIZE
];
108 static volatile i2c_msg_t xmit
;
118 tmp_stat
= xmit
.stat
;
122 switch (twsr
& 0xf8) {
126 tmp_stat
= BUSY
| START
;
127 tmp_idx
= 0; /* reset xmit_buf index */
129 if (tmp_idx
< xmit
.len
) { /* all bytes transmited? */
130 TWDR
= xmit
.buf
[tmp_idx
];
132 next_twcr
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
);
134 tmp_stat
|= XMIT_DONE
;
136 next_twcr
= (1<<TWEN
)|(0<<TWIE
)|(1<<TWINT
)|(1<<TWSTO
);
140 case TWI_MTX_ADR_ACK
:
141 case TWI_MTX_DATA_ACK
:
142 if ((twsr
&0xf8) == TWI_MTX_ADR_ACK
)
143 tmp_stat
|= ADDR_ACK
;
145 tmp_stat
|= DATA_ACK
;
147 if (tmp_idx
< xmit
.len
) { /* all bytes transmited? */
148 TWDR
= xmit
.buf
[tmp_idx
];
150 next_twcr
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
);
152 tmp_stat
|= XMIT_DONE
;
154 next_twcr
= (1<<TWEN
)|(0<<TWIE
)|(1<<TWINT
)|(1<<TWSTO
);
158 case TWI_MTX_DATA_NACK
:
159 tmp_stat
|= XMIT_DONE
;
161 next_twcr
= (1<<TWEN
)|(0<<TWIE
)|(1<<TWINT
)|(1<<TWSTO
);
164 case TWI_MRX_DATA_ACK
:
165 xmit
.buf
[tmp_idx
] = TWDR
;
168 case TWI_MRX_ADR_ACK
:
169 if ((twsr
&0xf8) == TWI_MRX_ADR_ACK
)
170 tmp_stat
|= ADDR_ACK
;
172 tmp_stat
|= DATA_ACK
;
176 next_twcr
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
)|(1<<TWEA
);
178 next_twcr
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
);
182 case TWI_MRX_DATA_NACK
:
183 tmp_stat
|= ADDR_ACK
| DATA_ACK
;
185 xmit
.buf
[tmp_idx
] = TWDR
;
190 next_twcr
= (1<<TWEN
)|(0<<TWIE
)|(1<<TWINT
)|(1<<TWSTO
);
194 xmit
.stat
= tmp_stat
;
197 debug_i2c("|%02x", twsr
);
202 /*------------------------------------------------------------------*/
208 static void _init(void)
212 /* Disable TWI, disable TWI interrupt. */
213 /* (Reset TWI hardware state machine.) */
214 TWCR
= TWI_C_DISABLE
;
217 memset((void *) xmit
.buf
, 0xdf, sizeof(xmit
.buf
));
226 void i2c_init(uint32_t speed
)
229 uint32_t tmp_twbr
= F_CPU
/2 / speed
- 8;
231 while (tmp_twbr
> 255) {
235 debug_cond((twps
> 3), "*** TWCLK too low: %lu Hz\n", speed
);
237 twbr
= (uint8_t) tmp_twbr
;
244 int_fast8_t i2c_waitready(void)
246 uint32_t timer
= get_timer(0);
250 if (get_timer(timer
) >= 30) {
254 } while ((TWCR
& ((1<<TWIE
)|(1<<TWSTO
))) != 0 && !timeout
);
256 xmit
.stat
|= timeout
;
259 dump_ram((uint8_t *) &xmit
, 4, "=== i2c_wait ready: (done)");
266 int i2c_send(uint8_t chip
, uint16_t addr
, uint8_t alen
, uint8_t *buffer
, int8_t len
)
271 rc
= i2c_waitready();
272 if ((rc
& (BUSY
| TIMEOUT
)) != 0)
276 xmit
.buf
[0] = chip
<<1;
277 for (i
= 1; i
< alen
+1; i
++) {
278 xmit
.buf
[i
] = (uint8_t) addr
;
281 for (n
= len
+ i
; i
< n
; i
++)
282 xmit
.buf
[i
] = *buffer
++;
286 dump_ram((uint8_t *) &xmit
, 0x20, "=== i2c_send");
289 /* Enable TWI, TWI int and initiate start condition */
290 TWCR
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
)|(1<<TWSTA
);
298 int i2c_recv(uint8_t chip
, uint8_t *buffer
, int8_t len
)
302 rc
= i2c_waitready();
303 if ((rc
& (BUSY
| TIMEOUT
)) != 0)
308 xmit
.buf
[0] = (chip
<<1) | 1;
311 dump_ram((uint8_t *) &xmit
, 0x20, "=== i2c_recv: before start");
314 /* Enable TWI, TWI int and initiate start condition */
315 TWCR
= (1<<TWEN
)|(1<<TWIE
)|(1<<TWINT
)|(1<<TWSTA
);
316 rc
= i2c_waitready();
319 dump_ram((uint8_t *) &xmit
, 0x20, "=== i2c_recv: after completion");
323 /* at least 1 byte received */
324 for (uint8_t i
=1, n
=xmit
.idx
; i
< n
; i
++)
325 *buffer
++ = xmit
.buf
[i
];
332 * Read/Write interface:
333 * chip: I2C chip address, range 0..127
334 * addr: Memory (register) address within the chip
335 * alen: Number of bytes to use for addr (typically 1, 2 for larger
336 * memories, 0 for register type devices with only one
338 * buffer: Where to read/write the data
339 * len: How many bytes to read/write
341 * Returns: 0 on success, not 0 on failure
344 int i2c_write(uint8_t chip
, unsigned int addr
, uint_fast8_t alen
,
345 uint8_t *buffer
, uint_fast8_t len
)
349 if ((alen
> 2) || (1 + alen
+ len
> CONFIG_SYS_I2C_BUFSIZE
)) {
350 debug("** i2c_write: buffer overflow, alen: %u, len: %u\n",
355 i2c_send(chip
, addr
, alen
, buffer
, len
);
356 rc
= i2c_waitready();
358 return (rc
& XMIT_DONE
) != 0;
361 int i2c_read(uint8_t chip
, unsigned int addr
, uint_fast8_t alen
,
362 uint8_t *buffer
, uint_fast8_t len
)
366 if ((alen
> 2) || (1 + len
> CONFIG_SYS_I2C_BUFSIZE
)) {
367 debug("** i2c_read: parameter error: alen: %u, len: %u\n",
373 i2c_send(chip
, addr
, alen
, NULL
, 0);
375 rc
= i2c_recv(chip
, buffer
, len
);
377 return !((rc
& (XMIT_DONE
|DATA_ACK
)) == (XMIT_DONE
|DATA_ACK
));