GebraBit

ICM20948 Sensor Project With STM32F303 Microcontroller

متن سربرگ خود را وارد کنید

ICM20948 gebrabit project

ICM20948 Sensor Project With STM32F303 Microcontroller

ICM20948 gebrabit project
  1. Home
  2. »
  3. motion
  4. »
  5. ICM20948 Sensor Project With STM32F303 Microcontroller

What's the purpose of this project?

In this section, we are going to launch the ICM20948 sensor using ARM microcontroller, STM32F series. In order to use more conveniently and optimally in this project, we use two ready modules GB308IM and GebraBit STM32F303. These two modules contain the minimum necessary elements of the ICM20948 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 ICM20948 sensor, you will get to know all the ICM20948 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 GB308IM 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 on GebraBit ICM20948 using the  on-board selector jumpers, then we place it as Pin To Pin on the GebraBit STM32F303 module as shown below:

Note: The above image is used only to show how the GebraBit ICM20948 module is placed on the GebraBit STM32F303 module. Therefore, to use the SPI communication protocol, the user must choose the correct state of the jumpers on the board.

Also please note that here  we placed the GebraBit ICM20948 module  on the microcontroller just for better understanding, but since the logic level of the GebraBit ICM20948  module is 1.8V and the logic level of the GebraBit STM32F303 microcontroller module is 3V3, according to the datasheet, users must avoid placing GebraBit ICM20948 as Pin to Pin on GebraBit STM32F303 microcontroller module . Therefore, it is necessary to convert the logic level of two modules through a logic level converter.

In fact,the correct way to connect GebraBit ICM20948 module with GebraBit STM32F303 microcontroller module using SPI protocol is as shown in the below.

In the new version of the GebraBit ICM20948 module, this problem has been solved and users can easily place the neew version of this module as pin to pin on the GebraBit STM32F303 microcontroller module.

Finally, we will see the values of temperature and acceleration, angular velocity and magnetic field in the three “X, Y, Z” axes in real time in “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 SPI, RCC, Debug, and Clock sections in the STM32F303 microcontroller to launch the GebraBit ICM20948 module.

RCC settings

we select the “external clock” in the RCC section cause there is an 8Mhz crystal in the GebraBit STM32F30 module.

Debug & Programming settings

Considering that we have access to the 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, in order to establish SPI communication with the GebraBit STM32F303 module, we must select the “Full Duplex Master” mode and select the PB3, PB4, and PB5 pins as SCK, MISO, and MOSI, and select 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

The “clock” settings for each part of the STM32F303 microcontroller in this code, are as follows:

Project Manager settings

“Project Manager” settings are as follows, here we have used “MDK-ARM”  version “5.32” compiler :

After completing all the above settings, we can develop our code easily just by one click on “GENERATE CODE” and adding the ICM20948 library and driver (provided by GebraBit).

You can download the “STM32Cube MX”, “ICM20789 library”, “driver” and KEIL project at the end of this tutorial.  

ICM20948 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 ICM20948.h header file

In this 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 ICM20948 sensor and the configurations related to each of the ICM20948 sensor internal blocks are defined in the form of a “STRUCT” with the name GebraBit_ ICM20948. Finally, in the Debug Session environment, all the configurations related to each block can be seen in real time.

ICM20948 _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                         							 
} ICM20948_Bank_Sel;

				
			

ICM20948 _Interface Enum

This enum is used to select the communication protocol with the sensor:

				
					typedef enum  interface
{  
 NOT_SPI = 0,                  						  
 IS_SPI                                 					
}ICM20948_Interface;

				
			

ICM20948_Accel_Fs_Sel Enum

This enum is used to select the Full Scale Range of the Accelerometer sensor:

				
					typedef enum accel_fs_sel
{  
FULL_SCALE_2g = 0  ,                    						 
FULL_SCALE_4g      ,                						     
FULL_SCALE_8g      ,               					     
FULL_SCALE_16g            							
}ICM20948_Accel_Fs_Sel;

				
			

ICM20948_Accel_Scale_Factor Enum

