|
| 1 | +#include "Gpio.h" |
| 2 | + |
| 3 | +Gpio::Gpio(bool enable_spi) { |
| 4 | + if (this->init_gpio() == NO_ERROR && enable_spi) { |
| 5 | + this->init_spi(); |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +Gpio::~Gpio() { |
| 10 | + this->close_gpio(); |
| 11 | +} |
| 12 | + |
| 13 | +int Gpio::init_gpio() { |
| 14 | + int errnum = NO_ERROR; |
| 15 | + // Start the BCM2835 Library to access GPIO |
| 16 | + if (!bcm2835_init()) { |
| 17 | + cerr << "bcm2835_init failed (Are you running as root?)" << endl; |
| 18 | + errnum = BCM2835_INIT_ERROR; |
| 19 | + exit(errnum); |
| 20 | + } |
| 21 | + return errnum; |
| 22 | +} |
| 23 | + |
| 24 | +int Gpio::init_spi() { |
| 25 | + int errnum = NO_ERROR; |
| 26 | + // Enable SPI bus |
| 27 | + if (!bcm2835_spi_begin()) { |
| 28 | + cerr << "bcm2835_spi_begin failed (Are you running as root?)" << endl; |
| 29 | + errnum = BCM2835_INIT_SPI_ERROR; |
| 30 | + exit(errnum); |
| 31 | + } |
| 32 | + return errnum; |
| 33 | +} |
| 34 | + |
| 35 | +int Gpio::close_gpio() { |
| 36 | + int errnum = NO_ERROR; |
| 37 | + if (!bcm2835_close()) { |
| 38 | + cerr << "bcm2835_close failed (Are you running as root?)" << endl; |
| 39 | + errnum = BCM2835_CLOSE_ERROR; |
| 40 | + exit(errnum); |
| 41 | + } |
| 42 | + return errnum; |
| 43 | +} |
| 44 | + |
| 45 | +void Gpio::setup_pwm(GpioPwmClock clock) { |
| 46 | + std::uint32_t clock_static = static_cast<std::uint32_t>(clock); |
| 47 | + |
| 48 | + bcm2835_pwm_set_clock(clock_static); |
| 49 | +} |
| 50 | + |
| 51 | +void Gpio::setup_pin(GpioPin pin) { |
| 52 | + std::uint8_t funStatic = static_cast<std::uint8_t>(pin.function); |
| 53 | + std::uint8_t pullUpStatic = static_cast<std::uint8_t>(pin.pullUp); |
| 54 | + std::uint8_t pinStatic = static_cast<std::uint8_t>(pin.name); |
| 55 | + Feature feature = getFeature(pin); |
| 56 | + LOG("set_direction(function=%d, pullUp=%d) on pin:%d", unsigned(funStatic), unsigned(pullUpStatic), unsigned(pinStatic)); |
| 57 | + |
| 58 | + if (pin.name != GpioPinName::unknown) { |
| 59 | + // Set function |
| 60 | + if (feature == Feature::io) { |
| 61 | + bcm2835_gpio_fsel(pinStatic, funStatic); |
| 62 | + } else if (feature == Feature::pwm) { |
| 63 | + std::uint8_t channel = get_pwm_channel(pin); |
| 64 | + std::uint8_t mode_static = static_cast<std::uint8_t>(pin.pwmMode); |
| 65 | + std::uint8_t range = pin.pwmRange; |
| 66 | + |
| 67 | + if (channel >= 0 && pin.pwmMode != GpioPwmMode::unknown) { |
| 68 | + bcm2835_pwm_set_mode(channel, mode_static, 1 /* 1=enable */); |
| 69 | + } |
| 70 | + |
| 71 | + if (channel >= 0 && range > 0) { |
| 72 | + bcm2835_pwm_set_range(channel, range); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // if specified set PullUp resistor |
| 77 | + if (pin.pullUp != GpioPullUp::none) { |
| 78 | + LOG("override pull up setting"); |
| 79 | + bcm2835_gpio_set_pud(pinStatic, pullUpStatic); |
| 80 | + } else { |
| 81 | + LOG("leave pull up setting as is"); |
| 82 | + } |
| 83 | + } else { |
| 84 | + cerr << "setup_pin() pin name not initialised" << endl; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void Gpio::read(std::list<GpioPin*> pin_list) { |
| 89 | + for (GpioPin *pin : pin_list) { |
| 90 | + uint8_t pin_static = static_cast<std::uint8_t>(pin->name); |
| 91 | + pin->value = bcm2835_gpio_lev(pin_static); |
| 92 | + LOG("read pin:%d value:%d", pin_static, pin->value); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +std::list<GpioPin*> Gpio::get_pins_up(std::list<GpioPin*> pins_to_check) { |
| 97 | + std::list<GpioPin*> switches_pushed = {}; |
| 98 | + |
| 99 | + if (!pins_to_check.empty()) { |
| 100 | + read(pins_to_check); |
| 101 | + for (GpioPin *pin : pins_to_check) { |
| 102 | + // Keep pins up only |
| 103 | + if ((pin->value == 0 && pin->pullUp == GpioPullUp::up) || (pin->value != 0 && pin->pullUp != GpioPullUp::up)) { |
| 104 | + switches_pushed.push_back(pin); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return switches_pushed; |
| 110 | +} |
| 111 | + |
| 112 | +void Gpio::write(GpioPin* pin, uint8_t val) { |
| 113 | + std::uint8_t val_static = static_cast<std::uint8_t>(val); |
| 114 | + std::uint8_t pin_static = static_cast<std::uint8_t>(pin->name); |
| 115 | + std::uint8_t channel = get_pwm_channel(*pin); |
| 116 | + |
| 117 | + if (channel < 0) { |
| 118 | + bcm2835_gpio_write(pin_static, val_static); |
| 119 | + LOG("write pin:%d value:%d", pin_static, val_static); |
| 120 | + } else { |
| 121 | + bcm2835_pwm_set_data(channel, val_static); |
| 122 | + LOG("write PWM pin:%d value:%d", pin_static, val_static); |
| 123 | + } |
| 124 | + |
| 125 | + pin->value = val_static; |
| 126 | +} |
| 127 | + |
| 128 | +std::uint8_t Gpio::get_pwm_channel(GpioPin pin) { |
| 129 | + std::uint8_t channel = -1; |
| 130 | + GpioPinName name = pin.name; |
| 131 | + GpioFunction func = pin.function; |
| 132 | + |
| 133 | + if (getFeature(pin) != Feature::pwm) { |
| 134 | + channel = -1; |
| 135 | + } else if ((name == GpioPinName::gpio13 && func == GpioFunction::alt0) || |
| 136 | + (name == GpioPinName::gpio19 && func == GpioFunction::alt5)) { |
| 137 | + channel = 1; |
| 138 | + } else if (name == GpioPinName::gpio18 && func == GpioFunction::alt5) { |
| 139 | + channel = 0; |
| 140 | + } |
| 141 | + |
| 142 | + return channel; |
| 143 | +} |
| 144 | + |
| 145 | +Feature Gpio::getFeature(GpioPin pin) { |
| 146 | + Feature feature = Feature::unknown; |
| 147 | + GpioPinName name = pin.name; |
| 148 | + GpioFunction func = pin.function; |
| 149 | + |
| 150 | + if ((name != GpioPinName::unknown && func == GpioFunction::input) || |
| 151 | + (name != GpioPinName::unknown && func == GpioFunction::output)) { |
| 152 | + feature = Feature::io; |
| 153 | + } else if ((name == GpioPinName::gpio13 && func == GpioFunction::alt0) || |
| 154 | + (name == GpioPinName::gpio19 && func == GpioFunction::alt5) || |
| 155 | + (name == GpioPinName::gpio18 && func == GpioFunction::alt5)) { |
| 156 | + feature = Feature::pwm; |
| 157 | + } |
| 158 | + |
| 159 | + return feature; |
| 160 | +} |
0 commit comments