Skip to content

Commit

Permalink
major refactor and implement hal
Browse files Browse the repository at this point in the history
  • Loading branch information
fred13kim committed Dec 22, 2024
1 parent 4f73812 commit 53b4a42
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 115 deletions.
15 changes: 12 additions & 3 deletions components/fk/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@

int app_main(void)
{
mpu6050_init();
mpu6050_handle_t mpu6050_handle;
mpu6050_config_t mpu6050_config = {
.fs = FS_SEL_500,
.afs = AFS_SEL_4G,
.scl = (gpio_num_t) (40),
.sda = (gpio_num_t) (37),
};

i2c_mpu6050_raw_gyro_t raw_gyro;
mpu6050_init(&mpu6050_handle, &mpu6050_config);

mpu6050_raw_gyro_t raw_gyro;

float x, y, z;
for (;;) {
mpu6050_get_raw_gyro(dev_handle, &raw_gyro);
mpu6050_get_raw_gyro(&mpu6050_handle, &raw_gyro);
x = raw_gyro.gyro_raw_xout / 65.5;
y = raw_gyro.gyro_raw_yout / 65.5;
z = raw_gyro.gyro_raw_zout / 65.5;
printf("%f %f %f\n", x, y, z);
vTaskDelay(pdMS_TO_TICKS(10));
}

return EXIT_SUCCESS;
}
2 changes: 1 addition & 1 deletion lib/drivers/mpu6050/SConscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

Import('env')

source = [env.File('mpu6050.c')]
source = [env.File('mpu6050.c'), env.File('i2c_hal_esp32.c')]

Return('source')
24 changes: 24 additions & 0 deletions lib/drivers/mpu6050/i2c_hal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef I2C_HAL_H
#define I2C_HAL_H

#include <stdint.h>

typedef struct i2c_hal_handle_s i2c_hal_handle_t;
typedef struct i2c_hal_config_s i2c_hal_config_t;

typedef struct {
void (*i2c_hal_init)(i2c_hal_handle_t *i2c_hal_handle,
i2c_hal_config_t *i2c_hal_config);
void (*i2c_hal_read)(i2c_hal_handle_t *i2c_hal_handle,
const uint8_t addr,
uint8_t *data,
uint32_t num_bytes);
void (*i2c_hal_write)(i2c_hal_handle_t *i2c_hal_handle,
const uint8_t addr,
uint8_t *data,
uint32_t num_bytes);
} i2c_hal_t;

extern i2c_hal_t i2c_hal;

#endif // I2C_HAL_H
70 changes: 70 additions & 0 deletions lib/drivers/mpu6050/i2c_hal_esp32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "i2c_hal_esp32.h"

#include "i2c_hal.h"

#include <string.h>

#define I2C_MASTER_PORT I2C_NUM_0

#define I2C_TIMEOUT_MS 500

i2c_hal_t i2c_hal = {
.i2c_hal_init = i2c_hal_init,
.i2c_hal_read = i2c_hal_read,
.i2c_hal_write = i2c_hal_write,
};

void i2c_hal_init(
i2c_hal_handle_t *i2c_hal_handle, i2c_hal_config_t *i2c_hal_config)
{
i2c_master_bus_config_t i2c_master_conf = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.i2c_port = I2C_MASTER_PORT,
.scl_io_num = i2c_hal_config->scl,
.sda_io_num = i2c_hal_config->sda,
.glitch_ignore_cnt = 7,
};

i2c_master_bus_handle_t bus_handle;

ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_master_conf, &bus_handle));

i2c_device_config_t i2c_dev_conf = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,

// should probably make a check that device_address &
// scl_speed_hz is configured properly before we continue
.device_address = i2c_hal_config->device_address,
.scl_speed_hz = i2c_hal_config->scl_speed_hz,
};

ESP_ERROR_CHECK(i2c_master_bus_add_device(
bus_handle, &i2c_dev_conf, &(i2c_hal_handle->dev_handle)));
}

void i2c_hal_read(i2c_hal_handle_t *i2c_hal_handle,
const uint8_t addr,
uint8_t *data,
uint32_t num_bytes)
{
i2c_master_transmit_receive(i2c_hal_handle->dev_handle,
&addr,
1,
data,
num_bytes,
I2C_TIMEOUT_MS);
}

void i2c_hal_write(i2c_hal_handle_t *i2c_hal_handle,
const uint8_t addr,
uint8_t *data,
uint32_t num_bytes)
{
uint8_t tx[num_bytes + 1];
tx[0] = addr;

memcpy(tx + 1, data, num_bytes);

i2c_master_transmit(
i2c_hal_handle->dev_handle, tx, sizeof(tx), I2C_TIMEOUT_MS);
}
30 changes: 30 additions & 0 deletions lib/drivers/mpu6050/i2c_hal_esp32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef I2C_HAL_ESP32
#define I2C_HAL_ESP32

#include "i2c_hal.h"
#include <driver/i2c_master.h>
#include <freertos/FreeRTOS.h>

