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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//! I2C support for F4, which uses an older peripheral than the other families supported
//! by this library.
// todo: Merge this with the other i2c module?
// Based on `stm32f4xx-hal`.
use core::ops::Deref;
use crate::{
clocks::Clocks,
error::{Error, Result},
pac::{RCC, i2c1},
util::{bounded_loop, rcc_en_reset},
};
#[derive(Clone, Copy)]
pub enum I2cDevice {
One,
Two,
#[cfg(not(feature = "f410"))]
Three,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Eq, PartialEq, defmt::Format)]
pub enum I2cError {
// Note: The error type is not currently returned, but is maintained for backwards
// compatibility.
Bus,
Arbitration,
Nack,
Overrun,
Pec,
Timeout,
Crc,
}
/// Represents an Inter-Integrated Circuit (I2C) peripheral.
pub struct I2c<R> {
pub regs: R,
}
impl<R> I2c<R>
where
R: Deref<Target = i2c1::RegisterBlock>,
{
pub fn new(regs: R, device: I2cDevice, speed: u32, clocks: &Clocks) -> Self {
let rcc = unsafe { &(*RCC::ptr()) };
match device {
I2cDevice::One => {
rcc_en_reset!(apb1, i2c1, rcc);
}
I2cDevice::Two => {
rcc_en_reset!(apb1, i2c2, rcc);
}
#[cfg(not(feature = "f410"))]
I2cDevice::Three => {
rcc_en_reset!(apb1, i2c3, rcc);
}
}
let i2c = Self { regs };
i2c.i2c_init(speed, clocks.apb1());
i2c
}
fn i2c_init(&self, speed: u32, pclk: u32) {
// Make sure the I2C unit is disabled so we can configure it
self.regs.cr1().modify(|_, w| w.pe().clear_bit());
// Calculate settings for I2C speed modes
let clock = pclk;
let freq = clock / 1_000_000;
assert!(freq >= 2 && freq <= 50);
// Configure bus frequency into I2C peripheral
self.regs
.cr2()
.write(|w| unsafe { w.freq().bits(freq as u8) });
let trise = if speed <= 100_000 {
freq + 1
} else {
(freq * 300) / 1000 + 1
};
// Configure correct rise times
self.regs
.trise()
.write(|w| unsafe { w.trise().bits(trise as u8) });
// I2C clock control calculation
if speed <= 100_000 {
let ccr = {
let ccr = clock / (speed * 2);
if ccr < 4 { 4 } else { ccr }
};
// Set clock to standard mode with appropriate parameters for selected speed
self.regs.ccr().write(|w| unsafe {
w.f_s()
.clear_bit()
.duty()
.clear_bit()
.ccr()
.bits(ccr as u16)
});
} else {
const DUTYCYCLE: u8 = 0;
if DUTYCYCLE == 0 {
let ccr = clock / (speed * 3);
let ccr = if ccr < 1 { 1 } else { ccr };
// Set clock to fast mode with appropriate parameters for selected speed (2:1 duty cycle)
self.regs.ccr().write(|w| unsafe {
w.f_s().bit(true).duty().clear_bit().ccr().bits(ccr as u16)
});
} else {
let ccr = clock / (speed * 25);
let ccr = if ccr < 1 { 1 } else { ccr };
// Set clock to fast mode with appropriate parameters for selected speed (16:9 duty cycle)
self.regs.ccr().write(|w| unsafe {
w.f_s().bit(true).duty().bit(true).ccr().bits(ccr as u16)
});
}
}
// Enable the I2C processing
self.regs.cr1().modify(|_, w| w.pe().bit(true));
}
pub fn check_and_clear_error_flags(&self) -> Result<()> {
// Note that flags should only be cleared once they have been registered. If flags are
// cleared otherwise, there may be an inherent race condition and flags may be missed.
let sr1 = self.regs.sr1().read();
if sr1.timeout().bit_is_set() {
self.regs.sr1().modify(|_, w| w.timeout().clear_bit());
return Err(Error::I2cError(I2cError::Timeout));
}
if sr1.pecerr().bit_is_set() {
self.regs.sr1().modify(|_, w| w.pecerr().clear_bit());
return Err(Error::I2cError(I2cError::Crc));
}
if sr1.ovr().bit_is_set() {
self.regs.sr1().modify(|_, w| w.ovr().clear_bit());
return Err(Error::I2cError(I2cError::Overrun));
}
if sr1.af().bit_is_set() {
self.regs.sr1().modify(|_, w| w.af().clear_bit());
return Err(Error::I2cError(I2cError::Nack));
}
if sr1.arlo().bit_is_set() {
self.regs.sr1().modify(|_, w| w.arlo().clear_bit());
return Err(Error::I2cError(I2cError::Arbitration));
}
// The errata indicates that BERR may be incorrectly detected. It recommends ignoring and
// clearing the BERR bit instead.
if sr1.berr().bit_is_set() {
self.regs.sr1().modify(|_, w| w.berr().clear_bit());
}
Ok(())
}
pub fn write_bytes(&mut self, addr: u8, bytes: &[u8]) -> Result<()> {
// Send a START condition
self.regs.cr1().modify(|_, w| w.start().bit(true));
// Wait until START condition was generated
bounded_loop!(
self.regs.sr1().read().sb().bit_is_clear(),
Error::RegisterUnchanged,
{
self.check_and_clear_error_flags()?;
}
);
// Also wait until signalled we're master and everything is waiting for us
let sr2 = &self.regs.sr2();
bounded_loop!(
sr2.read().msl().bit_is_clear() && sr2.read().busy().bit_is_clear(),
Error::RegisterUnchanged
);
// Set up current address, we're trying to talk to
self.regs
.dr()
.write(|w| unsafe { w.bits(u16::from(addr) << 1) });
// Wait until address was sent
//
// Check for any I2C errors. If a Nack occurs, the ADDR bit will never be set.
// Wait for the address to be acknowledged
bounded_loop!(
self.regs.sr1().read().addr().bit_is_clear(),
Error::RegisterUnchanged,
{
self.check_and_clear_error_flags()?;
}
);
// Clear condition by reading SR2
self.regs.sr2().read();
// Send bytes
for c in bytes {
self.send_byte(*c)?;
}
// Fallthrough is success
Ok(())
}
pub fn send_byte(&self, byte: u8) -> Result<()> {
// Wait until we're ready for sending
// Check for any I2C errors. If a Nack occurs, the ADDR bit will never be set.
bounded_loop!(
self.regs.sr1().read().tx_e().bit_is_clear(),
Error::RegisterUnchanged,
{
self.check_and_clear_error_flags()?;
}
);
// Push out a byte of data
self.regs.dr().write(|w| unsafe { w.bits(u16::from(byte)) });
// Wait until byte is transferred
bounded_loop!(
self.regs.sr1().read().btf().bit_is_clear(),
Error::RegisterUnchanged,
{
// Check for any potential error conditions.
self.check_and_clear_error_flags()?;
}
);
Ok(())
}
pub fn recv_byte(&self) -> Result<u8> {
// Check for any potential error conditions.
bounded_loop!(
self.regs.sr1().read().rx_ne().bit_is_clear(),
Error::RegisterUnchanged,
{
self.check_and_clear_error_flags()?;
}
);
let value = self.regs.dr().read().bits() as u8;
Ok(value)
}
}
#[cfg(feature = "embedded_hal")]
#[cfg_attr(docsrs, doc(cfg(feature = "embedded_hal")))]
mod embedded_hal_impl {
use embedded_hal::i2c::{ErrorType, I2c as I2cEh, Operation, SevenBitAddress};
use super::*;
impl<R> ErrorType for I2c<R>
where
R: Deref<Target = i2c1::RegisterBlock>,
{
type Error = crate::error::Error;
}
impl<R> I2cEh<SevenBitAddress> for I2c<R>
where
R: Deref<Target = i2c1::RegisterBlock>,
{
fn transaction(
&mut self,
address: SevenBitAddress,
operations: &mut [Operation<'_>],
) -> core::result::Result<(), Self::Error> {
/*
embedded_hal Operation Contract:
1 - Before executing the first operation an ST is sent automatically. This is followed by SAD+R/W as appropriate.
2 - Data from adjacent operations of the same type are sent after each other without an SP or SR.
3 - Between adjacent operations of a different type an SR and SAD+R/W is sent.
4 - After executing the last operation an SP is sent automatically.
5 - If the last operation is a Read the master does not send an acknowledge for the last byte.
- ST = start condition
- SAD+R/W = slave address followed by bit 1 to indicate reading or 0 to indicate writing
- SR = repeated start condition
- SP = stop condition
Note: This hasn't been implemented! I assume 1 and 3 will be, but for 2 we start/stop after each operation
*/
for op in operations {
match op {
Operation::Write(buffer) => {
self.write_bytes(address, buffer)?;
// Send a STOP condition
self.regs.cr1().modify(|_, w| w.stop().set_bit());
}
Operation::Read(buffer) => {
if let Some((last, buffer)) = buffer.split_last_mut() {
// Send a START condition and set ACK bit
self.regs
.cr1()
.modify(|_, w| w.start().set_bit().ack().set_bit());
// Wait for STOP condition to transmit.
while self.regs.cr1().read().stop().bit_is_set() {
cortex_m::asm::nop();
}
// Also wait until signalled we're master and everything is waiting for us
while self.regs.sr2().read().msl().bit_is_clear()
&& self.regs.sr2().read().busy().bit_is_clear()
{
cortex_m::asm::nop();
}
// Set up current address, we're trying to talk to
self.regs
.dr()
.write(|w| unsafe { w.bits((u16::from(address) << 1) + 1) });
self.check_and_clear_error_flags()?;
// Wait until address was sent
while self.regs.sr1().read().addr().bit_is_clear() {
cortex_m::asm::nop();
}
// Clear condition by reading SR2
self.regs.sr2().read();
// Receive bytes into buffer
for c in buffer {
*c = self.recv_byte()?;
}
// Prepare to send Nack then STOP after next byte
self.regs
.cr1()
.modify(|_, w| w.ack().clear_bit().stop().set_bit());
// Receive last byte
*last = self.recv_byte()?;
// Wait for the STOP to be sent.
while self.regs.cr1().read().stop().bit_is_set() {
cortex_m::asm::nop();
}
} else {
return Err(Error::I2cError(I2cError::Overrun));
}
}
}
}
Ok(())
}
}
}