Skip to content

Commit

Permalink
Basic display drawing complete
Browse files Browse the repository at this point in the history
  • Loading branch information
sky-dragn committed Feb 10, 2024
1 parent 378156a commit 4fe0c0a
Show file tree
Hide file tree
Showing 11 changed files with 323 additions and 27 deletions.
1 change: 0 additions & 1 deletion .genignore

This file was deleted.

5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/target
Cargo.lock
.DS_Store
.DS_Store

.gdb_history
.idea
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
# TODO fix `authors` and `name` if you didn't use `cargo-generate`
name = "test-app"
name = "gps-watch"
edition = "2021"
version = "0.1.0"

Expand All @@ -14,7 +14,9 @@ rtic = { version = "2.0.0", features = [ "thumbv7-backend" ] }
# TODO(5) Add hal as dependency
stm32l4xx-hal = { version = "0.7.1", features = ["stm32l442"] }
# TODO add a monotonic if you use scheduling
# rtic-monotonics = { version = "1.0.0", features = [ "cortex-m-systick" ]}
rtic-monotonics = { version = "1.0.0", features = [ "cortex-m-systick", "systick-100hz"]}
thiserror = { version = "1.0.50", package = "thiserror-core", default-features = false }
embedded-graphics = "0.8.1"

# cargo build/run
[profile.dev]
Expand Down Expand Up @@ -54,6 +56,9 @@ lto = 'fat'
opt-level = "s" # <-
overflow-checks = false # <-

[patch.crates-io]
stm32l4xx-hal = { path = "./stm32l4xx-hal" }

# uncomment this to switch from the crates.io version of defmt to its git version
# check app-template's README for instructions
# [patch.crates-io]
Expand Down
14 changes: 14 additions & 0 deletions gdb.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set architecture armv7e-m

define reset
monitor reset halt
end

define flash
monitor program target/thumbv7em-none-eabihf/debug/minimal
end

target remote localhost:1337

symbol-file target/thumbv7em-none-eabihf/debug/minimal
monitor reset halt
1 change: 0 additions & 1 deletion memory.x
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
MEMORY
{
/* NOTE K = KiBi = 1024 bytes */
/* TODO Adjust these memory regions to match your device memory layout */
FLASH : ORIGIN = 0x8000000, LENGTH = 128K
RAM : ORIGIN = 0x20000000, LENGTH = 32K
}
Expand Down
10 changes: 10 additions & 0 deletions openocd.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
telnet_port 4444
gdb_port 1337

source [find interface/stlink.cfg]

transport select hla_swd

source [find target/stm32l4x.cfg]

reset_config srst_only
2 changes: 2 additions & 0 deletions run-gdb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
gdb-multiarch -x gdb.cfg
145 changes: 123 additions & 22 deletions src/bin/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,46 @@
#![no_std]
#![feature(type_alias_impl_trait)]

use test_app as _; // global logger + panicking-behavior + memory layout
use gps_watch as _;
use stm32l4xx_hal as hal;

use hal::{pac, prelude::*};
use rtic_monotonics::create_systick_token;
use rtic_monotonics::systick::Systick;
use stm32l4xx_hal::gpio::{Alternate, Output, PA11, PB3, PB4, PB5, PinExt, PushPull};
use stm32l4xx_hal::hal::spi::{Mode, Phase, Polarity};
use stm32l4xx_hal::pac::SPI1;
use stm32l4xx_hal::spi::Spi;
use defmt::{trace, info};
use core::num::Wrapping;
use embedded_graphics::primitives::PrimitiveStyle;
use embedded_graphics::prelude::*;
use rtic::Mutex;

type SharpMemDisplay = gps_watch::display::SharpMemDisplay<
Spi<SPI1, (
PB3<Alternate<PushPull, 5>>,
PB4<Alternate<PushPull, 5>>,
PB5<Alternate<PushPull, 5>>
)>, PA11<Output<PushPull>>>;

