Difference between revisions of "Hammer LED Userspace App"
From eLinux.org
m |
|||
| Line 9: | Line 9: | ||
</pre> | </pre> | ||
| − | then transfer the ledblink binary to the hammer file system. make sure that the permissions on the file are set to executable | + | then transfer the ledblink binary to the hammer file system. make sure that the permissions on the file are set to executable. |
<pre> | <pre> | ||
#chmod 755 ledblink | #chmod 755 ledblink | ||
| + | #./ledblink | ||
| + | </pre> | ||
| + | |||
| + | |||
| + | ==Source== | ||
| + | <pre> | ||
| + | /* | ||
| + | * ledblink.c: Simple program to blink a led on GPIO F0 | ||
| + | * | ||
| + | * Copyright (c) 2008 TinCanTools | ||
| + | * David Anders <danders@amltd.com> | ||
| + | * | ||
| + | * This program is free software; you can redistribute it and/or modify | ||
| + | * it under the terms of the GNU General Public License as published by | ||
| + | * the Free Software Foundation; either version 2 of the License, or | ||
| + | * (at your option) any later version. | ||
| + | * | ||
| + | * This program is distributed in the hope that it will be useful, | ||
| + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| + | * GNU General Public License for more details. | ||
| + | * | ||
| + | * You should have received a copy of the GNU General Public License | ||
| + | * along with this program; if not, write to the Free Software | ||
| + | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| + | * | ||
| + | */ | ||
| + | |||
| + | #include <stdio.h> | ||
| + | #include <stdlib.h> | ||
| + | #include <unistd.h> | ||
| + | #include <string.h> | ||
| + | #include <errno.h> | ||
| + | #include <signal.h> | ||
| + | #include <fcntl.h> | ||
| + | #include <ctype.h> | ||
| + | #include <termios.h> | ||
| + | #include <sys/types.h> | ||
| + | #include <sys/mman.h> | ||
| + | |||
| + | #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ | ||
| + | __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) | ||
| + | |||
| + | #define MAP_SIZE 4096UL | ||
| + | #define MAP_MASK (MAP_SIZE - 1) | ||
| + | |||
| + | #define GPIOF_CON 0x56000050 | ||
| + | #define GPIOF_DAT 0x56000054 | ||
| + | #define SECONDS 30 | ||
| + | |||
| + | int main(int argc, char **argv) { | ||
| + | int fd; | ||
| + | void *map_base, *virt_addr; | ||
| + | unsigned long read_result, writeval; | ||
| + | int count; | ||
| + | |||
| + | /* open the memory device file descriptor */ | ||
| + | if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL; | ||
| + | |||
| + | /* Map one page */ | ||
| + | map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIOF_CON & ~MAP_MASK); | ||
| + | if(map_base == (void *) -1) FATAL; | ||
| + | |||
| + | /* set the virtual memory address to the GPIO F control register */ | ||
| + | virt_addr = map_base + (GPIOF_CON & MAP_MASK); | ||
| + | |||
| + | /* read the current value for GPIO F control register */ | ||
| + | read_result = *((unsigned long *) virt_addr); | ||
| + | |||
| + | writeval = read_result; | ||
| + | writeval &= ~0x03; /* mask off the first two bits */ | ||
| + | writeval |= 0x01; /* set gpio f0 to output with value of 0x01 */ | ||
| + | |||
| + | /* write the modified value to GPIO F0 control register to set GPIO F0 to output */ | ||
| + | *((unsigned long *) virt_addr) = writeval; | ||
| + | |||
| + | |||
| + | /* change virtual memory address to GPIO F data register */ | ||
| + | virt_addr = map_base + (GPIOF_DAT & MAP_MASK); | ||
| + | |||
| + | for ( count=0; count < SECONDS ; count++) { | ||
| + | |||
| + | /* read the current value for GPIO F data register */ | ||
| + | read_result = *((unsigned long *) virt_addr); | ||
| + | writeval = read_result; | ||
| + | |||
| + | if ( (read_result&(1<<0)) ) | ||
| + | writeval &= ~(1<<0); /* set GPIO F0 to a value of 0(low-on) */ | ||
| + | else | ||
| + | writeval |= (1<<0); /* set GPIO F0 to a value of 1(high-off) */ | ||
| + | |||
| + | /* write the modified value to the GPIO F0 data register */ | ||
| + | *((unsigned long *) virt_addr) = writeval; | ||
| + | sleep(1); | ||
| + | |||
| + | } | ||
| + | |||
| + | if(munmap(map_base, MAP_SIZE) == -1) FATAL; | ||
| + | close(fd); | ||
| + | return 0; | ||
| + | |||
| + | } | ||
</pre> | </pre> | ||
Revision as of 16:59, 12 May 2008
This is an example program that uses the /dev/mem device node to access the physical memory registers directly([[media:ledblink.c|source)
Usage
to compile the source use your toolchain command line
#arm-linux-uclibc-gcc -o ledblink ledblink.c
then transfer the ledblink binary to the hammer file system. make sure that the permissions on the file are set to executable.
#chmod 755 ledblink #./ledblink
Source
/*
* ledblink.c: Simple program to blink a led on GPIO F0
*
* Copyright (c) 2008 TinCanTools
* David Anders <danders@amltd.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
#define GPIOF_CON 0x56000050
#define GPIOF_DAT 0x56000054
#define SECONDS 30
int main(int argc, char **argv) {
int fd;
void *map_base, *virt_addr;
unsigned long read_result, writeval;
int count;
/* open the memory device file descriptor */
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
/* Map one page */
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIOF_CON & ~MAP_MASK);
if(map_base == (void *) -1) FATAL;
/* set the virtual memory address to the GPIO F control register */
virt_addr = map_base + (GPIOF_CON & MAP_MASK);
/* read the current value for GPIO F control register */
read_result = *((unsigned long *) virt_addr);
writeval = read_result;
writeval &= ~0x03; /* mask off the first two bits */
writeval |= 0x01; /* set gpio f0 to output with value of 0x01 */
/* write the modified value to GPIO F0 control register to set GPIO F0 to output */
*((unsigned long *) virt_addr) = writeval;
/* change virtual memory address to GPIO F data register */
virt_addr = map_base + (GPIOF_DAT & MAP_MASK);
for ( count=0; count < SECONDS ; count++) {
/* read the current value for GPIO F data register */
read_result = *((unsigned long *) virt_addr);
writeval = read_result;
if ( (read_result&(1<<0)) )
writeval &= ~(1<<0); /* set GPIO F0 to a value of 0(low-on) */
else
writeval |= (1<<0); /* set GPIO F0 to a value of 1(high-off) */
/* write the modified value to the GPIO F0 data register */
*((unsigned long *) virt_addr) = writeval;
sleep(1);
}
if(munmap(map_base, MAP_SIZE) == -1) FATAL;
close(fd);
return 0;
}