The Pedigree Project  0.1
Gpio.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef _GPIO_H
21 #define _GPIO_H
22 
23 #include "pedigree/kernel/Log.h"
24 #include "pedigree/kernel/processor/MemoryRegion.h"
25 #include "pedigree/kernel/processor/PhysicalMemoryManager.h"
26 #include "pedigree/kernel/processor/VirtualAddressSpace.h"
27 #include "pedigree/kernel/processor/types.h"
28 
30 class Gpio
31 {
32  public:
33  Gpio()
34  : m_Gpio1("GPIO1"), m_Gpio2("GPIO2"), m_Gpio3("GPIO3"),
35  m_Gpio4("GPIO4"), m_Gpio5("GPIO5"), m_Gpio6("GPIO6")
36  {
37  }
38  ~Gpio()
39  {
40  }
41 
42  static Gpio &instance()
43  {
44  return m_Instance;
45  }
46 
47  void initialise(
48  uintptr_t gpio1, uintptr_t gpio2, uintptr_t gpio3, uintptr_t gpio4,
49  uintptr_t gpio5, uintptr_t gpio6)
50  {
51  if (!PhysicalMemoryManager::instance().allocateRegion(
52  m_Gpio1, 1,
56  gpio1))
57  {
58  ERROR("GPIO: Couldn't get a memory region!");
59  return;
60  }
62  1, reinterpret_cast<volatile uint32_t *>(m_Gpio1.virtualAddress()));
63 
64  if (!PhysicalMemoryManager::instance().allocateRegion(
65  m_Gpio2, 1,
69  gpio2))
70  {
71  ERROR("GPIO: Couldn't get a memory region!");
72  return;
73  }
75  2, reinterpret_cast<volatile uint32_t *>(m_Gpio2.virtualAddress()));
76 
77  if (!PhysicalMemoryManager::instance().allocateRegion(
78  m_Gpio3, 1,
82  gpio3))
83  {
84  ERROR("GPIO: Couldn't get a memory region!");
85  return;
86  }
88  3, reinterpret_cast<volatile uint32_t *>(m_Gpio3.virtualAddress()));
89 
90  if (!PhysicalMemoryManager::instance().allocateRegion(
91  m_Gpio4, 1,
95  gpio4))
96  {
97  ERROR("GPIO: Couldn't get a memory region!");
98  return;
99  }
100  initspecific(
101  4, reinterpret_cast<volatile uint32_t *>(m_Gpio4.virtualAddress()));
102 
103  if (!PhysicalMemoryManager::instance().allocateRegion(
104  m_Gpio5, 1,
108  gpio5))
109  {
110  ERROR("GPIO: Couldn't get a memory region!");
111  return;
112  }
113  initspecific(
114  5, reinterpret_cast<volatile uint32_t *>(m_Gpio5.virtualAddress()));
115 
116  if (!PhysicalMemoryManager::instance().allocateRegion(
117  m_Gpio6, 1,
121  gpio6))
122  {
123  ERROR("GPIO: Couldn't get a memory region!");
124  return;
125  }
126  initspecific(
127  6, reinterpret_cast<volatile uint32_t *>(m_Gpio6.virtualAddress()));
128  }
129 
130  void clearpin(int pin);
131 
132  void drivepin(int pin);
133 
134  bool pinstate(int pin);
135 
136  int capturepin(int pin);
137 
138  void enableoutput(int pin);
139 
140  private:
142  void initspecific(int n, volatile uint32_t *gpio);
143 
146  volatile uint32_t *getGpioForPin(int pin, int *bit);
147 
148  MemoryRegion m_Gpio1;
149  MemoryRegion m_Gpio2;
150  MemoryRegion m_Gpio3;
151  MemoryRegion m_Gpio4;
152  MemoryRegion m_Gpio5;
153  MemoryRegion m_Gpio6;
154 
155  static Gpio m_Instance;
156 };
157 
158 #endif
void initspecific(int n, volatile uint32_t *gpio)
Initialises a specific GPIO to a given set of defaults.
Definition: Gpio.cc:112
static PhysicalMemoryManager & instance()
volatile uint32_t * getGpioForPin(int pin, int *bit)
Definition: Gpio.cc:137
Definition: Gpio.h:30
Special memory entity in the kernel&#39;s virtual address space.
Definition: MemoryRegion.h:35
void * virtualAddress() const
Definition: MemoryRegion.cc:39
#define ERROR(text)
Definition: Log.h:82