diff --git a/mpu6050/Makefile b/mpu6050/Makefile old mode 100644 new mode 100755 index c2abd90..f6e5b7d --- a/mpu6050/Makefile +++ b/mpu6050/Makefile @@ -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 diff --git a/mpu6050/dt/compile.sh b/mpu6050/dt/compile.sh new file mode 100755 index 0000000..3c4f6ad --- /dev/null +++ b/mpu6050/dt/compile.sh @@ -0,0 +1 @@ +olegh@orangepione:/boot/overlay-user$ dtc -I dts -O dtb mpu6050.dtso > mpu6050.dtbo \ No newline at end of file diff --git a/mpu6050/dt/mpu6050.dtbo b/mpu6050/dt/mpu6050.dtbo new file mode 100755 index 0000000..eba7d22 Binary files /dev/null and b/mpu6050/dt/mpu6050.dtbo differ diff --git a/mpu6050/dt/mpu6050.dts b/mpu6050/dt/mpu6050.dts new file mode 100755 index 0000000..33c425e --- /dev/null +++ b/mpu6050/dt/mpu6050.dts @@ -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"; + }; + }; + }; +}; \ No newline at end of file diff --git a/mpu6050/mpu6050-mod.log b/mpu6050/mpu6050-mod.log new file mode 100755 index 0000000..09956db --- /dev/null +++ b/mpu6050/mpu6050-mod.log @@ -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$ diff --git a/mpu6050/mpu6050.c b/mpu6050/mpu6050.c old mode 100644 new mode 100755 index b6ef64b..893a2b7 --- a/mpu6050/mpu6050.c +++ b/mpu6050/mpu6050.c @@ -5,14 +5,22 @@ #include #include +#include + #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; @@ -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) { @@ -114,7 +129,8 @@ 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, @@ -122,76 +138,66 @@ static struct i2c_driver mpu6050_i2c_driver = { .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; @@ -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 */ @@ -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"); @@ -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); @@ -290,7 +275,7 @@ static void mpu6050_exit(void) module_init(mpu6050_init); module_exit(mpu6050_exit); -MODULE_AUTHOR("Andriy.Khulap "); +MODULE_AUTHOR("Oleg.Khokhlov "); MODULE_DESCRIPTION("mpu6050 I2C acc&gyro"); MODULE_LICENSE("GPL"); -MODULE_VERSION("0.1"); +MODULE_VERSION("0.3"); diff --git a/mpu6050/mpu6050.c0 b/mpu6050/mpu6050.c0 new file mode 100644 index 0000000..b6ef64b --- /dev/null +++ b/mpu6050/mpu6050.c0 @@ -0,0 +1,296 @@ +#include +#include +#include +#include +#include +#include + +#include "mpu6050-regs.h" + + +struct mpu6050_data { + struct i2c_client *drv_client; + int accel_values[3]; + int gyro_values[3]; + int temperature; +}; + +static struct mpu6050_data g_mpu6050_data; + +static int mpu6050_read_data(void) +{ + int temp; + struct i2c_client *drv_client = g_mpu6050_data.drv_client; + + if (drv_client == 0) + return -ENODEV; + + /* accel */ + g_mpu6050_data.accel_values[0] = (s16)((u16)i2c_smbus_read_word_swapped(drv_client, REG_ACCEL_XOUT_H)); + g_mpu6050_data.accel_values[1] = (s16)((u16)i2c_smbus_read_word_swapped(drv_client, REG_ACCEL_YOUT_H)); + g_mpu6050_data.accel_values[2] = (s16)((u16)i2c_smbus_read_word_swapped(drv_client, REG_ACCEL_ZOUT_H)); + /* gyro */ + g_mpu6050_data.gyro_values[0] = (s16)((u16)i2c_smbus_read_word_swapped(drv_client, REG_GYRO_XOUT_H)); + g_mpu6050_data.gyro_values[1] = (s16)((u16)i2c_smbus_read_word_swapped(drv_client, REG_GYRO_YOUT_H)); + g_mpu6050_data.gyro_values[2] = (s16)((u16)i2c_smbus_read_word_swapped(drv_client, REG_GYRO_ZOUT_H)); + /* Temperature in degrees C = + * (TEMP_OUT Register Value as a signed quantity)/340 + 36.53 + */ + temp = (s16)((u16)i2c_smbus_read_word_swapped(drv_client, REG_TEMP_OUT_H)); + g_mpu6050_data.temperature = (temp + 12420 + 170) / 340; + + dev_info(&drv_client->dev, "sensor data read:\n"); + dev_info(&drv_client->dev, "ACCEL[X,Y,Z] = [%d, %d, %d]\n", + g_mpu6050_data.accel_values[0], + g_mpu6050_data.accel_values[1], + g_mpu6050_data.accel_values[2]); + dev_info(&drv_client->dev, "GYRO[X,Y,Z] = [%d, %d, %d]\n", + g_mpu6050_data.gyro_values[0], + g_mpu6050_data.gyro_values[1], + g_mpu6050_data.gyro_values[2]); + dev_info(&drv_client->dev, "TEMP = %d\n", + g_mpu6050_data.temperature); + + return 0; +} + +static int mpu6050_probe(struct i2c_client *drv_client, + const struct i2c_device_id *id) +{ + int ret; + + dev_info(&drv_client->dev, + "i2c client address is 0x%X\n", drv_client->addr); + + /* Read who_am_i register */ + ret = i2c_smbus_read_byte_data(drv_client, REG_WHO_AM_I); + if (IS_ERR_VALUE(ret)) { + dev_err(&drv_client->dev, + "i2c_smbus_read_byte_data() failed with error: %d\n", + ret); + return ret; + } + if (ret != MPU6050_WHO_AM_I) { + dev_err(&drv_client->dev, + "wrong i2c device found: expected 0x%X, found 0x%X\n", + MPU6050_WHO_AM_I, ret); + return -1; + } + dev_info(&drv_client->dev, + "i2c mpu6050 device found, WHO_AM_I register value = 0x%X\n", + ret); + + /* Setup the device */ + /* No error handling here! */ + i2c_smbus_write_byte_data(drv_client, REG_CONFIG, 0); + i2c_smbus_write_byte_data(drv_client, REG_GYRO_CONFIG, 0); + i2c_smbus_write_byte_data(drv_client, REG_ACCEL_CONFIG, 0); + i2c_smbus_write_byte_data(drv_client, REG_FIFO_EN, 0); + i2c_smbus_write_byte_data(drv_client, REG_INT_PIN_CFG, 0); + i2c_smbus_write_byte_data(drv_client, REG_INT_ENABLE, 0); + i2c_smbus_write_byte_data(drv_client, REG_USER_CTRL, 0); + i2c_smbus_write_byte_data(drv_client, REG_PWR_MGMT_1, 0); + i2c_smbus_write_byte_data(drv_client, REG_PWR_MGMT_2, 0); + + g_mpu6050_data.drv_client = drv_client; + + dev_info(&drv_client->dev, "i2c driver probed\n"); + return 0; +} + +static int mpu6050_remove(struct i2c_client *drv_client) +{ + g_mpu6050_data.drv_client = 0; + + dev_info(&drv_client->dev, "i2c driver removed\n"); + return 0; +} + +static const struct i2c_device_id mpu6050_idtable[] = { + { "mpu6050", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, mpu6050_idtable); + +static struct i2c_driver mpu6050_i2c_driver = { + .driver = { + .name = "gl_mpu6050", + }, + + .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) +{ + mpu6050_read_data(); + + sprintf(buf, "%d\n", g_mpu6050_data.accel_values[1]); + return strlen(buf); +} + +static ssize_t accel_z_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); +} + +static ssize_t gyro_x_show(struct class *class, + struct class_attribute *attr, char *buf) +{ + mpu6050_read_data(); + + 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) +{ + mpu6050_read_data(); + + sprintf(buf, "%d\n", g_mpu6050_data.gyro_values[1]); + return strlen(buf); +} + +static ssize_t gyro_z_show(struct class *class, + struct class_attribute *attr, char *buf) +{ + mpu6050_read_data(); + + sprintf(buf, "%d\n", g_mpu6050_data.gyro_values[2]); + 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); +} + +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(temperature, 0444, &temp_show, NULL); + +static struct class *attr_class; + +static int mpu6050_init(void) +{ + int ret; + + /* Create i2c driver */ + ret = i2c_add_driver(&mpu6050_i2c_driver); + if (ret) { + pr_err("mpu6050: failed to add new i2c driver: %d\n", ret); + return ret; + } + pr_info("mpu6050: i2c driver created\n"); + + /* Create class */ + attr_class = class_create(THIS_MODULE, "mpu6050"); + if (IS_ERR(attr_class)) { + ret = PTR_ERR(attr_class); + pr_err("mpu6050: failed to create sysfs class: %d\n", ret); + return ret; + } + 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); + if (ret) { + pr_err("mpu6050: failed to create sysfs class attribute accel_y: %d\n", ret); + return ret; + } + /* Create accel_z */ + ret = class_create_file(attr_class, &class_attr_accel_z); + 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); + return ret; + } + /* Create temperature */ + ret = class_create_file(attr_class, &class_attr_temperature); + if (ret) { + pr_err("mpu6050: failed to create sysfs class attribute temperature: %d\n", ret); + return ret; + } + + pr_info("mpu6050: sysfs class attributes created\n"); + + pr_info("mpu6050: module loaded\n"); + return 0; +} + +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_temperature); + pr_info("mpu6050: sysfs class attributes removed\n"); + + class_destroy(attr_class); + pr_info("mpu6050: sysfs class destroyed\n"); + } + + i2c_del_driver(&mpu6050_i2c_driver); + pr_info("mpu6050: i2c driver deleted\n"); + + pr_info("mpu6050: module exited\n"); +} + +module_init(mpu6050_init); +module_exit(mpu6050_exit); + +MODULE_AUTHOR("Andriy.Khulap "); +MODULE_DESCRIPTION("mpu6050 I2C acc&gyro"); +MODULE_LICENSE("GPL"); +MODULE_VERSION("0.1"); diff --git a/mpu6050_scripts/cvt-mpu6050-t.c b/mpu6050_scripts/cvt-mpu6050-t.c new file mode 100755 index 0000000..ad3852e --- /dev/null +++ b/mpu6050_scripts/cvt-mpu6050-t.c @@ -0,0 +1,15 @@ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + if (argc != 3) return 1; + unsigned H = strtol(argv[1], NULL, 0); + unsigned L = strtol(argv[2], NULL, 0); + int W = (signed short)((H << 8) | (L & 0xFF)); + float T = W * (1.0/340.0) + 36.53; + printf("%0.2f\n", T); + return 0; +} + diff --git a/mpu6050_scripts/makefile b/mpu6050_scripts/makefile new file mode 100755 index 0000000..210c1e0 --- /dev/null +++ b/mpu6050_scripts/makefile @@ -0,0 +1,75 @@ +# +# 'make depend' uses makedepend to automatically generate dependencies +# (dependencies are added to end of Makefile) +# 'make' build executable file 'mycc' +# 'make clean' removes all .o and executable files +# + +# define the C compiler to use +CC = gcc + +# define any compile-time flags +CFLAGS = -Wall -g + +# define any directories containing header files other than /usr/include +# +INCLUDES = -I../include + +# define library paths in addition to /usr/lib +# if I wanted to include libraries not in /usr/lib I'd specify +# their path using -Lpath, something like: +LFLAGS = -L../lib + +# define any libraries to link into executable: +# if I want to link in libraries (libx.so or libx.a) I use the -llibname +# option, something like (this will link in libmylib.so and libm.so: +#LIBS = -lmylib -lm + +# define the C source files +SRCS = cvt-mpu6050-t.c + +# define the C object files +# +# This uses Suffix Replacement within a macro: +# $(name:string1=string2) +# For each word in 'name' replace 'string1' with 'string2' +# Below we are replacing the suffix .c of all words in the macro SRCS +# with the .o suffix +# +OBJS = $(SRCS:.c=.o) + +# define the executable file +MAIN = cvt-mpu6050-t + +# +# The following part of the makefile is generic; it can be used to +# build any executable just by changing the definitions above and by +# deleting dependencies appended to the file from 'make depend' +# + +.PHONY: depend clean + +all: $(MAIN) + @echo "--- That's all, folks! ---" + +$(MAIN): $(OBJS) + $(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS) + +# this is a suffix replacement rule for building .o's from .c's +# it uses automatic variables $<: the name of the prerequisite of +# the rule(a .c file) and $@: the name of the target of the rule (a .o file) +# (see the gnu make manual section about automatic variables) +.c.o: + $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ + +clean: + $(RM) *.o *~ $(MAIN) + +depend: $(SRCS) + makedepend $(INCLUDES) $^ + +# DO NOT DELETE THIS LINE -- make depend needs it + +rps.o: /usr/include/stdio.h /usr/include/stdlib.h /usr/include/ctype.h +rps.o: /usr/include/features.h /usr/include/stdc-predef.h +rps.o: /usr/include/endian.h /usr/include/time.h diff --git a/mpu6050_scripts/mpu6050.sh b/mpu6050_scripts/mpu6050.sh new file mode 100755 index 0000000..3ac2154 --- /dev/null +++ b/mpu6050_scripts/mpu6050.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +#check WHO_AM_I register +WAMI=$(sudo i2cget -y 0 0x68 0x75) +if [ "$WAMI" != "0x68" ] +then + echo "MPU6050 not found ($WAMI)" + exit 1 +fi + +#start conversion: set PWR_MGMT_1 to 0 +PM1=$(sudo i2cget -y 0 0x68 0x6B) +if [ "$PM1" != "0x00" ] +then + sudo i2cset -y 0 0x68 0x6B 0 + echo "MPU6050: start conversion..." + sleep 1 +fi + +while true +do + echo -n "MPU6050 T: " + TH=$(sudo i2cget -y 0 0x68 0x41) + echo -n "$TH," + + TL=$(sudo i2cget -y 0 0x68 0x42) + echo -n "$TL : " + + T=$(./cvt-mpu6050-t $TH $TL) + echo "$T degC" + + sleep 1 +done