This enum is used to select the Scale Factor value of the Accelerometer sensor:

				
					typedef enum Accel_Scale_Factor
{  
SCALE_FACTOR_16384_LSB_g = 16384,                          
SCALE_FACTOR_8192_LSB_g  = 8192 ,                  
SCALE_FACTOR_4096_LSB_g  = 4096 ,                   
SCALE_FACTOR_2048_LSB_g  = 2048      			
}ICM20948_Accel_Scale_Factor;

				
			

ICM20948_Gyro_Fs_Sel Enum

This enum is used to select the Full Scale value of the Gyroscope sensor:

				
					typedef enum gyro_fs_sel
{ 
 FS_250_DPS ,               	                               
 FS_500_DPS ,                    					
 FS_1000_DPS,                				
 FS_2000_DPS          						
}ICM20948_Gyro_Fs_Sel;

				
			

ICM20948_Gyro_Scale_Factor Enum

This enum is used to select the Scale Factor value of the Gyroscope sensor.

				
					typedef enum Gyro_Scale_Factor
{  
SCALE_FACTOR_131_LSB_DPS   = 131 ,                  
SCALE_FACTOR_65p5_LSB_DPS  = 65  ,               
SCALE_FACTOR_32p8_LSB_DPS  = 32  ,                    
SCALE_FACTOR_16p4_LSB_DPS  = 16    			
}ICM20948_Gyro_Scale_Factor;

				
			

ICM20948_FIFO_MODE Enum

The FIFO working mode of the sensor is set by using the values of this enum:

				
					typedef enum FIFO_Mode
{  
STREAM_TO_FIFO      ,                          
STOP_ON_FULL_FIFO_SNAPSHOT = 31                                  
}ICM20948_FIFO_MODE ;

				
			

ICM20948_Ability Enum

The values of this enum are used to activate and deactivate different parts of the sensor:

				
					typedef enum Ability
{  
Disable = 0,                      
Enable     
}ICM20948_Ability;

				
			

ICM20948_Power_Mode Enum

The values of this enum are used to set the Power Mode of the sensor:

				
					typedef enum Power_Mode
{
ICM20948_LOW_NOISE = 0,        						
ICM20948_LOW_POWER = 1,			
ICM20948_SLEEP_OFF = 2						              
} ICM20948_Power_Mode;

				
			

ICM20948_ GYRO_Averaging_Filter Enum

The values of this enum are used to determine the filter used in the Gyroscope sensor in Low Power mode:

				
					typedef enum
{
 GYRO_AVERAGE_1_SAMPLES_FILTER  = 0 ,										 
 GYRO_AVERAGE_2_SAMPLES_FILTER  = 1 , 										
 GYRO_AVERAGE_4_SAMPLES_FILTER  = 2 ,										
 GYRO_AVERAGE_8_SAMPLES_FILTER  = 3 ,                    
 GYRO_AVERAGE_16_SAMPLES_FILTER = 4 ,											 
 GYRO_AVERAGE_32_SAMPLES_FILTER = 5	,									  				 
 GYRO_AVERAGE_64_SAMPLES_FILTER = 6	,                     				
 GYRO_AVERAGE_128_SAMPLES_FILTER= 7	                      				  
} ICM20948_GYRO_Averaging_Filter;

				
			

ICM20948_ ACCEL_Averaging_Filter Enum

To determine the filter used in the Accelerometer sensor in Low Power mode, the values of this enum are used:

				
					typedef enum ACCEL_Averaging_Filter
{
 ACCEL_AVERAGE_1_4_SAMPLES_FILTER  = 0 ,                 		
 ACCEL_AVERAGE_8_SAMPLES_FILTER    = 1 ,			
 ACCEL_AVERAGE_16_SAMPLES_FILTER   = 2 ,				
 ACCEL_AVERAGE_32_SAMPLES_FILTER   = 3 			 
} ICM20948_ACCEL_Averaging_Filter;

				
			

ICM20948_Preparation Enum

This enum reflects the status of being ready or not for any data in the sensor:

				
					typedef enum Preparation
{  
IS_NOT_Ready = 0,                      
IS_Ready     
}ICM20948_Preparation;

				
			

ICM20948_Reset_Status Enum

