Skip to content

Use blocking spi traits #29

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

Merged
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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changes
- [breaking-change] Use SPI blocking traits instead to ease SPI peripheral sharing.
See: https://github.com/rust-embedded-community/embedded-sdmmc-rs/issues/28


[Unreleased]: https://github.com/rust-embedded-community/embedded-sdmmc-rs/compare/v0.3.0...develop
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ edition = "2018"
readme = "README.md"

[dependencies]
embedded-hal = "^0.2.2"
embedded-hal = "0.2.3"
byteorder = { version = "1", default-features = false }
nb = "0.1"
log = "0.4"

[dev-dependencies]
env_logger = "0.7"
hex-literal = "0.3"
env_logger = "0.8"
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
//! # struct DummyCsPin;
//! # struct DummyUart;
//! # struct DummyTimeSource;
//! # impl embedded_hal::spi::FullDuplex<u8> for DummySpi {
//! # impl embedded_hal::blocking::spi::Transfer<u8> for DummySpi {
//! # type Error = ();
//! # fn read(&mut self) -> nb::Result<u8, ()> { Ok(0) }
//! # fn send(&mut self, byte: u8) -> nb::Result<(), ()> { Ok(()) }
//! # fn transfer<'w>(&mut self, data: &'w mut [u8]) -> Result<&'w [u8], ()> { Ok(&[0]) }
//! # }
//! # impl embedded_hal::digital::v2::OutputPin for DummyCsPin {
//! # type Error = ();
Expand Down
18 changes: 9 additions & 9 deletions src/sdmmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use super::sdmmc_proto::*;
use super::{Block, BlockCount, BlockDevice, BlockIdx};
use core::cell::RefCell;
use nb::block;

const DEFAULT_DELAY_COUNT: u32 = 32_000;

Expand All @@ -17,9 +16,9 @@ const DEFAULT_DELAY_COUNT: u32 = 32_000;
/// bytes without Chip Select asserted (which puts the card into SPI mode).
pub struct SdMmcSpi<SPI, CS>
where
SPI: embedded_hal::spi::FullDuplex<u8>,
SPI: embedded_hal::blocking::spi::Transfer<u8>,
CS: embedded_hal::digital::v2::OutputPin,
<SPI as embedded_hal::spi::FullDuplex<u8>>::Error: core::fmt::Debug,
<SPI as embedded_hal::blocking::spi::Transfer<u8>>::Error: core::fmt::Debug,
{
spi: RefCell<SPI>,
cs: RefCell<CS>,
Expand Down Expand Up @@ -106,9 +105,9 @@ impl Delay {

impl<SPI, CS> SdMmcSpi<SPI, CS>
where
SPI: embedded_hal::spi::FullDuplex<u8>,
SPI: embedded_hal::blocking::spi::Transfer<u8>,
CS: embedded_hal::digital::v2::OutputPin,
<SPI as embedded_hal::spi::FullDuplex<u8>>::Error: core::fmt::Debug,
<SPI as embedded_hal::blocking::spi::Transfer<u8>>::Error: core::fmt::Debug,
{
/// Create a new SD/MMC controller using a raw SPI interface.
pub fn new(spi: SPI, cs: CS) -> SdMmcSpi<SPI, CS> {
Expand Down Expand Up @@ -417,8 +416,9 @@ where
/// Send one byte and receive one byte.
fn transfer(&self, out: u8) -> Result<u8, Error> {
let mut spi = self.spi.borrow_mut();
block!(spi.send(out)).map_err(|_e| Error::Transport)?;
block!(spi.read()).map_err(|_e| Error::Transport)
spi.transfer(&mut [out])
.map(|b| b[0])
.map_err(|_e| Error::Transport)
}

/// Spin until the card returns 0xFF, or we spin too many times and
Expand All @@ -438,8 +438,8 @@ where

impl<SPI, CS> BlockDevice for SdMmcSpi<SPI, CS>
where
SPI: embedded_hal::spi::FullDuplex<u8>,
<SPI as embedded_hal::spi::FullDuplex<u8>>::Error: core::fmt::Debug,
SPI: embedded_hal::blocking::spi::Transfer<u8>,
<SPI as embedded_hal::blocking::spi::Transfer<u8>>::Error: core::fmt::Debug,
CS: embedded_hal::digital::v2::OutputPin,
{
type Error = Error;
Expand Down