aboutsummaryrefslogtreecommitdiff
path: root/src/i2c.c
blob: cca68c6a255d615d540d0bba71ddaac6657583e9 (plain)
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
#include <i2c.h>
#include <hardware.h>

#define SDA (1 << 0)
#define SCL (1 << 1)

void
_delay_ms(uint8_t ms);

void
i2c_start_condition(void)
{
	port_a_data &= ~SDA;
	_delay_ms(1);
	port_a_data &= ~SCL;
	_delay_ms(1);
}

void
i2c_restart_condition(void)
{
	port_a_data |= SDA;
	_delay_ms(1);
	port_a_data |= SCL;
	_delay_ms(1);
	i2c_start_condition();
}

void
i2c_stop_condition(void)
{
	port_a_data |= SCL;
	_delay_ms(1);
	port_a_data |= SDA;
	_delay_ms(1);
}

void
i2c_send(uint8_t b)
{
	uint8_t i;

	for (i = 0; i < 8; ++i) {
		if (b & 1)
			port_a_data |= SDA;
		else
			port_a_data &= ~SDA;

		port_a_data |= SCL;
		_delay_ms(1);
		port_a_data &= ~SCL;
		_delay_ms(1);

		b >>= 1;
	}

	port_a_ctrl = PIO_MODE_3;
	port_a_ctrl = 0x7D;

	// Read ack
	port_a_data |= SCL;
	_delay_ms(1);
	port_a_data &= ~SCL;
	_delay_ms(1);

	port_a_ctrl = PIO_MODE_3;
	port_a_ctrl = 0x7C;
}

uint8_t
i2c_recv(uint8_t ack)
{
	uint8_t i;
	uint8_t b = 0;

	port_a_ctrl = PIO_MODE_3;
	port_a_ctrl = 0x7D;

	for (i = 0; i < 8; ++i) {
		port_a_data |= SCL;
		_delay_ms(1);
		if (port_a_data & 1)
			b |= 1;

		port_a_data &= ~SCL;
		_delay_ms(1);
		b <<= 1;
	}

	port_a_ctrl = PIO_MODE_3;
	port_a_ctrl = 0x7C;

	if (ack)
		port_a_data |= SDA;
	else
		port_a_data &= ~SDA;

	port_a_data |= SCL;
	_delay_ms(1);
	port_a_data &= ~SCL;
	_delay_ms(1);
	return b;
}