blob: 79d38bc0559777a42a92795f8a795c5a68a3d8cb [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
8#include "aos/common/logging/logging_impl.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.
Brian Silverman0aa48a92014-01-25 17:10:17 -080015 FILE *export_handle = fopen("/sys/class/gpio/export", "a");
16 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) {
38 LOG(FATAL, "fopen(%s, \"w+\") failed with %d: %s\n",
39 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 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) {
57 LOG(WARNING,
58 "Could not open file to unexport pin (%d,%d) because of %d: %s.\n",
59 bank_, pin_, errno, strerror(errno));
60 } else {
61 if (fprintf(unexport_handle, "%d", kernel_pin_) < 0) {
62 LOG(WARNING, "Could not write to file %p to unexport pin (%d,%d) because "
63 "of %d: %s.\n",
64 unexport_handle, bank_, pin_, errno, strerror(errno));
65 }
66 if (fclose(unexport_handle) == -1) {
67 LOG(WARNING, "fclose(%p) failed with %d: %s\n", unexport_handle, errno,
68 strerror(errno));
69 }
Daniel Petti23dcf6c2013-12-19 08:56:41 -080070 }
Brian Silverman1662a0e2013-12-19 17:50:01 -080071}
72
73} // namespace bbb