drv_mpu9250.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "drv_mpu9250.h"
  2. #include "nrf_delay.h"
  3. #include "twi_master.h"
  4. #include "drv_iic_01.h"
  5. #include "drv_mpu9250.h"
  6. #include "drv_iic_01.h"
  7. bool MPU9250_Write_Byte(uint8_t Device_Address,uint8_t REG_Address,uint8_t REG_data)
  8. {
  9. return IIC_01_WriteBytes(Device_Address,REG_Address,&REG_data,1);
  10. }
  11. bool MPU9250_Read_nBytes(uint8_t Device_Address,uint8_t REG_Address,uint8_t *readDataBuf,uint8_t readDataLen)
  12. {
  13. return IIC_01_ReadBytes(Device_Address,REG_Address,readDataBuf,readDataLen);
  14. }
  15. bool MPU9250_register_write_len(uint8_t Device_Address,uint8_t register_address, uint8_t len,uint8_t *buf)
  16. {
  17. return IIC_01_WriteBytes(Device_Address,register_address,buf,len);
  18. }
  19. bool MPU9250_register_read_len(uint8_t Device_Address,uint8_t register_address, uint8_t number_of_bytes,uint8_t * destination )
  20. {
  21. return IIC_01_ReadBytes(Device_Address,register_address,destination,number_of_bytes);
  22. }
  23. //**************************************
  24. //写入一个字节数据
  25. ////**************************************
  26. //bool MPU9250_Write_Byte(uint8_t Device_Address,uint8_t REG_Address,uint8_t REG_data)
  27. //{
  28. // uint8_t buf[2];
  29. // SEGGER_RTT_printf(0,"MPU9250_Write_Byte\n");
  30. // buf[0] = REG_Address;
  31. // buf[1] = REG_data;
  32. // return twi_master_transfer(Device_Address, buf, 2, TWI_ISSUE_STOP);
  33. //}
  34. //**************************************
  35. //读取N个字节数据
  36. //**************************************
  37. //bool MPU9250_Read_nBytes(uint8_t Device_Address,uint8_t REG_Address,uint8_t *readDataBuf,uint8_t readDataLen)/*
  38. //{
  39. // bool ret;
  40. // ret = twi_master_transfer(Device_Address, &REG_Address, 1, TWI_DONT_ISSUE_STOP);
  41. // ret &= twi_master_transfer(Device_Address|TWI_READ_BIT, readDataBuf, readDataLen, TWI_ISSUE_STOP);
  42. // return ret;
  43. //}
  44. //bool MPU9250_register_write_len(uint8_t Device_Address,uint8_t register_address, uint8_t len,uint8_t *buf)
  45. //{
  46. // uint8_t w2_data[50],i;
  47. // w2_data[0] = register_address;
  48. // for(i=0;i<len;i++)w2_data[i+1] = *(buf+i);
  49. //
  50. // if(twi_master_transfer(Device_Address, w2_data, len+1, TWI_ISSUE_STOP) == true)return 0;
  51. // else return true;
  52. //}
  53. //bool MPU9250_register_read_len(uint8_t Device_Address,uint8_t register_address, uint8_t number_of_bytes,uint8_t * destination )
  54. //{
  55. // bool transfer_succeeded;
  56. // transfer_succeeded = twi_master_transfer(Device_Address, &register_address, 1, TWI_DONT_ISSUE_STOP);
  57. // transfer_succeeded &= twi_master_transfer(Device_Address|TWI_READ_BIT, destination, number_of_bytes, TWI_ISSUE_STOP);
  58. //
  59. // if(transfer_succeeded == true)return 0;
  60. // else return true;
  61. //}
  62. bool MPU9250_Reset(void)
  63. {
  64. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_PWR_MGMT_1,0x80)) return false; //复位所有寄存器,复位完后自动进入睡眠模式,所以要sleep位要清0
  65. SEGGER_RTT_printf(0,"MPU9250_Reset OK\n");
  66. return true;
  67. }
  68. bool MPU9250_sleep(void)
  69. {
  70. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_PWR_MGMT_1,0x00)) return false; //唤醒mpu9250
  71. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_PWR_MGMT_2,0x3f)) return false; //关闭陀螺仪和三轴
  72. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_PWR_MGMT_1,0x60)) return false; //enable cycle mode (accel low power mode)
  73. SEGGER_RTT_printf(0,"MPU9250_Acc_Init OK\n");
  74. return true;
  75. }
  76. bool MPU9250_MageInit(void)
  77. {
  78. if(!MPU9250_Write_Byte(AK8963_I2C_ADDR,AK8963_CNTL1,0x00)) return false; //power-down mode
  79. SEGGER_RTT_printf(0,"MPU9250_MageInit OK\n");
  80. return true;
  81. }
  82. bool MPU9250_Acc_Init(void)
  83. {
  84. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_PWR_MGMT_1,0x00)) return false; //唤醒mpu9250
  85. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_PWR_MGMT_2,0x07)) return false; //关闭陀螺仪三轴
  86. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_ACCEL_CONFIG,0x18)) return false; //(0x00 +-2g;) ( 0x08 +-4g;) (0x10 +-8g;) (0x18 +-16g)
  87. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_ACCEL_CONFIG_2,0x09)) return false; //set accel lpf setting to 184 hz bandwidth
  88. // if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_LP_ACCEL_ODR,0x02)) return false; //set frequency of wake-up 低功耗加速度计输出速率0.98hz
  89. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_LP_ACCEL_ODR,0x07)) return false; //set frequency of wake-up 低功耗加速度计输出速率31.25hz
  90. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_PWR_MGMT_1,0x20)) return false; //enable cycle mode (accel low power mode)
  91. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_INT_PIN_CFG,0x02)) return false; //bypass enable
  92. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_USER_CTRL,0x00)) return false; //使能I2C
  93. SEGGER_RTT_printf(0,"MPU9250_Acc_Init OK\n");
  94. return true;
  95. }
  96. bool MPU9250_Read_Acc(int16_t* acc)
  97. {
  98. uint8_t buf[6];
  99. // SEGGER_RTT_printf(0,"MPU9250_Read_nBytes=%d\n",MPU9250_Read_nBytes(MPU9250_I2C_ADDR,MPU9250_ACCEL_XOUT_H, buf, 6));
  100. if(!MPU9250_Read_nBytes(MPU9250_I2C_ADDR,MPU9250_ACCEL_XOUT_H, buf, 6)) return false;//读取加速度,温度,陀螺仪传感器
  101. acc[0] = ((int16_t)buf[0]<<8)|((int16_t)buf[1]);
  102. acc[1] = ((int16_t)buf[2]<<8)|((int16_t)buf[3]);
  103. acc[2] = ((int16_t)buf[4]<<8)|((int16_t)buf[5]);
  104. return true;
  105. }
  106. //int MPU9250_LowPowerAccelModeInit(void)
  107. //{
  108. // if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_PWR_MGMT_1,0x00)) return 2; //唤醒mpu9250
  109. // if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_PWR_MGMT_2,0x07)) return 3; //关闭陀螺仪三轴
  110. // if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_ACCEL_CONFIG,0x18)) return 4; //(0x00 +-2g;) ( 0x08 +-4g;) (0x10 +-8g;) (0x18 +-16g)
  111. // if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_ACCEL_CONFIG_2,0x09)) return 5; //set accel lpf setting to 184 hz bandwidth
  112. // if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_LP_ACCEL_ODR,0x02)) return 6; //set frequency of wake-up 低功耗加速度计输出速率0.98hz
  113. // if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_PWR_MGMT_1,0x20)) return 7; //enable cycle mode (accel low power mode)
  114. // if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_INT_PIN_CFG,0x02)) return 8; //bypass enable
  115. // if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_USER_CTRL,0x00)) return 9; //使能I2C
  116. // if(!MPU9250_Write_Byte(AK8963_I2C_ADDR,AK8963_CNTL1,0x00)) return 10; //power-down mode
  117. //
  118. // return 0;
  119. //}
  120. bool MPU9250_Init(void)
  121. {
  122. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_PWR_MGMT_1,0x00)) return false; //唤醒mpu9250
  123. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_SMPLRT_DIV,0x07)) return false;
  124. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_CONFIG,0x00)) return false; //低通滤波5hz
  125. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_GYRO_CONFIG,MPU9250_GYR_SEL)) return false; //不自检,2000deg/s
  126. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_ACCEL_CONFIG,MPU9250_ACC_SEL)) return false; //(0x00 +-2g;) ( 0x08 +-4g;) (0x10 +-8g;) (0x18 +-16g)
  127. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_ACCEL_CONFIG_2,0x04)) return false; //
  128. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_INT_PIN_CFG,0x02)) return false;
  129. if(!MPU9250_Write_Byte(MPU9250_I2C_ADDR,MPU9250_USER_CTRL,0x00)) return false; //使能I2C
  130. if(!MPU9250_Write_Byte(AK8963_I2C_ADDR,AK8963_CNTL1,0x0f)) return false;
  131. SEGGER_RTT_printf(0,"MPU9250_Init OK\n");
  132. return true;
  133. }
  134. bool MPU9250_Read(int16_t* acc,int16_t* gyr,int16_t* mag,int16_t* temperature)
  135. {
  136. uint8_t buf[14],bufM[6];
  137. if(!MPU9250_Read_nBytes(MPU9250_I2C_ADDR,MPU9250_ACCEL_XOUT_H, buf, 14)) return false;//读取加速度,温度,陀螺仪传感器
  138. if(!MPU9250_Read_nBytes(AK8963_I2C_ADDR,AK8963_HXL, bufM, 6)) return false;//读取地磁传感器
  139. acc[0] = ((int16_t)buf[0]<<8)|((int16_t)buf[1]);
  140. acc[1] = ((int16_t)buf[2]<<8)|((int16_t)buf[3]);
  141. acc[2] = ((int16_t)buf[4]<<8)|((int16_t)buf[5]);
  142. temperature[0] = ((int16_t)buf[6]<<8)|((int16_t)buf[7]);
  143. gyr[0] = ((int16_t)buf[8]<<8)|((int16_t)buf[9]);
  144. gyr[1] = ((int16_t)buf[10]<<8)|((int16_t)buf[11]);
  145. gyr[2] = ((int16_t)buf[12]<<8)|((int16_t)buf[13]);
  146. if(!MPU9250_Write_Byte(AK8963_I2C_ADDR,AK8963_CNTL1,0x11)) return false;//每读一次数据,ak8963会自动进入powerdown模式,这里需要重新设定为单测量模式
  147. mag[0] = ((int16_t)bufM[1]<<8)|((int16_t)bufM[0]);
  148. mag[1] = ((int16_t)bufM[3]<<8)|((int16_t)bufM[2]);
  149. mag[2] = ((int16_t)bufM[5]<<8)|((int16_t)bufM[4]);
  150. return true;
  151. }