The final status of the sensor software reset is expressed in this enum:

				
					typedef enum Reset_Status
{  
DONE = 0,                      
FAILED     
}ICM20948_Reset_Status;

				
			

ICM20948_FIFO_Ability Enum

This Enum is used to enable or disable FIFO:

				
					typedef enum FIFO_Ability
{  
FIFO_DISABLE = 0,                      
FIFO_ENABLE     
} ICM20948_FIFO_Ability;

				
			

ICM20948_Get_DATA Enum

How to receive data from the sensor is described in this enum:

				
					typedef enum Get_DATA
{  
FROM_REGISTER = 0,                      
FROM_FIFO     
} ICM20948_Get_DATA;

				
			

ICM20948_ Interrupt_Pin Enum

This Enum is used to select the interrupt pin:

				
					typedef enum Interrupt_Pin
{  
INTERRUPT_ON_PIN_1 = 0,                      
INTERRUPT_ON_PIN_2     
} ICM20948_Interrupt_Pin;

				
			

ICM20948_ DMP_LP Enum

This Enum is used to enable or disable DMP in LOW POWER mode:

				
					typedef enum DMP_LP
{  
NOT_DMP_LOW_POWER = 0,                      
DMP_LOW_POWER     
} ICM20948_DMP_LP;

				
			

ICM20948_Sleep Enum

The values of this enum are used to set the sensor working mode:

				
					typedef enum Sleep
{
ICM20948_AWAKE   = 0 ,					
ICM20948_SLEEP                                  
}ICM20948_Sleep ;

				
			

ICM20948_Clock_Source Enum

The values of this enum are used to determine the clock of the sensor :

				
					typedef enum Clock_Source
{  
INTERNAL_20MHZ_OSCILLATOR = 0 ,                      
AUTO_SELECT               = 1 ,                    			
CLOCK_STOP                = 7                                        
}ICM20948_Clock_Source ;

				
			

ICM20948_Sensor Enum

To enable or disable each of the sensors, the values of this enum are used:

				
					typedef enum Sensor
{  
SENSOR_ENABLE   = 0 ,                                      			
SENSOR_DISABLE  = 7                                     
}ICM20948_Sensor ;

				
			

ICM20948_INT_Level Enum

To determine the basic logic level of Interrupt, the values of this enum are used:

				
					typedef enum int_level
{  
ACTIVE_HIGH = 0 ,                      
ACTIVE_LOW     
} ICM20948_INT_Level;

				
			

ICM20948_Latch_Type Enum

The values of this enum are used to determine the latch type of the Interrupt output:

				
					typedef enum latch_type
{  
_50_US = 0,                                        
HELD_STATUS_CLEAR     					
} ICM20948_Latch_Type;

				
			

ICM20948_INT_Type Enum

The values of this enum are used to determine the Interrupt output type:

				
					typedef enum int_type
{  
PUSH_PULL = 0 ,                      
OPEN_DRAIN     
}ICM20948_INT_Type;

				
			

ICM20948_FIFO_ Reset Enum

By using the values of this Enum, the FIFO of the sensor can be reset:

				
					typedef enum FIFO_Reset
{  
FIFO_DE_ASSERT = 0 ,                                
FIFO_ASSERT    = 31                                     
} ICM20948_FIFO_Reset;

				
			

ICM20948_FIFO_Overflow Enum

The values of this enum indicate FIFO overflows or not:

				
					typedef enum FIFO_Overflow
{  
NOT_OVERFLOW   = 0 ,                      
FIFO_OVERFLOW  = 1     
} ICM20948_FIFO_Overflow;

				
			

ICM20948_ Sample_Rate Enum

The values of this enum determine the value of the output data rate:

				
					typedef enum sample_rate
{  
_1_25_KHz   = 1125 ,                        
_4_5_KHz    = 4500 ,                    					
_9_KHz      = 9000                                          
}ICM20948_Sample_Rate ;

				
			

ICM20948_ Data_Copy_FIFO Enum

The values of this enum specify whether the data is copied into the FIFO or not:

				
					typedef enum Data_Copy_FIFO
{  
NOT_COPY_FIFO = 0 ,               
COPY_TO_FIFO  = 3     
} ICM20948_Data_Copy_FIFO;

				
			

