2020-10-11 19:56:10 +00:00
|
|
|
use utralib::generated::*;
|
2021-01-03 10:31:51 +00:00
|
|
|
use xous::MemoryRange;
|
2020-08-17 11:43:00 +00:00
|
|
|
|
|
|
|
|
const FB_WIDTH_WORDS: usize = 11;
|
|
|
|
|
const FB_WIDTH_PIXELS: usize = 336;
|
|
|
|
|
const FB_LINES: usize = 536;
|
|
|
|
|
const FB_SIZE: usize = FB_WIDTH_WORDS * FB_LINES; // 44 bytes by 536 lines
|
|
|
|
|
const CONFIG_CLOCK_FREQUENCY: u32 = 100_000_000;
|
|
|
|
|
|
|
|
|
|
const COMMAND_OFFSET: usize = 0;
|
|
|
|
|
const BUSY_OFFSET: usize = 1;
|
|
|
|
|
const PRESCALER_OFFSET: usize = 2;
|
|
|
|
|
|
|
|
|
|
pub struct XousDisplay {
|
2020-08-18 04:07:41 +00:00
|
|
|
fb: MemoryRange,
|
|
|
|
|
control: MemoryRange,
|
2020-08-17 11:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl XousDisplay {
|
|
|
|
|
pub fn new() -> XousDisplay {
|
2020-08-18 04:07:41 +00:00
|
|
|
let fb = xous::syscall::map_memory(
|
2020-10-11 19:56:10 +00:00
|
|
|
xous::MemoryAddress::new(HW_MEMLCD_MEM),
|
2020-08-18 04:07:41 +00:00
|
|
|
None,
|
|
|
|
|
((FB_WIDTH_WORDS * FB_LINES * 4) + 4096) & !4095,
|
|
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
)
|
2020-10-11 19:23:15 +00:00
|
|
|
.expect("couldn't map frame buffer");
|
2020-10-30 02:21:06 +00:00
|
|
|
for mem_offset in 0..(FB_SIZE / 4) {
|
|
|
|
|
unsafe { fb.as_mut_ptr().add(mem_offset).write_volatile(0) };
|
|
|
|
|
}
|
2020-08-18 04:07:41 +00:00
|
|
|
|
|
|
|
|
let control = xous::syscall::map_memory(
|
2020-10-11 19:56:10 +00:00
|
|
|
xous::MemoryAddress::new(HW_MEMLCD_BASE),
|
2020-08-18 04:07:41 +00:00
|
|
|
None,
|
|
|
|
|
4096,
|
|
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't map control port");
|
|
|
|
|
let mut display = XousDisplay { fb, control };
|
|
|
|
|
|
2020-08-17 11:43:00 +00:00
|
|
|
display.set_clock(CONFIG_CLOCK_FREQUENCY);
|
|
|
|
|
display.sync_clear();
|
2020-08-18 04:07:41 +00:00
|
|
|
|
2020-08-17 11:43:00 +00:00
|
|
|
display
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn redraw(&mut self) {
|
|
|
|
|
while self.busy() {}
|
|
|
|
|
self.update_dirty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn update(&mut self) {}
|
|
|
|
|
|
2020-10-30 02:21:06 +00:00
|
|
|
pub fn native_buffer(&mut self) -> &mut [u32; FB_SIZE] {
|
|
|
|
|
unsafe { &mut *(self.fb.as_mut_ptr() as *mut [u32; FB_SIZE]) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn blit_screen(&mut self, bmp: [u32; FB_SIZE]) {
|
2020-10-11 19:23:15 +00:00
|
|
|
let framebuffer = self.fb.as_mut_ptr() as *mut u32;
|
|
|
|
|
|
|
|
|
|
for words in 0..FB_SIZE {
|
|
|
|
|
unsafe {
|
|
|
|
|
framebuffer.add(words).write_volatile(bmp[words]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.update_all();
|
|
|
|
|
|
|
|
|
|
while self.busy() {}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 11:43:00 +00:00
|
|
|
/// Beneath this line are pure-HAL layer, and should not be user-visible
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
|
fn set_clock(&mut self, clk_mhz: u32) {
|
|
|
|
|
unsafe {
|
2020-08-18 04:07:41 +00:00
|
|
|
(self.control.as_ptr() as *mut u32)
|
2020-08-17 11:43:00 +00:00
|
|
|
.add(PRESCALER_OFFSET)
|
|
|
|
|
.write_volatile((clk_mhz / 2_000_000) - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update_all(&mut self) {
|
2020-08-18 04:07:41 +00:00
|
|
|
unsafe {
|
|
|
|
|
(self.control.as_ptr() as *mut u32)
|
|
|
|
|
.add(COMMAND_OFFSET)
|
|
|
|
|
.write_volatile(2)
|
|
|
|
|
};
|
2020-08-17 11:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update_dirty(&mut self) {
|
2020-08-18 04:07:41 +00:00
|
|
|
unsafe {
|
|
|
|
|
(self.control.as_ptr() as *mut u32)
|
|
|
|
|
.add(COMMAND_OFFSET)
|
|
|
|
|
.write_volatile(1)
|
|
|
|
|
};
|
2020-08-17 11:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// "synchronous clear" -- must be called on init, so that the state of the LCD
|
|
|
|
|
/// internal memory is consistent with the state of the frame buffer
|
|
|
|
|
fn sync_clear(&mut self) {
|
2020-08-18 04:07:41 +00:00
|
|
|
let framebuffer = self.fb.as_mut_ptr() as *mut u32;
|
2020-08-17 11:43:00 +00:00
|
|
|
for words in 0..FB_SIZE {
|
|
|
|
|
if words % FB_WIDTH_WORDS != 10 {
|
2020-08-18 04:07:41 +00:00
|
|
|
unsafe { framebuffer.add(words).write_volatile(0xFFFF_FFFF) };
|
2020-08-17 11:43:00 +00:00
|
|
|
} else {
|
2020-08-18 04:07:41 +00:00
|
|
|
unsafe { framebuffer.add(words).write_volatile(0x0000_FFFF) };
|
2020-08-17 11:43:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.update_all(); // because we force an all update here
|
|
|
|
|
while self.busy() {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn busy(&self) -> bool {
|
2020-08-18 04:07:41 +00:00
|
|
|
unsafe {
|
|
|
|
|
(self.control.as_ptr() as *mut u32)
|
|
|
|
|
.add(BUSY_OFFSET)
|
|
|
|
|
.read_volatile()
|
|
|
|
|
== 1
|
|
|
|
|
}
|
2020-08-17 11:43:00 +00:00
|
|
|
}
|
|
|
|
|
}
|