forked from stm32-rs/stm32h7xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedded-graphics.rs
423 lines (359 loc) · 12 KB
/
embedded-graphics.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! This example uses the embedded-graphics library to draw text and an image on
//! an external display. The external display is connected through the LTDC
//! peripheral.
//!
//! Tested on a STM32H735G-DK development board with a ST MB1315 Display
//! (supplied together with the development kit)
#![deny(warnings)]
#![no_main]
#![no_std]
use core::{mem, slice};
#[macro_use]
mod utilities;
use log::info;
extern crate cortex_m;
extern crate cortex_m_rt as rt;
use core::sync::atomic::{AtomicU32, Ordering};
use cortex_m_rt::{entry, exception};
use stm32h7xx_hal::gpio::Speed;
use stm32h7xx_hal::rcc::CoreClocks;
use stm32h7xx_hal::{ltdc, xspi};
use stm32h7xx_hal::{prelude::*, stm32};
use embedded_display_controller::DisplayController;
use embedded_graphics::image::Image;
use embedded_graphics::mono_font::{ascii, MonoTextStyle};
use embedded_graphics::prelude::*;
use embedded_graphics::text::Text;
use numtoa::NumToA;
use tinybmp::Bmp;
const WIDTH: usize = 480;
const HEIGHT: usize = 272;
/// Configure SYSTICK for 1ms timebase
fn systick_init(syst: &mut stm32::SYST, clocks: CoreClocks) {
let c_ck_mhz = clocks.c_ck().to_MHz();
let syst_calib = 0x3E8;
syst.set_clock_source(cortex_m::peripheral::syst::SystClkSource::Core);
syst.set_reload((syst_calib * c_ck_mhz) - 1);
syst.enable_interrupt();
syst.enable_counter();
}
/// TIME is an atomic u32 that counts milliseconds. Although not used
/// here, it is very useful to have for network protocols
static TIME: AtomicU32 = AtomicU32::new(0);
/// Configure a pin for the FMC controller
macro_rules! pins_alternate_high_speed {
($($func:ident: $port:ident.$pin:ident: $af:expr);*) => {
(
$(
$port.$pin.into_alternate::<$af>()
.speed(Speed::High)
.internal_pull_up(true)
),*
)
};
}
// the program entry point
#[entry]
fn main() -> ! {
utilities::logger::init();
let dp = stm32::Peripherals::take().unwrap();
let mut cp = stm32::CorePeripherals::take().unwrap();
// Constrain and Freeze power
info!("Setup PWR... ");
let pwr = dp.PWR.constrain();
let pwrcfg = example_power!(pwr).freeze();
// Constrain and Freeze clock
info!("Setup RCC... ");
let rcc = dp.RCC.constrain();
let ccdr = rcc
.sys_ck(400.MHz())
// Octo SPI
.pll2_p_ck(400.MHz() / 5)
.pll2_q_ck(400.MHz() / 2)
.pll2_r_ck(400.MHz() / 2)
// LTDC
.pll3_p_ck(800.MHz() / 2)
.pll3_q_ck(800.MHz() / 2)
.pll3_r_ck(800.MHz() / 83)
.freeze(pwrcfg, &dp.SYSCFG);
// Get frequency of LTDC pixel clock
let pll3_r = ccdr.clocks.pll3_r_ck().expect("pll3 must run!");
// Get the delay provider.
let mut delay = cp.SYST.delay(ccdr.clocks);
// Initialise system...
cp.SCB.invalidate_icache();
cp.SCB.enable_icache();
//cp.SCB.enable_dcache(&mut cp.CPUID); // TODO invalidate dcache when writing to the display
cp.DWT.enable_cycle_counter();
// Initialise IO...
let gpioa = dp.GPIOA.split(ccdr.peripheral.GPIOA);
let gpiob = dp.GPIOB.split(ccdr.peripheral.GPIOB);
let gpioc = dp.GPIOC.split(ccdr.peripheral.GPIOC);
let gpiod = dp.GPIOD.split(ccdr.peripheral.GPIOD);
let gpioe = dp.GPIOE.split(ccdr.peripheral.GPIOE);
let gpiof = dp.GPIOF.split(ccdr.peripheral.GPIOF);
let gpiog = dp.GPIOG.split(ccdr.peripheral.GPIOG);
let gpioh = dp.GPIOH.split(ccdr.peripheral.GPIOH);
let _tracweswo = gpiob.pb3.into_alternate::<0>();
// pin setup for HyperRAM
let _ = pins_alternate_high_speed! {
_ncs: gpiog.pg12: 3;
_dqs: gpiof.pf12: 9;
_clk: gpiof.pf4: 9;
_io0: gpiof.pf0: 9;
_io1: gpiof.pf1: 9;
_io2: gpiof.pf2: 9;
_io3: gpiof.pf3: 9;
_io4: gpiog.pg0: 9;
_io5: gpiog.pg1: 9;
_io6: gpiog.pg10: 3;
_io7: gpiog.pg11: 9
};
let hyperram_size = 16 * 1024 * 1024; // 16 MByte
let config = xspi::HyperbusConfig::new(40.MHz())
.device_size_bytes(24) // 16 Mbyte
.refresh_interval(4.micros())
.read_write_recovery(4) // 50ns
.access_initial_latency(6);
let hyperram = dp.OCTOSPI2.octospi_hyperbus_unchecked(
config,
&ccdr.clocks,
ccdr.peripheral.OCTOSPI2,
);
let (fb1, fb2) = unsafe {
// Initialise controller and SDRAM
let ram_ptr: *mut u32 = hyperram.init();
let ram_slice = slice::from_raw_parts_mut(
ram_ptr,
hyperram_size / mem::size_of::<u32>(),
);
let fb_size = WIDTH * HEIGHT;
let mut lcd_framebuffers = ram_slice.chunks_exact_mut(fb_size);
let fb1 = lcd_framebuffers.next().unwrap();
let fb2 = lcd_framebuffers.next().unwrap();
(fb1, fb2)
};
info!("Initialised HyperRAM...");
// ----------------------------------------------------------
// Initialise LCD...
// setup LTDC (LTDC_MspInit)
let _ = pins_alternate_high_speed! {
_i: gpioa.pa3: 14;
_i: gpioa.pa4: 14;
_i: gpioa.pa6: 14;
_i: gpiob.pb0: 14;
_i: gpiob.pb1: 14;
_i: gpiob.pb8: 14;
_i: gpiob.pb9: 14;
_i: gpioc.pc6: 14;
_i: gpioc.pc7: 14;
_i: gpiod.pd0: 14;
_i: gpiod.pd3: 14;
_i: gpiod.pd6: 14;
_i: gpioe.pe0: 14;
_i: gpioe.pe1: 14;
_i: gpioe.pe11: 14;
_i: gpioe.pe12: 14;
_i: gpioe.pe15: 14;
_i: gpiog.pg7: 14;
_i: gpiog.pg14: 14;
_i: gpioh.ph3: 14;
_i: gpioh.ph8: 14;
_i: gpioh.ph9: 14;
_i: gpioh.ph10: 14;
_i: gpioh.ph11: 14;
_i: gpioh.ph15: 14;
_i: gpioa.pa8: 13;
_i: gpioh.ph4: 9
};
let mut lcd_disp_en = gpioe.pe13.into_push_pull_output();
let mut lcd_disp_ctrl = gpiod.pd10.into_push_pull_output();
let mut lcd_bl_ctrl = gpiog.pg15.into_push_pull_output();
delay.delay_ms(40u8);
let mut ltdc = ltdc::Ltdc::new(dp.LTDC, ccdr.peripheral.LTDC, &ccdr.clocks);
const RK043FN48H_HSYNC: u16 = 41; // Horizontal synchronization
const RK043FN48H_HBP: u16 = 13; // Horizontal back porch
const RK043FN48H_HFP: u16 = 32; // Horizontal front porch
const RK043FN48H_VSYNC: u16 = 10; // Vertical synchronization
const RK043FN48H_VBP: u16 = 2; // Vertical back porch
const RK043FN48H_VFP: u16 = 2; // Vertical front porch
ltdc.init(embedded_display_controller::DisplayConfiguration {
active_width: WIDTH as _,
active_height: HEIGHT as _,
h_back_porch: RK043FN48H_HBP - 11, // -11 from MX_LTDC_Init
h_front_porch: RK043FN48H_HFP,
v_back_porch: RK043FN48H_VBP,
v_front_porch: RK043FN48H_VFP,
h_sync: RK043FN48H_HSYNC,
v_sync: RK043FN48H_VSYNC,
h_sync_pol: false,
v_sync_pol: false,
not_data_enable_pol: false,
pixel_clock_pol: false,
});
let layer = ltdc.split();
let mut disp = BufferedDisplay::new(layer, fb1, fb2);
lcd_disp_en.set_low();
lcd_disp_ctrl.set_high();
lcd_bl_ctrl.set_high();
info!("Initialised Display...");
// ----------------------------------------------------------
// Begin periodic tasks
systick_init(&mut delay.free(), ccdr.clocks);
unsafe {
cp.SCB.shpr[15 - 4].write(128);
} // systick exception priority
// ----------------------------------------------------------
// Main application loop
loop {
// Draw on a double buffered display
disp.layer(|draw| {
draw.clear();
// Text
let text_style =
MonoTextStyle::new(&ascii::FONT_9X18, RgbColor::WHITE);
Text::new("Hello Rust!", Point::new(100, 100), text_style)
.draw(draw)
.unwrap();
// Text variable
let mut buffer = [0u8; 20];
Text::new(
pll3_r.raw().numtoa_str(10, &mut buffer),
Point::new(100, 140),
text_style,
)
.draw(draw)
.unwrap();
// Image
let ferris =
Bmp::from_slice(include_bytes!("./ferris.bmp")).unwrap();
let ferris = Image::new(&ferris, Point::new(106, 20));
ferris.draw(draw).unwrap();
});
disp.swap_layer_wait();
}
}
/// A display with swappable framebuffers
pub struct BufferedDisplay<'a, LY> {
layer: LY,
front_buffer: &'a mut [u32],
back_buffer: &'a mut [u32],
}
/// An individual display layer, borrowing from `BufferedDisplay`
pub struct DisplayBuffer<'a, 'p>(
/// Underlying buffer
pub &'p mut &'a mut [u32],
);
impl<'a, LY> BufferedDisplay<'a, LY>
where
LY: embedded_display_controller::DisplayControllerLayer,
{
pub fn new(
mut layer: LY,
front_buffer: &'a mut [u32],
back_buffer: &'a mut [u32],
) -> Self {
// Safety: the frame buffer has the right size
unsafe {
layer.enable(
front_buffer.as_ptr() as *const u8,
embedded_display_controller::PixelFormat::ARGB8888,
);
}
BufferedDisplay {
layer,
front_buffer,
back_buffer,
}
}
/// Swaps frame buffers
///
/// # Safety
///
/// Does not wait for the swap to actually occur, and hence layer accesses
/// after this may write to the wrong layer. For a safe version, use
/// swap_layer_wait
pub unsafe fn swap_layer(&mut self) {
// Have been filling back buffer
self.layer.swap_framebuffer(self.back_buffer.as_ptr());
// Swap the back buffer to the front and visa versa
mem::swap(&mut self.back_buffer, &mut self.front_buffer);
}
/// Swaps frame buffers then waits for the swap to occour on the next
/// vertical blanking period
pub fn swap_layer_wait(&mut self) {
// unsafe: we wait for the swap to occour, so current
// displayed buffer is protected
unsafe { self.swap_layer() };
while self.layer.is_swap_pending() {}
}
/// Access to layer via closure
pub fn layer<F, T>(&mut self, func: F) -> T
where
F: FnOnce(&mut DisplayBuffer) -> T,
{
// Create a layer that lives until the end of this call
let mut layer = DisplayBuffer(&mut self.back_buffer);
func(&mut layer)
}
}
use embedded_graphics::{
geometry,
pixelcolor::raw::{RawData, RawU24},
pixelcolor::Rgb888,
Pixel,
};
// Implement DrawTarget for
impl embedded_graphics::draw_target::DrawTarget for DisplayBuffer<'_, '_> {
type Color = Rgb888;
type Error = ();
/// Draw a pixel
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = Pixel<Self::Color>>,
{
for pixel in pixels {
let Pixel(point, color) = pixel;
let raw: RawU24 = color.into();
let rgb: u32 = 0xFF00_0000u32 | raw.into_inner();
if point.x >= 0
&& point.y >= 0
&& point.x < (WIDTH as i32)
&& point.y < (HEIGHT as i32)
{
let index = (point.y * (WIDTH as i32)) + point.x;
self.0[index as usize] = rgb;
} else {
// Ignore invalid points
}
}
Ok(())
}
}
impl geometry::OriginDimensions for DisplayBuffer<'_, '_> {
/// Return the size of the display
fn size(&self) -> geometry::Size {
geometry::Size::new(HEIGHT as u32, WIDTH as u32)
}
}
impl DisplayBuffer<'_, '_> {
/// Clears the buffer
pub fn clear(&mut self) {
let pixels = WIDTH * HEIGHT;
for a in self.0[..pixels as usize].iter_mut() {
*a = 0xFF00_0000u32; // Solid black
}
}
}
#[exception]
fn SysTick() {
TIME.fetch_add(1, Ordering::Relaxed);
}
#[exception]
unsafe fn HardFault(ef: &cortex_m_rt::ExceptionFrame) -> ! {
panic!("HardFault at {:#?}", ef);
}
#[exception]
unsafe fn DefaultHandler(irqn: i16) {
panic!("Unhandled exception (IRQn = {})", irqn);
}