|
|
This page provides documentation for our own SPI driver implementation, which you can use when writing a sensor driver. Some basic knowledge about how SPI works is required to read this page.
|
|
|
This page provides documentation for our own SPI driver implementation, which you can use when writing a sensor driver.
|
|
|
|
|
|
Some basic knowledge about how SPI works is required to read this page. You can take a look at the [Low-Level SPI](Low-Level-SPI) page to see an example.
|
|
|
|
|
|
# SPIDriver
|
|
|
Classes in the file `shared/drivers/spi/SPIDriver.h` provide both low and high level access to an SPI Bus, in order to perform *read*, *write* and *full duplex transactions* with slaves on the bus.
|
|
|
Classes in the file `shared/drivers/spi/SPIDriver.h` provide both low and high level access to an SPI Bus, in order to perform _read_, _write_ and _full duplex transactions_ with slaves on the bus.
|
|
|
Formerly, these function were provided by the infamous `BusTemplate.h`.
|
|
|
SPIDriver allow for sensors working with different bus configurations (baud rate, polarity, phase etc) to work togheter on the same bus, by appropriately reconfiguring the bus before each transaction.
|
|
|
|
... | ... | @@ -15,7 +17,7 @@ Low level access to the SPI bus is provided trough the interface SPIBusInterface |
|
|
- `read(...)` reads bytes from the bus
|
|
|
- `transfer(...)` performs a full duplex transaction, writing the provided bytes and returning the received bytes
|
|
|
### Interface implementations (SPIBus)
|
|
|
This interface is implemented primarly by the `SPIBus` class, which performs the *real* operations on a SPI bus, using the SPI peripheral of the STM32F4 micro.
|
|
|
This interface is implemented primarly by the `SPIBus` class, which performs the _real_ operations on a SPI bus, using the SPI peripheral of the STM32F4 micro.
|
|
|
The interface may be implemented by other classes, for example `MockSpiBus`, that implements a "fake" SPI bus used for unit testing.
|
|
|
The various **drivers must access the an SPI bus exclusively through the interface**. By doing this, the implementation of the driver is completely transparent to the use of a real SPI bus (`SPIBus`), a mock one (`MockSPIBus`) or other implementations: all can be used interchangeably without needing to change a single line of code in the driver.
|
|
|
### SPITransaction
|
... | ... | @@ -28,7 +30,7 @@ Aggregates all the objects needed by a driver to access a bus. |
|
|
## Examples
|
|
|
### Simple SPI sensor
|
|
|
In this example, SPIDriver in a sensor driver to configure and sample it.
|
|
|
More details about sensors can be found here: [Sensor](Sensor).
|
|
|
More details about sensors can be found in the [Sensor](Sensor) page.
|
|
|
|
|
|
#### main.cpp
|
|
|
```cpp
|
... | ... | @@ -75,8 +77,8 @@ int main() |
|
|
If not, you have to:
|
|
|
- Enable the SPI peripheral clock
|
|
|
Example: `RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;`
|
|
|
- Set `MISO`, `MOSI`, `CLOCK` gpios to the correct alternate mode
|
|
|
- Set `CS` gpio to output mode, and set its value to *high*.
|
|
|
- Set `MISO`, `MOSI`, `CLOCK` GPIOs to the correct alternate mode
|
|
|
- Set `CS` GPIO to output mode, and set its value to _high_.
|
|
|
- The `SPIBus` object is shared among all slaves on the same physical bus. It must thus be passed **by reference** to each slave.
|
|
|
- Each slave can, however, have a different bus configuration (`SPIBusConfig`).
|
|
|
#### MySensor.h
|
... | ... | |