added some error handling
diff --git a/includes/gpio.c b/includes/gpio.c
index a7057a5..4b22f89 100644
--- a/includes/gpio.c
+++ b/includes/gpio.c
@@ -11,19 +11,21 @@
#include "gpio.h"
-void gpio_writec(GPIO* gpio, char value)
+int gpio_writec(GPIO* gpio, char value)
{
+ int rc = GPIO_OK;
char buf[1];
buf[0] = value;
if (write(gpio->fd, buf, 1) != 1)
{
- //TODO: error handling
- //printf("Write error\n");
- }
+ rc = GPIO_WRITE_ERROR;
+ }
+ return rc;
}
-void gpio_write(GPIO* gpio, uint8_t value)
+int gpio_write(GPIO* gpio, uint8_t value)
{
+ int rc = GPIO_OK;
char buf[1];
buf[0] = '0';
if (value==1)
@@ -32,37 +34,48 @@
}
if (write(gpio->fd, buf, 1) != 1)
{
- //TODO: error handling
- //printf("write error\n");
- }
+ rc = GPIO_WRITE_ERROR;
+ }
+ return rc;
}
-uint8_t gpio_read(GPIO* gpio)
+int gpio_read(GPIO* gpio, uint8_t* value)
{
char buf[1];
-
+ int r = GPIO_OK;
if (read(gpio->fd,&buf,1) != 1)
{
- //g_print("read error\n");
- //TODO: error handling
+ r = GPIO_READ_ERROR;
+ } else {
+ if (buf[0]=='1') {
+ *value = 1;
+ } else {
+ *value = 0;
+ }
}
- if (buf[0]=='1') {
- return 1;
- }
- return 0;
+ return r;
}
-void gpio_clock_cycle(GPIO* gpio, int num_clks) {
+int gpio_clock_cycle(GPIO* gpio, int num_clks) {
int i=0;
+ int r=GPIO_OK;
for (i=0;i<num_clks;i++) {
- gpio_writec(gpio,'0');
- gpio_writec(gpio,'1');
+ if (gpio_writec(gpio,'0') == -1) {
+ r = GPIO_WRITE_ERROR;
+ break;
+ }
+ if (gpio_writec(gpio,'1') == -1) {
+ r = GPIO_WRITE_ERROR;
+ break;
+ }
}
+ return r;
}
// Gets the gpio device path from gpio manager object
-void gpio_init(GDBusConnection *connection, GPIO* gpio)
+int gpio_init(GDBusConnection *connection, GPIO* gpio)
{
+ int rc = GPIO_OK;
GDBusProxy *proxy;
GError *error;
GVariant *result;
@@ -78,7 +91,9 @@
"org.openbmc.managers.System", /* interface */
NULL, /* GCancellable */
&error);
- g_assert_no_error (error);
+ if (error != NULL) {
+ return GPIO_LOOKUP_ERROR;
+ }
result = g_dbus_proxy_call_sync (proxy,
"gpioInit",
@@ -88,7 +103,9 @@
NULL,
&error);
- g_assert_no_error (error);
+ if (error != NULL) {
+ return GPIO_LOOKUP_ERROR;
+ }
g_assert (result != NULL);
g_variant_get (result, "(&si&s)", &gpio->dev,&gpio->num,&gpio->direction);
g_print("GPIO Lookup: %s = %d,%s\n",gpio->name,gpio->num,gpio->direction);
@@ -97,17 +114,31 @@
char dev[254];
char data[4];
sprintf(dev,"%s/export",gpio->dev);
- int fd = open(dev, O_WRONLY);
- sprintf(data,"%d",gpio->num);
- write(fd,data,strlen(data));
- close(fd);
+ do {
+ int fd = open(dev, O_WRONLY);
+ if (fd == GPIO_ERROR) {
+ rc = GPIO_OPEN_ERROR;
+ break;
+ }
+ sprintf(data,"%d",gpio->num);
+ rc = write(fd,data,strlen(data));
+ close(fd);
+ if (rc == GPIO_ERROR) {
+ rc = GPIO_WRITE_ERROR;
+ break;
+ }
- sprintf(dev,"%s/gpio%d/direction",gpio->dev,gpio->num);
- fd = open(dev,O_WRONLY);
- write(fd,gpio->direction,strlen(gpio->direction));
- close(fd);
+ sprintf(dev,"%s/gpio%d/direction",gpio->dev,gpio->num);
+ fd = open(dev,O_WRONLY);
+ if (fd == GPIO_ERROR) {
+ rc = GPIO_WRITE_ERROR;
+ break;
+ }
+ rc = write(fd,gpio->direction,strlen(gpio->direction));
+ close(fd);
+ } while(0);
-
+ return rc;
}
char* get_gpio_dev(GPIO* gpio)
{
@@ -120,6 +151,7 @@
{
// open gpio for writing or reading
char buf[254];
+ int rc = 0;
if (strcmp(gpio->direction,"in")==0)
{
sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num);
@@ -131,10 +163,6 @@
gpio->fd = open(buf, O_WRONLY);
}
- if (gpio->fd == -1)
- {
- printf("error opening: %s\n",buf);
- }
return gpio->fd;
}
diff --git a/includes/gpio.h b/includes/gpio.h
index 68d01bd..54d912d 100644
--- a/includes/gpio.h
+++ b/includes/gpio.h
@@ -14,12 +14,20 @@
//gpio functions
-void gpio_init(GDBusConnection*, GPIO*);
+#define GPIO_OK 0
+#define GPIO_ERROR -1
+#define GPIO_OPEN_ERROR -2
+#define GPIO_INIT_ERROR -3
+#define GPIO_READ_ERROR -4
+#define GPIO_WRITE_ERROR -5
+#define GPIO_LOOKUP_ERROR -6
+
+int gpio_init(GDBusConnection*, GPIO*);
void gpio_close(GPIO*);
int gpio_open(GPIO*);
-void gpio_write(GPIO*, uint8_t);
-void gpio_writec(GPIO*, char);
-void gpio_clock_cycle(GPIO*, int);
-uint8_t gpio_read(GPIO*);
+int gpio_write(GPIO*, uint8_t);
+int gpio_writec(GPIO*, char);
+int gpio_clock_cycle(GPIO*, int);
+int gpio_read(GPIO*,uint8_t*);
#endif
diff --git a/includes/openbmc.h b/includes/openbmc.h
index b8e8d0f..5ae0ede 100644
--- a/includes/openbmc.h
+++ b/includes/openbmc.h
@@ -13,17 +13,23 @@
#define NEW_VARIANT_B(v) g_variant_new_variant(g_variant_new_byte(v))
#define VARIANT_COMPARE(x,y) g_variant_compare(GET_VARIANT(x),GET_VARIANT(y))
-
-static inline void writel(uint32_t val,void* addr)
+#ifdef __arm__
+static inline void write_reg(uint32_t val,void* addr)
{
asm volatile("" : : : "memory");
*(volatile uint32_t *)addr = val;
}
static inline devmem(uint32_t val, uint32_t reg)
{
- writel(val,reg);
+ void* r = (void*)reg;
+ write_reg(val,r);
}
+#else
+static inline devmem(uint32_t val, uint32_t reg)
+{
+}
+#endif
typedef struct {
gint argc;