lab2-2-3.c
840 Bytes
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
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#define GPIO_BASE 0x3F200000
#define GPFSEL1 0x04
#define GPSET0 0x1C
#define GPCLR0 0x28
int main() {
int fd = open( "/dev/mem", O_RDWR|O_SYNC );
if ( fd < 0 ){
printf( "can't open /dev/mem \n" );
exit(-1);
}
char *gpio_memory_map
= (char *)mmap(0, 4096, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, GPIO_BASE );
if ( gpio_memory_map == MAP_FAILED ) {
printf( " Error : mmap \n" );
exit(-1);
}
volatile unsigned int* gpio
= (volatile unsigned int*)gpio_memory_map;
gpio[GPFSEL1/4] = (1<<24);
int i;
for ( i=0; i<5; i++ ) {
gpio[GPCLR0/4] = (1<<18);
sleep(1);
gpio[GPSET0/4] = (1<<18);
sleep(1);
}
munmap( gpio_memory_map, 4096);
return 0;
}