mpu_dmp.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #ifndef MPU_H__
  2. #define MPU_H__
  3. #include "user_twim.h"
  4. #include "mygyro.h"
  5. #include "nrf_drv_gpiote.h"
  6. #define MPU_CS_ENABLE nrf_gpio_pin_write(MPU_SS_PIN, 0);
  7. #define MPU_CS_DISABLE nrf_gpio_pin_write(MPU_SS_PIN, 1);
  8. #define DEFAULT_MPU_HZ IMU_SF
  9. #define ACCEL_ON 0x01
  10. #define GYRO_ON 0x02
  11. #define MOTION 0
  12. #define NO_MOTION 1
  13. struct rx_s {
  14. uint8_t header[3];
  15. uint8_t cmd;
  16. };
  17. struct hal_s {
  18. uint8_t sensors;
  19. uint8_t dmp_on;
  20. uint8_t wait_for_tap;
  21. volatile uint8_t new_gyro;
  22. uint16_t report;
  23. uint16_t dmp_features;
  24. uint8_t motion_int_mode;
  25. struct rx_s rx;
  26. };
  27. typedef void (*set_acc_gyro_act_func)(void);
  28. typedef void (*set_sleep_act_func)(bool stat);
  29. //typedef void (*set_lowpower_act_func)(bool stat);
  30. typedef bool (*get_mpu_testing_act_func)(void);
  31. typedef void (*mpu_check_finish_func)(void);
  32. typedef enum{
  33. MPU_NULL=0,
  34. MPU_INIT,
  35. MPU_ORIGIN,
  36. MPU_OFFSET,
  37. MPU_GET_DATA,
  38. MPU_SLEEP_ACT,
  39. MPU_SLEEP,
  40. MPU_NO_REG_ACT,
  41. MPU_NO_REG,
  42. MPU_WAKEUP,
  43. MPU_LOWPOWER_ACT,
  44. MPU_LOWPOWER,
  45. MPU_DELAY,
  46. MPU_FAULT,
  47. }MPU_WORK_STAT;
  48. typedef enum{
  49. MPU_LOWPOWER_DELAY,
  50. MPU_LOWPOWER_OFFSET,
  51. MPU_LOWPOWER_OFFSET_DELAY,
  52. MPU_LOWPOWER_ACTION
  53. }MPU_LOWPOWER_SEL;
  54. typedef struct{
  55. set_acc_gyro_act_func acc_gyro_func;
  56. set_sleep_act_func sleep_act_func;
  57. // set_lowpower_act_func lowpower_act_func;
  58. get_mpu_testing_act_func mpu_testing_act_func;
  59. mpu_check_finish_func check_finish_func ;
  60. MPU_WORK_STAT work_stat;
  61. int16_t acc[3];
  62. int16_t gyro[3];
  63. bool offset_stat;
  64. MPU_LOWPOWER_SEL lowpower_stat;
  65. int8_t *gyro_orientation;
  66. uint8_t sensor_stat;
  67. bool test_stop;
  68. uint32_t mpu_wakeup;
  69. }mpu_work_s;
  70. #define MPU_WORK_DEFAULT_CONFIG \
  71. { \
  72. .sleep_act_func=NULL, \
  73. .acc_gyro_func=NULL, \
  74. .work_stat=MPU_INIT, \
  75. .acc={0}, \
  76. .gyro={0}, \
  77. .offset_stat=false, \
  78. .lowpower_stat=MPU_LOWPOWER_DELAY, \
  79. .gyro_orientation=NULL, \
  80. .sensor_stat=0, \
  81. .test_stop=false, \
  82. .mpu_wakeup=0, \
  83. };
  84. typedef enum
  85. {
  86. MPU_QUEUE_TIME=0,
  87. MPU_QUEUE_INT=1,
  88. MPU_QUEUE_SLEEP=2,
  89. }MPU_QUEUE_TYPE;
  90. void Set_Mpu_Queue(MPU_QUEUE_TYPE type);
  91. void mpu_dio_init(nrf_drv_gpiote_in_config_t *config,nrfx_gpiote_evt_handler_t evt_handler);
  92. /**
  93. @brief Function for writing a mpu register contents over TWI.
  94. @param[in] register_address Register address to start writing to
  95. @param[in] value Value to write to register
  96. @retval true Register write succeeded
  97. @retval false Register write failed
  98. */
  99. static void mpu_register_write(uint8_t register_address, uint8_t value);
  100. /**
  101. @brief Function for reading mpu register contents over TWI.
  102. Reads one or more consecutive registers.
  103. @param[in] register_address Register address to start reading from
  104. @param[in] number_of_bytes Number of bytes to read
  105. @param[out] destination Pointer to a data buffer where read data will be stored
  106. @retval true Register read succeeded
  107. @retval false Register read failed
  108. */
  109. static void mpu_read_write(uint8_t register_address);
  110. /**
  111. @brief Function for reading mpu register contents over TWI.
  112. Reads one or more consecutive registers.
  113. @param[in] register_address Register address to start reading from
  114. @param[in] number_of_bytes Number of bytes to read
  115. @param[out] destination Pointer to a data buffer where read data will be stored
  116. @retval true Register read succeeded
  117. @retval false Register read failed
  118. */
  119. static void mpu_register_read(uint8_t * destination, uint8_t number_of_bytes);
  120. void mpu_timers_init(void);
  121. void mpu_Start(uint32_t time);
  122. bool mpu_offset_start(void);
  123. void Set_Mpu_Value(void);
  124. void mpu_int_act(void);
  125. void mpu_set_sleep(void);
  126. void mpu_work_init(void);
  127. void mpu_set_no_reg(void);
  128. void set_mpu_sleep_act_func(set_sleep_act_func func);
  129. //void set_mpu_lowpower_act_func(set_lowpower_act_func func);
  130. void set_mpu_testing_act_func(get_mpu_testing_act_func func);
  131. void set_mpu_check_finish_func(mpu_check_finish_func func);
  132. void mpu_set_test_stop(void);
  133. #ifdef MPU_SPI
  134. void mpu_spi_ss_init(void);
  135. int spi_write(uint8_t addr, uint8_t reg, uint8_t len, uint8_t *data);
  136. int spi_read(uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf);
  137. #else
  138. int i2c_write(uint8_t addr, uint8_t reg, uint8_t len, uint8_t *data);
  139. int i2c_read(uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf);
  140. #endif
  141. extern mpu_work_s mpu_work;
  142. extern my_gyro_t gyro_s;
  143. #endif //MPU_H__