凌云实验室推出的ARM Linux物联网网关开发板IGKBoard(IoT Gateway Kit Board)项目源码
guowenxue
2021-11-17 8d7c9614f2fdfc3ec7db15501d74335aed012efa
Add UAPI test program
4 files added
478 ■■■■■ changed files
bsp/uapi/test/makefile 76 ●●●●● patch | view | raw | blame | history
bsp/uapi/test/test_fb.c 154 ●●●●● patch | view | raw | blame | history
bsp/uapi/test/test_key.c 173 ●●●●● patch | view | raw | blame | history
bsp/uapi/test/test_led.c 75 ●●●●● patch | view | raw | blame | history
bsp/uapi/test/makefile
New file
@@ -0,0 +1,76 @@
#*********************************************************************************
#      Copyright:  (C) 2021 Guo Wenxue <guowenxue@gmail.com>
#                  All rights reserved.
#
#       Filename:  Makefile
#    Description:  This Makefile used to compile all the C source code file in current
#                  folder to respective excutable binary files.
#
#        Version:  1.0.0(11/17/2021~)
#                  Author:  Guo Wenxue <guowenxue@gmail.com>
#      ChangeLog:  1, Release initial version on "11/17/2021 01:29:33 PM"
#
#********************************************************************************/
PWD=$(shell pwd)
INSTPATH=/tftp
CROSS_COMPILE=/opt/buildroot/cortexA7/bin/arm-linux-
export CC=${CROSS_COMPILE}gcc
export CXX=${CROSS_COMPILE}g++
export AR=${CROSS_COMPILE}ar
export AS=${CROSS_COMPILE}as
export RANLIB=${CROSS_COMPILE}ranlib
export STRIP=${CROSS_COMPILE}strip
LIB_PATH=$(shell dirname ${PWD})
LIB_NAME=$(shell basename ${LIB_PATH})
CFLAGS+=-I${LIB_PATH}
#LDFLAGS+=-L${LIB_PATH} -l${LIB_NAME}
#LDFLAGS+=-static
SRCS = $(wildcard ${VPATH}/*.c)
OBJS = $(patsubst %.c,%.o,$(SRCS))
SRCFILES = $(wildcard *.c)
BINARIES=$(SRCFILES:%.c=%)
all: entry binaries install
libs:
    make -C ..
entry:
    @echo " ";
    @echo " =========================================================";
    @echo " **      Cross  compile \"${BINARIES}\"                   ";
    @echo " =========================================================";
    @make clean
binaries:  ${BINARIES}
    @echo " Compile over"
%:  %.c
    $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
tag:
    @ctags --c-kinds=+defglmnstuvx --langmap=c:.c.h.ho.hem.het.hec.hev.him.hit.hic.hiv -R .
    @cscope -Rbq
install:
    cp $(BINARIES) ${INSTPATH}
clean:
    @rm -f version.h
    @rm -f *.o *.lo $(BINARIES)
    @rm -rf *.gdb *.a *.so *.elf*
    @rm -f *.log
distclean: clean
    @rm -f  tags cscope*
.PHONY: clean entry
bsp/uapi/test/test_fb.c
New file
@@ -0,0 +1,154 @@
/*********************************************************************************
 *      Copyright:  (C) 2021 Guo Wenxue<Email:guowenxue@gmail.com QQ:281143292>
 *                  All rights reserved.
 *
 *       Filename:  test_key.c
 *    Description:  This file used to test GPIO button driver builtin Linux kernel on ARM board
 *
 *        Version:  1.0.0(11/17/2021~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "11/17/2021 02:46:18 PM"
 *
 *     Linux Clear LCD screen command :
 *
 *                  dd if=/dev/zero of=/dev/fb0 bs=320 count=720  [ 240*(24bpp/8)=720 ]
 *
 ********************************************************************************/
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#define CONFIG_RGB565
#ifdef  CONFIG_RGB565  /* RRRR RGGG GGGB BBBB  */
#define RED       0xF800
#define GREEN     0x07E0
#define BLUE      0x001F
#define YELLOW    0xFFE0
#define WHITE     0xFFFF
#endif
void fill_color16(short *fb_addr, short bit_map, int psize)
{
    int i;
    for(i=0; i<psize; i++) {
        *fb_addr = bit_map;
        fb_addr++;
    }
}
int main ()
{
    int fp=0;
    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    long screensize=0;
    char *fbp = NULL, *test_fbp=NULL;
    int x = 0, y = 0;
    long location = 0;
    int i;
    int num = 5;
    int pix_size=0;
    fp = open("/dev/fb0", O_RDWR);
    if(fp < 0) {
        printf("Error : Can not open framebuffer device/n");
        exit(1);
    }
    if(ioctl(fp, FBIOGET_FSCREENINFO, &finfo)){
        printf("Error reading fixed information/n");
        exit(2);
    }
    if(ioctl(fp, FBIOGET_VSCREENINFO, &vinfo)){
        printf("Error reading variable information/n");
        exit(3);
    }
    screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
    printf("The phy mem = 0x%x, total size = %d(byte)\n", finfo.smem_start, finfo.smem_len);
    printf("xres =  %d, yres =  %d, bits_per_pixel = %d\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
    printf("So the screensize = %d(byte), using %d frame\n", screensize, finfo.smem_len/screensize);
    printf("vinfo.xoffset = %d, vinfo.yoffset = %d\n", vinfo.xoffset, vinfo.yoffset);
    printf("vinfo.vmode is :%d\n", vinfo.vmode);
    printf("finfo.ypanstep is :%d\n", finfo.ypanstep);
    printf("vinfo.red.offset=0x%x\n", vinfo.red.offset);
    printf("vinfo.red.length=0x%x\n", vinfo.red.length);
    printf("vinfo.green.offset=0x%x\n", vinfo.green.offset);
    printf("vinfo.green.length=0x%x\n", vinfo.green.length);
    printf("vinfo.blue.offset=0x%x\n", vinfo.blue.offset);
    printf("vinfo.blue.length=0x%x\n", vinfo.blue.length);
    printf("vinfo.transp.offset=0x%x\n", vinfo.transp.offset);
    printf("vinfo.transp.length=0x%x\n", vinfo.transp.length);
    fbp =(char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fp,0);
    if ((int)fbp == -1)
    {
        printf ("Error: failed to map framebuffer device to memory./n");
        exit (4);
    }
    printf("Get virt mem = %p\n", fbp);
    pix_size = vinfo.xres * vinfo.yres;
    /* using first frame, for FBIOPAN_DISPLAY
     * 当刷新需要调用FBIOPAN_DISPLAY, 要告知驱动刷哪块帧, 用到下面两个参数
     * 如果使用第二帧buffer -> vinfo.xoffset = 0; vinfo.yoffset = vinfo.yres;
     */
    vinfo.xoffset = 0;
    vinfo.yoffset = 0;
    /* show color loop */
    while(num--) {
        printf("\ndrawing red ...\n");
        fill_color16((short *)fbp, RED, pix_size);
        //ioctl(fp, FBIOPAN_DISPLAY, &vinfo);
        sleep(3);
        printf("\ndrawing green ...\n");
        fill_color16((short *)fbp, GREEN, pix_size);
        //ioctl(fp, FBIOPAN_DISPLAY, &vinfo);
        sleep(3);
        printf("\ndrawing blue ...\n");
        fill_color16((short *)fbp, BLUE, pix_size);
        //ioctl(fp, FBIOPAN_DISPLAY, &vinfo);
        sleep(3);
        printf("\ndrawing yellow ...\n");
        fill_color16((short *)fbp, YELLOW, pix_size);
        //ioctl(fp, FBIOPAN_DISPLAY, &vinfo);
        sleep(3);
    }
#if 1
    /*这是你想画的点的位置坐标,(0,0)点在屏幕左上角*/
    x = 10;
    y = 10;
    location = x * (vinfo.bits_per_pixel / 8) + y  *  finfo.line_length;
    test_fbp = fbp + location;
    printf("draw line.......\n");
    for(i = 0; i < (vinfo.xres - x); i++)
        *test_fbp++ = i+30;
    //ioctl(fp, FBIOPAN_DISPLAY, &vinfo);
#endif
    munmap(fbp, screensize); /*解除映射*/
    close (fp);
    return 0;
}
bsp/uapi/test/test_key.c
New file
@@ -0,0 +1,173 @@
/*********************************************************************************
 *      Copyright:  (C) 2021 Guo Wenxue<Email:guowenxue@gmail.com QQ:281143292>
 *                  All rights reserved.
 *
 *       Filename:  test_key.c
 *    Description:  This file used to test GPIO button driver builtin Linux kernel on ARM board
 *
 *        Version:  1.0.0(11/17/2021~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "11/17/2021 02:46:18 PM"
 *
 ********************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <libgen.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include <linux/kd.h>
#include <linux/keyboard.h>
#if 0 /* Just for comment here, Reference to linux-3.3/include/linux/input.h */
struct input_event
{
    struct timeval time;
    __u16 type;  /* 0x00:EV_SYN 0x01:EV_KEY 0x04:EV_MSC 0x11:EV_LED*/
    __u16 code;  /* key value, which key */
    __s32 value; /* 1: Pressed  0:Not pressed  2:Always Pressed */
};
#endif
#define EV_RELEASED        0
#define EV_PRESSED         1
#define BUTTON_CNT         10
void usage(char *name);
void display_button_event(struct input_event *ev, int cnt);
int main(int argc, char **argv)
{
    char                  *kbd_dev = "/dev/input/event2";
    char                  kbd_name[256] = "Unknown";
    int                   kbd_fd = -1;
    int                   rv, opt;
    int                   size = sizeof (struct input_event);
    fd_set                rds;
    struct input_event    ev[BUTTON_CNT];
    struct option long_options[] = {
        {"device", required_argument, NULL, 'd'},
        {"help", no_argument, NULL, 'h'},
        {NULL, 0, NULL, 0}
    };
    while ((opt = getopt_long(argc, argv, "d:h", long_options, NULL)) != -1)
    {
        switch (opt)
        {
            case 'd':
                kbd_dev = optarg;
                break;
            case 'h':
                usage(argv[0]);
                return 0;
            default:
                break;
        }
    }
    if(NULL == kbd_dev)
    {
        usage(argv[0]);
        return -1;
    }
    if ((getuid ()) != 0)
        printf ("You are not root! This may not work...\n");
    if ((kbd_fd = open(kbd_dev, O_RDONLY)) < 0)
    {
        printf("Open %s failure: %s", kbd_dev, strerror(errno));
        return -1;
    }
    ioctl (kbd_fd, EVIOCGNAME (sizeof (kbd_name)), kbd_name);
    printf ("Monitor input device %s (%s) event on poll mode:\n", kbd_dev, kbd_name);
    while (1)
    {
        FD_ZERO(&rds);
        FD_SET(kbd_fd, &rds);
        rv = select(kbd_fd + 1, &rds, NULL, NULL, NULL);
        if (rv < 0)
        {
            printf("Select() system call failure: %s\n", strerror(errno));
            goto CleanUp;
        }
        else if (FD_ISSET(kbd_fd, &rds))
        {
            if ((rv = read (kbd_fd, ev, size*BUTTON_CNT )) < size)
            {
                printf("Reading data from kbd_fd failure: %s\n", strerror(errno));
                break;
            }
            else
            {
                display_button_event(ev, rv/size);
            }
        }
    }
CleanUp:
    close(kbd_fd);
    return 0;
}
void usage(char *name)
{
    char *progname = NULL;
    char *ptr = NULL;
    ptr = strdup(name);
    progname = basename(ptr);
    printf("Usage: %s [-p] -d <device>\n", progname);
    printf(" -d[device  ] button device name\n");
    printf(" -p[poll    ] Use poll mode, or default use infinit loop.\n");
    printf(" -h[help    ] Display this help information\n");
    free(ptr);
    return;
}
void display_button_event(struct input_event *ev, int cnt)
{
    int i;
    struct timeval        pressed_time, duration_time;
    for(i=0; i<cnt; i++)
    {
        if(EV_KEY==ev[i].type && EV_PRESSED==ev[i].value)
        {
            pressed_time = ev[i].time;
            printf("Keypad[%d] pressed time: %ld.%ld\n", ev[i].code, pressed_time.tv_sec, pressed_time.tv_usec);
        }
        if(EV_KEY==ev[i].type && EV_RELEASED==ev[i].value)
        {
            timersub(&ev[i].time, &pressed_time, &duration_time);
            printf("keypad[%d] released time: %ld.%ld\n", ev[i].code, ev[i].time.tv_sec, ev[i].time.tv_usec);
            printf("keypad[%d] duration time: %ld.%ld\n", ev[i].code, duration_time.tv_sec, duration_time.tv_usec);
        }
    } /*  for(i=0; i<cnt; i++) */
}
bsp/uapi/test/test_led.c
New file
@@ -0,0 +1,75 @@
/*********************************************************************************
 *      Copyright:  (C) 2021 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  test_led.c
 *    Description:  This file is imx6ull LED test program
 *
 *        Version:  1.0.0(2021年11月17日)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "2021年11月17日 22时59分31秒"
 *
 ********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define CLASS_LED    "/sys/class/leds"
int turn_led(char *which, unsigned char brightness);
int main (int argc, char **argv)
{
    while(1)
    {
        turn_led("sysled", 255);
        sleep(1);
        turn_led("sysled", 0);
        sleep(1);
    }
    return 0;
}
int turn_led(char *which, unsigned char brightness)
{
    char            led_path[64] = {0x0};
    char            buf[5] = {0x0};
    int             fd = -1;
    int             rv = 0;
    if( !which )
    {
        printf("%s() Invalid input arguments\n", __func__);
        return -1;
    }
    snprintf(led_path, sizeof(led_path), "%s/%s/brightness", CLASS_LED, which);
    if( (fd=open(led_path, O_WRONLY)) < 0 )
    {
        printf("Open led file '%s' failure: %s\n", strerror(errno));
        return -2;
    }
    snprintf(buf, sizeof(buf), "%d", brightness);
    if( write(fd, buf, strlen(buf)) < 0 )
    {
        printf("Open led file '%s' failure: %s\n", strerror(errno));
        rv = -3;
    }
    close(fd);
    return rv;
}