Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RP2040 QT Py Example #3

Merged
merged 3 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by Cargo
# will have compiled files and executables
/target/
target

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "is31fl3743"
name = "is31fl3743a"
version = "0.1.0"
edition = "2021"
authors = ["Daniel Schaefer"]
Expand Down
12 changes: 12 additions & 0 deletions examples/qt-py-rp2040/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[build]
target = "thumbv6m-none-eabi"

[target.thumbv6m-none-eabi]
runner = "elf2uf2-rs -d"
# runner = "picotool load -x -t elf"
# runner = "probe-run --chip RP2040"

rustflags = [
"-C", "link-arg=-Tlink.x",
"-C", "link-arg=--nmagic",
]
19 changes: 19 additions & 0 deletions examples/qt-py-rp2040/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
edition = "2021"
name = "is31fl3741-qt-py-rp2040"
version = "0.3.0"

[dependencies]
embedded-hal = "0.2.7"
embedded-graphics-core = { optional = true, version = "0.4.0" }

cortex-m-rt = "0.7.3"
cortex-m = "0.7.7"
panic-halt = "0.2.0"
rp-pico = "0.7.0"
adafruit-qt-py-rp2040 = {version = "0.6.0", features = ["rt"]}
tinybmp = "0.5.0"
embedded-graphics = "0.8.1"
fugit = "0.3.7"

is31fl3743a = { path = "../.." }
31 changes: 31 additions & 0 deletions examples/qt-py-rp2040/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! This build script copies the `memory.x` file from the crate root into
//! a directory where the linker can always find it at build time.
//! For many projects this is optional, as the linker always searches the
//! project root directory -- wherever `Cargo.toml` is. However, if you
//! are using a workspace or have a more complicated build setup, this
//! build script becomes required. Additionally, by requesting that
//! Cargo re-run the build script whenever `memory.x` is changed,
//! updating `memory.x` ensures a rebuild of the application with the
//! new memory settings.

use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

fn main() {
// Put `memory.x` in our output directory and ensure it's
// on the linker search path.
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("memory.x"))
.unwrap()
.write_all(include_bytes!("memory.x"))
.unwrap();
println!("cargo:rustc-link-search={}", out.display());

// By default, Cargo will re-run a build script whenever
// any file in the project changes. By specifying `memory.x`
// here, we ensure the build script is only re-run when
// `memory.x` is changed.
println!("cargo:rerun-if-changed=memory.x");
}
15 changes: 15 additions & 0 deletions examples/qt-py-rp2040/memory.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
MEMORY {
BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100
FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100
RAM : ORIGIN = 0x20000000, LENGTH = 256K
}

EXTERN(BOOT2_FIRMWARE)

SECTIONS {
/* ### Boot loader */
.boot2 ORIGIN(BOOT2) :
{
KEEP(*(.boot2));
} > BOOT2
} INSERT BEFORE .text;
77 changes: 77 additions & 0 deletions examples/qt-py-rp2040/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#![no_std]
#![no_main]

// pick a panicking behavior
use panic_halt as _;

use adafruit_qt_py_rp2040::entry;
use adafruit_qt_py_rp2040::{
hal::{
clocks::{init_clocks_and_plls, Clock},
i2c::I2C,
pac,
watchdog::Watchdog,
Sio,
},
Pins, XOSC_CRYSTAL_FREQ,
};
use fugit::RateExtU32;
use is31fl3743a::devices::UnknownDevice;

#[entry]
fn main() -> ! {
let mut pac = pac::Peripherals::take().unwrap();
let core = pac::CorePeripherals::take().unwrap();

let mut watchdog = Watchdog::new(pac.WATCHDOG);

let clocks = init_clocks_and_plls(
XOSC_CRYSTAL_FREQ,
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut watchdog,
)
.ok()
.unwrap();

let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());
let sio = Sio::new(pac.SIO);
let pins = Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);

// Using STEMMA QT connector
let sda = pins.sda1.into_mode(); // gpio22
let scl = pins.scl1.into_mode(); // gpio23

//let sda = pins.sda.into_mode(); // gpio24
//let scl = pins.scl.into_mode(); // gpio25

let i2c = I2C::i2c1(
pac.I2C1,
sda,
scl,
400.kHz(),
&mut pac.RESETS,
125_000_000.Hz(),
);

let mut matrix = UnknownDevice::configure(i2c);
matrix
.setup(&mut delay)
.expect("failed to setup rgb controller");

matrix.set_scaling(0xFF).expect("failed to set scaling");

loop {
matrix.device.fill(0xFF).expect("couldn't turn on");
delay.delay_ms(100u32);
matrix.device.fill(0x00).expect("couldn't turn off");
}
}
46 changes: 0 additions & 46 deletions examples/stm32.rs

This file was deleted.

48 changes: 4 additions & 44 deletions src/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,6 @@ pub struct UnknownDevice<I2C> {
pub device: IS31FL3743<I2C>,
}

#[cfg(feature = "embedded_graphics")]
use embedded_graphics_core::{pixelcolor::Rgb888, prelude::*, primitives::Rectangle};

#[cfg(feature = "embedded_graphics")]
impl<I2C, I2cError> Dimensions for UnknownDevice<I2C>
where
I2C: Write<Error = I2cError>,
I2C: Read<Error = I2cError>,
{
fn bounding_box(&self) -> Rectangle {
Rectangle::new(Point::zero(), Size::new(13, 9))
}
}

#[cfg(feature = "embedded_graphics")]
impl<I2C, I2cError> DrawTarget for UnknownDevice<I2C>
where
I2C: Write<Error = I2cError>,
I2C: Read<Error = I2cError>,
I2cError:,
{
type Color = Rgb888;
type Error = Error<I2cError>;

fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = Pixel<Self::Color>>,
{
for Pixel(coord, color) in pixels.into_iter() {
// Check if the pixel coordinates are out of bounds (negative or greater than
// (63,63)). `DrawTarget` implementation are required to discard any out of bounds
// pixels without returning an error or causing a panic.
if let Ok((x @ 0..=13, y @ 0..=9)) = coord.try_into() {
// Calculate the index in the framebuffer.
self.pixel_rgb(x as u8, y as u8, color.r(), color.g(), color.b())?
}
}

Ok(())
}
}

impl<I2C, I2cError> UnknownDevice<I2C>
where
I2C: Write<Error = I2cError>,
Expand All @@ -72,11 +30,13 @@ where
device: IS31FL3743 {
i2c,
address: 0b0100000,
// Dummy values, not used
width: 18 * 11,
// Dummy values, not used
height: 1,
calc_pixel: |x: u8, y: u8| -> u8 {
calc_pixel: |_x: u8, _y: u8| -> u8 {
// Dummy value, don't use this function
0x00
unimplemented!("No Matrix support yet")
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_std]
#![doc = include_str!("../README.md")]
/// Preconfigured devices
//pub mod devices;
pub mod devices;
use embedded_hal::blocking::delay::DelayMs;
use embedded_hal::blocking::i2c::Read;
use embedded_hal::blocking::i2c::Write;
Expand Down