diff --git a/project/modules/Makefile b/project/modules/Makefile new file mode 100755 index 0000000..3eefd1d --- /dev/null +++ b/project/modules/Makefile @@ -0,0 +1,29 @@ +# Makefile +CC=gcc +CFLAGS = -W -Wall -pedantic -g -std=gnu99 -c +LDFLAGS = -static +RM = rm -f + +TARGET = motor +OBJECTS = gpio.o +HEADERS = $(OBJECTS:.o=.h) +SOURCES = $(TARGET).c $(OBJECTS:.o=.c) + +%.o: %.c + $(CC) $(CFLAGS) $< -o $@ + +.PHONY: all clean + +all: $(TARGET) + +clean: + $(RM) $(TARGET) $(TARGET).o $(OBJECTS) depend + +depend: $(SOURCES) $(HEADERS) + $(CC) $(CPPFLAGS) -MM $(SOURCES) > $@ + +$(TARGET): $(TARGET).o $(OBJECTS) + $(CC) $(LDFLAGS) $^ -o $@ + $(RM) depend $(TARGET).o $(OBJECTS) + +include depend diff --git a/project/modules/_common/infrared_sensor.def.h b/project/modules/_common/infrared_sensor.def.h new file mode 100644 index 0000000..dfe45f0 --- /dev/null +++ b/project/modules/_common/infrared_sensor.def.h @@ -0,0 +1,4 @@ +static dev_t devno; +static struct cdev chardev; +static struct class *chardev_class; +static struct device *chardev_device[NUMBER_OF_MINOR_DEVICE]; diff --git a/project/modules/_common/infrared_sensor.h b/project/modules/_common/infrared_sensor.h new file mode 100644 index 0000000..181b976 --- /dev/null +++ b/project/modules/_common/infrared_sensor.h @@ -0,0 +1,82 @@ +MODULE_AUTHOR("Manuel Vögele, Siegfried Kienzle, Simon Wörner"); +MODULE_DESCRIPTION(DEVICE_NAME "driver"); +MODULE_LICENSE("GPL"); + +static int __init ModInit(void) +{ + + unsigned int i = 0; + + printk(KERN_DEBUG DEVICE_NAME ": init"); + + if (alloc_chrdev_region(&devno, 0, NUMBER_OF_MINOR_DEVICE, DEVICE_NAME) < 0) + return -EIO; + + cdev_init(&chardev, &fops); + chardev.owner = THIS_MODULE; + chardev.ops = &fops; + + if (cdev_add(&chardev, devno, NUMBER_OF_MINOR_DEVICE)) { + pr_err(DEVICE_NAME ": cdev_add failed."); + goto free_device_number; + } + + chardev_class = class_create(THIS_MODULE, DEVICE_NAME); + + if (IS_ERR(chardev_class)) { + pr_err(DEVICE_NAME ": no udev support\n"); + goto free_cdev; + } + + for (i = 0; i < NUMBER_OF_MINOR_DEVICE; ++i) + { +#if NUMBER_OF_MINOR_DEVICE == 1 + chardev_device[i] = device_create(chardev_class, NULL, devno + i, NULL, DEVICE_NAME); +#else + chardev_device[i] = device_create(chardev_class, NULL, devno + i, NULL, DEVICE_NAME "%d", i); +#endif + + if (IS_ERR(chardev_device[i])) { + pr_err(DEVICE_NAME ": device create failed\n"); + goto free_device; + } + } + +#ifdef CUSTOM_INIT + CUSTOM_INIT +#endif + + return 0; + +free_device: + for (; i > 0; --i) { + device_destroy(chardev_class, devno + i - 1); + } + class_destroy(chardev_class); +free_cdev: + cdev_del(&chardev); +free_device_number: + unregister_chrdev_region(devno, NUMBER_OF_MINOR_DEVICE); + return -EIO; +} + +static void __exit ModExit(void) +{ + unsigned int i = 0; + + printk(KERN_DEBUG DEVICE_NAME ": exit"); + +#ifdef CUSTOM_EXIT + CUSTOM_EXIT +#endif + + for (i = NUMBER_OF_MINOR_DEVICE; i > 0; --i) { + device_destroy(chardev_class, devno + i - 1); + } + class_destroy(chardev_class); + cdev_del(&chardev); + unregister_chrdev_region(devno, NUMBER_OF_MINOR_DEVICE); +} + +module_init( ModInit ); +module_exit( ModExit ); \ No newline at end of file diff --git a/project/modules/infrared_sensor/infrared_sensor.c b/project/modules/infrared_sensor/infrared_sensor.c new file mode 100644 index 0000000..296d118 --- /dev/null +++ b/project/modules/infrared_sensor/infrared_sensor.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEVICE_NAME "infrared_sensor"; +#define NUMBER_OF_MINOR_DEVICE (1); + +#include "infrared_sensor.def.h" + +static unsigned int infrared_sensor_in_1 = 2; +static unsigned int infrared_sensor_in_2 = 3; +static unsigned int infrared_sensor_in_3 = 4; +static unsigned int infrared_sensor_in_4 = 17; + + +static int infrared_sensor_open(struct inode devfile, struct file *instance) +{ + int err; + + err = gpio_request(infrared_sensor_in_1, "rpi-gpio-1"); + if (err) { + printk("gpio_request failed %d\n", err); + return -1; + } + err = gpio_direction_output(infrared_sensor_in_1); + if (err) { + printk("gpio_direction_input failed %d\n", err); + gpio_free(infrared_sensor_in_1); + return -1; + } + printk("gpio 2 successfull configured\n"); + + + +} + +static ssize_t infrared_sensor_read( struct file *instance, char __user *buffer, + size_t max_bytes_to_read, loff_t *offset) +{ + +} + +static int infrared_sensor_close( struct inode devfile, struct file *instance) +{ + +} + + +static struct file_operations fops = { + .owner = THIS_MODULE, + .open = infrared_sensor_open, + .release = infrared_sensor_close, + .read = infrared_sensor_read, +}; + +#include "infrared_sensor.h"; diff --git a/project/modules/infrared_sensor/infrared_sensor.def.h b/project/modules/infrared_sensor/infrared_sensor.def.h new file mode 100644 index 0000000..dfe45f0 --- /dev/null +++ b/project/modules/infrared_sensor/infrared_sensor.def.h @@ -0,0 +1,4 @@ +static dev_t devno; +static struct cdev chardev; +static struct class *chardev_class; +static struct device *chardev_device[NUMBER_OF_MINOR_DEVICE]; diff --git a/project/modules/infrared_sensor/infrared_sensor.h b/project/modules/infrared_sensor/infrared_sensor.h new file mode 100644 index 0000000..181b976 --- /dev/null +++ b/project/modules/infrared_sensor/infrared_sensor.h @@ -0,0 +1,82 @@ +MODULE_AUTHOR("Manuel Vögele, Siegfried Kienzle, Simon Wörner"); +MODULE_DESCRIPTION(DEVICE_NAME "driver"); +MODULE_LICENSE("GPL"); + +static int __init ModInit(void) +{ + + unsigned int i = 0; + + printk(KERN_DEBUG DEVICE_NAME ": init"); + + if (alloc_chrdev_region(&devno, 0, NUMBER_OF_MINOR_DEVICE, DEVICE_NAME) < 0) + return -EIO; + + cdev_init(&chardev, &fops); + chardev.owner = THIS_MODULE; + chardev.ops = &fops; + + if (cdev_add(&chardev, devno, NUMBER_OF_MINOR_DEVICE)) { + pr_err(DEVICE_NAME ": cdev_add failed."); + goto free_device_number; + } + + chardev_class = class_create(THIS_MODULE, DEVICE_NAME); + + if (IS_ERR(chardev_class)) { + pr_err(DEVICE_NAME ": no udev support\n"); + goto free_cdev; + } + + for (i = 0; i < NUMBER_OF_MINOR_DEVICE; ++i) + { +#if NUMBER_OF_MINOR_DEVICE == 1 + chardev_device[i] = device_create(chardev_class, NULL, devno + i, NULL, DEVICE_NAME); +#else + chardev_device[i] = device_create(chardev_class, NULL, devno + i, NULL, DEVICE_NAME "%d", i); +#endif + + if (IS_ERR(chardev_device[i])) { + pr_err(DEVICE_NAME ": device create failed\n"); + goto free_device; + } + } + +#ifdef CUSTOM_INIT + CUSTOM_INIT +#endif + + return 0; + +free_device: + for (; i > 0; --i) { + device_destroy(chardev_class, devno + i - 1); + } + class_destroy(chardev_class); +free_cdev: + cdev_del(&chardev); +free_device_number: + unregister_chrdev_region(devno, NUMBER_OF_MINOR_DEVICE); + return -EIO; +} + +static void __exit ModExit(void) +{ + unsigned int i = 0; + + printk(KERN_DEBUG DEVICE_NAME ": exit"); + +#ifdef CUSTOM_EXIT + CUSTOM_EXIT +#endif + + for (i = NUMBER_OF_MINOR_DEVICE; i > 0; --i) { + device_destroy(chardev_class, devno + i - 1); + } + class_destroy(chardev_class); + cdev_del(&chardev); + unregister_chrdev_region(devno, NUMBER_OF_MINOR_DEVICE); +} + +module_init( ModInit ); +module_exit( ModExit ); \ No newline at end of file diff --git a/project/modules/infrared_sensor/infrared_sensor_in_1.c b/project/modules/infrared_sensor/infrared_sensor_in_1.c new file mode 100644 index 0000000..491726a --- /dev/null +++ b/project/modules/infrared_sensor/infrared_sensor_in_1.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEVICE_NAME "infrared_sensor"; +#define NUMBER_OF_MINOR_DEVICE (1); + +#include "../_common/infrared_sensor.def.h" + +static unsigned int infrared_sensor_in_1 = 2; + +static int infrared_sensor_open(struct inode devfile, struct file *instance) +{ + int err; + + err = gpio_request(infrared_sensor_in_1, "rpi-gpio-2"); + if (err) { + printk("gpio_request for in_1 failed %d\n", err); + return -1; + } + err = gpio_direction_input(infrared_sensor_in_3); + if (err) { + printk("gpio_direction_input for in_1 failed %d\n", err); + gpio_free(infrared_sensor_in_1); + return -1; + } + printk("gpio 2 successfull configured\n"); + return 0; + + +} + +static ssize_t infrared_sensor_read( struct file *instance, char __user *buffer, + size_t max_bytes_to_read, loff_t *offset) +{ + +} + +static int infrared_sensor_close( struct inode devfile, struct file *instance) +{ + printk( "driver_close called\n" ); + gpio_free( infrared_sensor_in_1 ); + return 0; +} + + +static struct file_operations fops = { + .owner = THIS_MODULE, + .open = infrared_sensor_open, + .release = infrared_sensor_close, + .read = infrared_sensor_read, +}; + +#include "../_common/infrared_sensor.h"; diff --git a/project/modules/infrared_sensor/infrared_sensor_in_2.c b/project/modules/infrared_sensor/infrared_sensor_in_2.c new file mode 100644 index 0000000..05aab43 --- /dev/null +++ b/project/modules/infrared_sensor/infrared_sensor_in_2.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEVICE_NAME "infrared_sensor"; +#define NUMBER_OF_MINOR_DEVICE (1); + +#include "../_common/infrared_sensor.def.h" + +static unsigned int infrared_sensor_in_2 = 3; + + +static int infrared_sensor_open(struct inode devfile, struct file *instance) +{ + int err; + + err = gpio_request(infrared_sensor_in_2, "rpi-gpio-2"); + if (err) { + printk("gpio_request for in_2 failed %d\n", err); + return -1; + } + err = gpio_direction_input(infrared_sensor_in_2); + if (err) { + printk("gpio_direction_input for in_2 failed %d\n", err); + gpio_free(infrared_sensor_in_2); + return -1; + } + printk("gpio 3 successfull configured\n"); + return 0; + + +} + +static ssize_t infrared_sensor_read( struct file *instance, char __user *buffer, + size_t max_bytes_to_read, loff_t *offset) +{ + +} + +static int infrared_sensor_close( struct inode devfile, struct file *instance) +{ + printk( "driver_close called\n" ); + gpio_free( infrared_sensor_in_2 ); + return 0; +} + + +static struct file_operations fops = { + .owner = THIS_MODULE, + .open = infrared_sensor_open, + .release = infrared_sensor_close, + .read = infrared_sensor_read, +}; + +#include "../_common/infrared_sensor.h"; diff --git a/project/modules/infrared_sensor/infrared_sensor_in_3.c b/project/modules/infrared_sensor/infrared_sensor_in_3.c new file mode 100644 index 0000000..a9e3430 --- /dev/null +++ b/project/modules/infrared_sensor/infrared_sensor_in_3.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEVICE_NAME "infrared_sensor"; +#define NUMBER_OF_MINOR_DEVICE (1); + +#include "../_common/infrared_sensor.def.h" + +static unsigned int infrared_sensor_in_3 = 4; + +static int infrared_sensor_open(struct inode devfile, struct file *instance) +{ + int err; + + err = gpio_request(infrared_sensor_in_3, "rpi-gpio-2"); + if (err) { + printk("gpio_request for in_3 failed %d\n", err); + return -1; + } + err = gpio_direction_input(infrared_sensor_in_3); + if (err) { + printk("gpio_direction_input for in_3 failed %d\n", err); + gpio_free(infrared_sensor_in_3); + return -1; + } + printk("gpio 4 successfull configured\n"); + return 0; + + +} + +static ssize_t infrared_sensor_read( struct file *instance, char __user *buffer, + size_t max_bytes_to_read, loff_t *offset) +{ + +} + +static int infrared_sensor_close( struct inode devfile, struct file *instance) +{ + printk( "driver_close called\n" ); + gpio_free( infrared_sensor_in_3 ); + return 0; +} + + +static struct file_operations fops = { + .owner = THIS_MODULE, + .open = infrared_sensor_open, + .release = infrared_sensor_close, + .read = infrared_sensor_read, +}; + +#include "../_common/infrared_sensor.h"; diff --git a/project/modules/infrared_sensor/infrared_sensor_in_4.c b/project/modules/infrared_sensor/infrared_sensor_in_4.c new file mode 100644 index 0000000..fc6c225 --- /dev/null +++ b/project/modules/infrared_sensor/infrared_sensor_in_4.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEVICE_NAME "infrared_sensor"; +#define NUMBER_OF_MINOR_DEVICE (1); + +#include "../_common/infrared_sensor.def.h" + +static unsigned int infrared_sensor_in_4 = 17; + +static int infrared_sensor_open(struct inode devfile, struct file *instance) +{ + int err; + + err = gpio_request(infrared_sensor_in_4, "rpi-gpio-17"); + if (err) { + printk("gpio_request for in_4 failed %d\n", err); + return -1; + } + err = gpio_direction_input(infrared_sensor_in_3); + if (err) { + printk("gpio_direction_input for in_4 failed %d\n", err); + gpio_free(infrared_sensor_in_4); + return -1; + } + printk("gpio 17 successfull configured\n"); + return 0; + + +} + +static ssize_t infrared_sensor_read( struct file *instance, char __user *buffer, + size_t max_bytes_to_read, loff_t *offset) +{ + +} + +static int infrared_sensor_close( struct inode devfile, struct file *instance) +{ + printk( "driver_close called\n" ); + gpio_free( infrared_sensor_in_4 ); + return 0; +} + + +static struct file_operations fops = { + .owner = THIS_MODULE, + .open = infrared_sensor_open, + .release = infrared_sensor_close, + .read = infrared_sensor_read, +}; + +#include "../_common/infrared_sensor.h";