Last active
February 20, 2016 03:11
-
-
Save meAbab/17b31ccfcfbc3dcc5481 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file IR-Transmitter.c | |
* Driver for Infrared Transmitter Sensor for RPI | |
* | |
*/ | |
#include <linux/init.h> | |
#include <linux/module.h> | |
#include <linux/device.h> | |
#include <linux/kernel.h> | |
#include <linux/gpio.h> | |
#include <linux/fs.h> | |
#include <asm/uaccess.h> | |
#define DEVICE_NAME "ir_tr" | |
#define CLASS_NAME "irTR" | |
static int majorNumber; | |
int cp_cnt; | |
static struct class *cl = NULL; | |
static struct device *dev = NULL; | |
static unsigned gpio_irtr = 2; | |
static int open_init (struct inode *inodep, struct file *filep); | |
ssize_t write_init (struct file *filep, const char __user *buffer, size_t len, loff_t *offset); | |
static int release_init (struct inode *inodep, struct file *filep); | |
static struct file_operations fops = | |
{ | |
.open = open_init, | |
.write = write_init, | |
.release = release_init, | |
}; | |
static int open_init (struct inode *inodep, struct file *filep){ | |
printk (KERN_INFO "IRTR: iRTr Sensor Device Opened\n"); | |
return 0; | |
} | |
static int release_init (struct inode *inodep, struct file *filep){ | |
printk (KERN_INFO "IRTR: iRTr Sensor Device closing\n"); | |
return 0; | |
} | |
static int __init irtr_init (void){ | |
printk(KERN_INFO "IRTR: Initializing driver to IRTR sensor\n"); | |
majorNumber = register_chrdev (0, "irTr", &fops); | |
if (majorNumber < 0){ | |
printk (KERN_INFO "Failed to register iRTr device file\n"); | |
return -EIO; | |
} | |
printk (KERN_INFO "IRTR: Sensor irTr registered correctly with majorNumber = %d\n", majorNumber); | |
cl = class_create (THIS_MODULE, CLASS_NAME); // class_create (THIS_MODULE, "CLASS_NAME"); // ISO C90: NO mixing in declaration | |
if (IS_ERR(cl)){ | |
unregister_chrdev(majorNumber, DEVICE_NAME); | |
printk(KERN_ALERT "Failed to register device CLASS\n"); | |
return -EIO; | |
} | |
dev = device_create (cl, NULL, MKDEV(majorNumber, 0), NULL, DEVICE_NAME); // device_create (CLASS, NULL, MKDEV(MajNum, 0), NULL, "DEVICE_NAME") | |
if (IS_ERR(dev)){ | |
class_destroy (cl); | |
unregister_chrdev(majorNumber, DEVICE_NAME); | |
printk(KERN_ALERT "Failed to register device CLASS\n"); | |
return -EIO; | |
} | |
if(!gpio_is_valid (gpio_irtr)){ | |
printk (KERN_INFO "GPIO_ERR: Invalid GPIO PIN\n"); | |
return -ENODEV; | |
} | |
gpio_request (gpio_irtr, "sysfs"); // int gpio_request(unsigned int gpio, const char *label) | |
gpio_direction_output (gpio_irtr, 0); // int gpio_direction_output(unsigned gpio, int value) | |
gpio_export (gpio_irtr, false); // int gpio_export(unsigned int gpio, bool direction_may_change); | |
//gpio_set_value (gpio_irtr, 1); | |
return 0; | |
} | |
static void __exit irtr_exit(void){ | |
device_destroy (cl, MKDEV(majorNumber, 0)); | |
class_unregister(cl); | |
class_destroy (cl); | |
unregister_chrdev(majorNumber, DEVICE_NAME); | |
printk (KERN_INFO "Clearing the iRTr sensor driver\n"); | |
} | |
char irtr_st[2]; | |
ssize_t write_init (struct file *filep, const char __user *buffer, size_t len, loff_t *offset){ | |
cp_cnt = copy_from_user (irtr_st, buffer, sizeof(irtr_st)); | |
if (irtr_st[0] == '1'){ | |
gpio_set_value (gpio_irtr, 1); | |
} | |
else if (irtr_st[0] == '0'){ | |
gpio_set_value (gpio_irtr, 0); | |
} | |
if(cp_cnt == 0){ | |
printk (KERN_INFO "Something written from USER_SPACE\n"); | |
return 0; | |
} | |
else{ | |
printk (KERN_INFO "Failed to data transfer from space->(KERN 2 USER)\n"); | |
return -EFAULT; | |
} | |
return 0; | |
} | |
module_init (irtr_init); | |
module_exit (irtr_exit); | |
MODULE_LICENSE ("GPL"); | |
MODULE_AUTHOR ("-Rafiq-"); | |
MODULE_DESCRIPTION ("Infrared Transmitter Kmod"); | |
MODULE_VERSION ("0.1"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
obj-m := IR-Transmitter.o | |
all: | |
make -C /lib/modules/$(shell uname -r)/build/ M=$(shell pwd) modules | |
clean: | |
make -C /lib/modules/$(shell uname -r)/build/ M=$(shell pwd) clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file U_irtr.c | |
* User program for Infrared Transmitter Sensor | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <fcntl.h> | |
int main(){ | |
char *sen_st; | |
int fd; | |
fd = open ("/dev/ir_tr", O_RDWR); | |
while (1){ | |
sen_st = "1"; | |
write (fd, sen_st, 1); | |
sleep (1); | |
sen_st = "0"; | |
write (fd, sen_st, 1); | |
sleep (1); | |
} | |
close (fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment