From 334c0f768757075c94f4080f3275fc6cd006e6f7 Mon Sep 17 00:00:00 2001 From: Jeremy Tan Date: Wed, 25 Sep 2013 20:50:33 +0800 Subject: [PATCH] partial fix to gpio stuff (limit what can be exported) --- .gitignore | 2 + .../GPIO pin correspondence unrestricted.csv | 50 +++++++++ notes/pin maps/GPIO pin correspondence.csv | 65 ++++++++++++ notes/pin maps/GPIO pin correspondence.xls | Bin 0 -> 17920 bytes notes/pin maps/gpioindex_lut.py | 17 +++ notes/pin maps/gpionums.csv | 50 +++++++++ notes/pin maps/parseit.py | 48 +++++++++ notes/pin maps/readme.txt | 3 + server/bbb_pin.c | 99 ++++++++++++++---- server/bbb_pin_defines.h | 10 +- server/pin_test.c | 4 +- 11 files changed, 319 insertions(+), 29 deletions(-) create mode 100644 notes/pin maps/GPIO pin correspondence unrestricted.csv create mode 100644 notes/pin maps/GPIO pin correspondence.csv create mode 100644 notes/pin maps/GPIO pin correspondence.xls create mode 100644 notes/pin maps/gpioindex_lut.py create mode 100644 notes/pin maps/gpionums.csv create mode 100644 notes/pin maps/parseit.py create mode 100644 notes/pin maps/readme.txt diff --git a/.gitignore b/.gitignore index 74ede4e..729e81b 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,7 @@ ehthumbs.db Thumbs.db +__pycache__ + server/win32 **/nbproject/private/ \ No newline at end of file diff --git a/notes/pin maps/GPIO pin correspondence unrestricted.csv b/notes/pin maps/GPIO pin correspondence unrestricted.csv new file mode 100644 index 0000000..c076e18 --- /dev/null +++ b/notes/pin maps/GPIO pin correspondence unrestricted.csv @@ -0,0 +1,50 @@ +P8_07,66 +P8_08,67 +P8_09,69 +P8_10,68 +P8_11,45 +P8_12,44 +P8_13,23 +P8_14,26 +P8_15,47 +P8_16,46 +P8_17,27 +P8_18,65 +P8_19,22 +P8_26,61 +P8_27,86 +P8_28,88 +P8_29,87 +P8_30,89 +P8_31,10 +P8_32,11 +P8_33,9 +P8_34,81 +P8_35,8 +P8_36,80 +P8_37,78 +P8_38,79 +P8_39,76 +P8_40,77 +P8_41,74 +P8_42,75 +P8_43,72 +P8_44,73 +P8_45,70 +P8_46,71 +P9_11,30 +P9_12,60 +P9_13,31 +P9_14,50 +P9_15,48 +P9_16,51 +P9_17,5 +P9_18,4 +P9_21,3 +P9_22,2 +P9_23,49 +P9_24,15 +P9_25,117 +P9_26,14 +P9_27,115 +P9_30,112 diff --git a/notes/pin maps/GPIO pin correspondence.csv b/notes/pin maps/GPIO pin correspondence.csv new file mode 100644 index 0000000..ceb41a2 --- /dev/null +++ b/notes/pin maps/GPIO pin correspondence.csv @@ -0,0 +1,65 @@ +P8_03,38 +P8_04,39 +P8_05,34 +P8_06,35 +P8_07,66 +P8_08,67 +P8_09,69 +P8_10,68 +P8_11,45 +P8_12,44 +P8_13,23 +P8_14,26 +P8_15,47 +P8_16,46 +P8_17,27 +P8_18,65 +P8_19,22 +P8_20,63 +P8_21,62 +P8_22,37 +P8_23,36 +P8_24,33 +P8_25,1 +P8_26,61 +P8_27,86 +P8_28,88 +P8_29,87 +P8_30,89 +P8_31,10 +P8_32,11 +P8_33,9 +P8_34,81 +P8_35,8 +P8_36,80 +P8_37,78 +P8_38,79 +P8_39,76 +P8_40,77 +P8_41,74 +P8_42,75 +P8_43,72 +P8_44,73 +P8_45,70 +P8_46,71 +P9_11,30 +P9_12,60 +P9_13,31 +P9_14,50 +P9_15,48 +P9_16,51 +P9_17,5 +P9_18,4 +P9_19,13 +P9_20,12 +P9_21,3 +P9_22,2 +P9_23,49 +P9_24,15 +P9_25,117 +P9_26,14 +P9_27,115 +P9_28,113 +P9_29,111 +P9_30,112 +P9_31,110 diff --git a/notes/pin maps/GPIO pin correspondence.xls b/notes/pin maps/GPIO pin correspondence.xls new file mode 100644 index 0000000000000000000000000000000000000000..3e7d55c549c3eaa383991d0c292e419c6fed7ccb GIT binary patch literal 17920 zcmeHPYiu0V6+XLO+Z!A*j-AbhJe-N0ho7<@61j@ zO6iNvICsw6bG~!#oiq2`*UaXZFRi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^5QE`?B3M+*w?ZC?|=I{T>t+ZiEsb7{vSi)t{>O^cpHSz z6-3|g&4F_5UPj`&|8q!u|MW0Ybw7an15YB=?gwC7Ko73von0+ziwwRsewE4ZEx9<~ibVOlNTlKMpYZ^H3{})07v735TFUsb;YB=| z>p#}x>yc{B*^b;e&-esg&-ziYxx)GU>n{!c0pDuyc~_bDp!5YfCug-D=c?@MrubhV zmdj^7u2=CQ8z7EGIf)-x7W4;Uy!R9E!u?t&{zR;dpN|!<`9ra8l+PGq{9JGO+#Ayk dGLS}m|73pX$)Bn53H)RA8U6tGzoDl5zXN GPIO_NUM_PINS) + if (pin < 0 || pin >= GPIO_INDEX_SIZE || g_gpio_index[pin] == 128) { - Abort("Invalid pin number %d", pin); + Abort("Not a useable pin (number %d)", pin); } - - // Export the pin sprintf(g_buffer, "%s/export", GPIO_DEVICE_PATH); FILE * export = fopen(g_buffer, "w"); @@ -78,10 +110,11 @@ void GPIO_Export(int pin) fprintf(export, "%d", pin); fclose(export); + GPIO_Pin *gpio = &g_gpio[g_gpio_index[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 +122,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)); } @@ -105,14 +138,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_index[pin] == 128) { - Abort("Invalid pin number %d", pin); + Abort("Not a useable pin (number %d)", pin); } + GPIO_Pin *gpio = &g_gpio[g_gpio_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 +250,6 @@ void PWM_Unexport(int pin) fprintf(export, "%d", pin); fclose(export); - - } /** @@ -256,13 +288,19 @@ 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_index[pin] == 128) + { + Abort("Not a useable pin (number %d)", pin); + } + + GPIO_Pin *gpio = &g_gpio[g_gpio_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 +313,17 @@ 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_index[pin] == 128) + { + Log(LOGERR, "Not a useable pin (number %d)", pin); + return false; + } + + GPIO_Pin *gpio = &g_gpio[g_gpio_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 +339,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 +362,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)); diff --git a/server/bbb_pin_defines.h b/server/bbb_pin_defines.h index dba2d5c..661f969 100644 --- a/server/bbb_pin_defines.h +++ b/server/bbb_pin_defines.h @@ -9,9 +9,9 @@ /** GPIO0 defines **/ #define GPIO0_1 1 -#define GPIO0_2 2 -//#define GPIO0_3 3 // Used for PWM -//#define GPIO0_4 4 // Used for PWM +#define GPIO0_2 2 // Used for PWM +#define GPIO0_3 3 // Used for PWM +#define GPIO0_4 4 #define GPIO0_5 5 #define GPIO0_6 6 #define GPIO0_7 7 @@ -111,8 +111,8 @@ #define GPIO2_31 95 #define GPIO2_32 96 -/** Number of GPIO pins **/ -#define GPIO_NUM_PINS 97 +/** Number of useable GPIO pins **/ +#define GPIO_NUM_PINS 50 /** Export path **/ #define GPIO_DEVICE_PATH "/sys/class/gpio" diff --git a/server/pin_test.c b/server/pin_test.c index 3ccf458..ca857ec 100644 --- a/server/pin_test.c +++ b/server/pin_test.c @@ -12,12 +12,12 @@ */ void Pin_Init() { -/* for (int i = 0; i < GPIO_NUM_PINS; ++i) + for (int i = 0; i < 128; ++i) GPIO_Export(i); for (int i = 0; i < ADC_NUM_PINS; ++i) ADC_Export(); -*/ + for (int i = 0; i < PWM_NUM_PINS; ++i) PWM_Export(i); } -- 2.20.1