Skip to content

Commit ee88104

Browse files
author
jason_yao
committed
update
1 parent b7f287c commit ee88104

File tree

101 files changed

+5751
-5961
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+5751
-5961
lines changed

example/嵌入式Linux驅動模板精講與項目實踐/第11章/devmem2/devmem2.c

Lines changed: 87 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* but WITHOUT ANY WARRANTY; without even the implied warranty of
3131
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3232
* GNU General Public License for more details.
33-
*
33+
*
3434
* You should have received a copy of the GNU General Public License
3535
* along with this program; if not, write to the Free Software
3636
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@@ -48,83 +48,104 @@
4848
#include <termios.h>
4949
#include <sys/types.h>
5050
#include <sys/mman.h>
51-
51+
5252
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
53-
__LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
54-
53+
__LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
54+
5555
#define MAP_SIZE 4096UL
5656
#define MAP_MASK (MAP_SIZE - 1)
5757

58-
int main(int argc, char **argv) {
58+
int main(int argc, char** argv) {
5959
int fd;
60-
void *map_base, *virt_addr;
61-
unsigned long read_result, writeval;
62-
off_t target;
63-
int access_type = 'w';
64-
65-
if(argc < 2) {
66-
fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n"
67-
"\taddress : memory address to act upon\n"
68-
"\ttype : access operation type : [b]yte, [h]alfword, [w]ord\n"
69-
"\tdata : data to be written\n\n",
70-
argv[0]);
71-
exit(1);
72-
}
73-
target = strtoul(argv[1], 0, 0);
74-
75-
if(argc > 2)
76-
access_type = tolower(argv[2][0]);
77-
78-
79-
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
80-
printf("/dev/mem opened.\n");
60+
void* map_base, *virt_addr;
61+
unsigned long read_result, writeval;
62+
off_t target;
63+
int access_type = 'w';
64+
65+
if (argc < 2) {
66+
fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n"
67+
"\taddress : memory address to act upon\n"
68+
"\ttype : access operation type : [b]yte, [h]alfword, [w]ord\n"
69+
"\tdata : data to be written\n\n",
70+
argv[0]);
71+
exit(1);
72+
}
73+
74+
target = strtoul(argv[1], 0, 0);
75+
76+
if (argc > 2) {
77+
access_type = tolower(argv[2][0]);
78+
}
79+
80+
81+
if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) {
82+
FATAL;
83+
}
84+
85+
printf("/dev/mem opened.\n");
8186
fflush(stdout);
82-
87+
8388
/* Map one page */
8489
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
85-
if(map_base == (void *) -1) FATAL;
86-
printf("Memory mapped at address %p.\n", map_base);
90+
91+
if (map_base == (void*) - 1) {
92+
FATAL;
93+
}
94+
95+
printf("Memory mapped at address %p.\n", map_base);
8796
fflush(stdout);
88-
97+
8998
virt_addr = map_base + (target & MAP_MASK);
90-
switch(access_type) {
91-
case 'b':
92-
read_result = *((unsigned char *) virt_addr);
93-
break;
94-
case 'h':
95-
read_result = *((unsigned short *) virt_addr);
96-
break;
97-
case 'w':
98-
read_result = *((unsigned long *) virt_addr);
99-
break;
100-
default:
101-
fprintf(stderr, "Illegal data type '%c'.\n", access_type);
102-
exit(2);
103-
}
104-
printf("Value at address 0x%X (%p): 0x%X\n", target, virt_addr, read_result);
99+
100+
switch (access_type) {
101+
case 'b':
102+
read_result = *((unsigned char*) virt_addr);
103+
break;
104+
105+
case 'h':
106+
read_result = *((unsigned short*) virt_addr);
107+
break;
108+
109+
case 'w':
110+
read_result = *((unsigned long*) virt_addr);
111+
break;
112+
113+
default:
114+
fprintf(stderr, "Illegal data type '%c'.\n", access_type);
115+
exit(2);
116+
}
117+
118+
printf("Value at address 0x%X (%p): 0x%X\n", target, virt_addr, read_result);
105119
fflush(stdout);
106120

107-
if(argc > 3) {
108-
writeval = strtoul(argv[3], 0, 0);
109-
switch(access_type) {
110-
case 'b':
111-
*((unsigned char *) virt_addr) = writeval;
112-
read_result = *((unsigned char *) virt_addr);
113-
break;
114-
case 'h':
115-
*((unsigned short *) virt_addr) = writeval;
116-
read_result = *((unsigned short *) virt_addr);
117-
break;
118-
case 'w':
119-
*((unsigned long *) virt_addr) = writeval;
120-
read_result = *((unsigned long *) virt_addr);
121-
break;
122-
}
123-
printf("Written 0x%X; readback 0x%X\n", writeval, read_result);
124-
fflush(stdout);
125-
}
126-
127-
if(munmap(map_base, MAP_SIZE) == -1) FATAL;
121+
if (argc > 3) {
122+
writeval = strtoul(argv[3], 0, 0);
123+
124+
switch (access_type) {
125+
case 'b':
126+
*((unsigned char*) virt_addr) = writeval;
127+
read_result = *((unsigned char*) virt_addr);
128+
break;
129+
130+
case 'h':
131+
*((unsigned short*) virt_addr) = writeval;
132+
read_result = *((unsigned short*) virt_addr);
133+
break;
134+
135+
case 'w':
136+
*((unsigned long*) virt_addr) = writeval;
137+
read_result = *((unsigned long*) virt_addr);
138+
break;
139+
}
140+
141+
printf("Written 0x%X; readback 0x%X\n", writeval, read_result);
142+
fflush(stdout);
143+
}
144+
145+
if (munmap(map_base, MAP_SIZE) == -1) {
146+
FATAL;
147+
}
148+
128149
close(fd);
129150
return 0;
130151
}
Lines changed: 73 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,73 @@
1-
#include <stdio.h>
2-
#include <string.h>
3-
#include <signal.h>
4-
#include "stdlib.h"
5-
#include <fcntl.h>
6-
7-
8-
typedef struct iotest
9-
{
10-
int a;
11-
int b;
12-
char c[4];
13-
}iotest;
14-
15-
16-
17-
18-
int g_mainthread_running = 1;
19-
20-
void stop_handler( int signum )
21-
{
22-
printf("User press Ctrl+C to shut down the service.\n");
23-
g_mainthread_running = 0;
24-
exit(0);
25-
}
26-
27-
28-
29-
int main()
30-
{
31-
int fd;
32-
33-
char bufRead[10];
34-
int cn;
35-
36-
37-
signal(SIGINT, stop_handler);
38-
39-
40-
/*打开设备文件*/
41-
fd = open("/dev/chardev",O_RDWR);
42-
if (fd < 0) {
43-
printf("Open chardev Error!\n");
44-
return -1;
45-
}
46-
iotest ioctlTest;
47-
48-
while(g_mainthread_running)
49-
{
50-
cn = read(fd,bufRead,2);
51-
if(cn == 0)
52-
{
53-
break;
54-
}
55-
bufRead[cn] = '\0';
56-
// printf("read:%c,%c\n",bufRead[0],bufRead[1]);
57-
printf("read:%s\n",bufRead);
58-
sleep(1);
59-
60-
ioctl(fd,0,NULL);
61-
sleep(2);
62-
ioctl(fd,1,NULL);
63-
sleep(2);
64-
ioctlTest.a = 11;
65-
ioctlTest.b = 22;
66-
67-
ioctl(fd,0x22,&ioctlTest);
68-
printf("change:c[0]:%d,c[1]:%d,c[2]:%d,c[3]:%d",\
69-
ioctlTest.c[0],ioctlTest.c[1],ioctlTest.c[2],ioctlTest.c[3]);
70-
sleep(2);
71-
}
72-
73-
return 0;
74-
}
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <signal.h>
4+
#include "stdlib.h"
5+
#include <fcntl.h>
6+
7+
8+
typedef struct iotest {
9+
int a;
10+
int b;
11+
char c[4];
12+
} iotest;
13+
14+
15+
16+
17+
int g_mainthread_running = 1;
18+
19+
void stop_handler(int signum) {
20+
printf("User press Ctrl+C to shut down the service.\n");
21+
g_mainthread_running = 0;
22+
exit(0);
23+
}
24+
25+
26+
27+
int main() {
28+
int fd;
29+
30+
char bufRead[10];
31+
int cn;
32+
33+
34+
signal(SIGINT, stop_handler);
35+
36+
37+
/*打开设备文件*/
38+
fd = open("/dev/chardev", O_RDWR);
39+
40+
if (fd < 0) {
41+
printf("Open chardev Error!\n");
42+
return -1;
43+
}
44+
45+
iotest ioctlTest;
46+
47+
while (g_mainthread_running) {
48+
cn = read(fd, bufRead, 2);
49+
50+
if (cn == 0) {
51+
break;
52+
}
53+
54+
bufRead[cn] = '\0';
55+
// printf("read:%c,%c\n",bufRead[0],bufRead[1]);
56+
printf("read:%s\n", bufRead);
57+
sleep(1);
58+
59+
ioctl(fd, 0, NULL);
60+
sleep(2);
61+
ioctl(fd, 1, NULL);
62+
sleep(2);
63+
ioctlTest.a = 11;
64+
ioctlTest.b = 22;
65+
66+
ioctl(fd, 0x22, &ioctlTest);
67+
printf("change:c[0]:%d,c[1]:%d,c[2]:%d,c[3]:%d", \
68+
ioctlTest.c[0], ioctlTest.c[1], ioctlTest.c[2], ioctlTest.c[3]);
69+
sleep(2);
70+
}
71+
72+
return 0;
73+
}

0 commit comments

Comments
 (0)