summaryrefslogtreecommitdiff
path: root/include/arch/stm32f446re-nucleo/driver/gpio.h
blob: b98078d527664f29da08be21629db17e4f461d6d (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
#ifndef GPIO_H
#define GPIO_H

#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>

class GPIO {
	private:
		GPIO(const GPIO &copy);

	public:
		GPIO () {}

		enum Pin : unsigned char {
			pa_0 = 0, pa_1, pa_2, pa_3, pa_4, pa_5, pa_6, pa_7,
			pa_8, pa_9, pa_10, pa_11, pa_12, pa_13, pa_14, pa_15,
			pb_0, pb_1, pb_2, pb_3, pb_4, pb_5, pb_6, pb_7,
			pb_8, pb_9, pb_10, pb_11, pb_12, pb_13, pb_14, pb_15,
			pc_0, pc_1, pc_2, pc_3, pc_4, pc_5, pc_6, pc_7,
			pc_8, pc_9, pc_10, pc_11, pc_12, pc_13, pc_14, pc_15,
			PIN_INVALID
		};

		inline void setup() {
			rcc_periph_clock_enable(RCC_GPIOA);
			rcc_periph_clock_enable(RCC_GPIOB);
			rcc_periph_clock_enable(RCC_GPIOC);

			// Set LED as output
			gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO5);
		}
#pragma GCC diagnostic ignored "-Wunused-parameter"
		inline void led_on(unsigned char id) {
			gpio_set(GPIOA, GPIO5);
		}
		inline void led_off(unsigned char id) {
			gpio_clear(GPIOA, GPIO5);
		}
		inline void led_toggle(unsigned char id) {
			gpio_toggle(GPIOA, GPIO5);
		}
#pragma GCC diagnostic pop
		inline void input(unsigned char const pin) {
			if (pin < pb_0) {
				gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, 1 << pin);
			} else if (pin < pc_0) {
				gpio_mode_setup(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_NONE, 1 << (pin - pb_0));
			} else if (pin < PIN_INVALID) {
				gpio_mode_setup(GPIOC, GPIO_MODE_INPUT, GPIO_PUPD_NONE, 1 << (pin - pc_0));
			}
		}
		inline void input(unsigned char const pin, unsigned char const pull) {
		}
		inline void output(unsigned char const pin) {
			if (pin < pb_0) {
				gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, 1 << pin);
			} else if (pin < pc_0) {
				gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, 1 << (pin - pb_0));
			} else if (pin < PIN_INVALID) {
				gpio_mode_setup(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, 1 << (pin - pc_0));
			}
		}
		/*
		inline void output(unsigned char const pin, unsigned char const value) {
		}*/
		inline unsigned int read(unsigned char const pin) {
			if (pin < pb_0) {
				return gpio_get(GPIOA, 1 << pin);
			} else if (pin < pc_0) {
				return gpio_get(GPIOB, 1 << (pin-pb_0));
			} else if (pin < PIN_INVALID) {
				return gpio_get(GPIOC, 1 << (pin-pc_0));
			}
		}
		inline void write(unsigned char const pin, unsigned char value) {
			if (pin < pb_0) {
				if (value) {
					gpio_port_write(GPIOA, gpio_port_read(GPIOA) | (1 << pin));
				} else {
					gpio_port_write(GPIOA, gpio_port_read(GPIOA) & ~(1 << pin));
				}
			} else if (pin < pc_0) {
				if (value) {
					gpio_port_write(GPIOB, gpio_port_read(GPIOB) | (1 << (pin-pb_0)));
				} else {
					gpio_port_write(GPIOB, gpio_port_read(GPIOB) & ~(1 << (pin-pb_0)));
				}
			} else if (pin < PIN_INVALID) {
				if (value) {
					gpio_port_write(GPIOC, gpio_port_read(GPIOC) | (1 << (pin-pc_0)));
				} else {
					gpio_port_write(GPIOC, gpio_port_read(GPIOC) & ~(1 << (pin-pc_0)));
				}
			}
		}
};

extern GPIO gpio;

#endif