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

lcd_driver

parents
No related branches found
No related tags found
No related merge requests found
app.c 0 → 100644
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/socket.h>
#define LCD_DEV_PATH "/dev/lcd_dev"
#define BUFF_SIZE 50
#define INTERVAL 50000
void lcd_convert(char* input, int row) {
unsigned char hex = 0x00;
unsigned char cursor = 0x00;
instr(0x06);
cursor = 0x80 | (row + row * 0x40);
instr(cursor);
for (int i = 0; i < strlen(input); i++) {
hex=0xFF & input[i];
writedata(hex);
}
}
void writedata(unsigned char data) {
unsigned char instr[2] = { 0x00, 0x00 };
instr[0] = data & 0xF0;
instr[1] = (data & 0x0F) << 4; //data shift
int lcd_dev;
lcd_dev = open(LCD_DEV_PATH, O_RDWR);
usleep(40);
char buf[0];
for (int i = 0; i < 2; i++) {
instr[i] |= (1 << 3);
instr[i] |= (1 << 0);
instr[i] &= ~(1 << 1);
instr[i] |= (1 << 2);
buf[0]=instr[i];
write(lcd_dev, buf, 1);
instr[i] &= ~(1 << 2);
buf[0]=instr[i];
write(lcd_dev, buf, 1);
}
}
void instr(unsigned char instr) {
int k=0;
unsigned char instr1[2] = { 0x00, 0x00 };
instr1[0] = instr & 0xF0;
instr1[1] = (instr & 0x0F) << 4;
int lcd_dev;
lcd_dev = open(LCD_DEV_PATH, O_RDWR);
usleep(40);
char buf[1];
for (int i = 0; i < 2; i++) {
instr1[i] &= ~(1 << 3);
instr1[i] &= ~(1 << 0);
instr1[i] &= ~(1 << 1);
instr1[i] |= (1 << 2);
buf[0]=instr1[i];
write(lcd_dev, buf, 1);
instr1[i] &= ~(1 << 2);
buf[0]=instr1[i];
write(lcd_dev, buf, 1);
}
}
int main(void)
{
int server_socket;
int client_socket;
int client_addr_size;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
char buff_r[BUFF_SIZE+5];
char buff_s[BUFF_SIZE+5];
int lcd_fd;
char lcd_data;
lcd_fd = open(LCD_DEV_PATH, O_RDWR);
if(lcd_fd < 0){
printf("fail to open LCD sensor device\n");
return 1;
}
server_socket = socket(PF_INET, SOCK_STREAM, 0);
if(server_socket == -1) {
printf("server socket fail\n");
exit(1);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(4026);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
printf("cannot bind server\n");
exit(1);
}
if(listen(server_socket, 1024) == -1) {
printf("cannot listen\n");
exit(1);
}
listen(server_socket, 3);
client_addr_size = sizeof(client_addr);
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_size);
if(client_socket == -1) {
printf("cannot connect to client\n");
exit(1);
}
char a[32]={
0x34, 0x30, 0x34, 0x30, 0x34, 0x30, 0x24, 0x20, 0x24, 0x20, 0x84, 0x80, 0x4, 0x0,
0x84, 0x80, 0x4, 0x0, 0xE4, 0xE0, 0x4, 0x0, 0x64, 0x60, 0x84, 0x80, 0x4, 0x0, 0x2D,
0x29, 0x1D, 0x19};
char buf[1];
int data=0;
for(int i=0;i<32;i++){
buf[0]=a[i];
lcd_data = buf[0];
write(lcd_fd, buf, 1);
lcd_data = '\0';
}
while(1){
recv(client_socket, buff_r, BUFF_SIZE, 0);
recv(client_socket, buff_s, BUFF_SIZE, 0);
lcd_convert(buff_r,0);
lcd_convert(buff_s,1);
printf("%s %s\n", buff_r, buff_s);
sleep(2);
memset(buff_r, '\0', sizeof(buff_r));
memset(buff_s, '\0', sizeof(buff_s));
}
sleep(1);
close(client_socket);
close(server_socket);
close(lcd_fd);
return 0;
}
lcd_dev.c 0 → 100644
//#include "bcm2835.h"
#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 IR_MAJOR_NUMBER 504
#define IR_DEV_NAME "lcd_dev"
#define I2C_BASE_ADDR 0x3F804000
#define DLEN 0x08
#define FIFO 0x10
#define S 0x04
#define C 0x00
#define DIV 0x14
#define BSC_A 0x0C
static void __iomem *i2c_base;
volatile unsigned int *i2c, paddr;
int lcd_open(struct inode *inode, struct file *filp){
printk(KERN_ALERT "LCD sensor driver open!!\n");
i2c_base = ioremap(I2C_BASE_ADDR, 0xFF);
i2c=(volatile unsigned int*)(i2c_base);
return 0;
}
int lcd_release(struct inode *inode, struct file *filp){
printk(KERN_ALERT "Infrared distance sensor driver closed!!\n");
return 1;
}
ssize_t lcd_write(struct file* flip, const char* buf, size_t count, loff_t* f_pos){
char* kbuf;
kbuf=kmalloc(count, GFP_KERNEL);
copy_from_user(kbuf, buf, 1);
//printk(KERN_ALERT "SEX: %x\n", *kbuf);
//char buffer[1];
//buffer[0]=0x01;
unsigned int len=1;
volatile unsigned int* dlen = (volatile unsigned int*)(i2c_base + DLEN);
volatile unsigned int* fifo = (volatile unsigned int*)(i2c_base + FIFO);
volatile unsigned int* status = (volatile unsigned int*)(i2c_base + S);
volatile unsigned int* control = (volatile unsigned int*)(i2c_base + C);
volatile unsigned int* bsc_a = (volatile unsigned int*)(i2c_base + BSC_A);
*bsc_a = 0x3F;
unsigned int remaining=len;
unsigned int i=0;
unsigned int reason = 0;
*control |= 0x20;
*status = (0x200 | 0x100 | 0x02);
*dlen = len;
while(remaining &&(i<16)){
*fifo = kbuf[i];
i++;
remaining--;
}
*control = (0x8000|0x80);
while(!(*status & 0x02)){
while(remaining && (*status & 0x10)){
*fifo = kbuf[i];
i++;
remaining--;
}
}
if(*status & 0x100)
reason = 1;
else if(*status & 0x200)
reason = 2;
else if(remaining)
reason = 4;
*control |= 0x02;
printk(KERN_ALERT "reason: %d\n", reason);
//copy_to_user(buf, &reason, sizeof(int));
return 0;
}
/*
ssize_t lcd_read(struct file *filp, char *buf, size_t size, loff_t *f_pos){
int
char buffer[1];
buffer[0]=0x01;
unsigned int len=1;
volatile unsigned int* dlen = (volatile unsigned int*)(i2c_base + DLEN);
volatile unsigned int* fifo = (volatile unsigned int*)(i2c_base + FIFO);
volatile unsigned int* status = (volatile unsigned int*)(i2c_base + S);
volatile unsigned int* control = (volatile unsigned int*)(i2c_base + C);
unsigned int remaining=len;
unsigned int i=0;
unsigned int reason = 0;
*control |= 0x20;
*status = (0x200 | 0x100 | 0x02);
*dlen = len;
while(remaining &&(i<16)){
*fifo = buffer[i];
i++;
remaining--;
}
*control = (0x8000|0x80);
while(!(*status & 0x02)){
while(remaining && (*status & 0x10)){
*fifo = buffer[i];
i++;
remaining--;
}
}
if(*status & 0x100)
reason = 1;
else if(*status & 0x200)
reason = 2;
else if(remaining)
reason = 4;
*control |= 0x02;
printk(KERN_ALERT "reason: %d\n", reason);
copy_to_user(buf, &reason, sizeof(int));
return 0;
return size;
}
*/
static struct file_operations ir_fops = {
.owner = THIS_MODULE,
.open = lcd_open,
.release = lcd_release,
.write = lcd_write
};
int __init lcd_init(void){
if(register_chrdev(IR_MAJOR_NUMBER, IR_DEV_NAME, &ir_fops) < 0)
printk(KERN_ALERT "LCD initialization failed\n");
else
printk(KERN_ALERT "LCD initialization success\n");
return 0;
}
void __exit lcd_exit(void){
unregister_chrdev(IR_MAJOR_NUMBER, IR_DEV_NAME);
printk(KERN_ALERT "LCD driver exit done");
}
module_init(lcd_init);
module_exit(lcd_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("MW");
MODULE_DESCRIPTION("joreka");
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment