Skip to content
This repository was archived by the owner on Aug 9, 2022. It is now read-only.

Commit bb1c7be

Browse files
committed
Implement I2C master writing and add example using SSD1306 display
1 parent 85d4188 commit bb1c7be

File tree

4 files changed

+456
-0
lines changed

4 files changed

+456
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ void = { version = "1.0.2", default-features = false }
4343

4444
[dev-dependencies]
4545
panic-halt = "0.2.0"
46+
ssd1306 = "0.3.1"
47+
embedded-graphics = "0.6.2"
4648

4749
[[example]]
4850
name = "alloc"

examples/i2c.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
extern crate esp32_hal as hal;
5+
extern crate xtensa_lx6_rt;
6+
7+
use {
8+
core::panic::PanicInfo,
9+
embedded_graphics::{
10+
pixelcolor::BinaryColor, prelude::*, primitives::Circle, primitives::Rectangle,
11+
style::PrimitiveStyle, style::PrimitiveStyleBuilder,
12+
},
13+
hal::{
14+
clock_control::{self, sleep, CPUSource, ClockControl, ClockControlConfig},
15+
dport::Split,
16+
dprintln, i2c,
17+
prelude::*,
18+
timer::Timer,
19+
},
20+
ssd1306::{prelude::*, Builder},
21+
};
22+
23+
#[no_mangle]
24+
fn main() -> ! {
25+
let dp = esp32::Peripherals::take().unwrap();
26+
27+
let (mut dport, dport_clock_control) = dp.DPORT.split();
28+
29+
// setup clocks & watchdog
30+
let mut clkcntrl = ClockControl::new(
31+
dp.RTCCNTL,
32+
dp.APB_CTRL,
33+
dport_clock_control,
34+
clock_control::XTAL_FREQUENCY_AUTO,
35+
)
36+
.unwrap();
37+
38+
// set desired clock frequencies
39+
clkcntrl
40+
.set_cpu_frequencies(
41+
CPUSource::PLL,
42+
80.MHz(),
43+
CPUSource::PLL,
44+
240.MHz(),
45+
CPUSource::PLL,
46+
80.MHz(),
47+
)
48+
.unwrap();
49+
50+
let (clkcntrl_config, mut watchdog) = clkcntrl.freeze().unwrap();
51+
watchdog.disable();
52+
let (_, _, _, mut watchdog0) = Timer::new(dp.TIMG0, clkcntrl_config);
53+
watchdog0.disable();
54+
let (_, _, _, mut watchdog1) = Timer::new(dp.TIMG1, clkcntrl_config);
55+
watchdog1.disable();
56+
57+
let pins = dp.GPIO.split();
58+
59+
let i2c0 = i2c::I2C::new(
60+
dp.I2C0,
61+
i2c::Pins {
62+
sda: pins.gpio4,
63+
scl: pins.gpio15,
64+
},
65+
400_000,
66+
&mut dport,
67+
);
68+
69+
let mut display: GraphicsMode<_> = Builder::new().connect_i2c(i2c0).into();
70+
71+
let mut rst = pins.gpio16.into_push_pull_output();
72+
rst.set_low().unwrap();
73+
sleep(10.ms());
74+
rst.set_high().unwrap();
75+
76+
display.init().unwrap();
77+
78+
loop {
79+
display.clear();
80+
Rectangle::new(Point::new(16, 24), Point::new(48, 40))
81+
.into_styled(
82+
PrimitiveStyleBuilder::new()
83+
.fill_color(BinaryColor::On)
84+
.build(),
85+
)
86+
.draw(&mut display)
87+
.unwrap();
88+
display.flush().unwrap();
89+
sleep(500.ms());
90+
91+
display.clear();
92+
Circle::new(Point::new(96, 32), 20)
93+
.into_styled(PrimitiveStyle::with_fill(BinaryColor::On))
94+
.draw(&mut display)
95+
.unwrap();
96+
display.flush().unwrap();
97+
sleep(500.ms());
98+
}
99+
}
100+
101+
#[panic_handler]
102+
fn panic(info: &PanicInfo) -> ! {
103+
// park the other core
104+
unsafe { ClockControlConfig {}.park_core(esp32_hal::get_other_core()) };
105+
106+
// print panic message
107+
dprintln!("\n\n*** {:?}", info);
108+
109+
// park this core
110+
unsafe { ClockControlConfig {}.park_core(esp32_hal::get_core()) };
111+
112+
dprintln!("Not reached because core is parked.");
113+
114+
// this statement will not be reached, but is needed to make this a diverging function
115+
loop {}
116+
}

0 commit comments

Comments
 (0)