Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions mpu6050/Makefile
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,36 @@ obj-m := mpu6050.o

else

export KERNELDIR = /usr/src/linux-headers-4.14.84-sunxi

ifeq ($(KERNELDIR),)
ifeq ($(BBB_KERNEL),)
$(error Path to kernel tree - KERNELDIR or BBB_KERNEL variable is not defined!)
endif
endif

KERNELDIR ?= $(BBB_KERNEL)
#KERNELDIR ?= $(BBB_KERNEL)

export ARCH = arm
export CROSS_COMPILE ?= arm-linux-gnueabihf-


.PHONY: all clean

all:
ifeq ($(KERNELDIR),)
ifeq ($(BBB_KERNEL),)
$(error Path to kernel tree - KERNELDIR or BBB_KERNEL variable is not defined!)
endif
endif
$(MAKE) -C $(KERNELDIR) M=$(CURDIR) modules

clean:
ifeq ($(KERNELDIR),)
ifeq ($(BBB_KERNEL),)
$(error Path to kernel tree - KERNELDIR or BBB_KERNEL variable is not defined!)
endif
endif
$(MAKE) -C $(KERNELDIR) M=$(CURDIR) clean


install:
#sudo cp mpu6050.ko /lib/modules/4.14.84-sunxi/kernel/
#sudo cp mpu6050.ko /lib/modules/4.14.84-sunxi/kernel/drivers/
sudo cp mpu6050.ko /lib/modules/4.14.84-sunxi/kernel/drivers/i2c/
sudo depmod -a

endif
1 change: 1 addition & 0 deletions mpu6050/dt/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
olegh@orangepione:/boot/overlay-user$ dtc -I dts -O dtb mpu6050.dtso > mpu6050.dtbo

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work as expected?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean did overlays work - yes, sure, it works.
If you mean that there is no dtso file in the pull request - yes, it's an missmatch and my little error. After some experiments I had changed file name to .dts, but this example had been left unchanged - i was made before. It was long time ago, more than a month.

Binary file added mpu6050/dt/mpu6050.dtbo
Binary file not shown.
20 changes: 20 additions & 0 deletions mpu6050/dt/mpu6050.dts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/dts-v1/;
/plugin/;

/ {
compatible = "allwinner,sun8i-h3";

fragment@0 {
target = <&i2c0>;
__overlay__ {
#address-cells = <1>;
#size-cells = <0>;
status = "okay";
mpu6050@68 {
compatible = "mpu6050";
reg = <0x68>;
status = "okay";
};
};
};
};
11 changes: 11 additions & 0 deletions mpu6050/mpu6050-mod.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
olegh@orangepione:/sys/class/mpu6050$ cat accel
10476 2944 12188
olegh@orangepione:/sys/class/mpu6050$ cat gyro
-198 138 -126
olegh@orangepione:/sys/class/mpu6050$ cat temperature
30
olegh@orangepione:/sys/class/mpu6050$ cat all
10356 2812 12276
-248 191 -105
30
olegh@orangepione:/sys/class/mpu6050$
157 changes: 71 additions & 86 deletions mpu6050/mpu6050.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@
#include <linux/i2c.h>
#include <linux/i2c-dev.h>

#include <linux/jiffies.h>

#include "mpu6050-regs.h"

uint32_t GetTickCount(void)
{
return jiffies_to_msecs(get_jiffies_64());
}


struct mpu6050_data {
struct i2c_client *drv_client;
int accel_values[3];
int gyro_values[3];
int temperature;
uint32_t LastReadTick;
};

static struct mpu6050_data g_mpu6050_data;
Expand Down Expand Up @@ -51,9 +59,16 @@ static int mpu6050_read_data(void)
dev_info(&drv_client->dev, "TEMP = %d\n",
g_mpu6050_data.temperature);

g_mpu6050_data.LastReadTick = GetTickCount();
return 0;
}

static int mpu6050_read_data2(void)
{
if ((GetTickCount() - g_mpu6050_data.LastReadTick) < 3) return 0;
return mpu6050_read_data();
}

