Compare commits

..

4 Commits

Author SHA1 Message Date
Savanni D'Gerinel d76fd8cc39 This gets communication working with the device
The device is currently emmitting primes. pi-usb-serial and serial-comm
give me a template for being able to do communication with a USB device
through a serial port.
2024-08-09 01:06:30 -04:00
Savanni D'Gerinel 93b84ee72d Attempting to communicate with the TFT 2024-08-08 22:04:12 -04:00
Savanni D'Gerinel 506a13e802 Set up the demo blinky app with a pi pico 2024-08-01 10:00:22 -04:00
Savanni D'Gerinel 98ceab9833 Start on a usb-serial project 2024-08-01 00:21:52 -04:00
17 changed files with 1493 additions and 91 deletions

1066
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -30,5 +30,5 @@ members = [
"sgf", "sgf",
"timezone-testing", "timezone-testing",
"tree", "tree",
"visions/server", "visions/server", "pi-usb-serial", "tft"
] , "serial-comm"]

15
bike-lights/bike/build.rs Normal file
View File

@ -0,0 +1,15 @@
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
fn main() {
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());
println!("cargo:rerun-if-changed=memory.x");
}

View File

@ -0,0 +1,6 @@
[build]
target = "thumbv6m-none-eabi"
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
runner = "elf2uf2-rs -d"

21
pi-usb-serial/Cargo.toml Normal file
View File

@ -0,0 +1,21 @@
[package]
name = "pi-usb-serial"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
embassy-embedded-hal = { version = "0.1.0", features = ["defmt"] }
embassy-sync = { version = "0.6.0", features = ["defmt"] }
embassy-executor = { version = "0.5.0", features = ["task-arena-size-98304", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] }
embassy-time = { version = "0.3.1", features = ["defmt", "defmt-timestamp-uptime"] }
embassy-rp = { version = "0.1.0", features = ["defmt", "unstable-pac", "time-driver", "critical-section-impl"] }
embassy-usb = { version = "0.3.0" }
defmt = "0.3"
defmt-rtt = "0.4"
cortex-m-rt = "0.7.0"
panic-probe = { version = "0.3", features = ["print-defmt"] }
portable-atomic = { version = "1.5", features = ["critical-section"] }
static_cell = "2"

26
pi-usb-serial/build.rs Normal file
View File

@ -0,0 +1,26 @@
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");
println!("cargo:rustc-link-arg-bins=--nmagic");
println!("cargo:rustc-link-arg-bins=-Tlink.x");
println!("cargo:rustc-link-arg-bins=-Tlink-rp.x");
println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
}

36
pi-usb-serial/memory.x Normal file
View File

@ -0,0 +1,36 @@
MEMORY {
BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100
FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100
/*
* RAM consists of 4 banks, SRAM0-SRAM3, with a striped mapping.
* This is usually good for performance, as it distributes load on
* those banks evenly.
*/
RAM : ORIGIN = 0x20000000, LENGTH = 256K
/*
* RAM banks 4 and 5 use a direct mapping. They can be used to have
* memory areas dedicated for some specific job, improving predictability
* of access times.
* Example: Separate stacks for core0 and core1.
*/
SRAM4 : ORIGIN = 0x20040000, LENGTH = 4k
SRAM5 : ORIGIN = 0x20041000, LENGTH = 4k
/* SRAM banks 0-3 can also be accessed directly. However, those ranges
alias with the RAM mapping, above. So don't use them at the same time!
SRAM0 : ORIGIN = 0x21000000, LENGTH = 64k
SRAM1 : ORIGIN = 0x21010000, LENGTH = 64k
SRAM2 : ORIGIN = 0x21020000, LENGTH = 64k
SRAM3 : ORIGIN = 0x21030000, LENGTH = 64k
*/
}
EXTERN(BOOT2_FIRMWARE)
SECTIONS {
/* ### Boot loader */
.boot2 ORIGIN(BOOT2) :
{
KEEP(*(.boot2));
} > BOOT2
} INSERT BEFORE .text;

112
pi-usb-serial/src/main.rs Normal file
View File

