on 2024-09-04 04:00 AM
The article offers an in-depth demonstration of incorporating the X-CUBE-MEMS1 software package into an STM32CubeIDE project, tailored specifically for the B-U585I-IOT02A board with its onboard ISM330DHCX accelerometer.
The X-CUBE-MEMS1 expansion software package for the STM32Cube ecosystem is tailored to run on STM32 microcontrollers. The package offers drivers for sensor recognition and data collection across a diverse range of sensors. It includes sample implementations of the drivers running on the X-NUCLEO-IKS4A1, X-NUCLEO-IKS01A3, and X-NUCLEO-IKS02A1 expansion boards connected to featured STM32 Nucleo development boards. Additionally, the software provides sample applications and advanced motion libraries. This includes accelerometer calibration, airplane detection, activity recognition, and more. It also features compatibility with the Unicleo-GUI graphical user interface and offers easy portability across different MCU families, thanks to the STM32Cube environment.
The X-CUBE-MEMS1 provides comprehensive support for a wide array of sensors, including temperature, humidity, pressure, and motion. Leveraging STM32Cube software technology ensures seamless portability across various STM32 microcontroller models. The article dives into the detailed integration process of the X-CUBE-MEMS1 package to retrieve data from the ISM330DHCX accelerometer. Moreover, to subsequently transmit and display this information via the serial bus in the STM32CubeIDE environment.
Now, let us work with the STM32CubeIDE to write our application code!
Firstly, you need to create a new STM32CubeIDE project for your board. We use the B-U585I-IOT02A discovery kit in this case:
Name your project:
Choose [No] for the next pop-up “Initialize all peripherals with their default Mode?”
At the *.ioc file, we must include the X-CUBE-MEMS1 software package, which holds the necessary files and codes for configuring the accelerometer.
Go to [Software Packs] → [Manage Software Packs] and find X-CUBE-MEMS1 at the “STMicroelectronics” tab. Select the latest version available and click on the [Install] button if not downloaded already. After installing the software package, go to [Software Packs] → [Select Components] and select the accelerometer via I2the C interface. Within the Component Selector, go to [STMicroelectronics X-CUBE-MEMS1] → [Board Part AccGyr] → [ISM330DHCX] and select “I²C”, then click on the [Ok] button.
Going back to the *.ioc file [Pinout & Configuration] tab, we first need to configure the I²C. Go to the [Connectivity] tab and select the [I2C2]. At the [Parameter Settings] select the I²C speed mode as “Fast Mode”:
At the [GPIO Settings] tab, change the I²C pinout to the following pins, according to the MCU schematic:
We can now select the software package for our project. Go to [Middleware and Software Packs] → [X-CUBE-MEMS1] and check the “Board Part AccGyr” box. Then, select “I2C2” as [Found Solutions] at [Platform Settings]:
The next step is to set the ISM330DHCX interruption pin, also through the MCU schematic:
Looking for the INT1 interruption pin, we have the PE11 as the final pinout:
Then, select the PE11 pin and set it as GPIO_EXTI11. After this, go to [System Core] → [NVIC] and check the “EXTI Line11 interrupt” box:
The last peripheral to be set is the USART1, so the result can be printed through serial communication.
Go to [Connectivity] → [USART1] and configure it as the figure below:
Make sure to change the USART1 pinout as well for PA9 and PA10, like the figure below:
By the end of the configuration, the MCU graphical interface should look like the figure below:
We can now generate the code by clicking on the gear icon or by using the [Alt] + [K] keyboard shortcut.
The code is implemented within the “main.c” file. Open the “main.c” file and add the following code in each section described.
/* USER CODE BEGIN Includes */
//Accelerometer header
#include "ism330dhcx.h"
//I2C2 Bus and BSP header
#include "b_u585i_iot02a_bus.h"
//Standard IO library
#include <stdio.h>
/* USER CODE END Includes */
/* USER CODE BEGIN PD */
//Declare the accelerometer data struct
ISM330DHCX_Object_t MotionSensor;
ISM330DHCX_Axes_t acc_axes;
//Data reception Indicator
volatile uint32_t dataRdyIntReceived;
/* USER CODE END PD */
/* USER CODE BEGIN PM */
static void MEMS_Init(void);
/* USER CODE END PM */
/* USER CODE BEGIN 0 */
static void MEMS_Init(void)
{
ISM330DHCX_IO_t io_ctx;
uint8_t id;
ISM330DHCX_AxesRaw_t axes;
/* Link I2C functions to the ISM330DHCX driver */
io_ctx.BusType = ISM330DHCX_I2C_BUS;
io_ctx.Address = ISM330DHCX_I2C_ADD_H;
io_ctx.Init = BSP_I2C2_Init;
io_ctx.DeInit = BSP_I2C2_DeInit;
io_ctx.ReadReg = BSP_I2C2_ReadReg;
io_ctx.WriteReg = BSP_I2C2_WriteReg;
io_ctx.GetTick = BSP_GetTick;
ISM330DHCX_RegisterBusIO(&MotionSensor, &io_ctx);
/* Read the ISM330DHCX WHO_AM_I register */
ISM330DHCX_ReadID(&MotionSensor, &id);
if (id != ISM330DHCX_ID) {
Error_Handler();
}
/* Initialize the ISM330DHCX sensor */
ISM330DHCX_Init(&MotionSensor);
/* Configure the ISM330DHCX accelerometer (ODR, scale and interrupt) */
ISM330DHCX_ACC_SetOutputDataRate(&MotionSensor, 26.0f); /* 26 Hz */
ISM330DHCX_ACC_SetFullScale(&MotionSensor, 4); /* [-4000mg; +4000mg] */
ISM330DHCX_Set_INT1_Drdy(&MotionSensor, ENABLE); /* Enable DRDY */
ISM330DHCX_ACC_GetAxesRaw(&MotionSensor, &axes); /* Clear DRDY */
/* Start the ISM330DHCX accelerometer */
ISM330DHCX_ACC_Enable(&MotionSensor);
}
/* USER CODE END 0 */
/* USER CODE BEGIN 2 */
dataRdyIntReceived = 0;
MEMS_Init();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
ISM330DHCX_ACC_GetAxes(&MotionSensor, &acc_axes);
printf("X = %5d, Y = %5d, Z = %5d\r\n", (int) acc_axes.x, (int) acc_axes.y, (int) acc_axes.z);
}
/* USER CODE END 3 */
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == GPIO_PIN_11)
dataRdyIntReceived++;
}
int _write(int fd, char * ptr, int len)
{
HAL_UART_Transmit(&huart1, (uint8_t *) ptr, len, HAL_MAX_DELAY);
return len;
}
/* USER CODE END 4 */
After completing the code execution, you can build and run it onto your board by clicking right in the project name, then selecting: “Run As” → STM32 C/C++ Application.
To verify if the code is properly working, open the STM32CubeIDE embedded terminal or Tera Term Terminal with the following parameters: 115200 / 8 / N / 1.
By adding the software pack, initializing and configuring peripherals, and programming the firmware for the accelerometer, you can easily open the preferred serial terminal to monitor the sampled data transmission.
Integrating the X-CUBE-MEMS1 package into your STM32CubeIDE project for the B-U585I-IOT02A board is a straightforward process that significantly enhances the capabilities of your STM32 microcontroller by enabling advanced sensor functionalities. By following the detailed steps outlined in this guide, you can efficiently configure and utilize the ISM330DHCX accelerometer to collect and transmit sensor data.
The integration process involves setting up the STM32CubeIDE project, installing the necessary software packages, configuring the I²C and USART peripherals, and writing the application code to initialize and retrieve data from the accelerometer. The provided code snippets and configuration instructions ensure that you can seamlessly implement the required functionalities.
Once the setup is complete, you can validate the code by monitoring the data transmission through a serial terminal emulator like Tera Term. This comprehensive approach ensures that you can leverage the full potential of the X-CUBE-MEMS1 package to develop robust and efficient applications for your STM32-based projects.
Product page: B-U585I-IOT02A - Discovery kit for IoT node with STM32U5 series
ST Wiki: Artificial Intelligence - stm32mcu
ST Wiki: NanoEdgeAI - stm32mcu
ST Wiki: NanoEdge AI Studio - stm32mcu
Knowledge base article: How to install and import an X-CUBE Expansion Package with STM32CubeMX