■Thread




*스레드 생성 및 종료


//    pthread_test_q.c    //

#include <stdio.h>

#include <pthread.h>


int thread_args[3] = { 0, 1, 2 };                        /*스레드가 사용할 인자*/


void* Thread( void *arg )

{

    int i;

    for ( i=0; i<3; i++ )

        printf( "thread %d: %dth iteration\n", *(int*)arg, i );


//while(1); // TODO: hold here and try $ps -L


pthread_exit(0);

}


int main( void )

{

    int i, clock_get;

    pthread_t threads[3];         /*스레드 아이디 변수 */

    

    for ( i=0; i<3; i++ ) 

        // TODO: create a thread having Thread function 

        pthread_create( &threads[i],                

                        NULL,                      

                        ( void* (*)(void*) )Thread,

                        &thread_args[i] );

    

    while(1)sleep(1);

    

    //TODO: exit

    pthread_exit(0);

}


# cc pthread_test_q.c -lpthread

# ./a.out 


thread 2: 0th iteration

thread 2: 1th iteration

thread 2: 2th iteration

thread 1: 0th iteration

thread 1: 1th iteration

thread 1: 2th iteration

thread 0: 0th iteration

thread 0: 1th iteration

thread 0: 2th iteration

.....


thread 함수에 while(1); 
main에 while(1)sleep(1);

# ./a.out &
# ps -L

  PID   LWP TTY          TIME CMD
14150 14150 pts/0    00:00:00 a.out 
14150 14151 pts/0    00:00:01 a.out
14150 14152 pts/0    00:00:01 a.out
14150 14153 pts/0    00:00:01 a.out

thread 함수에 while(1); 
main에 while(1)sleep(1); 뺀 경우
# ./a.out &
# ps -L

  PID   LWP TTY          TIME CMD
14150 14150 pts/0    00:00:00 a.out <defunct>
14150 14151 pts/0    00:00:01 a.out
14150 14152 pts/0    00:00:01 a.out
14150 14153 pts/0    00:00:01 a.out

thread 함수에 while(1); 
main에 while(1)sleep(1);     pthread_exit(0);   뺀 경우


# ps -L

  PID   LWP TTY          TIME CMD

14061 14061 pts/0    00:00:00 bash

14439 14439 pts/0    00:00:00 ps


부모 프로세스가 끝나면 나머지 스레드들도 같이 끝난다.







*스레드 속성


//    pthread_attr_a.c    //

 

#include <stdio.h>

#include <pthread.h>

#include <sched.h>

#include <unistd.h>

#include <sys/types.h>


int thread_args[3] = { 0, 1, 2 };


void* Thread( void *arg )

{

    int i;

//    nice(10); // only for SCHED_NORMAL

for ( i=0; i<100; i++ ){

        printf( "%d", *(int*)arg);

    }


fflush(stdout); // try if you have not displayed 2s all enough !

//system("ps -l -L ");


sleep(100); //sleep for probe


    pthread_exit(0);  

}


int main( void ) {

    int i;

    pthread_t threads[3];

int policy;

  pid_t mypid;

    /*TODO: thread attribute variable here */

    pthread_attr_t  thread_attrs;


    /*TODO: thread schedule parameter here */

    struct sched_param  param;


mypid = getpid();


policy = sched_getscheduler(0); // get policy of self

printf("current policy is : %s \n", (policy==0) ? "SCHED_NORMAL" : (policy==1) ? "SCHED_FIFO" :(policy==2) ? "SCHED_RR" : "else" );

/*

#define SCHED_NORMAL            0

#define SCHED_FIFO              1

#define SCHED_RR                2

#define SCHED_BATCH             3

*/


#if 1

param.sched_priority = 30; /* 1 - 99 */

sched_setscheduler(mypid, SCHED_FIFO, &param);


policy = sched_getscheduler(0); // get policy of self

printf("current policy is : %s \n", (policy==0) ? "SCHED_NORMAL" : (policy==1) ? "SCHED_FIFO" :(policy==2) ? "SCHED_RR" : "else" );

#endif

    for ( i=0; i<3; i++ ) {

#if 1


/*

2 options of  : inherit( default: follow Mom's ) or explicit ( my own )

**/


/*TODO: initialize thread atrribute variable here */ 

        pthread_attr_init( &thread_attrs );


        /*TODO: set schedule policy : round-robin here */ 

        pthread_attr_setschedpolicy( &thread_attrs, SCHED_FIFO );  /* you can't change under inherit. just follow leader thread's **/


        /*TODO: set thread priority : 15 */   /* you can't change under inherit. just follow leader thread's **/

        param.sched_priority = 15;

        pthread_attr_setschedparam( &thread_attrs, &param );

#endif

        pthread_create( &threads[i], 

                        &thread_attrs, 

                        ( void* (*)(void*) )Thread, 

                        &thread_args[i] );


    }


    

system("ps -l -L ");

while(1)sleep(1);

    pthread_exit(0); 

}



<    user mode    >

$ cc pthread_attr_a.c -lpthread


$ ./a.out 

current policy is : SCHED_NORMAL 

current policy is : SCHED_NORMAL 

222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000F S   UID   PID  PPID   LWP  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD

0 S  1001  1430  1428  1430  0  80   0 -  1518 wait   pts/0    00:00:00 bash

0 S  1001  2151  1430  2151  0  80   0 -  6610 wait   pts/0    00:00:00 a.out

1 S  1001  2151  1430  2152  0  80   0 -  6610 hrtime pts/0    00:00:00 a.out

1 S  1001  2151  1430  2153  0  80   0 -  6610 hrtime pts/0    00:00:00 a.out

1 S  1001  2151  1430  2154  0  80   0 -  6610 hrtime pts/0    00:00:00 a.out

0 S  1001  2155  2151  2155  0  80   0 -   457 wait   pts/0    00:00:00 sh

0 R  1001  2156  2155  2156  0  80   0 -   625 -      pts/0    00:00:00 ps


 nice(10); 가 있으면


$ ./a.out 

current policy is : SCHED_NORMAL 

current policy is : SCHED_NORMAL                 ==>user mode라서 바뀌지 않는다.

222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

F S   UID   PID  PPID   LWP  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD

0 S  1001  1430  1428  1430  1  80   0 -  1518 wait   pts/0    00:00:00 bash

0 S  1001  1481  1430  1481  0  80   0 -  6610 wait   pts/0    00:00:00 a.out

1 S  1001  1481  1430  1482  0  90  10 -  6610 hrtime pts/0    00:00:00 a.out

1 S  1001  1481  1430  1483  0  90  10 -  6610 hrtime pts/0    00:00:00 a.out

1 S  1001  1481  1430  1484  0  90  10 -  6610 hrtime pts/0    00:00:00 a.out

0 S  1001  1485  1481  1485  0  80   0 -   457 wait   pts/0    00:00:00 sh

0 R  1001  1486  1485  1486  0  80   0 -   625 -      pts/0    00:00:00 ps


<    supervisor mode    >


# ./a.out 

current policy is : SCHED_NORMAL 

current policy is : SCHED_FIFO 

000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222

F S   UID   PID  PPID   LWP  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD

4 S     0  2157  1430  2157  3  80   0 -  1530 wait   pts/0    00:00:00 bash

4 S     0  2172  2157  2172  0  29   - -  6610 wait   pts/0    00:00:00 a.out

1 S     0  2172  2157  2173  0  29   - -  6610 hrtime pts/0    00:00:00 a.out

1 S     0  2172  2157  2174  0  29   - -  6610 hrtime pts/0    00:00:00 a.out

1 S     0  2172  2157  2175  0  29   - -  6610 hrtime pts/0    00:00:00 a.out

0 S     0  2176  2172  2176  0  29   - -   457 wait   pts/0    00:00:00 sh

4 R     0  2177  2176  2177  0  29   - -   625 -      pts/0    00:00:00 ps


















//    pthread_attr_a3.c    // 


/*

refer To:

https://www.systutorials.com/docs/linux/man/3-pthread_setschedparam/


*/


#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>


#define handle_error_en(en, msg) \

        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)


static void

usage(char *prog_name, char *msg)

{

    if (msg != NULL)

        fputs(msg, stderr);


    fprintf(stderr, "Usage: %s [options]\n", prog_name);

    fprintf(stderr, "Options are:\n");

#define fpe(msg) fprintf(stderr, "\t%s", msg);          /* Shorter */

    fpe("-a<policy><prio> Set scheduling policy and priority in\n");

    fpe("                 thread attributes object\n");

    fpe("                 <policy> can be\n");

    fpe("                     f  SCHED_FIFO\n");

    fpe("                     r  SCHED_RR\n");

    fpe("                     o  SCHED_OTHER\n");

    fpe("-A               Use default thread attributes object\n");

    fpe("-i {e|s}         Set inherit scheduler attribute to\n");

    fpe("                 'explicit' or 'inherit'\n");

    fpe("-m<policy><prio> Set scheduling policy and priority on\n");

    fpe("                 main thread before pthread_create() call\n");

    exit(EXIT_FAILURE);

}


static int

get_policy(char p, int *policy)

{

    switch (p) {

    case 'f': *policy = SCHED_FIFO;     return 1;

    case 'r': *policy = SCHED_RR;       return 1;

    case 'o': *policy = SCHED_OTHER;    return 1;

    default:  return 0;

    }

}


