-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgpio.h
149 lines (128 loc) · 6.02 KB
/
gpio.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
PINE 64 - C++ lib 1
Copyright (c) 2017 Daniele Contarino <[email protected]> i111i
This library is based over work by: .ft .; .tf.
Eric Ptak <[email protected]> ft. ,fft. .ff
Stefan Mavrodiev @ OLIMEX LTD <[email protected]> 1fffffffffff1
Kamil Trzcinski <[email protected]> ,LLf. ,fffffff, ,LLL,
Permission is hereby granted, free of charge, to any person obtaining a copy of ,LLLLLLLi iLLLLLLL,
this software and associated documentation files (the "Software"), to deal in ,LLL; 1LLLLLi ;LLL,
the Software without restriction, including without limitation the rights to :LLLLLLLLLLLLLLL:
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies .LLL; 1LLLLLLLi ;LLL
of the Software, and to permit persons to whom the Software is furnished to do :LLLLLLL: :LLLLLLL,
so, subject to the following conditions: iLLf. :LLLLL, .LLL;
The above copyright notice and this permission notice shall be included in all 1LLLLLLLLLLLLLi
copies or substantial portions of the Software. :LLLLL:
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR L
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, L
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef PINE64_GPIO_H
#define PINE64_GPIO_H
#define BCM2708_PERI_BASE_DEFAULT 0x20000000
#define BCM2709_PERI_BASE_DEFAULT 0x3f000000
#define GPIO_BASE_OFFSET 0x200000
#define FSEL_OFFSET 0 // 0x0000
#define SET_OFFSET 7 // 0x001c / 4
#define CLR_OFFSET 10 // 0x0028 / 4
#define PINLEVEL_OFFSET 13 // 0x0034 / 4
#define EVENT_DETECT_OFFSET 16 // 0x0040 / 4
#define RISING_ED_OFFSET 19 // 0x004c / 4
#define FALLING_ED_OFFSET 22 // 0x0058 / 4
#define HIGH_DETECT_OFFSET 25 // 0x0064 / 4
#define LOW_DETECT_OFFSET 28 // 0x0070 / 4
#define PULLUPDN_OFFSET 37 // 0x0094 / 4
#define PULLUPDNCLK_OFFSET 38 // 0x0098 / 4
#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)
#define PINEA64_GPIO_MASK (0xFFFFFF80)
#define SUNXI_GPIO_BASE 0x01C20000
#define SUNXI_GPIO_REG_OFFSET 0x800
#define PINEA64_GPIO_BASE (SUNXI_GPIO_BASE + SUNXI_GPIO_REG_OFFSET)
#define SUNXI_CFG_OFFSET 0x00
#define SUNXI_DATA_OFFSET 0x10
#define SUNXI_PUD_OFFSET 0x1C
#define SUNXI_BANK_SIZE 0x24
#define MAP_SIZE (4096*2)
#define MAP_MASK (MAP_SIZE - 1)
#define GPIO_BANK(pin) ((pin) >> 5)
#define GPIO_NUM(pin) ((pin) & 0x1F)
#define GPIO_CFG_INDEX(pin) (((pin) & 0x1F) >> 3)
#define GPIO_CFG_OFFSET(pin) ((((pin) & 0x1F) & 0x7) << 2)
#define GPIO_PUL_INDEX(pin) (((pin) & 0x1F )>> 4)
#define GPIO_PUL_OFFSET(pin) (((pin) & 0x0F) << 1)
#define SETUP_OK 0
#define SETUP_DEVMEM_FAIL 1
#define SETUP_MALLOC_FAIL 2
#define SETUP_MMAP_FAIL 3
#define SETUP_CPUINFO_FAIL 4
#define SETUP_NOT_RPI_FAIL 5
#define INPUT 1 // is really 0 for control register!
#define OUTPUT 0 // is really 1 for control register!
#define ALT0 4
#define HIGH 1
#define LOW 0
#define PUD_OFF 0
#define PUD_UP 1
#define PUD_DOWN 2
namespace Pine64 {
typedef struct sunxiGPIO_t {
unsigned int CFG[4];
unsigned int DAT;
unsigned int DRV[2];
unsigned int PULL[2];
} sunxiGPIO;
/* gpio interrupt control */
typedef struct sunxiGPIOInt_t {
unsigned int CFG[3];
unsigned int CTL;
unsigned int STA;
unsigned int DEB;
} ssunxiGPIOInt;
typedef struct sunxiGPIOReg_t {
struct sunxiGPIO_t gpio_bank[9];
unsigned char res[0xbc];
struct sunxiGPIOInt_t gpio_int;
} sunxiGPIOReg;
class GPIO {
private :
uint64_t _epochMilli, _epochMicro;
volatile uint32_t* _pioMap;
volatile uint32_t* _gpioMap;
int _pinea64Found;
static void _shortWait(void) ;
void _clearEventDetect(int gpio);
uint32_t _sunxiReadl(volatile uint32_t *addr);
void _sunxiWritel(volatile uint32_t *addr, uint32_t val);
void _setPullupdn(int gpio, int pud);
public :
GPIO();
~GPIO(void);
//Manage GPIO
int setup(void);
void pinMode(int gpio, int direction);
void pinMode(int gpio, int direction, int pud);
int digitalRead(int gpio);
void digitalWrite(int gpio, int value);
int GPIOFunction(int gpio);
//Event
void setRisingEvent(int gpio, int enable);
void setFallingEvent(int gpio, int enable);
void setHighEvent(int gpio, int enable);
void setLowEvent(int gpio, int enable);
int eventDetected(int gpio);
void cleanup(void);
//Time function
void initialiseEpoch (void);
static void delayMicrosecondsHard (unsigned int howLong);
static void delayMicroseconds (unsigned int howLong);
static void delay (unsigned int howLong);
unsigned int millis (void);
unsigned int micros (void);
};
}
#endif