Skip to content

Commit 4bfe6b3

Browse files
committed
Add example for Spi bidi transfer mode
1 parent fd4bb72 commit 4bfe6b3

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ micromath = "1.0.0"
5959
cortex-m-rtic = "0.5.6"
6060
st7789 = "0.5.0"
6161
panic-rtt-core = "0.2.1"
62+
display-interface-spi = "0.4"
63+
ist7920 = "0.1.0"
6264

6365
[features]
6466
device-selected = []
@@ -401,4 +403,8 @@ required-features = ["rt", "stm32f413", "fsmc_lcd"]
401403

402404
[[example]]
403405
name= "pwm-input"
404-
required-features = ["stm32f446"]
406+
required-features = ["stm32f446"]
407+
408+
[[example]]
409+
name = "ist7920_bidi_normal_spi"
410+
required-features = ["rt", "stm32f401"]

examples/ist7920_bidi_normal_spi.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
extern crate panic_halt;
5+
6+
use cortex_m_rt::entry;
7+
8+
use stm32f4xx_hal as hal;
9+
10+
use crate::hal::{pac, prelude::*, spi::Spi};
11+
12+
use hal::spi::{Mode, NoMiso, Phase, Polarity};
13+
14+
use display_interface_spi::SPIInterface;
15+
use ist7920::Ist7920;
16+
17+
#[entry]
18+
fn main() -> ! {
19+
let dp = pac::Peripherals::take().unwrap();
20+
let cp = cortex_m::peripheral::Peripherals::take().unwrap();
21+
22+
let gpioa = dp.GPIOA.split();
23+
let gpiob = dp.GPIOB.split();
24+
let rcc = dp.RCC.constrain();
25+
26+
let clocks = rcc.cfgr.freeze();
27+
28+
let mut led = gpioa.pa5.into_push_pull_output();
29+
led.set_low();
30+
31+
let sck = gpiob.pb3.into_alternate();
32+
let miso = NoMiso {};
33+
let mosi = gpiob.pb5.into_alternate();
34+
35+
let dc = gpiob.pb4.into_push_pull_output();
36+
let mut res = gpiob.pb10.into_push_pull_output();
37+
let cs = gpiob.pb13.into_push_pull_output();
38+
39+
let mut delay = hal::delay::Delay::new(cp.SYST, &clocks);
40+
41+
let mode = Mode {
42+
polarity: Polarity::IdleLow,
43+
phase: Phase::CaptureOnFirstTransition,
44+
};
45+
46+
// Change spi transfer mode to Bidi for more efficient operations.
47+
let spi =
48+
Spi::new(dp.SPI1, (sck, miso, mosi), mode, 8_000_000.hz(), clocks).to_bidi_transfer_mode();
49+
50+
let iface = SPIInterface::new(spi, dc, cs);
51+
52+
let mut display = Ist7920::new(iface);
53+
54+
display.reset(&mut res, &mut delay).ok();
55+
56+
display.init(&mut delay).ok();
57+
58+
let mut select_figure = 0;
59+
loop {
60+
delay.delay_ms(500_u16);
61+
let (begin, end) = match select_figure {
62+
0 => {
63+
select_figure = 1;
64+
((48, 48), (48 + 31, 48 + 31))
65+
}
66+
67+
1 => {
68+
select_figure = 2;
69+
((32, 32), (32 + 63, 32 + 63))
70+
}
71+
_ => {
72+
select_figure = 0;
73+
((24, 24), (24 + 79, 24 + 79))
74+
}
75+
};
76+
display.clear().ok();
77+
display.set_draw_area(begin, end).ok();
78+
for _ in 0..((end.1 - begin.1 + 1) / 16) {
79+
for _ in 0..((end.0 - begin.0 + 1) / 16) {
80+
display
81+
.draw(&[
82+
0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xff, 0xff, 0xff, 0xff,
83+
0xff, 0xff, 0xff, 0xff,
84+
])
85+
.ok();
86+
}
87+
for _ in 0..((end.0 - begin.0 + 1) / 16) {
88+
display
89+
.draw(&[
90+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
91+
0x00, 0x00, 0x00, 0x00,
92+
])
93+
.ok();
94+
}
95+
}
96+
led.toggle();
97+
}
98+
}

0 commit comments

Comments
 (0)