Skip to content

Commit fb0e834

Browse files
committed
Update init function.
1 parent c2c183a commit fb0e834

File tree

2 files changed

+103
-6
lines changed

2 files changed

+103
-6
lines changed

src/i2c/ast1060_i2c.rs

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,108 @@ macro_rules! dbg {
319319
impl<'a, I2C: Instance, I2CT: I2CTarget> HardwareInterface for I2cController<'a, I2C, I2CT> {
320320
type Error = Error;
321321

322-
fn init(&mut self) {
323-
self.init();
322+
fn init(&mut self, mut config: &mut I2cConfig) {
323+
dbg!(self, "i2c init");
324+
dbg!(
325+
self,
326+
"mdma_buf {:p}, sdma_buf {:p}",
327+
self.mdma_buf.as_ptr(),
328+
self.sdma_buf.as_ptr()
329+
);
330+
let scu = unsafe { &*Scu::ptr() };
331+
// global init
332+
if I2CGLOBAL_INIT
333+
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
334+
.is_ok()
335+
{
336+
dbg!(self, "i2c global init");
337+
scu.scu050().write(|w| w.rst_i2csmbus_ctrl().set_bit());
338+
let mut delay = DummyDelay {};
339+
delay.delay_ns(1_000_000); // 1ms delay
340+
scu.scu054().write(|w| unsafe { w.bits(0x4) });
341+
delay.delay_ns(1_000_000); // 1ms delay
342+
343+
let i2cg = unsafe { &*I2cglobal::ptr() };
344+
i2cg.i2cg0c().write(|w| {
345+
w.clk_divider_mode_sel()
346+
.set_bit()
347+
.reg_definition_sel()
348+
.set_bit()
349+
.select_the_action_when_slave_pkt_mode_rxbuf_empty()
350+
.set_bit()
351+
});
352+
/*
353+
* APB clk : 50Mhz
354+
* div : scl : baseclk [APB/((div/2) + 1)] : tBuf [1/bclk * 16]
355+
* I2CG10[31:24] base clk4 for i2c auto recovery timeout counter (0x62)
356+
* I2CG10[23:16] base clk3 for Standard-mode (100Khz) min tBuf 4.7us
357+
* 0x1d : 100.8Khz : 3.225Mhz : 4.96us
358+
* 0x1e : 97.66Khz : 3.125Mhz : 5.12us
359+
* 0x1f : 97.85Khz : 3.03Mhz : 5.28us
360+
* 0x20 : 98.04Khz : 2.94Mhz : 5.44us
361+
* 0x21 : 98.61Khz : 2.857Mhz : 5.6us
362+
* 0x22 : 99.21Khz : 2.77Mhz : 5.76us (default)
363+
* I2CG10[15:8] base clk2 for Fast-mode (400Khz) min tBuf 1.3us
364+
* 0x08 : 400Khz : 10Mhz : 1.6us
365+
* I2CG10[7:0] base clk1 for Fast-mode Plus (1Mhz) min tBuf 0.5us
366+
* 0x03 : 1Mhz : 20Mhz : 0.8us
367+
*/
368+
i2cg.i2cg10().write(|w| unsafe { w.bits(0x62220803) });
369+
}
370+
371+
// i2c reset
372+
self.i2c.i2cc00().write(|w| unsafe { w.bits(0) });
373+
if !config.multi_master {
374+
self.i2c
375+
.i2cc00()
376+
.write(|w| w.dis_multimaster_capability_for_master_fn_only().set_bit());
377+
}
378+
self.i2c.i2cc00().write(|w| {
379+
w.enbl_bus_autorelease_when_scllow_sdalow_or_slave_mode_inactive_timeout()
380+
.set_bit()
381+
.enbl_master_fn()
382+
.set_bit()
383+
});
384+
385+
// set AC timing
386+
self.configure_timing(&mut config);
387+
// clear interrupts
388+
self.i2c.i2cm14().write(|w| unsafe { w.bits(0xffffffff) });
389+
// set interrupt
390+
self.i2c.i2cm10().write(|w| {
391+
w.enbl_pkt_cmd_done_int()
392+
.set_bit()
393+
.enbl_bus_recover_done_int()
394+
.set_bit()
395+
});
396+
dbg!(
397+
self,
398+
"i2c init after set interrupt: {:#x}",
399+
self.i2c.i2cm14().read().bits()
400+
);
401+
if config.smbus_alert {
402+
self.i2c
403+
.i2cm10()
404+
.write(|w| w.enbl_smbus_dev_alert_int().set_bit());
405+
}
406+
407+
if cfg!(feature = "i2c_target") {
408+
dbg!(self, "i2c target enabled");
409+
// clear slave interrupts
410+
self.i2c.i2cs24().write(|w| unsafe { w.bits(0xffffffff) });
411+
if config.xfer_mode == I2cXferMode::ByteMode {
412+
self.i2c.i2cs20().write(|w| unsafe { w.bits(0xffff) });
413+
} else {
414+
self.i2c.i2cs20().write(|w| {
415+
w.enbl_slave_mode_inactive_timeout_int()
416+
.set_bit()
417+
.enbl_pkt_cmd_done_int()
418+
.set_bit()
419+
});
420+
}
421+
}
324422
}
325-
fn configure_timing(&mut self, config: &mut I2cConfig) -> Result<(), Self::Error> {
423+
fn configure_timing(&mut self, config: &mut I2cConfig) {
326424
let scu = unsafe { &*Scu::ptr() };
327425
config.timing_config.clk_src =
328426
HPLL_FREQ / ((scu.scu310().read().apbbus_pclkdivider_sel().bits() as u32 + 1) * 2);
@@ -436,7 +534,6 @@ impl<'a, I2C: Instance, I2CT: I2CTarget> HardwareInterface for I2cController<'a,
436534
});
437535
}
438536
}
439-
Ok(())
440537
}
441538
fn enable_interrupts(&mut self, mask: u32) {
442539
self.i2c.i2cm10().write(|w| unsafe { w.bits(mask) });

src/i2c/i2c.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ pub trait HardwareInterface {
77
type Error: embedded_hal::i2c::Error + core::fmt::Debug;
88

99
// Methods return hardware-specific errors
10-
fn init(&mut self);
11-
fn configure_timing(&mut self, config: &mut I2cConfig) -> Result<(), Self::Error>;
10+
fn init(&mut self, config: &mut I2cConfig);
11+
fn configure_timing(&mut self, config: &mut I2cConfig);
1212
fn enable_interrupts(&mut self, mask: u32);
1313
fn clear_interrupts(&mut self, mask: u32);
1414
#[cfg(feature = "i2c_target")]

0 commit comments

Comments
 (0)