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