What's the purpose of this project?
In this section, we are going to launch the IIM42351 sensor using ARM microcontroller, STM32F series. In order to use more conveniently and optimally in this project, we use two ready modules GB309A and GebraBit STM32F303. These two modules contain the minimum necessary elements of the IIM42351 sensor and the STM32F microcontroller, which are provided by the GebraBit team to facilitate the work.
What are we going to learn in this tutorial?
In this tutorial, in addition to setting up and using the IIM42351 sensor, you will get to know all the IIM42351 sensor registers, how to set the various parts of the STM32 microcontroller to set up this sensor using the SPI protocol, how to use the GB309A module specific library and driver file. You will also learn how to declare functions and finally receive sensor data in the Keil compiler.
What do we need to start this project?
As you probably know, we need some hardware and software to do this project. The titles of these hardware and software are provided to you in the table below and you can prepare/download by clicking on each of them and get ready to start.
Required hardware
|
Required software
|
---|---|
Keil compiler
|
|
STM32CubeMX program
|
|
ST-LINK/V2 programmer
|
To communicate via the SPI protocol, first, the SPI communication protocol must be selected using the on-board selector jumpers, then we place it as Pin To Pin on the GebraBit STM32F303 module as shown below:
Note: Please note that the above image is only for showing how the GebraBit IIM42351 module is placed as Pin to Pin on the GebraBit STM32F303 module, and users must choose the correct state of the on-board selector jumpers to use the SPI communication protocol.
at the end, we will see the values of temperature and acceleration in the three “X, Y, Z” axes in real time in the “Watch1” window of the Keil compiler in “Debug Session” mode.
STM32CubeMX settings
In the following, we will explain the settings related to each of the I2C, RCC, Debug, and Clock sections in the STM32F303 microcontroller to set up the GebraBit IAM20380 module.
RCC settings
Given that there is an 8Mhz crystal in the GebraBit STM32F303 module, we select the external clock in the “RCC” section:
Debug & Programming settings
Regarding the access to “SWCLK” and “SWDIO” pins in the GebraBit STM32F303 module, to reduce the number of pins during “Debug & Programming”, in the “SYS” block, we select the “Serial Wire” option in the “Debug” section:
SPI settings
now what we need to do to communicate with the GebraBit STM32F303 module via SPI, is to select the “Full Duplex Master” mode and select the PB3, PB4, and PB5 pins as SCK, MISO and MOSI and define the PC13 pin as CS:
According to the sensor data sheet, the settings of the SPI parameters in the “Parameter Settings” section will be set as shown in the above image.
Clock settings
In the below image, you can see the clock settings for each part of the STM32F303 microcontroller in this code:
Project Manager settings
“Project Manager” settings are as follows, here we have used “MDK-ARM” version “5.32” compiler:
Once all these settings have been completed, simply click on the ‘GENERATE CODE’ button and you will be able to create a code in as little as one click by adding an IIM42351 library and driver provided by GebraBit. You can download the “STM32Cube MX”, “IIM42351 library”, “driver” and KEIL project at the end of this tutorial.
IIM42351 library and driver
In addition to the modular design of various sensors and ICs, GebraBit tries to provide variety of structured and hardware-independent libraries in C language for the ease of users in setting up and developing software.
For this purpose, after preparing each GebraBit module, the users can refer to the “toturial” section of the desired module and download the dedicated library, which contains the “ .h” and “ .c” file (Header and Source) and a sample training program under “GebraBit STM32F303”, “GebraBit ATMEGA32A” or “Arduino” development boards.
All the defined functions and structures in the library are commented in full detail and all the received parameters in the arguments of the functions and their return values, are briefly explained. Since the libraries are hardware independed. , the user can easily add the library in any of their favorite compilers and develop it by desired microcontroller and development board.
GebraBit IIM42351.h header file
In GebraBit IIM42351.h header file, based on the datasheet of the sensor or IC, all address registers, the values of each register are defined in the form of “Enumeration”. Also, the casing of the IIM42351 sensor and the configurations related to each of the IIM42351 sensor internal blocks are defined in the form of a “STRUCT” with the name GebraBit_IIM42351. Finally, in the Debug Session environment, all the configurations related to each block can be seen in real time.
IIM42351_Bank_Sel Enum
The sensor internal registry banks are defined in this enum:
typedef enum bank_sel
{
BANK_0 = 0 ,
BANK_1 ,
BANK_2 ,
BANK_3 ,
BANK_4
}IIM42351_Bank_Sel;
IIM42351_Interface Enum
This enum is used to select the communication protocol with the sensor:
typedef enum interface
{
NOT_SPI = 0 ,
IS_SPI
}IIM42351_Interface;
IIM42351_Soft_Reset_Config Enum
This enum is used for software reset of the sensor:
typedef enum Soft_Reset_Config
{
IIM42351_RESET = 0x01,
IIM42351_NOT_RESET = 0x00,
} IIM42351_Soft_Reset_Config;
IIM42351_PIN9_FUNCTION Enum
This enum is used for pin number 9 function settings are defined in this structure:
typedef enum Pin9_Function
{
INT2 = 0,
FSYNC = 1,
CLKIN = 2
} IIM42351_PIN9_FUNCTION;
IIM42351_Accel_Fs_Sel Enum
This enum is used to set the Full Scale Range of the sensor:
typedef enum accel_fs_sel
{
FS_16g = 0 ,
FS_8g ,
FS_4g ,
FS_2g
}IIM42351_Accel_Fs_Sel;
IIM42351_Accel_Scale_Factor Enum
The Scale Factor values corresponding to Full Scale Range are defined in this enum:
typedef enum Scale_Factor
{
SCALE_FACTOR_2048_LSB_g = 2048 ,
SCALE_FACTOR_4096_LSB_g = 4096 ,
SCALE_FACTOR_8192_LSB_g = 8192 ,
SCALE_FACTOR_16384_LSB_g = 16384
}IIM42351_Accel_Scale_Factor;
IIM42351_Accel_ODR Enum
typedef enum accel_odr
{
ODR_8KHz = 3,
ODR_4KHz = 4,
ODR_2KHz = 5,
ODR_1KHz = 6,
ODR_200Hz = 7,
ODR_100Hz = 8,
ODR_50Hz = 9,
ODR_25Hz = 10,
ODR_12Hz5 = 11,
ODR_6Hz25 = 12,
ODR_3Hz125 = 13,
ODR_1Hz5625 = 14,
ODR_500Hz = 15
}IIM42351_Accel_ODR;
IIM42351_FIFO_MODE Enum
The FIFO working mode of the sensor is set by using the values of this enum:
typedef enum FIFO_Config
{
BYPASS = 0 ,
STREAM_TO_FIFO ,
STOP_ON_FULL
}IIM42351_FIFO_MODE ;
IIM42351_Ability Enum
The values of this enum are used to activate and deactivate different parts of the sensor:
typedef enum Ability
{
Disable = 0 ,
Enable
}IIM42351_Ability;
IIM42351_Data_Endian Enum
The values of this enum are used to specify the data format in FIFO:
typedef enum
{
LITTLE = 0 ,
BIG
}IIM42351_Data_Endian;
IIM42351_Timestamp_Resolution Enum
To determine the sensor “Time Stamp” resolution, the values of this enum are set:
typedef enum timestamp_resolution
{
_1_uS = 0 ,
_16_uS
} IIM42351_Timestamp_Resolution;
IIM42351_FIFO_Counting Enum
typedef enum
{
IN_BYTES = 0 ,
IN_RECORDS
}IIM42351_FIFO_Counting;
IIM42351_UI_Filter_Order Enum
typedef enum UI_Filter_Order
{
_1_ORDER = 0 ,
_2_ORDER ,
_3_ORDER
}IIM42351_UI_Filter_Order ;
IIM42351_Power_Mode Enum
The values of this enum are used to set the Power Mode of the sensor:
typedef enum Power_Mode
{
IIM42351_LOW_NOISE = 0x03,
IIM42351_LOW_POWER = 0x02,
IIM42351_ACCEL_OFF = 0x01
} IIM42351_Power_Mode;
IIM42351_Low_Noise_Filter_BW Enum
typedef enum
{
LN_FILTER_BW_40 = 0x7 ,
LN_FILTER_BW_20 = 0x6 ,
LN_FILTER_BW_16 = 0x5 ,
LN_FILTER_BW_10 = 0x4 ,
LN_FILTER_BW_8 = 0x3 ,
LN_FILTER_BW_5 = 0x2 ,
LN_FILTER_BW_4 = 0x1 ,
LN_FILTER_BW_2 = 0x0
} IIM42351_Low_Noise_Filter_BW;
IIM42351_ Low_Power_Filter_AVG Enum
The values of this enum are used to determine the filter used in the sensor in Low Power mode:
typedef enum
{
LP_1x_AVG_FILTER = 0x1 ,
LP_16x_AVG_FILTER = 0x6
} IIM42351_Low_Power_Filter_AVG;
IIM42351_Preparation Enum
typedef enum Preparation
{
IS_NOT_Ready = 0 ,
IS_Ready
}IIM42351_Preparation;
IIM42351_Reset_Status Enum
typedef enum Reset_Status
{
FAILED = 0 ,
DONE
}IIM42351_Reset_Status;
GebraBit_IIM42351 structure
Declaration of functions
At the end of this file, all the functions for reading and writing in IIM42351 registers, sensor configuration , FIFO and receiving data from the sensor are declared:
/********************************************************
*Declare Read&Write IIM42351 Register Values Functions *
********************************************************/
extern uint8_t GB_IIM42351_Read_Reg_Data ( uint8_t regAddr, IIM42351_Bank_Sel regBank, uint8_t* data);
extern uint8_t GB_IIM42351_Read_Reg_Bits (uint8_t regAddr, IIM42351_Bank_Sel regBank, uint8_t start_bit, uint8_t len, uint8_t* data);
extern uint8_t GB_IIM42351_Burst_Read(uint8_t regAddr, IIM42351_Bank_Sel regBank, uint8_t *data, uint16_t byteQuantity);
extern uint8_t GB_IIM42351_Write_Reg_Data(uint8_t regAddr, IIM42351_Bank_Sel regBank, uint8_t data);
extern uint8_t GB_IIM42351_Write_Reg_Bits(uint8_t regAddr, IIM42351_Bank_Sel regBank, uint8_t start_bit, uint8_t len, uint8_t data);
extern uint8_t GB_IIM42351_Burst_Write ( uint8_t regAddr, IIM42351_Bank_Sel regBank, uint8_t *data, uint16_t byteQuantity);
/********************************************************
* Declare IIM42351 Configuration Functions *
********************************************************/
extern void GB_IIM42351_Bank_Selection( IIM42351_Bank_Sel bsel);
extern void GB_IIM42351_Who_am_I(GebraBit_IIM42351 * iim42351);
extern void GB_IIM42351_Select_SPI4_Interface( IIM42351_Interface spisel);
extern void GB_IIM42351_Select_PIN9_Function( IIM42351_PIN9_FUNCTION pin9f);
extern void GB_IIM42351_DISABLE_FSYNC ( IIM42351_Ability able ) ;
extern void GB_IIM42351_DISABLE_RTC_Mode ( void ) ;
extern void GB_IIM42351_SET_Time_Stamp_Register(IIM42351_Ability ability);
extern void GB_IIM42351_Set_Timestamp_Resolution ( IIM42351_Timestamp_Resolution res ) ;
extern void GB_IIM42351_SET_INT_ASYNC_RESET_ZERO(void );
/********************************************************
* Declare IIM42351 FIFO Functions *
********************************************************/
extern void GB_IIM42351_Set_FIFO_MODE ( IIM42351_FIFO_MODE mode ) ;
extern void GB_IIM42351_SET_FIFO_Count ( IIM42351_FIFO_Counting counting , IIM42351_Data_Endian endian ) ;
extern void GB_IIM42351_SET_AllPackets_To_FIFO( IIM42351_Ability allpack);
extern void GB_IIM42351_SET_FIFO_WATERMARK (IIM42351_Ability watermark , uint16_t wm );
extern void GB_IIM42351_SET_FIFO_Decimation_Factor (uint8_t factor );
extern void GB_IIM42351_FIFO_Configuration ( GebraBit_IIM42351 * iim42351 ) ;
extern void GB_IIM42351_SET_FIFO_High_Resolution( IIM42351_Ability highres);
/********************************************************
* Declare IIM42351 ACCEL Functions *
********************************************************/
extern void GB_IIM42351_Set_ACCEL_FS ( GebraBit_IIM42351 * iim42351 , IIM42351_Accel_Fs_Sel fs ) ;
extern void GB_IIM42351_Set_ACCEL_ODR ( IIM42351_Accel_ODR odr ) ;
extern void GB_IIM42351_UI_Filter_Order ( IIM42351_UI_Filter_Order order ) ;
extern void GB_IIM42351_ACCEL_LN_Filter_Configuration( IIM42351_Low_Noise_Filter_BW filter);
extern void GB_IIM42351_ACCEL_LP_Filter_Configuration( IIM42351_Low_Power_Filter_AVG filter);
extern void GB_IIM42351_Set_Power_Management(GebraBit_IIM42351 * iim42351);
extern void GB_IIM42351_SET_Data_Ready_Interrupt(IIM42351_Ability ability);
extern IIM42351_Preparation GB_IIM42351_Check_Data_Preparation(GebraBit_IIM42351 * iim42351);
extern void GB_IIM42351_Get_Sensor_Data_Endian ( IIM42351_Data_Endian * data_end ) ;
/********************************************************
* Declare IIM42351 DATA Functions *
********************************************************/
extern void GB_IIM42351_Get_Temp_Register_Raw_Data(GebraBit_IIM42351 * iim42351);
extern void GB_IIM42351_Get_Temp_Register_Valid_Data(GebraBit_IIM42351 * iim42351);
extern void GB_IIM42351_Get_ACCEL_DATA_X_Register_Raw(GebraBit_IIM42351 * iim42351);
extern void GB_IIM42351_Get_ACCEL_DATA_Y_Register_Raw(GebraBit_IIM42351 * iim42351);
extern void GB_IIM42351_Get_ACCEL_DATA_Z_Register_Raw(GebraBit_IIM42351 * iim42351);
extern void GB_IIM42351_Get_ACCEL_DATA_X_Register_Valid_Data(GebraBit_IIM42351 * iim42351);
extern void GB_IIM42351_Get_ACCEL_DATA_Y_Register_Valid_Data(GebraBit_IIM42351 * iim42351);
extern void GB_IIM42351_Get_ACCEL_DATA_Z_Register_Valid_Data(GebraBit_IIM42351 * iim42351);
extern void GB_IIM42351_Get_Temperature(GebraBit_IIM42351 * iim42351);
extern void GB_IIM42351_Get_XYZ_ACCELERATION(GebraBit_IIM42351 * iim42351);
extern void GB_IIM42351_Get_Data(GebraBit_IIM42351 * iim42351);
/********************************************************
* Declare IIM42351 HIGH LEVEL Functions *
********************************************************/
extern void GB_IIM42351_Format_Data_Base_On_Endian(GebraBit_IIM42351 * iim42351, const uint8_t *datain, uint16_t *dataout);
extern void GB_IIM42351_Soft_Reset ( GebraBit_IIM42351 * iim42351 );
extern void GB_IIM42351_initialize( GebraBit_IIM42351 * iim42351 );
extern void GB_IIM42351_Configuration(GebraBit_IIM42351 * iim42351);
GebraBit_IIM42351.c source file
The GebraBit_IIM42351.c source file is written in C language and all the functions are commented in full detail, and all the parameters received in the arguments of the functions and their return values are clearly explained so we confine to these explanations and invite users to check this file directly for more information.
Sample program in Keil
After making the Keil project using STM32CubeMX and adding the GebraBit_IIM42351.c library provided by GebraBit, we are going to examine the main part of the sample training program, the main.c file and view the output of the GebraBit IIM42351 module in the watch part of the Debugging environment of the Keil program.
Description of “main.c” file
If you look carefully at the beginning part of the “main.c” file, you will notice that the “GebraBit_IIM42351.h” header has been added to access the structures and functions required by the GebraBit IIM42351 module. In the next part, a variable named IIM42351_Module of the GebraBit_IIM42351 structure type (this structure is in the GebraBit_IIM42351 header and is explained in the GebraBit_IIM42351 library description section) is defined for the configuration of the GebraBit IIM42351 module:
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
GebraBit_IIM42351 IIM42351_Module;
/* USER CODE END PTD */
In the next part of the written code, the configuration and settings of the GebraBit IIM42351 module are done by using the “IIM42351_Module” structure . finally, the GebraBit IIM42351 module is configured by referencing the IIM42351_Module structure to the arguments of the GB_IIM42351_initialize() and GB_IIM42351_Configuration() functions:
GB_IIM42351_initialize( &IIM42351_Module );
GB_IIM42351_Configuration(&IIM42351_Module);
at last, in the “while” part of the program, the values of GebraBit IIM42351 module temperature and 3 axes (X, Y, Z) are received continuously:
GB_IIM42351_Get_Data( &IIM42351_Module )
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
//#include "i2c.h"
#include "spi.h"
#include "gpio.h"
#include "GebraBit_IIM42351.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
GebraBit_IIM42351 IIM42351_Module;
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
IIM42351_Module.Bank_Sel = BANK_0 ;
IIM42351_Module.INT_ASYNC_RESET = 0 ;
IIM42351_Module.Interface = IS_SPI ;
IIM42351_Module.Pin9_Function = FSYNC ;
IIM42351_Module.FSYNC = Disable;
IIM42351_Module.RTC_Mode = Disable ;
IIM42351_Module.ACCEL_FS_SEL = FS_4g ;
IIM42351_Module.ACCEL_ODR = ODR_1KHz ;
IIM42351_Module.UI_FILTER_ORDER = _2_ORDER ;
IIM42351_Module.Power_Mode = IIM42351_LOW_NOISE ;
IIM42351_Module.LN_Filter_BW = LN_FILTER_BW_4 ;
IIM42351_Module.Data_Ready_INT = Enable ;
//IIM42351_Module.SENSOR_DATA_ENDIAN = BIG ;
IIM42351_Module.FIFO_STREAM = Disable ;
IIM42351_Module.FIFO_MODE = BYPASS ;
IIM42351_Module.AllPackets_To_FIFO = Disable ;
IIM42351_Module.FIFO_WATERMARK = Disable ;
//IIM42351_Module.VALID_ACCEL_DATA_Z = 0.0f ;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
//MX_I2C1_Init();
MX_SPI1_Init();
GB_IIM42351_initialize( &IIM42351_Module );
GB_IIM42351_Configuration(&IIM42351_Module);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
GB_IIM42351_Get_Data( &IIM42351_Module ) ;
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_SYSCLK;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
Program output
After generating the Keil project using STM32CubeMX and adding the library, we connect the STLINK V2 programmer to the GebraBit STM32F303 using the STLINKV2 adapter:
STLINKV2 adapter:
By connecting the STLINK V2 programmer to the GebraBit STM32F303, there is no need to apply power to the GebraBit STM32F303 and GebraBit IIM42351 modules, because they receive their supply voltage directly from the STLINK V2 programmer.
finally, enter the “Debug” mode and by adding the “IIM42351_Module” to the “watch” window and running the program, we can see the changes of temperature and the GebraBit IIM42351 module values in the 3 axes( X, Y, Z) :
In the following, you can download the “GebraBit IIM42351 module setup project” using the GebraBit STM32F303 module in the Keil environment, the “STM32CubeMX file”, the schematic of the modules and the “IIM42351 datasheet”.