semi update lut stuff
[matches/MCTX3420.git] / server / bbb_pin.c
index 4569578..5ab14e9 100644 (file)
@@ -53,20 +53,17 @@ static PWM_Pin g_pwm[PWM_NUM_PINS] = {{0}};
 static char g_buffer[BUFSIZ] = "";
 
 
-
-
 /**
  * Export a GPIO pin and open the file descriptors
  */
 void GPIO_Export(int pin)
 {
-       if (pin < 0 || pin > GPIO_NUM_PINS)
+       if (pin < 0 || pin >= GPIO_INDEX_SIZE || g_gpio_to_index[pin] == 128)
        {
-               Abort("Invalid pin number %d", pin);
+               Abort("Not a useable pin number: %d", pin);
        }
 
-       
-
+       GPIO_Pin *gpio = &g_gpio[g_gpio_to_index[pin]];
        // Export the pin
        sprintf(g_buffer, "%s/export", GPIO_DEVICE_PATH);
        FILE * export = fopen(g_buffer, "w");
@@ -80,8 +77,8 @@ void GPIO_Export(int pin)
        
        // Setup direction file descriptor
        sprintf(g_buffer, "%s/gpio%d/direction", GPIO_DEVICE_PATH, pin);
-       g_gpio[pin].fd_direction = open(g_buffer, O_RDWR);
-       if (g_gpio[pin].fd_direction < 0)
+       gpio->fd_direction = open(g_buffer, O_RDWR);
+       if (gpio->fd_direction < 0)
        {
                Abort("Couldn't open %s for GPIO pin %d - %s", g_buffer, pin, strerror(errno));
        }
@@ -89,8 +86,8 @@ void GPIO_Export(int pin)
 
        // Setup value file descriptor
        sprintf(g_buffer, "%s/gpio%d/value", GPIO_DEVICE_PATH, pin);
-       g_gpio[pin].fd_value = open(g_buffer, O_RDWR);
-       if (g_gpio[pin].fd_value < 0)
+       gpio->fd_value = open(g_buffer, O_RDWR);
+       if (gpio->fd_value < 0)
        {
                Abort("Couldn't open %s for GPIO pin %d - %s", g_buffer, pin, strerror(errno));
        }
@@ -104,15 +101,15 @@ void GPIO_Export(int pin)
  */
 void GPIO_Unexport(int pin)
 {
-
-       if (pin < 0 || pin > GPIO_NUM_PINS)
+       if (pin < 0 || pin >= GPIO_INDEX_SIZE || g_gpio_to_index[pin] == 128)
        {
-               Abort("Invalid pin number %d", pin);
+               Abort("Not a useable pin number: %d", pin);
        }
 
+       GPIO_Pin *gpio = &g_gpio[g_gpio_to_index[pin]];
        // Close file descriptors
-       close(g_gpio[pin].fd_value);
-       close(g_gpio[pin].fd_direction);
+       close(gpio->fd_value);
+       close(gpio->fd_direction);
 
        // Unexport the pin
 
@@ -216,8 +213,6 @@ void PWM_Unexport(int pin)
        
        fprintf(export, "%d", pin);
        fclose(export);
-
-
 }
 
 /**
@@ -256,13 +251,20 @@ void ADC_Unexport()
  */
 void GPIO_Set(int pin, bool value)
 {
-       if (pwrite(g_gpio[pin].fd_direction, "out", 3, 0) != 3)
+       if (pin < 0 || pin >= GPIO_INDEX_SIZE || g_gpio_to_index[pin] == 128)
+       {
+               Abort("Not a useable pin number: %d", pin);
+       }
+
+       GPIO_Pin *gpio = &g_gpio[g_gpio_to_index[pin]];
+
+       if (pwrite(gpio->fd_direction, "out", 3, 0) != 3)
        {
                Abort("Couldn't set GPIO %d direction - %s", pin, strerror(errno));
        }
 
        char c = '0' + (value);
-       if (pwrite(g_gpio[pin].fd_value, &c, 1, 0) != 1)
+       if (pwrite(gpio->fd_value, &c, 1, 0) != 1)
        {
                Abort("Couldn't read GPIO %d value - %s", pin, strerror(errno));
        }
@@ -275,10 +277,18 @@ void GPIO_Set(int pin, bool value)
  */
 bool GPIO_Read(int pin)
 {
-       if (pwrite(g_gpio[pin].fd_direction, "in", 2, 0) != 2)
+       if (pin < 0 || pin >= GPIO_INDEX_SIZE || g_gpio_to_index[pin] == 128)
+       {
+               Log(LOGERR, "Not a useable pin number: %d", pin);
+               return false;
+       }
+
+       GPIO_Pin *gpio = &g_gpio[g_gpio_to_index[pin]];
+
+       if (pwrite(gpio->fd_direction, "in", 2, 0) != 2)
                Log(LOGERR,"Couldn't set GPIO %d direction - %s", pin, strerror(errno)); 
        char c = '0';
-       if (pread(g_gpio[pin].fd_value, &c, 1, 0) != 1)
+       if (pread(gpio->fd_value, &c, 1, 0) != 1)
                Log(LOGERR,"Couldn't read GPIO %d value - %s", pin, strerror(errno));
 
        return (c == '1');
@@ -294,6 +304,15 @@ bool GPIO_Read(int pin)
  */
 void PWM_Set(int pin, bool polarity, long period, long duty)
 {
+       Log(LOGDEBUG, "Pin %d, pol %d, period: %lu, duty: %lu", pin, polarity, period, duty);
+       
+       rewind(g_pwm[pin].file_duty);
+
+       if (fprintf(g_pwm[pin].file_duty, "0") == 0)
+       {
+               Abort("Couldn't zero the duty cycle for PWM %d - s", pin, strerror(errno));
+       }
+       
        // Have to stop PWM before changing it
        if (pwrite(g_pwm[pin].fd_run, "0", 1, 0) != 1)
        {
@@ -308,15 +327,16 @@ void PWM_Set(int pin, bool polarity, long period, long duty)
        
        rewind(g_pwm[pin].file_period); 
        rewind(g_pwm[pin].file_duty);
+       if (fprintf(g_pwm[pin].file_period, "%lu", period) == 0)
+       {
+               Abort("Couldn't set period for PWM %d - %s", pin, strerror(errno));
+       }
 
        if (fprintf(g_pwm[pin].file_duty, "%lu", duty) == 0)
        {
                Abort("Couldn't set duty cycle for PWM %d - %s", pin, strerror(errno));
        }
-       if (fprintf(g_pwm[pin].file_period, "%lu", period) == 0)
-       {
-               Abort("Couldn't set period for PWM %d - %s", pin, strerror(errno));
-       }
+
        if (pwrite(g_pwm[pin].fd_run, "1", 1, 0) != 1)
        {
                Abort("Couldn't start PWM %d - %s", pin, strerror(errno));

UCC git Repository :: git.ucc.asn.au