Skip to content

Commit

Permalink
Merge tag 'lcd-0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
buhe committed Oct 28, 2021
2 parents 2d81974 + 0239949 commit d2edc48
Show file tree
Hide file tree
Showing 7 changed files with 449 additions and 271 deletions.
2 changes: 1 addition & 1 deletion os/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub const USER_STACK_SIZE: usize = 4096 * 2;
pub const KERNEL_STACK_SIZE: usize = 4096 * 2;
pub const KERNEL_HEAP_SIZE: usize = 0x30_0000;
pub const KERNEL_HEAP_SIZE: usize = 0x55_0000;
pub const MEMORY_END: usize = 0x80800000;
pub const PAGE_SIZE: usize = 0x1000;
pub const PAGE_SIZE_BITS: usize = 0xc; //12 bit
Expand Down
4 changes: 2 additions & 2 deletions os/src/console.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::fmt::{self, Write};

use crate::{driver::print_lcd, scall_sbi::put_char};
use crate::{driver::print_with_lcd, scall_sbi::put_char};

struct STDOUT;

Expand All @@ -15,7 +15,7 @@ impl Write for STDOUT {

pub fn print(args: fmt::Arguments) {
// STDOUT.write_fmt(args).unwrap();
print_lcd(args);
print_with_lcd(args);
}

#[macro_export]
Expand Down
568 changes: 334 additions & 234 deletions os/src/driver/lcd/console.rs

Large diffs are not rendered by default.

97 changes: 85 additions & 12 deletions os/src/driver/lcd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,100 @@
use core::fmt::{self, Write};
use core::fmt;

use k210_pac::SPI0;
use lazy_static::*;
use k210_hal::prelude::*;
use k210_hal::pac::Peripherals;
use k210_soc::{dmac::DMACExt, fpioa::{self, io}, sleep::usleep, spi::{SPIExt, SPIImpl}, sysctl::{self, dma_channel}};
use spin::Mutex;

use crate::driver::lcd::console::Console;
use self::{console::{Color, Console, DISP_HEIGHT, DISP_PIXELS, DISP_WIDTH, ScreenImage}, st7789v::{LCD, LCDHL}};

mod st7789v;

mod color;
mod coord;
mod palette_xterm256;
mod lcd_colors;
mod color;
mod cp437;
mod cp437_8x8;
mod lcd_colors;
mod palette_xterm256;
// 用 LCD 输出
pub mod console;
mod console;
static mut image: ScreenImage = [0; DISP_PIXELS / 2];
pub fn init() {

DRIVER.lock();
}

use alloc::sync::Arc;
use spin::Mutex;

lazy_static! {
pub static ref LCD_DRIVER: Arc<Mutex<Console>> = Arc::new(Mutex::new(Console::init()));
/** Connect pins to internal functions */
fn io_mux_init() {
/* Init SPI IO map and function settings */
fpioa::set_function(io::LCD_RST, fpioa::function::gpiohs(st7789v::RST_GPIONUM));
fpioa::set_io_pull(io::LCD_RST, fpioa::pull::DOWN); // outputs must be pull-down
fpioa::set_function(io::LCD_DC, fpioa::function::gpiohs(st7789v::DCX_GPIONUM));
fpioa::set_io_pull(io::LCD_DC, fpioa::pull::DOWN);
fpioa::set_function(io::LCD_CS, fpioa::function::SPI0_SS3);
fpioa::set_function(io::LCD_WR, fpioa::function::SPI0_SCLK);

sysctl::set_spi0_dvp_data(true);
}

/** Set correct voltage for pins */
fn io_set_power() {
/* Set dvp and spi pin to 1.8V */
sysctl::set_power_mode(sysctl::power_bank::BANK6, sysctl::io_power_mode::V18);
sysctl::set_power_mode(sysctl::power_bank::BANK7, sysctl::io_power_mode::V18);
}

lazy_static!{
pub static ref CONSOLE: Mutex<Console> = {
let mut console: Console =
Console::new(&cp437_8x8::FONT, None);

/* Make a border */
let fg = Color::new(0x80, 0x40, 0x40);
let bg = Color::new(0x00, 0x00, 0x00);
// Sides
for x in 1..console.width() - 1 {
console.put(x, 0, fg, bg, '─');
console.put(x, console.height() - 1, fg, bg, '─');
}
for y in 1..console.height() - 1 {
console.put(0, y, fg, bg, '│');
console.put(console.width() - 1, y, fg, bg, '│');
}

Mutex::new(console)
};
pub static ref DRIVER: Mutex<LCD<SPIImpl<SPI0>>> = {
let p = Peripherals::take().unwrap();
sysctl::pll_set_freq(sysctl::pll::PLL0, 800_000_000).unwrap();
sysctl::pll_set_freq(sysctl::pll::PLL1, 300_000_000).unwrap();
sysctl::pll_set_freq(sysctl::pll::PLL2, 45_158_400).unwrap();
// Configure clocks (TODO)
// let clocks = k210_hal::clock::Clocks::new();
// sleep a bit to let clients connect
usleep(200000);

io_mux_init();
io_set_power();

/* LCD init */
let dmac = p.DMAC.configure();
let spi = p.SPI0.constrain();
let mut lcd = LCD::new(spi, dmac, dma_channel::CHANNEL0);
lcd.init();
lcd.set_direction(st7789v::direction::YX_LRUD);
lcd.clear(lcd_colors::BLUE);
Mutex::new(lcd)
};
}

pub fn print_lcd(args: fmt::Arguments) {
LCD_DRIVER.lock().write_fmt(args).unwrap();
pub fn print_with_lcd(args: fmt::Arguments){

CONSOLE.lock().write_fmt(args).unwrap();
unsafe{
CONSOLE.lock().render(&mut image);// render 会导致不执行 task
DRIVER.lock().flush(&image);
}
}
36 changes: 20 additions & 16 deletions os/src/driver/lcd/st7789v.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//! ST7789V LCD driver
use k210_soc::dmac::{dma_channel, DMAC};
use k210_soc::gpio;
use k210_soc::gpiohs;
use k210_soc::sleep::usleep;
use k210_soc::spi::{aitm, frame_format, tmod, work_mode, SPI};
use k210_soc::spi::{SPI,work_mode,frame_format,aitm,tmod};
use k210_soc::dmac::{DMAC,dma_channel};

use super::console::DISP_HEIGHT;
use super::console::DISP_WIDTH;
use super::console::ScreenImage;

// These are the values used in the Kendryte SDK but should not ideally be hardcoded here, but
// defined in the main.rs and passed to the constructor
Expand Down Expand Up @@ -124,12 +128,12 @@ pub enum direction {
pub const DIR_XY_MASK: u8 = 0x20;
pub const DIR_MASK: u8 = 0xE0;

pub struct LCD<'a, SPI> {
pub struct LCD<SPI> {
spi: SPI,
spi_cs: u32,
dcx_gpionum: u8,
rst_gpionum: u8,
dmac: &'a DMAC,
dmac: DMAC,
channel: dma_channel,
pub width: u16,
pub height: u16,
Expand Down Expand Up @@ -161,8 +165,8 @@ pub trait LCDHL {
fn shutdown(&mut self);
}

impl<'a, X: SPI> LCD<'a, X> {
pub fn new(spi: X, dmac: &'a DMAC, channel: dma_channel) -> Self {
impl<X: SPI> LCD<X> {
pub fn new(spi: X, dmac: DMAC, channel: dma_channel) -> Self {
Self {
spi,
spi_cs: SPI_CS,
Expand All @@ -175,6 +179,10 @@ impl<'a, X: SPI> LCD<'a, X> {
}
}

pub fn flush(&self, image: &ScreenImage){
self.draw_picture(0, 0, DISP_WIDTH, DISP_HEIGHT, image);
}

fn init_dcx(&self) {
gpiohs::set_direction(self.dcx_gpionum, gpio::direction::OUTPUT);
gpiohs::set_pin(self.dcx_gpionum, true);
Expand Down Expand Up @@ -219,7 +227,7 @@ impl<'a, X: SPI> LCD<'a, X> {
}

/** Low-level functions */
impl<X: SPI> LCDLL for LCD<'_, X> {
impl<X: SPI> LCDLL for LCD<X> {
fn hard_init(&self) {
self.init_dcx();
self.init_rst();
Expand Down Expand Up @@ -252,8 +260,7 @@ impl<X: SPI> LCDLL for LCD<'_, X> {
aitm::AS_FRAME_FORMAT,
tmod::TRANS,
);
self.spi
.send_data_dma(self.dmac, self.channel, self.spi_cs, &[cmd as u32]);
self.spi.send_data_dma(&self.dmac, self.channel, self.spi_cs, &[cmd as u32]);
}

fn write_byte(&self, data_buf: &[u32]) {
Expand All @@ -269,8 +276,7 @@ impl<X: SPI> LCDLL for LCD<'_, X> {
aitm::AS_FRAME_FORMAT,
tmod::TRANS,
);
self.spi
.send_data_dma(self.dmac, self.channel, self.spi_cs, data_buf);
self.spi.send_data_dma(&self.dmac, self.channel, self.spi_cs, data_buf);
}

fn write_word(&self, data_buf: &[u32]) {
Expand All @@ -286,8 +292,7 @@ impl<X: SPI> LCDLL for LCD<'_, X> {
aitm::AS_FRAME_FORMAT,
tmod::TRANS,
);
self.spi
.send_data_dma(self.dmac, self.channel, self.spi_cs, data_buf);
self.spi.send_data_dma(&self.dmac, self.channel, self.spi_cs, data_buf);
}

fn fill_data(&self, data: u32, length: usize) {
Expand All @@ -303,13 +308,12 @@ impl<X: SPI> LCDLL for LCD<'_, X> {
aitm::AS_FRAME_FORMAT,
tmod::TRANS,
);
self.spi
.fill_data_dma(self.dmac, self.channel, self.spi_cs, data, length);
self.spi.fill_data_dma(&self.dmac, self.channel, self.spi_cs, data, length);
}
}

/* High-level functions */
impl<X: SPI> LCDHL for LCD<'_, X> {
impl<X: SPI> LCDHL for LCD<X> {
fn init(&mut self) {
self.hard_init();
/*soft reset*/
Expand Down
4 changes: 2 additions & 2 deletions os/src/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ mod gpio;
mod lcd;
pub fn init() {
gpio::init();
lcd::init();
}

pub use lcd::print_lcd;
pub use lcd::print_with_lcd;
9 changes: 5 additions & 4 deletions os/src/heap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ mod bump;
// use buddy_system_allocator::LockedHeap;

use crate::config::KERNEL_HEAP_SIZE;
use buddy_system_allocator::LockedHeap;
use bump::{BumpAllocator, Locked};

#[global_allocator]
static HEAP_ALLOCATOR: Locked<BumpAllocator> = Locked::new(BumpAllocator::new());

// #[global_allocator]
// static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
// static HEAP_ALLOCATOR: Locked<BumpAllocator> = Locked::new(BumpAllocator::new());

#[global_allocator]
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();

static mut HEAP_SPACE: [u8; KERNEL_HEAP_SIZE] = [0; KERNEL_HEAP_SIZE];

Expand Down

0 comments on commit d2edc48

Please sign in to comment.