struct i2c_hal_handle_s {
i2c_master_dev_handle_t dev_handle;
};

struct i2c_hal_config_s {
uint8_t device_address;
uint32_t scl_speed_hz;
uint8_t scl;
uint8_t sda;
};

void i2c_hal_init(
i2c_hal_handle_t *i2c_hal_handle, i2c_hal_config_t *i2c_hal_config);
void i2c_hal_read(i2c_hal_handle_t *i2c_hal_handle,
const uint8_t addr,
uint8_t *data,
uint32_t num_bytes);
void i2c_hal_write(i2c_hal_handle_t *i2c_hal_handle,
const uint8_t addr,
uint8_t *data,
uint32_t num_bytes);

#endif // I2C_HAL_ESP32
185 changes: 79 additions & 106 deletions lib/drivers/mpu6050/mpu6050.c
Original file line number Diff line number Diff line change
@@ -1,92 +1,69 @@
#include "mpu6050.h"

#include "i2c_hal_esp32.h"
#include <esp_log.h>

#include <string.h>
#define MPU6050_DEV_ADDR 0x68 // AD0
#define MPU6050_DEV_FREQ_HZ 400000

#define I2C_MASTER_PORT I2C_NUM_0
#define I2C_MASTER_SCL_IO (gpio_num_t)(40)
#define I2C_MASTER_SDA_IO (gpio_num_t)(37)
#define MPU6050_WHO_AM_I 0x75
#define MPU6050_DEV_ID 0x68
#define MPU6050_GYRO_CONFIG 0x1B
#define MPU6050_PWR_MGTM_1 0x6B
#define MPU6050_ACCEL_XOUT 0x3B
#define MPU6050_GYRO_XOUT 0x43


#define I2C_MPU6050_DEV_ADDR 0x68 // AD0
#define I2C_MPU6050_DEV_FREQ_HZ 400000

#define I2C_MPU6050_WHO_AM_I 0x75
#define I2C_MPU6050_GYRO_CONFIG 0x1B
#define I2C_MPU6050_PWR_MGTM_1 0x6B
#define I2C_MPU6050_ACCEL_XOUT 0x3B
#define I2C_MPU6050_GYRO_XOUT 0x43

#define I2C_MPU6050_DEV_ID 0x68

#define I2C_TIMEOUT_MS 500

typedef enum {
FS_SEL_250,
FS_SEL_500,
FS_SEL_1000,
FS_SEL_2000
} i2c_mpu6050_fs_sel_t;

typedef enum {
AFS_SEL_2G,
AFS_SEL_4G,
AFS_SEL_8G,
AFS_SEL_16G
} i2c_mpu6050_afs_sel_t;

/* Static Prototypes */
static const char *TAG = "MPU6050.c";

static void i2c_mpu6050_read(i2c_master_dev_handle_t i2c_dev,
uint8_t addr,
uint8_t *data,
size_t num_bytes)
static void mpu6050_configure(
mpu6050_handle_t *mpu6050_handle, mpu6050_config_t *mpu6050_config);
static void mpu6050_power_up(mpu6050_handle_t *mpu6050_handle);

/*
* Performs startup initialization:
* Checks device id
* Configures sensitivity
* Powers up
*/
void mpu6050_init(
mpu6050_handle_t *mpu6050_handle, mpu6050_config_t *mpu6050_config)
{
i2c_master_transmit_receive(
i2c_dev, &addr, 1, data, num_bytes, I2C_TIMEOUT_MS);
}

static void i2c_mpu6050_write(i2c_master_dev_handle_t i2c_dev,
uint8_t addr,
uint8_t *data,
size_t num_bytes)
{
uint8_t tx[num_bytes + 1];
tx[0] = addr;

memcpy(tx + 1, data, num_bytes);

i2c_master_transmit(i2c_dev, tx, sizeof(tx), I2C_TIMEOUT_MS);
}
mpu6050_config->i2c_hal_config.device_address = MPU6050_DEV_ADDR;
mpu6050_config->i2c_hal_config.scl_speed_hz = MPU6050_DEV_FREQ_HZ;
mpu6050_config->i2c_hal_config.scl = mpu6050_config->scl;
mpu6050_config->i2c_hal_config.sda = mpu6050_config->sda;

void mpu6050_config(i2c_master_dev_handle_t i2c_dev,
i2c_mpu6050_fs_sel_t fs_sel,
i2c_mpu6050_afs_sel_t afs_sel)
{
uint8_t data[2] = {fs_sel << 3, afs_sel << 3};
i2c_hal_init(&(mpu6050_handle->i2c_hal_handle),
&(mpu6050_config->i2c_hal_config));

i2c_mpu6050_write(
i2c_dev, I2C_MPU6050_GYRO_CONFIG, data, sizeof(data));
}
uint8_t dev_id;
i2c_hal_read(&(mpu6050_handle->i2c_hal_handle),
MPU6050_WHO_AM_I,
&dev_id,
1);

