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

seat control Pi

parents
No related branches found
No related tags found
No related merge requests found
led2/app 0 → 100755
File added
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/sysmacros.h>
#include <time.h>
#define LED_MAJOR_NUMBER 507
#define LED_MINOR_NUMBER 100
#define LED_DEV_PATH_NAME "/dev/led2_dev"
#define IOCTL_MAGIC_NUMBER 'J'
#define IOCTL_CMD_SET_DIRECTION _IOWR(IOCTL_MAGIC_NUMBER,0,int)
#define IOCTL_CMD_SET_BLINK _IOWR(IOCTL_MAGIC_NUMBER,1,int)
#define IOCTL_CMD_SET_OFF _IOWR(IOCTL_MAGIC_NUMBER,2,int)
int main(){
dev_t led2_dev;
int fd;
int a=1;
int b=5;
led2_dev=makedev(LED_MAJOR_NUMBER, LED_MINOR_NUMBER);
mknod(LED_DEV_PATH_NAME, S_IFCHR|0666, led2_dev);
fd=open(LED_DEV_PATH_NAME, O_RDWR);
if(fd<0){
printf("fail to open led\n");
return -1;
}
ioctl(fd, IOCTL_CMD_SET_DIRECTION, &a);
sleep(1);
ioctl(fd, IOCTL_CMD_SET_BLINK, &b);
sleep(1);
ioctl(fd, IOCTL_CMD_SET_OFF, &b);
close(fd);
return 0;
}
#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>
#define LED_MAJOR_NUMBER 507
#define LED_DEV_NAME "led2_dev"
#define GPIO_BASE_ADDR 0x3F200000
#define GPFSEL1 0x04
#define GPFSEL2 0x08
#define GPSET0 0x1C
#define GPCLR0 0x28
#define GPLEV0 0x34
#define IOCTL_MAGIC_NUMBER 'J'
#define IOCTL_CMD_SET_DIRECTION _IOWR(IOCTL_MAGIC_NUMBER,0,int)
#define IOCTL_CMD_SET_BLINK _IOWR(IOCTL_MAGIC_NUMBER,1,int)
#define IOCTL_CMD_SET_OFF _IOWR(IOCTL_MAGIC_NUMBER,2,int)
static char *buffer = NULL;
static int buf2;
static void __iomem *gpio_base;
volatile unsigned int *gpsel1;
volatile unsigned int *gpsel2;
volatile unsigned int *gpset1;
volatile unsigned int *gpclr1;
volatile unsigned int *gplev0;
int led_open(struct inode *inode, struct file *flip) {
printk(KERN_ALERT "LED driver open!!\n");
gpio_base = ioremap(GPIO_BASE_ADDR, 0x60);
gpsel1 = (volatile unsigned int *)(gpio_base + GPFSEL1);
gpsel2 = (volatile unsigned int *)(gpio_base + GPFSEL2);
gpset1 = (volatile unsigned int *)(gpio_base + GPSET0);
gpclr1 = (volatile unsigned int *)(gpio_base + GPCLR0);
gplev0 = (volatile unsigned int *)(gpio_base + GPLEV0);
return 0;
}
int led_release(struct inode *inode, struct file *flip) {
printk(KERN_ALERT "LED driver closed!!\n");
iounmap((void *)gpio_base);
return 0;
}
long led_ioctl(struct file *flip, unsigned int cmd, unsigned long arg) {
int kbuf = -1;
int i = 0;
*gpsel1 |= (1 << 24);
switch (cmd) {
case IOCTL_CMD_SET_DIRECTION:
copy_from_user(&kbuf, (const void*)arg, 4);
if (kbuf == 0) {
printk(KERN_ALERT "LED set direction in !!\n");
*gpsel1 |= (0 << 24);
}
else if (kbuf == 1) {
printk(KERN_ALERT "LED set direction out!!\n");
*gpsel1 |= (1 << 24);
//*gpsel2 |= (1 << 3);
}
else {
printk(KERN_ALERT "ERROR direction parameter error\n");
return -1;
}
break;
case IOCTL_CMD_SET_BLINK:
copy_from_user(&kbuf, (const void*)arg, 4);
*gpset1 |= (1 << 18);
break;
case IOCTL_CMD_SET_OFF:
*gpclr1 |= (1<<18);
break;
}
return 0;
}
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.release = led_release,
.unlocked_ioctl = led_ioctl
};
int __init led_init(void) {
if (register_chrdev(LED_MAJOR_NUMBER, LED_DEV_NAME, &led_fops) < 0)
printk(KERN_ALERT "LED driver initialization fail\n");
else {
printk(KERN_ALERT "LED driver intialization success\n");
}
//*gpsel1 |= (0 << 24);
return 0;
}
void __exit led_exit(void) {
unregister_chrdev(LED_MAJOR_NUMBER, LED_DEV_NAME);
printk(KERN_ALERT "LED driver exit doen\n");
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("joreka");
MODULE_DESCRIPTION("led2_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" },
{ 0x5f754e5a, "memset" },
{ 0x28cc25db, "arm_copy_from_user" },
{ 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, "8EA22616AAFFE91DFC17AFE");
File added
File added
kernel//home/pi/Desktop/project/led2/led2_dev.ko
sudo rmmod led2_dev
sudo rm /dev/led2_dev
make
sudo insmod led2_dev.ko
sudo mknod -m 666 /dev/led2_dev c 502 0
cmd_/home/pi/Desktop/project/sound/sound_dev.ko := ld -r -EL -T ./scripts/module-common.lds -T ./arch/arm/kernel/module.lds --build-id -o /home/pi/Desktop/project/sound/sound_dev.ko /home/pi/Desktop/project/sound/sound_dev.o /home/pi/Desktop/project/sound/sound_dev.mod.o ; true
This diff is collapsed.
This diff is collapsed.
/home/pi/Desktop/project/sound/sound_dev.ko
/home/pi/Desktop/project/sound/sound_dev.o
KERNEL_DIR = /lib/modules/4.19.97-v7+/build
obj-m := sound_dev.o
PWD := $(shell pwd)
all:
make -C $(KERNEL_DIR) M=$(PWD) modules
clean:
make -C $(KERNEL_DIR) M=$(PWD) clean
in:
sudo insmod sound_dev.ko
out:
sudo rmmod sound_dev
in2:
sudo mknod -m 666 /dev/sound_dev c 502 0
sound/app 0 → 100755
File added
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include <errno.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
//#define TEMP_DEV_PATH "/dev/temp_dev"
#define SOUND_DEV_PATH "/dev/sound_dev"
#define LED_DEV_PATH "/dev/led_dev"
#define LED2_DEV_PATH "/dev/led2_dev"
#define INTERVAL 50000
int main() {
int sound_fd = open(SOUND_DEV_PATH, O_RDWR);
int sound_data = 0;
int led_fd, led2_fd;
int led_data, led2_data;
//led_fd = open(LED_DEV_PATH, O_RDONLY);
//led2_fd = open(LED2_DEV_PATH, O_RDONLY);
char config[3] = {0, };
int data[2] = {0, };
float result[2] = {0, };
char reg = 0x03;
if(sound_fd < 0) {
printf("fail to open SOUND sensor device\n");
return 1;
}
while(1){
read(sound_fd, &sound_data, sizeof(int));
printf("%d\n", sound_data);
sleep(1);
}
close(sound_fd);
//close(led_fd);
//close(led2_fd);
return 0;
}
kernel//home/pi/Desktop/project/sound/sound_dev.ko
sudo rmmod sound_dev
sudo rm /dev/sound_dev
make
sudo insmod sound_dev.ko
sudo mknod -m 666 /dev/sound_dev c 502 0
#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>
#define SOUND_MAJOR_NUMBER 502
#define SOUND_DEV_NAME "/dev/sound_dev"
#define GPIO_BASE_ADDR 0x3F200000
#define SPI_BASE_ADDR 0x3F204000
#define GPFSEL0 0x00
#define GPFSEL1 0x04
#define SPI_CS 0x00
#define SPI_FIFO 0x04
static void __iomem *gpio_base;
static void __iomem *spi_base;
volatile unsigned int *gpfsel0, *gpfsel1;
volatile unsigned int *spi_cs, *spi_fifo;
int sound_open(struct inode *inode, struct file *filp){
printk(KERN_ALERT "sound detection sensor driver open!!\n");
gpio_base = ioremap(GPIO_BASE_ADDR, 0xFF);
spi_base = ioremap(SPI_BASE_ADDR, 0xFF);
gpfsel0 = (volatile unsigned int*)(gpio_base + GPFSEL0);
gpfsel1 = (volatile unsigned int*)(gpio_base + GPFSEL1);
spi_cs = (volatile unsigned int*)(spi_base + SPI_CS);
spi_fifo = (volatile unsigned int*)(spi_base + SPI_FIFO);
*gpfsel0 &= ~(0x1FF << 21);
*gpfsel0 |= (0x24 << 24);
*gpfsel1 &= ~(0x3F); // 111111 = 0x3F
*gpfsel1 |= (0x24); // 100100 = 0x24
*spi_cs &= ~(0xFFFF); // cs clear
*spi_cs &= ~(0x01<<2);
*spi_cs &= ~(0x01<<3); // spi(0,0)
*spi_cs |= (0x03<<4); // clear FIFO
return 0;
}
int sound_release(struct inode *inode, struct file *filp){
printk(KERN_ALERT "sound detection sensor driver closed!!\n");
*gpfsel0 &= ~(0x1FF << 21);
*gpfsel1 &= ~(0x3F); // 111111 = 0x3F
*spi_cs &= ~(0xFFFF); // cs clear
iounmap((void*)gpio_base);
iounmap((void*)spi_base);
return 0;
}
ssize_t sound_read(struct file* flip, char* buf, size_t count, loff_t* f_pos){
printk(KERN_ALERT "read function called!!\n");
unsigned char spi_tData[3];
unsigned char spi_rData[3];
int tCount = 0;
int rCount = 0;
spi_tData[0] = 1;
spi_tData[1] = (0x08) << 4;
spi_tData[2] = 0; // start single d2 d1 d0
*spi_cs |= (0x03<<4); // clear FIFO
*spi_cs |= (0x01<<7); // TA = 1
while((tCount < 3) || (rCount < 3)){
while((*spi_cs & (1<<18)) && (tCount < 3)){
*spi_fifo = spi_tData[tCount];
tCount++;
}
while((*spi_cs & (1<<17)) && (rCount < 3)){
spi_rData[rCount] = *spi_fifo;
rCount++;
}
}
while((!(*spi_cs)) & (1<<16));
*spi_cs |= (0x03<<4); // clear FIFO
*spi_cs &= ~(1<<7); // TA = 0
int data = ((spi_rData[1]&0x03)<<8) + spi_rData[2];
printk(KERN_ALERT "reading input : %d \n", data);
copy_to_user(buf, &data, sizeof(int));
return count;
}
static struct file_operations sound_fops = {
.owner = THIS_MODULE,
.open = sound_open,
.release = sound_release,
.read = sound_read
};
int __init sound_init(void){
if(register_chrdev(SOUND_MAJOR_NUMBER, SOUND_DEV_NAME, &sound_fops) < 0)
printk(KERN_ALERT "sound detection sensor initialization failed!!\n");
else
printk(KERN_ALERT "sound detection sensor initialization success!!!\n");
return 0;
}
void __exit sound_exit(void){
unregister_chrdev(SOUND_MAJOR_NUMBER, SOUND_DEV_NAME);
printk(KERN_ALERT "sound detection sensor driver exit done!!!!");
}
module_init(sound_init);
module_exit(sound_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("joreka");
MODULE_DESCRIPTION("SOUND_DEV");
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment