Skip to content

Instantly share code, notes, and snippets.

@daschl
Last active October 25, 2020 16:07
Show Gist options
  • Save daschl/377186070bea5cdae159238a1b3fdf3c to your computer and use it in GitHub Desktop.
Save daschl/377186070bea5cdae159238a1b3fdf3c to your computer and use it in GitHub Desktop.
display.rs
#![no_main]
#![no_std]
use defmt_rtt as _;
use nrf52840_hal as _; // memory layout // global logger
use embedded_hal::digital::v2::OutputPin;
use nrf52840_hal::gpio::{p0, Level, Output, Pin, PushPull};
use nrf52840_hal::pac::{Peripherals, SPIM0, TIMER0};
use nrf52840_hal::prelude::*;
use nrf52840_hal::spim::{Frequency, Pins, MODE_0};
use nrf52840_hal::Spim;
use nrf52840_hal::Timer;
#[cortex_m_rt::entry]
fn main() -> ! {
let peripherals = Peripherals::take().unwrap();
let port0 = p0::Parts::new(peripherals.P0);
let mut timer = Timer::new(peripherals.TIMER0);
let dc_pin = port0.p0_06.into_push_pull_output(Level::Low).degrade();
let rst_pin = port0.p0_07.into_push_pull_output(Level::Low).degrade();
let cs_pin = port0.p0_08.into_push_pull_output(Level::Low).degrade();
let spi_pins = Pins {
// display clk
sck: port0.p0_10.into_push_pull_output(Level::Low).degrade(),
// display din
mosi: Some(port0.p0_09.into_push_pull_output(Level::Low).degrade()),
miso: None,
};
let spi = Spim::new(peripherals.SPIM0, spi_pins, Frequency::M2, MODE_0, 0);
let mut display = Display::new(spi, dc_pin, rst_pin, cs_pin);
display.init(&mut timer);
display.draw_pixel(5, 5);
loop {
cortex_m::asm::nop();
}
}
struct Display {
dc_pin: Pin<Output<PushPull>>,
rst_pin: Pin<Output<PushPull>>,
cs_pin: Pin<Output<PushPull>>,
spi: Spim<SPIM0>,
}
impl Display {
pub fn new(
spi: Spim<SPIM0>,
dc_pin: Pin<Output<PushPull>>,
rst_pin: Pin<Output<PushPull>>,
cs_pin: Pin<Output<PushPull>>,
) -> Self {
Self {
spi,
dc_pin,
rst_pin,
cs_pin,
}
}
fn cmd(&mut self, cmd: u8) {
self.dc_pin.set_low().unwrap();
self.spi.write(&mut self.cs_pin, &[cmd]).unwrap();
self.dc_pin.set_high().unwrap();
}
fn data(&mut self, data: u8) {
self.dc_pin.set_high().unwrap();
self.spi.write(&mut self.cs_pin, &[data]).unwrap();
self.dc_pin.set_low().unwrap();
}
fn cmd_and_data(&mut self, cmd: u8, data: u8) {
self.cmd(cmd);
self.data(data);
}
pub fn init(&mut self, timer: &mut Timer<TIMER0>) {
self.cs_pin.set_low().unwrap();
self.rst_pin.set_low().unwrap();
timer.delay_ms(500u16);
self.rst_pin.set_high().unwrap();
timer.delay_ms(500u16);
self.cmd_and_data(CMD_COMMANDLOCK, 0x12);
self.cmd_and_data(CMD_COMMANDLOCK, 0xB1);
self.cmd(CMD_DISPLAYOFF);
self.cmd(CMD_NORMALDISPLAYMODE);
self.cmd(0x15);
self.data(0x00);
self.data(0x7f);
self.cmd(0x75);
self.data(0x00);
self.data(0x7f);
self.cmd_and_data(0xb3, 0xf1);
self.cmd_and_data(0xca, 0x7f);
self.cmd_and_data(0xa0, 0x74);
self.cmd_and_data(0xa1, 0x00);
self.cmd_and_data(0xa2, 0x00);
self.cmd(0xab);
self.cmd(0x01);
self.cmd(0xb4);
self.data(0xa0);
self.data(0xb5);
self.data(0x55);
self.cmd(0xc1);
self.data(0xc8);
self.data(0x80);
self.data(0xc0);
self.cmd_and_data(0xc7, 0x0f);
self.cmd_and_data(0xb1, 0x32);
self.cmd(0xb2);
self.data(0xa4);
self.data(0x00);
self.data(0x00);
self.cmd_and_data(0xbb, 0x17);
self.cmd_and_data(0xb6, 0x01);
self.cmd_and_data(0xbe, 0x05);
self.cmd(0xa6);
self.clear_screen();
self.cmd(0xaf);
}
pub fn draw_pixel(&mut self, x: u8, y: u8) {
self.set_address(x, y);
self.data(0xff);
self.data(0xff);
}
fn set_address(&mut self, column: u8, row: u8) {
self.cmd(0x15);
self.data(column);
self.data(column);
self.cmd(0x75);
self.data(row);
self.data(row + 7);
self.cmd(0x5C);
}
fn clear_screen(&mut self) {
self.ram_address();
self.cmd(0x5c);
for _ in 0..128 {
for _ in 0..128 {
self.data(0xff);
self.data(0xff);
}
}
}
fn ram_address(&mut self) {
self.cmd(0x15);
self.data(0x00);
self.data(0x7f);
self.cmd(0x75);
self.data(0x00);
self.data(0x7f);
}
}
const CMD_COMMANDLOCK: u8 = 0xfd;
const CMD_DISPLAYOFF: u8 = 0xae;
const CMD_NORMALDISPLAYMODE: u8 = 0xa4;
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
exit()
}
pub fn exit() -> ! {
loop {
cortex_m::asm::bkpt();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment