Skip to content

Commit 58ecffa

Browse files
pbrinkmeieroli-obk
authored andcommitted
Add example for printing a whole line
1 parent 2425f9e commit 58ecffa

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

examples/lcd.rs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#![feature(alloc)]
2+
#![feature(alloc_error_handler)]
3+
#![no_main]
4+
#![no_std]
5+
6+
#[macro_use]
7+
extern crate alloc;
8+
9+
use alloc::string::String;
10+
use alloc_cortex_m::CortexMHeap;
11+
use core::alloc::Layout as AllocLayout;
12+
use core::fmt::Write;
13+
use core::panic::PanicInfo;
14+
use cortex_m::{asm, interrupt, peripheral::NVIC};
15+
use cortex_m_rt::{entry, exception, ExceptionFrame};
16+
use cortex_m_semihosting::hio::{self, HStdout};
17+
use stm32f7::{
18+
interrupt,
19+
stm32f7x6::{CorePeripherals, Interrupt, Peripherals}
20+
};
21+
use stm32f7_discovery::{
22+
print, println,
23+
gpio::{GpioPort, InputPin, OutputPin},
24+
init,
25+
lcd,
26+
};
27+
28+
#[global_allocator]
29+
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
30+
31+
const HEAP_SIZE: usize = 50 * 1024; // in bytes
32+
33+
#[entry]
34+
fn main() -> ! {
35+
let core_peripherals = CorePeripherals::take().unwrap();
36+
let mut nvic = core_peripherals.NVIC;
37+
38+
let peripherals = Peripherals::take().unwrap();
39+
let mut rcc = peripherals.RCC;
40+
let mut pwr = peripherals.PWR;
41+
let mut flash = peripherals.FLASH;
42+
let mut fmc = peripherals.FMC;
43+
let mut ltdc = peripherals.LTDC;
44+
45+
init::init_system_clock_216mhz(&mut rcc, &mut pwr, &mut flash);
46+
init::enable_gpio_ports(&mut rcc);
47+
48+
let gpio_a = GpioPort::new(peripherals.GPIOA);
49+
let gpio_b = GpioPort::new(peripherals.GPIOB);
50+
let gpio_c = GpioPort::new(peripherals.GPIOC);
51+
let gpio_d = GpioPort::new(peripherals.GPIOD);
52+
let gpio_e = GpioPort::new(peripherals.GPIOE);
53+
let gpio_f = GpioPort::new(peripherals.GPIOF);
54+
let gpio_g = GpioPort::new(peripherals.GPIOG);
55+
let gpio_h = GpioPort::new(peripherals.GPIOH);
56+
let gpio_i = GpioPort::new(peripherals.GPIOI);
57+
let gpio_j = GpioPort::new(peripherals.GPIOJ);
58+
let gpio_k = GpioPort::new(peripherals.GPIOK);
59+
let mut pins = init::pins(
60+
gpio_a, gpio_b, gpio_c, gpio_d, gpio_e, gpio_f, gpio_g, gpio_h, gpio_i, gpio_j, gpio_k,
61+
);
62+
63+
init::init_sdram(&mut rcc, &mut fmc);
64+
let mut lcd = init::init_lcd(&mut ltdc, &mut rcc);
65+
pins.display_enable.set(true);
66+
pins.backlight.set(true);
67+
68+
let mut layer_1 = lcd.layer_1().unwrap();
69+
let mut layer_2 = lcd.layer_2().unwrap();
70+
71+
layer_1.clear();
72+
layer_2.clear();
73+
lcd::init_stdout(layer_2);
74+
75+
println!("Try pressing the blue button one the left side!");
76+
77+
// Initialize the allocator BEFORE you use it
78+
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) }
79+
80+
nvic.enable(Interrupt::EXTI0);
81+
82+
let mut previous_button_state = pins.button.get();
83+
loop {
84+
// poll button state
85+
let current_button_state = pins.button.get();
86+
if current_button_state != previous_button_state {
87+
if current_button_state {
88+
pins.led.toggle();
89+
90+
// trigger the `EXTI0` interrupt
91+
NVIC::pend(Interrupt::EXTI0);
92+
}
93+
94+
previous_button_state = current_button_state;
95+
}
96+
}
97+
}
98+
99+
interrupt!(EXTI0, exti0, state: Option<HStdout> = None);
100+
101+
fn exti0(_state: &mut Option<HStdout>) {
102+
println!("Interrupt fired! This means that the button was pressed.");
103+
println!("{}", String::from_utf8(vec![b'-'; 60]).unwrap());
104+
}
105+
106+
#[exception]
107+
fn HardFault(ef: &ExceptionFrame) -> ! {
108+
panic!("HardFault at {:#?}", ef);
109+
}
110+
111+
// define what happens in an Out Of Memory (OOM) condition
112+
#[alloc_error_handler]
113+
fn rust_oom(_: AllocLayout) -> ! {
114+
panic!("out of memory");
115+
}
116+
117+
#[panic_handler]
118+
fn panic(info: &PanicInfo) -> ! {
119+
interrupt::disable();
120+
121+
if lcd::stdout::is_initialized() {
122+
println!("{}", info);
123+
}
124+
125+
if let Ok(mut hstdout) = hio::hstdout() {
126+
let _ = writeln!(hstdout, "{}", info);
127+
}
128+
129+
// OK to fire a breakpoint here because we know the microcontroller is connected to a debugger
130+
asm::bkpt();
131+
132+
loop {}
133+
}

0 commit comments

Comments
 (0)