@ -16,16 +16,16 @@ 
			
		
	
		
		
			
				
					
					# include  <stdint.h> # include  <stdint.h>  
			
		
	
		
		
			
				
					
					# include  <stdbool.h> # include  <stdbool.h>  
			
		
	
		
		
			
				
					
					# include  <math.h> # include  <math.h>  
			
		
	
		
		
			
				
					
					# include  "em7180_common.h"  
			
		
	
		
		
			
				
					
					# include  "em7180.h" # include  "em7180.h"  
			
		
	
		
		
			
				
					
					# include  "i2c.h"  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					/* Data Structures */  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					/* Private Global Variables */ /* Private Global Variables */  
			
		
	
		
		
			
				
					
					static  uint8_t  _intPin ;  
			
		
	
		
		
			
				
					
					static  bool  _passThru ;  
			
		
	
		
		
			
				
					
					static  float  _aRes ;  
			
		
	
		
		
			
				
					
					static  float  _gRes ;  
			
		
	
		
		
			
				
					
					static  float  _mRes ;  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					/* Function Prototypes */ /* Function Prototypes */  
			
		
	
		
		
			
				
					
					static  void  em7180_float_to_bytes ( float  param_val ,  uint8_t  * buf ) ;  
			
		
	
		
		
			
				
					
					static  void  m24512dfm_write_byte ( uint8_t  device_address ,  uint8_t  data_address1 , static  void  m24512dfm_write_byte ( uint8_t  device_address ,  uint8_t  data_address1 ,  
			
		
	
		
		
			
				
					
					                                 uint8_t  data_address2 ,  uint8_t  data ) ;                                   uint8_t  data_address2 ,  uint8_t  data ) ;   
			
		
	
		
		
			
				
					
					static  void  m24512dfm_write ( uint8_t  device_address ,  uint8_t  data_address1 , static  void  m24512dfm_write ( uint8_t  device_address ,  uint8_t  data_address1 ,  
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -39,150 +39,38 @@ static void em7180_read(uint8_t address, uint8_t subAddress, uint8_t count, 
			
		
	
		
		
			
				
					
					                        uint8_t  * dest ) ;                          uint8_t  * dest ) ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					/* Function Definitions */ /* Function Definitions */  
			
		
	
		
		
			
				
					
					em7180_new ( uint8_t  pin ,  bool  passthru ) void  em7180_init ( em7180_t  * em7180 ,  lsm6dsm_t  * lsm6dsm ,  I2C_HandleTypeDef  * hi2c1 ,  
			
				
				
			
		
	
		
		
			
				
					
					{                  uint16_t  acc_fs ,  uint16_t  gyro_fs ,  uint16_t  mag_fs ,   
			
				
				
			
		
	
		
		
			
				
					
						/*	pinMode(pin, INPUT); */                   uint8_t  q_rate_div ,  uint8_t  mag_rate ,  uint8_t  acc_rate ,   
			
				
				
			
		
	
		
		
			
				
					
						_intPin  =  pin ;                   uint8_t  gyro_rate ,  uint8_t  baro_rate )   
			
				
				
			
		
	
		
		
			
				
					
						_passThru  =  passthru ;   
			
		
	
		
		
			
				
					
					}  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					void  em7180_chip_id_get ( )  
			
		
	
		
		
			
				
					
					{  
			
		
	
		
		
			
				
					
						// Read SENtral device information
   
			
		
	
		
		
			
				
					
						uint16_t  ROM1  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ROMVersion1 ) ;   
			
		
	
		
		
			
				
					
						uint16_t  ROM2  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ROMVersion2 ) ;   
			
		
	
		
		
			
				
					
						/* Serial.print("EM7180 ROM Version: 0x"); */   
			
		
	
		
		
			
				
					
						/* Serial.print(ROM1, HEX); */   
			
		
	
		
		
			
				
					
						/* Serial.println(ROM2, HEX); */   
			
		
	
		
		
			
				
					
						/* Serial.println("Should be: 0xE609"); */   
			
		
	
		
		
			
				
					
						uint16_t  RAM1  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_RAMVersion1 ) ;   
			
		
	
		
		
			
				
					
						uint16_t  RAM2  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_RAMVersion2 ) ;   
			
		
	
		
		
			
				
					
						/* Serial.print("EM7180 RAM Version: 0x"); */   
			
		
	
		
		
			
				
					
						/* Serial.print(RAM1); */   
			
		
	
		
		
			
				
					
						/* Serial.println(RAM2); */   
			
		
	
		
		
			
				
					
						uint8_t  PID  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ProductID ) ;   
			
		
	
		
		
			
				
					
						/* Serial.print("EM7180 ProductID: 0x"); */   
			
		
	
		
		
			
				
					
						/* Serial.print(PID, HEX); */   
			
		
	
		
		
			
				
					
						/* Serial.println(" Should be: 0x80"); */   
			
		
	
		
		
			
				
					
						uint8_t  RID  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_RevisionID ) ;   
			
		
	
		
		
			
				
					
						/* Serial.print("EM7180 RevisionID: 0x"); */   
			
		
	
		
		
			
				
					
						/* Serial.print(RID, HEX); */   
			
		
	
		
		
			
				
					
						/* Serial.println(" Should be: 0x02"); */   
			
		
	
		
		
			
				
					
					}  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					void  em7180_load_fw_from_eeprom ( )  
			
		
	
		
		
			
				
					
					{  
			
		
	
		
		
			
				
					
						// Check which sensors can be detected by the EM7180
   
			
		
	
		
		
			
				
					
						uint8_t  featureflag  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,   
			
		
	
		
		
			
				
					
						EM7180_FeatureFlags ) ;   
			
		
	
		
		
			
				
					
						if ( featureflag  &  0x01 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("A barometer is installed"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( featureflag  &  0x02 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("A humidity sensor is installed"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( featureflag  &  0x04 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("A temperature sensor is installed"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( featureflag  &  0x08 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("A custom sensor is installed"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( featureflag  &  0x10 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("A second custom sensor is installed"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( featureflag  &  0x20 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("A third custom sensor is installed"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						HAL_Delay ( 1000 ) ;  // give some time to read the screen
   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						// Check SENtral status, make sure EEPROM upload of firmware was accomplished
   
			
		
	
		
		
			
				
					
						uint8_t  STAT  =  ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )   
			
		
	
		
		
			
				
					
						    &  0x01 ) ;   
			
		
	
		
		
			
				
					
						if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x01 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("EEPROM detected on the sensor bus!"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x02 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("EEPROM uploaded config file!"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x04 )   
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					{ {  
			
		
	
		
		
			
				
					
							/* Serial.println("EEPROM CRC incorrect!"); */  	if ( ! em7180 )   
			
				
				
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x08 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("EM7180 in initialized state!"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x10 )   
			
		
	
		
		
	
		
		
			
				
					
						{  	{   
			
		
	
		
		
			
				
					
							/* Serial.println("No EEPROM detected!"); */  		return ;   
			
				
				
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						int  count  =  0 ;   
			
		
	
		
		
			
				
					
						while ( ! STAT )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_ResetRequest ,  0x01 ) ;   
			
		
	
		
		
			
				
					
							HAL_Delay ( 500 ) ;   
			
		
	
		
		
			
				
					
							count + + ;   
			
		
	
		
		
			
				
					
							STAT  =  ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x01 ) ;   
			
		
	
		
		
			
				
					
							if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x01 )   
			
		
	
		
		
			
				
					
							{   
			
		
	
		
		
			
				
					
								/* Serial.println("EEPROM detected on the sensor bus!"); */   
			
		
	
		
		
			
				
					
							}   
			
		
	
		
		
			
				
					
							if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x02 )   
			
		
	
		
		
			
				
					
							{   
			
		
	
		
		
			
				
					
								/* Serial.println("EEPROM uploaded config file!"); */   
			
		
	
		
		
			
				
					
							}   
			
		
	
		
		
			
				
					
							if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x04 )   
			
		
	
		
		
			
				
					
							{   
			
		
	
		
		
			
				
					
								/* Serial.println("EEPROM CRC incorrect!"); */   
			
		
	
		
		
			
				
					
							}   
			
		
	
		
		
			
				
					
							if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x08 )   
			
		
	
		
		
			
				
					
							{   
			
		
	
		
		
			
				
					
								/* Serial.println("EM7180 in initialized state!"); */   
			
		
	
		
		
			
				
					
							}   
			
		
	
		
		
			
				
					
							if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x10 )   
			
		
	
		
		
			
				
					
							{   
			
		
	
		
		
			
				
					
								/* Serial.println("No EEPROM detected!"); */   
			
		
	
		
		
			
				
					
							}   
			
		
	
		
		
			
				
					
							if ( count  >  10 )   
			
		
	
		
		
			
				
					
							{   
			
		
	
		
		
			
				
					
								break ;   
			
		
	
		
		
			
				
					
							}   
			
		
	
		
		
	
		
		
			
				
					
						}  	}   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						if ( ! ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x04 ) )  	em7180 - > lsm6dsm  =  lsm6dsm ;   
			
				
				
			
		
	
		
		
			
				
					
						{  	em7180 - > acc_fs  =  acc_fs ;   
			
				
				
			
		
	
		
		
			
				
					
							/* Serial.println("EEPROM upload successful!"); */  	em7180 - > gyro_fs  =  gyro_fs ;   
			
				
				
			
		
	
		
		
			
				
					
						}  	em7180 - > mag_fs  =  mag_fs ;   
			
				
				
			
		
	
		
		
			
				
					
					} 	em7180 - > q_rate_div  =  q_rate_div ;   
			
				
				
			
		
	
		
		
			
				
					
					
	em7180 - > mag_rate  =  mag_rate ;   
			
				
				
			
		
	
		
		
			
				
					
					uint8_t  em7180_status ( ) 	em7180 - > acc_rate  =  acc_rate ;   
			
				
				
			
		
	
		
		
			
				
					
					{ 	em7180 - > gyro_rate  =  gyro_rate ;   
			
				
				
			
		
	
		
		
			
				
					
						// Check event status register, way to check data ready by polling rather than interrupt
  	em7180 - > baro_rate  =  baro_rate ;   
			
				
				
			
		
	
		
		
			
				
					
						uint8_t  c  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_EventStatus ) ;  // reading clears the register and interrupt
   
			
		
	
		
		
			
				
					
						return  c ;   
			
		
	
		
		
			
				
					
					}  
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					uint8_t  em7180_errors ( ) 	em7180_config ( em7180 ) ;   
			
				
				
			
		
	
		
		
			
				
					
					{  
			
		
	
		
		
			
				
					
						uint8_t  c  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ErrorRegister ) ;  // check error register
   
			
		
	
		
		
			
				
					
						return  c ;   
			
		
	
		
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					void  em7180_init ( uint8_t  accBW ,  uint8_t  gyroBW ,  uint16_t  accFS ,  uint16_t  gyroFS , void  em7180_config ( em7180_t  * em7180 )  
			
				
				
			
		
	
		
		
			
				
					
					                 uint16_t  magFS ,  uint8_t  QRtDiv ,  uint8_t  magRt ,  uint8_t  accRt ,   
			
		
	
		
		
			
				
					
					                 uint8_t  gyroRt ,  uint8_t  baroRt )   
			
		
	
		
		
	
		
		
			
				
					
					{ {  
			
		
	
		
		
			
				
					
						uint16_t  EM7180_mag_fs ,  EM7180_acc_fs ,  EM7180_gyro_fs ;  // EM7180 sensor full scale ranges
   
			
		
	
		
		
			
				
					
						uint8_t  param [ 4 ] ;  	uint8_t  param [ 4 ] ;   
			
		
	
		
		
			
				
					
						uint8_t  param_xfer ;   
			
		
	
		
		
			
				
					
						uint8_t  runStatus ;   
			
		
	
		
		
			
				
					
						uint8_t  algoStatus ;   
			
		
	
		
		
			
				
					
						uint8_t  passthruStatus ;   
			
		
	
		
		
			
				
					
						uint8_t  eventStatus ;   
			
		
	
		
		
			
				
					
						uint8_t  sensorStatus ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						// Enter EM7180 initialized state
  	// Enter EM7180 initialized state
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_HostControl ,  0x00 ) ;  // set SENtral in initialized state to configure registers
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_HostControl ,  0x00 ) ;  // set SENtral in initialized state to configure registers
   
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -190,15 +78,17 @@ void em7180_init(uint8_t accBW, uint8_t gyroBW, uint16_t accFS, uint16_t gyroFS, 
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_HostControl ,  0x01 ) ;  // Force initialize
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_HostControl ,  0x01 ) ;  // Force initialize
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_HostControl ,  0x00 ) ;  // set SENtral in initialized state to configure registers
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_HostControl ,  0x00 ) ;  // set SENtral in initialized state to configure registers
   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						/* Legacy MPU6250 stuff, it seems
   
			
		
	
		
		
			
				
					
						 // Setup LPF bandwidth (BEFORE setting ODR's)
  	 // Setup LPF bandwidth (BEFORE setting ODR's)
   
			
		
	
		
		
			
				
					
						 lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_ACC_LPF_BW ,  accBW ) ;  // accBW = 3 = 41Hz
  	 lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_ACC_LPF_BW ,  accBW ) ;  // accBW = 3 = 41Hz
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_GYRO_LPF_BW ,  gyroBW ) ;  // gyroBW = 3 = 41Hz
  	  lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_GYRO_LPF_BW ,  gyroBW ) ;  // gyroBW = 3 = 41Hz */ 
   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
						// Set accel/gyro/mag desired ODR rates
  	// Set accel/gyro/mag desired ODR rates
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_QRateDivisor ,  QRtDiv ) ;  // quat rate = gyroRt/(1 QRTDiv)
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_QRateDivisor ,  em7180 - > q_rate_div ) ;  // quat rate = gyroRt/(1 QRTDiv)
   
			
				
				
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_MagRate ,  magRt ) ;  // 0x64 = 100 Hz
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_MagRate ,  em7180 - > mag_rate ) ;  // 0x64 = 100 Hz
   
			
				
				
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_AccelRate ,  accRt ) ;  // 200/10 Hz, 0x14 = 200 Hz
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_AccelRate ,  em7180 - > acc_rate ) ;  // 200/10 Hz, 0x14 = 200 Hz
   
			
				
				
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_GyroRate ,  gyroRt ) ;  // 200/10 Hz, 0x14 = 200 Hz
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_GyroRate ,  em7180 - > gyro_rate ) ;  // 200/10 Hz, 0x14 = 200 Hz
   
			
				
				
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_BaroRate ,  0x80  |  baroRt ) ;  // set enable bit and set Baro rate to 25 Hz, rate = baroRt/2, 0x32 = 25 Hz
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_BaroRate ,   
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
						                   0x80  |  em7180 - > baro_rate ) ;  // set enable bit and set Baro rate to 25 Hz, rate = baroRt/2, 0x32 = 25 Hz
   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						// Configure operating mode
  	// Configure operating mode
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_AlgorithmControl ,  0x00 ) ;  // read scale sensor data
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_AlgorithmControl ,  0x00 ) ;  // read scale sensor data
   
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -213,95 +103,20 @@ void em7180_init(uint8_t accBW, uint8_t gyroBW, uint16_t accFS, uint16_t gyroFS, 
			
		
	
		
		
			
				
					
						// EM7180 parameter adjustments
  	// EM7180 parameter adjustments
   
			
		
	
		
		
			
				
					
						/* Serial.println("Beginning Parameter Adjustments"); */  	/* Serial.println("Beginning Parameter Adjustments"); */   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						// Read sensor default FS values from parameter space
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_ParamRequest ,  0x4A ) ;  // Request to read parameter 74
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_AlgorithmControl ,  0x80 ) ;  // Request parameter transfer process
   
			
		
	
		
		
			
				
					
						uint8_t  param_xfer  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,   
			
		
	
		
		
			
				
					
						EM7180_ParamAcknowledge ) ;   
			
		
	
		
		
			
				
					
						while ( ! ( param_xfer  = =  0x4A ) )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							param_xfer  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ParamAcknowledge ) ;   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						param [ 0 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte0 ) ;   
			
		
	
		
		
			
				
					
						param [ 1 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte1 ) ;   
			
		
	
		
		
			
				
					
						param [ 2 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte2 ) ;   
			
		
	
		
		
			
				
					
						param [ 3 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte3 ) ;   
			
		
	
		
		
			
				
					
						EM7180_mag_fs  =  ( ( int16_t )  ( param [ 1 ]  < <  8 )  |  param [ 0 ] ) ;   
			
		
	
		
		
			
				
					
						EM7180_acc_fs  =  ( ( int16_t )  ( param [ 3 ]  < <  8 )  |  param [ 2 ] ) ;   
			
		
	
		
		
			
				
					
						/* Serial.print("Magnetometer Default Full Scale Range: +/-"); */   
			
		
	
		
		
			
				
					
						/* Serial.print(EM7180_mag_fs); */   
			
		
	
		
		
			
				
					
						/* Serial.println("uT"); */   
			
		
	
		
		
			
				
					
						/* Serial.print("Accelerometer Default Full Scale Range: +/-"); */   
			
		
	
		
		
			
				
					
						/* Serial.print(EM7180_acc_fs); */   
			
		
	
		
		
			
				
					
						/* Serial.println("g"); */   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_ParamRequest ,  0x4B ) ;  // Request to read  parameter 75
   
			
		
	
		
		
			
				
					
						param_xfer  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ParamAcknowledge ) ;   
			
		
	
		
		
			
				
					
						while ( ! ( param_xfer  = =  0x4B ) )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							param_xfer  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ParamAcknowledge ) ;   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						param [ 0 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte0 ) ;   
			
		
	
		
		
			
				
					
						param [ 1 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte1 ) ;   
			
		
	
		
		
			
				
					
						param [ 2 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte2 ) ;   
			
		
	
		
		
			
				
					
						param [ 3 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte3 ) ;   
			
		
	
		
		
			
				
					
						EM7180_gyro_fs  =  ( ( int16_t )  ( param [ 1 ]  < <  8 )  |  param [ 0 ] ) ;   
			
		
	
		
		
			
				
					
						/* Serial.print("Gyroscope Default Full Scale Range: +/-"); */   
			
		
	
		
		
			
				
					
						/* Serial.print(EM7180_gyro_fs); */   
			
		
	
		
		
			
				
					
						/* Serial.println("dps"); */   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_ParamRequest ,  0x00 ) ;  //End parameter transfer
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_AlgorithmControl ,  0x00 ) ;  // re-enable algorithm
   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						// Disable stillness mode for balancing robot application
  	// Disable stillness mode for balancing robot application
   
			
		
	
		
		
			
				
					
						EM 7180_set_integer_param( 0x49 ,  0x00 ) ;  	em7180_set_integer_param ( 0x49 ,  0x00 ) ;   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						// Write desired sensor full scale ranges to the EM7180
  	// Write desired sensor full scale ranges to the EM7180
   
			
		
	
		
		
			
				
					
						EM7180_set_mag_acc_FS ( magFS ,  accFS ) ;  // 1000 uT == 0x3E8, 8 g == 0x08
  	em7180_mag_acc_set_fs ( em7180 - > mag_fs ,  em7180 - > acc_fs ) ;  // 1000 uT == 0x3E8, 8 g == 0x08
   
			
				
				
			
		
	
		
		
			
				
					
						EM7180_set_gyro_FS ( gyroFS ) ;  // 2000 dps == 0x7D0
  	em7180_gyro_set_fs ( em7180 - > gyro_fs ) ;  // 2000 dps == 0x7D0
   
			
				
				
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						// Read sensor new FS values from parameter space
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_ParamRequest ,  0x4A ) ;  // Request to read  parameter 74
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_AlgorithmControl ,  0x80 ) ;  // Request parameter transfer process
   
			
		
	
		
		
			
				
					
						param_xfer  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ParamAcknowledge ) ;   
			
		
	
		
		
			
				
					
						while ( ! ( param_xfer  = =  0x4A ) )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							param_xfer  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ParamAcknowledge ) ;   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						param [ 0 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte0 ) ;   
			
		
	
		
		
			
				
					
						param [ 1 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte1 ) ;   
			
		
	
		
		
			
				
					
						param [ 2 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte2 ) ;   
			
		
	
		
		
			
				
					
						param [ 3 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte3 ) ;   
			
		
	
		
		
			
				
					
						EM7180_mag_fs  =  ( ( int16_t )  ( param [ 1 ]  < <  8 )  |  param [ 0 ] ) ;   
			
		
	
		
		
			
				
					
						EM7180_acc_fs  =  ( ( int16_t )  ( param [ 3 ]  < <  8 )  |  param [ 2 ] ) ;   
			
		
	
		
		
			
				
					
						/* Serial.print("Magnetometer New Full Scale Range: +/-"); */   
			
		
	
		
		
			
				
					
						/* Serial.print(EM7180_mag_fs); */   
			
		
	
		
		
			
				
					
						/* Serial.println("uT"); */   
			
		
	
		
		
			
				
					
						/* Serial.print("Accelerometer New Full Scale Range: +/-"); */   
			
		
	
		
		
			
				
					
						/* Serial.print(EM7180_acc_fs); */   
			
		
	
		
		
			
				
					
						/* Serial.println("g"); */   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_ParamRequest ,  0x4B ) ;  // Request to read  parameter 75
   
			
		
	
		
		
			
				
					
						param_xfer  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ParamAcknowledge ) ;   
			
		
	
		
		
			
				
					
						while ( ! ( param_xfer  = =  0x4B ) )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							param_xfer  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ParamAcknowledge ) ;   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						param [ 0 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte0 ) ;   
			
		
	
		
		
			
				
					
						param [ 1 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte1 ) ;   
			
		
	
		
		
			
				
					
						param [ 2 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte2 ) ;   
			
		
	
		
		
			
				
					
						param [ 3 ]  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SavedParamByte3 ) ;   
			
		
	
		
		
			
				
					
						EM7180_gyro_fs  =  ( ( int16_t )  ( param [ 1 ]  < <  8 )  |  param [ 0 ] ) ;   
			
		
	
		
		
			
				
					
						/* Serial.print("Gyroscope New Full Scale Range: +/-"); */   
			
		
	
		
		
			
				
					
						/* Serial.print(EM7180_gyro_fs); */   
			
		
	
		
		
			
				
					
						/* Serial.println("dps"); */   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_ParamRequest ,  0x00 ) ;  //End parameter transfer
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_AlgorithmControl ,  0x00 ) ;  // re-enable algorithm
   
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						// Read EM7180 status
  	// Read EM7180 status
   
			
		
	
		
		
			
				
					
						uint8_t  runStatus  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_RunStatus ) ;  	runStatus  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_RunStatus ) ;   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
						if ( runStatus  &  0x01 )  	if ( runStatus  &  0x01 )   
			
		
	
		
		
			
				
					
						{  	{   
			
		
	
		
		
			
				
					
							/* Serial.println(" EM7180 run status = normal mode"); */  		/* Serial.println(" EM7180 run status = normal mode"); */   
			
		
	
		
		
			
				
					
						}  	}   
			
		
	
		
		
			
				
					
						uint8_t  algoStatus  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  	algoStatus  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
						EM7180_AlgorithmStatus ) ;  	EM7180_AlgorithmStatus ) ;   
			
		
	
		
		
			
				
					
						if ( algoStatus  &  0x01 )  	if ( algoStatus  &  0x01 )   
			
		
	
		
		
			
				
					
						{  	{   
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -327,13 +142,13 @@ void em7180_init(uint8_t accBW, uint8_t gyroBW, uint16_t accFS, uint16_t gyroFS, 
			
		
	
		
		
			
				
					
						{  	{   
			
		
	
		
		
			
				
					
							/* Serial.println(" EM7180 unreliable sensor data"); */  		/* Serial.println(" EM7180 unreliable sensor data"); */   
			
		
	
		
		
			
				
					
						}  	}   
			
		
	
		
		
			
				
					
						uint8_t  passthruStatus  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  	passthruStatus  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
						EM7180_PassThruStatus ) ;  	EM7180_PassThruStatus ) ;   
			
		
	
		
		
			
				
					
						if ( passthruStatus  &  0x01 )  	if ( passthruStatus  &  0x01 )   
			
		
	
		
		
			
				
					
						{  	{   
			
		
	
		
		
			
				
					
							/* Serial.print(" EM7180 in passthru mode!"); */  		/* Serial.print(" EM7180 in passthru mode!"); */   
			
		
	
		
		
			
				
					
						}  	}   
			
		
	
		
		
			
				
					
						uint8_t  eventStatus  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_EventStatus ) ;  	eventStatus  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_EventStatus ) ;   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
						if ( eventStatus  &  0x01 )  	if ( eventStatus  &  0x01 )   
			
		
	
		
		
			
				
					
						{  	{   
			
		
	
		
		
			
				
					
							/* Serial.println(" EM7180 CPU reset"); */  		/* Serial.println(" EM7180 CPU reset"); */   
			
		
	
	
		
		
			
				
					
						
							
								 
						
						
							
								 
						
						
					 
					@ -362,8 +177,7 @@ void em7180_init(uint8_t accBW, uint8_t gyroBW, uint16_t accFS, uint16_t gyroFS, 
			
		
	
		
		
			
				
					
						HAL_Delay ( 1000 ) ;  // give some time to read the screen
  	HAL_Delay ( 1000 ) ;  // give some time to read the screen
   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						// Check sensor status
  	// Check sensor status
   
			
		
	
		
		
			
				
					
						uint8_t  sensorStatus  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  	sensorStatus  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SensorStatus ) ;   
			
				
				
			
		
	
		
		
			
				
					
						EM7180_SensorStatus ) ;   
			
		
	
		
		
	
		
		
			
				
					
						/* Serial.print(" EM7180 sensor status = "); */  	/* Serial.print(" EM7180 sensor status = "); */   
			
		
	
		
		
			
				
					
						/* Serial.println(sensorStatus); */  	/* Serial.println(sensorStatus); */   
			
		
	
		
		
			
				
					
						if ( sensorStatus  &  0x01 )  	if ( sensorStatus  &  0x01 )   
			
		
	
	
		
		
			
				
					
						
							
								 
						
						
							
								 
						
						
					 
					@ -405,6 +219,137 @@ void em7180_init(uint8_t accBW, uint8_t gyroBW, uint16_t accFS, uint16_t gyroFS, 
			
		
	
		
		
			
				
					
						/* Serial.println(" Hz"); */  	/* Serial.println(" Hz"); */   
			
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					void  em7180_chip_id_get ( )  
			
		
	
		
		
			
				
					
					{  
			
		
	
		
		
			
				
					
						// Read SENtral device information
   
			
		
	
		
		
			
				
					
						uint16_t  ROM1  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ROMVersion1 ) ;   
			
		
	
		
		
			
				
					
						uint16_t  ROM2  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ROMVersion2 ) ;   
			
		
	
		
		
			
				
					
						/* Serial.print("EM7180 ROM Version: 0x"); */   
			
		
	
		
		
			
				
					
						/* Serial.print(ROM1, HEX); */   
			
		
	
		
		
			
				
					
						/* Serial.println(ROM2, HEX); */   
			
		
	
		
		
			
				
					
						/* Serial.println("Should be: 0xE609"); */   
			
		
	
		
		
			
				
					
						uint16_t  RAM1  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_RAMVersion1 ) ;   
			
		
	
		
		
			
				
					
						uint16_t  RAM2  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_RAMVersion2 ) ;   
			
		
	
		
		
			
				
					
						/* Serial.print("EM7180 RAM Version: 0x"); */   
			
		
	
		
		
			
				
					
						/* Serial.print(RAM1); */   
			
		
	
		
		
			
				
					
						/* Serial.println(RAM2); */   
			
		
	
		
		
			
				
					
						uint8_t  PID  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ProductID ) ;   
			
		
	
		
		
			
				
					
						/* Serial.print("EM7180 ProductID: 0x"); */   
			
		
	
		
		
			
				
					
						/* Serial.print(PID, HEX); */   
			
		
	
		
		
			
				
					
						/* Serial.println(" Should be: 0x80"); */   
			
		
	
		
		
			
				
					
						uint8_t  RID  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_RevisionID ) ;   
			
		
	
		
		
			
				
					
						/* Serial.print("EM7180 RevisionID: 0x"); */   
			
		
	
		
		
			
				
					
						/* Serial.print(RID, HEX); */   
			
		
	
		
		
			
				
					
						/* Serial.println(" Should be: 0x02"); */   
			
		
	
		
		
			
				
					
					}  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					void  em7180_load_fw_from_eeprom ( )  
			
		
	
		
		
			
				
					
					{  
			
		
	
		
		
			
				
					
						// Check which sensors can be detected by the EM7180
   
			
		
	
		
		
			
				
					
						uint8_t  featureflag  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,   
			
		
	
		
		
			
				
					
						EM7180_FeatureFlags ) ;   
			
		
	
		
		
			
				
					
						if ( featureflag  &  0x01 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("A barometer is installed"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( featureflag  &  0x02 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("A humidity sensor is installed"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( featureflag  &  0x04 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("A temperature sensor is installed"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( featureflag  &  0x08 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("A custom sensor is installed"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( featureflag  &  0x10 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("A second custom sensor is installed"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( featureflag  &  0x20 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("A third custom sensor is installed"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						HAL_Delay ( 1000 ) ;  // give some time to read the screen
   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						// Check SENtral status, make sure EEPROM upload of firmware was accomplished
   
			
		
	
		
		
			
				
					
						uint8_t  STAT  =  ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )   
			
		
	
		
		
			
				
					
						    &  0x01 ) ;   
			
		
	
		
		
			
				
					
						if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x01 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("EEPROM detected on the sensor bus!"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x02 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("EEPROM uploaded config file!"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x04 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("EEPROM CRC incorrect!"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x08 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("EM7180 in initialized state!"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x10 )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("No EEPROM detected!"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
						int  count  =  0 ;   
			
		
	
		
		
			
				
					
						while ( ! STAT )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_ResetRequest ,  0x01 ) ;   
			
		
	
		
		
			
				
					
							HAL_Delay ( 500 ) ;   
			
		
	
		
		
			
				
					
							count + + ;   
			
		
	
		
		
			
				
					
							STAT  =  ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x01 ) ;   
			
		
	
		
		
			
				
					
							if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x01 )   
			
		
	
		
		
			
				
					
							{   
			
		
	
		
		
			
				
					
								/* Serial.println("EEPROM detected on the sensor bus!"); */   
			
		
	
		
		
			
				
					
							}   
			
		
	
		
		
			
				
					
							if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x02 )   
			
		
	
		
		
			
				
					
							{   
			
		
	
		
		
			
				
					
								/* Serial.println("EEPROM uploaded config file!"); */   
			
		
	
		
		
			
				
					
							}   
			
		
	
		
		
			
				
					
							if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x04 )   
			
		
	
		
		
			
				
					
							{   
			
		
	
		
		
			
				
					
								/* Serial.println("EEPROM CRC incorrect!"); */   
			
		
	
		
		
			
				
					
							}   
			
		
	
		
		
			
				
					
							if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x08 )   
			
		
	
		
		
			
				
					
							{   
			
		
	
		
		
			
				
					
								/* Serial.println("EM7180 in initialized state!"); */   
			
		
	
		
		
			
				
					
							}   
			
		
	
		
		
			
				
					
							if ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x10 )   
			
		
	
		
		
			
				
					
							{   
			
		
	
		
		
			
				
					
								/* Serial.println("No EEPROM detected!"); */   
			
		
	
		
		
			
				
					
							}   
			
		
	
		
		
			
				
					
							if ( count  >  10 )   
			
		
	
		
		
			
				
					
							{   
			
		
	
		
		
			
				
					
								break ;   
			
		
	
		
		
			
				
					
							}   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						if ( ! ( lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_SentralStatus )  &  0x04 ) )   
			
		
	
		
		
			
				
					
						{   
			
		
	
		
		
			
				
					
							/* Serial.println("EEPROM upload successful!"); */   
			
		
	
		
		
			
				
					
						}   
			
		
	
		
		
			
				
					
					}  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					uint8_t  em7180_status ( )  
			
		
	
		
		
			
				
					
					{  
			
		
	
		
		
			
				
					
						// Check event status register, way to check data ready by polling rather than interrupt
   
			
		
	
		
		
			
				
					
						uint8_t  c  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_EventStatus ) ;  // reading clears the register and interrupt
   
			
		
	
		
		
			
				
					
						return  c ;   
			
		
	
		
		
			
				
					
					}  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					uint8_t  em7180_errors ( )  
			
		
	
		
		
			
				
					
					{  
			
		
	
		
		
			
				
					
						uint8_t  c  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ErrorRegister ) ;  // check error register
   
			
		
	
		
		
			
				
					
						return  c ;   
			
		
	
		
		
			
				
					
					}  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					float  em7180_uint32_reg_to_float ( uint8_t  * buf ) float  em7180_uint32_reg_to_float ( uint8_t  * buf )  
			
		
	
		
		
			
				
					
					{ {  
			
		
	
		
		
			
				
					
						union  	union   
			
		
	
	
		
		
			
				
					
						
							
								 
						
						
							
								 
						
						
					 
					@ -431,19 +376,21 @@ float em7180_int32_reg_to_float(uint8_t *buf) 
			
		
	
		
		
			
				
					
						return  u . f ;  	return  u . f ;   
			
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					void  em7180_float_to_bytes ( float  param_val ,  uint8_t  * buf ) static  void  em7180_float_to_bytes ( float  param_val ,  uint8_t  * buf )  
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					{ {  
			
		
	
		
		
			
				
					
						union  	union   
			
		
	
		
		
			
				
					
						{  	{   
			
		
	
		
		
			
				
					
							float  f ;  		float  f ;   
			
		
	
		
		
			
				
					
							uint8_t  comp [ sizeof ( float ) ] ;  		uint8_t  u8 [ sizeof ( float ) ] ;   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
						}  u ;  	}  u ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						u . f  =  param_val ;  	u . f  =  param_val ;   
			
		
	
		
		
			
				
					
						for ( uint8_t  i  =  0 ;  i  <  sizeof ( float ) ;  i + + )  	for ( uint8_t  i  =  0 ;  i  <  sizeof ( float ) ;  i + + )   
			
		
	
		
		
			
				
					
						{  	{   
			
		
	
		
		
			
				
					
							buf [ i ]  =  u . comp [ i ] ;  		buf [ i ]  =  u . u8 [ i ] ;   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
						}  	}   
			
		
	
		
		
			
				
					
						// Convert to LITTLE ENDIAN
  	// Convert to LITTLE ENDIAN
   
			
		
	
		
		
			
				
					
						/* FIXME: What the hell? */   
			
		
	
		
		
			
				
					
						for ( uint8_t  i  =  0 ;  i  <  sizeof ( float ) ;  i + + )  	for ( uint8_t  i  =  0 ;  i  <  sizeof ( float ) ;  i + + )   
			
		
	
		
		
			
				
					
						{  	{   
			
		
	
		
		
			
				
					
							buf [ i ]  =  buf [ ( sizeof ( float )  -  1 )  -  i ] ;  		buf [ i ]  =  buf [ ( sizeof ( float )  -  1 )  -  i ] ;   
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -461,7 +408,7 @@ void em7180_gyro_set_fs(uint16_t gyro_fs) 
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_LoadParamByte1 ,  bytes [ 1 ] ) ;  // Gyro MSB
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_LoadParamByte1 ,  bytes [ 1 ] ) ;  // Gyro MSB
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_LoadParamByte2 ,  bytes [ 2 ] ) ;  // Unused
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_LoadParamByte2 ,  bytes [ 2 ] ) ;  // Unused
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_LoadParamByte3 ,  bytes [ 3 ] ) ;  // Unused
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_LoadParamByte3 ,  bytes [ 3 ] ) ;  // Unused
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_ParamRequest ,  0xCB ) ;  //Parameter 75; 0xCB is 75 decimal with the MSB set high to indicate a paramter write proces ss
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_ParamRequest ,  0xCB ) ;  //  Parameter 75; 0xCB is 75 decimal with the MSB set high to indicate a parame ter write process
   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_AlgorithmControl ,  0x80 ) ;  // Request parameter transfer procedure
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_AlgorithmControl ,  0x80 ) ;  // Request parameter transfer procedure
   
			
		
	
		
		
			
				
					
						STAT  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ParamAcknowledge ) ;  // Check the parameter acknowledge register and loop until the result matches parameter request byte
  	STAT  =  lsm6dsm_read_byte ( EM7180_ADDRESS ,  EM7180_ParamAcknowledge ) ;  // Check the parameter acknowledge register and loop until the result matches parameter request byte
   
			
		
	
		
		
			
				
					
						while ( ! ( STAT  = =  0xCB ) )  	while ( ! ( STAT  = =  0xCB ) )   
			
		
	
	
		
		
			
				
					
						
							
								 
						
						
							
								 
						
						
					 
					@ -520,7 +467,7 @@ void em7180_set_integer_param(uint8_t param, uint32_t param_val) 
			
		
	
		
		
			
				
					
					void  em7180_param_set_float ( uint8_t  param ,  float  param_val ) void  em7180_param_set_float ( uint8_t  param ,  float  param_val )  
			
		
	
		
		
			
				
					
					{ {  
			
		
	
		
		
			
				
					
						uint8_t  bytes [ 4 ] ,  STAT ;  	uint8_t  bytes [ 4 ] ,  STAT ;   
			
		
	
		
		
			
				
					
						float_to_bytes ( param_val ,  & bytes [ 0 ] ) ;  	em7180_ float_to_bytes( param_val ,  & bytes [ 0 ] ) ;   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
						param  =  param  |  0x80 ;  // Parameter is the decimal value with the MSB set high to indicate a paramter write processs
  	param  =  param  |  0x80 ;  // Parameter is the decimal value with the MSB set high to indicate a paramter write processs
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_LoadParamByte0 ,  bytes [ 0 ] ) ;  // Param LSB
  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_LoadParamByte0 ,  bytes [ 0 ] ) ;  // Param LSB
   
			
		
	
		
		
			
				
					
						lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_LoadParamByte1 ,  bytes [ 1 ] ) ;  	lsm6dsm_write_byte ( EM7180_ADDRESS ,  EM7180_LoadParamByte1 ,  bytes [ 1 ] ) ;   
			
		
	
	
		
		
			
				
					
						
							
								 
						
						
							
								 
						
						
					 
					@ -577,71 +524,88 @@ void em7180_magdata_get(int16_t *destination) 
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					float  em7180_mres_get ( uint8_t  Mscale ) float  em7180_mres_get ( uint8_t  Mscale )  
			
		
	
		
		
			
				
					
					{ {  
			
		
	
		
		
			
				
					
						float  m_res ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						switch ( Mscale )  	switch ( Mscale )   
			
		
	
		
		
			
				
					
						{  	{   
			
		
	
		
		
			
				
					
							// Possible magnetometer scales (and their register bit settings) are:
  		/*
   
			
				
				
			
		
	
		
		
			
				
					
							// 14 bit resolution (0) and 16 bit resolution (1)
  		 *  Possible  magnetometer  scales  ( and  their  register  bit  settings )  are :   
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
							 *  14  bit  resolution  ( 0 )  and  16  bit  resolution  ( 1 )   
			
		
	
		
		
			
				
					
							 */   
			
		
	
		
		
			
				
					
							case  MFS_14BITS :  		case  MFS_14BITS :   
			
		
	
		
		
			
				
					
								_mRes  =  10.  *  4912.  /  8190. ;  // Proper scale to return milliGauss
  			m_res  =  10.  *  4912.  /  8190. ;  // Proper scale to return milliGauss
   
			
				
				
			
		
	
		
		
			
				
					
								return  _mRes ;   
			
		
	
		
		
	
		
		
			
				
					
								break ;  			break ;   
			
		
	
		
		
			
				
					
							case  MFS_16BITS :  		case  MFS_16BITS :   
			
		
	
		
		
			
				
					
								_mRes  =  10.  *  4912.  /  32760.0 ;  // Proper scale to return milliGauss
  			m_res  =  10.  *  4912.  /  32760.0 ;  // Proper scale to return milliGauss
   
			
				
				
			
		
	
		
		
			
				
					
								return  _mRes ;  			break ;   
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
							default :   
			
		
	
		
		
			
				
					
								m_res  =  NAN ;   
			
		
	
		
		
			
				
					
								break ;  			break ;   
			
		
	
		
		
			
				
					
						}  	}   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						return  m_res ;   
			
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					float  em7180_gres_get ( uint8_t  gscale ) float  em7180_gres_get ( uint8_t  gscale )  
			
		
	
		
		
			
				
					
					{ {  
			
		
	
		
		
			
				
					
						float  g_res ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						switch ( gscale )  	switch ( gscale )   
			
		
	
		
		
			
				
					
						{  	{   
			
		
	
		
		
			
				
					
							// Possible gyro scales (and their register bit settings) are:
  		/*
   
			
				
				
			
		
	
		
		
			
				
					
							// 250 DPS (00), 500 DPS (01), 1000 DPS (10), and 2000 DPS  (11).
  		 *  Possible  gyro  scales  ( and  their  register  bit  settings )  are :   
			
				
				
			
		
	
		
		
			
				
					
							// Here's a bit of an algorith to calculate DPS/(ADC tick) based on that 2-bit value:
  		 *  250  DPS  ( 00 ) ,  500  DPS  ( 01 ) ,  1000  DPS  ( 10 ) ,  and  2000  DPS  ( 11 ) .   
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
							 *  Here ' s  a  bit  of  an  algorithm  to  calculate  DPS / ( ADC  tick )  based  on  that  2 - bit  value :   
			
		
	
		
		
			
				
					
							 */   
			
		
	
		
		
			
				
					
							case  GFS_250DPS :  		case  GFS_250DPS :   
			
		
	
		
		
			
				
					
								_gRes  =  250.0  /  32768.0 ;  			g_res  =  250.0  /  32768.0 ;   
			
				
				
			
		
	
		
		
			
				
					
								return  _gRes ;   
			
		
	
		
		
	
		
		
			
				
					
								break ;  			break ;   
			
		
	
		
		
			
				
					
							case  GFS_500DPS :  		case  GFS_500DPS :   
			
		
	
		
		
			
				
					
								_gRes  =  500.0  /  32768.0 ;  			g_res  =  500.0  /  32768.0 ;   
			
				
				
			
		
	
		
		
			
				
					
								return  _gRes ;   
			
		
	
		
		
	
		
		
			
				
					
								break ;  			break ;   
			
		
	
		
		
			
				
					
							case  GFS_1000DPS :  		case  GFS_1000DPS :   
			
		
	
		
		
			
				
					
								_gRes  =  1000.0  /  32768.0 ;  			g_res  =  1000.0  /  32768.0 ;   
			
				
				
			
		
	
		
		
			
				
					
								return  _gRes ;   
			
		
	
		
		
	
		
		
			
				
					
								break ;  			break ;   
			
		
	
		
		
			
				
					
							case  GFS_2000DPS :  		case  GFS_2000DPS :   
			
		
	
		
		
			
				
					
								_gRes  =  2000.0  /  32768.0 ;  			g_res  =  2000.0  /  32768.0 ;   
			
				
				
			
		
	
		
		
			
				
					
								return  _gRes ;  			break ;   
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
							default :   
			
		
	
		
		
			
				
					
								g_res  =  NAN ;   
			
		
	
		
		
			
				
					
								break ;  			break ;   
			
		
	
		
		
			
				
					
						}  	}   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						return  g_res ;   
			
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					float  em7180_ares_get ( uint8_t  ascale ) float  em7180_ares_get ( uint8_t  ascale )  
			
		
	
		
		
			
				
					
					{ {  
			
		
	
		
		
			
				
					
						float  a_res ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						switch ( ascale )  	switch ( ascale )   
			
		
	
		
		
			
				
					
						{  	{   
			
		
	
		
		
			
				
					
							// Possible accelerometer scales (and their register bit settings) are:
  		/*
   
			
				
				
			
		
	
		
		
			
				
					
							// 2 Gs (00), 4 Gs (01), 8 Gs (10), and 16 Gs  (11).
  		 *  Possible  accelerometer  scales  ( and  their  register  bit  settings )  are :   
			
				
				
			
		
	
		
		
			
				
					
							// Here's a bit of an algorith to calculate DPS/(ADC tick) based on that 2-bit value:
  		 *  2  Gs  ( 00 ) ,  4  Gs  ( 01 ) ,  8  Gs  ( 10 ) ,  and  16  Gs  ( 11 ) .   
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
							 *  Here ' s  a  bit  of  an  algorithm  to  calculate  DPS / ( ADC  tick )  based  on  that  2 - bit  value :   
			
		
	
		
		
			
				
					
							 */   
			
		
	
		
		
			
				
					
							case  AFS_2G :  		case  AFS_2G :   
			
		
	
		
		
			
				
					
								_aRes  =  2.0  /  32768.0 ;  			a_res  =  2.0  /  32768.0 ;   
			
				
				
			
		
	
		
		
			
				
					
								return  _aRes ;   
			
		
	
		
		
	
		
		
			
				
					
								break ;  			break ;   
			
		
	
		
		
			
				
					
							case  AFS_4G :  		case  AFS_4G :   
			
		
	
		
		
			
				
					
								_aRes  =  4.0  /  32768.0 ;  			a_res  =  4.0  /  32768.0 ;   
			
				
				
			
		
	
		
		
			
				
					
								return  _aRes ;   
			
		
	
		
		
	
		
		
			
				
					
								break ;  			break ;   
			
		
	
		
		
			
				
					
							case  AFS_8G :  		case  AFS_8G :   
			
		
	
		
		
			
				
					
								_aRes  =  8.0  /  32768.0 ;  			a_res  =  8.0  /  32768.0 ;   
			
				
				
			
		
	
		
		
			
				
					
								return  _aRes ;   
			
		
	
		
		
	
		
		
			
				
					
								break ;  			break ;   
			
		
	
		
		
			
				
					
							case  AFS_16G :  		case  AFS_16G :   
			
		
	
		
		
			
				
					
								_aRes  =  16.0  /  32768.0 ;  			a_res  =  16.0  /  32768.0 ;   
			
				
				
			
		
	
		
		
			
				
					
								return  _aRes ;  			break ;   
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
							default :   
			
		
	
		
		
			
				
					
								a_res  =  NAN ;   
			
		
	
		
		
			
				
					
								break ;  			break ;   
			
		
	
		
		
			
				
					
						}  	}   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
						return  a_res ;   
			
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					int16_t  em7180_baro_get ( ) int16_t  em7180_baro_get ( )