Exemplo n.º 1
0
static void doI2Cdetect (int argc, char *argv [])
{
    int port = piBoardRev () == 1 ? 0 : 1 ;
    char command [128] ;
    struct stat statBuf ;

    //add for S500
    if( S500_REV == piBoardRev())
    {
        if (stat (I2CDETECT, &statBuf) < 0)
        {
            fprintf (stderr, "%s: Unable to find i2cdetect command: %s\n", argv [0], strerror (errno)) ;
            return ;
        }

        if (!moduleLoaded ("i2c-owl"))
        {
            fprintf (stderr, "%s: The I2C kernel module(s) are not loaded.\n", argv [0]) ;
        }

        sprintf (command, "%s -y %d", I2CDETECT, 2) ;
        if (system (command) < 0)
            fprintf (stderr, "%s: Unable to run i2cdetect: %s\n", argv [0], strerror (errno)) ;
    }
    /*add for BananaPro by LeMaker team*/
    else if(BP_REV == piBoardRev())
    {
        if (stat (I2CDETECT, &statBuf) < 0)
        {
            fprintf (stderr, "%s: Unable to find i2cdetect command: %s\n", argv [0], strerror (errno)) ;
            return ;
        }

        if (!moduleLoaded ("i2c-sunxi"))
        {
            fprintf (stderr, "%s: The I2C kernel module(s) are not loaded.\n", argv [0]) ;
            //return ;
        }

        sprintf (command, "%s -y %d", I2CDETECT, 2) ;
        if (system (command) < 0)
            fprintf (stderr, "%s: Unable to run i2cdetect: %s\n", argv [0], strerror (errno)) ;
    }
    else
    {

    }
    /*end 2014.08.19*/
}
Exemplo n.º 2
0
static int raspberrypiI2CSetup(int devId) {
	int rev = 0, fd = 0;
	const char *device = NULL;

	if((rev = piBoardRev()) < 0) {
		wiringXLog(LOG_ERR, "raspberrypi->I2CSetup: Unable to determine Pi board revision");
		return -1;
	}

	if(rev == 1)
		device = "/dev/i2c-0";
	else
		device = "/dev/i2c-1";

	if((fd = open(device, O_RDWR)) < 0) {
		wiringXLog(LOG_ERR, "raspberrypi->I2CSetup: Unable to open %s: %s", device, strerror(errno));
		return -1;
	}

	if(ioctl(fd, I2C_SLAVE, devId) < 0) {
		wiringXLog(LOG_ERR, "raspberrypi->I2CSetup: Unable to set %s to slave mode: %s", device, strerror(errno));
		return -1;
	}

	return fd;
}
Exemplo n.º 3
0
static void doReadall (void)
{
  int pin ;

  if (wiringPiNodes != NULL)	// External readall
    doReadallExternal () ;
  else
  {
    printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ;
    printf ("| wiringPi | GPIO | Phys | Name   | Mode | Value |\n") ;
    printf ("+----------+------+------+--------+------+-------+\n") ;

    for (pin = 0 ; pin < 64 ; ++pin)	// Crude, but effective
    {
      if (wpiPinToGpio (pin) == -1)
	continue ;

      printf ("| %6d   | %3d  | %3d  | %s | %s | %s  |\n",
	  pin, wpiPinToGpio (pin), wpiToPhys [pin],
	  pinNames [pin], 
	  alts [getAlt (pin)], 
	  digitalRead (pin) == HIGH ? "High" : "Low ") ;
    }

    printf ("+----------+------+------+--------+------+-------+\n") ;
  }
}
Exemplo n.º 4
0
static void doReadall (void)
{
  int pin ;

  printf ("+----------+------+--------+-------+\n") ;
  printf ("| wiringPi | GPIO | Name   | Value |\n") ;
  printf ("+----------+------+--------+-------+\n") ;

  for (pin = 0 ; pin < NUM_PINS ; ++pin)
    printf ("| %6d   | %3d  | %s | %s  |\n",
	pin, wpiPinToGpio (pin),
	pinNames [pin], 
	digitalRead (pin) == HIGH ? "High" : "Low ") ;

  printf ("+----------+------+--------+-------+\n") ;

  if (piBoardRev () == 1)
    return ;

  for (pin = 17 ; pin <= 20 ; ++pin)
    printf ("| %6d   | %3d  | %s | %s  |\n",
	pin, wpiPinToGpio (pin),
	pinNames [pin], 
	digitalRead (pin) == HIGH ? "High" : "Low ") ;

  printf ("+----------+------+--------+-------+\n") ;
}
Exemplo n.º 5
0
/* piBoardRev
 *
 * Parameters: None
 * Return Type: int
 */
