|
|
|
|
|
It's time to try out the SPI driver that we wrote in the previous tutorial.
|
|
It's time to try out the SPI driver that we wrote in the previous tutorial.
|
|
|
|
|
|
This example uses the _LIS3DSH_ accelerometer and temperature sensor that is present on the STM32F407VG discovery board.
|
|
This example uses the _LIS3DSH_ accelerometer and temperature sensor that is present on the STM32F407VG discovery board.
|
... | @@ -8,6 +7,7 @@ Here we will only consider reading the temperature data. |
... | @@ -8,6 +7,7 @@ Here we will only consider reading the temperature data. |
|
> :information_source: **Suggestion: keep the sensor's datasheet with you while following the tutorial and try to find in it all the things that here are mentioned. This datasheet is pretty simple and it's useful to start dealing with this type of documents.**
|
|
> :information_source: **Suggestion: keep the sensor's datasheet with you while following the tutorial and try to find in it all the things that here are mentioned. This datasheet is pretty simple and it's useful to start dealing with this type of documents.**
|
|
|
|
|
|
## Header file
|
|
## Header file
|
|
|
|
|
|
We define the methods that we will be implemented in the `cpp` file:
|
|
We define the methods that we will be implemented in the `cpp` file:
|
|
- _init_: configure sensor driver. It returns a boolean indicating if the initialization was successful.
|
|
- _init_: configure sensor driver. It returns a boolean indicating if the initialization was successful.
|
|
- _checkWhoAmI_: every sensor has a special read_only register that contains a number called `who_am_i`. This method checks that the `who_am_i` value is equal to the default one specified in the sensor's datasheet (which in this case is `63`). It returns a boolean indicating if the `who_am_i` value is correct.
|
|
- _checkWhoAmI_: every sensor has a special read_only register that contains a number called `who_am_i`. This method checks that the `who_am_i` value is equal to the default one specified in the sensor's datasheet (which in this case is `63`). It returns a boolean indicating if the `who_am_i` value is correct.
|
... | @@ -18,9 +18,34 @@ We also need two other memebers: |
... | @@ -18,9 +18,34 @@ We also need two other memebers: |
|
- _BDU_: it indicates the _block data update_ mode. This can be set to continuous mode (i.e. the sensor continuously produces samples) or we can configure the sensor so that it outputs a new sample only after the previous one has been read.
|
|
- _BDU_: it indicates the _block data update_ mode. This can be set to continuous mode (i.e. the sensor continuously produces samples) or we can configure the sensor so that it outputs a new sample only after the previous one has been read.
|
|
|
|
|
|
```cpp
|
|
```cpp
|
|
|
|
/* Copyright (c) <YEAR_HERE> Skyward Experimental Rocketry
|
|
|
|
* Author: <YOUR_NAME_HERE>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
#pragma once
|
|
#pragma once
|
|
|
|
|
|
#include "SpiDriver.h"
|
|
#include <SpiDriver.h>
|
|
|
|
|
|
|
|
namespace Boardcore
|
|
|
|
{
|
|
|
|
|
|
class TempSensor
|
|
class TempSensor
|
|
{
|
|
{
|
... | @@ -43,7 +68,10 @@ private: |
... | @@ -43,7 +68,10 @@ private: |
|
const uint8_t WHO_AM_I_VALUE = 63;
|
|
const uint8_t WHO_AM_I_VALUE = 63;
|
|
const uint8_t TEMPERATURE_REF = 25;
|
|
const uint8_t TEMPERATURE_REF = 25;
|
|
};
|
|
};
|
|
|
|
|
|
|
|
} // namespace Boardcore
|
|
```
|
|
```
|
|
|
|
|
|
In order to configure the _ODR_ and _BDU_ values we can define two `enum` that contain all the possible values for those two variables. All these values can be found in the sensor's datasheet.
|
|
In order to configure the _ODR_ and _BDU_ values we can define two `enum` that contain all the possible values for those two variables. All these values can be found in the sensor's datasheet.
|
|
|
|
|
|
> :warning: **Since the default value of _ODR_ is `ODR_POWER_DOWN` (i.e. the sensor is turned off), we are obliged to configure the _ODR_ value in order for the sensor to work.**
|
|
> :warning: **Since the default value of _ODR_ is `ODR_POWER_DOWN` (i.e. the sensor is turned off), we are obliged to configure the _ODR_ value in order for the sensor to work.**
|
... | @@ -77,6 +105,7 @@ The registers that we need are: |
... | @@ -77,6 +105,7 @@ The registers that we need are: |
|
- The `CTRL_REG4` which is needed in order to configure the _ODR_ and _BDU_ values.
|
|
- The `CTRL_REG4` which is needed in order to configure the _ODR_ and _BDU_ values.
|
|
|
|
|
|
The registers addresses, of course, are specified in the sensor's datasheet.
|
|
The registers addresses, of course, are specified in the sensor's datasheet.
|
|
|
|
|
|
```cpp
|
|
```cpp
|
|
enum Registers : uint8_t
|
|
enum Registers : uint8_t
|
|
{
|
|
{
|
... | @@ -87,19 +116,48 @@ enum Registers : uint8_t |
... | @@ -87,19 +116,48 @@ enum Registers : uint8_t |
|
```
|
|
```
|
|
|
|
|
|
## Cpp file
|
|
## Cpp file
|
|
|
|
|
|
Import the header file and in the constructor simply initialize the *odr* and *bdu* members:
|
|
Import the header file and in the constructor simply initialize the *odr* and *bdu* members:
|
|
|
|
|
|
```cpp
|
|
```cpp
|
|
|
|
/* Copyright (c) <YEAR_HERE> Skyward Experimental Rocketry
|
|
|
|
* Author: <YOUR_NAME_HERE>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
#pragma once
|
|
#pragma once
|
|
|
|
|
|
#include "TempSensor.h"
|
|
#include "TempSensor.h"
|
|
|
|
|
|
|
|
namespace Boardcore
|
|
|
|
{
|
|
|
|
|
|
TempSensor::TempSensor(uint8_t odr, uint8_t bdu)
|
|
TempSensor::TempSensor(uint8_t odr, uint8_t bdu)
|
|
: odr(odr), bdu(bdu)
|
|
: odr(odr), bdu(bdu)
|
|
{
|
|
{
|
|
}
|
|
}
|
|
```
|
|
```
|
|
|
|
|
|
#### Check WHO_AM_I value
|
|
#### Check WHO_AM_I value
|
|
In order to check the `who_am_i` value we can simply ask the SPI driver to read the `WHO_AM_I` register. If the returned value is the correct one, than we can return `true` (otherwise `false`).
|
|
In order to check the `who_am_i` value we can simply ask the SPI driver to read the `WHO_AM_I` register. If the returned value is the correct one, than we can return `true` (otherwise `false`).
|
|
|
|
|
|
```cpp
|
|
```cpp
|
|
bool TempSensor::checkWhoAmI()
|
|
bool TempSensor::checkWhoAmI()
|
|
{
|
|
{
|
... | @@ -145,6 +203,7 @@ bool TempSensor::init() |
... | @@ -145,6 +203,7 @@ bool TempSensor::init() |
|
|
|
|
|
#### Read the temperature
|
|
#### Read the temperature
|
|
In order to read the temperature sample the only things we need is to read the `OUT_T` register through the SPI driver, rembering to add the `TEMPERATURE_REF` value to all the samples:
|
|
In order to read the temperature sample the only things we need is to read the `OUT_T` register through the SPI driver, rembering to add the `TEMPERATURE_REF` value to all the samples:
|
|
|
|
|
|
```cpp
|
|
```cpp
|
|
int8_t TempSensor::getTemperature()
|
|
int8_t TempSensor::getTemperature()
|
|
{
|
|
{
|
... | @@ -156,15 +215,40 @@ int8_t TempSensor::getTemperature() |
... | @@ -156,15 +215,40 @@ int8_t TempSensor::getTemperature() |
|
```
|
|
```
|
|
|
|
|
|
## Create your entrypoint
|
|
## Create your entrypoint
|
|
|
|
|
|
In `src/entrypoints` add a file called `test-tempsensor.cpp`.
|
|
In `src/entrypoints` add a file called `test-tempsensor.cpp`.
|
|
|
|
|
|
A simple entrypoint creates a `TempSensor` object and periodically reads the temperature value:
|
|
A simple entrypoint creates a `TempSensor` object and periodically reads the temperature value:
|
|
|
|
|
|
```cpp
|
|
```cpp
|
|
|
|
/* Copyright (c) <YEAR_HERE> Skyward Experimental Rocketry
|
|
|
|
* Author: <YOUR_NAME_HERE>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
#include <miosix.h>
|
|
#include <miosix.h>
|
|
#include <Common.h>
|
|
#include <Common.h>
|
|
#include "TempSensor.h"
|
|
#include "TempSensor.h"
|
|
|
|
|
|
using namespace miosix;
|
|
using namespace miosix;
|
|
|
|
using namespace Boardcore;
|
|
|
|
|
|
int main()
|
|
int main()
|
|
{
|
|
{
|
... | @@ -186,26 +270,33 @@ int main() |
... | @@ -186,26 +270,33 @@ int main() |
|
```
|
|
```
|
|
|
|
|
|
## Compile with SBS
|
|
## Compile with SBS
|
|
Let's write our dependencies section. We need `Debug.cpp` (for the _TRACE()_ function) and the source code of the SPI driver:
|
|
|
|
```sh
|
|
Let's write our dependencies variables:
|
|
[spi_driver]
|
|
|
|
Type: srcfiles
|
|
```cmake
|
|
Files: src/shared/SpiDriver.cpp
|
|
set(LIS3DSH_SENSOR_SOURCES
|
|
src/shared/Debug.cpp # for the TRACE
|
|
src/shared/SpiDriver.cpp
|
|
```
|
|
src/shared/LIS3DSH.cpp
|
|
And then specify the entrypoint section, including the dependencies:
|
|
)
|
|
```
|
|
```
|
|
[test-tempsensor]
|
|
|
|
Type: board
|
|
And then specify the entrypoint, including the dependencies variable:
|
|
BoardId: stm32f407vg_stm32f4discovery
|
|
|
|
BinName: test-tempsensor
|
|
```cmake
|
|
Include: %spi_driver
|
|
add_executable(test-tempsensor
|
|
Defines: -DDEBUG
|
|
src/entrypoints/test-tempsensor.cpp
|
|
Main: test-tempsensor
|
|
${LIS3DSH_SENSOR_SOURCES}
|
|
|
|
)
|
|
|
|
sbs_target(test-tempsensor stm32f407vg_stm32f4discovery)
|
|
```
|
|
```
|
|
|
|
|
|
## Run it!
|
|
## Run it!
|
|
Same as before: build the entrypoint with `python sbs -b test-tempsensor` and [flash the binary on the board](Flashing-on-a-Target-Board).
|
|
|
|
|
|
Same as before: you can use the SBS script to build with debug enabled and [flash](Flashing-on-a-Target-Board) the entrypoint:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
./sbs -d -f test-tempsensor
|
|
|
|
```
|
|
|
|
|
|
## References
|
|
## References
|
|
|
|
|
... | | ... | |