|
| 1 | +/**************************************************************************** |
| 2 | + * apps/examples/uio/uio_test.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <errno.h> |
| 26 | +#include <fcntl.h> |
| 27 | +#include <poll.h> |
| 28 | +#include <stdio.h> |
| 29 | +#include <stdlib.h> |
| 30 | +#include <string.h> |
| 31 | +#include <sys/mman.h> |
| 32 | +#include <unistd.h> |
| 33 | + |
| 34 | +#define DEV_INDEX 1 |
| 35 | +#define SIZE_INDEX 2 |
| 36 | +#define W_TEST_NUM_INDEX 3 |
| 37 | +#define INPUT_NUM 4 |
| 38 | + |
| 39 | +/**************************************************************************** |
| 40 | + * Public Functions |
| 41 | + ****************************************************************************/ |
| 42 | + |
| 43 | +int main(int argc, FAR char *argv[]) |
| 44 | +{ |
| 45 | + struct pollfd events; |
| 46 | + FAR int *access_addr; |
| 47 | + char mem_size[32]; |
| 48 | + ssize_t size; |
| 49 | + int fd; |
| 50 | + int size_fd; |
| 51 | + int num; |
| 52 | + int ret; |
| 53 | + int i; |
| 54 | + int counter = 0; |
| 55 | + |
| 56 | + if (argc != INPUT_NUM) |
| 57 | + { |
| 58 | + printf("Usage: %s <dev> <size> <num>\n", argv[0]); |
| 59 | + return -EINVAL; |
| 60 | + } |
| 61 | + |
| 62 | + /* open uio related files */ |
| 63 | + |
| 64 | + fd = open(argv[DEV_INDEX], O_RDWR); |
| 65 | + if (fd < 0) |
| 66 | + { |
| 67 | + fprintf(stderr, "open %s: %s\n", argv[DEV_INDEX], strerror(errno)); |
| 68 | + return fd; |
| 69 | + } |
| 70 | + |
| 71 | + size_fd = open(argv[SIZE_INDEX], O_RDONLY); |
| 72 | + if (size_fd < 0) |
| 73 | + { |
| 74 | + fprintf(stderr, "open %s: %s\n", |
| 75 | + argv[SIZE_INDEX], strerror(errno)); |
| 76 | + ret = size_fd; |
| 77 | + goto err_size; |
| 78 | + } |
| 79 | + |
| 80 | + ret = read(size_fd, mem_size, sizeof(mem_size)); |
| 81 | + if (ret < 0) |
| 82 | + { |
| 83 | + fprintf(stderr, "read %s: %s\n", |
| 84 | + argv[SIZE_INDEX], strerror(errno)); |
| 85 | + goto err; |
| 86 | + } |
| 87 | + |
| 88 | + size = strtol(mem_size, NULL, 0); |
| 89 | + |
| 90 | + num = atoi(argv[W_TEST_NUM_INDEX]); |
| 91 | + if (num > size / sizeof(int)) |
| 92 | + { |
| 93 | + printf("input num exceeds shmem range\n"); |
| 94 | + ret = -EINVAL; |
| 95 | + goto err; |
| 96 | + } |
| 97 | + |
| 98 | + /* mmap /dev/uio addr */ |
| 99 | + |
| 100 | + access_addr = mmap(NULL, size, PROT_READ | PROT_WRITE, |
| 101 | + MAP_SHARED, fd, 0); |
| 102 | + if (access_addr == MAP_FAILED) |
| 103 | + { |
| 104 | + printf("mmap %s: %s\n", argv[DEV_INDEX], strerror(errno)); |
| 105 | + ret = errno; |
| 106 | + goto err; |
| 107 | + } |
| 108 | + |
| 109 | + printf("The device (lenth %ld)\n" "logical address %p\n", |
| 110 | + size, access_addr); |
| 111 | + |
| 112 | + /* read/write uio mem */ |
| 113 | + |
| 114 | + for (i = 0; i < num; i++) |
| 115 | + { |
| 116 | + access_addr[i] += 1; |
| 117 | + printf("i:%u, write %u\n", i, access_addr[i]); |
| 118 | + } |
| 119 | + |
| 120 | + memset(&events, 0 , sizeof(events)); |
| 121 | + events.fd = fd; |
| 122 | + events.events = POLLIN | POLLRDNORM; |
| 123 | + |
| 124 | + while (1) |
| 125 | + { |
| 126 | + ret = poll(&events, 1, 1000); |
| 127 | + if (ret < 0) |
| 128 | + { |
| 129 | + printf("[poll] read POLL ERROR, break\n"); |
| 130 | + break; |
| 131 | + } |
| 132 | + else if (ret == 0) |
| 133 | + { |
| 134 | + printf("[poll] read POLL timeout, continue\n"); |
| 135 | + continue; |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + if ((events.revents & (POLLIN | POLLRDNORM)) != 0) |
| 140 | + { |
| 141 | + ret = read(fd, &counter, 4); |
| 142 | + if (ret < 0) |
| 143 | + { |
| 144 | + printf("[poll] read error, error:%s, errno = %d\n", |
| 145 | + strerror(errno), errno); |
| 146 | + if (errno == ENODEV) |
| 147 | + { |
| 148 | + printf("[poll] device disconnect, break\n"); |
| 149 | + break; |
| 150 | + } |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + printf("Interrupt number is :%u\n", counter); |
| 155 | + break; |
| 156 | + } |
| 157 | + } |
| 158 | + else if (((events.revents & POLLERR) != 0) || |
| 159 | + ((events.revents & POLLHUP) != 0)) |
| 160 | + { |
| 161 | + printf("[poll] device error, break\n"); |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + munmap(access_addr, size); |
| 168 | + |
| 169 | +err: |
| 170 | + close(size_fd); |
| 171 | +err_size: |
| 172 | + close(fd); |
| 173 | + |
| 174 | + return ret; |
| 175 | +} |
0 commit comments