@ -0,0 +1,112 @@
//! This example test the RP Pico on board LED.
//!
//! It does not work with the RP Pico W board. See wifi_blinky.rs.
#![no_std]
#![no_main]
use embassy_usb::{Builder, Config, class::cdc_acm::{CdcAcmClass, State}, UsbDevice, driver::EndpointError};
use embassy_executor::Spawner;
use embassy_rp::{bind_interrupts, gpio, peripherals::USB, usb::{Driver, Instance, InterruptHandler}};
use embassy_time::Timer;
use gpio::{Level, Output};
use defmt::{info, panic, unwrap};
use defmt_rtt as _;
use panic_probe as _;
use static_cell::StaticCell;
bind_interrupts!(struct Irqs {
USBCTRL_IRQ => InterruptHandler<USB>;
});
#[embassy_executor::task]
async fn usb_task(mut usb: UsbDevice<'static, Driver<'static, USB>>) {
usb.run().await
}
#[embassy_executor::main]
async fn main(spawner: Spawner) {
let p = embassy_rp::init(Default::default());
let mut led = Output::new(p.PIN_15, Level::Low);
let driver = embassy_rp::usb::Driver::new(p.USB, Irqs);
let mut config = Config::new(0x9988, 0x8899);
config.manufacturer = Some("Savanni");
config.product = Some("USB test device");
config.serial_number = Some("abcdefg");
config.max_power = 100;
config.max_packet_size_0 = 64;
let mut builder = {
static CONFIG_DESCRIPTOR: StaticCell<[u8; 256]> = StaticCell::new();
static BOS_DESCRIPTOR: StaticCell<[u8; 256]> = StaticCell::new();
static CONTROL_BUF: StaticCell<[u8; 64]> = StaticCell::new();
let mut builder = Builder::new(
driver,
config,
CONFIG_DESCRIPTOR.init([0; 256]),
BOS_DESCRIPTOR.init([0; 256]),
&mut [],
CONTROL_BUF.init([0; 64]),
);
builder
};
let mut class = {
static STATE: StaticCell<State> = StaticCell::new();
let state = STATE.init(State::new());
CdcAcmClass::new(&mut builder, state, 64)
};
let usb = builder.build();
unwrap!(spawner.spawn(usb_task(usb)));
loop {
class.wait_connection().await;
led.set_high();
info!("Connected");
// let _ = echo(&mut class).await;
let _ = primes(&mut class).await;
info!("Disconnected");
led.set_low();
}
}
struct Disconnected {}
impl From<EndpointError> for Disconnected {
fn from(val: EndpointError) -> Self {
match val {
EndpointError::BufferOverflow => {
panic!("Buffer overflow");
}
EndpointError::Disabled => Disconnected{},
}
}
}
async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
let mut buf = [0; 64];
loop {
let n = class.read_packet(&mut buf).await?;
buf[n] = b'\r';
buf[n+1] = b'\n';
let data = &buf[..n+2];
info!("data: {:x}", data);
class.write_packet(&data).await?;
}
}
async fn primes<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
let PRIMES: [u8; 55] = [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251];
let mut buf = [0; 64];
loop {
for idx in 0..PRIMES.len() {
buf[0] = PRIMES[idx];
class.write_packet(&buf).await?;
Timer::after_secs(1).await;
};
}
}

View File

@ -1,3 +1,3 @@
[toolchain] [toolchain]
channel = "1.77.0" channel = "1.80.0"
targets = [ "wasm32-unknown-unknown", "thumbv6m-none-eabi" ] targets = [ "wasm32-unknown-unknown", "thumbv6m-none-eabi" ]

8
serial-comm/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "serial-comm"
version = "0.1.0"
edition = "2021"
[dependencies]
tokio = { version = "1.39.2", features = ["full"] }
tokio-serial = "5.4.4"

22
serial-comm/src/main.rs Normal file
View File

@ -0,0 +1,22 @@
use tokio_serial::{DataBits, StopBits, Parity, SerialStream};
#[tokio::main]
async fn main() {
let mut port = SerialStream::open(&tokio_serial::new("/dev/ttyACM0", 115200).parity(Parity::None).stop_bits(StopBits::One).data_bits(DataBits::Eight)).expect("serial port to open");
/*
port.writable().await.expect("writeable to succeed");
let bytes = port.try_write(b"abcdefg").expect("write to succeed");
println!("{} bytes written", bytes);
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
*/
loop {
port.readable().await.expect("readable to succeed");
let mut buf: [u8; 64]= [0; 64];
if let Ok(_) = port.try_read(&mut buf) {
println!("{:?}", &buf);
}
}
}

6
tft/.cargo/config.toml Normal file
View File