void mpu6050_power_up(i2c_master_dev_handle_t i2c_dev)
{
uint8_t data;
i2c_mpu6050_read(i2c_dev, I2C_MPU6050_PWR_MGTM_1, &data, 1);
if (dev_id != (uint8_t) MPU6050_DEV_ID) {
ESP_LOGE(TAG, "Failure @ dev id: %x", dev_id);
return;
}
ESP_LOGI(TAG, "MPU6050 RECOGNIZED!");

// Turn off sleep mode
data &= (~0x40);
// configure sensitivity
mpu6050_configure(mpu6050_handle, mpu6050_config);
ESP_LOGI(TAG, "GRYO & ACCEL CONFIG FINISHED!");

i2c_mpu6050_write(i2c_dev, I2C_MPU6050_PWR_MGTM_1, &data, 1);
mpu6050_power_up(mpu6050_handle);
ESP_LOGI(TAG, "AWAKE!");
}

void mpu6050_get_raw_gyro(
i2c_master_dev_handle_t i2c_dev, i2c_mpu6050_raw_gyro_t *gyro_raw_val)
mpu6050_handle_t *mpu6050_handle, mpu6050_raw_gyro_t *gyro_raw_val)
{
uint8_t data[6];
i2c_mpu6050_read(i2c_dev,
I2C_MPU6050_GYRO_XOUT,
i2c_hal_read(&(mpu6050_handle->i2c_hal_handle),
MPU6050_GYRO_XOUT,
(uint8_t *) &data,
sizeof(data));

Expand All @@ -95,48 +72,44 @@ void mpu6050_get_raw_gyro(
gyro_raw_val->gyro_raw_zout = (int16_t) (data[4] << 8 | data[5]);
}

void mpu6050_init(i2c_master_dev_handle_t dev_handle)
void mpu6050_get_raw_accel(
mpu6050_handle_t *mpu6050_handle, mpu6050_raw_accel_t *accel_raw_val)
{
i2c_master_bus_config_t i2c_master_conf = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.i2c_port = I2C_NUM_0,
.scl_io_num = I2C_MASTER_SCL_IO,
.sda_io_num = I2C_MASTER_SDA_IO,
.glitch_ignore_cnt = 7,
};

i2c_master_bus_handle_t bus_handle;

ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_master_conf, &bus_handle));

i2c_device_config_t i2c_dev_conf = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = I2C_MPU6050_DEV_ADDR,
.scl_speed_hz = I2C_MPU6050_DEV_FREQ_HZ,
};


ESP_ERROR_CHECK(i2c_master_bus_add_device(
bus_handle, &i2c_dev_conf, &dev_handle));
uint8_t data[6];
i2c_hal_read(&(mpu6050_handle->i2c_hal_handle),
MPU6050_ACCEL_XOUT,
(uint8_t *) &data,
sizeof(data));

// Check Dev ID
uint8_t dev_id;
i2c_mpu6050_read(dev_handle, I2C_MPU6050_WHO_AM_I, &dev_id, 1);
accel_raw_val->accel_raw_xout = (int16_t) (data[0] << 8 | data[1]);
accel_raw_val->accel_raw_yout = (int16_t) (data[2] << 8 | data[3]);
accel_raw_val->accel_raw_zout = (int16_t) (data[4] << 8 | data[5]);
}

if (dev_id != (uint8_t) I2C_MPU6050_DEV_ID) {
ESP_LOGE(TAG, "Failure @ dev id: %x", dev_id);
return;
}
ESP_LOGI(TAG, "MPU6050 RECOGNIZED!");
static void mpu6050_configure(
mpu6050_handle_t *mpu6050_handle, mpu6050_config_t *mpu6050_config)
{
uint8_t data[2] = {mpu6050_config->fs << 3, mpu6050_config->afs << 3};

// Config GYRO & ACCEL
mpu6050_config(dev_handle, FS_SEL_500, AFS_SEL_4G);
uint8_t tmp[2];
i2c_mpu6050_read(dev_handle, I2C_MPU6050_GYRO_CONFIG, tmp, 2);
i2c_hal_write(&(mpu6050_handle->i2c_hal_handle),
MPU6050_GYRO_CONFIG,
data,
sizeof(data));
}

ESP_LOGI(TAG, "GRYO & ACCEL CONFIG FINISHED!");
static void mpu6050_power_up(mpu6050_handle_t *mpu6050_handle)
{
uint8_t data;
i2c_hal_read(&(mpu6050_handle->i2c_hal_handle),
MPU6050_PWR_MGTM_1,
&data,
1);

mpu6050_power_up(dev_handle);
// Turn off sleep mode
data &= (~0x40);

ESP_LOGI(TAG, "AWAKE!");
i2c_hal_write(&(mpu6050_handle->i2c_hal_handle),
MPU6050_PWR_MGTM_1,
&data,
1);
}
Loading

0 comments on commit 53b4a42

Please sign in to comment.