element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Experts, Learning and Guidance
  • Technologies
  • More
Experts, Learning and Guidance
Ask an Expert Forum stm32 i2c mpu9250
  • Blog
  • Forum
  • Documents
  • Leaderboard
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Experts, Learning and Guidance to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 5 replies
  • Subscribers 285 subscribers
  • Views 1350 views
  • Users 0 members are here
  • firmware
  • help
  • robotics
  • accelerometer
  • stm32
  • magnetometer
  • microcontroller
  • gyroscope
  • new_member_introduction
  • mpu9250
  • software
Related
See a helpful answer?

Be sure to click 'more' and select 'suggest as answer'!

If you're the thread creator, be sure to click 'more' then 'Verify as Answer'!

stm32 i2c mpu9250

pooja_14
pooja_14 over 1 year ago

Im working on stm32f4 series microcontroller.

I'm trying to read mpu9250 three sensors(accelerometer,gyroscope ,magnetometer) data from fifo buffer. I'm not able to read magnetormeter data from FIFO buffer Because I may have made errors on the I2C master and slave(slave 0) configuration. I have attached the code. I require help in resolving this.

void mpu9250_init()
{
	HAL_StatusTypeDef ret = HAL_I2C_IsDeviceReady(&hi2c1,0x68<<1,1,100);

	// REG 107 PWR_MGMT_1 wake up
	//SLEEP When set to 0, this bit puts the MPU out of sleep mode.0x6b
	uint8_t data = 0x00;
	ret = HAL_I2C_Mem_Write(&hi2c1, 0x68<<1,107,1,&data,1,100);
    HAL_Delay(100);

	//register 27 or 0x1b,GYRO_FS_SEL configure gyro sensitivity full scale range
	data = 0x10; // +/- 1000 degree/sec 32.8
	ret = HAL_I2C_Mem_Write(&hi2c1, 0x68<<1, 27, 1,&data,1,100);
	HAL_Delay(100);

	//register 28 or 0x1c,ACCEL_FS_SEL configure ACCEL_CONFIG
	data = 0x10; // +/- 8g  4,096
	ret = HAL_I2C_Mem_Write(&hi2c1, 0x68<<1, 28, 1,&data,1,100);
	HAL_Delay(100);

	data = 0x01; // DLPF (184HZ BW gyroscope) results in 1000hz output
	ret = HAL_I2C_Mem_Write(&hi2c1,0x68<<1,26, 1,&data,1,100);
	HAL_Delay(100);

	data = 0x01; // DLPF (ACC CONFIG2 218.1 BW)
    ret = HAL_I2C_Mem_Write(&hi2c1, 0x68<<1,29 , 1,&data,1,100);
    HAL_Delay(100);

	data = 0x04; // sample rate divider
	ret = HAL_I2C_Mem_Write(&hi2c1, 0x68<<1, 25, 1,&data,1,100);
	HAL_Delay(100);

//Magnetometer I2C Master

//uint8_t temp6_data = 0X02; // Set bypass mode for the magnetometer.(im not using this bcoz i want to do with i2c master)
//ret = HAL_I2C_Mem_Write(&hi2c1,0x68<<1,55,1,&temp6_data,1,100);
//HAL_Delay(200);

	   //( PWR_MGMNT_1, CLOCK_SEL_PLL )
			data = 0x01;
			ret = HAL_I2C_Mem_Write(&hi2c1, 0x68<<1,107, 1,&data,1,100);
			HAL_Delay(100);

	   //( USER_CTRL, I2C_MST_EN )
			//Enable I2C Master Mode
			data = 0b0010000;//(bit 5)     
			HAL_I2C_Mem_Write(&hi2c1, 0x68 << 1,106, 1, &data, 1,100);
			HAL_Delay(100);

	   //( I2C_MST_CTRL,I2C_MST_CLK )
			//set the I2C bus speed to 400 kHz ,set 13 = D
		    data = 0x0D; // Enable I2C Master reg 36 I2C_MST_CTRL (0X24)
			HAL_I2C_Mem_Write(&hi2c1, 0x68 << 1,36, 1, &data, 1,100);
			HAL_Delay(200);

//Configure i2c slave register
		//Set SLV0 to read from AK8963
		// AK8963_ADDR; // Set I2C address of magnetometer // REG 43(0X2B) I2C_SLV0_ADDR
			data =  0x8C;//0b10001100
			ret = HAL_I2C_Mem_Write(&hi2c1, 0x68 << 1,37, 1, &data, 1,100);
			HAL_Delay(100);

		//Configure the I2C Master to access magnetometer data
			data = 0X03; //Set Slave 0 to read from AK8963 //I2C_SLV0_REG
			ret = HAL_I2C_Mem_Write(&hi2c1, 0x68 << 1,38, 1, &data, 1,100);
			HAL_Delay(100);

		//Set the number of bytes to read from AK8963
			data = 0x88; // 8 bytes of data from magnetometer // I2C_SLV0_CTRL 1000 1000
			ret = HAL_I2C_Mem_Write(&hi2c1, 0x68 << 1,39, 1, &data, 1,100);
			HAL_Delay(100);

//AK8963(magnetometer) init

	   //(AK8963_CNTL1,AK8963_PWR_DOWN)
			data = 0x00; //power down the magnetometer (CNTL1)
			ret = HAL_I2C_Mem_Write(&hi2c1,0x0C<<1,0x0A,1,&data,1,100);

	   // Set 16-bits output & fuse ROM access mode
			data = 0x0F;
			ret = HAL_I2C_Mem_Write(&hi2c1,0x0C<<1,0x0A,1,&data,1,100);
			//power down the magnetometer (CNTL1)
			data = 0x00;
			ret = HAL_I2C_Mem_Write(&hi2c1,0x0C<<1,0x0A,1,&data,1,100); 

	   //reset magnetometer
		    data = 0x01;// CONTROL 2  (0 NORMAL OR 1 RESET)
			ret = HAL_I2C_Mem_Write(&hi2c1,0x0C<<1,0x0B,1,&data,1,100);

	   // Request continuous magnetometer measurements
			data = 0x06;
			ret = HAL_I2C_Mem_Write(&hi2c1,0x0C<<1,0x0A,1,&data,1,100);

//Enable fifo for Accelerometer,Magnetometer,Gyroscope

		   // set fifo en (bit 6) in USER_CTRL REG (0X6A)
			  data = 0b01100000;//40 fifo enable //bit 5 for i2c master enable
			  ret = HAL_I2C_Mem_Write(&hi2c1,0x68<<1,106,1,&data,1,100);
			  HAL_Delay(100);

			  data = 0b01000000;//bit 6 set to 1 (no overwrite)
			  ret = HAL_I2C_Mem_Write(&hi2c1,0x68<<1,26,1,&data,1,100);
			  HAL_Delay(100);

	       //Enable fifo for gyro and acc and mag - in REG 35 set  0b01111100 0x78
			  data = 0b01111001;//
			  ret = HAL_I2C_Mem_Write(&hi2c1,0x68<<1,35,1,&data,1,100);
			  HAL_Delay(100);

}
//Registers 59 to 64 – Accelerometer Measurements
//Registers 0X03 to 0x08 - Magnetometer Measurements
//Registers 67 to 72 – Gyroscope Measurements