ICM20948_ FCHOICEB Enum

To enable or disable the DLPF filter, the values of this enum are used:

				
					typedef enum FCHOICEB
{  
ENABLE_DLPF_FCHOICEB = 1 ,                      
BYPASS_DLPF_FCHOICEB = 0 ,                          
}ICM20948_FCHOICEB;

				
			

ICM20948_ Gyro_ DLPF_CFG Enum

To determine the DLPF filter in the gyroscope sensor, the values of this enum are used:

				
					typedef enum Gyro_DLPF_CFG
{
ICM20948_GYRO_DLPF_196    = 0, 								
ICM20948_GYRO_DLPF_152	  = 1,								 		 
ICM20948_GYRO_DLPF_119	  = 2, 								
ICM20948_GYRO_DLPF_51	  = 3, 										
ICM20948_GYRO_DLPF_24	  = 4, 											
ICM20948_GYRO_DLPF_12	  = 5, 								
ICM20948_GYRO_DLPF_6	  = 6, 										
ICM20948_GYRO_DLPF_361	  = 7 										
}ICM20948_GYRO_DLPF ;

				
			

ICM20948_ Accel_DLPF_CFG Enum

To determine the DLPF filter in the accelerometer sensor, the values of this enum are used:

				
					typedef enum Accel_DLPF_CFG
{
ICM20948_ACCEL_DLPF_246	        = 1,								 
ICM20948_ACCEL_DLPF_111	        = 2, 									
ICM20948_ACCEL_DLPF_50	        = 3, 									
ICM20948_ACCEL_DLPF_24	        = 4, 								
ICM20948_ACCEL_DLPF_11	        = 5, 								
ICM20948_ACCEL_DLPF_6	        = 6, 								
ICM20948_ACCEL_DLPF_473         = 7 								
}ICM20948_ACCEL_DLPF ;

				
			

ICM20948_ Temp_DLPF_CFG Enum

To determine the DLPF filter in the temperature sensor, the values of this enum are used:

				
					typedef enum Temp_DLPF_CFG
{
ICM20948_TEMP_DLPF_7932 = 0, 									
ICM20948_TEMP_DLPF_218	= 1,								   
ICM20948_TEMP_DLPF_123	= 2, 									
ICM20948_TEMP_DLPF_66	= 3, 								
ICM20948_TEMP_DLPF_34	= 4, 								
ICM20948_TEMP_DLPF_17 	= 5, 									
ICM20948_TEMP_DLPF_9	= 6, 									
}ICM20948_TEMP_DLPF ;

				
			

ICM20948_ Aux_Between Enum

The values of this enum are used to control “RESTART” and “STOP” in external I2C connection:

				
					typedef enum Aux_Between
{  
	RESTART = 0 ,                                    
	STOP                                            
} ICM20948_Aux_Between;

				
			

ICM20948_I2C_Slave Enum

The values of this enum are used to specify the slave number in the external I2C connection:

				
					typedef enum I2C_Slave
{  
 SLAVE_0 = 0 ,                      
 SLAVE_1 = 1 ,
 SLAVE_2 = 2 ,
 SLAVE_3 = 3 ,
 SLAVE_4 	
} ICM20948_I2C_Slave;

				
			

ICM20948_ AK09916_Power Enum

The values of this enum are used to select the working mode of the AK09916 internal sensor:

				
					typedef enum AK09916_Power
{  
AK09916_POWER_DOWN             = 0 ,                      
AK09916_SINGLE_MEASUREMENT     = 1 ,
AK09916_CONTINUOUS_MEASUREMENT = 2 , 
AK09916_SELF_TEST              = 65536 	
} AK09916_Power_Mode;

				
			

GebraBit_ICM20948 structure

All the information and configuration implemented on the sensor are stored in this “structure” and you can see the changes in each part of the sensor in the “Debug Session” environment.

Declaration of functions

