C/C++

Need to Set 8 gpio pins in one opperation

3 662
Anyone knows how i can set 8 pins of the gpio in one opertion in C ? I have tried digitalWriteByte, but that gives a truly terrible performance,
and setting it individual limits the bandwith enormously.
Any pointers are greatly appreciated..

Thanks.
( bananian, wiringpi, banana pro, C )


swipon  
I'think it's possible with C/C++ threads launch simoultaneus part of code in multithreading.

You can watch here some explanation and example: http://softpixel.com/~cwright/programming/threads/threads.c.php

Thanks swipon, ill have a look. it would be great to fire some threads simultaneously.

swipon  
Ok, i've found a little time to write an example of thread using (not tested yet):

  1. //libraries
  2. #include <wiringPi.h>
  3. #include <wiringPiSPI.h>
  4. #include <pthread.h>
  5. #include <stdio.h>
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <signal.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <unistd.h>

  12. //SPI channel
  13. #define CHANNEL 0

  14. //pin definition with an array
  15. Int OUTPINS[5][2]={{7,0},{8,0},{9,0},{10,0},{11,0}};
  16. Int INPINS[7]={0,1,2,3,4,5,6};
  17. #define NUMOUT 5
  18. #define NUMIN 7

  19. //Structs for pass to thread out value
  20. typedef struct s_outvalue {
  21.     int PIN_NUM;
  22.     int PIN_VALUE;
  23. } outvalue;
  24. outvalue struttura;

  25. //Array for the inputs values
  26. Int valoriLetti[6];

  27. //thread definition
  28. pthread_t threadout_id[5];
  29. pthread_t threadin_id[6];

  30. // function to create thread for out pin
  31. void *funzione_avvio_thread_out(void *arg)
  32. {
  33. struct struttura *params = arg;
  34. int PIN =params-> PIN_NUM;
  35. int PIN_VALUE= params-> PIN_VALUE;
  36. pinMode(PIN,OUTPUT);
  37. digitalWrite(PIN, PIN_VALUE);
  38. pthread_exit(NULL);
  39. }
  40. //function to create thread for in pin
  41. void *funzione_avvio_thread_in(void *arg)
  42. {
  43. int PIN = *((int *) arg);
  44. Int PINVALUE;
  45. PIN = atoi(arg);
  46. PINVALUE=0;
  47. pinMode(PIN,INPUT);
  48. PINVALUE = digitalRead(PIN);
  49. valoriLetti[PIN]=PINVALUE;
  50. pthread_exit(NULL);
  51. }

  52. int main (int argc, char *argv[])
  53. {
  54.   //if (wiringPiSetupPhys() == -1)
  55.   if (wiringPiSetup() == -1)
  56.     {
  57.         printf("Error: wiringPi setup failed.\n");
  58.         return 1;
  59.     } else {
  60.       if (wiringPiSPISetup(CHANNEL, 1000000) < 0) {
  61.                 fprintf (stderr, "Serial SPI Setup failed: %s\n", strerror (errno));
  62.                 exit(errno);
  63.         }
  64.       if (setup() == -1)
  65.       {
  66.         printf("Error while initialize program in Setup section.\n");
  67.         return 1;
  68.       }
  69.       for (;;){
  70.         //for a loop like arduino standard code
  71.         loop();
  72.       }
  73.       return 0;
  74.     }
  75. }

  76. int loop(void){
  77. int *y;
  78. int i;

  79. //example how to setting up some output pin
  80. OUTPINS[0][1]=1;
  81. OUTPINS[2][1]=1;
  82. OUTPINS[4][1]=1;

  83. //initialize thread for output
  84.     for(i=0; i < NUMOUT; i++)
  85.     {
  86.         pthread_create( & threadout_id [i], NULL, funzione_avvio_thread_out, &struttura);
  87.     }

  88. //initialize thread for input
  89.     for(i=0; i < NUMIN; i++)
  90.     {
  91.         struttura. PIN_NUM = OUTPINS[i][0];
  92.         struttura. PIN_ VALUE = OUTPINS[i][1];
  93.         pthread_create( & threadin_id [i], NULL, funzione_avvio_thread_in, &i);
  94.     }

  95. //print result of reading the input
  96.    for ( i = 0; i < NUMIN; i++ ) {
  97.           printf("Valore PIN[%d] = %d\n", j, valoriLetti [ i ]);
  98.    }

  99. //close output thread
  100.    for ( i = 0; i < NUMOUT; i++ ) {
  101.           pthread_join(threadout_id [i],NULL);
  102.    }
  103. //close input thread
  104.    for ( i = 0; i < NUMIN; i++ ) {
  105.           pthread_join(threadin_id [i],NULL);
  106.    }
  107. return 0;
  108. }
Copy the Code


I don't tryed this code but it would be ok. (the only thing not sure is the structs for passing multiple parameters  at output threads)

Luca

You have to log in before you can reply Login | Sign Up

Points Rules