static int mpu6050_probe(struct i2c_client *drv_client,
const struct i2c_device_id *id)
{
Expand Down Expand Up @@ -114,84 +129,75 @@ MODULE_DEVICE_TABLE(i2c, mpu6050_idtable);

static struct i2c_driver mpu6050_i2c_driver = {
.driver = {
.name = "gl_mpu6050",
.name = "mpu6050",
.owner = THIS_MODULE,
},

.probe = mpu6050_probe,
.remove = mpu6050_remove,
.id_table = mpu6050_idtable,
};

static ssize_t accel_x_show(struct class *class,
struct class_attribute *attr, char *buf)
{
mpu6050_read_data();

sprintf(buf, "%d\n", g_mpu6050_data.accel_values[0]);
return strlen(buf);
}

static ssize_t accel_y_show(struct class *class,
struct class_attribute *attr, char *buf)
static ssize_t accel_show(struct class *class, struct class_attribute *attr, char *buf)
{
mpu6050_read_data();
mpu6050_read_data2();

sprintf(buf, "%d\n", g_mpu6050_data.accel_values[1]);
sprintf(buf, "%d %d %d\n",
g_mpu6050_data.accel_values[0],
g_mpu6050_data.accel_values[1],
g_mpu6050_data.accel_values[2]
);
return strlen(buf);
}

static ssize_t accel_z_show(struct class *class,
struct class_attribute *attr, char *buf)
static ssize_t gyro_show(struct class *class, struct class_attribute *attr, char *buf)
{
mpu6050_read_data();

sprintf(buf, "%d\n", g_mpu6050_data.accel_values[2]);
return strlen(buf);
}
mpu6050_read_data2();

static ssize_t gyro_x_show(struct class *class,
struct class_attribute *attr, char *buf)
{
mpu6050_read_data();
sprintf(buf, "%d %d %d\n",
g_mpu6050_data.gyro_values[0],
g_mpu6050_data.gyro_values[1],
g_mpu6050_data.gyro_values[2]
);

sprintf(buf, "%d\n", g_mpu6050_data.gyro_values[0]);
return strlen(buf);
}

static ssize_t gyro_y_show(struct class *class,
struct class_attribute *attr, char *buf)
static ssize_t temp_show(struct class *class, struct class_attribute *attr, char *buf)
{
mpu6050_read_data();
mpu6050_read_data2();

sprintf(buf, "%d\n", g_mpu6050_data.gyro_values[1]);
sprintf(buf, "%d\n", g_mpu6050_data.temperature);
return strlen(buf);
}

static ssize_t gyro_z_show(struct class *class,
struct class_attribute *attr, char *buf)
static ssize_t all_show(struct class *class, struct class_attribute *attr, char *buf)
{
mpu6050_read_data();
mpu6050_read_data2();

sprintf(buf, "%d\n", g_mpu6050_data.gyro_values[2]);
sprintf(buf, "%d %d %d\n%d %d %d\n%d\n",
g_mpu6050_data.accel_values[0],
g_mpu6050_data.accel_values[1],
g_mpu6050_data.accel_values[2],
g_mpu6050_data.gyro_values[0],
g_mpu6050_data.gyro_values[1],
g_mpu6050_data.gyro_values[2],
g_mpu6050_data.temperature
);
return strlen(buf);
}

static ssize_t temp_show(struct class *class,
struct class_attribute *attr, char *buf)
{
mpu6050_read_data();

sprintf(buf, "%d\n", g_mpu6050_data.temperature);
return strlen(buf);
}
#define CLASS_ATTR(myname, mymode, showfunc, mynull) \
static struct class_attribute class_attr_##myname = { \
.attr = { .name = #myname , .mode = mymode }, \
.show = showfunc, \
};

CLASS_ATTR(accel_x, 0444, &accel_x_show, NULL);
CLASS_ATTR(accel_y, 0444, &accel_y_show, NULL);
CLASS_ATTR(accel_z, 0444, &accel_z_show, NULL);
CLASS_ATTR(gyro_x, 0444, &gyro_x_show, NULL);
CLASS_ATTR(gyro_y, 0444, &gyro_y_show, NULL);
CLASS_ATTR(gyro_z, 0444, &gyro_z_show, NULL);
CLASS_ATTR(accel, 0444, &accel_show, NULL);
CLASS_ATTR(gyro, 0444, &gyro_show, NULL);
CLASS_ATTR(temperature, 0444, &temp_show, NULL);
CLASS_ATTR(all, 0444, &all_show, NULL);


static struct class *attr_class;

Expand All @@ -216,40 +222,16 @@ static int mpu6050_init(void)
}
pr_info("mpu6050: sysfs class created\n");