@ -0,0 +1,6 @@
[build]
target = "thumbv6m-none-eabi"
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
runner = "elf2uf2-rs -d -s"

19
tft/Cargo.toml Normal file
View File

@ -0,0 +1,19 @@
[package]
name = "e-ink"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
embassy-embedded-hal = { version = "0.1.0", features = ["defmt"] }
embassy-sync = { version = "0.6.0", features = ["defmt"] }
embassy-executor = { version = "0.5.0", features = ["task-arena-size-98304", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] }
embassy-time = { version = "0.3.1", features = ["defmt", "defmt-timestamp-uptime"] }
embassy-rp = { version = "0.1.0", features = ["defmt", "unstable-pac", "time-driver", "critical-section-impl"] }
defmt = "0.3"
defmt-rtt = "0.4"
cortex-m-rt = "0.7.0"
panic-probe = { version = "0.3", features = ["print-defmt"] }
embedded-hal = "1.0.0"

26
tft/build.rs Normal file
View File

@ -0,0 +1,26 @@
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");
println!("cargo:rustc-link-arg-bins=--nmagic");
println!("cargo:rustc-link-arg-bins=-Tlink.x");
println!("cargo:rustc-link-arg-bins=-Tlink-rp.x");
println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
}

36
tft/memory.x Normal file
View File

@ -0,0 +1,36 @@
MEMORY {
BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100
FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100
/*
* RAM consists of 4 banks, SRAM0-SRAM3, with a striped mapping.
* This is usually good for performance, as it distributes load on
* those banks evenly.
*/
RAM : ORIGIN = 0x20000000, LENGTH = 256K
/*
* RAM banks 4 and 5 use a direct mapping. They can be used to have
* memory areas dedicated for some specific job, improving predictability
* of access times.
* Example: Separate stacks for core0 and core1.
*/
SRAM4 : ORIGIN = 0x20040000, LENGTH = 4k
SRAM5 : ORIGIN = 0x20041000, LENGTH = 4k
/* SRAM banks 0-3 can also be accessed directly. However, those ranges
alias with the RAM mapping, above. So don't use them at the same time!
SRAM0 : ORIGIN = 0x21000000, LENGTH = 64k
SRAM1 : ORIGIN = 0x21010000, LENGTH = 64k
SRAM2 : ORIGIN = 0x21020000, LENGTH = 64k
SRAM3 : ORIGIN = 0x21030000, LENGTH = 64k
*/
}
EXTERN(BOOT2_FIRMWARE)
SECTIONS {
/* ### Boot loader */
.boot2 ORIGIN(BOOT2) :
{
KEEP(*(.boot2));
} > BOOT2
} INSERT BEFORE .text;

179
tft/src/main.rs Normal file
View File

