Skip to content

Commit d026b26

Browse files
committed
[feat] Start incremental i2c refactoring. Modify HardwareInterface trait.
- Introduce init_with_system_control function. - Make init function fallible.
1 parent af384a3 commit d026b26

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/i2c/ast1060_i2c.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use ast1060_pac::{I2cglobal, Scu};
99
use core::cmp::min;
1010
use core::fmt::Write;
1111
use core::marker::PhantomData;
12+
use core::ops::Drop;
1213
use core::sync::atomic::{AtomicBool, Ordering};
1314

1415
use embedded_hal::delay::DelayNs;
@@ -305,7 +306,27 @@ macro_rules! i2c_error {
305306
impl<I2C: Instance, I2CT: I2CTarget, L: Logger> HardwareInterface for Ast1060I2c<'_, I2C, I2CT, L> {
306307
type Error = Error;
307308

308-
fn init(&mut self, config: &mut I2cConfig) {
309+
fn init_with_system_control<
310+
S: proposed_traits::system_control::ResetControl<ResetId = crate::syscon::ResetId>
311+
+ proposed_traits::system_control::ClockControl,
312+
>(
313+
&mut self,
314+
system_control: &mut S,
315+
config: &mut I2cConfig,
316+
) -> Result<(), Self::Error> {
317+
use crate::syscon::ResetId;
318+
319+
// Enable I2C clock and deassert reset
320+
let reset_id = ResetId::RstI2C;
321+
system_control
322+
.reset_deassert(&reset_id)
323+
.map_err(|_| Error::Bus)?;
324+
325+
// Initialize the I2C controller
326+
self.init(config)
327+
}
328+
329+
fn init(&mut self, config: &mut I2cConfig) -> Result<(), Self::Error> {
309330
i2c_debug!(self.logger, "i2c init");
310331
i2c_debug!(
311332
self.logger,
@@ -408,6 +429,8 @@ impl<I2C: Instance, I2CT: I2CTarget, L: Logger> HardwareInterface for Ast1060I2c
408429
});
409430
}
410431
}
432+
433+
Ok(())
411434
}
412435
#[allow(clippy::too_many_lines)]
413436
fn configure_timing(&mut self, config: &mut I2cConfig) {

src/i2c/i2c_controller.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@
22

33
use crate::common::{Logger, NoOpLogger};
44
use crate::i2c::common::I2cConfig;
5+
use core::result::Result;
56
use embedded_hal::i2c::{Operation, SevenBitAddress};
7+
use proposed_traits::system_control::{ClockControl, ResetControl};
68

79
pub trait HardwareInterface {
810
type Error: embedded_hal::i2c::Error + core::fmt::Debug;
911

12+
fn init_with_system_control<
13+
S: ResetControl<ResetId = crate::syscon::ResetId>
14+
+ ClockControl<ClockId = crate::syscon::ClockId>,
15+
>(
16+
&mut self,
17+
system_control: &mut S,
18+
config: &mut I2cConfig,
19+
) -> Result<(), Self::Error>;
20+
1021
// Methods return hardware-specific errors
11-
fn init(&mut self, config: &mut I2cConfig);
22+
fn init(&mut self, config: &mut I2cConfig) -> Result<(), Self::Error>;
1223
fn configure_timing(&mut self, config: &mut I2cConfig);
1324
fn enable_interrupts(&mut self, mask: u32);
1425
fn clear_interrupts(&mut self, mask: u32);

src/tests/functional/i2c_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub fn test_i2c_master(uart: &mut UartController<'_>) {
122122
};
123123

124124
pinctrl::Pinctrl::apply_pinctrl_group(pinctrl::PINCTRL_I2C1);
125-
i2c1.hardware.init(&mut i2c1.config);
125+
let _ = i2c1.hardware.init(&mut i2c1.config);
126126

127127
let addr = 0x2e; //device ADT7490
128128
let mut buf = [0x4e];

0 commit comments

Comments
 (0)