blob: 0ec3051eb47eb7ec6624245beaee60df4401f2ff [file] [log] [blame]
Brian Silverman1662a0e2013-12-19 17:50:01 -08001#include "bbb/gpios.h"
2
3#include <stdio.h>
4#include <stdlib.h>
Brian Silverman0aa48a92014-01-25 17:10:17 -08005#include <string.h>
Daniel Petti23dcf6c2013-12-19 08:56:41 -08006
Brian Silverman04fac622014-01-26 18:32:15 -08007#include "aos/common/logging/logging.h"
Daniel Petti23dcf6c2013-12-19 08:56:41 -08008
9namespace bbb {
10
Brian Silverman0aa48a92014-01-25 17:10:17 -080011GpioPin::GpioPin(int bank, int pin, bool input, bool initial_value)
12 : bank_(bank), pin_(pin), kernel_pin_(bank * 32 + pin) {
Brian Silverman1662a0e2013-12-19 17:50:01 -080013 // Export the pin.
Austin Schuh30c8db82014-03-02 11:46:16 -080014 FILE *export_handle = fopen("/sys/class/gpio/export", "w");
Brian Silverman0aa48a92014-01-25 17:10:17 -080015 if (export_handle == NULL) {
Brian Silvermand7844882014-05-10 23:35:39 -070016 PLOG(WARNING, "Could not open file to export pin (%d,%d)", bank_, pin_);
Brian Silverman0aa48a92014-01-25 17:10:17 -080017 } else {
18 if (fprintf(export_handle, "%d", kernel_pin_) < 0) {
Brian Silvermand7844882014-05-10 23:35:39 -070019 PLOG(WARNING, "Could not write to file %p to export pin (%d,%d)",
20 export_handle, bank_, pin_);
Brian Silverman0aa48a92014-01-25 17:10:17 -080021 }
22 if (fclose(export_handle) == -1) {
Brian Silvermand7844882014-05-10 23:35:39 -070023 PLOG(WARNING, "fclose(%p) failed", export_handle);
Brian Silverman0aa48a92014-01-25 17:10:17 -080024 }
Daniel Petti23dcf6c2013-12-19 08:56:41 -080025 }
26
Brian Silverman0aa48a92014-01-25 17:10:17 -080027 char direction_path[64];
28 snprintf(direction_path, sizeof(direction_path),
29 "/sys/class/gpio/gpio%d/direction", kernel_pin_);
30
31 FILE *direction_handle = fopen(direction_path, "w");
32 if (direction_handle == NULL) {
Brian Silvermand7844882014-05-10 23:35:39 -070033 PLOG(FATAL, "fopen(%s, \"w\") failed", direction_path);
Brian Silverman0aa48a92014-01-25 17:10:17 -080034 }
35
36 if (fputs(input ? "in" : (initial_value ? "high" : "low"),
37 direction_handle) < 0) {
Brian Silvermand7844882014-05-10 23:35:39 -070038 PLOG(FATAL, "setting direction for pin (%d,%d) failed", bank_, pin_);
Brian Silverman0aa48a92014-01-25 17:10:17 -080039 }
40 if (fclose(direction_handle) == -1) {
Brian Silvermand7844882014-05-10 23:35:39 -070041 PLOG(WARNING, "fclose(%p) failed", direction_handle);
Brian Silverman0aa48a92014-01-25 17:10:17 -080042 }
Brian Silvermane364e382014-02-08 21:51:33 -080043
44 char value_path[64];
45 snprintf(value_path, sizeof(value_path),
46 "/sys/class/gpio/gpio%d/value", kernel_pin_);
47 value_handle_ = fopen(value_path, "w+");
48 if (value_handle_ == NULL) {
Brian Silvermand7844882014-05-10 23:35:39 -070049 PLOG(FATAL, "fopen(%s, \"w+\") failed", value_path);
Brian Silvermane364e382014-02-08 21:51:33 -080050 }
Brian Silverman1662a0e2013-12-19 17:50:01 -080051}
Daniel Petti23dcf6c2013-12-19 08:56:41 -080052
Brian Silverman0aa48a92014-01-25 17:10:17 -080053GpioPin::~GpioPin() {
54 // Unexport the pin.
55 FILE *unexport_handle = fopen("/sys/class/gpio/unexport", "a");
56 if (unexport_handle == NULL) {
Brian Silvermand7844882014-05-10 23:35:39 -070057 PLOG(WARNING, "Could not open file to unexport pin (%d,%d)", bank_, pin_);
Brian Silverman0aa48a92014-01-25 17:10:17 -080058 } else {
59 if (fprintf(unexport_handle, "%d", kernel_pin_) < 0) {
Brian Silvermand7844882014-05-10 23:35:39 -070060 PLOG(WARNING, "Could not write to file %p to unexport pin (%d,%d)",
61 unexport_handle, bank_, pin_);
Brian Silverman0aa48a92014-01-25 17:10:17 -080062 }
63 if (fclose(unexport_handle) == -1) {
Brian Silvermand7844882014-05-10 23:35:39 -070064 PLOG(WARNING, "fclose(%p) failed", unexport_handle);
Brian Silverman0aa48a92014-01-25 17:10:17 -080065 }
Daniel Petti23dcf6c2013-12-19 08:56:41 -080066 }
Brian Silverman1662a0e2013-12-19 17:50:01 -080067}
68
69} // namespace bbb