... | ... | @@ -46,7 +46,6 @@ SPIBus bus(SPI1); |
|
|
|
|
|
GpioPin cs_mysensor(GPIOC_BASE, 1); // Chip select pin of the sensor (GPIO C1)
|
|
|
|
|
|
|
|
|
SPIBusConfig cfg_mysensor; // Bus configuration for the sensor
|
|
|
|
|
|
int main()
|
... | ... | @@ -57,16 +56,19 @@ int main() |
|
|
cfg_mysensor.cpol = 1; // Set clock polarity to 1
|
|
|
|
|
|
// Create an instance of the sensor
|
|
|
MySensor sensor{bus, cs_mysensor, cgf_mysensor};
|
|
|
MySensor sensor(bus, cs_mysensor, cgf_mysensor);
|
|
|
|
|
|
// Initialize the sensor (configures its registers)
|
|
|
sensor.init();
|
|
|
if (!sensor.init() || !sensor.selfTest())
|
|
|
{
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
// Sample and display
|
|
|
for(;;)
|
|
|
{
|
|
|
sensor.onSimpleUpdate();
|
|
|
int d = sensor.getData();
|
|
|
sensor.sample();
|
|
|
int d = sensor.getLastSample().data;
|
|
|
|
|
|
printf("Sampled: %d\n", d);
|
|
|
}
|
... | ... | @@ -87,7 +89,12 @@ If not, you have to: |
|
|
```cpp
|
|
|
#include "drivers/spi/SPIDriver.h"
|
|
|
|
|
|
class MySensor : public Sensor
|
|
|
struct MySensorData : public TimestampData
|
|
|
{
|
|
|
int data;
|
|
|
}
|
|
|
|
|
|
class MySensor : public Sensor<MySensorData>
|
|
|
{
|
|
|
public:
|
|
|
// Constructor with default bus config
|
... | ... | @@ -128,7 +135,9 @@ public: |
|
|
return true;
|
|
|
}
|
|
|
|
|
|
void onSimpleUpdate() override
|
|
|
bool selfTest() { return true; }
|
|
|
|
|
|
MySensorData sampleImpl() override
|
|
|
{
|
|
|
SPITransaction spi{spislave};
|
|
|
|
... | ... | @@ -137,9 +146,10 @@ public: |
|
|
|
|
|
// Store into data
|
|
|
data = buf[0] || buf[1] << 8;
|
|
|
|
|
|
return MySensorData{data};
|
|
|
}
|
|
|
|
|
|
int getData() { return data; }
|
|
|
private:
|
|
|
SPISlave spislave;
|
|
|
|
... | ... | @@ -150,15 +160,15 @@ private: |
|
|
- It is good practice to provide 2 constructors: one which initializes the `SPIBusConfig` with the sensor's default parameters, the other that accepts a custom `SPIBusConfig` from outside.
|
|
|
- Constructors must accept a **reference** to `SPIBusInterface`. **Do not** pass it by value (it will not compile) and **do not** use `SPIBus` instead of `SPIBusInterface`, to allow for greater flexibility.
|
|
|
- `SPITransaction` must be instantiated inside a method scope. Do not instantiate and store it at class level (eg, do not store it in a local class variable).
|
|
|
- Reason: other sensor may access the bus (and change its configuration) between calls to onSimpleUpdate() (or other methods that access the bus), so the bus must be reconfigured by instantiation a new `SPITransaction` before accessing it.
|
|
|
- Reason: other sensor may access the bus (and change its configuration) between calls to `sample()` (or other methods that access the bus), so the bus must be reconfigured by instantiation a new `SPITransaction` before accessing it.
|
|
|
|
|
|
### Low level acccess through SPIBusInterface
|
|
|
In some cases, it may be necessary to access the bus directly via the low level methods provided by `SPIBusInterface` (eg. if methods provided by `SPITransaction` are not suitable to communicate with a particular sensor).
|
|
|
Here, the `onSimpleUpdate()` method from the previous example is modified to do so.
|
|
|
Here, the `sampleImpl()` method from the previous example is modified to do so.
|
|
|
```cpp
|
|
|
// ...
|
|
|
// Same as before, but using directly SPIBusInterface
|
|
|
void onSimpleUpdate() override
|
|
|
MySensorData sampleImpl() override
|
|
|
{
|
|
|
splislave.bus.configure(spislave.config); // Configure the bus before accessing it
|
|
|
|
... | ... | @@ -174,6 +184,8 @@ void onSimpleUpdate() override |
|
|
|
|
|
// Store into data
|
|
|
data = buf[0] || buf[1] << 8;
|
|
|
|
|
|
return MySensorData{data};
|
|
|
}
|
|
|
// ...
|
|
|
|
... | ... | |