diff options
Diffstat (limited to 'include/arch/arduino-nano/driver/gpio.h')
-rw-r--r-- | include/arch/arduino-nano/driver/gpio.h | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/include/arch/arduino-nano/driver/gpio.h b/include/arch/arduino-nano/driver/gpio.h new file mode 100644 index 0000000..6acf977 --- /dev/null +++ b/include/arch/arduino-nano/driver/gpio.h @@ -0,0 +1,111 @@ +#ifndef GPIO_H +#define GPIO_H + +#include <avr/io.h> + +class GPIO { + private: + GPIO(const GPIO ©); + + public: + GPIO () {} + + enum Pin : unsigned char { + pb0 = 8, + pb1 = 9, + pb2 = 10, + pb3 = 11, + pb4 = 12, + pb5 = 13, + pb6 = 14, + pb7 = 15, + pc0 = 16, + pc1 = 17, + pc2 = 18, + pc3 = 19, + pc4 = 20, + pc5 = 21, + pc6 = 22, + pd0 = 24, + pd1 = 25, + pd2 = 26, + pd3 = 27, + pd4 = 28, + pd5 = 29, + pd6 = 30, + pd7 = 31 + }; + + inline void setup() { + DDRB = _BV(PB5); + } + inline void led_on(unsigned char id) { + PORTB |= _BV(PB5); + } + inline void led_off(unsigned char id) { + PORTB &= ~_BV(PB5); + } + inline void led_toggle(unsigned char id) { + PINB = _BV(PB5); + } + inline void input(unsigned char const pin) { + if (pin < 8) { + } else if (pin < 16) { + DDRB &= ~_BV(pin - 8); + } else if (pin < 24) { + DDRC &= ~_BV(pin - 16); + } else if (pin < 32) { + DDRD &= ~_BV(pin - 24); + } + } + inline void output(unsigned char const pin) { + if (pin < 8) { + } else if (pin < 16) { + DDRB |= _BV(pin - 8); + } else if (pin < 24) { + DDRC |= _BV(pin - 16); + } else if (pin < 32) { + DDRD |= _BV(pin - 24); + } + } + inline unsigned char read(unsigned char const pin) { + if (pin < 8) { + } + if (pin < 16) { + return (PINB & _BV(pin - 8)); + } + if (pin < 24) { + return (PINC & _BV(pin - 16)); + } + if (pin < 32) { + return (PIND & _BV(pin - 24)); + } + return 0; + } + inline void write(unsigned char const pin, unsigned char value) { + if (pin < 8) { + } else if (pin < 16) { + if (value) { + PORTB |= _BV(pin - 8); + } else { + PORTB &= ~_BV(pin - 8); + } + } else if (pin < 24) { + if (value) { + PORTB |= _BV(pin - 16); + } else { + PORTB &= ~_BV(pin - 16); + } + } else if (pin < 32) { + if (value) { + PORTB |= _BV(pin - 24); + } else { + PORTB &= ~_BV(pin - 24); + } + } + } +}; + +extern GPIO gpio; + +#endif |