From: Jeremy Tan Date: Wed, 25 Sep 2013 12:50:33 +0000 (+0800) Subject: partial fix to gpio stuff (limit what can be exported) X-Git-Url: https://git.ucc.asn.au/?p=matches%2FMCTX3420.git;a=commitdiff_plain;h=1faf2ef76c719ed79e13b359591082854e739fa3 partial fix to gpio stuff (limit what can be exported) --- 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 0000000..3e7d55c Binary files /dev/null and b/notes/pin maps/GPIO pin correspondence.xls differ diff --git a/notes/pin maps/gpioindex_lut.py b/notes/pin maps/gpioindex_lut.py new file mode 100644 index 0000000..14457d5 --- /dev/null +++ b/notes/pin maps/gpioindex_lut.py @@ -0,0 +1,17 @@ +import sys, re, os +from parseit import printlut + +def doit2(x): + with open(x) as f: + lut = {} + i = 0 + for line in f: + gpionum = int(line) + lut[gpionum] = i + i += 1 + + lutarr = [] + for i in range(128): + lutarr.append(lut.get(i, 128)) + return lutarr + diff --git a/notes/pin maps/gpionums.csv b/notes/pin maps/gpionums.csv new file mode 100644 index 0000000..d0a7697 --- /dev/null +++ b/notes/pin maps/gpionums.csv @@ -0,0 +1,50 @@ +2 +3 +4 +5 +8 +9 +10 +11 +14 +15 +22 +23 +26 +27 +30 +31 +44 +45 +46 +47 +48 +49 +50 +51 +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 +117 diff --git a/notes/pin maps/parseit.py b/notes/pin maps/parseit.py new file mode 100644 index 0000000..0aab5ba --- /dev/null +++ b/notes/pin maps/parseit.py @@ -0,0 +1,48 @@ +import sys, re, os +#lut size of 93 (46 pins/header; 2 headers; padding for 1-indexed) + +def doit(x): + '''generate the lut from the csv''' + lut = {} + reverselut = {} + + with open(x) as f: + for line in f: + m = re.search("P(\d)_(\d+),(\d+)", line) + header = int(m.group(1)) + pin = int(m.group(2)) + gpionum = int(m.group(3)) + + if header==8: + header = 0 + else: + header = 1 + + lut[header*46+pin] = gpionum + reverselut[gpionum] = header*46+pin + lutarr = [] + reverselutarr =[] + + for i in range(0, 93): + lutarr.append(lut.get(i, 0)) + + for i in range(0, 128): + reverselutarr.append(reverselut.get(i, 0)) + + return (lutarr, reverselutarr) + +def printlut(lut, name="g_gpio_lut"): + '''print the lut for C''' + rowsize = 14 + print("const char %s[%d] = {" % (name, len(lut))) + low = 0 + high = rowsize + for i in range(0, len(lut), rowsize): + print("\t", end="") + print(*("%3d" % g for g in lut[low:high]), sep=', ') + low = high + high += rowsize + print("}") + + + diff --git a/notes/pin maps/readme.txt b/notes/pin maps/readme.txt new file mode 100644 index 0000000..89e57b9 --- /dev/null +++ b/notes/pin maps/readme.txt @@ -0,0 +1,3 @@ +GPIO Pin correspondence.xls - all maps with descriptions +GPIO pin correspondence unrestricted.csv - all unused pins + hdmi pins +GPIO pin correspondence.csv - all gpio pins \ No newline at end of file diff --git a/server/bbb_pin.c b/server/bbb_pin.c index 4569578..d1e5a23 100644 --- a/server/bbb_pin.c +++ b/server/bbb_pin.c @@ -52,21 +52,53 @@ static PWM_Pin g_pwm[PWM_NUM_PINS] = {{0}}; static char g_buffer[BUFSIZ] = ""; +#define GPIO_LUT_SIZE 93 +#define GPIO_INDEX_SIZE 128 +/** + * A lookup table from header number to GPIO pin number. + * e.g P8_13 is g_gpio_lut[0*46+13] = g_gpio_lut[13] + * e.g P9_13 is g_gpio_lut[1*46+13] = g_gpio_lut[59] + * + * Where the returned value is 0, there is no GPIO pin + * at that location. + */ +const unsigned char g_gpio_lut[GPIO_LUT_SIZE] = { + 0, 0, 0, 0, 0, 0, 0, 66, 67, 69, 68, 45, 44, 23, + 26, 47, 46, 27, 65, 22, 0, 0, 0, 0, 0, 0, 61, 86, + 88, 87, 89, 10, 11, 9, 81, 8, 80, 78, 79, 76, 77, 74, + 75, 72, 73, 70, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 30, 60, 31, 50, 48, 51, 5, 4, 0, 0, 3, 2, 49, + 15, 117, 14, 115, 0, 0, 112, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; +/** + * Converts GPIO number to index into g_gpio, or 128 if no map. + */ +const unsigned char g_gpio_index[GPIO_INDEX_SIZE] = { + 128, 128, 0, 1, 2, 3, 128, 128, 4, 5, 6, 7, 128, 128, + 8, 9, 128, 128, 128, 128, 128, 128, 10, 11, 128, 128, 12, 13, + 128, 128, 14, 15, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 16, 17, 18, 19, 20, 21, 22, 23, 128, 128, 128, 128, + 128, 128, 128, 128, 24, 25, 128, 128, 128, 26, 27, 28, 29, 30, + 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 128, 128, + 128, 128, 43, 44, 45, 46, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 47, 128, 128, 48, 128, 49, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128 +}; /** * 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_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); }