Skip to content
Snippets Groups Projects
Commit f228d35d authored by hyunjun_cho's avatar hyunjun_cho
Browse files

control Pi

parents
No related branches found
No related tags found
No related merge requests found
Showing with 442 additions and 0 deletions
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
typedef struct{
unsigned int hydro_integer;
unsigned int hydro_float;
unsigned int temp_integer;
unsigned int temp_float;
}Data;
#define HIGH 1
#define LOW 0
#define HYDRO_MAJOR_NUMBER 505
#define HYDRO_DEV_NAME "hydro_dev"
#define IOCTL_HYDRO_MAGIC_NUMBER 'h'
#define IOCTL_CMD_HYDRO _IOR(IOCTL_HYDRO_MAGIC_NUMBER, 0, Data)
#define GPIO_BASE_ADDRESS 0x3F200000
#define GPFSEL0 0x00
#define GPFSEL1 0x04
#define GPSET1 0x1C
#define GPCLR1 0x28
#define GPLEV1 0x34
static void __iomem* gpio_base;
volatile unsigned int* gpsel0;
volatile unsigned int* gpsel1;
volatile unsigned int* gpset1;
volatile unsigned int* gpclr1;
volatile unsigned int* gplev1;
int hydro_open(struct inode * inode, struct file * filp){
printk(KERN_ALERT "\nhumidity driver open\n");
gpio_base = ioremap(GPIO_BASE_ADDRESS, 0xFF);
gpsel0 = (volatile unsigned int*)(gpio_base + GPFSEL0);
gpsel1 = (volatile unsigned int*)(gpio_base + GPFSEL1);
gpset1 = (volatile unsigned int*)(gpio_base + GPSET1);
gpclr1 = (volatile unsigned int*)(gpio_base + GPCLR1);
gplev1 = (volatile unsigned int*)(gpio_base + GPLEV1);
/*
*gpsel0 |= (1<<26);
*gpsel0 |= (1<<29);
*gpsel1 |= (1<<2);
*gpsel1 |= (1<<5);*/
return 0;
}
int hydro_release(struct inode * inode, struct file * filp) {
printk(KERN_ALERT "humidity driver closed\n\n");
iounmap((void *)gpio_base);
return 0;
}
long hydro_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
Data data;
int result[5] = {0};
int index = 0;
int pre_data = HIGH;
int counter = 0;
int i = 0;
switch (cmd){
case IOCTL_CMD_HYDRO:
*gpsel0 |= (1 << 12); //GPIO 4 SET OUTPUT MODE
*gpset1 |= (1 << 4); //GPIO 4 SET HIGH
mdelay(800); //SLEEP FOR 800 miliseconds to synchronize bits from loop
*gpclr1 |= (1 << 4); //GPIO 4 SET LOW
mdelay(18); //SLEEP FOR 18 miliseconds
*gpset1 |= (1 << 4); //GPIO 4 SET HIGH
udelay(30); //SLEEP FOR 30 microseconds
*gpsel0 &= ~(1 << 12); //GPIO 4 SET INPUT MODE
//except first 3(wait, response, ready) signals
for(i = 0; i < 3; i++){
if(((*gplev1 >> 4) & 0x01) == LOW){
while(((*gplev1 >> 4) & 0x01) == LOW){} continue;
}
if(((*gplev1 >> 4) & 0x01) == HIGH){
while(((*gplev1 >> 4) & 0x01) == HIGH){} continue;
}
}
//interpret '0' and '1' bits
while(index < 40){
if(((*gplev1 >> 4) & 0x01) == LOW) continue;
counter = 0;
while(((*gplev1 >> 4) & 0x01) == HIGH){
counter++;
udelay(1);
}
if(counter > 80) break;
result[index / 8] <<= 1;
if(counter > 28) result[index / 8] |= 1;
index++;
}
//printk(KERN_INFO "data[0]: %x data[1]: %x data[2]: %x data[3]: %x data[4]: %x\n", data[0], data[1], data[2], data[3], data[4]);
if(result[4] != ((result[0] + result[1] + result[2] + result[3]) & 0xFF))
break;
data.hydro_integer = result[0];
data.hydro_float = result[1];
data.temp_integer = result[2];
data.temp_float = result[3];
//printk(KERN_INFO "humidity: %d.%d%% temperature: %d.%d%%\n", info.humidity_integer, info.humidity_float, info.temperature_integer, info.temperature_float);
copy_to_user((void*)arg, (void*)&data, sizeof(data));
printk(KERN_ALERT "hydro done\n");
break;
default :
printk(KERN_ALERT "ioctl : command error\n");
}
return 0;
}
static struct file_operations hydro_fops = {
.owner = THIS_MODULE,
.open = hydro_open,
.release = hydro_release,
.unlocked_ioctl = hydro_ioctl
};
int __init hydro_init (void) {
if(register_chrdev(HYDRO_MAJOR_NUMBER, HYDRO_DEV_NAME, &hydro_fops) < 0)
printk(KERN_ALERT "hydro driver initalization failed\n");
else
printk(KERN_ALERT "hydro driver initalization succeed\n");
return 0;
}
void __exit hydro_exit(void){
unregister_chrdev(HYDRO_MAJOR_NUMBER, HYDRO_DEV_NAME);
printk(KERN_ALERT "hydro driver exit");
}
module_init(hydro_init);
module_exit(hydro_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("joreka");
MODULE_DESCRIPTION("hydro_dev");
File added
#include <linux/build-salt.h>
#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
BUILD_SALT;
MODULE_INFO(vermagic, VERMAGIC_STRING);
MODULE_INFO(name, KBUILD_MODNAME);
__visible struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
#ifdef CONFIG_RETPOLINE
MODULE_INFO(retpoline, "Y");
#endif
static const struct modversion_info ____versions[]
__used
__attribute__((section("__versions"))) = {
{ 0xad1a7def, "module_layout" },
{ 0x6bc3fbc0, "__unregister_chrdev" },
{ 0xcfc78319, "__register_chrdev" },
{ 0xdb7305a1, "__stack_chk_fail" },
{ 0xf4fa543b, "arm_copy_to_user" },
{ 0x8e865d3c, "arm_delay_ops" },
{ 0x8f678b07, "__stack_chk_guard" },
{ 0xedc03953, "iounmap" },
{ 0x2e5810c6, "__aeabi_unwind_cpp_pr1" },
{ 0xe97c4103, "ioremap" },
{ 0x7c32d0f0, "printk" },
{ 0xb1ad28e0, "__gnu_mcount_nc" },
};
static const char __module_depends[]
__used
__attribute__((section(".modinfo"))) =
"depends=";
MODULE_INFO(srcversion, "848B380E4218A5ABA3C9C06");
File added
File added
kernel//home/pi/Desktop/project/hum/hum_dev.ko
cmd_/home/pi/Desktop/project/hydro/hydro_dev.ko := ld -r -EL -T ./scripts/module-common.lds -T ./arch/arm/kernel/module.lds --build-id -o /home/pi/Desktop/project/hydro/hydro_dev.ko /home/pi/Desktop/project/hydro/hydro_dev.o /home/pi/Desktop/project/hydro/hydro_dev.mod.o ; true
This diff is collapsed.
This diff is collapsed.
/home/pi/Desktop/project/hydro/hydro_dev.ko
/home/pi/Desktop/project/hydro/hydro_dev.o
KERNEL_VER := $(shell uname -r)
KERNEL_DIR = '/lib/modules/$(KERNEL_VER)/build'
obj-m := hydro_dev.o
PWD := $(shell pwd)
all :
make -C $(KERNEL_DIR) M=$(PWD) modules
clean :
make -C $(KERNEL_DIR) M=$(PWD) clean
in :
sudo insmod hydro_dev.ko
sudo mknod -m 666 /dev/hydro c 505 0
out :
sudo rmmod hydro_dev
in2 :
sudo insmod hydro_dev.ko
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <asm/mach/map.h>
#include <asm/io.h>
#include <asm/uaccess.h>
typedef struct {
unsigned int tempi;
unsigned int tempf;
unsigned int hydroi;
unsigned int hydrof;
}Data;
#define HYDRO_MAJOR_NUMBER 505
#define HYDRO_DEV_NAME "hydro_dev"
#define GPIO_BASE_ADDR 0x3F200000
#define GPFSEL0 0x00
#define GPFSEL1 0x04
#define GPSET0 0x1C
#define GPCLR0 0x28
#define GPLEV0 0x34
#define HIGH 1
#define LOW 0
static void __iomem *gpio_base;
volatile unsigned int *gpsel0;
volatile unsigned int *gpsel1;
volatile unsigned int *gpset1;
volatile unsigned int *gpclr1;
volatile unsigned int *gplev1;
int hydro_open(struct inode *inode,struct file *flip){
printk(KERN_ALERT "HYDRO driver open!!\n");
gpio_base = ioremap(GPIO_BASE_ADDR, 0x60);
gpsel0 = (volatile unsigned int *)(gpio_base + GPFSEL0);
gpsel1 = (volatile unsigned int *)(gpio_base + GPFSEL1);
gpset1 = (volatile unsigned int *)(gpio_base + GPSET0);
gpclr1 = (volatile unsigned int *)(gpio_base + GPCLR0);
gplev1 = (volatile unsigned int *)(gpio_base + GPLEV0);
*gpsel0 |= (1<<26);
*gpsel0 |= (1<<29);
*gpsel1 |= (1<<2);
*gpsel1 |= (1<<5);
return 0;
}
int hydro_release(struct inode *inode,struct file *flip){
printk(KERN_ALERT "HYDRO driver closed!!\n");
iounmap((void *)gpio_base);
return 0;
}
ssize_t hydro_read(struct file *filp, char* buf, size_t counts, loff_t *f_pos) {
printk(KERN_ALERT "read function called!!\n");
Data data;
int result[5] = {0, };
int time = 0;
int prev = HIGH;
int index = 0;
int i = 0;
*gpsel0 |= (1<<12);
*gpset1 |= (1<<4);
mdelay(800);
*gpclr1 |= (1<<4);
mdelay(18);
*gpset1 |= (1<<4);
udelay(30);
*gpsel0 = (0<<24);
for(i = 0; i < 3; i++) {
if(((*gplev1 >> 4) & HIGH) == LOW) {
while(((*gplev1 >> 4) & HIGH) == LOW){}
continue;
}
if(((*gplev1 >> 4) & HIGH) == HIGH) {
while(((*gplev1 >> 4) & HIGH) ==HIGH){}
continue;
}
}
//int status = LOW;
while(index < 40) {
if(((*gplev1 >> 4) & 0x01) == LOW)
continue;
time = 0;
while(((*gplev1 >> 4) & 0x01) == HIGH){
time++;
udelay(1);
}
if(time > 70)
break;
result[index / 8] <<= 1;
if(time > 28)
result[index/8] |= 1;
time++;
}
int wrong = -1;
printk(KERN_ALERT "%d %d %d %d\n", result[0], result[1], result[2], result[3]);
if((result[4] == (result[0] + result[1] + result[2] + result[3])& 0xFF) && result[0] != 0) {
data.hydroi = result[0];
data.hydrof = result[1];
data.tempi = result[2];
data.tempf = result[3];
copy_to_user(buf, (const void*)&data, sizeof(data));
}
else
copy_to_user(buf, &wrong, sizeof(int));
return counts;
}
static struct file_operations hydro_fops={
.owner = THIS_MODULE,
.open = hydro_open,
.release = hydro_release,
.read = hydro_read
};
int __init hydro_init(void){
if(register_chrdev(HYDRO_MAJOR_NUMBER, HYDRO_DEV_NAME, &hydro_fops)<0)
printk(KERN_ALERT "HYDRO driver initialization fail\n");
else
printk(KERN_ALERT "HYDRO driver intialization success\n");
return 0;
}
void __exit hydro_exit(void){
unregister_chrdev(HYDRO_MAJOR_NUMBER, HYDRO_DEV_NAME);
printk(KERN_ALERT "HYDRO driver exit doen\n");
}
module_init(hydro_init);
module_exit(hydro_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("joreka");
MODULE_DESCRIPTION("HYDRO_DEV");
File added
#include <linux/build-salt.h>
#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
BUILD_SALT;
MODULE_INFO(vermagic, VERMAGIC_STRING);
MODULE_INFO(name, KBUILD_MODNAME);
__visible struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
#ifdef CONFIG_RETPOLINE
MODULE_INFO(retpoline, "Y");
#endif
static const struct modversion_info ____versions[]
__used
__attribute__((section("__versions"))) = {
{ 0xad1a7def, "module_layout" },
{ 0x6bc3fbc0, "__unregister_chrdev" },
{ 0xcfc78319, "__register_chrdev" },
{ 0xdb7305a1, "__stack_chk_fail" },
{ 0xf4fa543b, "arm_copy_to_user" },
{ 0x8e865d3c, "arm_delay_ops" },
{ 0x8f678b07, "__stack_chk_guard" },
{ 0xedc03953, "iounmap" },
{ 0x2e5810c6, "__aeabi_unwind_cpp_pr1" },
{ 0xe97c4103, "ioremap" },
{ 0x7c32d0f0, "printk" },
{ 0xb1ad28e0, "__gnu_mcount_nc" },
};
static const char __module_depends[]
__used
__attribute__((section(".modinfo"))) =
"depends=";
MODULE_INFO(srcversion, "CBED33CF7ABA6C5B93EA66B");
File added
File added
kernel//home/pi/Desktop/project/hydro/hydro_dev.ko
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment