작업 환경
-메인 OS : Windows 8.1K(Intel Core i5-4590)
-작업 OS : Ubuntu 14.04.5 LTS 64bit(VirtualBox)
-장 비 명 : Hybus-Smart4412
문제1. 다양항 방법으로 LED 점멸
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/ioctl.h>
#include<sys/stat.h>
//디바이스 드라이버가 들어있는 경로 위치를 적어논것
#define led "/dev/led"
#define tact_d "/dev/tactsw"
void LedUp(int dev);
void LedDown(int dev);
void LedAll(int dev);
void LedOddEven(int dev);
void LedInside(int dev);
void LedOutside(int dev);
int main()
{
int dev , i , num , tact;
unsigned char data , c;
//1. 해당 드라이버 경로
//2. O_RDWR
dev = open(led,O_RDWR);
tact = open(tact_d , O_RDWR);
if(dev <0)
{
printf("open failed!\n");
exit(0);
}
if(tact <0)
{
printf("open failed!\n");
exit(0);
}
while(1)
{
puts("**********JooSeok Menu**********");
puts("* 1. LED UP Shift *");
puts("* 2. LED DOWN Shift *");
puts("* 3. A L L *");
puts("* 4. O D D _ E V E N *");
puts("* 5. I N S I D E *");
puts("* 6. O U T S I D E *");
puts("* 12. E X I T *");
puts("********************************");
fputs("Select the menu number : " , stdout);
while(1)
{
read (tact , &c , sizeof(c));
usleep(100000);
if(c) break;
}
switch(c)
{
case 1:
printf("%d\n" , c);
LedUp(dev);
break;
case 2:
printf("%d\n" , c);
LedDown(dev);
break;
case 3:
printf("%d\n" , c);
LedAll(dev);
break;
case 4:
printf("%d\n" , c);
LedOddEven(dev);
break;
case 5:
printf("%d\n" , c);
LedInside(dev);
break;
case 6:
printf("%d\n" , c);
LedOutside(dev);
break;
case 12:
printf("%d\n" , c);
close(dev);
return 0;
}
putchar('\n');
}
close(tact);
close(dev);
return 0;
}
void LedUp(int dev)
{
int i;
unsigned char data;
for(i=1 ; i<9 ; ++i)
{
data = 0xff<<i;
usleep(100000);
//1. 파일의 디스크립트
//2. 데이터 전달
//3. 크기를 넣어준다.
write(dev , &data , sizeof(unsigned char));
usleep(100000);
}
}
void LedDown(int dev)
{
int i;
unsigned char data;
for(i=1 ; i<9 ; ++i)
{
data = 0xff>>i;
usleep(100000);
//1. 파일의 디스크립트
//2. 데이터 전달
//3. 크기를 넣어준다.
write(dev , &data , sizeof(unsigned char));
usleep(100000);
}
}
void LedAll(int dev)
{
int i;
unsigned char data;
for(i=1 ; i<15 ; ++i)
{
if(i%2 == 1)
{
data = 0x00; // led 켜짐
}
else
{
data = 0xff;
}
//1. 파일의 디스크립트
//2. 데이터 전달
//3. 크기를 넣어준다.
write(dev , &data , sizeof(unsigned char));
usleep(100000);
}
}
void LedOddEven(int dev)
{
int i , j;
unsigned char data;
for(i=1 ; i<15 ; ++i)
{
if(i%2 == 1)
{
data = 0x55;
}
else
{ data = 0xAA;
}
usleep(100000);
write(dev , &data , sizeof(unsigned char));
usleep(100000);
}
}
void LedInside(int dev)
{
int i;
unsigned char data;
for(i=1 ; i<5 ; ++i)
{
data = (0xFF>>i) & (0xFF<<i);
usleep(100000);
write(dev , &data , sizeof(unsigned char));
usleep(100000);
}
}
void LedOutside(int dev)
{
int i;
unsigned char data;
for(i=4 ; i>=0 ; --i)
{
data = ~((0xFF>>i) & (0xFF<<i));
usleep(100000);
write(dev , &data , sizeof(unsigned char));
usleep(100000);
}
}
문제2. tact버튼을 누를 경우 lcd 화면에 문자를 띄운다.
ex) A B C
D E F
G H 초기화
증가 감소 종료
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <asm/ioctls.h>
#define tact_d "/dev/tactsw"
#define led_d "/dev/led"
void ChaseAdd(int led, unsigned char *chase);
int tact, led;
int main()
{
unsigned char c;
unsigned char buf=0xff, chase=0xff;
if((tact = open(tact_d,O_RDWR)) < 0)
{
perror("tact open");
exit(1);
}
if((led = open(led_d,O_RDWR)) < 0)
{
perror("led open");
exit(1);
}
while(1)
{
while(1)
{
read(tact,&c,sizeof(c));
if(c)
break;
}
switch(c)
{
case 1: buf = ~(0x01);
write(led,&buf,sizeof(buf));
break;
case 2: buf = ~(0x02);
write(led,&buf,sizeof(buf));
break;
case 3: buf = ~(0x04);
write(led,&buf,sizeof(buf));
break;
case 4: buf = ~(0x08);
write(led,&buf,sizeof(buf));
break;
case 5: buf = ~(0x10);
write(led,&buf,sizeof(buf));
break;
case 6: buf = ~(0x20);
write(led,&buf,sizeof(buf));
break;
case 7: buf = ~(0x40);
write(led,&buf,sizeof(buf));
break;
case 8: buf = 0x7f;
write(led,&buf,sizeof(buf));
break;
case 9: buf = 0x00;
write(led,&buf,sizeof(buf));
break;
case 10: ChaseAdd(led,&chase);
break;
case 11: write(led,&chase,sizeof(chase));
break;
case 12: exit(1);
}
sleep(1);
buf = 0xff;
write(led,&buf,sizeof(buf));
}
close(tact);
close(led);
return 0;
}
void ChaseAdd(int led,unsigned char *chase)
{
sleep(1);
unsigned char d=0;
printf("input\n");
while(1)
{
while(1)
{
read(tact,&d,sizeof(d));
if(d)
break;
}
printf("%d\n",d);
switch(d)
{
case 1: *chase &= ~(0x01);
break;
case 2: *chase &= ~(0x02);
break;
case 3: *chase &= ~(0x04);
break;
case 4: *chase &= ~(0x08);
break;
case 5: *chase &= ~(0x10);
break;
case 6: *chase &= ~(0x20);
break;
case 7: *chase &= ~(0x40);
break;
case 8: *chase &= ~(0x80);
break;
case 10: return;
break;
case 12: exit(1);
}
sleep(1);
d=0;
}
printf("chase end\n");
}
문제3. 번호를 누룰때마다 숫자가 왼쪽으로 추가되며 출력되게 하고, 만약 10자를 넘을 경우 가장 왼쪽 숫자가 사라지게 만들자
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <asm/ioctls.h>
#define tact_d "/dev/tactsw"
#define lcd_d "/dev/clcd"
int main()
{
int tact, lcd, flag=0;
char op;
unsigned char data[10];
unsigned char c;
if((tact = open(tact_d,O_RDWR)) < 0)
{
perror("open");
exit(1);
}
if((lcd = open(lcd_d,O_RDWR)) < 0)
{
perror("open 2");
exit(1);
}
memset(data,0,sizeof(data));
while(1)
{
while(1)
{
read(tact,&c,sizeof(c));
if(c)
break;
}
if(flag >= 10)
{
strcpy(data,data+1);
}
switch(c)
{
case 1: strcat(data,"1");
write(lcd,&data,sizeof(data));
flag++;
break;
case 2: strcat(data,"2");
write(lcd,&data,sizeof(data));
flag++;
break;
case 3: strcat(data,"3");
write(lcd,&data,sizeof(data));
flag++;
break;
case 4: strcat(data,"4");
write(lcd,&data,sizeof(data));
flag++;
break;
case 5: strcat(data,"5");
write(lcd,&data,sizeof(data));
flag++;
break;
case 6: strcat(data,"6");
write(lcd,&data,sizeof(data));
flag++;
break;
case 7: strcat(data,"7");
write(lcd,&data,sizeof(data));
flag++;
break;
case 8: strcat(data,"8");
write(lcd,&data,sizeof(data));
flag++;
break;
case 9: strcat(data,"9");
write(lcd,&data,sizeof(data));
flag++;
break;
case 10: strcat(data,"0");
write(lcd,&data,sizeof(data));
flag++;
break;
case 11: exit(1);
case 12: exit(1);
}
sleep(1);
c=0;
}
close(tact);
return 0;
}
문제4. tactsw를 눌렀을 경우 dipsw가 켜져있을 경우 led불이 켜지게 만든다.
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <asm/ioctls.h>
#define tact_d "/dev/tactsw"
#define dip_d "/dev/dipsw"
#define led_d "/dev/led"
void ChaseAdd(int led, int tact, unsigned char *chase);
int main()
{
int tact, dip, led, flag=0;
char op;
unsigned char data, chase=0xff, chase2=0xff;
unsigned char c,t;
if((tact = open(tact_d,O_RDWR)) < 0)
{
perror("open");
exit(1);
}
if((dip = open(dip_d,O_RDWR)) < 0)
{
perror("open 3");
exit(1);
}
if((led = open(led_d,O_RDWR)) < 0)
{
perror("open 2");
exit(1);
}
while(1)
{
while(1)
{
read(tact,&c,sizeof(c));
if(c)
break;
}
read(dip,&t,sizeof(t));
switch(c)
{
case 1: if((t & 0x01) == 0x01)
{
data = 0xfe;
write(led,&data,sizeof(unsigned char));
}
break;
case 2: if((t & 0x02) == 0x02)
{
data = 0xfd;
write(led,&data,sizeof(unsigned char));
}
break;
case 3: if((t & 0x04) == 0x04)
{
data = 0xfb;
write(led,&data,sizeof(unsigned char));
}
break;
case 4: if((t & 0x08) == 0x08)
{
data = 0xf7;
write(led,&data,sizeof(unsigned char));
}
break;
case 5: if((t & 0x10) == 0x10)
{
data = 0xef;
write(led,&data,sizeof(unsigned char));
}
break;
case 6: if((t & 0x20) == 0x20)
{
data = 0xdf;
write(led,&data,sizeof(unsigned char));
}
break;
case 7: if((t & 0x40) == 0x40)
{
data = 0xbf;
write(led,&data,sizeof(unsigned char));
}
break;
case 8: if((t & 0x80) == 0x80)
{
data = 0x7f;
write(led,&data,sizeof(unsigned char));
}
break;
case 9: data = ~t;
write(led,&data,sizeof(unsigned char));
break;
case 10: ChaseAdd(led,tact,&chase);
break;
case 11: chase2 = ~(~chase & t);
write(led,&chase2,sizeof(unsigned char));
break;
case 12: exit(1);
}
sleep(1);
c=0;
}
close(tact);
close(dip);
close(led);
return 0;
}
void ChaseAdd(int led, int tact, unsigned char *chase)
{
sleep(1);
unsigned char d=0;
printf("input\n");
while(1)
{
while(1)
{
read(tact,&d,sizeof(d));
if(d)
break;
}
printf("%d\n",d);
switch(d)
{
case 1: *chase &= ~(0x01);
break;
case 2: *chase &= ~(0x02);
break;
case 3: *chase &= ~(0x04);
break;
case 4: *chase &= ~(0x08);
break;
case 5: *chase &= ~(0x10);
break;
case 6: *chase &= ~(0x20);
break;
case 7: *chase &= ~(0x40);
break;
case 8: *chase &= ~(0x80);
break;
case 10: return;
break;
case 12: exit(1);
}
sleep(1);
d=0;
}
printf("chase end\n");
}
1. 초기설정
sudo apt-get install gcc-4.4 g++-4.4 g++-4.4-multilib gcc-4.4-multilib lib32z1
2. 컴파일
/opt/gnueabi/opt/ext-toolchain/bin/arm-linux-gnueabihf-gcc led_[파일명.c] -o [실행파일명]
3. Test
- 윈도우(메인 OS)로 실행파일 복사
- Tera Term에서 포트 설정 후 설정 -> 시리얼 포트 -> 속도를 115200으로 설정
- Hybus-Smart4412 장비 부팅
- 메뉴 -> 전송 -> ZMODEM -> 보내기 에서 실행파일 선택
- chmod 777 [실행파일명] (실행파일 권한 설정)
- ./[실행파일명] (실행 후 테스트)
(★중요 : 만약 소스파일을 수정 했다면 Tera Term 에서 먼저 rm [실행파일명] 을 한 뒤 다시 ZMODEM -> 보내기 에서 실행파일 선택 후 chmod 777 [실행파일명] 와 ./[실행파일명] 다시 실행)
'Embedded > Kernel Porting' 카테고리의 다른 글
[Kernel Porting] FND에 소수점 표현하기 (0) | 2016.12.18 |
---|---|
[Kernel Proting] FND 점멸하기 (0) | 2016.12.18 |
[Kernel Porting] Tact 스위치를 이용한 사칙연산 (0) | 2016.12.18 |
[Kernel Porting] LED 점멸하기2 (1) | 2016.12.18 |
[Kernel Porting] LED 점멸하기1 (0) | 2016.12.18 |