Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
GPIOInputOutput.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <unistd.h>
3 
4 #include "../hubolib.h"
5 #include "../hubocfg.h"
6 
7 using namespace BCM2835;
8 using namespace HuboLib;
9 
10 
11 /*
12 Compile and link:
13  g++ GPIOInputOutput.cpp -O2 -L../ -lhubo -lpthread -lrt -o GPIOInputOutput.out
14 Run:
15  sudo ./GPIOInputOutput.out
16 Purpose:
17  Configure, read from and write to GPIO pins directly.
18 */
19 
20 int main(void)
21 {
22  // While we do not explicitly need to initialize the GPIO interface we still require to check for a correct initialisation.
23  if (!IsGPIOInitialized())
24  {
25  printf ("The GPIO interface could not be initialised - terminating.\n");
26  return -1;
27  }
28 
29  // g_bcm2835Config.m_bUseMutexProtection = false;
30 
31  // We will use Broadcom pin 22 which is known as GPIO3 and located on pin header number 15.
32  unsigned char pin = 22;
33 
34  // Configure the pin to be an input.
35  FunctionSelectPin (pin, Input);
36 
37  // Read pin.
38  printf ("Input pin %d is set to %d\n", pin, ReadPin(pin));
39 
40  // We set the pull up resistor on the pin.
41  SetPullUpDown (pin, PUD_UP);
42  printf ("Input pin %d with pull up is set to %d\n", pin, ReadPin(pin));
43 
44  // We set the pull down resistor on the pin.
45  SetPullUpDown (pin, PUD_DOWN);
46  printf ("Input pin %d with pull down is set to %d\n", pin, ReadPin(pin));
47 
48  // Configure the pin to be an output.
50 
51  // Let's set the output and read back it's value.
52  WritePin (pin, 0);
53  printf ("Cleared output pin %d is set to %d\n", pin, ReadPin(pin));
54  WritePin (pin, 1);
55  printf ("Set output pin %d is set to %d\n", pin, ReadPin(pin));
56 
57 
58  // Some timing measurements.
59  unsigned long long tStart, tStop;
60  printf ("\n");
61 
62  // Lets see how fast we can write a value.
63  tStart = GetTime_MicroSeconds();
64  for (int i=0; i<1000000; i++)
65  {
66  WritePin (pin, 1);
67  }
68  tStop = GetTime_MicroSeconds();
69  printf ("1000000 samples written in %lluµs - thus %fMHz\n", tStop-tStart, 1000000.0/(tStop-tStart));
70 
71  // Lets see how fast we can read a value.
72  tStart = GetTime_MicroSeconds();
73  for (int i=0; i<1000000; i++)
74  {
75  ReadPin (pin);
76  }
77  tStop = GetTime_MicroSeconds();
78  printf ("1000000 samples read in %lluµs - thus %fMHz\n", tStop-tStart, 1000000.0/(tStop-tStart));
79 
80 
81  // Precision of µsleep() vs. Delay_MicroSeconds().
82  unsigned long long delay;
83  printf ("\n");
84 
85  // Compare for short delays.
86  delay = 30;
87  tStart = GetTime_MicroSeconds();
88  usleep (delay);
89  tStop = GetTime_MicroSeconds();
90  printf ("usleep(%lluµs) took %lluµs\n", delay, tStop-tStart);
91 
92  tStart = GetTime_MicroSeconds();
93  Delay_MicroSeconds (delay);
94  tStop = GetTime_MicroSeconds();
95  printf ("Delay_MicroSeconds(%lluµs) took %lluµs\n", delay, tStop-tStart);
96 
97  // Compare for long delays.
98  delay = 1000000;
99  tStart = GetTime_MicroSeconds();
100  usleep (delay);
101  tStop = GetTime_MicroSeconds();
102  printf ("usleep(%lluµs) took %lluµs\n", delay, tStop-tStart);
103 
104  tStart = GetTime_MicroSeconds();
105  Delay_MicroSeconds (delay);
106  tStop = GetTime_MicroSeconds();
107  printf ("Delay_MicroSeconds(%lluµs) took %lluµs\n", delay, tStop-tStart);
108 
109  // Output of the above.
110  /*
111  Input pin 22 is set to 0
112  Input pin 22 with pull up is set to 1
113  Input pin 22 with pull down is set to 0
114  Cleared output pin 22 is set to 0
115  Set output pin 22 is set to 1
116 
117  1000000 samples written in 362509µs - thus 2.758552MHz
118  1000000 samples read in 412230µs - thus 2.425830MHz
119 
120  usleep(30µs) took 129µs
121  Delay_MicroSeconds(30µs) took 41µs
122  usleep(1000000µs) took 1000077µs
123  Delay_MicroSeconds(1000000µs) took 1000004µs
124  */
125 
126  // If mutex protection is bypassed (g_bcm2835Config.m_bUseMutexProtection = false;) reading and writing
127  // to the GPIO's further increases in speed.
128  /*
129  Input pin 22 is set to 0
130  Input pin 22 with pull up is set to 1
131  Input pin 22 with pull down is set to 0
132  Cleared output pin 22 is set to 0
133  Set output pin 22 is set to 1
134 
135  1000000 samples written in 129636µs - thus 7.713907MHz
136  1000000 samples read in 193106µs - thus 5.178503MHz
137 
138  usleep(30µs) took 207µs
139  Delay_MicroSeconds(30µs) took 39µs
140  usleep(1000000µs) took 1000078µs
141  Delay_MicroSeconds(1000000µs) took 1000003µs
142  */
143 
144  return 0;
145 }
146 
unsigned short ReadPin(unsigned short pin)
Reads the value of an (input) pin.
void Delay_MicroSeconds(unsigned long delay_micros)
delays the execution of the calling thread for the given number of micro seconds. ...
bool IsGPIOInitialized()
Returns the status of the initialisation of the GPIO part of the library.
void FunctionSelectPin(unsigned char pin, FunctionSelectType mode)
Use FunctionSelect() to configure the mode of a BCM2835 pin.
int main(void)
void WritePin(unsigned char pin, unsigned char value)
Sets or clears a pin configured to be an output.
unsigned long long GetTime_MicroSeconds()
Returns the number of micro seconds.
void SetPullUpDown(unsigned char pin, PullUpDownType pud)
Enables or disables the BCM2835 internal pull -up or -down resistors for a pin configured as an input...