1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #include "nvidiagpio.h" NvidiaGpio::NvidiaGpio() { } NvidiaGpio::~NvidiaGpio() { } int NvidiaGpio::openGpio(const char *port) { int fd = -1; const char *path = "/sys/class/gpio/export"; if((fd = open(path, O_WRONLY)) == -1) { perror("Failed to open gpio! "); return -1; } write(fd, port, sizeof(port)); close(fd); return 0; } int NvidiaGpio::closeGpio(const char *port) { int fd = -1; const char *path = "/sys/class/gpio/unexport"; if((fd = open(path, O_WRONLY)) == -1) { perror("Failed to close gpio! "); return -1; } write(fd, port, sizeof(port)); close(fd); return 0; } int NvidiaGpio::setGpioDirection(const char *port, const char *direction) { int fd = -1; char path[40] = {0}; sprintf(path, "/sys/class/gpio/gpio%s/direction", port); if((fd = open(path, O_WRONLY)) == -1) { perror("Failed to set GPIO dirention! "); return -1; } write(fd, direction, sizeof(direction)); close(fd); return 0; } int NvidiaGpio::writeGpioValue(const char *port, const char *value) { int fd = -1; char path[40] = {0}; sprintf(path, "/sys/class/gpio/gpio%s/value", port); if((fd = open(path, O_WRONLY)) == -1) { perror("Failed to set GPIO value! "); return -1; } write(fd, value, sizeof(value)); close(fd); return 0; } int NvidiaGpio::readGpioValue(const char *port) { int fd = -1; int value = 0; char path[40] = {0}; sprintf(path, "/sys/class/gpio/gpio%s/value", port); if((fd = open(path, O_RDONLY)) == -1) { perror("Failed to set GPIO value! "); return -1; } char tmp[2] = {0}; read(fd, tmp, 1); close(fd); value = atoi(tmp); return value; }
|
Gitalk 加载中 ...