Re: How to access the GPIO pins in AT91SAM9260-EK under Linux

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


Hi,

I don't know, how far you are by now. First you would not be able to
access direct addressing from the userspace. You can write a driver or use
some tool to remap your access to GPIO addresses. For the most part, it is
quite simple.

The other thing is that the linux dist. does not have all of the chip's io
definitions. I have had to add bunch of defines in the chip header. You
can look through the chip header and see how the io is used.

Roman


>
> Hi,
>
>        I am using AT91SAM9260-EK in which linux is ported also i wrote
> some sample application under linux and compiled using arm toolchain
> now i want to access the GPIO pins for blinking the led on the board i
> have written the code and if i try to run the executable file in the
> target board it will give error as segmentation fault and below is the
> code please help us it would be great pleasure.
>
>
> ********LED***************/
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <errno.h>
> #include <sys/shm.h>
>
> #include <sys/types.h>
> #include <sys/ioctl.h>
> #include <sys/mman.h>
> #include <sys/stat.h>
> #include <sys/time.h>
> #include <unistd.h>
> #include <fcntl.h>
> #include <strings.h>
> #include <string.h>
> #include <time.h>
>
> #include "AT91SAM9260.h"
>
> /* ********************** LOCAL DEFINES ****************************** */
> #define    LED_MAX    2
>
> /
> static unsigned ledSignalMapping[] = {AT91C_PIO_PA6,AT91C_PIO_PA9};
>
> #define    GPIO_LED_MASK (AT91C_PIO_PA6 | AT91C_PIO_PA9)
>
>
> static int memMapFile;
> static AT91PS_PIO at91SysCtrlr;
>
> /* ************************ UTILITY FUNCTIONS
> ******************************* */
>
>
> /*
>   * Set the LED output(s)
>   */
> static void UpdateLED(unsigned ledIndex, unsigned on)
> {
>      if (ledIndex >= LED_MAX)
>          /* invalid index */
>          return ;
>
>      /* remember that LED signals are active low */
>      if (on)
>      {
>          at91SysCtrlr->PIO_CODR = (ledSignalMapping[ledIndex]);
>      }
>      else
>      {
>          at91SysCtrlr->PIO_SODR = (ledSignalMapping[ledIndex]);
>      }
>
> }
>
>
> /* *************** HARDWARE SUPPORT FUNCTIONS ************************ */
>
>
> /*
>   * Access the hardware
>   */
> static int OpenSystemController(void)
> {
>      if ((memMapFile = open("/dev/mem", O_RDWR | O_SYNC)) < 0)
>      {
>          printf("ERROR: Unable to open /dev/mem\n");
>          return (errno);
>      }
>
>      at91SysCtrlr = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
>          MAP_SHARED, memMapFile, (unsigned)AT91C_BASE_SYS);
>
>      if (at91SysCtrlr == MAP_FAILED)
>      {
>          printf("ERROR: Unable to mmap the system controller\n");
>          close (memMapFile);
>          memMapFile = -1;
>          return (errno);
>      }
>
>      /* set LED signals for output */
>      /* set digital output ports init value = all on (active low) */
>      at91SysCtrlr->PIO_CODR    = GPIO_LED_MASK;
>      at91SysCtrlr->PIO_PPUDR= GPIO_LED_MASK;
>      at91SysCtrlr->PIO_PER    = GPIO_LED_MASK;
>      at91SysCtrlr->PIO_OER    = GPIO_LED_MASK;
>
>
>      return (0);
> }
>
>
> /* ************************ MAIN LOOPS ******************************* */
>
>
> int main(int argc, char **argv)
> {
>      unsigned    ledIndex;
>
>
>      if (OpenSystemController())
>      {
>          printf("Unable to map hardware resources\n");
>          return (-1);
>      }
>
>      ledIndex = 0;
>      while (1)
>      {
>          /* loop forever toggling LEDs at 1 second interval */
>
>          /* turn on */
>          UpdateLED(ledIndex, 1);
>          printf ("LED %d is now on\n", ledIndex);
>
>          /* wait for 0.5 seconds */
>          usleep(500000);
>
>          /* turn off */
>          UpdateLED(ledIndex, 0);
>          printf ("LED %d is now off\n", ledIndex);
>
>          /* wait for 0.5 seconds */
>          usleep(500000);
>
>          /* reset index to first LED if we finished all of them */
>          if (++ledIndex >= LED_MAX)
>              ledIndex = 0;
>      }
>
>      return (0);
> }
>
>
> Sandeep G R,
> Design Engineer,
> Drass Mechatronics Pvt Ltd,
> #304-5/M, Seetharam Industrial Estate,
> MES Ring Road, Jalahalli,
> Bangaalore-560013.
> India
>
>
>
>
>
>
> -------------------------------------------------------------------
> List admin: http://lists.arm.linux.org.uk/mailman/listinfo/linux-arm
> FAQ:        http://www.arm.linux.org.uk/mailinglists/faq.php
> Etiquette:  http://www.arm.linux.org.uk/mailinglists/etiquette.php
>


-------------------------------------------------------------------
List admin: http://lists.arm.linux.org.uk/mailman/listinfo/linux-arm
FAQ:        http://www.arm.linux.org.uk/mailinglists/faq.php
Etiquette:  http://www.arm.linux.org.uk/mailinglists/etiquette.php

[Linux ARM]     [Linux ARM MSM]     [Linux ARM Kernel]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]

Add to Google Follow linuxarm on Twitter