X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Fbbb_pin.c;h=bd31365cdecb61aa17768554531d680bfb6b4f19;hb=15a32ab1123375e1a52c319fca71ab8d02c58261;hp=f351f53c08c360436eae812433ea3154e1bb8302;hpb=5725205e01ba058ed114fea9f8ea81de76916d04;p=matches%2FMCTX3420.git diff --git a/server/bbb_pin.c b/server/bbb_pin.c index f351f53..bd31365 100644 --- a/server/bbb_pin.c +++ b/server/bbb_pin.c @@ -1,6 +1,7 @@ /** * @file bbb_pin.c - * @purpose Implementation of BBB pin control functions and structures + * @brief Implementation of BBB pin control functions and structures + * On non-beaglebone (actually non-arm) platforms, this code is disabled. * THIS CODE IS NOT THREADSAFE */ @@ -10,7 +11,6 @@ #include #include #include -#include #include "options.h" /** @@ -56,6 +56,32 @@ static PWM_Pin g_pwm[PWM_NUM_PINS] = {{0}}; static char g_buffer[BUFSIZ] = {0}; +/** + * Maps a GPIO number to an index into g_gpio (only for use in bbb_pin.c) + * If there is no index for that GPIO number, 128 is returned. + */ +const unsigned char g_pin_gpio_to_index[GPIO_MAX_NUMBER+1] = { + 128, 128, 128, 128, 0, 1, 128, 128, 2, 3, 4, 5, 128, 128, + 6, 7, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 8, 9, + 128, 128, 10, 11, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 12, 13, 14, 15, 16, 17, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 18, 19, 128, 128, 128, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 128, 128, + 128, 128, 37, 38, 39, 40, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 41, 128, 128, 42 +}; + +/** + * Maps an index in g_gpio to the corresponding GPIO number. + */ +const unsigned char g_pin_index_to_gpio[GPIO_NUM_PINS] = { + 4, 5, 8, 9, 10, 11, 14, 15, 26, 27, 30, 31, 44, 45, + 46, 47, 48, 49, 60, 61, 65, 66, 67, 68, 69, 70, 71, 72, + 73, 74, 75, 76, 77, 78, 79, 80, 81, 86, 87, 88, 89, 112, + 115 +}; + /** * Export a GPIO pin and open the file descriptors * @param pin The GPIO number to be exported @@ -109,6 +135,7 @@ bool GPIO_Export(int pin) /** * Unexport a GPIO pin and close its' file descriptors + * @param pin The GPIO number to be unexported */ void GPIO_Unexport(int pin) { @@ -272,7 +299,7 @@ bool ADC_Export(int pin) return true; } - sprintf(g_buffer, "%s/in_voltage%d_raw", g_options.adc_device_path, pin); + sprintf(g_buffer, "%s/in_voltage%d_raw", ADC_DEVICE_PATH, pin); g_adc[pin].fd_value = open(g_buffer, O_RDONLY); if (g_adc[pin].fd_value <0) { @@ -349,13 +376,21 @@ bool GPIO_Read(int pin, bool *result) } GPIO_Pin *gpio = &g_gpio[g_pin_gpio_to_index[pin]]; + if (!gpio->initialised) + { + AbortBool("GPIO %d is not initialised.", pin); + } if (pwrite(gpio->fd_direction, "in", 2, 0) != 2) - AbortBool("Couldn't set GPIO %d direction - %s", pin, strerror(errno)); + { + AbortBool("Couldn't set GPIO %d direction - %s", pin, strerror(errno)); + } char c = '0'; if (pread(gpio->fd_value, &c, 1, 0) != 1) + { AbortBool("Couldn't read GPIO %d value - %s", pin, strerror(errno)); + } *result = (c == '1'); return true; @@ -386,36 +421,36 @@ bool PWM_Set(int pin, bool polarity, long period, long duty) // Have to stop PWM before changing it if (pwrite(pwm->fd_run, "0", 1, 0) != 1) { - AbortBool("Couldn't stop PWM%d - %s", pin, strerror(errno)); + AbortBool("Couldn't stop PWM %d - %s", pin, strerror(errno)); } char c = polarity ? '1' : '0'; if (pwrite(pwm->fd_polarity, &c, 1, 0) != 1) { - AbortBool("Couldn't set PWM%d polarity - %s", pin, strerror(errno)); + AbortBool("Couldn't set PWM %d polarity - %s", pin, strerror(errno)); } //This must be done first, otherwise period/duty settings can conflict if (fwrite("0", 1, 1, pwm->file_duty) < 1) { - AbortBool("Couldn't zero the duty for PWM%d - %s", pin, strerror(errno)); + AbortBool("Couldn't zero the duty for PWM %d - %s", pin, strerror(errno)); } if (fprintf(pwm->file_period, "%lu", period) < 0) { - AbortBool("Couldn't set period for PWM%d - %s", pin, strerror(errno)); + AbortBool("Couldn't set period for PWM %d - %s", pin, strerror(errno)); } if (fprintf(pwm->file_duty, "%lu", duty) < 0) { - AbortBool("Couldn't set duty cycle for PWM%d - %s", pin, strerror(errno)); + AbortBool("Couldn't set duty cycle for PWM %d - %s", pin, strerror(errno)); } if (pwrite(pwm->fd_run, "1", 1, 0) != 1) { - AbortBool("Couldn't start PWM%d - %s", pin, strerror(errno)); + AbortBool("Couldn't start PWM %d - %s", pin, strerror(errno)); } return true; @@ -424,18 +459,25 @@ bool PWM_Set(int pin, bool polarity, long period, long duty) /** * Deactivate a PWM pin * @param pin - The syfs pin number + * @return true on success, false otherwise */ -void PWM_Stop(int pin) +bool PWM_Stop(int pin) { if (pin < 0 || pin >= PWM_NUM_PINS) { - Abort("Invalid PWM pin number %d specified.", pin); + AbortBool("Invalid PWM pin number %d specified.", pin); + } + else if (!g_pwm[pin].initialised) + { + AbortBool("PWM %d is not initialised.", pin); } if (pwrite(g_pwm[pin].fd_run, "0", 1, 0) != 1) { - Abort("Couldn't stop PWM %d - %s", pin, strerror(errno)); + AbortBool("Couldn't stop PWM %d - %s", pin, strerror(errno)); } + + return true; } /** @@ -459,8 +501,17 @@ bool ADC_Read(int id, int *value) if (pread(g_adc[id].fd_value, adc_str, ADC_DIGITS-1, 0) == -1) { - AbortBool("ADC %d read failed: %s", id, strerror(errno)); + //AbortBool("ADC %d read failed: %s", id, strerror(errno)); + return false; } + *value = strtol(adc_str, NULL, 10); return true; -} \ No newline at end of file +} + +#ifndef _BBB +//For running on systems that are not the BBB +bool True_Stub(int arg, ...) { return true; } +bool ADC_Read_Stub(int *val, ...) { *val = 0; return true; } +bool GPIO_Read_Stub(bool *val, ...) { *val = false; return true; } +#endif