guowenxue
2020-08-21 1e6bc76e37f1a0aab7429aac91394c1b4f138bc0
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
/*********************************************************************************
 *      Copyright:  (C) 2017 LingYun IoT Studio <www.iot-yun.com>
 *                  All rights reserved.
 *
 *       Filename:  test_s3c_led.c
 *    Description:  This file is used to tst s3c_led.c driver
 *                 
 *        Version:  1.0.0(03/15/2014)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "03/15/2014 02:03:22 PM"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/select.h>
 
#define LED_CNT        4
#define DEVNAME_LEN    10
 
#define PLATDRV_MAGIC             0x60
#define LED_OFF                   _IO (PLATDRV_MAGIC, 0x18)
#define LED_ON                    _IO (PLATDRV_MAGIC, 0x19)
 
static inline msleep(unsigned long ms)
{
    struct timeval       tv;
 
    tv.tv_sec = ms/1000;
    tv.tv_usec = (ms%1000)*1000;
 
    select(0, NULL, NULL, NULL, &tv);
}
 
int main (int argc, char **argv)
{
    int           i;
    int           fd[LED_CNT];
    char          dev_name[DEVNAME_LEN]={0,0,0,0};
 
    for(i=0; i<LED_CNT; i++)
    {
        snprintf(dev_name, sizeof(dev_name), "/dev/led%d", i);
        fd[i] = open(dev_name, O_RDWR, 0755);
        if(fd[i] < 0)
            goto err;
    }
 
    while(1)
    {
        for(i=0; i<LED_CNT; i++)
        {
            ioctl(fd[i], LED_ON);
            msleep(300);
            ioctl(fd[i], LED_OFF);
            msleep(300);
        }
    }
 
    for(i=0; i<LED_CNT; i++)
    {
        close(fd[i]);
    }
 
    return 0;
 
err:
    for(i=0; i<LED_CNT; i++)
    {
        if(fd[i] >= 0)
        {
            close(fd[i]);
        }
    }
    return -1;
} /* ----- End of main() ----- */