For IOT (Internet of Things) solution, it often uses accelerometer sensor like IMU (Inertial Measurement Unit). The EFR32BG22 Thunderboard (kit SLTB010A) has an onboard 6-axis ICM-20648 sensor from vendor TDK InvenSense.
This lab shows how to add this accelerometer sensor quickly.
Topic Covered
- Accelerometer (Inertial Measurement Unit) sensor
- Software component
- EFR connect app.
Review the following materials before starting this lab. Ensure that you have the correct hardware and software prepared to successfully complete the lab.
-
Silicon Labs BG22 Thunderboard Kit: SLTB010A
Which included:
- EFR32BG22C224F512IM40 Soc
- 6-axis IMU (Inertial Measurement Unit): TDK InvenSense ICM-20648 with SPI interface
-
A mobile device for installing EFR Connect Mobile App: Android phone/pad or Apple phone/pad
- Simplicity Studio v5
- Gecko SDK v3.1 (GSDK) or above with the Bluetooth Stack (v3.1.1) installed
- EFR Connect Mobile App
Download and install Simplicity Studio v5 if it is not already installed. Be sure to update to have GSDK 3.1.x and Bluetooth Stack installed.
Attach the BG Thunderboard kit to the PC with Simplicity Studio installed by using a USB cable (not a charging cable). Connecting between the PC host USB port to the J-link USB port (micro) on the kit.
Refer to EFR32BG22 thunderboard schemtic for detailed hardware information (Schematic for BG22 Thunderboard in pdf format).
Pins used to connect between the EFR32BG22 and IMU:
- SPI interface:
- SPI_MOSI (PC00)
- SPI_MISO (PC01)
- SPI_SCLK (PC02)
- SPI_CS (PB02)
- IMU_ENABLE (PB04)
- IMU_INT (PB03)
- If the BG22 Thunderboard has not been plugged into PC using the USB cable (micro type), do so now.
- In the "Debug Adapters window", click on the Thunderboard EFR32BG22 (board). The kit and debug information (board and target) should be displayed in the Launcher->Debug Adapters window.
- Information about the target hardware and software will appear in Launcher -> Overview tab (together with the Adapter FW and Secure FW version). If this does not appear, click on the Launcher button in the top right corner. Note: If the Secure FW showed as Unknown, you could click Read FW Version on the right side of it to get the version. You may also upgrade the Adapter FW to the latest if it was not the latest yet.
- Select the "Preferred SDK" to the latest version. For this lab, the latest version of "Gecko SDK Suite" v3.1.1 is used.
- Click on "Create New Project" in the upper right hand corner. A "New Project Wizard" window should appear.
- For this lab, the Bluetooth -> SoC Empty project will be used as the starter project. Scroll and select Bluetooth -> SoC Empty. Note: to filter the projects, Select/Checked the Bluetooth for the "Technology Type" and empty for "keywords".
- Click Next to move on.
- Rename the project under "Project name". For this lab, name the project soc_spi_acc.
- Select (check) "Copy contents" under "With Project files" to copy the project files into your project. This makes version control easier to manage and future updates to the Simplicity Studio libraries will not impact the copied files in this project.
- Check "Use default location" (workspace).
- Click Finish to generate the project.
- The IDE perspective launchered automatically.
- You could see gatt_configuration.btconf, soc_spi_acc.slcp and readme showed up.
In fact, the SOC Empty project will install some software components. You could check what has installed via checking the "Installed Components" under "Software Components", you will see component like below were installed:
- Advanced Configurator -> Bluetooth GATT Configurator
- Bluethooth -> OTA -> AppLoader,
- Platform -> services->Sleep Timer
- etc.
- Select the Software Components tab at the top.
- Scroll down to the "Platform" section. Notice how there are many components available that you can install for your application with ease.
- Install the following components using the Install button as shown in the image. The process is repeated for all components needed to add.
- Services -> IO stream ->IO Stream: USART (dependent).
- Platform -> Board Driver -> IMU - Inertial Measurement Unit
- Bluetooth -> GATT -> Inertial Measurement Unit GATT Service.
- Bluetooth -> Sensor -> Inertial Measurement Unit Sensor.
After you add/install the motion sensor component, you will see some files was added. For example, sl_icm20648.c and sl_icm20648.h will be added.
After you add/install the Inertial Measurement Unit GATT Service, you will see somes other files was added.
If you open the config -> btconf -> gatt_configuration.btcon gatt configure file, you could see the Acceleration and Orientation was added.
In file sl_event_handler.c, you could see the API sl_gatt_service_imu_step was added into the routine sl_internal_app_process_action.
void sl_internal_app_process_action(void)
{
sl_gatt_service_imu_step();
}
Note: UUID for Acceleration and Orientation could be attained in this step.
- Copy app.c source file to the top level of the project. The source files (automatically added included) and code details are found at the Code Explanation section of this doc. app.c will overwrite the existing file to add the new application. The source files can be dragged and dropped into Simplicity Studio or placed in this file path.
C:\Users\user_acount\SimplicityStudio\v5_workshop\soc_spi_acc
Where user_acount is the default workspace and SS installation path.
You can also edit the app.c file manually if you prefer to this way.
- Build the project by clicking on the hammer icon in the top left corner of the Simplicity Studio IDE perspective. Right-click on the hex file and select Flash to Device... to make the Flash Programmer window appear. Note: if a Device Selection window appears, select the correct device.
- Click Program to flash the device. Note: The BG22 has additional security features and in some cases (i.e., when the board is first plugged in), the tools will prompt to query the Debug Challenge Interface (DCI). Select the connected device and then the link for “Click to Query Lock Status.” The device target to program text will no longer be grayed out and then select “OK.”
- With the EFR Connect App, connect to the device and view the sensor data that is sent from the EFR32BG22 device (via notification).
- Click the notify button (UUID that has 885D and 45DD).
- You should see the sensor data get updated regularly. You could change the orientation of the Thunderboard to see this change.
The following sections explain critical lines of code pertinent to this lab. The code can be found in different files (driver).
This is a header file generated automatically by the Simplicity Studio pintool/software component. You may need to change the pin map based on your hardware.
Use the software components->Platform->Board drivers->ICM20648->Configure to change this.
C:\SiliconLabs\SimplicityStudio\v5\developer\sdks\gecko_sdk_suite\v3.1\hardware\driver\icm20648\src This is the driver file for ICM20648 prepared by Silabs. If you use sensor from another vendor, you may need to consider implementing the similar driver for it. Please consider contacting with the vendor of the sensor you use to get help on how implementing the driver.
These are re-pack of the API provided in sl_icm20648.c. The high-level code (app.c) call API like sl_sensor_imu_init, sl_sensor_imu_get and others provided in these files to initialize and enable/disable the imu sensor. These files are in folder like below: C:\SiliconLabs\SimplicityStudio\v5\developer\sdks\gecko_sdk_suite\v3.1\hardware\driver\imu\src C:\SiliconLabs\SimplicityStudio\v5\developer\sdks\gecko_sdk_suite\v3.1\app\bluetooth\common\sensor_imu
The SoC-Empty project generates a default app.c source file with a skeleton Bluetooth event handler. The app.c file provided for this lab adds code to handle the BLE connection and notifications.
The imu sensor is initialized and enabled when event sl_bt_evt_connection_opened_id received. The imu sampling does not start sampling until a connection has been made and the user has enabled GATT notifications acceleration (or orientation) characteristics.
static void sensor_init(void)
{
sl_sensor_imu_init();
sl_sensor_imu_enable(true);
}
// -------------------------------
// This event indicates that a new connection was opened.
case sl_bt_evt_connection_opened_id:
sensor_init();
break;
When the connection is closed, the sl_bt_evt_connection_closed_id event is triggered, to save power when no devices are connected the sensor was disabled via sensor_deinit() function.
// -------------------------------
// This event indicates that a connection was closed.
case sl_bt_evt_connection_closed_id:
// Restart advertising after client has disconnected.
sc = sl_bt_advertiser_start(
advertising_set_handle,
advertiser_general_discoverable,
advertiser_connectable_scannable);
sl_app_assert(sc == SL_STATUS_OK,
"[E: 0x%04x] Failed to start advertising\n",
(int)sc);
break;
Once the user has enabled GATT notifications to the characteristic, the sl_bt_evt_gatt_server_characteristic_status_id event is triggered. In this event, the device will periodically update the characteristic value until the device disconnects.
This file was automatically generated and was in folder (workspace) here:
C:\Users\delu\SimplicityStudio\v5_workshop\soc_spi_acc\autogen
void sl_internal_app_process_action(void)
{
sl_gatt_service_imu_step();
}
This file was in folder like below: C:\SiliconLabs\SimplicityStudio\v5\developer\sdks\gecko_sdk_suite\v3.1\app\bluetooth\common\gatt_service_imu
void sl_gatt_service_imu_step(void)
{
if (imu_state) {
if (SL_STATUS_OK == sl_gatt_service_imu_get(imu_ovec, imu_avec)) {
if (imu_acceleration_notification) {
imu_acceleration_notify();
}
if (imu_orientation_notification) {
imu_orientation_notify();
}
}
}
}
BG22 Thunderboard also integrated other sensors:
- Silabs Relative humidity & temperature sensor: I2C Si7021
- Silabs UV and ambient light sensor: I2C Si1133
- Silabs Hall effect sensor: I2C Si7210
If your solution needs these sensors, you may use similar procedure to add them.
The SOC Empty project don't have the bootloader, ensure to program the bootloader to the device first. In some cases, the bootloader may be missing from the device if it has been completely erased. If that happens:
- Optional solutions:
- open the Flash Programmer and program the bootloader found here: C:\SiliconLabs\SimplicityStudio_v5\developer\sdks\gecko_sdk_suite\v3.1\platform\bootloader\sample-apps\bootloader-storage-internal-single-512k\efr32mg22c224f512im40-brd4182a\bootloader-storage-internal-single-512k.s37
- flash a demo example first (like Bluetooth SOC - thunderboard EFR32BG22), then flash the application got here.