From 307b3d6e72fabcb0d77a67fa9e3e6c2805a5db79 Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Fri, 7 Sep 2018 01:31:12 +0200 Subject: [PATCH] Added missing primitives --- back.txt | 136 ---------------------------------------------- src/st7735/mod.rs | 40 ++++++++++++++ 2 files changed, 40 insertions(+), 136 deletions(-) delete mode 100644 back.txt diff --git a/back.txt b/back.txt deleted file mode 100644 index 6e85fc1..0000000 --- a/back.txt +++ /dev/null @@ -1,136 +0,0 @@ -#![no_std] -#![feature(asm, used, const_fn, naked_functions, alloc, box_syntax)] -#![no_main] -#![feature(extern_prelude)] - -#[macro_use] -extern crate hcl; - -#[macro_use] -extern crate alloc; - -use core::mem; -use hcl::platform::gpio; -use hcl::platform::irq; -use hcl::platform::rcc; -use hcl::platform::scs; -use hcl::platform::timer; -use hcl::platform::usart; -use hcl::platform::dma; -use hcl::dma::*; - -mod printer; - -use printer::UsartPrinter; - -fn configure_clocks(rcc: &mut rcc::RCC) { - rcc.clock_control.set_hse_on(true); - while !rcc.clock_control.hse_ready() { - } - - rcc.clock_config.configure(|c| c - .set_pll_multiplier(10) - .set_pll_source(rcc::PllSource::HsiDiv2)); - - rcc.clock_control.set_pll_on(true); - while !rcc.clock_control.pll_ready() { - } - - rcc.clock_config.switch_clock_source(rcc::SystemClockSource::Pll); -} - -fn configure_peripherals(rcc: &mut hcl::platform::rcc::RCC, - gpio: &mut gpio::GPIO, - usart: &mut usart::USART) { - rcc.apb2_enable.configure(|a| a - .set_gpio_a(true) - .set_spi1(true)); - rcc.apb1_enable.configure(|a| a - .set_usart2(true)); - - gpio.configure(|g| g - .set_mode(2, gpio::PinMode::Output50MHz) - .set_output_config(2, gpio::OutputConfig::AfPushPull) - .set_mode(3, gpio::PinMode::Output50MHz) - .set_output_config(3, gpio::OutputConfig::AfPushPull) - // SCK1 - .set_mode(5, gpio::PinMode::Output50MHz) - .set_output_config(5, gpio::OutputConfig::AfPushPull) - // MOSI1 - .set_mode(7, gpio::PinMode::Output50MHz) - .set_output_config(7, gpio::OutputConfig::AfPushPull) - // MISO1 - .set_mode(6, gpio::PinMode::Input) - .set_input_config(6, gpio::InputConfig::PullUpDown) - // NSS1 - .set_mode(4, gpio::PinMode::Output50MHz) - .set_output_config(4, gpio::OutputConfig::PushPull)); - - usart.configure(|u| u - .set_enabled(true) - .set_tx_enabled(true) - .set_baudgen((21, 11))); // 115.2 kbaud -} - - - -// allowing inlining into main() breaks the stack, since main() must be naked to set up a process stack. -#[inline(never)] -fn run(mut scs: scs::Instance, mut p: hcl::platform::Instance) { - configure_clocks(&mut p.rcc); - configure_peripherals(&mut p.rcc, &mut p.gpio_a, &mut p.usart2); - - let mut printer = UsartPrinter::init(p.usart2); - - let mut spi = p.spi1; - spi.configure(|s| s - .set_enabled(true) - .set_master_mode(true) - .set_slave_select_output_enabled(true) - .set_bidi_tx(false) - .set_clock_divider(128) - .set_data_16bit(true) - .set_clock_idles_high(false) - .set_lsb_first(false) - .set_clock_skip_first(true)); - - loop { - - p.gpio_a.reset_bit(4); - spi.set_data(0xFFFF); - - while !spi.rx_buffer_not_empty() { - } - - let i = spi.data() >> 3; - let temp = (i as f32) * 0.25; - - while spi.busy() { - } - p.gpio_a.set_bit(4); - - - - let x = format!("> {}\r\n", temp).into_bytes(); - - printer.print(x); - - for y in 0u32..0xFFFFFF { - unsafe { asm!("nop" :::: "volatile") }; - } - - } -} - - -entry_point!(main); -fn main(scs: scs::Instance, p: hcl::platform::Instance) { - declare_thread_stack!(stack, 4096); - - unsafe { - hcl::set_process_stack(&stack); - hcl::use_process_stack(true); - } - - run(scs, p); -} diff --git a/src/st7735/mod.rs b/src/st7735/mod.rs index c2855b2..dfa5c19 100644 --- a/src/st7735/mod.rs +++ b/src/st7735/mod.rs @@ -415,9 +415,41 @@ impl St7735IO self.write_command(&Command::MADCTL(param)); } + pub fn draw_pixel(&mut self, x : u8, y : u8, color : u16) { + if x >= self.st7735.width || y >= self.st7735.height { + return; + } + + self.set_addr_win(x, y, x+1, y+1); + + self.set_rs(); + self.reset_cs(); + + self.write_color_bytes(color); + + self.set_cs(); + } pub fn fill_rect(&mut self, x : u8, y : u8, w : u8, h : u8, color: u16) { + if x >= self.st7735.width || y >= self.st7735.height { + return; + } + + let w = if (x + w - 1) >= self.st7735.width { + self.st7735.width - x + } + else { + w + }; + + let h = if (y + h - 1) >= self.st7735.height { + self.st7735.height - y + } + else { + h + }; + self.set_addr_win(x, y, x + w - 1, y + h - 1); self.set_rs(); @@ -432,6 +464,14 @@ impl St7735IO self.set_cs(); } + pub fn draw_fast_vline(&mut self, x : u8, y : u8, h : u8, color : u16) { + self.fill_rect(x, y, 1, h, color); + } + + pub fn draw_fast_hline(&mut self, x : u8, y : u8, w : u8, color : u16) { + self.fill_rect(x, y, w, 1, color); + } + pub fn done(mut self) -> (St7735, PeripheralRef, PeripheralRef) { self.set_cs(); self.spi.set_enabled(false);