// TODO(7) Configure the `rtic::app` macro
#[rtic::app(
// TODO: Replace `some_hal::pac` with the path to the PAC
device = stm32l4xx_hal::pac,
// peripherals = true,
// TODO: Replace the `FreeInterrupt1, ...` with free interrupt vectors if software tasks are used
// You can usually find the names of the interrupt vectors in the some_hal::pac::interrupt enum.
dispatchers = [EXTI0]
dispatchers = [EXTI0],
)]
mod app {
use stm32l4xx_hal as hal;

use hal::{pac, prelude::*};
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::primitives::Rectangle;
use super::*;

// Shared resources go here
#[shared]
struct Shared {
// TODO: Add resources
display: SharpMemDisplay
}

// Local resources go here
Expand All @@ -30,21 +51,67 @@ mod app {
}

#[init]
fn init(_cx: init::Context) -> (Shared, Local) {
let peripherals = pac::Peripherals::take();
fn init(cx: init::Context) -> (Shared, Local) {
trace!("init enter");

// Configure clocks
// unsafe {
// // MSI is already on
// // Set MSI to 200 kHz
// cx.device.RCC.cr.write(|w| w.msirange().bits(0b0001).msirgsel().set_bit());
// // Set MSI as system clock source
// cx.device.RCC.cfgr.write(|w| w.sw().bits(0b00));
// }
let mut flash = cx.device.FLASH.constrain();
let mut rcc = cx.device.RCC.constrain();
let mut pwr = cx.device.PWR.constrain(&mut rcc.apb1r1);
let clocks = rcc.cfgr.freeze(&mut flash.acr, &mut pwr);

// Create SysTick monotonic for task scheduling
Systick::start(
cx.core.SYST,
4_000_000,
// 200_000,
create_systick_token!()
);

// Initialize SPI
let mut gpioa = cx.device.GPIOA.split(&mut rcc.ahb2);
let mut gpiob = cx.device.GPIOB.split(&mut rcc.ahb2);

defmt::info!("init");
let mut cs = gpioa.pa11.into_push_pull_output(&mut gpioa.moder, &mut gpioa.otyper);
cs.set_low();

// TODO setup monotonic if used
// let sysclk = { /* clock setup + returning sysclk as an u32 */ };
// let token = rtic_monotonics::create_systick_token!();
// rtic_monotonics::systick::Systick::new(cx.core.SYST, sysclk, token);
// <cs as hal::hal::digital::v2::OutputPin>;
let mut sck = gpiob.pb3.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let mut mosi = gpiob.pb5.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let mut miso = gpiob.pb4.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);

blink_led::spawn().ok();

let SPI1_COPY = unsafe { core::ptr::read(&cx.device.SPI1) };
let spi1 = hal::spi::Spi::spi1(
cx.device.SPI1,
(sck, miso, mosi),
Mode {
phase: Phase::CaptureOnFirstTransition,
polarity: Polarity::IdleLow
},
1.MHz(),
clocks,
&mut rcc.apb2
);
// SPI1_COPY.cr1.write(|w| w.lsbfirst().lsbfirst());
// let spi1_cr1 = 0x4001_3000 as *mut u32;
// unsafe { core::ptr::write_volatile(spi1_cr1, core::ptr::read_volatile(spi1_cr1) | (1u32 << 7)); }

let mut display: SharpMemDisplay = SharpMemDisplay::new(spi1, cs);

display_task::spawn().unwrap();

trace!("init exit");
(
Shared {
// Initialization of shared resources go here
display
},
Local {
// Initialization of local resources go here
Expand All @@ -55,16 +122,50 @@ mod app {
// Optional idle, can be removed if not needed.
#[idle]
fn idle(_: idle::Context) -> ! {
defmt::info!("idle");
trace!("idle enter");

// let device_stolen = unsafe { pac::Peripherals::steal() };

loop {
continue;
core::hint::spin_loop();
// Sleep
// rtic::export::wfi();
// defmt::info!("hewo 1");
// unsafe {
// device_stolen.RCC.cr.write(|w| w.msirange().bits(0b0001).msirgsel().set_bit());
// }
// defmt::info!("hewo 2");
// // Return to our previously-selected MSI frequency
// device_stolen.RCC.cr.write(|w| w.msirgsel().set_bit());
// defmt::info!("hewo 3");
// unsafe {
// device_stolen.RCC.cfgr.write(|w| w.sw().bits(0b00));
// }
// defmt::info!("hewo 4");
}
}

// TODO: Add tasks
#[task(priority = 1)]
async fn blink_led(_cx: blink_led::Context) {
defmt::info!("Hello from task1!");
#[task(
priority = 1,
shared = [display]
)]
async fn display_task(mut cx: display_task::Context) {
trace!("display_task enter");
cx.shared.display.lock(|display| display.clear());

let mut i = Wrapping(0u8);
loop {
Systick::delay(500.millis()).await;
let stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 3);
let rect_styled = Rectangle { top_left: Point {x: i.0 as i32, y: i.0 as i32}, size: Size { width: 20, height: 20 } }
.into_styled(stroke);
cx.shared.display.lock(|display| {
rect_styled.draw(display).unwrap();
display.flush();
});
info!("hello world");
i += 1;
}
}
}
Loading

0 comments on commit 4fe0c0a

Please sign in to comment.