blob: 4f9dce3c1e52b03debe7df36b25369362ff9da18 [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
Brian Silverman1662a0e2013-12-19 17:50:01 -080010Pin::~Pin() {
11 // Unexport the pin.
12 if ((handle_ = fopen("/sys/class/gpio/unexport", "ab")) == NULL) {
Daniel Pettid6ff3d52014-01-02 11:24:39 -080013 LOG(WARNING, "Could not open file to unexport pin.\n");
Brian Silverman1662a0e2013-12-19 17:50:01 -080014 // There's nothing intelligent we can really do here.
15 return;
Daniel Petti23dcf6c2013-12-19 08:56:41 -080016 }
17
Daniel Pettid6ff3d52014-01-02 11:24:39 -080018 fprintf(handle_, "%d", kernel_pin_);
Brian Silverman1662a0e2013-12-19 17:50:01 -080019 fclose(handle_);
20}
Daniel Petti23dcf6c2013-12-19 08:56:41 -080021
Daniel Pettid6ff3d52014-01-02 11:24:39 -080022bool Pin::InitPin(int bank, int pin) {
23 kernel_pin_ = bank * 32 + pin;
Daniel Petti23dcf6c2013-12-19 08:56:41 -080024
Brian Silverman1662a0e2013-12-19 17:50:01 -080025 // Export the pin.
26 if ((handle_ = fopen("/sys/class/gpio/export", "ab")) == NULL) {
Daniel Pettid6ff3d52014-01-02 11:24:39 -080027 LOG(ERROR, "Could not open file for exporting pin.\n");
28 return false;
Daniel Petti23dcf6c2013-12-19 08:56:41 -080029 }
30
Daniel Pettid6ff3d52014-01-02 11:24:39 -080031 fprintf(handle_, "%d", kernel_pin_);
Brian Silverman1662a0e2013-12-19 17:50:01 -080032 fclose(handle_);
Daniel Pettid6ff3d52014-01-02 11:24:39 -080033 return true;
Brian Silverman1662a0e2013-12-19 17:50:01 -080034}
Daniel Petti23dcf6c2013-12-19 08:56:41 -080035
Daniel Pettid6ff3d52014-01-02 11:24:39 -080036bool Pin::DoPinDirSet(int direction) {
37 char type_path[64];
Brian Silverman1662a0e2013-12-19 17:50:01 -080038 snprintf(type_path, sizeof(type_path), "/sys/class/gpio/gpio%d/direction",
39 kernel_pin_);
40
41 if ((handle_ = fopen(type_path, "rb+")) == NULL) {
Daniel Pettid6ff3d52014-01-02 11:24:39 -080042 LOG(ERROR, "Unable open file for pin direction setting.\n");
43 return false;
Daniel Petti23dcf6c2013-12-19 08:56:41 -080044 }
45
Brian Silverman1662a0e2013-12-19 17:50:01 -080046 switch (direction) {
47 case 1:
Daniel Pettid6ff3d52014-01-02 11:24:39 -080048 // Input.
49 fprintf(handle_, "in");
Brian Silverman1662a0e2013-12-19 17:50:01 -080050 break;
51 case 2:
Daniel Pettid6ff3d52014-01-02 11:24:39 -080052 // Output.
53 // If it's an output, we should specify an initial direction.
54 fprintf(handle_, "low");
Brian Silverman1662a0e2013-12-19 17:50:01 -080055 break;
Brian Silverman1662a0e2013-12-19 17:50:01 -080056 default:
57 LOG(ERROR, "Invalid direction identifier %d.\n", direction);
Daniel Pettid6ff3d52014-01-02 11:24:39 -080058 fclose(handle_);
59 return false;
Brian Silverman1662a0e2013-12-19 17:50:01 -080060 }
Daniel Pettid6ff3d52014-01-02 11:24:39 -080061
Brian Silverman1662a0e2013-12-19 17:50:01 -080062 fclose(handle_);
Daniel Pettid6ff3d52014-01-02 11:24:39 -080063 return true;
Brian Silverman1662a0e2013-12-19 17:50:01 -080064}
65
66} // namespace bbb