Difference between revisions of "Hammer LED Userspace App"
Line 43: | Line 43: | ||
#include <stdio.h> | #include <stdio.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
− | |||
− | |||
#include <errno.h> | #include <errno.h> | ||
− | |||
#include <fcntl.h> | #include <fcntl.h> | ||
− | |||
− | |||
− | |||
#include <sys/mman.h> | #include <sys/mman.h> | ||
Line 59: | Line 53: | ||
#define MAP_MASK (MAP_SIZE - 1) | #define MAP_MASK (MAP_SIZE - 1) | ||
− | #define GPIOF_CON 0x56000050 | + | #define GPIOF_CON 0x56000050 /*physical address of the GPIO F control register */ |
− | #define GPIOF_DAT 0x56000054 | + | #define GPIOF_DAT 0x56000054 /*physical address of the GPIO F data register */ |
− | |||
int main(int argc, char **argv) { | int main(int argc, char **argv) { | ||
Line 68: | Line 61: | ||
unsigned long read_result, writeval; | unsigned long read_result, writeval; | ||
int count; | int count; | ||
+ | |||
+ | if(argc < 2) { | ||
+ | fprintf(stderr, "\nUsage:\t%s { seconds }\n" | ||
+ | "\tseconds : number of seconds to blink\n\n", | ||
+ | argv[0]); | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | count = strtoul(argv[1], 0, 0); | ||
/* open the memory device file descriptor */ | /* open the memory device file descriptor */ | ||
Line 93: | Line 95: | ||
virt_addr = map_base + (GPIOF_DAT & MAP_MASK); | virt_addr = map_base + (GPIOF_DAT & MAP_MASK); | ||
− | for ( count | + | for ( count ; count > 0 ; count--) { |
/* read the current value for GPIO F data register */ | /* read the current value for GPIO F data register */ | ||
Line 109: | Line 111: | ||
} | } | ||
+ | |||
+ | |||
+ | /* read the current value for GPIO F control register */ | ||
+ | read_result = *((unsigned long *) virt_addr); | ||
+ | |||
+ | /* make the LED is off when leaving the program */ | ||
+ | writeval = read_result; | ||
+ | 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; | ||
if(munmap(map_base, MAP_SIZE) == -1) FATAL; | if(munmap(map_base, MAP_SIZE) == -1) FATAL; | ||
Line 115: | Line 128: | ||
} | } | ||
+ | |||
</pre> | </pre> |
Revision as of 11:12, 12 May 2008
This is an example program that uses the /dev/mem device node to access the physical memory registers directly(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
the default setting in the source is to blink the LED for 30 seconds
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 <errno.h> #include <fcntl.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 /*physical address of the GPIO F control register */ #define GPIOF_DAT 0x56000054 /*physical address of the GPIO F data register */ int main(int argc, char **argv) { int fd; void *map_base, *virt_addr; unsigned long read_result, writeval; int count; if(argc < 2) { fprintf(stderr, "\nUsage:\t%s { seconds }\n" "\tseconds : number of seconds to blink\n\n", argv[0]); exit(1); } count = strtoul(argv[1], 0, 0); /* 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 ; count > 0 ; 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); } /* read the current value for GPIO F control register */ read_result = *((unsigned long *) virt_addr); /* make the LED is off when leaving the program */ writeval = read_result; 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; if(munmap(map_base, MAP_SIZE) == -1) FATAL; close(fd); return 0; }