@ -0,0 +1,179 @@
#![no_std]
#![no_main]
use embassy_executor::Spawner;
use embassy_rp::{spi, spi::Spi, gpio};
use embedded_hal::delay::DelayNs;
use embassy_time::Delay;
use gpio::{Level, Output};
use defmt::*;
use defmt_rtt as _;
use panic_probe as _;
/*
* width: 320
* height: 170
*/
fn software_reset<T, M, R>(spi: &mut Spi<T, M>, timer: &mut Delay, dcx: &mut Output<R>)
where
T: embassy_rp::spi::Instance,
M: embassy_rp::spi::Mode,
R: embassy_rp::gpio::Pin,
{
dcx.set_low();
let _ = spi.blocking_write(&[0x01]);
timer.delay_ms(150);
}
fn sleep_out<T, M, R>(spi: &mut Spi<T, M>, timer: &mut Delay, dcx: &mut Output<R>)
where
T: embassy_rp::spi::Instance,
M: embassy_rp::spi::Mode,
R: embassy_rp::gpio::Pin,
{
dcx.set_low();
let _ = spi.blocking_write(&[0x11]);
timer.delay_ms(10);
}
fn color_mode<T, M, R>(spi: &mut Spi<T, M>, timer: &mut Delay, dcx: &mut Output<R>)
where
T: embassy_rp::spi::Instance,
M: embassy_rp::spi::Mode,
R: embassy_rp::gpio::Pin,
{
dcx.set_low();
let _ = spi.blocking_write(&[0x11]);
dcx.set_high();
let _ = spi.blocking_write(&[0x63]);
timer.delay_ms(10);
}
fn normal_display<T, M, R>(spi: &mut Spi<T, M>, timer: &mut Delay, dcx: &mut Output<R>)
where
T: embassy_rp::spi::Instance,
M: embassy_rp::spi::Mode,
R: embassy_rp::gpio::Pin,
{
dcx.set_low();
let _ = spi.blocking_write(&[0x13]);
timer.delay_ms(10);
}
fn display_on<T, M, R>(spi: &mut Spi<T, M>, timer: &mut Delay, dcx: &mut Output<R>)
where
T: embassy_rp::spi::Instance,
M: embassy_rp::spi::Mode,
R: embassy_rp::gpio::Pin,
{
dcx.set_low();
let _ = spi.blocking_write(&[0x29]);
timer.delay_ms(10);
}
fn memory_address_set<T, M, R>(spi: &mut Spi<T, M>, timer: &mut Delay, dcx: &mut Output<R>)
where
T: embassy_rp::spi::Instance,
M: embassy_rp::spi::Mode,
R: embassy_rp::gpio::Pin,
{
dcx.set_low();
spi.blocking_write(&[0x36]);
dcx.set_high();
spi.blocking_write(&[0x08]);
timer.delay_ms(10);
}
fn column_set<T, M, R>(spi: &mut Spi<T, M>, timer: &mut Delay, dcx: &mut Output<R>)
where
T: embassy_rp::spi::Instance,
M: embassy_rp::spi::Mode,
R: embassy_rp::gpio::Pin,
{
let width: u16 = 320;
dcx.set_low();
let _ = spi.blocking_write(&[0x2a]);
dcx.set_high();
let _ = spi.blocking_write(&[0x0, 0x0, (width >> 8) as u8, (width & 0xff) as u8]);
timer.delay_ms(10);
}
fn row_set<T, M, R>(spi: &mut Spi<T, M>, timer: &mut Delay, dcx: &mut Output<R>)
where
T: embassy_rp::spi::Instance,
M: embassy_rp::spi::Mode,
R: embassy_rp::gpio::Pin,
{
let height: u16 = 170;
dcx.set_low();
let _ = spi.blocking_write(&[0x2b]);
dcx.set_high();
let _ = spi.blocking_write(&[0x0, 0x0, (height >> 8) as u8, (170 & 0xff) as u8]);
timer.delay_ms(10);
}
fn write_image<T, M, R>(spi: &mut Spi<T, M>, timer: &mut Delay, dcx: &mut Output<R>)
where
T: embassy_rp::spi::Instance,
M: embassy_rp::spi::Mode,
R: embassy_rp::gpio::Pin,
{
dcx.set_low();
let _ = spi.blocking_write(&[0x2c]);
dcx.set_high();
let mut buf: [u8; 320 * 170 * 3] = [0; 320 * 170 * 3];
buf[0] = 0xff;
buf[2] = 0xff;
buf[3] = 0xff;
let _ = spi.blocking_write(&buf);
}
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default());
let mut timer = Delay;
let mut tft_select = Output::new(p.PIN_10, Level::High); /* Pull low to activate the chip */
let mut tft_reset = Output::new(p.PIN_11, Level::Low);
timer.delay_ms(10);
tft_reset.set_high();
let mut dcx = Output::new(p.PIN_15, Level::Low); /* Low == Command, High == Data */
let mut spi_config = spi::Config::default();
spi_config.frequency = 2_000_000;
let mut spi = embassy_rp::spi::Spi::new_blocking(
p.SPI0,
p.PIN_2,
p.PIN_3,
p.PIN_4,
spi_config
);
tft_select.set_low();
/* Software reset, 150ms delay */
software_reset(&mut spi, &mut timer, &mut dcx);
/* 10 ms delay after each additonal command */
/* Sleep out */
sleep_out(&mut spi, &mut timer, &mut dcx);
/* Color mode */
color_mode(&mut spi, &mut timer, &mut dcx);
/* Memory access ctrl */
memory_address_set(&mut spi, &mut timer, &mut dcx);
/* Column addr set */
column_set(&mut spi, &mut timer, &mut dcx);
/* Row addr set */
row_set(&mut spi, &mut timer, &mut dcx);
/* Normal Display on */
normal_display(&mut spi, &mut timer, &mut dcx);
/* Main screen on */
display_on(&mut spi, &mut timer, &mut dcx);
write_image(&mut spi, &mut timer, &mut dcx);
tft_select.set_high();
}