At the end of this file, all the functions for reading and writing in ICM20948 registers, sensor configuration , FIFO and receiving data from the sensor are declared:

				
					/********************************************************
 *Declare Read&Write ICM20948 Register Values Functions *
 ********************************************************/
extern	uint8_t	GB_ICM20948_Read_Reg_Data ( uint8_t regAddr, ICM20948_Bank_Sel regBank, uint8_t* data);
extern	uint8_t GB_ICM20948_Read_Reg_Bits (uint8_t regAddr, ICM20948_Bank_Sel regBank, uint8_t start_bit, uint8_t len, uint8_t* data);
extern	uint8_t GB_ICM20948_Burst_Read(uint8_t regAddr, ICM20948_Bank_Sel regBank, uint8_t *data, uint16_t byteQuantity);
extern	uint8_t GB_ICM20948_Write_Reg_Data(uint8_t regAddr, ICM20948_Bank_Sel regBank, uint8_t data);
extern	uint8_t	GB_ICM20948_Write_Reg_Bits(uint8_t regAddr, ICM20948_Bank_Sel regBank, uint8_t start_bit, uint8_t len, uint8_t data);
extern	uint8_t GB_ICM20948_Burst_Write		( uint8_t regAddr, ICM20948_Bank_Sel regBank, uint8_t *data, 	uint16_t byteQuantity);
/********************************************************
 *       Declare ICM20948 Configuration Functions       *
 ********************************************************/
