2022-07-12 22:41:36 -04:00
|
|
|
| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 |
|
2022-05-20 05:50:08 -04:00
|
|
|
| ----------------- | ----- | -------- | -------- | -------- |
|
|
|
|
|
2021-01-14 00:52:36 -05:00
|
|
|
# Example: C++ SPI sensor read for MCU9250 inertial/giroscope sensor
|
|
|
|
|
|
|
|
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
|
|
|
|
|
|
|
This example demonstrates usage of C++ SPI classes in ESP-IDF to read the `WHO_AM_I` register of the sensor.
|
|
|
|
|
2022-05-20 05:50:08 -04:00
|
|
|
In this example, the `sdkconfig.defaults` file sets the `CONFIG_COMPILER_CXX_EXCEPTIONS` option.
|
2021-01-14 00:52:36 -05:00
|
|
|
This enables both compile time support (`-fexceptions` compiler flag) and run-time support for C++ exception handling.
|
|
|
|
This is necessary for the C++ SPI API.
|
|
|
|
|
|
|
|
## How to use example
|
|
|
|
|
|
|
|
### Hardware Required
|
|
|
|
|
|
|
|
An MCU9250 sensor and any commonly available ESP32 development board.
|
|
|
|
|
|
|
|
### Configure the project
|
|
|
|
|
|
|
|
```
|
|
|
|
idf.py menuconfig
|
|
|
|
```
|
|
|
|
|
|
|
|
### Build and Flash
|
|
|
|
|
|
|
|
```
|
|
|
|
idf.py -p PORT flash monitor
|
|
|
|
```
|
|
|
|
|
|
|
|
(Replace PORT with the name of the serial port.)
|
|
|
|
|
|
|
|
(To exit the serial monitor, type ``Ctrl-]``.)
|
|
|
|
|
|
|
|
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
|
|
|
|
|
|
|
## Example Output
|
|
|
|
|
|
|
|
If the sensor is read correctly:
|
|
|
|
|
|
|
|
```
|
|
|
|
...
|
|
|
|
I (0) cpu_start: Starting scheduler on APP CPU.
|
|
|
|
Result of WHO_AM_I register: 0x71
|
|
|
|
I (437) gpio: GPIO[23]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
|
|
|
I (447) gpio: GPIO[25]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
|
|
|
I (457) gpio: GPIO[26]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
|
|
|
I (467) gpio: GPIO[27]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
|
|
|
|
|
|
|
Done
|
|
|
|
```
|
|
|
|
|
|
|
|
If there's an error with the SPI peripheral:
|
|
|
|
```
|
|
|
|
...
|
|
|
|
I (0) cpu_start: Starting scheduler on APP CPU.
|
|
|
|
E (434) spi: spicommon_bus_initialize_io(429): mosi not valid
|
2021-11-16 21:29:50 -05:00
|
|
|
Couldn't read SPI!
|
2021-01-14 00:52:36 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
If the SPI pins are not connected properly, the resulting read may just return 0, this error can not be detected:
|
|
|
|
```
|
|
|
|
...
|
|
|
|
I (0) cpu_start: Starting scheduler on APP CPU.
|
|
|
|
Result of WHO_AM_I register: 0x00
|
|
|
|
I (437) gpio: GPIO[23]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
|
|
|
I (447) gpio: GPIO[25]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
|
|
|
I (457) gpio: GPIO[26]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
|
|
|
I (467) gpio: GPIO[27]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
|
|
|
```
|