Difference between revisions of "Hammer LED Userspace App"

From eLinux.org
Jump to: navigation, search
(Change to Category TCT-Hammer)
 
(5 intermediate revisions by 2 users not shown)
Line 13: Line 13:
 
#chmod 755 ledblink
 
#chmod 755 ledblink
 
#./ledblink
 
#./ledblink
 +
 +
Usage:  ./ledblink { seconds }
 +
        seconds : number of seconds to blink
 +
 +
#./ledblink 20
 
</pre>
 
</pre>
  
the default setting in the source is to blink the LED for 30 seconds
+
this will cause the onboard LED to blink for 20 seconds
 +
 
  
 
==Source==
 
==Source==
Line 43: Line 49:
 
#include <stdio.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <stdlib.h>
#include <errno.h>
 
 
#include <fcntl.h>
 
#include <fcntl.h>
 
#include <sys/mman.h>
 
#include <sys/mman.h>
 
    
 
    
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
+
#define MAP_SIZE 4096UL /* the size of one page of virtual memory */
  __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
+
#define MAP_MASK (MAP_SIZE - 1) /* mask used for the one page of virtual memory */
+
#define MAP_SIZE 4096UL
+
#define MAP_MASK (MAP_SIZE - 1)
+
  
#define GPIOF_CON 0x56000050 /*physical address of the GPIO F control register */
+
#define GPIOF_CON 0x56000050 /* physical address of the GPIO F control register */
#define GPIOF_DAT 0x56000054 /*physical address of the GPIO F data register */
+
#define GPIOF_DAT 0x56000054 /* physical address of the GPIO F data register */
  
 
int main(int argc, char **argv) {
 
int main(int argc, char **argv) {
     int fd;
+
     int fd,count;
 
     void *map_base, *virt_addr;  
 
     void *map_base, *virt_addr;  
unsigned long read_result, writeval;
+
    unsigned long read_result, writeval;
    int count;
+
  
 
     if(argc < 2) {
 
     if(argc < 2) {
Line 72: Line 73:
  
 
     /* open the memory device file descriptor */
 
     /* open the memory device file descriptor */
     if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
+
     if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
 +
return -1;
 
      
 
      
 
     /* Map one page */
 
     /* Map one page */
 
     map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIOF_CON & ~MAP_MASK);
 
     map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIOF_CON & ~MAP_MASK);
     if(map_base == (void *) -1) FATAL;
+
     if(map_base == (void *) -1)
 +
return -1;
 
      
 
      
 
     /* set the virtual memory address to the GPIO F control register */
 
     /* set the virtual memory address to the GPIO F control register */
Line 116: Line 119:
 
     read_result = *((unsigned long *) virt_addr);
 
     read_result = *((unsigned long *) virt_addr);
  
     /* make the LED is off when leaving the program */
+
     /* make sure the LED is off when leaving the program */
 
     writeval = read_result;
 
     writeval = read_result;
 
     writeval |= (1<<0); /* set GPIO F0 to a value of 1(high-off) */  
 
     writeval |= (1<<0); /* set GPIO F0 to a value of 1(high-off) */  
Line 122: Line 125:
 
     /* write the modified value to the GPIO F0 data register */
 
     /* write the modified value to the GPIO F0 data register */
 
     *((unsigned long *) virt_addr) = writeval;
 
     *((unsigned long *) virt_addr) = writeval;
+
 
if(munmap(map_base, MAP_SIZE) == -1) FATAL;
+
    /* unmap the virtual memory before exiting the program */
 +
    if(munmap(map_base, MAP_SIZE) == -1)
 +
return -1;
 +
   
 
     close(fd);
 
     close(fd);
 
     return 0;
 
     return 0;
Line 130: Line 136:
  
 
</pre>
 
</pre>
 +
 +
[[Category:TCT-Hammer]]

Latest revision as of 16:48, 14 May 2011

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

Usage:  ./ledblink { seconds }
        seconds : number of seconds to blink

#./ledblink 20

this will cause the onboard LED to blink for 20 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 <fcntl.h>
#include <sys/mman.h>
  
#define MAP_SIZE 4096UL		/* the size of one page of virtual memory */
#define MAP_MASK (MAP_SIZE - 1)	/* mask used for the one page of virtual memory */

#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,count;
    void *map_base, *virt_addr; 
    unsigned long read_result, writeval;

    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)
	return -1;
    
    /* 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)
	return -1;
    
    /* 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 sure 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;

    /* unmap the virtual memory before exiting the program */
    if(munmap(map_base, MAP_SIZE) == -1)
	return -1;
	    
    close(fd);
    return 0;

}