Skip to content

Instantly share code, notes, and snippets.

@fbeutel

fbeutel/spi.rs Secret

Created August 10, 2022 12:35
Show Gist options
  • Save fbeutel/c8d5c974e9ed63ecfc81ef323849009c to your computer and use it in GitHub Desktop.
Save fbeutel/c8d5c974e9ed63ecfc81ef323849009c to your computer and use it in GitHub Desktop.
ftdi_embedded_hal SPI example
use std::path::Path;
use ftdi_embedded_hal as hal;
#[derive(Debug)]
pub enum Error {
GeneralError(String)
}
trait SPIBus {
fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Error>;
}
impl<'a> SPIBus for ftdi_embedded_hal::Spi<'a, ftdi::Device>
{
fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Error> {
embedded_hal::blocking::spi::Transfer::transfer(self, words).map_err(|_| Error::GeneralError("foo".to_string()))
}
}
fn init_spi_device(config_file: &Path) -> Box<dyn SPIBus> {
// .. load device depending on configuration file
let device = ftdi::find_by_vid_pid(0x0403, 0x6010)
.interface(ftdi::Interface::A)
.open()
.unwrap();
let hal = hal::FtHal::init_freq(device, 3_000_000).unwrap();
let spi = hal.spi().unwrap();
Box::new(spi)
}
fn main() {
let spi_device = init_spi_device(todo!());
// ... use spi device and pass it to drivers
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment