Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 1 | #include "bbb/gpios.h" |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 5 | #include <errno.h> |
| 6 | #include <string.h> |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 7 | |
Brian Silverman | 04fac62 | 2014-01-26 18:32:15 -0800 | [diff] [blame^] | 8 | #include "aos/common/logging/logging.h" |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 9 | |
| 10 | namespace bbb { |
| 11 | |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 12 | GpioPin::GpioPin(int bank, int pin, bool input, bool initial_value) |
| 13 | : bank_(bank), pin_(pin), kernel_pin_(bank * 32 + pin) { |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 14 | // Export the pin. |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 15 | 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 Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 30 | } |
| 31 | |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 32 | 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 Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 51 | } |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 52 | |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 53 | GpioPin::~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 Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 70 | } |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | } // namespace bbb |