In this guide series, we shall see how to create library for SPI sensor based to acquire data from MPU9250.
In part 1, we shall cover the following:
- Gathering the required data.
- Connection.
- STM32CubeIDE setup.
1. Gathering the Required Data:
The MPU9250 has two documentation, first one is the specification and the other one is the register map.
First we can gather the feature as following:
Gyroscope:
- Angular rate from 250 to 2000 degrees per seconds for all three axes (x, y and z).
- 16-bit ADC.
- Factory calibrated.
- Selftest.
Accelerometer:
- Acceleration from 2g all the way to 16g for all three axis.
- 16-bit ADC.
- Wakeup on motion.
- Selftest mode.
From additional features:
We can find that the supply voltage range from 2.4 to 3.6 which is the same range of STM32F4.
1MHZ SPI speed interface. Hence, we shall limit the SPI speed to 1MHz.
From SPI section:
We can find the following:
- MSB for SPI.
- Maximum clock is 1MHz.
- Requires special bits for multiple byte mode and read/operation (Will be handled in part 2).
Hence, we can conclude that the sensor requires the following:
- MCU to operate in SPI mode with maximum frequency of 1MHz.
- SPI mode is Mode0.
- Extra pin for CS line.
2. Connection:
The connection as following:
Since STM32F411 Nucleo64 has arduino pinout, we shall use the D13, D12 and D11 for SPI communication, to find which pins they are, we can refer to Nucleo-64 user manual, we can find the following:
Hence, the connection as following:
STM32F411 Nucleo-64 | MPU9250 Module |
5V | Vcc |
GND | GND |
PA0 | nCS |
PA5 (SCK) | SCL |
PA7 (MOSI) | SDA |
PA6 (MISO) | AD0 |
3. STM32CubeIDE Configuration:
Open STM32CubeIDE after selecting the workspace and create new project as following:
Select the MCU:
Give the project a name:
When this window appears:
Set the following:
- PA0 as GPIO Output.
- PA2 and PA3 for UART2 to print the results.
- PA5, PA6 and PA7 for SPI1.
Next from connectivity, select SPI1 as following:
Set the mode to Full-Duplex Master mode.
This will convert the PA5, PA6 and PA7 from yellow to green.
Next from parameter settings, set the prescaler to 16 as following:
This will provide 1MHz clock signal that needed by MPU9250.
Next, enable UART2 as following:
Keep everything to default settings.
From Project Manager tab, select Code Generation and enable Generate peripheral initialization as pair of .c/.h files per peripheral as following:
Thats all for the configuration.
Save the project and this will generate the project.
In part 2, we start developing the library.
Happy coding 😉
Add Comment