summaryrefslogtreecommitdiff
path: root/hal/i2c.cpp
blob: 867597a2829540fecbdbfd388eaf7b77c4ffa347 (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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "i2c.h"

#include "rcc.h"
#include "stm32.h"
#include "thread.h"

I2C* I2C::self;

template <>
void interrupt<Interrupt::I2C1_EV>() {
	I2C::self->irq_ev();
}

template <>
void interrupt<Interrupt::I2C1_ER>() {
	I2C::self->irq_er();
}

void I2C::irq_ev() {
	uint32_t sr1 = I2C1.SR1;
	I2C1.SR2;
	
	// EV5, SB = 1: Start condition sent.
	if(sr1 & 0x01) {
		// Send address.
		I2C1.DR = (addr << 1) | (writing ? 0 : 1);
	}
	
	// EV6, ADDR = 1: Address sent.
	if(sr1 & 0x02) {
		if(writing) {
			I2C1.DR = *write_p++;
			writing--;
		} else {
			if(reading > 1) {
				I2C1.CR1 |= 0x400; // Set ACK.
			} else {
				I2C1.CR1 |= 0x200; // Set STOP.
			}
		}
	}
	
	// EV7, RxNE = 1: Receive buffer not empty.
	if(sr1 & 0x40) {
		*read_p++ = I2C1.DR;
		reading--;
		
		if(reading == 1) {
			// Unset ACK, set STOP.
			I2C1.CR1 = (I2C1.CR1 & ~0x400) | 0x200;
		}
		
		if(reading == 0) {
			busy = 0;
		}
	}
	
	//I2C1.CR1 &= ~0x400;
	
	// EV8, TxE = 1, BTF = 0: Transmit buffer empty, still writing.
	if(sr1 & 0x80 && !(sr1 & 0x04)) {
		if(writing) {
			// Send data.
			I2C1.DR = *write_p++;
			writing--;
		} else {
			// All data sent.
			
			if(reading) {
				// Send repeat start.
				I2C1.CR1 |= 0x100;
			} else {
				// Send stop.
				I2C1.CR1 |= 0x200;
				busy = 0;
			}
		}
	}
}

void I2C::irq_er() {
	handle_error();
}

void I2C::handle_error() {
	I2C1.SR1;
	I2C1.SR2;
	
	//while(1);
	I2C1.CR1 |= 0x200;
	busy = 0;
}

void I2C::enable() {
	RCC.enable(RCC.I2C1);
	asm volatile("nop");
	
	I2C1.CR1 = 0x8000;
	I2C1.CR1 = 0;
	
	I2C1.CR2 = 0x700 | 36;
	I2C1.TRISE = 37;
	I2C1.CCR = 180;
	
	Interrupt::enable(Interrupt::I2C1_EV);
	Interrupt::enable(Interrupt::I2C1_ER);
	
	I2C1.CR1 = 1;
}

void I2C::write_reg(uint8_t addr_, uint8_t reg, uint8_t data) {
	addr = addr_;
	writing = 2;
	reading = 0;
	volatile uint8_t buf[] = {reg, data};
	write_p = buf;
	busy = 1;
	
	I2C1.CR1 |= 0x100;
	
	while(busy) {
		Thread::yield();
	}
	
	/*
	while(!(I2C1.SR1 & 0x01)); // Wait for SB.
	
	I2C1.DR = (addr << 1) | 0;
	while (!(I2C1.SR1 & 0x02)); // Wait for ADDR.
	I2C1.SR2;
	
	I2C1.DR = reg;
	while (!(I2C1.SR1 & 0x80)); // Wait for TxE.
	
	I2C1.DR = data;
	while (!(I2C1.SR1 & 0x04)); // Wait for BTF.
	
	I2C1.CR1 |= 0x200;*/
}

void I2C::read_reg(uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf) {
	addr = addr_;
	writing = 1;
	reading = len;
	write_p = &reg;
	read_p = buf;
	busy = 1;
	
	I2C1.CR1 |= 0x100;
	
	while(busy) {
		Thread::yield();
	}
	
	/*
	I2C1.CR1 |= 0x100;
	while(!(I2C1.SR1 & 0x01)); // Wait for SB.
	
	I2C1.DR = (addr << 1) | 0;
	while (!(I2C1.SR1 & 0x02)); // Wait for ADDR.
	I2C1.SR2;
	
	I2C1.DR = reg;
	while (!(I2C1.SR1 & 0x80)); // Wait for TxE.
	
	I2C1.CR1 |= 0x100;
	while(!(I2C1.SR1 & 0x01)); // Wait for SB.
	
	I2C1.DR = (addr << 1) | 1;
	while (!(I2C1.SR1 & 0x02)); // Wait for ADDR.
	I2C1.SR2;
	
	I2C1.CR1 |= 0x400; // Set ACK.
	
	while(len) {
		if(len == 3) {
			while (!(I2C1.SR1 & 0x04)); // Wait for BTF.
			
			I2C1.CR1 &= ~0x400; // Clear ACK.
			*buf++ = I2C1.DR;
			len--;
			
			I2C1.CR1 |= 0x200; // Set STOP.
			*buf++ = I2C1.DR;
			len--;
			
		} else {
			while (!(I2C1.SR1 & 0x40)); // Wait for RxNE.
			
			*buf++ = I2C1.DR;
			len--;
		}
	}
	*/
}