blob: a5a891a11d1a4aab8e24e1cc5728efec6c7e54fd [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 <errno.h>
6#include <string.h>
Daniel Petti23dcf6c2013-12-19 08:56:41 -08007
Brian Silverman04fac622014-01-26 18:32:15 -08008#include "aos/common/logging/logging.h"
Daniel Petti23dcf6c2013-12-19 08:56:41 -08009
10namespace bbb {
11
Brian Silverman0aa48a92014-01-25 17:10:17 -080012GpioPin::GpioPin(int bank, int pin, bool input, bool initial_value)
13 : bank_(bank), pin_(pin), kernel_pin_(bank * 32 + pin) {
Brian Silverman1662a0e2013-12-19 17:50:01 -080014 // Export the pin.
Austin Schuh30c8db82014-03-02 11:46:16 -080015 FILE *export_handle = fopen("/sys/class/gpio/export", "w");
Brian Silverman0aa48a92014-01-25 17:10:17 -080016 if (export_handle == NULL) {
17 LOG(WARNING,
18 "Could not open file to export pin (%d,%d) because of %d: %s.\n",
19 bank_, pin_, errno, strerror(errno));
20 } else {
21 if (fprintf(export_handle, "%d", kernel_pin_) < 0) {
22 LOG(WARNING, "Could not write to file %p to export pin (%d,%d) because "
23 "of %d: %s.\n",
24 export_handle, bank_, pin_, errno, strerror(errno));
25 }
26 if (fclose(export_handle) == -1) {
27 LOG(WARNING, "fclose(%p) failed with %d: %s\n", export_handle, errno,
28 strerror(errno));
29 }
Daniel Petti23dcf6c2013-12-19 08:56:41 -080030 }
31
Brian Silverman0aa48a92014-01-25 17:10:17 -080032 char direction_path[64];
33 snprintf(direction_path, sizeof(direction_path),
34 "/sys/class/gpio/gpio%d/direction", kernel_pin_);
35
36 FILE *direction_handle = fopen(direction_path, "w");
37 if (direction_handle == NULL) {
Austin Schuh30c8db82014-03-02 11:46:16 -080038 LOG(FATAL, "fopen(%s, \"w\") failed with %d: %s\n",
Brian Silverman0aa48a92014-01-25 17:10:17 -080039 direction_path, errno, strerror(errno));
40 }
41
42 if (fputs(input ? "in" : (initial_value ? "high" : "low"),
43 direction_handle) < 0) {
44 LOG(FATAL, "setting direction for pin (%d,%d) failed with %d: %s\n",
45 bank_, pin_, errno, strerror(errno));
46 }
47 if (fclose(direction_handle) == -1) {
48 LOG(WARNING, "fclose(%p) failed with %d: %s\n", direction_handle, errno,
49 strerror(errno));
50 }
Brian Silvermane364e382014-02-08 21:51:33 -080051
52 char value_path[64];
53 snprintf(value_path, sizeof(value_path),
54 "/sys/class/gpio/gpio%d/value", kernel_pin_);
55 value_handle_ = fopen(value_path, "w+");
56 if (value_handle_ == NULL) {
Austin Schuh30c8db82014-03-02 11:46:16 -080057 LOG(FATAL, "fopen(%s, \"w+\") failed with %d: %s\n",
Brian Silvermane364e382014-02-08 21:51:33 -080058 value_path, errno, strerror(errno));
59 }
Brian Silverman1662a0e2013-12-19 17:50:01 -080060}
Daniel Petti23dcf6c2013-12-19 08:56:41 -080061
Brian Silverman0aa48a92014-01-25 17:10:17 -080062GpioPin::~GpioPin() {
63 // Unexport the pin.
64 FILE *unexport_handle = fopen("/sys/class/gpio/unexport", "a");
65 if (unexport_handle == NULL) {
66 LOG(WARNING,
67 "Could not open file to unexport pin (%d,%d) because of %d: %s.\n",
68 bank_, pin_, errno, strerror(errno));
69 } else {
70 if (fprintf(unexport_handle, "%d", kernel_pin_) < 0) {
71 LOG(WARNING, "Could not write to file %p to unexport pin (%d,%d) because "
72 "of %d: %s.\n",
73 unexport_handle, bank_, pin_, errno, strerror(errno));
74 }
75 if (fclose(unexport_handle) == -1) {
76 LOG(WARNING, "fclose(%p) failed with %d: %s\n", unexport_handle, errno,
77 strerror(errno));
78 }
Daniel Petti23dcf6c2013-12-19 08:56:41 -080079 }
Brian Silverman1662a0e2013-12-19 17:50:01 -080080}
81
82} // namespace bbb