CrosseyeJack / spincl

spincl is a command-line utility for executing spi commands with the Broadcom bcm2835.
4 stars 2 forks source link

pacth for use any gpio #1

Open md5sum-as opened 9 years ago

md5sum-as commented 9 years ago
*** ./spimain.c.old 2013-06-26 09:40:51.000000000 +0400
--- ./spimain.c 2015-07-14 20:49:14.284691132 +0300
*************** uint8_t mode = BCM2835_SPI_MODE0;
*** 103,108 ****
--- 103,109 ----
  uint16_t clk_div = BCM2835_SPI_CLOCK_DIVIDER_65536;
  uint8_t cs = BCM2835_SPI_CS0;
  uint8_t polarity = LOW;
+ uint8_t gpio = RPI_V2_GPIO_P1_24;

  //*******************************************************************************
*************** int main (int argc, char **argv)
*** 134,146 ****
      bcm2835_spi_chipSelect(cs);
      bcm2835_spi_setChipSelectPolarity(cs, polarity);

      // execute SPI transfer   
      memset(rbuf, 0, sizeof(rbuf));    
      bcm2835_spi_transfernb(wbuf, rbuf, len);

      // This SPI end is done after a transfer if specified
      if (init == SPI_END) bcm2835_spi_end();
!     
      // Close the bcm2835 library
      bcm2835_close();

--- 135,165 ----
      bcm2835_spi_chipSelect(cs);
      bcm2835_spi_setChipSelectPolarity(cs, polarity);

+     // if CS =3 will use GPIO port specified in -g option
+     // PORT DOWN
+     if (cs == 3) {
+   bcm2835_gpio_fsel(gpio,BCM2835_GPIO_FSEL_OUTP);
+   bcm2835_gpio_clr(gpio);
+     }
+ 
      // execute SPI transfer   
      memset(rbuf, 0, sizeof(rbuf));    
      bcm2835_spi_transfernb(wbuf, rbuf, len);

+     // if CS =3 will use GPIO port specified in -g option
+     // PORT UP
+     if (cs == 3) {
+   bcm2835_gpio_set(gpio);
+     }
+ 
      // This SPI end is done after a transfer if specified
      if (init == SPI_END) bcm2835_spi_end();
! 
!     // Restore gpio to INPUT    
!     if (cs == 3) {
!   bcm2835_gpio_fsel(gpio,BCM2835_GPIO_FSEL_OUTP);
!     }
! 
      // Close the bcm2835 library
      bcm2835_close();

*************** static int comparse(int argc, char **arg
*** 216,221 ****
--- 235,248 ----
                  }
                  break;

+             case 'g':  // GPIO
+                 if (argv[argnum][2] < '0' || (i = atoi(argv[argnum]+2)) > 45) {
+                     fprintf(stderr, "Invalid GPIO port\n");
+                     return EXIT_FAILURE;
+                 }
+                 gpio = i;
+                 break;
+ 
              default:
                  fprintf(stderr, "%c is not a valid option\n", argv[argnum][1]);
                  return EXIT_FAILURE;
*************** static int showusage(int errcode)
*** 290,295 ****
--- 317,323 ----
      printf("      Corresponding frequencies are specified in bcm2835.h.\n");
      printf("    -sx where x is 0 (CS0), 1 (CS1), 2 (CS1&CS2), or 3 (None)\n");
      printf("    -px where x is chip select polarity, 0(LOW) or 1(HIGH)\n");
+     printf("    -gx where x is GPIO number. Use this option with the option -s3. Allowed values are 0 through 45\n");
      printf("\n");
      printf("  len: The number of bytes to be transmitted and received (full duplex).\n");
      printf("    The maximum number of bytes allowed is %d\n", MAX_LEN);
CrosseyeJack commented 9 years ago

Submit a pull request and I'll pull it in.

This isn't my code and I've not actually used it in a while - http://ipsolutionscorp.com/raspberry-pi-spi-utility/

I just pulled it into GitHub because at the time the site hosting it was down and the code was code was GPL'ed so rehosted it incase I needed it in the future.