mrb_value
mrb_Pi_piBoardRev(mrb_state* mrb, mrb_value self) {
  /* Invocation */
  int result = piBoardRev();

  /* Box the return value */
  mrb_value return_value = mrb_fixnum_value(result);

  return return_value;
}
Exemplo n.º 6
0
void doReadall (void)
{
  int pin ;

  if (wiringPiNodes != NULL)	// External readall
  {
    doReadallExternal () ;
    return ;
  }

  if (cmReadall ())
    return ;

  /**/ if (wpMode == WPI_MODE_GPIO)
  {
    printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ;
    printf (" | BCM |  Name | Mode | Val| Physical |Val | Mode | Name  | BCM |\n") ;
    printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
    for (pin = 1 ; pin <= 26 ; pin += 2)
      readallPhys (pin) ;
    printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
  }
  else if (wpMode == WPI_MODE_PHYS)
  {
    printf (" +-------+------+----+-Rev%d-----+----+------+-------+\n", piBoardRev ()) ;
    printf (" |  Name | Mode | Val| Physical |Val | Mode | Name  |\n") ;
    printf (" +-------+------+----+----++----+----+------+-------+\n") ;
    for (pin = 1 ; pin <= 26 ; pin += 2)
      readallPhys (pin) ;
    printf (" +-------+------+----+----++----+----+------+-------+\n") ;
  }
  else	// wiringPi
  {
    printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ;
    printf (" | wPi |  Name | Mode | Val| Physical |Val | Mode | Name  | wPi |\n") ;
    printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
    for (pin = 1 ; pin <= 26 ; pin += 2)
      readallPhys (pin) ;
    printf (" +-----+-------+------+----+----++----+----+------+-------+-----+\n") ;
  }
}
Exemplo n.º 7
0
int wiringPiSetupSys (void)
{
  int boardRev ;
  int pin ;
  struct timeval tv ;
  char fName [128] ;

  if (getenv ("WIRINGPI_DEBUG") != NULL)
    wiringPiDebug = TRUE ;

  if (wiringPiDebug)
    printf ("wiringPi: wiringPiSetupSys called\n") ;

            pinMode =           pinModeSys ;
    pullUpDnControl =   pullUpDnControlSys ;
       digitalWrite =      digitalWriteSys ;
   digitalWriteByte =  digitalWriteByteSys ;
           pwmWrite =          pwmWriteSys ;
        setPadDrive =       setPadDriveSys ;
        digitalRead =       digitalReadSys ;
   waitForInterrupt =  waitForInterruptSys ;
  delayMicroseconds = delayMicrosecondsSys ;
         pwmSetMode =        pwmSetModeSys ;
        pwmSetRange =       pwmSetRangeSys ;
        pwmSetClock =       pwmSetClockSys ;

  boardRev = piBoardRev () ;

  if (boardRev == 1)
    pinToGpio = pinToGpioR1 ;
  else
    pinToGpio = pinToGpioR2 ;

// Open and scan the directory, looking for exported GPIOs, and pre-open
//	the 'value' interface to speed things up for later
  
  for (pin = 0 ; pin < 64 ; ++pin)
  {
    sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
    sysFds [pin] = open (fName, O_RDWR) ;
  }

// Initialise the epoch for mills() ...

  gettimeofday (&tv, NULL) ;
  epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;

  wiringPiMode = WPI_MODE_GPIO_SYS ;

  return 0 ;
}
Exemplo n.º 8
0
int wiringPiI2CSetup (const int devId)
{
    int rev ;
    const char *device ;

    rev = piBoardRev () ;

    if (rev == 1)
        device = "/dev/i2c-0" ;
    else
        device = "/dev/i2c-1" ;

    return wiringPiI2CSetupInterface (device, devId) ;
}
Exemplo n.º 9
0
int main (void)
{
  printf ("Raspberry Pi Quick2Wire blink board rev %d\n", piBoardRev()) ;

  // wiringPiSetup () ;
  wiringPiSetupGpio();

  pinMode (LED_GPIO, OUTPUT) ;

  for (;;)
  {
    digitalWrite (LED_GPIO, HIGH) ;  // On
    delay (100) ;               // mS
    digitalWrite (LED_GPIO, LOW) ;   // Off
    delay (100) ;
  }
  return 0 ;
}
Exemplo n.º 10
0
Arquivo: misc.c Projeto: M0NRD/Hamfest
short open_i2c(int address)
{
	short fd;
	char i2c_dev[16];

	sprintf(i2c_dev, "/dev/i2c-%d", piBoardRev()-1);

	if ((fd = open(i2c_dev, O_RDWR)) < 0)
	{                                        // Open port for reading and writing
		printf("Failed to open i2c port\n");
		return 0;
	}

	if (ioctl(fd, I2C_SLAVE, address) < 0)                                 // Set the port options and set the address of the device we wish to speak to
	{
		printf("Unable to get bus access to talk to slave on address %02Xh\n", address);
		return 0;
	}

	return fd;
}
Exemplo n.º 11
0
static int setup(void) {
	int fd;
	int boardRev;
	int model, rev, mem, maker, overVolted;

	boardRev = piBoardRev();

	if(boardRev == 1) {
		pinToGpio =  pinToGpioR1;
		physToGpio = physToGpioR1;
	} else {
		if(piModel2 == 1) {
			BCM2708_PERI_BASE = 0x3F000000;
		}
		pinToGpio =  pinToGpioR2;
		physToGpio = physToGpioR2;
	}

	if((fd = open("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0) {
		wiringXLog(LOG_ERR, "raspberrypi->setup: Unable to open /dev/mem: %s", strerror(errno));
		return -1;
	}

	gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE);
	if((int32_t)gpio == -1) {
		wiringXLog(LOG_ERR, "raspberrypi->setup: mmap (GPIO) failed: %s", strerror(errno));
		return -1;
	}

	if(piBoardId(&model, &rev, &mem, &maker, &overVolted) == -1) {
		return -1;
	}
	if(model == PI_MODEL_CM) {
		wiringPiMode = WPI_MODE_GPIO;
	} else {
		wiringPiMode = WPI_MODE_PINS;
	}

	return 0;
}
Exemplo n.º 12
0
static void doReadall (void)
{
  int pin ;

  printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ;
  printf ("| wiringPi | GPIO | Phys | Name   | Mode | Value |\n") ;
  printf ("+----------+------+------+--------+------+-------+\n") ;

  for (pin = 0 ; pin < 64 ; ++pin)
  {
    if (wpiPinToGpio (pin) == -1)
      continue ;

    printf ("| %6d   | %3d  | %3d  | %s | %s | %s  |\n",
	pin, wpiPinToGpio (pin), wpiToPhys [pin],
	pinNames [pin], 
	alts [getAlt (pin)], 
	digitalRead (pin) == HIGH ? "High" : "Low ") ;
  }

  printf ("+----------+------+------+--------+------+-------+\n") ;
}
Exemplo n.º 13
0
int i2c_openadapter(uint8_t i2c_bus)
{
	// printf ("%d\n", i2c_bus);
	int id = getI2CId();
	// i2c_buses[id] = wiringPiI2CSetup (i2c_bus) ;
	int d = 0;
	if (piBoardRev ()==1)
	{
		d = 0;
	}
	else
	{
		d = 1;
	}
	char filepath[32];
    snprintf(filepath, 32, "/dev/i2c-%u", d);
    if ((i2c_buses[i2c_bus] = open(filepath, O_RDWR)) < 1) {
        fprintf(stderr, "Failed to open requested i2c port %s\n", filepath);
    }
	//perror ("set");
	return id;
}
Exemplo n.º 14
0
int wiringPiI2CSetup (const int devId)
{
  const char *device ;
  int model, rev, mem, maker, overVolted ;

  piBoardId (&model, &rev, &mem, &maker, &overVolted) ;

  if      ( model == PI_MODEL_ODROIDC )
    device = "/dev/i2c-1" ;
  else if ( model == PI_MODEL_ODROIDXU_34 )
    device = "/dev/i2c-3" ;
  else  {
    rev = piBoardRev () ;

    if (rev == 1)
      device = "/dev/i2c-0" ;
    else
      device = "/dev/i2c-1" ;
  }

  return wiringPiI2CSetupInterface (device, devId) ;
}
Exemplo n.º 15
0
static void doI2Cdetect (int argc, char *argv [])
{
  int port = piBoardRev () == 1 ? 0 : 1 ;
  char command [128] ;
  struct stat statBuf ;

  if (stat (I2CDETECT, &statBuf) < 0)
  {
    fprintf (stderr, "%s: Unable to find i2cdetect command: %s\n", argv [0], strerror (errno)) ;
    return ;
  }

  if (!moduleLoaded ("i2c_dev"))
  {
    fprintf (stderr, "%s: The I2C kernel module(s) are not loaded.\n", argv [0]) ;
    return ;
  }

  sprintf (command, "%s -y %d", I2CDETECT, port) ;
  if (system (command) < 0)
    fprintf (stderr, "%s: Unable to run i2cdetect: %s\n", argv [0], strerror (errno)) ;

}
Exemplo n.º 16
0
int wiringPiI2CSetup (int devId)
{
  int rev, fd ;
  char *device ;

  if ((rev = piBoardRev ()) < 0)
  {
    fprintf (stderr, "wiringPiI2CSetup: Unable to determine Pi board revision\n") ;
    exit (1) ;
  }

  if (rev == 1)
    device = "/dev/i2c-0" ;
  else
    device = "/dev/i2c-1" ;

  if ((fd = open (device, O_RDWR)) < 0)
    return -1 ;

  if (ioctl (fd, I2C_SLAVE, devId) < 0)
    return -1 ;

  return fd ;
}
Exemplo n.º 17
0
int main (int argc, char *argv [])
{
  int i ;

  if (getenv ("WIRINGPI_DEBUG") != NULL)
  {
    printf ("gpio: wiringPi debug mode enabled\n") ;
    wiringPiDebug = TRUE ;
  }

  if (argc == 1)
  {
    fprintf (stderr, "%s\n", usage) ;
    return 1 ;
  }

// Help

  if (strcasecmp (argv [1], "-h") == 0)
  {
    printf ("%s: %s\n", argv [0], usage) ;
    return 0 ;
  }

// Sort of a special:

  if (strcmp (argv [1], "-R") == 0)
  {
    printf ("%d\n", piBoardRev ()) ;
    return 0 ;
  }

// Version & Warranty

  if (strcmp (argv [1], "-V") == 0)
  {
    printf ("%d\n", piBoardRev ()) ;
    return 0 ;
  }

  if (strcmp (argv [1], "-v") == 0)
  {
    printf ("gpio version: %s\n", VERSION) ;
    printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ;
    printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
    printf ("For details type: %s -warranty\n", argv [0]) ;
    printf ("\n") ;
    printf ("This Raspberry Pi is a revision %d board.\n", piBoardRev ()) ;
    return 0 ;
  }

  if (strcasecmp (argv [1], "-warranty") == 0)
  {
    printf ("gpio version: %s\n", VERSION) ;
    printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ;
    printf ("\n") ;
    printf ("    This program is free software; you can redistribute it and/or modify\n") ;
    printf ("    it under the terms of the GNU Leser General Public License as published\n") ;
    printf ("    by the Free Software Foundation, either version 3 of the License, or\n") ;
    printf ("    (at your option) any later version.\n") ;
    printf ("\n") ;
    printf ("    This program is distributed in the hope that it will be useful,\n") ;
    printf ("    but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
    printf ("    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n") ;
    printf ("    GNU Lesser General Public License for more details.\n") ;
    printf ("\n") ;
    printf ("    You should have received a copy of the GNU Lesser General Public License\n") ;
    printf ("    along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
    printf ("\n") ;
    return 0 ;
  }

  if (geteuid () != 0)
  {
    fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
    return 1 ;
  }

// Initial test for /sys/class/gpio operations:

  /**/ if (strcasecmp (argv [1], "exports"    ) == 0)	{ doExports     (argc, argv) ;	return 0 ; }
  else if (strcasecmp (argv [1], "export"     ) == 0)	{ doExport      (argc, argv) ;	return 0 ; }
  else if (strcasecmp (argv [1], "edge"       ) == 0)	{ doEdge        (argc, argv) ;	return 0 ; }
  else if (strcasecmp (argv [1], "unexport"   ) == 0)	{ doUnexport    (argc, argv) ;	return 0 ; }
  else if (strcasecmp (argv [1], "unexportall") == 0)	{ doUnexportall (argv [0]) ;	return 0 ; }

// Check for load command:

  if (strcasecmp (argv [1], "load" ) == 0)	{ doLoad     (argc, argv) ; return 0 ; }

// Gertboard commands

  if (strcasecmp (argv [1], "gbr" ) == 0)	{ doGbr (argc, argv) ; return 0 ; }
  if (strcasecmp (argv [1], "gbw" ) == 0)	{ doGbw (argc, argv) ; return 0 ; }

// Check for -g argument

  /**/ if (strcasecmp (argv [1], "-g") == 0)
  {
    wiringPiSetupGpio () ;

    for (i = 2 ; i < argc ; ++i)
      argv [i - 1] = argv [i] ;
    --argc ;
    wpMode = WPI_MODE_GPIO ;
  }

// Check for -1 argument

  else if (strcasecmp (argv [1], "-1") == 0)
  {
    wiringPiSetupPhys () ;

    for (i = 2 ; i < argc ; ++i)
      argv [i - 1] = argv [i] ;
    --argc ;
    wpMode = WPI_MODE_PHYS ;
  }

// Check for -p argument for PiFace

  else if (strcasecmp (argv [1], "-p") == 0)
  {
    piFaceSetup (200) ;

    for (i = 2 ; i < argc ; ++i)
      argv [i - 1] = argv [i] ;
    --argc ;
    wpMode = WPI_MODE_PIFACE ;
  }

// Default to wiringPi mode

  else
  {
    wiringPiSetup () ;
    wpMode = WPI_MODE_PINS ;
  }

// Check for -x argument to load in a new extension

  if (strcasecmp (argv [1], "-x") == 0)
  {
    if (argc < 3)
    {
      fprintf (stderr, "%s: -x missing extension specification.\n", argv [0]) ;
      exit (EXIT_FAILURE) ;
    }

    if (!doExtension (argv [0], argv [2]))	// Prints its own error messages
      exit (EXIT_FAILURE) ;

    for (i = 3 ; i < argc ; ++i)
      argv [i - 2] = argv [i] ;
    argc -= 2 ;
  }

  if (argc <= 1)
  {
    fprintf (stderr, "%s: no command given\n", argv [0]) ;
    exit (EXIT_FAILURE) ;
  }

// Core wiringPi functions

  /**/ if (strcasecmp (argv [1], "mode"   ) == 0) doMode      (argc, argv) ;
  else if (strcasecmp (argv [1], "read"   ) == 0) doRead      (argc, argv) ;
  else if (strcasecmp (argv [1], "write"  ) == 0) doWrite     (argc, argv) ;
  else if (strcasecmp (argv [1], "pwm"    ) == 0) doPwm       (argc, argv) ;
  else if (strcasecmp (argv [1], "awrite" ) == 0) doAwrite    (argc, argv) ;
  else if (strcasecmp (argv [1], "aread"  ) == 0) doAread     (argc, argv) ;

// GPIO Nicies

  else if (strcasecmp (argv [1], "toggle" ) == 0) doToggle    (argc, argv) ;

// Pi Specifics

  else if (strcasecmp (argv [1], "pwm-bal"  ) == 0) doPwmMode   (PWM_MODE_BAL) ;
  else if (strcasecmp (argv [1], "pwm-ms"   ) == 0) doPwmMode   (PWM_MODE_MS) ;
  else if (strcasecmp (argv [1], "pwmr"     ) == 0) doPwmRange  (argc, argv) ;
  else if (strcasecmp (argv [1], "pwmc"     ) == 0) doPwmClock  (argc, argv) ;
  else if (strcasecmp (argv [1], "drive"    ) == 0) doPadDrive  (argc, argv) ;
  else if (strcasecmp (argv [1], "readall"  ) == 0) doReadall   () ;
  else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ;
  else if (strcasecmp (argv [1], "i2cd"     ) == 0) doI2Cdetect (argc, argv) ;
  else if (strcasecmp (argv [1], "reset"    ) == 0) doReset     (argv [0]) ;
  else if (strcasecmp (argv [1], "wb"       ) == 0) doWriteByte (argc, argv) ;
  else if (strcasecmp (argv [1], "clock"    ) == 0) doClock     (argc, argv) ;
  else if (strcasecmp (argv [1], "wfi"      ) == 0) doWfi       (argc, argv) ;
  else
  {
    fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
    exit (EXIT_FAILURE) ;
  }
  return 0 ;
}
Exemplo n.º 18
0
static void doLoad (int argc, char *argv [])
{
    char *module1, *module2 ;
    char cmd [80] ;
    char *file1, *file2 ;
    char args1 [32], args2 [32] ;

    if (argc < 3)
        _doLoadUsage (argv) ;

    args1 [0] = args2 [0] = 0 ;

    //add for S500
    if(S500_REV == piBoardRev())
    {
        if (strcasecmp (argv [2], "spi") == 0)
        {
            module1 = "spidev" ;
            module2 = "spi-owl" ;
            file1  = "/dev/spidev0.0" ;
            file2  = "/dev/spidev0.1" ;
            if (argc == 4)
                sprintf (args1, " bufsiz=%d", atoi (argv [3]) * 1024) ;
            else if (argc > 4)
                _doLoadUsage (argv) ;
        }
        else if (strcasecmp (argv [2], "i2c") == 0)
        {
            module1 = "i2c_dev" ;
            module2 = "i2c-owl" ;
            file1  = "/dev/i2c-2" ;
            file2  = "/dev/i2c-2" ;
            if (argc == 4)
                sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ;
            else if (argc > 4)
                _doLoadUsage (argv) ;
        }
        else
            _doLoadUsage (argv) ;


        if (!moduleLoaded (module1))
        {
            sprintf (cmd, "  %s%s", module1, args1) ;
            system (cmd) ;
        }


        if (!moduleLoaded (module2))
        {
            sprintf (cmd, "modprobe %s%s", module2, args2) ;
            system (cmd) ;
        }

        if (!moduleLoaded (module2))
        {
            fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
            exit (1) ;
        }
    }
    /*add for BananaPro by LeMaker team*/
    else if(BP_REV ==  piBoardRev())
    {
        /**/ if (strcasecmp (argv [2], "spi") == 0)
        {
            module1 = "spidev" ;
            module2 = "spi-sun7i" ;
            file1  = "/dev/spidev0.0" ;
            file2  = "/dev/spidev0.1" ;
            if (argc == 4)
                sprintf (args1, " bufsiz=%d", atoi (argv [3]) * 1024) ;
            else if (argc > 4)
                _doLoadUsage (argv) ;
        }
        else if (strcasecmp (argv [2], "i2c") == 0)
        {
            module1 = "i2c_dev" ;
            module2 = "i2c-sunxi" ;
            file1  = "/dev/i2c-2" ;
            file2  = "/dev/i2c-2" ;
            if (argc == 4)
                sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ;
            else if (argc > 4)
                _doLoadUsage (argv) ;
        }
        else
            _doLoadUsage (argv) ;


        if (!moduleLoaded (module1))
        {
            sprintf (cmd, "  %s%s", module1, args1) ;
            system (cmd) ;
        }


        if (!moduleLoaded (module2))
        {
            sprintf (cmd, "modprobe %s%s", module2, args2) ;
            system (cmd) ;
        }

        if (!moduleLoaded (module2))
        {
            fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
            exit (1) ;
        }
    }
    else
    {

    }
    /*end 2014.08.19*/
    sleep (1) ;	// To let things get settled

    changeOwner (argv [0], file1) ;
    changeOwner (argv [0], file2) ;
}
Exemplo n.º 19
0
int main (int argc, char *argv [])
{
    int i ;
    int model, rev, mem, maker, overVolted ;

    if (getenv ("WIRINGPI_DEBUG") != NULL)
    {
        printf ("gpio: wiringPi debug mode enabled\n") ;
        wiringPiDebug = TRUE ;
    }

    if (argc == 1)
    {
        fprintf (stderr, "%s\n", usage) ;
        return 1 ;
    }

    // Help

    if (strcasecmp (argv [1], "-h") == 0)
    {
        printf ("%s: %s\n", argv [0], usage) ;
        return 0 ;
    }

    // Sort of a special:

    if (strcmp (argv [1], "-R") == 0)
    {
        printf ("%d\n", piBoardRev ()) ;
        return 0 ;
    }

    // Version & Warranty

    if (strcmp (argv [1], "-V") == 0)
    {
        printf ("%d\n", piBoardRev ()) ;
        return 0 ;
    }

    if (strcmp (argv [1], "-v") == 0)
    {
        printf ("gpio version: %s\n", VERSION) ;
        printf ("Copyright (c) 2012-2014 Gordon Henderson\n") ;
        printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
        printf ("For details type: %s -warranty\n", argv [0]) ;
        printf ("\n") ;
        piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
        if (model == PI_MODEL_UNKNOWN)
        {
            printf ("Your Raspberry Pi has an unknown model type. Please report this to\n") ;
            printf ("    [email protected]\n") ;
            printf ("with a copy of your /proc/cpuinfo if possible\n") ;
        }
        else
        {
            printf ("Banana Pro Details:\n") ;
            printf ("  Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n",
                    piModelNames [model], piRevisionNames [rev], mem, piMakerNames [maker], overVolted ? "[OV]" : "") ;
        }
        return 0 ;
    }

    if (strcasecmp (argv [1], "-warranty") == 0)
    {
        printf ("gpio version: %s\n", VERSION) ;
        printf ("Copyright (c) 2012-2014 Gordon Henderson\n") ;
        printf ("\n") ;
        printf ("    This program is free software; you can redistribute it and/or modify\n") ;
        printf ("    it under the terms of the GNU Leser General Public License as published\n") ;
        printf ("    by the Free Software Foundation, either version 3 of the License, or\n") ;
        printf ("    (at your option) any later version.\n") ;
        printf ("\n") ;
        printf ("    This program is distributed in the hope that it will be useful,\n") ;
        printf ("    but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
        printf ("    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n") ;
        printf ("    GNU Lesser General Public License for more details.\n") ;
        printf ("\n") ;
        printf ("    You should have received a copy of the GNU Lesser General Public License\n") ;
        printf ("    along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
        printf ("\n") ;
        return 0 ;
    }

    if (geteuid () != 0)
    {
        fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
        return 1 ;
    }

    // Initial test for /sys/class/gpio operations:

    /**/ if (strcasecmp (argv [1], "exports"    ) == 0)
    {
        doExports     (argc, argv) ;
        return 0 ;
    }
    else if (strcasecmp (argv [1], "export"     ) == 0)
    {
        doExport      (argc, argv) ;
        return 0 ;
    }
    else if (strcasecmp (argv [1], "edge"       ) == 0)
    {
        doEdge        (argc, argv) ;
        return 0 ;
    }
    else if (strcasecmp (argv [1], "unexport"   ) == 0)
    {
        doUnexport    (argc, argv) ;
        return 0 ;
    }
    else if (strcasecmp (argv [1], "unexportall") == 0)
    {
        doUnexportall (argv [0]) ;
        return 0 ;
    }

    // Check for load command:

    if (strcasecmp (argv [1], "load" ) == 0)
    {
        doLoad     (argc, argv) ;
        return 0 ;
    }


    // Check for -g argument

    if (strcasecmp (argv [1], "-g") == 0)
    {
        wiringPiSetupGpio () ;

        for (i = 2 ; i < argc ; ++i)
            argv [i - 1] = argv [i] ;
        --argc ;
        wpMode = WPI_MODE_GPIO ;
    }

    // Check for -1 argument

    else if (strcasecmp (argv [1], "-1") == 0)
    {
        wiringPiSetupPhys () ;

        for (i = 2 ; i < argc ; ++i)
            argv [i - 1] = argv [i] ;
        --argc ;
        wpMode = WPI_MODE_PHYS ;
    }

    // Default to wiringPi mode

    else
    {
        wiringPiSetup () ;
        wpMode = WPI_MODE_PINS ;
    }

    // Check for -x argument to load in a new extension

    if (strcasecmp (argv [1], "-x") == 0)
    {
        if (argc < 3)
        {
            fprintf (stderr, "%s: -x missing extension specification.\n", argv [0]) ;
            exit (EXIT_FAILURE) ;
        }

        if (!doExtension (argv [0], argv [2]))	// Prints its own error messages
            exit (EXIT_FAILURE) ;

        for (i = 3 ; i < argc ; ++i)
            argv [i - 2] = argv [i] ;
        argc -= 2 ;
    }

    if (argc <= 1)
    {
        fprintf (stderr, "%s: no command given\n", argv [0]) ;
        exit (EXIT_FAILURE) ;
    }

    // Core wiringPi functions

    /**/ if (strcasecmp (argv [1], "mode"   ) == 0) doMode      (argc, argv) ;
    else if (strcasecmp (argv [1], "read"   ) == 0) doRead      (argc, argv) ;
    else if (strcasecmp (argv [1], "write"  ) == 0) doWrite     (argc, argv) ;
    else if (strcasecmp (argv [1], "pwm"    ) == 0) doPwm       (argc, argv) ;
    else if (strcasecmp (argv [1], "awrite" ) == 0) doAwrite    (argc, argv) ;
    else if (strcasecmp (argv [1], "aread"  ) == 0) doAread     (argc, argv) ;

    // GPIO Nicies

    else if (strcasecmp (argv [1], "toggle" ) == 0) doToggle    (argc, argv) ;

    // Pi Specifics

    else if (strcasecmp (argv [1], "pwm-bal"  ) == 0) doPwmMode    (PWM_MODE_BAL) ;
    else if (strcasecmp (argv [1], "pwm-ms"   ) == 0) doPwmMode    (PWM_MODE_MS) ;
    else if (strcasecmp (argv [1], "pwmr"     ) == 0) doPwmRange   (argc, argv) ;
    else if (strcasecmp (argv [1], "pwmc"     ) == 0) doPwmClock   (argc, argv) ;
    else if (strcasecmp (argv [1], "pwmTone"  ) == 0) doPwmTone    (argc, argv) ;
    else if (strcasecmp (argv [1], "usbp"     ) == 0) doUsbP       (argc, argv) ;
    else if (strcasecmp (argv [1], "readall"  ) == 0) doReadall    () ;
    else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall    () ;
    else if (strcasecmp (argv [1], "pins"     ) == 0) doPins       () ;
    else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect  (argc, argv) ;
    else if (strcasecmp (argv [1], "i2cd"     ) == 0) doI2Cdetect  (argc, argv) ;
    else if (strcasecmp (argv [1], "reset"    ) == 0) doReset      (argv [0]) ;
    else if (strcasecmp (argv [1], "wb"       ) == 0) doWriteByte  (argc, argv) ;
    else
    {
        fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
        exit (EXIT_FAILURE) ;
    }
    return 0 ;
}
Exemplo n.º 20
0
void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted)
{
  FILE *cpuFd ;
  char line [120] ;
  char *c ;

//	Will deal with the properly later on - for now, lets just get it going...
//  unsigned int modelNum ;

  (void)piBoardRev () ;	// Call this first to make sure all's OK. Don't care about the result.

  if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
    piBoardRevOops ("Unable to open /proc/cpuinfo") ;

  while (fgets (line, 120, cpuFd) != NULL)
    if (strncmp (line, "Revision", 8) == 0)
      break ;

  fclose (cpuFd) ;

  if (strncmp (line, "Revision", 8) != 0)
    piBoardRevOops ("No \"Revision\" line") ;

// Chomp trailing CR/NL

  for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
    *c = 0 ;
  
  //if (wiringPiDebug)
    //printf ("piboardId: Revision string: %s\n", line) ;

  if (piModel2)
  {

// Scan to the colon

    for (c = line ; *c ; ++c)
      if (*c == ':')
	break ;

    if (*c != ':')
      piBoardRevOops ("Bogus \"Revision\" line") ;

//    modelNum = (unsigned int)strtol (++c, NULL, 16) ; // Hex number with no leading 0x
    
    *model = PI_MODEL_2  ;
    *rev   = PI_VERSION_1_1 ;
    *mem   = 1024 ;
    *maker = PI_MAKER_SONY   ;
  }
  else
  {

// Scan to first digit

    for (c = line ; *c ; ++c)
      if (isdigit (*c))
	break ;

// Make sure its long enough

    if (strlen (c) < 4)
      piBoardRevOops ("Bogus \"Revision\" line") ;

// If longer than 4, we'll assume it's been overvolted

    *overVolted = strlen (c) > 4 ;
  
// Extract last 4 characters:

    c = c + strlen (c) - 4 ;

// Fill out the replys as appropriate

    /**/ if (strcmp (c, "0002") == 0) { *model = PI_MODEL_B  ; *rev = PI_VERSION_1   ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
    else if (strcmp (c, "0003") == 0) { *model = PI_MODEL_B  ; *rev = PI_VERSION_1_1 ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
    else if (strcmp (c, "0004") == 0) { *model = PI_MODEL_B  ; *rev = PI_VERSION_2   ; *mem = 256 ; *maker = PI_MAKER_SONY   ; }
    else if (strcmp (c, "0005") == 0) { *model = PI_MODEL_B  ; *rev = PI_VERSION_2   ; *mem = 256 ; *maker = PI_MAKER_QISDA  ; }
    else if (strcmp (c, "0006") == 0) { *model = PI_MODEL_B  ; *rev = PI_VERSION_2   ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
    else if (strcmp (c, "0007") == 0) { *model = PI_MODEL_A  ; *rev = PI_VERSION_2   ; *mem = 256 ; *maker = PI_MAKER_EGOMAN ; }
    else if (strcmp (c, "0008") == 0) { *model = PI_MODEL_A  ; *rev = PI_VERSION_2   ; *mem = 256 ; *maker = PI_MAKER_SONY ; ; }
    else if (strcmp (c, "0009") == 0) { *model = PI_MODEL_B  ; *rev = PI_VERSION_2   ; *mem = 256 ; *maker = PI_MAKER_QISDA  ; }
    else if (strcmp (c, "000d") == 0) { *model = PI_MODEL_B  ; *rev = PI_VERSION_2   ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; }
    else if (strcmp (c, "000e") == 0) { *model = PI_MODEL_B  ; *rev = PI_VERSION_2   ; *mem = 512 ; *maker = PI_MAKER_SONY   ; }
    else if (strcmp (c, "000f") == 0) { *model = PI_MODEL_B  ; *rev = PI_VERSION_2   ; *mem = 512 ; *maker = PI_MAKER_EGOMAN ; }
    else if (strcmp (c, "0010") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY   ; }
    else if (strcmp (c, "0011") == 0) { *model = PI_MODEL_CM ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_SONY   ; }
    else if (strcmp (c, "0012") == 0) { *model = PI_MODEL_AP ; *rev = PI_VERSION_1_2 ; *mem = 256 ; *maker = PI_MAKER_SONY   ; }
    else if (strcmp (c, "0013") == 0) { *model = PI_MODEL_BP ; *rev = PI_VERSION_1_2 ; *mem = 512 ; *maker = PI_MAKER_MBEST  ; }
    else                              { *model = 0           ; *rev = 0              ; *mem =   0 ; *maker = 0 ;               }
  }
}
Exemplo n.º 21
0
static foreign_t
pl_piBoardRev (term_t t){
	return PL_unify_integer(t,piBoardRev());
}
Exemplo n.º 22
0
static int setup(void) {
	int fd;
	int boardRev;

	boardRev = piBoardRev();

	if(boardRev == 1) {
		pinToGpio =  pinToGpioR1;
		physToGpio = physToGpioR1;
	} else {
		if(piModel2 == 1) {
			BCM2708_PERI_BASE = 0x3F000000;
		}
		pinToGpio =  pinToGpioR2;
		physToGpio = physToGpioR2;
	}

#ifdef O_CLOEXEC
	if((fd = open("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC)) < 0) {
#else
	if((fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
#endif
		wiringXLog(LOG_ERR, "raspberrypi->setup: Unable to open /dev/mem: %s", strerror(errno));
		return -1;
	}

	gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE);
	if((int32_t)gpio == -1) {
		wiringXLog(LOG_ERR, "raspberrypi->setup: mmap (GPIO) failed: %s", strerror(errno));
		return -1;
	}

	return 0;
}

static int raspberrypiDigitalRead(int pin) {
	if(pinModes[pin] != INPUT && pinModes[pin] != SYS) {
		wiringXLog(LOG_ERR, "raspberrypi->digitalRead: Trying to write to pin %d, but it's not configured as input", pin);
		return -1;
	}

	if(raspberrypiValidGPIO(pin) != 0) {
		wiringXLog(LOG_ERR, "raspberrypi->digitalRead: Invalid pin number %d", pin);
		return -1;
	}

	if((pin & PI_GPIO_MASK) == 0) {
		pin = pinToGpio[pin];

		if((*(gpio + gpioToGPLEV[pin]) & (1 << (pin & 31))) != 0) {
			return HIGH;
		} else {
			return LOW;
		}
	}
	return 0;
}

static int raspberrypiDigitalWrite(int pin, int value) {
	if(pinModes[pin] != OUTPUT) {
		wiringXLog(LOG_ERR, "raspberrypi->digitalWrite: Trying to write to pin %d, but it's not configured as output", pin);
		return -1;
	}

	if(raspberrypiValidGPIO(pin) != 0) {
		wiringXLog(LOG_ERR, "raspberrypi->digitalWrite: Invalid pin number %d", pin);
		return -1;
	}

	if((pin & PI_GPIO_MASK) == 0) {
		pin = pinToGpio[pin];

		if(value == LOW)
			*(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31);
		else
			*(gpio + gpioToGPSET [pin]) = 1 << (pin & 31);
	}
	return 0;
}

static int raspberrypiPinMode(int pin, int mode) {
	int fSel, shift;

	if(raspberrypiValidGPIO(pin) != 0) {
		wiringXLog(LOG_ERR, "raspberrypi->pinMode: Invalid pin number %d", pin);
		return -1;
	}

	if((pin & PI_GPIO_MASK) == 0) {
		pinModes[pin] = mode;
		pin = pinToGpio[pin];

		fSel = gpioToGPFSEL[pin];
		shift = gpioToShift[pin];

		if(mode == INPUT) {
			*(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift));
		} else if(mode == OUTPUT) {
			*(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift);
		}
	}
	return 0;
}
Exemplo n.º 23
0
int wiringPiSetup (void)
{
  int      fd ;
  int      boardRev ;
  uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ;
  struct timeval tv ;

  if (geteuid () != 0)
  {
    fprintf (stderr, "wiringPi:\n  Must be root to call wiringPiSetup().\n  (Did you forget sudo?)\n") ;
    exit (EXIT_FAILURE) ;
  }

  if (getenv ("WIRINGPI_DEBUG") != NULL)
  {
    printf ("wiringPi: Debug mode enabled\n") ;
    wiringPiDebug = TRUE ;
  }

  if (wiringPiDebug)
    printf ("wiringPi: wiringPiSetup called\n") ;

            pinMode =           pinModeWPi ;
    pullUpDnControl =   pullUpDnControlWPi ;
       digitalWrite =      digitalWriteWPi ;
   digitalWriteByte = digitalWriteByteGpio ;	// Same code
           pwmWrite =          pwmWriteWPi ;
        setPadDrive =       setPadDriveWPi ;
        digitalRead =       digitalReadWPi ;
   waitForInterrupt =  waitForInterruptWPi ;
  delayMicroseconds = delayMicrosecondsWPi ;
         pwmSetMode =        pwmSetModeWPi ;
        pwmSetRange =       pwmSetRangeWPi ;
        pwmSetClock =       pwmSetClockWPi ;
  
  boardRev = piBoardRev () ;

  if (boardRev == 1)
    pinToGpio = pinToGpioR1 ;
  else
    pinToGpio = pinToGpioR2 ;

// Open the master /dev/memory device

  if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0)
  {
    if (wiringPiDebug)
      fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;
    return -1 ;
  }

// GPIO:

// Allocate 2 pages - 1 ...

  if ((gpioMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  {
    if (wiringPiDebug)
      fprintf (stderr, "wiringPiSetup: malloc failed: %s\n", strerror (errno)) ;
    return -1 ;
  }

// ... presumably to make sure we can round it up to a whole page size

  if (((uint32_t)gpioMem % PAGE_SIZE) != 0)
    gpioMem += PAGE_SIZE - ((uint32_t)gpioMem % PAGE_SIZE) ;

  gpio = (uint32_t *)mmap((caddr_t)gpioMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_BASE) ;

  if ((int32_t)gpio < 0)
  {
    if (wiringPiDebug)
      fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", strerror (errno)) ;
    return -1 ;
  }

// PWM

  if ((pwmMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  {
    if (wiringPiDebug)
      fprintf (stderr, "wiringPiSetup: pwmMem malloc failed: %s\n", strerror (errno)) ;
    return -1 ;
  }

  if (((uint32_t)pwmMem % PAGE_SIZE) != 0)
    pwmMem += PAGE_SIZE - ((uint32_t)pwmMem % PAGE_SIZE) ;

  pwm = (uint32_t *)mmap(pwmMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PWM) ;

  if ((int32_t)pwm < 0)
  {
    if (wiringPiDebug)
      fprintf (stderr, "wiringPiSetup: mmap failed (pwm): %s\n", strerror (errno)) ;
    return -1 ;
  }
 
// Clock control (needed for PWM)

  if ((clkMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  {
    if (wiringPiDebug)
      fprintf (stderr, "wiringPiSetup: clkMem malloc failed: %s\n", strerror (errno)) ;
    return -1 ;
  }

  if (((uint32_t)clkMem % PAGE_SIZE) != 0)
    clkMem += PAGE_SIZE - ((uint32_t)clkMem % PAGE_SIZE) ;

  clk = (uint32_t *)mmap(clkMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, CLOCK_BASE) ;

  if ((int32_t)clk < 0)
  {
    if (wiringPiDebug)
      fprintf (stderr, "wiringPiSetup: mmap failed (clk): %s\n", strerror (errno)) ;
    return -1 ;
  }
 
// The drive pads

  if ((padsMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  {
    if (wiringPiDebug)
      fprintf (stderr, "wiringPiSetup: padsMem malloc failed: %s\n", strerror (errno)) ;
    return -1 ;
  }

  if (((uint32_t)padsMem % PAGE_SIZE) != 0)
    padsMem += PAGE_SIZE - ((uint32_t)padsMem % PAGE_SIZE) ;

  pads = (uint32_t *)mmap(padsMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_PADS) ;

  if ((int32_t)pads < 0)
  {
    if (wiringPiDebug)
      fprintf (stderr, "wiringPiSetup: mmap failed (pads): %s\n", strerror (errno)) ;
    return -1 ;
  }

#ifdef	DEBUG_PADS
  printf ("Checking pads @ 0x%08X\n", (unsigned int)pads) ;
  printf (" -> %08X %08X %08X\n", *(pads + 11), *(pads + 12), *(pads + 13)) ;
#endif

// The system timer

  if ((timerMem = malloc (BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  {
    if (wiringPiDebug)
      fprintf (stderr, "wiringPiSetup: timerMem malloc failed: %s\n", strerror (errno)) ;
    return -1 ;
  }

  if (((uint32_t)timerMem % PAGE_SIZE) != 0)
    timerMem += PAGE_SIZE - ((uint32_t)timerMem % PAGE_SIZE) ;

  timer = (uint32_t *)mmap(timerMem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, GPIO_TIMER) ;

  if ((int32_t)timer < 0)
  {
    if (wiringPiDebug)
      fprintf (stderr, "wiringPiSetup: mmap failed (timer): %s\n", strerror (errno)) ;
    return -1 ;
  }

// Set the timer to free-running, 1MHz.
//	0xF9 is 249, the timer divide is base clock / (divide+1)
//	so base clock is 250MHz / 250 = 1MHz.

  *(timer + TIMER_CONTROL) = 0x0000280 ;
  *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  timerIrqRaw = timer + TIMER_IRQ_RAW ;

// Initialise our epoch for millis()

  gettimeofday (&tv, NULL) ;
  epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;

  wiringPiMode = WPI_MODE_PINS ;

  return 0 ;
}
Exemplo n.º 24
0
json_t *rpi_general_board_rev_get(duda_request_t *dr, int parameter)
{
    return json->create_number((double)piBoardRev());
}
Exemplo n.º 25
0
int main (int argc, char *argv [])
{
  int i ;

  if (argc == 1)
  {
    fprintf (stderr, "%s\n", usage) ;
    return 1 ;
  }

  if (strcasecmp (argv [1], "-h") == 0)
  {
    printf ("%s: %s\n", argv [0], usage) ;
    return 0 ;
  }

  if (strcasecmp (argv [1], "-v") == 0)
  {
    printf ("gpio version: %s\n", VERSION) ;
    printf ("Copyright (c) 2012 Gordon Henderson\n") ;
    printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
    printf ("For details type: %s -warranty\n", argv [0]) ;
    printf ("\n") ;
    printf ("This Raspberry Pi is a revision %d board.\n", piBoardRev ()) ;
    return 0 ;
  }

  if (strcasecmp (argv [1], "-warranty") == 0)
  {
    printf ("gpio version: %s\n", VERSION) ;
    printf ("Copyright (c) 2012 Gordon Henderson\n") ;
    printf ("\n") ;
    printf ("    This program is free software; you can redistribute it and/or modify\n") ;
    printf ("    it under the terms of the GNU Leser General Public License as published\n") ;
    printf ("    by the Free Software Foundation, either version 3 of the License, or\n") ;
    printf ("    (at your option) any later version.\n") ;
    printf ("\n") ;
    printf ("    This program is distributed in the hope that it will be useful,\n") ;
    printf ("    but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
    printf ("    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n") ;
    printf ("    GNU Lesser General Public License for more details.\n") ;
    printf ("\n") ;
    printf ("    You should have received a copy of the GNU Lesser General Public License\n") ;
    printf ("    along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
    printf ("\n") ;
    return 0 ;
  }

  if (geteuid () != 0)
  {
    fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
    return 1 ;
  }

// Initial test for /sys/class/gpio operations:

  /**/ if (strcasecmp (argv [1], "exports"    ) == 0)	{ doExports     (argc, argv) ;	return 0 ; }
  else if (strcasecmp (argv [1], "export"     ) == 0)	{ doExport      (argc, argv) ;	return 0 ; }
  else if (strcasecmp (argv [1], "edge"       ) == 0)	{ doEdge        (argc, argv) ;	return 0 ; }
  else if (strcasecmp (argv [1], "unexportall") == 0)	{ doUnexportall (argc, argv) ;	return 0 ; }
  else if (strcasecmp (argv [1], "unexport"   ) == 0)	{ doUnexport    (argc, argv) ;	return 0 ; }

// Check for load command:

  if (strcasecmp (argv [1], "load" ) == 0)	{ doLoad     (argc, argv) ; return 0 ; }

// Gertboard commands

  if (strcasecmp (argv [1], "gbr" ) == 0)	{ doGbr (argc, argv) ; return 0 ; }
  if (strcasecmp (argv [1], "gbw" ) == 0)	{ doGbw (argc, argv) ; return 0 ; }

// Check for -g argument

  if (strcasecmp (argv [1], "-g") == 0)
  {
    if (wiringPiSetupGpio () == -1)
    {
      fprintf (stderr, "%s: Unable to initialise GPIO mode.\n", argv [0]) ;
      exit (1) ;
    }

    for (i = 2 ; i < argc ; ++i)
      argv [i - 1] = argv [i] ;
    --argc ;
    wpMode = WPI_MODE_GPIO ;
  }

// Check for -p argument for PiFace

  else if (strcasecmp (argv [1], "-p") == 0)
  {
    if (wiringPiSetupPiFaceForGpioProg () == -1)
    {
      fprintf (stderr, "%s: Unable to initialise PiFace.\n", argv [0]) ;
      exit (1) ;
    }

    for (i = 2 ; i < argc ; ++i)
      argv [i - 1] = argv [i] ;
    --argc ;
    wpMode = WPI_MODE_PIFACE ;
  }

// Default to wiringPi mode

  else
  {
    if (wiringPiSetup () == -1)
    {
      fprintf (stderr, "%s: Unable to initialise wiringPi mode\n", argv [0]) ;
      exit (1) ;
    }
    wpMode = WPI_MODE_PINS ;
  }

// Check for PWM or Pad Drive operations

  if (wpMode != WPI_MODE_PIFACE)
  {
    if (strcasecmp (argv [1], "pwm-bal") == 0)	{ doPwmMode  (PWM_MODE_BAL) ;	return 0 ; }
    if (strcasecmp (argv [1], "pwm-ms")  == 0)	{ doPwmMode  (PWM_MODE_MS) ;	return 0 ; }
    if (strcasecmp (argv [1], "pwmr")    == 0)	{ doPwmRange (argc, argv) ;	return 0 ; }
    if (strcasecmp (argv [1], "pwmc")    == 0)	{ doPwmClock (argc, argv) ;	return 0 ; }
    if (strcasecmp (argv [1], "drive")   == 0)	{ doPadDrive (argc, argv) ;	return 0 ; }
  }

// Check for wiring commands

  /**/ if (strcasecmp (argv [1], "readall" ) == 0) doReadall  () ;
  else if (strcasecmp (argv [1], "read" )    == 0) doRead     (argc, argv) ;
  else if (strcasecmp (argv [1], "write")    == 0) doWrite    (argc, argv) ;
  else if (strcasecmp (argv [1], "pwm"  )    == 0) doPwm      (argc, argv) ;
  else if (strcasecmp (argv [1], "mode" )    == 0) doMode     (argc, argv) ;
  else
  {
    fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
    exit (1) ;
  }
  return 0 ;
}
Exemplo n.º 26
0
static int piBoardId(int *model, int *rev, int *mem, int *maker, int *overVolted) {
	FILE *cpuFd ;
	char line [120] ;
	char *c ;

	(void)piBoardRev();	// Call this first to make sure all's OK. Don't care about the result.

	if((cpuFd = fopen("/proc/cpuinfo", "r")) == NULL) {
		wiringXLog(LOG_ERR, "raspberrypi->piBoardId: Unable to open /proc/cpuinfo");
		return -1;
	}

	while(fgets (line, 120, cpuFd) != NULL) {
		if(strncmp (line, "Revision", 8) == 0) {
			break;
		}
	}

	fclose(cpuFd);

	if(strncmp(line, "Revision", 8) != 0) {
		wiringXLog(LOG_ERR, "raspberrypi->piBoardId: No \"Revision\" line");
		return -1;
	}

	// Chomp trailing CR/NL
	for(c = &line[strlen(line) - 1]; (*c == '\n') || (*c == '\r'); --c) {
		*c = 0;
	}

	// Scan to first digit
	for(c = line; *c; ++c) {
		if(isdigit(*c)) {
			break;
		}
	}

	// Make sure its long enough
	if(strlen(c) < 4) {
		wiringXLog(LOG_ERR, "raspberrypi->piBoardId: Bogus \"Revision\" line");
		return -1;
	}

	// If longer than 4, we'll assume it's been overvolted
	*overVolted = strlen(c) > 4;

	// Extract last 4 characters:
	c = c + strlen(c) - 4;

	// Fill out the replys as appropriate

	if(piModel2 == 1) {
		if(strcmp (c, "0010") == 0) {
			*model = PI_MODEL_2;
			*rev = PI_VERSION_1_1;
			*mem = 1024;
			*maker = PI_MAKER_SONY;
		} else {
			*model = 0;
			*rev = 0;
			*mem = 0;
			*maker = 0;
		}
	} else if(strcmp(c, "0002") == 0) {
		*model = PI_MODEL_B;
		*rev = PI_VERSION_1;
		*mem = 256;
		*maker = PI_MAKER_EGOMAN;
	} else if(strcmp(c, "0003") == 0) {
		*model = PI_MODEL_B;
		*rev = PI_VERSION_1_1;
		*mem = 256;
		*maker = PI_MAKER_EGOMAN;
	} else if(strcmp(c, "0004") == 0) {
		*model = PI_MODEL_B;
		*rev = PI_VERSION_2;
		*mem = 256;
		*maker = PI_MAKER_SONY;
	} else if(strcmp(c, "0005") == 0) {
		*model = PI_MODEL_B;
		*rev = PI_VERSION_2;
		*mem = 256;
		*maker = PI_MAKER_QISDA;
	} else if(strcmp(c, "0006") == 0) {
		*model = PI_MODEL_B;
		*rev = PI_VERSION_2;
		*mem = 256;
		*maker = PI_MAKER_EGOMAN;
	} else if(strcmp(c, "0007") == 0) {
		*model = PI_MODEL_A;
		*rev = PI_VERSION_2;
		*mem = 256;
		*maker = PI_MAKER_EGOMAN;
	} else if(strcmp(c, "0008") == 0) {
		*model = PI_MODEL_A;
		*rev = PI_VERSION_2;
		*mem = 256;
		*maker = PI_MAKER_SONY;
	} else if(strcmp(c, "0009") == 0) {
		*model = PI_MODEL_B;
		*rev = PI_VERSION_2;
		*mem = 256;
		*maker = PI_MAKER_QISDA;
	} else if(strcmp(c, "000d") == 0) {
		*model = PI_MODEL_B;
		*rev = PI_VERSION_2;
		*mem = 512;
		*maker = PI_MAKER_EGOMAN;
	} else if(strcmp(c, "000e") == 0) {
		*model = PI_MODEL_B;
		*rev = PI_VERSION_2;
		*mem = 512;
		*maker = PI_MAKER_SONY;
	} else if(strcmp(c, "000f") == 0) {
		*model = PI_MODEL_B;
		*rev = PI_VERSION_2;
		*mem = 512;
		*maker = PI_MAKER_EGOMAN;
	} else if(strcmp(c, "0010") == 0) {
		*model = PI_MODEL_BP;
		*rev = PI_VERSION_1_2;
		*mem = 512;
		*maker = PI_MAKER_SONY;
	} else if(strcmp(c, "0011") == 0) {
		*model = PI_MODEL_CM;
		*rev = PI_VERSION_1_2;
		*mem = 512;
		*maker = PI_MAKER_SONY;
  } else if(strcmp(c, "0012") == 0) { 
		*model = PI_MODEL_AP;
		*rev = PI_VERSION_1_2;
		*mem = 256;
		*maker = PI_MAKER_SONY;
	}	else if(strcmp(c, "0013") == 0) {
		*model = PI_MODEL_BP;
		*rev = PI_VERSION_1_2;
		*mem = 512;
		*maker = PI_MAKER_MBEST;
	} else {
		*model = 0;
		*rev = 0;
		*mem = 0;
		*maker = 0;
	}

	return 0;
}