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> |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 5 | |
| 6 | #include "aos/common/logging/logging_impl.h" |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 7 | |
| 8 | namespace bbb { |
| 9 | |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 10 | Pin::~Pin() { |
| 11 | // Unexport the pin. |
| 12 | if ((handle_ = fopen("/sys/class/gpio/unexport", "ab")) == NULL) { |
Daniel Petti | d6ff3d5 | 2014-01-02 11:24:39 -0800 | [diff] [blame] | 13 | LOG(WARNING, "Could not open file to unexport pin.\n"); |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 14 | // There's nothing intelligent we can really do here. |
| 15 | return; |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 16 | } |
| 17 | |
Daniel Petti | d6ff3d5 | 2014-01-02 11:24:39 -0800 | [diff] [blame] | 18 | fprintf(handle_, "%d", kernel_pin_); |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 19 | fclose(handle_); |
| 20 | } |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 21 | |
Daniel Petti | d6ff3d5 | 2014-01-02 11:24:39 -0800 | [diff] [blame] | 22 | bool Pin::InitPin(int bank, int pin) { |
| 23 | kernel_pin_ = bank * 32 + pin; |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 24 | |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 25 | // Export the pin. |
| 26 | if ((handle_ = fopen("/sys/class/gpio/export", "ab")) == NULL) { |
Daniel Petti | d6ff3d5 | 2014-01-02 11:24:39 -0800 | [diff] [blame] | 27 | LOG(ERROR, "Could not open file for exporting pin.\n"); |
| 28 | return false; |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Daniel Petti | d6ff3d5 | 2014-01-02 11:24:39 -0800 | [diff] [blame] | 31 | fprintf(handle_, "%d", kernel_pin_); |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 32 | fclose(handle_); |
Daniel Petti | d6ff3d5 | 2014-01-02 11:24:39 -0800 | [diff] [blame] | 33 | return true; |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 34 | } |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 35 | |
Daniel Petti | d6ff3d5 | 2014-01-02 11:24:39 -0800 | [diff] [blame] | 36 | bool Pin::DoPinDirSet(int direction) { |
| 37 | char type_path[64]; |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 38 | snprintf(type_path, sizeof(type_path), "/sys/class/gpio/gpio%d/direction", |
| 39 | kernel_pin_); |
| 40 | |
| 41 | if ((handle_ = fopen(type_path, "rb+")) == NULL) { |
Daniel Petti | d6ff3d5 | 2014-01-02 11:24:39 -0800 | [diff] [blame] | 42 | LOG(ERROR, "Unable open file for pin direction setting.\n"); |
| 43 | return false; |
Daniel Petti | 23dcf6c | 2013-12-19 08:56:41 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 46 | switch (direction) { |
| 47 | case 1: |
Daniel Petti | d6ff3d5 | 2014-01-02 11:24:39 -0800 | [diff] [blame] | 48 | // Input. |
| 49 | fprintf(handle_, "in"); |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 50 | break; |
| 51 | case 2: |
Daniel Petti | d6ff3d5 | 2014-01-02 11:24:39 -0800 | [diff] [blame] | 52 | // Output. |
| 53 | // If it's an output, we should specify an initial direction. |
| 54 | fprintf(handle_, "low"); |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 55 | break; |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 56 | default: |
| 57 | LOG(ERROR, "Invalid direction identifier %d.\n", direction); |
Daniel Petti | d6ff3d5 | 2014-01-02 11:24:39 -0800 | [diff] [blame] | 58 | fclose(handle_); |
| 59 | return false; |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 60 | } |
Daniel Petti | d6ff3d5 | 2014-01-02 11:24:39 -0800 | [diff] [blame] | 61 | |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 62 | fclose(handle_); |
Daniel Petti | d6ff3d5 | 2014-01-02 11:24:39 -0800 | [diff] [blame] | 63 | return true; |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } // namespace bbb |