Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Files Functions Variables Typedefs Macros Groups
Gpio.h
1 #ifndef _GPIO_H_INCLUDED_
2 #define _GPIO_H_INCLUDED_
3 #include <unistd.h>
4 #include <stdint.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <sys/mman.h>
9 #include <stdio.h>
10 #include "GPIOcontrol.h"
11 
12 /*
13 #define TEST_PIN_GPIO_BASE GPIO0_ADDRESS // Use GPIO0(31) for debugging
14 #define TEST_PIN_MASK (1 << 31)
15 #define TEST_PIN2_MASK (1 << 26)
16 
17 #define USERLED3_GPIO_BASE GPIO1_ADDRESS // GPIO1(24) is user LED 3
18 #define USERLED3_PIN_MASK (1 << 24)
19 */
20 
21 static const uint32_t GPIO_SIZE = 0x198;
22 static const uint32_t GPIO_DATAIN = (0x138 / 4);
23 static const uint32_t GPIO_CLEARDATAOUT = (0x190 / 4);
24 static const uint32_t GPIO_SETDATAOUT = (0x194 / 4);
25 static const uint32_t GPIO_ADDRESSES[4] = {
26  0x44E07000,
27  0x4804C000,
28  0x481AC000,
29  0x481AE000,
30 };
31 
32 class Gpio{
33 public:
34  Gpio():
35  pin(-1),
36  fd(-1)
37  {};
38 
39  ~Gpio(){
40  close();
41  }
42 
52  int open(unsigned int pin, unsigned int direction, bool unexport = true);
53 
57  void close();
58 
63  bool read(){
64  return (gpio[GPIO_DATAIN] & pinMask);
65  }
66 
70  void set(){
71  gpio[GPIO_SETDATAOUT] = pinMask;
72  }
73 
76  void clear(){
77  gpio[GPIO_CLEARDATAOUT] = pinMask;
78  }
79 
84  void write(bool value){
85  if(value){
86  set();
87  } else {
88  clear();
89  }
90  }
91 
97  bool enabled(){
98  return fd != -1;
99  }
100 private:
101  bool shouldUnexport;
102  int pin;
103  int fd;
104  uint32_t pinMask;
105  volatile uint32_t* gpio;
106 };
107 #endif /* _GPIO_H_INCLUDED_ */
void clear()
Definition: Gpio.h:76
void set()
Definition: Gpio.h:70
int open(unsigned int pin, unsigned int direction, bool unexport=true)
void write(bool value)
Definition: Gpio.h:84
Definition: Gpio.h:32
bool enabled()
Definition: Gpio.h:97
bool read()
Definition: Gpio.h:63
void close()