Skip to content

Commit b252828

Browse files
committed
Add i2c (UNSTABLE)
1 parent 8414e45 commit b252828

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

uno_def.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,94 @@ void usart_win_cursor(bool on)
113113
usart_setstr("\e[?25h");
114114
else
115115
usart_setstr("\e[?25l");
116+
}
117+
118+
void i2c_init(unsigned long clock, bool pullup)
119+
{
120+
TWBR = ((F_CPU/clock) - 16)/2;
121+
122+
INPUT(A5);
123+
INPUT(A4);
124+
125+
if(pullup)
126+
{
127+
ON(A5);
128+
ON(A4);
129+
}
130+
else
131+
{
132+
OFF(A5);
133+
OFF(A4);
134+
}
135+
}
136+
137+
void i2c_twi_start()
138+
{
139+
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
140+
while (!(TWCR & (1<<TWINT)));
141+
}
142+
143+
void i2c_twi_tx(uint8_t data, I2C_ACK ack)
144+
{
145+
TWDR = data;
146+
TWCR = (1<<TWINT)|(1<<TWEN);
147+
if (ack) TWCR |= (1<<TWEA);
148+
while (!(TWCR & (1<<TWINT)));
149+
}
150+
151+
void i2c_twi_tx16(uint16_t data, I2C_ACK ack)
152+
{
153+
i2c_twi_tx(data>>8, NACK);
154+
i2c_twi_tx((uint8_t)data, NACK);
155+
}
156+
157+
uint8_t i2c_twi_rx(I2C_ACK ack)
158+
{
159+
TWCR = (1<<TWINT)|(1<<TWEN);
160+
if (ack) TWCR |= (1<<TWEA);
161+
while (!(TWCR & (1<<TWINT)));
162+
return TWDR;
163+
}
164+
165+
void i2c_twi_stop()
166+
{
167+
TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
168+
}
169+
170+
void i2c_tx(uint8_t address, uint8_t data, I2C_ACK ack)
171+
{
172+
i2c_twi_start();
173+
174+
//address &= 0b00000001 & WRITE;
175+
176+
i2c_twi_tx(address, ACK);
177+
i2c_twi_tx(data, ack);
178+
179+
i2c_twi_stop();
180+
}
181+
182+
uint8_t i2c_rx(uint8_t address, I2C_ACK ack)
183+
{
184+
uint8_t data = 0;
185+
i2c_twi_start();
186+
187+
//address &= 0b00000001 & READ;
188+
189+
i2c_twi_tx(address, ACK);
190+
data = i2c_twi_rx(ack);
191+
192+
i2c_twi_stop();
193+
return data;
194+
}
195+
196+
void i2c_tx16(uint8_t address, uint16_t data, I2C_ACK ack)
197+
{
198+
i2c_twi_start();
199+
200+
//address &= 0b00000001 & WRITE;
201+
202+
i2c_twi_tx(data>>8, NACK);
203+
i2c_twi_tx((uint8_t)data, NACK);
204+
205+
i2c_twi_stop();
116206
}

uno_def.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define UNO_DEF_H_
1111

1212
#include "glob_def.h"
13+
#include "stdint.h"
1314
#include <util/setbaud.h>
1415

1516
// LED
@@ -173,4 +174,29 @@ void usart_setint(int i, BASE base);
173174

174175
void usart_win_cursor(bool onoff);
175176

177+
typedef enum
178+
{
179+
READ = 1,
180+
WRITE = 0,
181+
} I2C_RW;
182+
183+
typedef enum
184+
{
185+
ACK = 0,
186+
NACK = 1,
187+
} I2C_ACK;
188+
189+
void i2c_init(unsigned long clock, bool pullup);
190+
191+
void i2c_twi_start();
192+
void i2c_twi_tx(uint8_t data, I2C_ACK ack);
193+
void i2c_twi_tx16(uint16_t data, I2C_ACK ack);
194+
uint8_t i2c_twi_rx(I2C_ACK ack);
195+
void i2c_twi_stop();
196+
197+
void i2c_tx(uint8_t address, uint8_t data, I2C_ACK ack);
198+
uint8_t i2c_rx(uint8_t address, I2C_ACK ack);
199+
200+
void i2c_tx16(uint8_t address, uint16_t data, I2C_ACK ack);
201+
176202
#endif /* UNO_DEF_H_ */

0 commit comments

Comments
 (0)