-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2c.c
158 lines (139 loc) · 2.87 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
/* ------------------------------------------------------------------
* I2C Library for AVR Devices
* ------------------------------------------------------------------ */
#include "i2c.h"
/**
* Initialize I2C Interface
*/
void i2c_init ( void )
{
TWCR = _BV ( TWEN );
TWBR = ( F_CPU / I2C_CONFIG_F_SCL - 16 ) / 2;
}
/**
* Initialize I2C Interface with pullups
*/
void i2c_init_with_pullups ( void )
{
I2C_CONFIG_DDR &= ~( _BV ( I2C_CONFIG_SDA ) | _BV ( I2C_CONFIG_SCL ) );
I2C_CONFIG_PORT |= _BV ( I2C_CONFIG_SDA ) | _BV ( I2C_CONFIG_SCL );
i2c_init ( );
}
/**
* Uninitialize I2C Interface
*/
void i2c_uninit ( void )
{
TWCR &= ~_BV ( TWEN );
}
/**
* Wait until I2C Interface is ready
*/
static void i2c_wait ( void )
{
while ( !( TWCR & _BV ( TWINT ) ) );
}
/**
* Send I2C Start Condition
*/
int8_t i2c_start ( void )
{
TWCR = _BV ( TWEN ) | _BV ( TWINT ) | _BV ( TWSTA );
i2c_wait ( );
return ( TWSR & TW_STATUS_MASK ) != TW_START;
}
/**
* Send I2C Repeated-Start Condition
*/
int8_t i2c_restart ( void )
{
TWCR = _BV ( TWEN ) | _BV ( TWINT ) | _BV ( TWSTA );
i2c_wait ( );
return ( TWSR & TW_STATUS_MASK ) != TW_REP_START;
}
/**
* Send I2C Stop Condition
*/
int8_t i2c_stop ( void )
{
TWCR = _BV ( TWEN ) | _BV ( TWINT ) | _BV ( TWSTO );
while ( TWCR & _BV ( TWSTO ) );
return 0;
}
/**
* Select I2C Slave Address
*/
int8_t i2c_addr ( uint8_t addr )
{
TWDR = addr;
TWCR = _BV ( TWEN ) | _BV ( TWINT );
i2c_wait ( );
return ( TWSR & TW_STATUS_MASK ) != ( ( addr & 1 ) ? TW_MR_SLA_ACK : TW_MT_SLA_ACK );
}
/**
* Send data byte to Slave Device
*/
int8_t i2c_tx_byte ( uint8_t byte )
{
TWDR = byte;
TWCR = _BV ( TWEN ) | _BV ( TWINT );
i2c_wait ( );
return ( TWSR & TW_STATUS_MASK ) != TW_MT_DATA_ACK;
}
/**
* Send data bytes to Slave Device
*/
int8_t i2c_tx_data ( const uint8_t * data, size_t len )
{
size_t i;
for ( i = 0; i < len; i++ )
{
if ( i2c_tx_byte ( data[i] ) )
{
return 1;
}
}
return 0;
}
/**
* Receive data byte from Slave Device
*/
int8_t i2c_rx_byte ( uint8_t * byte )
{
TWCR = _BV ( TWEN ) | _BV ( TWINT ) | _BV ( TWEA );
i2c_wait ( );
*byte = TWDR;
return ( TWSR & TW_STATUS_MASK ) != TW_MR_DATA_ACK;
}
/**
* Receive last data byte from Slave Device
*/
int8_t i2c_rx_last ( uint8_t * byte )
{
TWCR = _BV ( TWEN ) | _BV ( TWINT );
i2c_wait ( );
*byte = TWDR;
return ( TWSR & TW_STATUS_MASK ) != TW_MR_DATA_NACK;
}
/**
* Receive data bytes from Slave Device
*/
int8_t i2c_rx_data ( uint8_t * data, size_t len )
{
size_t i;
for ( i = 0; i + 1 < len; i++ )
{
if ( i2c_rx_byte ( data + i ) )
{
return 1;
}
}
if ( len )
{
if ( i2c_rx_last ( data + i ) )
{
return 1;
}
}
return 0;
}