uint8_t data_acc[6]={};
uint8_t data_gyro[6]={};
uint8_t data_mag[6]={};

uint8_t fifo_count[2];
uint16_t fifo_size;
uint8_t fifo_data[23];
uint16_t length = sizeof(fifo_data);

#define MPU9250_ADD_WRITE	0x68<<1
#define MPU9250_ADD_READ	((MPU9250_ADD_WRITE)|(0x01))
void mpu9250_read()
{
	//FIFO_COUNTH 114 ,FIFO_COUNTL 115,FIFO_R_W 116
	HAL_StatusTypeDef ret1 = HAL_I2C_Mem_Read(&hi2c1,MPU9250_ADD_READ,114,1,&fifo_count[0],6,100);
	ret1 = HAL_I2C_Mem_Read(&hi2c1,MPU9250_ADD_READ,115,1,&fifo_count[1],6,100);
	fifo_size = (fifo_count[0]<<8)|fifo_count[1];

	if (fifo_size < length)
	{
		length = fifo_size;
	}
	ret1 = HAL_I2C_Mem_Read(&hi2c1,0x68<<1|0x01,116,1,fifo_data,length,100);
	//return fifo_data[0];

	x_acc = (uint16_t)fifo_data[0]<<8|fifo_data[1];
	y_acc = (uint16_t)fifo_data[2]<<8|fifo_data[3];
	z_acc = (uint16_t)fifo_data[4]<<8|fifo_data[5];

	x_gyro = (uint16_t)fifo_data[6]<<8|fifo_data[7];
	y_gyro = (uint16_t)fifo_data[8]<<8|fifo_data[9];
	z_gyro = (uint16_t)fifo_data[10]<<8|fifo_data[11];

	x_mag = (uint16_t)fifo_data[12]<<8|fifo_data[13];
	y_mag = (uint16_t)fifo_data[14]<<8|fifo_data[15];
	z_mag = (uint16_t)fifo_data[16]<<8|fifo_data[17];
}

  • Sign in to reply
  • Cancel

Top Replies

  • michaelkellett
    michaelkellett over 1 year ago +1
    When I have issues with and serial (UART or I2C or SPI ) link the first thing I do is connect a scope so I can see what is happening on the bus . Have you got a scope ? If so does it have protocol…
  • aswinvenu
    aswinvenu over 1 year ago +1 verified
    Two points here 1) Make sure that HAL_I2C_isDeviceReady is returning true. else you can loop it until the peripheral is ready 2) Looks like your HAL_I2C_Mem_Read function call is made with the wrong…
Parents
  • michaelkellett
    0 michaelkellett over 1 year ago

    When I have issues with and serial (UART or I2C or SPI ) link the first thing I do is connect a scope so I can see what is happening on the bus .

    Have you got a scope ?

    If so does it have protocol decoding ?

    MK

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • michaelkellett
    0 michaelkellett over 1 year ago

    When I have issues with and serial (UART or I2C or SPI ) link the first thing I do is connect a scope so I can see what is happening on the bus .

    Have you got a scope ?

    If so does it have protocol decoding ?

    MK

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube