1.测试了QMI8658的驱动,正常读出x,y,z轴数据,得到的pitch, roll, yaw正常
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
#include "I2C_Driver.h"
|
||||
|
||||
|
||||
#define I2C_TRANS_BUF_MINIMUM_SIZE (sizeof(i2c_cmd_desc_t) + \
|
||||
sizeof(i2c_cmd_link_t) * 8) /* It is required to have allocate one i2c_cmd_desc_t per command:
|
||||
* start + write (device address) + write buffer +
|
||||
* start + write (device address) + read buffer + read buffer for NACK +
|
||||
* stop */
|
||||
static const char *I2C_TAG = "I2C";
|
||||
/**
|
||||
* @brief i2c master initialization
|
||||
*/
|
||||
static esp_err_t i2c_master_init(void)
|
||||
{
|
||||
int i2c_master_port = I2C_MASTER_NUM;
|
||||
|
||||
i2c_config_t conf = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = I2C_SDA_IO,
|
||||
.scl_io_num = I2C_SCL_IO,
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.master.clk_speed = I2C_MASTER_FREQ_HZ,
|
||||
};
|
||||
|
||||
i2c_param_config(i2c_master_port, &conf);
|
||||
|
||||
return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
|
||||
}
|
||||
void I2C_Init(void)
|
||||
{
|
||||
/********************* I2C *********************/
|
||||
ESP_ERROR_CHECK(i2c_master_init());
|
||||
ESP_LOGI(I2C_TAG, "I2C initialized successfully");
|
||||
}
|
||||
|
||||
|
||||
// Reg addr is 8 bit
|
||||
esp_err_t I2C_Write(uint8_t Driver_addr, uint8_t Reg_addr, const uint8_t *Reg_data, uint32_t Length)
|
||||
{
|
||||
uint8_t buf[Length+1];
|
||||
|
||||
buf[0] = Reg_addr;
|
||||
// Copy Reg_data to buf starting at buf[1]
|
||||
memcpy(&buf[1], Reg_data, Length);
|
||||
return i2c_master_write_to_device(I2C_MASTER_NUM, Driver_addr, buf, Length+1, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
|
||||
|
||||
esp_err_t I2C_Read(uint8_t Driver_addr, uint8_t Reg_addr, uint8_t *Reg_data, uint32_t Length)
|
||||
{
|
||||
return i2c_master_write_read_device(I2C_MASTER_NUM, Driver_addr, &Reg_addr, 1, Reg_data, Length, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h> // For memcpy
|
||||
#include "esp_log.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/i2c.h"
|
||||
|
||||
|
||||
/********************* I2C *********************/
|
||||
#define I2C_SCL_IO 10 /*!< GPIO number used for I2C master clock */
|
||||
#define I2C_SDA_IO 11 /*!< GPIO number used for I2C master data */
|
||||
#define I2C_MASTER_NUM 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */
|
||||
#define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */
|
||||
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
|
||||
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
|
||||
#define I2C_MASTER_TIMEOUT_MS 1000
|
||||
|
||||
|
||||
void I2C_Init(void);
|
||||
// Reg addr is 8 bit
|
||||
esp_err_t I2C_Write(uint8_t Driver_addr, uint8_t Reg_addr, const uint8_t *Reg_data, uint32_t Length);
|
||||
esp_err_t I2C_Read(uint8_t Driver_addr, uint8_t Reg_addr, uint8_t *Reg_data, uint32_t Length);
|
||||
Reference in New Issue
Block a user