summaryrefslogtreecommitdiff
path: root/include/arch/posix/driver/gpio.h
blob: c2f332b9d33407bd75733531283ca2d81706cd57 (plain)
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
/*
 * Copyright 2020 Birte Kristina Friesel
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */
#ifndef GPIO_H
#define GPIO_H

#include <stdio.h>

class GPIO {
	private:
		GPIO(const GPIO &copy);
		unsigned int pin_dir, pin_pull, pin_out, pull_dir;
#ifdef GPIO_TRACE
		void print_pinstate() {
			unsigned int mask = 1;
			for (unsigned char i=0; i < 32; i++) {
				if (i == 0) {
					fputs("▶ LED ", stdout);
				}
				else if (i == 8) {
					fputs("  GPIO ", stdout);
				}
				else if (i == 16) {
					fputs("  ", stdout);
				}
				else if (i == 24) {
					fputs("  ", stdout);
				}
				if (pin_dir & mask) {
					if (pin_out & (1<<i)) {
						fputs("█", stdout);
					} else {
						putc(' ', stdout);
					}
				} else {
					if (pin_pull & mask) {
						if (pull_dir & mask) {
							fputs("↑", stdout);
						} else {
							fputs("↓", stdout);
						}
					} else {
						fputs("·", stdout);
					}
				}
				mask <<= 1;
			}
			putc('\n', stdout);
		}
#else
		inline void print_pinstate() {}
#endif

	public:
		GPIO () : pin_dir(0), pin_pull(0), pin_out(0), pull_dir(0) {}

		enum Pin : unsigned char {
			pl0 = 0, pl1, pl2, pl3, pl4, pl5, pl6, pl7,
			px00, px01, px02, px03, px04, px05, px06, px07,
			px08, px09, px10, px11, px12, px13, px14, px15,
			px16, px17, px18, px19, px20, px21, px22, px23
		};

		inline void setup() {
			pin_dir |= 0x0000ff;
		}
		inline void led_on(unsigned char id = 0) {
			if (id <= pl7) {
				pin_out |= (1 << id);
			}
			print_pinstate();
		}
		inline void led_off(unsigned char id = 0) {
			if (id <= pl7) {
				pin_out &= ~(1 << id);
			}
			print_pinstate();
		}
		inline void led_toggle(unsigned char id = 0) {
			if (id <= pl7) {
				if (pin_out & (1 << id)) {
					led_off(id);
				} else {
					led_on(id);
				}
			}
		}
		inline void input(unsigned char const pin) {
			pin_dir &= ~(1<<pin);
			print_pinstate();
		}
		inline void input(unsigned char const pin, unsigned char const pull) {
			pin_dir &= ~(1<<pin);
			pin_pull |= (1<<pin);
			if (pull) {
				pull_dir |= (1<<pin);
			} else {
				pull_dir &= ~(1<<pin);
			}
			print_pinstate();
		}
		inline void output(unsigned char const pin) {
			pin_dir |= (1<<pin);
			print_pinstate();
		}
		inline void output(unsigned char const pin, unsigned char const value) {
			pin_dir |= (1<<pin);
			if (value)
				pin_out |= (1<<pin);
			else
				pin_out &= ~(1<<pin);
			print_pinstate();
		}
#pragma GCC diagnostic ignored "-Wunused-parameter"
		inline unsigned char read(unsigned char const pin) {
			return 1;
		}
#pragma GCC diagnostic pop
		inline void write(unsigned char const pin, unsigned char value) {
			if (value)
				pin_out |= (1<<pin);
			else
				pin_out &= ~(1<<pin);
			print_pinstate();
		}
		inline void enable_int() {}
		inline void disable_int() {}
};

extern GPIO gpio;

#endif