blob: 8594ba473182bc3723709252ec8bc459a706538c [file] [log] [blame]
Brian Silverman1662a0e2013-12-19 17:50:01 -08001#include "bbb/gpios.h"
2
3#include <stdio.h>
4#include <stdlib.h>
Daniel Petti23dcf6c2013-12-19 08:56:41 -08005
6#include "aos/common/logging/logging_impl.h"
Daniel Petti23dcf6c2013-12-19 08:56:41 -08007
8namespace bbb {
9
Daniel Pettib23501c2014-01-06 16:57:52 -080010Pin::Pin() {}
11
Brian Silverman1662a0e2013-12-19 17:50:01 -080012Pin::~Pin() {
13 // Unexport the pin.
14 if ((handle_ = fopen("/sys/class/gpio/unexport", "ab")) == NULL) {
Daniel Pettid6ff3d52014-01-02 11:24:39 -080015 LOG(WARNING, "Could not open file to unexport pin.\n");
Brian Silverman1662a0e2013-12-19 17:50:01 -080016 // There's nothing intelligent we can really do here.
17 return;
Daniel Petti23dcf6c2013-12-19 08:56:41 -080018 }
19
Daniel Pettid6ff3d52014-01-02 11:24:39 -080020 fprintf(handle_, "%d", kernel_pin_);
Brian Silverman1662a0e2013-12-19 17:50:01 -080021 fclose(handle_);
22}
Daniel Petti23dcf6c2013-12-19 08:56:41 -080023
Daniel Pettid6ff3d52014-01-02 11:24:39 -080024bool Pin::InitPin(int bank, int pin) {
25 kernel_pin_ = bank * 32 + pin;
Daniel Petti23dcf6c2013-12-19 08:56:41 -080026
Brian Silverman1662a0e2013-12-19 17:50:01 -080027 // Export the pin.
28 if ((handle_ = fopen("/sys/class/gpio/export", "ab")) == NULL) {
Daniel Pettid6ff3d52014-01-02 11:24:39 -080029 LOG(ERROR, "Could not open file for exporting pin.\n");
30 return false;
Daniel Petti23dcf6c2013-12-19 08:56:41 -080031 }
32
Daniel Pettid6ff3d52014-01-02 11:24:39 -080033 fprintf(handle_, "%d", kernel_pin_);
Brian Silverman1662a0e2013-12-19 17:50:01 -080034 fclose(handle_);
Daniel Pettid6ff3d52014-01-02 11:24:39 -080035 return true;
Brian Silverman1662a0e2013-12-19 17:50:01 -080036}
Daniel Petti23dcf6c2013-12-19 08:56:41 -080037
Daniel Pettid6ff3d52014-01-02 11:24:39 -080038bool Pin::DoPinDirSet(int direction) {
39 char type_path[64];
Brian Silverman1662a0e2013-12-19 17:50:01 -080040 snprintf(type_path, sizeof(type_path), "/sys/class/gpio/gpio%d/direction",
41 kernel_pin_);
42
43 if ((handle_ = fopen(type_path, "rb+")) == NULL) {
Daniel Pettid6ff3d52014-01-02 11:24:39 -080044 LOG(ERROR, "Unable open file for pin direction setting.\n");
45 return false;
Daniel Petti23dcf6c2013-12-19 08:56:41 -080046 }
47
Brian Silverman1662a0e2013-12-19 17:50:01 -080048 switch (direction) {
49 case 1:
Daniel Pettid6ff3d52014-01-02 11:24:39 -080050 // Input.
51 fprintf(handle_, "in");
Brian Silverman1662a0e2013-12-19 17:50:01 -080052 break;
53 case 2:
Daniel Pettid6ff3d52014-01-02 11:24:39 -080054 // Output.
55 // If it's an output, we should specify an initial direction.
56 fprintf(handle_, "low");
Brian Silverman1662a0e2013-12-19 17:50:01 -080057 break;
Brian Silverman1662a0e2013-12-19 17:50:01 -080058 default:
59 LOG(ERROR, "Invalid direction identifier %d.\n", direction);
Daniel Pettid6ff3d52014-01-02 11:24:39 -080060 fclose(handle_);
61 return false;
Brian Silverman1662a0e2013-12-19 17:50:01 -080062 }
Daniel Pettid6ff3d52014-01-02 11:24:39 -080063
Brian Silverman1662a0e2013-12-19 17:50:01 -080064 fclose(handle_);
Daniel Pettid6ff3d52014-01-02 11:24:39 -080065 return true;
Brian Silverman1662a0e2013-12-19 17:50:01 -080066}
67
68} // namespace bbb