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