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 <string.h> |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 6 | |
Brian Silverman | 04fac62 | 2014-01-26 18:32:15 -0800 | [diff] [blame] | 7 | #include "aos/common/logging/logging.h" |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 8 | |
| 9 | namespace bbb { |
| 10 | |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 11 | GpioPin::GpioPin(int bank, int pin, bool input, bool initial_value) |
| 12 | : bank_(bank), pin_(pin), kernel_pin_(bank * 32 + pin) { |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 13 | // Export the pin. |
Austin Schuh | 30c8db8 | 2014-03-02 11:46:16 -0800 | [diff] [blame] | 14 | FILE *export_handle = fopen("/sys/class/gpio/export", "w"); |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 15 | if (export_handle == NULL) { |
Brian Silverman | d784488 | 2014-05-10 23:35:39 -0700 | [diff] [blame] | 16 | PLOG(WARNING, "Could not open file to export pin (%d,%d)", bank_, pin_); |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 17 | } else { |
| 18 | if (fprintf(export_handle, "%d", kernel_pin_) < 0) { |
Brian Silverman | d784488 | 2014-05-10 23:35:39 -0700 | [diff] [blame] | 19 | PLOG(WARNING, "Could not write to file %p to export pin (%d,%d)", |
| 20 | export_handle, bank_, pin_); |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 21 | } |
| 22 | if (fclose(export_handle) == -1) { |
Brian Silverman | d784488 | 2014-05-10 23:35:39 -0700 | [diff] [blame] | 23 | PLOG(WARNING, "fclose(%p) failed", export_handle); |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 24 | } |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 25 | } |
| 26 | |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 27 | 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 Silverman | d784488 | 2014-05-10 23:35:39 -0700 | [diff] [blame] | 33 | PLOG(FATAL, "fopen(%s, \"w\") failed", direction_path); |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | if (fputs(input ? "in" : (initial_value ? "high" : "low"), |
| 37 | direction_handle) < 0) { |
Brian Silverman | d784488 | 2014-05-10 23:35:39 -0700 | [diff] [blame] | 38 | PLOG(FATAL, "setting direction for pin (%d,%d) failed", bank_, pin_); |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 39 | } |
| 40 | if (fclose(direction_handle) == -1) { |
Brian Silverman | d784488 | 2014-05-10 23:35:39 -0700 | [diff] [blame] | 41 | PLOG(WARNING, "fclose(%p) failed", direction_handle); |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 42 | } |
Brian Silverman | e364e38 | 2014-02-08 21:51:33 -0800 | [diff] [blame] | 43 | |
| 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 Silverman | d784488 | 2014-05-10 23:35:39 -0700 | [diff] [blame] | 49 | PLOG(FATAL, "fopen(%s, \"w+\") failed", value_path); |
Brian Silverman | e364e38 | 2014-02-08 21:51:33 -0800 | [diff] [blame] | 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) { |
Brian Silverman | d784488 | 2014-05-10 23:35:39 -0700 | [diff] [blame] | 57 | PLOG(WARNING, "Could not open file to unexport pin (%d,%d)", bank_, pin_); |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 58 | } else { |
| 59 | if (fprintf(unexport_handle, "%d", kernel_pin_) < 0) { |
Brian Silverman | d784488 | 2014-05-10 23:35:39 -0700 | [diff] [blame] | 60 | PLOG(WARNING, "Could not write to file %p to unexport pin (%d,%d)", |
| 61 | unexport_handle, bank_, pin_); |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 62 | } |
| 63 | if (fclose(unexport_handle) == -1) { |
Brian Silverman | d784488 | 2014-05-10 23:35:39 -0700 | [diff] [blame] | 64 | PLOG(WARNING, "fclose(%p) failed", unexport_handle); |
Brian Silverman | 0aa48a9 | 2014-01-25 17:10:17 -0800 | [diff] [blame] | 65 | } |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 66 | } |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | } // namespace bbb |