/* Create accel_x */
ret = class_create_file(attr_class, &class_attr_accel_x);
if (ret) {
pr_err("mpu6050: failed to create sysfs class attribute accel_x: %d\n", ret);
return ret;
}
/* Create accel_y */
ret = class_create_file(attr_class, &class_attr_accel_y);
/* Create accel */
ret = class_create_file(attr_class, &class_attr_accel);
if (ret) {
pr_err("mpu6050: failed to create sysfs class attribute accel_y: %d\n", ret);
pr_err("mpu6050: failed to create sysfs class attribute accel: %d\n", ret);
return ret;
}
/* Create accel_z */
ret = class_create_file(attr_class, &class_attr_accel_z);
/* Create gyro */
ret = class_create_file(attr_class, &class_attr_gyro);
if (ret) {
pr_err("mpu6050: failed to create sysfs class attribute accel_z: %d\n", ret);
return ret;
}
/* Create gyro_x */
ret = class_create_file(attr_class, &class_attr_gyro_x);
if (ret) {
pr_err("mpu6050: failed to create sysfs class attribute gyro_x: %d\n", ret);
return ret;
}
/* Create gyro_y */
ret = class_create_file(attr_class, &class_attr_gyro_y);
if (ret) {
pr_err("mpu6050: failed to create sysfs class attribute gyro_y: %d\n", ret);
return ret;
}
/* Create gyro_z */
ret = class_create_file(attr_class, &class_attr_gyro_z);
if (ret) {
pr_err("mpu6050: failed to create sysfs class attribute gyro_z: %d\n", ret);
pr_err("mpu6050: failed to create sysfs class attribute gyro: %d\n", ret);
return ret;
}
/* Create temperature */
Expand All @@ -258,6 +240,12 @@ static int mpu6050_init(void)
pr_err("mpu6050: failed to create sysfs class attribute temperature: %d\n", ret);
return ret;
}
/* Create all */
ret = class_create_file(attr_class, &class_attr_all);
if (ret) {
pr_err("mpu6050: failed to create sysfs class attribute all: %d\n", ret);
return ret;
}

pr_info("mpu6050: sysfs class attributes created\n");

Expand All @@ -268,13 +256,10 @@ static int mpu6050_init(void)
static void mpu6050_exit(void)
{
if (attr_class) {
class_remove_file(attr_class, &class_attr_accel_x);
class_remove_file(attr_class, &class_attr_accel_y);
class_remove_file(attr_class, &class_attr_accel_z);
class_remove_file(attr_class, &class_attr_gyro_x);
class_remove_file(attr_class, &class_attr_gyro_y);
class_remove_file(attr_class, &class_attr_gyro_z);
class_remove_file(attr_class, &class_attr_accel);
class_remove_file(attr_class, &class_attr_gyro);
class_remove_file(attr_class, &class_attr_temperature);
class_remove_file(attr_class, &class_attr_all);
pr_info("mpu6050: sysfs class attributes removed\n");

class_destroy(attr_class);
Expand All @@ -290,7 +275,7 @@ static void mpu6050_exit(void)
module_init(mpu6050_init);
module_exit(mpu6050_exit);

MODULE_AUTHOR("Andriy.Khulap <andriy.khulap@globallogic.com>");
MODULE_AUTHOR("Oleg.Khokhlov <oleg.khokhlov.ua@gmail.com>");
MODULE_DESCRIPTION("mpu6050 I2C acc&gyro");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1");
MODULE_VERSION("0.3");
Loading