static void

display_sched_attr(int policy, struct sched_param *param)

{

    printf("    policy=%s, priority=%d\n",

            (policy == SCHED_FIFO)  ? "SCHED_FIFO" :

            (policy == SCHED_RR)    ? "SCHED_RR" :

            (policy == SCHED_OTHER) ? "SCHED_OTHER" :

            "???",

            param->sched_priority);

}


static void

display_thread_sched_attr(char *msg)

{

    int policy, s;

    struct sched_param param;


    s = pthread_getschedparam(pthread_self(), &policy, &param);

    if (s != 0)

        handle_error_en(s, "pthread_getschedparam");


    printf("%s\n", msg);

    display_sched_attr(policy, &param);

}


static void *

thread_start(void *arg)

{

    display_thread_sched_attr("Scheduler attributes of new thread");


    return NULL;

}


int

main(int argc, char *argv[])

{

    int s, opt, inheritsched, use_null_attrib, policy;

    pthread_t thread;

    pthread_attr_t attr;

    pthread_attr_t *attrp;

    char *attr_sched_str, *main_sched_str, *inheritsched_str;

    struct sched_param param;


    /* Process command-line options */


    use_null_attrib = 0;

    attr_sched_str = NULL;

    main_sched_str = NULL;

    inheritsched_str = NULL;


    while ((opt = getopt(argc, argv, "a:Ai:m:")) != -1) {

        switch (opt) {

        case 'a': attr_sched_str = optarg;      break;

        case 'A': use_null_attrib = 1;          break;

        case 'i': inheritsched_str = optarg;    break;

        case 'm': main_sched_str = optarg;      break;

        default:  usage(argv[0], "Unrecognized option\n");

        }

    }


    if (use_null_attrib &&

            (inheritsched_str != NULL || attr_sched_str != NULL))

        usage(argv[0], "Can't specify -A with -i or -a\n");


    /* Optionally set scheduling attributes of main thread,

       and display the attributes */


    if (main_sched_str != NULL) {

        if (!get_policy(main_sched_str[0], &policy))

            usage(argv[0], "Bad policy for main thread (-s)\n");

        param.sched_priority = strtol(&main_sched_str[1], NULL, 0);


        s = pthread_setschedparam(pthread_self(), policy, &param);

        if (s != 0)

            handle_error_en(s, "pthread_setschedparam");

    }

    display_thread_sched_attr("Scheduler settings of main thread");

    printf("\n");


    /* Initialize thread attributes object according to options */


    attrp = NULL;


    if (!use_null_attrib) {

        s = pthread_attr_init(&attr);

        if (s != 0)

            handle_error_en(s, "pthread_attr_init");

        attrp = &attr;

    }


    if (inheritsched_str != NULL) {

        if (inheritsched_str[0] == 'e')

            inheritsched = PTHREAD_EXPLICIT_SCHED;

        else if (inheritsched_str[0] == 'i')

            inheritsched = PTHREAD_INHERIT_SCHED;

        else

            usage(argv[0], "Value for -i must be 'e' or 'i'\n");


        s = pthread_attr_setinheritsched(&attr, inheritsched);

        if (s != 0)

            handle_error_en(s, "pthread_attr_setinheritsched");

    }


    if (attr_sched_str != NULL) {

        if (!get_policy(attr_sched_str[0], &policy))

            usage(argv[0],

                    "Bad policy for 'attr' (-a)\n");

        param.sched_priority = strtol(&attr_sched_str[1], NULL, 0);


        s = pthread_attr_setschedpolicy(&attr, policy);

        if (s != 0)

            handle_error_en(s, "pthread_attr_setschedpolicy");

        s = pthread_attr_setschedparam(&attr, &param);

        if (s != 0)

            handle_error_en(s, "pthread_attr_setschedparam");

    }


    /* If we initialized a thread attributes object, display

       the scheduling attributes that were set in the object */


    if (attrp != NULL) {

        s = pthread_attr_getschedparam(&attr, &param);

        if (s != 0)

            handle_error_en(s, "pthread_attr_getschedparam");

        s = pthread_attr_getschedpolicy(&attr, &policy);

        if (s != 0)

            handle_error_en(s, "pthread_attr_getschedpolicy");


        printf("Scheduler settings in 'attr'\n");

        display_sched_attr(policy, &param);


        s = pthread_attr_getinheritsched(&attr, &inheritsched);

        printf("    inheritsched is %s\n",

                (inheritsched == PTHREAD_INHERIT_SCHED)  ? "INHERIT" :

                (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :

                "???");

        printf("\n");

    }


    /* Create a thread that will display its scheduling attributes */


    s = pthread_create(&thread, attrp, &thread_start, NULL);

    if (s != 0)

        handle_error_en(s, "pthread_create");


    /* Destroy unneeded thread attributes object */


    s = pthread_attr_destroy(&attr);

    if (s != 0)

        handle_error_en(s, "pthread_attr_destroy");


    s = pthread_join(thread, NULL);

    if (s != 0)

        handle_error_en(s, "pthread_join");


    exit(EXIT_SUCCESS);

}





<    supervisor mode    >


# ./a.out -mf10 -ar20 -i e

Scheduler settings of main thread

    policy=SCHED_FIFO, priority=10


Scheduler settings in 'attr'

    policy=SCHED_RR, priority=20

    inheritsched is EXPLICIT


Scheduler attributes of new thread

    policy=SCHED_RR, priority=20



# ./a.out -mf10 -ar20 -i i

Scheduler settings of main thread

    policy=SCHED_FIFO, priority=10


Scheduler settings in 'attr'

    policy=SCHED_RR, priority=20

    inheritsched is INHERIT


Scheduler attributes of new thread

    policy=SCHED_FIFO, priority=10




*스레드 join


//    pthread_join_a.c    // 

#include <pthread.h>

#include <stdlib.h>

#include <unistd.h>

#include <stdio.h>


void *thread_function(void *arg) { 

     int i; 

     for ( i=0; i<10; i++ ) { 

         printf("Thread : says hi! [%d]\n",i ); 

         sleep(1); 

     } 

     return NULL; 

}


int main(void) {


    pthread_t mythread;


    if ( pthread_create( &mythread, NULL, thread_function, NULL) ) {

       printf("error creating thread."); 

       abort(); 

    }


    if ( pthread_join ( mythread, NULL ) ) { //TODO: join mythread 

         printf("error joining thread."); 

         abort(); 

    }


    printf("main: finally joined \n");


}



join : mythread로 시작된 쓰레드가 종료되는걸 기다린다.

# ./a.out 

Thread : says hi! [0]

Thread : says hi! [1]

Thread : says hi! [2]

Thread : says hi! [3]

Thread : says hi! [4]

Thread : says hi! [5]

Thread : says hi! [6]

Thread : says hi! [7]

Thread : says hi! [8]

Thread : says hi! [9]

main: finally joined 



■Multiplexed I/O


1. 프로세스는 하나 이상의 file descriptor가 준비될 때 까지 Block에 들어감.

2. File descriptor 중 하나가 요청한 입출력 준비가 되면 시스템이 알려준다.

3. Block되어 있던 프로세스가 깨어난 후

4. 프로세스는 입출력 준비된 file descriptor들에 대해 처리한다.


//    select_test.c    //

#include <sys/time.h> 

#include <sys/types.h> 

#include <unistd.h> 

#include <fcntl.h> 

#include <stdlib.h> 

#include <string.h> 

#include <stdio.h> 


int main()

{

    int fd[2];

    int i;

    int n;

    int state;


    char buf[255];


    struct timeval tv;


    fd_set readfds, writefds;


    if ((fd[0] = open("testfile0", O_RDONLY)) == -1)

    {

        perror("file open error : ");

        exit(0);

    }

    if ((fd[1] = open("testfile1", O_RDONLY)) == -1)

    {

        perror("file open error : ");

        exit(0);

    }


    memset (buf, 0x00, 255);


    for(;;)

    {

        FD_ZERO(&readfds);

        FD_SET(fd[0], &readfds);

        FD_SET(fd[1], &readfds);


        state = select(fd[1]+1, &readfds, NULL, NULL, NULL);

        switch(state)

        {

            case -1:

                perror("select error : ");

                exit(0);

                break;


            default :

                for (i = 0; i < 2; i++)

                {

                    if (FD_ISSET(fd[i], &readfds))

                    {

                        while ((n = read(fd[i], buf, 255)) > 0)

                            printf("(%d) [%d] %s", state, i, buf);

                    }

                }

                memset (buf, 0x00, 255);

                break;

        }

        usleep(1000);

    }

}


# touch testfile0 testfile1
# cc seletc_test.c
# ./a.out &
[4] 11443
# echo hello >> testfile0
(2) [0] hello
echo word >> testfile1
(2) [1] word
echo good >> testfile0

# cat testfile0

hello

good

# cat testfile1

word



*입출력 리다이렉션
//    std.c    //

#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<string.h>

#define STDIN 0
#define STDOUT 1
#define STDERR 2

#define ERRMSG "Devide Not Zero\n"

int main()
{
    int a;
    int b;
    char buf[80];

    read(STDIN, buf, 80);
    a = atoi(buf);

    read(STDIN, buf, 80);
    b = atoi(buf);
    if(b == 0)
    {
        write(STDERR, ERRMSG, strlen(ERRMSG));
        return 1;
    }

    sprintf(buf, "%d / %d = %d\n", a, b, (int)(a/b));

    write(STDOUT, buf, strlen(buf));
    return 0;
}

# cc std.c

# ./a.out


# ./a.out 

4

8

4 / 8 = 0


# ./a.out 

6

0

Devide Not Zero


# ./a.out 2>stderr.txt            -->2 : STDERR를 리다이렉션으로 stderr.txt에 저장
6
0
# cat stderr.txt 
Devide Not Zero

# ./a.out  1>stderr.txt
1
2
# cat stderr.txt 
1 / 2 = 0







■프로세스


//    fork.c    //
#include <stdio.h>
#include <stdlib.h> // for system()
#include <unistd.h> // for execl(), fork()
#include <wait.h>       // for wait()

int main(int argc, char *argv[])
{
    int pid;
    /* fork another process */
    pid = fork();
    if(pid < 0) {                       /* error occurred */
        fprintf(stderr,"Fork Failed");
        exit(-1);
    } else if (pid== 0) {       /* child process */
               // execl("/bin/ls", "ls", NULL);                ==>Child here! 없다
                //execl("/bin/ps", "ps", "aux", NULL);      ==>Child here! 없다
                //system("/bin/ps aux");                      ==>Child here! 없다있다
                printf("Child here!\n");
        //      for(;;) 
                sleep(1);
    } else {          /* parent process */
        wait(NULL);
        printf("Child Complete!\n");
        exit(0);
    }
}

# ./a.out 
Child here!
Child Complete!


 // execl("/bin/ls", "ls", NULL); 

# ./a.out 

a.out  execl.c fork.c orphan.c  prio.c  sched_param.c  wait.c  zombie.c

Child Complete!




*고아 프로세스 : 부모 프로세스가 먼저 종료된 경우 자동적으로 init의 상속이된다.


//    orphan.c    //

#include <sys/types.h>

#include <wait.h>

#include <stdio.h>


int main()

{

pid_t pid;

printf("I'm the original process : pid = %d, ppid = %d\n", 

getpid(), getppid());


pid =  fork();

if ( pid != 0 )  { /* parent process */

printf("I'm parent process : pid = %d, ppid = %d\n", 

getpid(), getppid());

printf("my child process : pid = %d\n", pid);

}

else { /* child process */

sleep(5);

printf("I'm child process : pid = %d, ppid = %d\n", 

getpid(), getppid());

}

/* parent & child  process */

printf("pid %d terminates.. \n", getpid());  

}



$ ./a.out 

I'm the original process : pid = 12382, ppid = 1623

I'm parent process : pid = 12382, ppid = 1623

my child process : pid = 12383

pid 12382 terminates.. 

$ ps -l

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD

0 S  1001  1623  1621  0  80   0 -  1518 wait   pts/0    00:00:00 bash

1 S  1001 12383     1  0  80   0 -   404 hrtime pts/0    00:00:00 a.out

0 R  1001 12386  1623  0  80   0 -   625 -      pts/0    00:00:00 ps







* 좀비 프로세스 : 실행이 종료되어 부모 프로세스가 반환 코드를 받을 때까지 프로세스 테이블 엔트리에 정보가 남아 있는 프로세스


//    Zombi.c    //

#include <sys/types.h>

#include <wait.h>

#include <stdlib.h>


int main()

{

   pid_t pid;

      

   pid = fork();


   if ( pid != 0 )  { /* parent process */

while(1) sleep(1000);

   }

   else { /* child  process */

exit(42);

   }


}



$ ./a.out &

[1] 12473

$ ps

  PID TTY          TIME CMD

 1623 pts/0    00:00:00 bash

12473 pts/0    00:00:00 a.out

12474 pts/0    00:00:00 a.out <defunct>

12475 pts/0    00:00:00 ps


$ kill 12473

$ ps

  PID TTY          TIME CMD

 1623 pts/0    00:00:00 bash

12478 pts/0    00:00:00 ps

[1]+  Terminated              ./a.out




*프로세스 상태 


//    prio.c    //


#include <stdio.h>

#include <sys/types.h>

#include <sys/resource.h>

#include <unistd.h>


int main(void)

{

//get process id of this process

pid_t current_pid = getpid();


//get priority   of this process

int priority = getpriority(PRIO_PROCESS, current_pid);        //nice값

printf("Process: %d, Priority: %d\n", current_pid, priority);

sleep(1);


/*

Try cases below... 


case 1: $ ./a.out &

$ ps -l

case 2: # ./a.out &

# ps -l

case 3: $ nice  -3 ./a.out &

$ ps -l

case 4: # nice --3 ./a.out &

# ps -l

/*결론 :  -3값은 sudo 권한만 가능 */

*/

/*  nice by +5 */

setpriority(PRIO_PROCESS, current_pid, 5);

priority = getpriority(PRIO_PROCESS, current_pid);

printf("Process: %d, Priority: %d\n", current_pid, priority);


system("ps -l");


sleep(1);


nice(-10); /* only superuser may lower priority */

priority = getpriority(PRIO_PROCESS, current_pid);

printf("Process: %d, Priority: %d\n", current_pid, priority);


system("ps -l");


return 0;

}



$ whoami

mds

$ cc prio.c 

$ ./a.out 


Process: 12723, Priority: 0

Process: 12723, Priority: 5

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD

0 S  1001  1623  1621  0  80   0 -  1519 wait   pts/0    00:00:00 bash

0 S  1001 12723  1623  0  85   5 -   404 wait   pts/0    00:00:00 a.out

0 S  1001 12724 12723  0  85   5 -   457 wait   pts/0    00:00:00 sh

0 R  1001 12725 12724  0  85   5 -   625 -      pts/0    00:00:00 ps

Process: 12723, Priority: 5

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD

0 S  1001  1623  1621  0  80   0 -  1519 wait   pts/0    00:00:00 bash

0 S  1001 12723  1623  0  85   5 -   404 wait   pts/0    00:00:00 a.out

0 S  1001 12726 12723  0  85   5 -   457 wait   pts/0    00:00:00 sh

0 R  1001 12727 12726  0  85   5 -   625 -      pts/0    00:00:00 ps


$ nice --3 ./a.out 
nice: cannot set niceness: Permission denied


# whoami

root

# cc prio.c 

# ./a.out 


Process: 12801, Priority: 0

Process: 12801, Priority: 5

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD

4 S     0 12769  1623  2  80   0 -  1533 wait   pts/0    00:00:00 bash

0 S     0 12801 12769  0  85   5 -   404 wait   pts/0    00:00:00 a.out

0 S     0 12802 12801  0  85   5 -   457 wait   pts/0    00:00:00 sh

4 R     0 12803 12802  0  85   5 -   625 -      pts/0    00:00:00 ps

Process: 12801, Priority: -5

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD

4 S     0 12769  1623  1  80   0 -  1533 wait   pts/0    00:00:00 bash

4 S     0 12801 12769  0  75  -5 -   404 wait   pts/0    00:00:00 a.out

0 S     0 12804 12801  0  75  -5 -   457 wait   pts/0    00:00:00 sh

4 R     0 12805 12804  0  75  -5 -   625 -      pts/0    00:00:00 ps







//    sched_param.c    //

#include <stdio.h>

#include <sys/types.h>

#include <sched.h>

#include <stdlib.h>

#include <unistd.h>


int main(void)

{

struct sched_param param;

int policy;

char *policy_name;


pid_t current_pid = getpid();

/*

only superuser may lower the priorities and change schedule

*/

sched_getparam(current_pid, &param);

param.sched_priority = 65;

sched_setscheduler(current_pid, SCHED_FIFO, &param);


sched_getparam(current_pid, &param);

policy = sched_getscheduler(current_pid);


if      (policy == 0) policy_name = "SCHED_OTHER";

else if (policy == 1) policy_name = "SCHED_FIFO";

else if (policy == 2) policy_name = "SCHED_RR";

else policy_name = "Unknown";

printf("Current PID: %d\n", (int)current_pid);

printf("Scheduling Policy: %d, %s\n", policy, policy_name );

printf("RT Sched Priority: %d  \n\n", param.sched_priority);


puts("#ps -l to check the priority");

sleep(100);


return 0;

}



# ./a.out &

[1] 12945

Current PID: 12945

Scheduling Policy: 1, SCHED_FIFO

RT Sched Priority: 65  


#ps -l to check the priority


# ps -l

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD

4 S     0 12893  1623  0  80   0 -  1535 wait   pts/0    00:00:00 bash

4 S     0 12945 12893  0  -6   - -   404 hrtime pts/0    00:00:00 a.out

4 R     0 12946 12893  0  80   0 -   625 -      pts/0    00:00:00 ps




■Makefile



*makefile 은 Rule로 이루어져 있다.


target : dependencies

<tab>command

target : command 부분이 실행되어 나온 결과 파일

dependencies : target을 만들기 위해 사용되는 파일

command : command의 시작은 반드시 tab후 명령어 사용


*기본 예제


//    bar.c    //

#include <stdio.h>


void bar(void)

{

printf("Good bye, my love \n");

}


//    foo.c    //

extern void bar(void);

int main(void)

{

bar();

return 0;

}

//    makefile    //

baz:foo.o      bar.o

gcc -o baz foo.o bar.o


foo.o:foo.c

gcc -c foo.c


bar.o:     bar.c

gcc -c bar.c



clean:

rm -f baz foo.o bar.o

# ./baz 

Good bye, my love 



*매크로와 라벨


# ls

func1.c  func2.c  io.h  main.c  makefile



//    func1.c    //

#include <stdio.h>


void func1()

{

printf("this is function1 program\n");

}


//    func2.c    //

#include <stdio.h>


void func2()

{

printf("this is function2 program\n");

}


//    main.c    //

#include <stdio.h>
#include "io.h"

int main()
{
printf("this is main program\n");
func1();
func2();
return 0;
}

//    makefile    //           

OBJECTS= main.o func1.o func2.o

TARGET= makeprog


$(TARGET): $(OBJECTS)

gcc -o $(TARGET) $(OBJECTS)


main.o: main.c

gcc -c main.c


func1.o: func1.c

gcc -c func1.c


func2.o: func2.c

gcc -c func2.c


clean:

rm -f $(TARGET) $(OBJECTS)


/*

매크로를 사용할때는 $() 사용

미리 정의된 매크로도 있다. 예)CC = gcc




OBJECTS= main.o func1.o func2.o

TARGET= makeprog

CC= gcc                                     //(CC : Macro)

CFLAGS= -c                                //c파일을 컴파일(CFLAGS : flag)

makeprog: $(OBJECTS)

$(CC) -o $(TARGET) $(OBJECTS)


main.o: main.c

$(CC) $(CFLAGS) main.c


func1.o: func1.c

$(CC) $(CFLAGS) func1.c


func2.o: func2.c

$(CC) $(CFLAGS) func2.c


clean:

rm -f $(TARGET) $(OBJECTS)


*/


*확장자 사용

.SUFFIXES = .c .o                            =>make가 어느확장자를 가진 파일들을 처리할 것인가

OBJECTS= main.o func1.o func2.o

SRCS= main.c func1.c func2.c

TARGET= makeprog

CC= gcc

CFLAGS= -g -c


$(TARGET):      $(OBJECTS)

        $(CC) -o $(TARGET) $(OBJECTS)


.c.o :

        $(CC) $(CFLAGS) $<                    =>확장자 규칙을 직접 구현


clean:

        rm -f $(TARGET) $(OBJECTS) core


main.o: io.h main.c

func1.o: io.h func1.c

func2.o: io.h func2.c



/*
직접구현이 안되어있을 때는 
밑에와 같이 자동으로 해준다.
.c.o : 
$(CC) $(CFLAGS) -c $< -o $@
*/

/*
$@ : target
$* : 확장자가 없는 target
$< : dependency 중 최근에 갱신된 파일
*/


■정적, 공유, 동적 라이브러리



라이브러리 : 자주 사용되는 기능들을 모듈 형태로 만들어 프로그램 유지보수 및 디버깅을 쉽게 할 수 있다.

  링크 단계에서 연결되거나 실행중 연결되기도 한다.




1. 정적 라이브러리 : 링크시에 포함되며 사이즈가 커지는 문제점이 발생


// mylib.h //

int sum(int a , int b);

int diff(int a, int b);


// mylib.c //

#include "mylib.h"

int sum(int a , int b)

{

return a+b;

}


int diff (int a, int b)

{

return a-b;

}


// main.c // 

#include "mylib.h"

#include <stdio.h>


int main()

{

int a, b;

a=100;

b=50;


printf("sum; %d\n", sum(a, b));

printf("diff: %d\n", diff(a,b));

return 0;

}


#gcc -c mylib.c                              =>mylib.o 오브젝트 파일 생성

#ar rc libmylib.a mylib.o                  =>아카이브 파일 생성
/*
r: replacement
c: create
*/
#gcc -o main main.c -L./ -lmylib       => -L은 라이브러리가 있는 경로, -l은 libmylib.a를 지정, 확장자 없이 라이브러리이름 넣어줍니다.

#./main                    




2. 공유 라이브러리

# gcc -fPIC -c ./lib/myfunc.c                                                             
/*
-fPIC : Position Independent Code
*/
# gcc -shared -W1,-soname,libmylib.so.1 -o libmylib.so.1.0.1 myfunc.o 
/*
-shared : 공유 라이브러리를 사용해서 링크
-W1 : warning level 1 에러시
-soname : 실제 만들 라이브러리인 libmylib.so.1.0.1 에 libmylib.so.1이라는 soname을 생성하여 나중에 
동적링크가 공유 라이브러리를 찾을때 soname인 libmylib.so.1를 찾아 링크 하도록 한다.
버전 관리를 융통성 있게 하기위함이고 gcc가 링크하는 파일명은 버전 정보가 없는 libmylib.so로 링크 파일 생성
*/
# cp libmylib.so.1.0.1 /usr/local/lib
# ln -s /usr/local/lib/libmylib.so.1.0.1 /usr/local/lib/libmylib.so
# gcc -o hello hello.c -L/usr/local/lib -lmylib
/*
libmylib.so를 찾으려면    -l옵션은 lib 를 기본적어로 붙여 찾는다. 그래서 -lmylib
*/
# export LD_LIBRARY_PATH = $LD_LIBRARY_PATH:/usr/local/lib
# ./hello


3. 동적 라이브러리
// dylib.c //
#include<stdio.h>
#include<dlfcn.h>

int main()
{
        int a=100,b=50;
        void *handle;
        char *error;
        int (*result)(int,int);

        handle = dlopen("./lib/libmylib.so.2",RTLD_NOW);

        if(!handle)
        {
                printf("open error\n");
                return 1;
        }

        result = dlsym(handle, "sum");
        if((error = dlerror()) !=NULL)
        {
                fputs(error,stderr);
                return 1;
        }
        else
        {
                printf("sum : %d\n",result(a,b));
        }
        dlclose(handle);
        return 0;
}


# gcc -o dylib dylib.c -ldl


■파일

*파일 열기
// open.c //

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
int fd;
fd = open("hello.txt", O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
}

# cc open.c 
# ./a.out 
# ls -al hello.txt 
-rw-r----- 1 root root 0 2018-12-11 14:45 hello.txt
/*
파일이름 : hello.txt
파일이 없을 경우 생성
권한 640
*/



*파일 읽기

// fly.txt //
Fly me to the moon And let me play among the stars
Let me see what spring is like on Jupiter and Mars
In other words hold my hand
In other words darling kiss me
on Jupiter and Mars
In other words hold my hand

// read.c //
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

#define MAXLEN 80
int main()
{
int fd;
int readn  =0;
char buf[MAXLEN];
fd = open("fly.txt", O_RDONLY);
if(fd <0)
{
perror("file open error:");
return 1;
}
memset(buf, 0x00, MAXLEN);
while((readn=read(fd, buf, MAXLEN-1))>0)
{
printf("%s", buf);
    memset(buf, 0x00, MAXLEN);
}
}

# cc read.c 
# ./a.out 


*파일 쓰기

#include <unistd.h>
#include<stdio.h>
#include <fcntl.h>
#include <string.h>

int main()
{
        int fd;
        int i;
        int wdata=0;
        int wsize=0;
        fd = open("data.txt", O_CREAT|O_WRONLY);
        if(fd<0)
        {
                perror("file open error");
                return -1;
        }
        for(i=0; i<100; i++)
        {
                wdata=i*2;
                wsize=write(fd, (void*)&wdata, sizeof(int));
                printf("Write %d(%d byte)\n", wdata, wsize);
        }
        close(fd);
}



*구조체 데이터 파일 읽기, 쓰기


// addr_make.c //

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>

struct userInfo
{
  char name[28];
  int age;
  int sex;
  char hobby[28];
};

void makeUserInfo(struct userInfo *uinfo,
  char *name, // 이름
  int age,    // 나이
  int sex,    // 성 (남: 0, 여: 1)
  char *hobby) // 취미
{
  memset((void *)uinfo, 0x00, sizeof(struct userInfo));
  strcpy(uinfo->name, name);
  uinfo->age = age;
  uinfo->sex = sex;
  strcpy(uinfo->hobby, hobby);
}

int main()
{
  int fd;
  struct userInfo myAddrBook;
  fd = open("hello.txt", O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
  if (fd < 0)
  {
perror("file open error");
return 1;
  }

  makeUserInfo((void *)&myAddrBook, "user1", 19, 0, "programming");
  write(fd, (void *)&myAddrBook, sizeof(myAddrBook));

  makeUserInfo((void *)&myAddrBook, "user2", 22, 1, "playing games");
  write(fd, (void *)&myAddrBook, sizeof(myAddrBook));

  makeUserInfo((void *)&myAddrBook, "user3", 33, 1, "playing soccer");
  write(fd, (void *)&myAddrBook, sizeof(myAddrBook));

  close(fd);
  return 0;
}

# cc addr_make.c

// addr_read.c //
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>

struct userInfo
{
char name[28];
int age;
int sex;
char hobby[28];
};

int main()
{
int fd;
struct userInfo myAddrBook;
int dataSize;
fd = open("hello.txt", O_RDONLY);
if(fd<0)
{
perror("file open error");
return 1;
}
dataSize = sizeof(myAddrBook);
printf("User Info=================================\n\n");
while(read(fd, (void*)&myAddrBook, dataSize) == dataSize)
{
printf("name : %s\n", myAddrBook.name);
printf("name : %d\n", myAddrBook.age);
printf("name : %d\n", myAddrBook.sex);
printf("name : %s\n", myAddrBook.hobby);
printf("======================================\n");
}
close(fd);
return 0;
}
# cc addr_read.c 
# ./a.out 
User Info=================================

name : user1
name : 19
name : 0
name : programming
======================================
name : user2
name : 22
name : 1
name : playing games
======================================
name : user3
name : 33
name : 1
name : playing soccer
======================================



*파일 탐색

off_t lseek(int fd, off_t pos, int origin);


origin 값 : SEEK_CUR : pos가 0 -> 현재위치값 else -> 현 위치값 + pos

   SEEK_END : pos가 0 -> 파일의 끝지점 else-> 끝 위치값 + pos

   SEEK_SET : post 가 0 -> 파일의 시작지점 else -> 시작 위치값 + pos


// lseek.c //

int main(int argc, char **argv)

{

int fd;

int i, buf;

fd = open("num.dat", O_RDWR|O_CREAT);

if (fd < 0)

{

perror("error : ");

exit(0);

}

for (i = 1000; i < 1010; i++)

write(fd, (void *)&i, sizeof(i));


lseek(fd, 0, SEEK_SET);

read(fd, (void *)&buf, sizeof(i));

printf("%d\n", buf);


lseek(fd, 4*sizeof(i), SEEK_SET);

read(fd, (void *)&buf, sizeof(i));

printf("%d\n", buf);


}


# cc lseek.c 

# ./a.out 

1000

1004


■Stairs_of_infinty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include<time.h>
#include<stdlib.h>
#include "2450addr.h"
#include "my_lib.h"
#include "macro.h"
void MMU_Init(void);
 
#include "./Images/BLOCK.H"
#include "./Images/CLOUD.H"
#include "./Images/TITLE1.H"
#include "./Images/TITLE2.H"
#include "./Images/ICON.H"
#include "./Images/ENDGAME.H"
 
#include "./Images/RPENGUIN.H"
#include "./Images/LPENGUIN.H"
#include "./Images/RHEDGE.H"
#include "./Images/LHEDGE.H"
#include "./Images/RDINO.H"
#include "./Images/LDINO.H"
 
// Color Define
#define BLACK    0x0000
#define WHITE        0xfffe
#define BLUE        0x003e
#define GREEN    0x07c0
#define RED        0xf800
#define YELLOW    0xffc0
 
// ISR Functions declaration
 
#define LCD_XSIZE         (480)    
#define LCD_YSIZE         (272)
 
// Functions Declaration
 
void HW_Initial(void);
void Show_Welcome(char * msg);
 
// Global Variables Declaration
 
volatile int Touch_pressed = 0;
volatile int ADC_x=0, ADC_y=0;
 
 
volatile int Cal_x1=848;
volatile int Cal_y1=656;
volatile int Cal_x2=186;
volatile int Cal_y2=349
 
 
volatile int Touch_x, Touch_y;
 
volatile unsigned int Touch_config=1;
/**************************************************************************************************/
typedef struct _Block{
    int x;
    int y;
}Block;
 
void Lcd_Make_Bmp_Buffer (int x, int y, const unsigned char *fp);
void Make_Buffer(int x,int y,int color);
 
void Creat_Block(void);
void Print_Block(void);
void touch_block(void);
void CountPrintf(void);
void ScorePrintf(void);
 
unsigned int bmp_buffer[272][480]; 
static unsigned int Fbuf[2= {0x338000000x33c00000};
 
unsigned short bfType;
unsigned int bfSize;
unsigned int bfOffbits;
unsigned int biWidth, biWidth2;
unsigned int biHeight;
/**************************************************************************************************/
void Touch_ISR(void) __attribute__ ((interrupt ("IRQ")));
 
void Touch_ISR()
{
    rINTSUBMSK |= (0x1<<9);
    rINTMSK1 |= (0x1<<31);    
    
    /* TO DO: Pendng Clear on Touch */    
    rSUBSRCPND |= (0x1<<9);
    rSRCPND1 |= (0x1<<31);
    rINTPND1 |= (0x1<<31);
    
    // Touch UP
    if(rADCTSC&0x100)
    {
        rADCTSC&=0xff;
        Touch_pressed = 0;
    }
    // Touch Down
    else 
    {
        rADCTSC=(0<<8)|(1<<7)|(1<<6)|(0<<5)|(1<<4)|(1<<3)|(1<<2)|(0);
        // SADC_ylus Down,Don't care,Don't care,Don't care,Don't care,XP pullup Dis,Auto,No operation
        rADCCON|=0x1;
        while(rADCCON & 0x1);
        while(!(0x8000&rADCCON));
        ADC_x=(int)(0x3ff&rADCDAT0);
        ADC_y=(int)(0x3ff&rADCDAT1);
        // Touch calibration complete
        if(Touch_config)
        {
            Touch_y=(ADC_y-Cal_y1)*(LCD_YSIZE-10)/(Cal_y2-Cal_y1)+5;
            Touch_x=(ADC_x-Cal_x2)*(LCD_XSIZE-10)/(Cal_x1-Cal_x2)+5;
            Touch_x=LCD_XSIZE-Touch_x;
            if(Touch_x<0) Touch_x=0;
            if(Touch_x>=LCD_XSIZE) Touch_x=LCD_XSIZE-1;
            if(Touch_y<0) Touch_y=0;
            if(Touch_y>=LCD_YSIZE) Touch_y=LCD_YSIZE-1;
        }
        // before calibration        
        else
        {
            Touch_x = ADC_x;
            Touch_y = ADC_y;
        }
 
        rADCTSC=(1<<8)|(1<<7)|(1<<6)|(0<<5)|(1<<4)|(0<<3)|(0<<2)|(3);
        // SADC_ylus Up,Don't care,Don't care,Don't care,Don't care,XP pullup En,Normal,Waiting mode
        Touch_pressed = 1
    }
 
    rINTSUBMSK &= ~(0x1<<9);
    rINTMSK1 &= ~(0x1<<31);
}
// Lower Layer Functions
 
void HW_Initial(void)
{
    Exception_Init();
    Uart_Init(115200);    
//    Led_Init();
    Buzzer_Init();
 
    Timer0_Init();
    Graphic_Init();
    
    Touch_Isr_Init(Touch_ISR);
    MMU_Init();
}
/***********************************************************************************************/
void Show_Welcome(char * msg)
{
    Uart_Printf("\n%s\n", msg);
    Lcd_Draw_BMP(0,0, title1);        //Flushing
    Timer0_Delay(3000);            
    Lcd_Draw_BMP(0,0, title2);        
    Timer0_Delay(3000);            
}
 
 
/********** Background Save **************************************************************************/
void Make_Buffer(int x,int y,int color)
{
    bmp_buffer[y][x] = (unsigned short int)color;    
}
void Lcd_Make_Bmp_Buffer (int x, int y, const unsigned char *fp)
{
     int xx=0, yy=0;    
     unsigned int tmp;
     unsigned char tmpR, tmpG, tmpB;
    
 
     bfType=*(unsigned short *)(fp+0);
     bfSize=*(unsigned short *)(fp+2);
     tmp=*(unsigned short *)(fp+4);
     bfSize=(tmp<<16)+bfSize;
     bfOffbits=*(unsigned short *)(fp+10);
     biWidth=*(unsigned short *)(fp+18);    
     biHeight=*(unsigned short *)(fp+22);    
 
     biWidth2=(bfSize-bfOffbits)/biHeight;    
 
     for(yy=0;yy<biHeight;yy++)
     {
         for(xx=0;xx<biWidth;xx++)
         {
             tmpB=*(unsigned char *)(fp+bfOffbits+(biHeight-yy-1)*biWidth*3+xx*3+0);
             tmpG=*(unsigned char *)(fp+bfOffbits+(biHeight-yy-1)*biWidth*3+xx*3+1);
             tmpR=*(unsigned char *)(fp+bfOffbits+(biHeight-yy-1)*biWidth*3+xx*3+2);
             tmpR>>=3
             tmpG>>=3;
             tmpB>>=3;
             
             if(xx<biWidth2) Make_Buffer(x+xx,y+yy,(tmpR<<10)+(tmpG<<5)+(tmpB<<0));
         } 
     }
}
/*************************************************************************************************/
void Lcd_Display_Frame_Buffer(unsigned int id)
{
    Lcd_Set_Address(Fbuf[id]);
}
 
void Lcd_Set_Address(unsigned int fp)
{
    rVIDW00ADD0B0 = fp;
    rVIDW00ADD1B0 = 0;
    rVIDW00ADD2B0 = (0<<13)|((LCD_XSIZE*4*2)&0x1fff);
 
    rVIDW00ADD1B0 = 0+(LCD_XSIZE*LCD_YSIZE);
}
/*************************************************************************************************/
int random;
int icon_flag=0;
int game_enable =1;
int Survival_count=20;//Survival time
int character_pose = 0;//0 : Right, 1 : Left
 
int size = 15;        //init block count
Block block_ary[15];
int blockS_x = 390, blockS_y = 120;    //start point
int block_cnt = 0;
 
 
void Creat_Block(void)    //height : 28 , width : 48 ; block
{
 
    int i;
    int position_x,position_y;
 
    block_ary[0].x=blockS_x;
    block_ary[0].y=blockS_y;
 
    position_x = blockS_x;
    position_y = blockS_y;
 
    for(i=1;i<size;i++)
    {
    random =rand()%5;
 
    if(random>2)
        {
            block_ary[i].x = position_x-28;
            block_ary[i].y = position_y-48;
            position_x = block_ary[i].x;
            position_y = block_ary[i].y;
        }
    else
        {
            block_ary[i].x = position_x-28;
            block_ary[i].y = position_y+48;
            position_x = block_ary[i].x;
            position_y = block_ary[i].y;
        }
    }
}
 
 
 
void Print_Block(void)
{
    int i;
 
    Lcd_Select_Frame_Buffer(0);
 
    for(i=0;i<size;i++)
    Lcd_Draw_BMP(block_ary[i].x,block_ary[i].y, block);    
 
    Lcd_Draw_BMP(432,0, icon);        
 
    if(icon_flag==0){
    if(character_pose== 0)
    Lcd_Draw_BMP(362,130, Rpenguin);
    else
    Lcd_Draw_BMP(362,130, Lpenguin);
    }
 
    if(icon_flag==1){
    if(character_pose== 0)
    Lcd_Draw_BMP(362,130, Rhedge);    
    else
    Lcd_Draw_BMP(362,130, Lhedge);    
    }
 
    if(icon_flag==2){
    if(character_pose== 0)
    Lcd_Draw_BMP(362,130, Rdino);    
    else
    Lcd_Draw_BMP(362,130, Ldino);    
    }
}
 
void touch_block(void)
{
    int i;
    int x,y;
 
    
    if(!game_enable && Touch_pressed)        //game end
    {
        block_cnt = 0;
        Survival_count=20;
 
 
        for(i=0;i<size;i++)
            for(x=block_ary[i].x;x<block_ary[i].x+28;x++)
                for(y=block_ary[i].y ;y<block_ary[i].y+48 ;y++)
                    Lcd_Put_Pixel(x,y,bmp_buffer[y][x]);
 
        Creat_Block();
        /*clear end bar, score bar*******************/
 
            for(x=150;x<150 +32;x++)
                for(y=0 ;y<272;y++)
                    Lcd_Put_Pixel(x,y,bmp_buffer[y][x]);
 
            for(x=450;x<450 +32;x++)
                for(y=0 ;y<272 + 10;y++)
                    Lcd_Put_Pixel(x,y,bmp_buffer[y][x]);
 
 
        /*************************************/
        game_enable =1;    //game restart
 
    }
 
    else if(game_enable&&Touch_pressed)    //game start
    {
        Uart_Printf("\nX:%d, Y:%d", Touch_x, Touch_y); 
 
        Jumpsound();    //buzzer
 
        /*difficulty control*************************/
        if(block_cnt <10)
        Survival_count=20;
        else
        Survival_count=14;    //2sec..
        /*************************************/
        if(Touch_x>200 &&Touch_x<450&& Touch_y>140){
 
        block_cnt++;
 
        /*clear block image from background*******************/
        for(i=0;i<size;i++)
            for(x=block_ary[i].x;x<block_ary[i].x+28;x++)
                for(y=block_ary[i].y ;y<block_ary[i].y+48 ;y++)
                    Lcd_Put_Pixel(x,y,bmp_buffer[y][x]);
        /************************************************/
 
            Uart_Printf("<<<<<<<<Left Click ... <<<<<<<<<<<<\n"); 
            for(i=0;i<size;i++)
            {
                block_ary[i].x+=28;
                block_ary[i].y-=48;
            }
 
            /*first block delete and create end block*******************/
            for(i=0;i<size-1;i++)
            {
                block_ary[i].x = block_ary[i+1].x;
                block_ary[i].y = block_ary[i+1].y;
            }
                random =rand()%5;
 
                if(random>2)
                {
                    block_ary[size-1].x = block_ary[size-2].x - 28;
                    block_ary[size-1].y = block_ary[size-2].y - 48;
                }
                else
                {
                    block_ary[size-1].x = block_ary[size-2].x - 28;
                    block_ary[size-1].y = block_ary[size-2].y + 48;
                }
        
 
            /*get off the block ***********************************/
            if(blockS_x != block_ary[0].x || blockS_y != block_ary[0].y){
                    Lcd_Draw_BMP(150,0, endgame);
                    game_enable=0;
                }
            /************************************************/
            character_pose = 1;
 
        }
        else if(Touch_x>200 &&Touch_x<450&& Touch_y<140){
 
        block_cnt++;
 
        for(i=0;i<size;i++)
            for(x=block_ary[i].x;x<block_ary[i].x+28;x++)
                for(y=block_ary[i].y ;y<block_ary[i].y+48 ;y++)
                    Lcd_Put_Pixel(x,y,bmp_buffer[y][x]);
        
            Uart_Printf(">>>>>>>>>Right Click ... >>>>>>>>>>>>>\n"); 
            
            for(i=0;i<size;i++)
            {
                block_ary[i].x+=28;
                block_ary[i].y+=48;
            }
            /*first block delete and create end block*******************/
            for(i=0;i<size-1;i++)
            {
                block_ary[i].x = block_ary[i+1].x;
                block_ary[i].y = block_ary[i+1].y;
            }
                random =rand()%5;
 
                if(random<3)
                {
                    block_ary[size-1].x = block_ary[size-2].x - 28;
                    block_ary[size-1].y = block_ary[size-2].y - 48;
                }
                else
                {
                    block_ary[size-1].x = block_ary[size-2].x - 28;
                    block_ary[size-1].y = block_ary[size-2].y + 48;
                }
            /*get off the block ***********************************/
            if(blockS_x != block_ary[0].x || blockS_y != block_ary[0].y){
                    Lcd_Draw_BMP(150,0, endgame);
                    game_enable=0;
                }
            /************************************************/
            character_pose= 0;
 
        }
        /*character change icon ***********************************/
        else if(Touch_x>430 &&Touch_x<480&& Touch_y<100)
        {
            icon_flag++;
            icon_flag = (icon_flag ==3) ? (icon_flag%3) : icon_flag;
        }
    }
}
 
void ScorePrintf()
{
    Lcd_Printf(170,450, RED, BLACK, 1,1,"Score : %d", block_cnt);
}
 
void CountPrintf()
{
        Survival_count--;
        if(Survival_count<0)// 3sec..
        {
            Survival_count++;
            Lcd_Draw_BMP(150,0, endgame);
            game_enable = 0 ;//gram end
        }
        Lcd_Printf(200,430, RED, BLACK, 1,1,"Survival time : %d",Survival_count/7);
}
 
void Main(void)
{
    Uart_Init(115200);
    HW_Initial();    
    Show_Welcome("Stairs of infinity");
 
    Creat_Block();
 
    Lcd_Select_Frame_Buffer(1);
    Lcd_Draw_BMP(0,0, cloud);
    Lcd_Copy(1,0);
    Lcd_Display_Frame_Buffer(0);
 
    srand(time(NULL));                
 
    Lcd_Make_Bmp_Buffer(0,0,cloud);
 
 
 
    while(1){    
        CountPrintf();
        Touch_ISR_Enable(1);
        touch_block();
        if(game_enable)
        Print_Block();
        ScorePrintf();
    }
    
}
cs



Stairs_of_infinity.tar.gz



Touch Screen

#Touch.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "2450addr.h"
 
void Touch_Init(void);
 
 
void Touch_Init(void)
{
    rADCDLY = (50000); 
     /* TO DO : prescaler enable, prescaler value=39, Analog no input, 
      *         Normal operation mode, Disable start, No operation */
        rADCCON |= (0x1<<14);
        rADCCON |= (39<<6);
        rADCCON &= ~(0x1<<3);
        rADCCON &= ~(0x1<<2);
        rADCCON &= ~(0x1<<1);
        rADCCON &= ~(0x1);
 
      
     
     /* TO DO :  For Waiting Interrupt Mode rADCTSC=0xd3 */
        rADCTSC &= ~(0x1<<8);
        rADCTSC |= (0x1<<7);
        rADCTSC |= (0x1<<6);
        rADCTSC &= ~(0x1<<5);
        rADCTSC |= (0x1<<4);
        rADCTSC &= ~(0x1<<3);
        rADCTSC &= ~(0x1<<2);
        rADCTSC |= (0x1<<1);
        rADCTSC |= (0x1);
 
}
 
cs


#Main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "2450addr.h"
#include "option.h"
 
#define EXAMPLE 610
 
#if EXAMPLE == 610
//**ISR Declaration
void Touch_ISR(void) __attribute__ ((interrupt ("IRQ")));
 
volatile  int ADC_x, ADC_y;
volatile  int Touch_Pressed=0;
 
void Touch_ISR()
{
    /* 인터럽트 허용하지 않음 on Touch */
    rINTSUBMSK |= (0x1<<9);
    rINTMSK1 |= (0x1<<31);    
    
    /* TO DO: Pendng Clear on Touch */    
    rSUBSRCPND |= (0x1<<9);
    rSRCPND1 |= (0x1<<31);
    rINTPND1 |= (0x1<<31);
 
    if(rADCTSC & 0x100)
    {
        rADCTSC &= (0xff); 
        Touch_Pressed = 0;
        Uart_Send_String("Detect Stylus Up Interrupt Signal \n");
    }
    
    else
    {
        Uart_Send_String(" ISR 내부  \n");
        
        rADCTSC &=~(0x1<<8);                //detect stylus down
        rADCTSC &= ~(0x1<<7);                //set bit for x position measurement
        rADCTSC |= (0x1<<6);                //YM=Hi-z, YP=Hi-z
        rADCTSC |= (0x1<<5);        
        rADCTSC &= ~(0x1<<4);                //XM=VSS,XP=VDD
        rADCTSC &= ~(0x1<<3);                //pull-up EN
        rADCTSC &= ~(0x1<<2);                //Normal-ADC
        rADCTSC &= ~(0x3);
        rADCTSC |= (0x1);                    //X-position = 1
 
 
        /* TO DO : ENABLE_START */        
        rADCCON |= (0x1);
        
        /* wait until End of A/D Conversion */
        while(!(rADCCON & (1<<15)));
        rADCCON &= ~(0x1);                    //Stop Conversion
 
        /*Set ADCTSC reg for Y Conversion*/ 
        ADC_x = (rADCDAT0 & 0x3ff);            //Store X value
        
        rADCTSC |= (0x1<<7);                //YM=VSS, YP=VDD
        rADCTSC &= ~(0x1<<6);
        rADCTSC &= ~(0x1<<5);                //XM=Hi-z, XP=Hi-z
        rADCTSC |= (0x1<<4);
        /* clear and then set  ADCTSC [1:0] for Y Conversion*/
        rADCTSC &= ~(0x3);
        rADCTSC |= (0x2);
    
        rADCCON |= (0x1);                //StartConversion
        while(!(rADCCON & (1<<15)));    //wait untill End of A/D Conversion
 
        ADC_y = (rADCDAT1 & 0x3ff);        //Store y value
        
        Touch_Pressed = 1;
        
        /* TO DO : change to Waiting for interrupt mode 
         *           Stylus Up, YM_out Enable, YP_out Disable, XM_out Disable, XP_out disable
         */
        rADCTSC |= (0x1<<8);
        rADCTSC |= (0x1<<7);
        rADCTSC |= (0x1<<6);
        rADCTSC &= ~(0x1<<5);
        rADCTSC |= (0x1<<4);
        rADCTSC &= ~(0x1<<3);
        rADCTSC &= ~(0x1<<2);
        rADCTSC |= (0x1<<1);
        rADCTSC |= (0x1);
             
    }
    
    /* 인터럽트 다시 허용  on Touch */
    rINTSUBMSK &= ~(0x1<<9);
    rINTMSK1 &= ~(0x1<<31);
    
}
 
void Main(void)
{    
    Uart_Init(115200);
    Touch_Init();    
    
    Uart_Printf("*** Touch Test *** \n");
    
    /* TO DO : 인터럽트 벡터에 Touch_ISR 함수 등록 */
    pISR_ADC = (unsigned int)Touch_ISR;
    
    /* TO DO :  인터럽트 허용 on Touch */
    rINTSUBMSK &= ~(0x1<<9);
    rINTMSK1 &= ~(0x1<<31);    
    
    while(1)
    {
        if(Touch_Pressed)
        {
            Uart_Printf("X : %d, Y: %d \n", ADC_x, ADC_y);    
        }
    }
}
 
#endif 
cs



********************************Touch_Init***************************


ADCDLY : ADC 간격 딜레이를 설정 (5000)

=====================================================================

ECFLG[15] : A/D 변환이 끝났는지 확인하는 비트.

PRSCEN[14] : prescaler 활성화 비트.

PRSCVL[13:6] : A/D 변환기 클럭  prescaler  값. ADC 주파수는 PCLK의 1/5보다 적은 주파수로 할당되어야 한다. 

RESSEL[3] : conversion 되는 디지털 값을 선택(10bit/12bit)

STDBM[2] : Standby mode 선택 비트. Standby 모드가 활성화 되면 현재 진행중인 A/D변환은 멈추게 되고, ADCDAT0와 ADCDAT1은 변환 이전의 데이타를 가지게 된다.

READ_START[1] : A/D변환을 시작시키는 비트이며, 변환 데이터가 읽혀 졌을 때 자동으로 다음 변환을 시작한다. 

ENABLE_START[0] : A/D 변환시작 비트이며 시작 이후에는 클리어된다.  A/D변환 방법은 2가지 방법이 있는데,  만약 READ_START 비트를 사용할 경우에는 이 비트는 의미가 없게 된다. 

=====================================================================



prescaler(프리스케일러) 란?


prescaler는 타이머에 공급하는 입력 클럭의 속도를 조절하는 분주기이다.

분주기라는 말 자체가 의미하듯이 클럭을 쪼갠다는, 즉 속도를 느리게 한다는 말이다.

예를 들어 8MHz의 입력 클럭을 2분주 하면 4MHz가 된다.




==================ADCCON=====================


[14] prescaler (Enalbe) (1)

[13:6] prescaler value를 39 (39)

[3] 10bit resolution (0)

[2] no standby mode (0) //동작 가능

[1] A/D conversion start (0) 

[0] Enable_start no setting (0)


==============================================


==================ADCTSC=====================


[8] Stylus Down (0)

[7] Y- (ON) (1)

[6] Y+ (OFF) (1)

[5] X- (OFF) (0)

[4] X+ (OFF) (1)

[3] Pull-up (ON) (0) //누설전류 방지 ->sleep/stop mode->pull-up스위치 활성화

[2] AUTO_PST(OFF) (0) //ADC conversion 사용을위해 normal ADC conversion

[1:0] Waiting for interrupt mode (1,1)


==============================================




********************************Touch_ISR***************************

start.

rINTSUBMSK(TC)

rINTMSK1(ADC)

비활성화


1. ADCTSC[8]이 stylus up이면 실행 x ,Touch_Pressed =0 설정(출력 x)



2.아니면 stylus down(pen을 종이에 쓸준비)이면 

Y- (OFF) :연결 X

Y+ (OFF) :ADC에 연결

X- (ON) :GND에 연결

X+ (ON) :VCC에 연결


==>X_POSITION 측정


3.rADCCON 0번 bit로 start


4.rADCCON 15번 bit로 A/D 끝났는지 확인(폴링)


5.끝났으면 rADCCON 0번 bit로 stop

X의 position 값은 ADCDAT0에 저장되어 있으므로 변수에 저장


6.

Y- (ON)

Y+ (ON)

X- (OFF)

X+ (OFF)



==>Y_POSITION 측정


3.rADCCON 0번 bit로 start


8.rADCCON 15번 bit로 A/D 끝났는지 확인(폴링)



9.끝났으면 rADCCON 0번 bit로 stop

Y의 position 값은 ADCDAT1에 저장되어 있으므로 변수에 저장


10. Touch_Pressed =1 설정 (출력)


11.

stylus up(pen을 종이에서 땐다)설정

Y-값 측정(ON)

Y+ (OFF)

X- (OFF)

X+ (OFF)

PULL-UP (ON)

Waiting for interrupt mode로 전환


end.

rINTSUBMSK(TC)

rINTMSK1(ADC)

다시 활성화

■DMA +Timer

#Main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 
#if EXAMPLE == 370
void MemFill(unsigned long ptr, unsigned long pattern, int size)
{
    int i;
    unsigned long *data;
    data = (unsigned long *)ptr;
 
    for (i=0; i<size;i++)
        *data++ = pattern;
}
 
void MemDump(unsigned long ptr, int size)
{
    int i;
    unsigned long *data;
    data = (unsigned long *)ptr;
 
    Uart_Printf("\n");
    Uart_Printf("*** Dump Memory from [0x%08x] to [0x%08x] : \n",ptr, (unsigned long *)ptr+size);
    for (i=0; i<size;i++) {
        if ( !(i%4) )
            Uart_Printf("\n  [0x%08x] : ",data);
        Uart_Printf("0x%08x ",*data++);
    }
    Uart_Printf("\n");
}
 
 
 
 
#define pISR_DMA0        (*(unsigned *)(0x33ffff64))
static unsigned long src = 0x33000000;
static unsigned long dst = 0x33100000;
static unsigned int size = 12//word size
unsigned long pattern;
 
static void DMA0_interrupt()
{
    
    rINTSUBMSK |=(0<<18);
    rINTMSK1 |=(1<<17);
    
    rSUBSRCPND |= 0x1<<18;
    rSRCPND1|=(1<<17);    //clear
    rINTPND1 |=(1<<17);    //clear
 
    Uart_Send_String("\n###DMA0 START ...!!");
    MemDump(dst,size);
 
    //    rDMASKTRIG0 |= 0x1 <<2;            이부분을 주석처리 해주면 single mode 
 
    rINTSUBMSK &=~(1<<18);
    rINTMSK1 &=~(1<<17);
 
}
 
void Main(void)
{    
 
    //interrupt handler set
    pISR_DMA0= (unsigned int)DMA0_interrupt;
 
    rINTSUBMSK &=~(0<<18);
    rINTMSK1 &=~(1<<17);
 
    rDMASKTRIG0=0x0;
 
    //src , dest init
    rDISRC0 = src;
    rDIDST0 = dst;
 
    //disrcc , didstc init
    rDISRCC0 &= ~0x3;
    rDIDSTC0    &= ~0x7;
 
    //dcon    init 
    rDCON0 = (1<<31| (1<<30| (1<<29|(0<<28| (1<<27)| (1<<24| (0<<23)|(0<<22| (0<<20)|(size*4<<0);
 
    
    //H/W request mode;1
    rDMAREQSEL0|= (0x1<<0);    
 
 
 
    rDMAREQSEL0|= (0x9<<1);                            //Hard ware mode로 설정
 
 
 
 
    Timer_Init(0);
 
    MemFill(src,0x00000000,size);
    MemFill(dst,0x00000000,size);
 
    pattern = 0x5555aaaa;
 
    MemFill(src,pattern,size);    //src malloc;
    MemDump(src,size);
 
    Timer_Delay(1000,0);
 
    rDMASKTRIG0 = (1<<1| (1<<0);
 
 
}
# endif 
cs

#Timer.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "2450addr.h"
#include "option.h"
#include "libc.h"
 
//Function Declaration
void Timer_Init(int k);
void Timer_Delay(int msec,int k);
 
 
 
 
void Timer_Init(int k)
{
    rTCFG0=0xffff;
    rTCFG1 |= (0x3 <<(4*k));
 
 
 
 
 
    rTCFG1 |= (0x1 <<20);        //DMA    requst    channel
 
 
 
    if(k==0)
    rTCON|= 0x1 <<3;
    else if(k==4)
    rTCON|= 0x1 <<22;
    else
    {
        rTCON|= 0x80 << (4*k);
 
    }
    *(&rTCNTB0 + (k *0xc)) = 0;
    *(&rTCMPB0 + (k *0xc)) = 0;
 
  
}
 
void Timer_Delay(int msec,int k)
{
 
    switch(k)
    {
        case 0: rTCNTB0= 16.113*msec; break;
        case 1: rTCNTB1= 16.113*msec; break;
        case 2: rTCNTB2= 16.113*msec; break;
        case 3: rTCNTB3= 16.113*msec; break;
        case 4: rTCNTB4= 16.113*msec; break;
    }
 
    if(k==0)
    {
    rTCON |= (1<<1| (0);
    rTCON &= ~(1<<1);
    rTCON |= 1;
    }    
    else
    {
        rTCON |= (0x20 <<(4 * k)) | (0);
        rTCON &= ~(0x20 <<(4 *k));
        rTCON |= (0x10<<(4 *k));
    }
 
}
cs

1. TCFG1 설정
rTCFG1 |= (0x1 <<20);


2. 하드웨어 인터럽트로 설정

rDMAREQSEL0|= (0x1<<0);

rDMAREQSEL0|= (0x9<<1);



#결과


■DMA +Uart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
static    unsigned long src=0x33000000;
static    unsigned long dst=0x33100000;
static    unsigned int size = 12/* word size */
static    unsigned long pattern;
 
void DMA0_UART_Init(void)
{
    /* Init DMASKTRIG on DMA0*/
    rDMASKTRIG0 = 0x0;
    
    /* TODO : Select Base Address of Source and Destination*/
    rDISRC0 =src;
    rDIDST0 =dst;
    
    /* TO DO : Init Source & Destination Control Register on DMA0 
     * (memory -> AHB bus, INC mode, CHK_INT -> after auto-reload )
     */
    
    rDISRCC0 &= ~0x3;
    rDIDSTC0 &=~(0x7);
    
    /* TO DO : setting for H/W DMA request
     * DCON0 : Handshake[31-1], HCLK[30-1], INT mode[29-1], Single Unit[28-0], Whole service[27-1], 
     * H/W trigger[23-1], Auto Reload ON[22-0], Byte size[21:20](Note: Word size<->Byte size)
     * DMAREQSEL0 : Uart1[5:1], SWHW_SEL[0-1]
     */
    rDCON0 =(unsigned int)(0<<31)+(1<<30)+(1<<29)+(0<<28)+(1<<27)+(1<<24)+(1<<23)+(0<<22)+(0<<20)+size*4;
    rDMAREQSEL0    = (21<<1)+(0x1<<0);
     /* TO DO : Turn on Trigger*/
    rDMASKTRIG0 |= (0x1<<1);
        
}
 
void DMA0_HW_Start(void)
{    
    MemFill(src, 0x00000000size);
    MemFill(dst, 0x00000000size);    
    Uart_Printf("\nStart Memory copy test With DMA\n");    
    
    /* memory copy test with DMA */
    pattern = 0x5555aaaa;
    Uart_Printf("Fill pattern [0x%08x] to [0x%08x]\n",pattern, src);
    MemFill(src, pattern, size);
    MemDump(src, size);
    Uart_Printf("\nCopy from [0x%08x] to [0x%08x] by DMA\n",src, dst);
            
}
 
 
void Main(void)
{    
    char value;
 
    Uart_DMA_Init(115200);    
    DMA0_UART_Init();
 
    Uart_Send_Byte('\n');
    Uart_Send_Byte('A');    
    Uart_Send_String("Hello Uart Test...!!!\n");
    
    /* TO DO : 인터럽트 벡터에 DMA0_ISR 함수 등록 */
        pISR_DMA0 = (unsigned int)DMA0_ISR;
    
        
    /* TO DO : 인터럽트 허용 on DMA0 */    
        rINTSUBMSK &= ~(0x1<<18);
    rINTMSK1 &= ~(0x1<<17);
    
    DMA0_HW_Start();
    
    while(1){
        Uart_Printf("Input Value = : ");    
        value = Uart_Get_Char();
        Uart_Printf("value=%c \n", value);                                
    }
}
cs


■DMA Interrupt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
 
#if EXAMPLE == 360
void MemFill(unsigned long ptr, unsigned long pattern, int size)
{
    int i;
    unsigned long *data;
    data = (unsigned long *)ptr;
 
    for (i=0; i<size;i++)
        *data++ = pattern;
}
 
void MemDump(unsigned long ptr, int size)
{
    int i;
    unsigned long *data;
    data = (unsigned long *)ptr;
 
    Uart_Printf("\n");
    Uart_Printf("*** Dump Memory from [0x%08x] to [0x%08x] : \n",ptr, (unsigned long *)ptr+size);
    for (i=0; i<size;i++) {
        if ( !(i%4) )
            Uart_Printf("\n  [0x%08x] : ",data);
        Uart_Printf("0x%08x ",*data++);
    }
    Uart_Printf("\n");
}
 
 
 
 
#define pISR_DMA0        (*(unsigned *)(0x33ffff64))
static unsigned long src = 0x33000000;
static unsigned long dst = 0x33100000;
static unsigned int size = 12//word size
unsigned long pattern;
 
static void DMA0_interrupt()
{
    
    rINTSUBMSK |=(0<<18);
    rINTMSK1 |=(1<<17);
    
    rSUBSRCPND |= 0x1<<18;
    rSRCPND1|=(1<<17);    //clear
    rINTPND1 |=(1<<17);    //clear
 
    Uart_Send_String("\n###DMA START ...!!");
    MemDump(dst,size);
 
    rDMASKTRIG0 |= 0x1 <<2;
 
    rINTSUBMSK &=~(1<<18);
    rINTMSK1 &=~(1<<17);
 
}
 
void Main(void)
{    
    rDMASKTRIG0=0x0;
 
    //src , dest init
    rDISRC0 = src;
    rDIDST0 = dst;
 
    //disrcc , didstc init
    rDISRCC0 &= ~0x3;
    rDIDSTC0 &= ~0x7;
 
    //dcon    init 
    rDCON0 = (1<<31| (1<<30| (1<<29|(0<<28| (1<<27)| (1<<24| (0<<23)|(0<<22| (0<<20)|(size*4<<0);
 
    
    //S/W request mode;0
    rDMAREQSEL0= (0x0<<0);    
 
    
    MemFill(src,0x00000000,size);
    MemFill(dst,0x00000000,size);
 
    pattern = 0x5555aaaa;
 
    MemFill(src,pattern,size);    //src malloc;
    MemDump(src,size);
 
 
    //on_off mode ; sw trigger(start)
    rDMASKTRIG0 = (1<<1| (1<<0);
 
 
    //interrupt handler set
    pISR_DMA0= (unsigned int)DMA0_interrupt;
 
 
    rINTSUBMSK &=~(0<<18);
    rINTMSK1 &=~(1<<17);
 
}
# endif 
cs



1.시작지점과 목적지점의 주소를 입력

 rDISRC0 = src;

 rDIDST0 = dst; 


 rDISRCC0 &= ~0x3; 

 rDIDSTC0 &= ~0x7;



2. DCON0 설정

/*setting for S/W DMA request

DCON0 : Handshake[31-1], HCLK[30-1], INT mode[29-1], Single Unit[28-0], Whole service[27-1],

HW_select[26:24],  S/W trigger[23-0], Auto Reload ON[22-0], Byte size[21:20](Note: Word size<->Byte size)*/

3. 소프트웨어 인터럽트로 설정

rDMAREQSEL0 = (0x0<<0);


4. 트리거 설정, start    =>소프트웨어 인터럽트

rDMASKTRIG0 = (1<<1) | (1<<0);


5.

rINTSUBMSK    DMA모드로 설정

rINTMSK1 &=~(1<<17);    interrupt 허용






+ Recent posts