Merge branch 'feature/new-usb-stack' into edge
[clinton/Smoothieware.git] / src / libs / gpio.cpp
CommitLineData
7064cca7
MM
1#include "gpio.h"
2
3#include "LPC17xx.h"
4#include "lpc17xx_pinsel.h"
5#include "lpc17xx_gpio.h"
6
7GPIO::GPIO(PinName pin) {
8 this->port = (pin >> 5) & 7;
9 this->pin = pin & 0x1F;
10
11 setup();
12}
13
14GPIO::GPIO(uint8_t port, uint8_t pin) {
15 GPIO::port = port;
16 GPIO::pin = pin;
17
18 setup();
19}
20
21GPIO::GPIO(uint8_t port, uint8_t pin, uint8_t direction) {
22 GPIO::port = port;
23 GPIO::pin = pin;
24
25 setup();
26
27 set_direction(direction);
28}
29// GPIO::~GPIO() {}
30
31void GPIO::setup() {
32 PINSEL_CFG_Type PinCfg;
33 PinCfg.Funcnum = 0;
34 PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL;
35 PinCfg.Pinmode = PINSEL_PINMODE_TRISTATE;
36 PinCfg.Portnum = GPIO::port;
37 PinCfg.Pinnum = GPIO::pin;
38 PINSEL_ConfigPin(&PinCfg);
39}
40
41void GPIO::set_direction(uint8_t direction) {
42 FIO_SetDir(port, 1UL << pin, direction);
43}
44
45void GPIO::output() {
46 set_direction(1);
47}
48
49void GPIO::input() {
50 set_direction(0);
51}
52
53void GPIO::write(uint8_t value) {
54 output();
55 if (value)
56 set();
57 else
58 clear();
59}
60
61void GPIO::set() {
62 FIO_SetValue(port, 1UL << pin);
63}
64
65void GPIO::clear() {
66 FIO_ClearValue(port, 1UL << pin);
67}
68
69uint8_t GPIO::get() {
70 return (FIO_ReadValue(port) & (1UL << pin))?255:0;
71}
72
73int GPIO::operator=(int value) {
74 if (value)
75 set();
76 else
77 clear();
78 return value;
79}