Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <unistd.h> |
| 7 | #include <argp.h> |
| 8 | #include <sys/stat.h> |
| 9 | #include <sys/mman.h> |
| 10 | |
| 11 | #include "gpio.h" |
| 12 | |
| 13 | |
| 14 | void gpio_writec(GPIO* gpio, char value) |
| 15 | { |
| 16 | char buf[1]; |
| 17 | buf[0] = value; |
| 18 | if (write(gpio->fd, buf, 1) != 1) |
| 19 | { |
| 20 | //TODO: error handling |
| 21 | printf("Write error\n"); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | void gpio_write(GPIO* gpio, uint8_t value) |
| 26 | { |
| 27 | char buf[1]; |
| 28 | buf[0] = '0'; |
| 29 | if (value==1) |
| 30 | { |
| 31 | buf[0]='1'; |
| 32 | } |
| 33 | if (write(gpio->fd, buf, 1) != 1) |
| 34 | { |
| 35 | //TODO: error handling |
| 36 | printf("write error\n"); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | uint8_t gpio_read(GPIO* gpio) |
| 41 | { |
| 42 | char buf[1]; |
| 43 | if (read(gpio->fd,&buf,1) != 1) |
| 44 | { |
| 45 | //TODO: error hjandling |
| 46 | printf("read error\n"); |
| 47 | } |
| 48 | if (buf[0]=='1') { |
| 49 | return 1; |
| 50 | } |
| 51 | return 0; |
| 52 | |
| 53 | } |
| 54 | void gpio_clock_cycle(GPIO* gpio, int num_clks) { |
| 55 | int i=0; |
| 56 | for (i=0;i<num_clks;i++) { |
| 57 | gpio_writec(gpio,'0'); |
| 58 | gpio_writec(gpio,'1'); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Gets the gpio device path from gpio manager object |
| 63 | void gpio_init(GDBusConnection *connection, GPIO* gpio) |
| 64 | { |
| 65 | GDBusProxy *proxy; |
| 66 | GError *error; |
| 67 | GVariant *result; |
| 68 | |
| 69 | error = NULL; |
| 70 | g_assert_no_error (error); |
| 71 | error = NULL; |
| 72 | proxy = g_dbus_proxy_new_sync (connection, |
| 73 | G_DBUS_PROXY_FLAGS_NONE, |
| 74 | NULL, /* GDBusInterfaceInfo */ |
| 75 | "org.openbmc.managers.Gpios", /* name */ |
| 76 | "/org/openbmc/managers/Gpios", /* object path */ |
| 77 | "org.openbmc.managers.Gpios", /* interface */ |
| 78 | NULL, /* GCancellable */ |
| 79 | &error); |
| 80 | g_assert_no_error (error); |
| 81 | |
| 82 | result = g_dbus_proxy_call_sync (proxy, |
| 83 | "init", |
| 84 | g_variant_new ("(s)", gpio->name), |
| 85 | G_DBUS_CALL_FLAGS_NONE, |
| 86 | -1, |
| 87 | NULL, |
| 88 | &error); |
| 89 | |
| 90 | g_assert_no_error (error); |
| 91 | g_assert (result != NULL); |
| 92 | g_variant_get (result, "(&si&s)", &gpio->dev,&gpio->num,&gpio->direction); |
| 93 | g_print("GPIO Lookup: %s = %d,%s\n",gpio->name,gpio->num,gpio->direction); |
| 94 | |
| 95 | //export and set direction |
| 96 | char dev[254]; |
| 97 | char data[4]; |
| 98 | sprintf(dev,"%s/export",gpio->dev); |
| 99 | int fd = open(dev, O_WRONLY); |
| 100 | sprintf(data,"%d",gpio->num); |
| 101 | write(fd,data,strlen(data)); |
| 102 | close(fd); |
| 103 | |
| 104 | sprintf(dev,"%s/gpio%d/direction",gpio->dev,gpio->num); |
| 105 | fd = open(dev,O_WRONLY); |
| 106 | write(fd,gpio->direction,strlen(gpio->direction)); |
| 107 | close(fd); |
| 108 | |
| 109 | |
| 110 | } |
| 111 | int gpio_open(GPIO* gpio) |
| 112 | { |
| 113 | // open gpio for writing or reading |
| 114 | char buf[254]; |
| 115 | if (strcmp(gpio->direction,"in")==0) |
| 116 | { |
| 117 | sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
| 118 | gpio->fd = open(buf, O_RDONLY); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
| 123 | gpio->fd = open(buf, O_WRONLY); |
| 124 | |
| 125 | } |
| 126 | if (gpio->fd == -1) |
| 127 | { |
| 128 | printf("error opening: %s\n",buf); |
| 129 | } |
| 130 | return gpio->fd; |
| 131 | } |
| 132 | |
| 133 | void gpio_close(GPIO* gpio) |
| 134 | { |
| 135 | close(gpio->fd); |
| 136 | } |