Numworks Epsilon  1.4.1
Graphing Calculator Operating System
gpio.h
Go to the documentation of this file.
1 #ifndef REGS_GPIO_H
2 #define REGS_GPIO_H
3 
4 #include "register.h"
5 
6 class GPIO {
7 public:
8  class MODER : public Register32 {
9  public:
10  enum class Mode {
11  Input = 0,
12  Output = 1,
14  Analog = 3
15  };
16  Mode getMode(int index) { return (Mode)getBitRange(2*index+1, 2*index); }
17  void setMode(int index, Mode mode) volatile { setBitRange(2*index+1, 2*index, (uint32_t)mode); }
18  };
19 
20  class OTYPER : Register32 {
21  public:
22  enum class Type {
23  PushPull = 0,
24  OpenDrain = 1
25  };
26  Type getType(int index) { return (Type)getBitRange(index, index); }
27  void setType(int index, Type type) volatile { setBitRange(index, index, (uint32_t)type); }
28  };
29 
30  class PUPDR : public Register32 {
31  public:
32  enum class Pull {
33  None = 0,
34  Up = 1,
35  Down = 2,
36  Reserved = 3
37  };
38  Pull getPull(int index) { return (Pull)getBitRange(2*index+1, 2*index); }
39  void setPull(int index, Pull pull) volatile { setBitRange(2*index+1, 2*index, (uint32_t)pull); }
40  };
41 
42  class IDR : public Register32 {
43  public:
44  bool get(int index) volatile { return (bool)getBitRange(index, index); }
45  };
46 
47  class ODR : public Register32 {
48  public:
49  bool get(int index) volatile { return (bool)getBitRange(index, index); }
50  void set(int index, bool state) volatile { setBitRange(index, index, state); }
51  };
52 
53  class AFR : Register64 {
54  /* The AFR register doesn't exist per-se in the documentation. It is the
55  * consolidation of AFRL and AFRH which are two 32 bits registers. */
56  public:
57  enum class AlternateFunction {
58  AF0 = 0, AF1 = 1, AF2 = 2, AF3 = 3,
59  AF4 = 4, AF5 = 5, AF6 = 6, AF7 = 7,
60  AF8 = 8, AF9 = 9, AF10 = 10, AF11 = 11,
61  AF12 = 12, AF13 = 13, AF14 = 14, AF15 = 15
62  };
64  return (AlternateFunction)getBitRange(4*index+3, 4*index);
65  }
66  void setAlternateFunction(int index, AlternateFunction af) volatile {
67  setBitRange(4*index+3, 4*index, (uint64_t)af);
68  }
69  };
70 
71  constexpr GPIO(int i) : m_index(i) {}
72  constexpr operator int() const { return m_index; }
73  REGS_REGISTER_AT(MODER, 0x00);
74  REGS_REGISTER_AT(OTYPER, 0x04);
75  REGS_REGISTER_AT(PUPDR, 0x0C);
76  REGS_REGISTER_AT(IDR, 0x10);
77  REGS_REGISTER_AT(ODR, 0x14);
78  REGS_REGISTER_AT(AFR, 0x20);
79 private:
80  constexpr uint32_t Base() const {
81  return 0x40020000 + 0x400*m_index;
82  };
83  int m_index;
84 };
85 
86 constexpr GPIO GPIOA(0);
87 constexpr GPIO GPIOB(1);
88 constexpr GPIO GPIOC(2);
89 constexpr GPIO GPIOD(3);
90 constexpr GPIO GPIOE(4);
91 constexpr GPIO GPIOF(5);
92 constexpr GPIO GPIOG(6);
93 constexpr GPIO GPIOH(7);
94 
95 class GPIOPin {
96 public:
97  constexpr GPIOPin(GPIO group, uint8_t pin) : m_data(((group&0xF) << 4) | (pin&0xF)) {}
98  GPIO group() const { return GPIO(m_data>>4); }
99  uint8_t pin() const { return m_data & 0xF; }
100 private:
101  uint8_t m_data;
102 };
103 
104 #endif
uint8_t pin() const
Definition: gpio.h:99
Definition: gpio.h:53
Definition: gpio.h:6
constexpr GPIO GPIOH(7)
constexpr GPIO GPIOG(6)
constexpr GPIOPin(GPIO group, uint8_t pin)
Definition: gpio.h:97
unsigned char uint8_t
Definition: stdint.h:4
Pull getPull(int index)
Definition: gpio.h:38
Definition: gpio.h:47
constexpr GPIO GPIOB(1)
void setType(int index, Type type) volatile
Definition: gpio.h:27
REGS_REGISTER_AT(MODER, 0x00)
Type getType(int index)
Definition: gpio.h:26
unsigned int uint32_t
Definition: stdint.h:6
AlternateFunction
Definition: gpio.h:57
unsigned long long uint64_t
Definition: stdint.h:7
void setAlternateFunction(int index, AlternateFunction af) volatile
Definition: gpio.h:66
constexpr GPIO GPIOE(4)
Mode getMode(int index)
Definition: gpio.h:16
constexpr GPIO GPIOD(3)
void setPull(int index, Pull pull) volatile
Definition: gpio.h:39
constexpr GPIO GPIOF(5)
void setBitRange(uint8_t high, uint8_t low, T value) volatile
Definition: register.h:21
Definition: gpio.h:95
Definition: gpio.h:42
GPIO group() const
Definition: gpio.h:98
void setMode(int index, Mode mode) volatile
Definition: gpio.h:17
constexpr GPIO GPIOA(0)
AlternateFunction getAlternateFunction(int index)
Definition: gpio.h:63
T getBitRange(uint8_t high, uint8_t low) volatile
Definition: register.h:24
constexpr GPIO(int i)
Definition: gpio.h:71
constexpr GPIO GPIOC(2)