Comments (3)
Hi,
it would be of great help to have a function here to set the SPI clock frequency.
As an example, MPU6000 is mostly connected via SPI to µC. This chips starts in I2C mode, so it needs SPI clock of below 400kHz. After you configure the MU6000 to use SPI mode you can use ~8MHz SPI clock to fasten up transfer.
This is a valid use case where you need a set_spi_frequency() function at a relative high abstraction level (abstracting the whole MPU6000 chip in a crate), independent from sharing some µC knowledge of how to set SPI clock frequency.
from embedded-hal.
I recently wrote two crates (mfrc522 and mpu9250) to interface two different SPI devices. These
crates use the traits in this embedded-hal repo and have been tested to work with Cortex-M
microcontrollers (no_std, svd2rust API) and the Raspberry Pi (std, spidev API).
I wanted to jot down some things I noticed while writing these crates.
(I would suggest reading RFC rust-embedded/svd2rust#157 first. It contains some ideas on using move
semantics (the type system) to enforce driver correctness (prevent misuse) at compile time. These
ideas have heavily influenced how I've written the aforementioned SPI device crates)
- SPI devices can operate in different modes. Your SPI peripheral must be configured to operate in
the same mode or you won't be able to interface a SPI device.
Would it be practical (ergonomics correctness trade-off) to encode the mode in the type system? As
in the type system would force you to configure the SPI in the correct mode to be able to use a
driver.
Is there an use case for switching the mode of a SPI peripheral at runtime? Or is it always a set once and forget setting?
If we want to do this I don't think the SPI trait should contain the mode as a type parameter but
for code sharing the mode types should live in the embedded-hal crate. (I already added some mode related Polarity / Phase enums to embedded-hal)
- A single SPI master can interface with several slaves by individually "selecting" them (driving
some digital I/O low).
Background: In my crates I modeled the device drivers as struct Driver { spi: impl Spi, nss: impl OutputPin }
that is the driver takes ownership of both the spi peripheral and the slave select pin.
This is a bit incompatible with multi-slave applications where you have a single SPI peripheral that
you want to use with several device driver instances.
Would it be more ergonomic to change the driver struct to only take ownership of the slave select
pin, as in struct Driver { nss: impl OutputPin }
, and have the API borrow the SPI peripheral, as
in impl Driver { fn do_stuff(&mut self, spi: &mut impl Spi) { .. } }
. Would this approach be still
be correct?
I consider the fact that these potential improvements don't require modifying the existing methods of
the Spi
trait a good sign :-).
from embedded-hal.
The spi::FullDuplex
and blocking::spi::{Transfer,Write}
traits are now part of a crates.io release so I'm going to close this. If you encounter any problem implementing / using those traits feel free to open a new issue!
If you want to propose adding futures or generator based SPI traits you should open a new RFC issue.
from embedded-hal.
Related Issues (20)
- Why do all `Error` traits need to implement `Debug`? HOT 4
- Document SemVer hazards of `spi::Operation::DelayNs` HOT 1
- Add `discard` to `BufRead`. HOT 5
- spi: specify expectations regarding peripheral state between transactions HOT 2
- embedded-io: API stability and 1.0.0 release
- Zero-length I2c transfers HOT 3
- embedded-hal-bus shared i2c usage HOT 5
- embedded-hal-bus does not impliment embedded-hal-async traits, despite claiming to HOT 5
- The SPI sharing utilities are broken for fallible chipselect pins HOT 3
- guidance on error handling/propagation of drivers HOT 3
- Handling of parity and framing errors in embedded-io / embedded-io-async
- CAN FD support HOT 1
- unable to return error with embedded hal i2c example HOT 1
- HAL-Bus SPI Exclusive Device Unsatisfied Traits HOT 5
- i2c: Merging of consecutive operations in transaction contract
- SpiDevice's interface can't be used for streaming transactions HOT 6
- SpiDevice implementations in embedded-hal-bus don't provide a way to use active-high chip select HOT 3
- Create an I3C Trait
- Read not implemented for &mut [u8] HOT 1
- README: Links to LICENSE-APACHE and LICENSE-MIT are not found
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from embedded-hal.