123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #include <stdbool.h>
- #include <stdint.h>
- #include "twi_master.h"
- #include "mpu6050.h"
- #define ADDRESS_WHO_AM_I (0x75U)
- #define ADDRESS_SIGNAL_PATH_RESET (0x68U)
- static const uint8_t expected_who_am_i = 0x68U;
- static uint8_t m_device_address;
- bool mpu6050_init(uint8_t device_address)
- {
- bool transfer_succeeded = true;
- m_device_address = (uint8_t)(device_address << 1);
-
- uint8_t reset_value = 0x04U | 0x02U | 0x01U;
- transfer_succeeded &= mpu6050_register_write(ADDRESS_SIGNAL_PATH_RESET, reset_value);
-
- transfer_succeeded &= mpu6050_verify_product_id();
- return transfer_succeeded;
- }
- bool mpu6050_verify_product_id(void)
- {
- uint8_t who_am_i;
- if (mpu6050_register_read(ADDRESS_WHO_AM_I, &who_am_i, 1))
- {
- if (who_am_i != expected_who_am_i)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else
- {
- return false;
- }
- }
- bool mpu6050_register_write(uint8_t register_address, uint8_t value)
- {
- uint8_t w2_data[2];
- w2_data[0] = register_address;
- w2_data[1] = value;
- return twi_master_transfer(m_device_address, w2_data, 2, TWI_ISSUE_STOP);
- }
- bool mpu6050_register_read(uint8_t register_address, uint8_t * destination, uint8_t number_of_bytes)
- {
- bool transfer_succeeded;
- transfer_succeeded = twi_master_transfer(m_device_address, ®ister_address, 1, TWI_DONT_ISSUE_STOP);
- transfer_succeeded &= twi_master_transfer(m_device_address|TWI_READ_BIT, destination, number_of_bytes, TWI_ISSUE_STOP);
- return transfer_succeeded;
- }
|