extern void GB_ICM20948_Soft_Reset ( GebraBit_ICM20948 * ICM20948 );
extern void GB_ICM20948_Bank_Selection( ICM20948_Bank_Sel bsel);
extern void GB_ICM20948_Who_am_I(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Select_SPI4_Interface(GebraBit_ICM20948 * ICM20948 , ICM20948_Interface spisel);
extern void GB_ICM20948_DMP(GebraBit_ICM20948* ICM20948 ,ICM20948_Ability dmp,ICM20948_DMP_LP dmp_lp);
extern void GB_ICM20948_DMP_Reset(GebraBit_ICM20948* ICM20948 ,ICM20948_Ability rst);
extern void GB_ICM20948_DMP_Interrupt( ICM20948_Ability interrupt);
extern void GB_ICM20948_Sleep_Awake (GebraBit_ICM20948 * ICM20948, ICM20948_Sleep  working  ) ;
extern void GB_ICM20948_ACCEL_Power_Mode(GebraBit_ICM20948* ICM20948 ,ICM20948_Power_Mode pmode);
extern void GB_ICM20948_GYRO_Power_Mode(GebraBit_ICM20948* ICM20948 ,ICM20948_Power_Mode pmode);
extern void GB_ICM20948_Set_Clock_Source(GebraBit_ICM20948 * ICM20948 , ICM20948_CLK clk) ;
extern void GB_ICM20948_Temperature(GebraBit_ICM20948* ICM20948 ,ICM20948_Ability temp);
extern void GB_ICM20948_Accelerometer(GebraBit_ICM20948 * ICM20948 , ICM20948_Sensor accel);
extern void GB_ICM20948_Gyroscope(GebraBit_ICM20948 * ICM20948 , ICM20948_Sensor gyro) ;
extern void GB_ICM20948_Set_INT1_Pin(GebraBit_ICM20948 * ICM20948 , ICM20948_INT_Level level ,ICM20948_INT_Type type , ICM20948_Latch_Type latch );
extern void Interrupt_Status_Enable(GebraBit_ICM20948 * ICM20948 , ICM20948_Ability interrupt );
extern ICM20948_Preparation GB_ICM20948_Check_Data_Preparation(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_GYRO_Full_Scale ( GebraBit_ICM20948 * ICM20948 , ICM20948_Gyro_Fs_Sel fs ) ;
extern void GB_ICM20948_GYRO_Low_Pass_Filter  (GebraBit_ICM20948 * ICM20948 ,  ICM20948_FCHOICEB bypass ) ;
extern void GB_ICM20948_GYRO_Low_Pass_Filter_Value  (GebraBit_ICM20948 * ICM20948 , ICM20948_GYRO_DLPF dlpf );
extern void GB_ICM20948_GYRO_LP_Averaging_Filter  (GebraBit_ICM20948 * ICM20948 , ICM20948_GYRO_Averaging_Filter avg );
extern void GB_ICM20948_GYRO_Output_Sample_Rate (GebraBit_ICM20948 * ICM20948 , uint16_t rate_hz);
extern void GB_ICM20948_ACCEL_Full_Scale ( GebraBit_ICM20948 * ICM20948 , ICM20948_Accel_Fs_Sel fs );
extern void GB_ICM20948_ACCEL_Low_Pass_Filter  (GebraBit_ICM20948 * ICM20948 ,  ICM20948_FCHOICEB bypass );
extern void GB_ICM20948_ACCEL_Low_Pass_Filter_Value  (GebraBit_ICM20948 * ICM20948 , ICM20948_ACCEL_DLPF dlpf );
extern void GB_ICM20948_ACCEL_LP_Averaging_Filter  (GebraBit_ICM20948 * ICM20948 , ICM20948_ACCEL_Averaging_Filter avg );
extern void GB_ICM20948_ACCEL_Output_Sample_Rate (GebraBit_ICM20948 * ICM20948 , uint16_t rate_hz);
extern void GB_ICM20948_TEMP_Low_Pass_Filter_Value  (GebraBit_ICM20948 * ICM20948 , ICM20948_TEMP_DLPF tdlpf );
/********************************************************
 *          Declare ICM20948 FIFO Functions             *
 ********************************************************/
extern void GB_ICM20948_Access_Serial_Interface_To_FIFO(GebraBit_ICM20948 * ICM20948 , ICM20948_Ability interface_access_fifo);
extern ICM20948_FIFO_Overflow GB_ICM20948_Check_FIFO_Overflow(GebraBit_ICM20948 * ICM20948) ;
extern void GB_ICM20948_Write_ACCEL_FIFO(GebraBit_ICM20948 * ICM20948 , ICM20948_Ability accel_fifo ) ;
extern void GB_ICM20948_Write_GYRO_FIFO(GebraBit_ICM20948 * ICM20948 , ICM20948_Ability gyro_fifo ) ;
extern void GB_ICM20948_Write_TEMP_FIFO(GebraBit_ICM20948 * ICM20948 , ICM20948_Ability temp_fifo );
extern void GB_ICM20948_Write_Slave_FIFO(GebraBit_ICM20948 * ICM20948 ,ICM20948_I2C_Slave slave , ICM20948_Ability slv_fifo );
extern void GB_ICM20948_FIFO_Mode(GebraBit_ICM20948 * ICM20948 , ICM20948_FIFO_Mode fifo_mode );
extern void GB_ICM20948_FIFO_Reset(void) ;
extern void GB_ICM20948_GET_FIFO_Count (GebraBit_ICM20948 * ICM20948 ) ;
extern void GB_ICM20948_Read_FIFO(GebraBit_ICM20948 * ICM20948 , uint16_t qty);
extern ICM20948_Data_Copy_FIFO GB_ICM20948_Check_Data_Copy_TO_FIFO(GebraBit_ICM20948 * ICM20948) ;
/********************************************************
 *          Declare ICM20948 DATA Functions             *
 ********************************************************/
extern void GB_ICM20948_Get_Temp_Register_Raw_Data(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_Temp_Valid_Data(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_GYRO_X_Register_Raw_DATA(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_GYRO_Y_Register_Raw_DATA(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_GYRO_Z_Register_Raw_DATA(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_GYRO_DATA_X_Valid_Data(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_GYRO_DATA_Y_Valid_Data(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_GYRO_DATA_Z_Valid_Data(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_ACCEL_X_Register_Raw_DATA(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_ACCEL_Y_Register_Raw_DATA(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_ACCEL_Z_Register_Raw_DATA(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_ACCEL_DATA_X_Valid_Data(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_ACCEL_DATA_Y_Valid_Data(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_ACCEL_DATA_Z_Valid_Data(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_Temperature(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_XYZ_GYROSCOPE(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_XYZ_ACCELERATION(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_ACCEL_GYRO_TEMP_MAG_From_Registers(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_FIFO_Data_Partition_ACCEL_GYRO_MAG_XYZ_TEMP(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_ACCEL_GYRO_MAG_TEMP_From_FIFO(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_Get_Data(GebraBit_ICM20948 * ICM20948 , ICM20948_Get_DATA get_data);
extern void GB_ICM20948_Get_XYZ_AK09916_Magnetometer(GebraBit_ICM20948 * ICM20948);
/********************************************************
 *          Declare ICM20948 HIGH LEVEL Functions       *
 ********************************************************/
extern void GB_ICM20948_FIFO_Configuration ( GebraBit_ICM20948 * ICM20948 , ICM20948_FIFO_Ability fifo );
extern void GB_ICM20948_Set_Power_Management(GebraBit_ICM20948 * ICM20948 , ICM20948_Power_Mode pmode) ;
extern void GB_ICM20948_initialize( GebraBit_ICM20948 * ICM20948 );
extern void GB_ICM20948_Configuration(GebraBit_ICM20948 * ICM20948, ICM20948_FIFO_Ability fifo);
/********************************************************
 *   Declare ICM20948 AUX I2C and AK09916  Functions    *
 ********************************************************/
extern void GB_ICM20948_AUX_I2C_Between_Reads(GebraBit_ICM20948 * ICM20948,ICM20948_Aux_Between bet );
extern void GB_ICM20948_External_Sensor_ODR(GebraBit_ICM20948 * ICM20948,uint8_t devider);
extern void GB_ICM20948_AUX_Initialize(GebraBit_ICM20948 * ICM20948);
extern void GB_ICM20948_AUX_I2C(GebraBit_ICM20948 * ICM20948,ICM20948_Ability aux);
extern void GB_ICM20948_Stop_AUX_I2C(GebraBit_ICM20948 * ICM20948,ICM20948_I2C_Slave slave);
extern void GB_ICM20948_Set_AUX_I2C_Read(GebraBit_ICM20948 * ICM20948, ICM20948_I2C_Slave slave, uint8_t slave_addr, uint8_t slave_reg, uint8_t data_len);
extern void GB_ICM20948_AUX_I2C_Read_Data(GebraBit_ICM20948 * ICM20948, ICM20948_I2C_Slave slave, uint8_t slave_addr, uint8_t slave_reg, uint8_t data_len, uint8_t * data);
extern void GB_ICM20948_Set_AUX_I2C_Write(GebraBit_ICM20948 * ICM20948, ICM20948_I2C_Slave slave, uint8_t slave_addr, uint8_t slave_reg, uint8_t data);
extern void GB_ICM20948_AUX_I2C_Write_Data(GebraBit_ICM20948 * ICM20948, ICM20948_I2C_Slave slave, uint8_t slave_addr, uint8_t slave_reg, uint8_t  data);
extern void GB_ICM20948_Get_AK09916_Device_ID(GebraBit_ICM20948 * ICM20948)  ;
extern ICM20948_Preparation GB_ICM20948_Check_AK09916_Data_Preparation(GebraBit_ICM20948 * ICM20948)  ;	
extern void GB_ICM20948_Set_AK09916_Magnetometer_Power_Mode(GebraBit_ICM20948 * ICM20948 , AK09916_Power_Mode pmode)  ;	
extern void GB_ICM20948_Set_AK09916_Soft_Reset(GebraBit_ICM20948 * ICM20948)   ;	
extern void GB_ICM20948_Config_AK09916_Magnetometer(GebraBit_ICM20948 * ICM20948) ;	
extern void GB_ICM20948_Read_AK09916_Status_2(GebraBit_ICM20948 * ICM20948) ;

				
			

GebraBit_ICM20948.c source file

In this file, which is written in C language, 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 by STM32CubeMX and adding the “GebraBit_ICM20948.c” library provided by GebraBit, we will examine the “main .c” file of the sample tutorial and view the output of the GebraBit_ICM20948 module in the “watch” part in the Keil compiler  “Debugging” environment.

Description of “main.c” file

If you look carefully at the beginning part of the “main.c” file, you will notice that the “GebraBit_ ICM20948.h” header has been added to access the structures and functions required by the GebraBit ICM20948 module. In the next part, a variable named ICM20948_Module of the GebraBit ICM20948 structure type (this structure is in the GebraBit_ ICM20948 header and is explained in the GebraBit_ ICM20948 library description section) is defined for the configuration of the GebraBit ICM20948 module:   

				
					/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
GebraBit_ICM20948 ICM20948_Module;
/* USER CODE END PTD */

				
			

In the next part of the written code, the configuration and settings of the GebraBit ICM20948 module are done by using the GB_ ICM20948_initialize() and GB_ ICM20948_Configuration() functions:

				
						GB_ICM20948_initialize( &ICM20948_Module );
	GB_ICM20948_Configuration(&ICM20948_Module ,FIFO_ENABLE);
//GB_ICM20948_Configuration(&ICM20948_Module , FIFO_DISABLE );
	GB_ICM20948_Config_AK09916_Magnetometer(&ICM20948_Module);

				
			

And finally, in the “while” part of the program, the values of the GebraBit ICM20948  module in 3 axes X, Y, Z and temperature are continuously received:

				
					GB_ICM20948_Get_Data( &ICM20948_Module , FROM_REGISTER);
//GB_ICM20948_Get_Data(  &ICM20948_Module , FROM_FIFO);

				
			

By removing the GB_ ICM20948_Configuration(&ICM20948_Module , FIFO_DISABLE ); and GB_ ICM20948_Get_Data( & ICM20948_Module , FROM_REGISTER ) functions; Data values can be read directly from the data registers.

The “main.c” file code text:

				
					/* USER CODE BEGIN Header */
/*
 * ________________________________________________________________________________________________________
 * Copyright (c) 2020 GebraBit Inc. All rights reserved.
 *
 * This software, related documentation and any modifications thereto (collectively “Software”) is subject
 * to GebraBit and its licensors' intellectual property rights under U.S. and international copyright
 * and other intellectual property rights laws. 
 *
 * GebraBit and its licensors retain all intellectual property and proprietary rights in and to the Software
 * and any use, reproduction, disclosure or distribution of the Software without an express license agreement
 * from GebraBit is strictly prohibited.
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT IN  
 * NO EVENT SHALL GebraBit BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, 
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THE SOFTWARE.
 * ________________________________________________________________________________________________________
 */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
	* @Author       	: Mehrdad Zeinali
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  *//* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
//#include "i2c.h"
#include "spi.h"
#include "gpio.h"
#include "GebraBit_ICM20948.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
 
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
GebraBit_ICM20948 ICM20948_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 */
  /* 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();
  /* USER CODE END 2 */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
	GB_ICM20948_initialize( &ICM20948_Module );
	//GB_ICM20948_Configuration(&ICM20948_Module ,FIFO_ENABLE );
	GB_ICM20948_Configuration(&ICM20948_Module , FIFO_DISABLE );
	GB_ICM20948_Config_AK09916_Magnetometer(&ICM20948_Module);
  while (1)
  {
 
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
		GB_ICM20948_Get_Data(  &ICM20948_Module , FROM_REGISTER  );	
		//GB_ICM20948_Get_Data(  &ICM20948_Module , FROM_FIFO  );	
	
  }
  /* 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_MUL7;
  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 ICM20948 modules, because they receive their supply voltage directly from the STLINK V2 programmer.

finally, enter the “Debug” mode and by adding the “ICM20948_Module” to the “watch” window and running  the program, we can see the changes of temperature and the GebraBit ICM20948 module values  in the 3 axes( X, Y, Z) directly from the data registers and FIFO :

Receiving sensor data directly from data registers:

Receiving sensor data from FIFO:

In the following, you can download the “GebraBit ICM20948 module setup project” using the GebraBit STM32F303 module in the Keil environment, the “STM32CubeMX file”, the schematic of the modules and the “ICM20948 datasheet”.

Program output video

این مقاله را با دوستانتان به اشتراک بگذارید!

Be the first to write a review

Please help the Gebra team to improve the quality by sending comments and ratings

Your email address will not be published. Required fields are marked *

Shopping cart
Start typing to see posts you are looking for.

Sign in

No account yet?