From 6fb169f93cfef6479fd9572aa478270568957f4f Mon Sep 17 00:00:00 2001 From: Jeremy Tan Date: Wed, 25 Sep 2013 21:22:14 +0800 Subject: [PATCH] semi update lut stuff --- notes/pin maps/gpioindex_lut.py | 7 +++- notes/pin maps/parseit.py | 8 +++-- server/Makefile | 2 +- server/bbb_pin.c | 63 ++++++++------------------------- server/bbb_pin_defines.c | 45 +++++++++++++++++++++++ server/bbb_pin_defines.h | 8 ++++- server/pin_test.c | 6 ++-- 7 files changed, 82 insertions(+), 57 deletions(-) create mode 100644 server/bbb_pin_defines.c diff --git a/notes/pin maps/gpioindex_lut.py b/notes/pin maps/gpioindex_lut.py index 14457d5..a7b600b 100644 --- a/notes/pin maps/gpioindex_lut.py +++ b/notes/pin maps/gpioindex_lut.py @@ -4,14 +4,19 @@ from parseit import printlut def doit2(x): with open(x) as f: lut = {} + rlut = {} i = 0 for line in f: gpionum = int(line) lut[gpionum] = i + rlut[i] = gpionum i += 1 lutarr = [] + reverse = [] for i in range(128): lutarr.append(lut.get(i, 128)) - return lutarr + for i in range(len(rlut)): + reverse.append(rlut[i]) + return (lutarr, reverse) diff --git a/notes/pin maps/parseit.py b/notes/pin maps/parseit.py index 0aab5ba..3ebf5c0 100644 --- a/notes/pin maps/parseit.py +++ b/notes/pin maps/parseit.py @@ -34,14 +34,18 @@ def doit(x): def printlut(lut, name="g_gpio_lut"): '''print the lut for C''' rowsize = 14 - print("const char %s[%d] = {" % (name, len(lut))) + print("const unsigned 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=', ') + print(*("%3d" % g for g in lut[low:high]), sep=', ', end="") low = high high += rowsize + if low < len(lut): + print(",") + else: + print("") print("}") diff --git a/server/Makefile b/server/Makefile index f3ce6b4..929b40d 100644 --- a/server/Makefile +++ b/server/Makefile @@ -2,7 +2,7 @@ CXX = gcc FLAGS = -std=c99 -Wall -pedantic -g -I/usr/include/opencv -I/usr/include/opencv2/highgui -L/usr/lib LIB = -lfcgi -lssl -lcrypto -lpthread -lm -lopencv_highgui -lopencv_core -lopencv_ml -lopencv_imgproc -OBJ = log.o control.o data.o fastcgi.o main.o sensor.o actuator.o image.o bbb_pin.o pin_test.o +OBJ = log.o control.o data.o fastcgi.o main.o sensor.o actuator.o image.o bbb_pin.o bbb_pin_defines.o pin_test.o RM = rm -f BIN = server diff --git a/server/bbb_pin.c b/server/bbb_pin.c index d1e5a23..5ab14e9 100644 --- a/server/bbb_pin.c +++ b/server/bbb_pin.c @@ -52,53 +52,18 @@ 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_INDEX_SIZE || g_gpio_index[pin] == 128) + if (pin < 0 || pin >= GPIO_INDEX_SIZE || g_gpio_to_index[pin] == 128) { - Abort("Not a useable 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"); @@ -110,7 +75,6 @@ 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); gpio->fd_direction = open(g_buffer, O_RDWR); @@ -137,13 +101,12 @@ void GPIO_Export(int pin) */ void GPIO_Unexport(int pin) { - - if (pin < 0 || pin >= GPIO_INDEX_SIZE || g_gpio_index[pin] == 128) + if (pin < 0 || pin >= GPIO_INDEX_SIZE || g_gpio_to_index[pin] == 128) { - Abort("Not a useable pin (number %d)", pin); + Abort("Not a useable pin number: %d", pin); } - GPIO_Pin *gpio = &g_gpio[g_gpio_index[pin]]; + GPIO_Pin *gpio = &g_gpio[g_gpio_to_index[pin]]; // Close file descriptors close(gpio->fd_value); close(gpio->fd_direction); @@ -288,12 +251,13 @@ void ADC_Unexport() */ void GPIO_Set(int pin, bool value) { - if (pin < 0 || pin >= GPIO_INDEX_SIZE || g_gpio_index[pin] == 128) + if (pin < 0 || pin >= GPIO_INDEX_SIZE || g_gpio_to_index[pin] == 128) { - Abort("Not a useable pin (number %d)", pin); + Abort("Not a useable pin number: %d", pin); } - GPIO_Pin *gpio = &g_gpio[g_gpio_index[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)); @@ -313,13 +277,14 @@ void GPIO_Set(int pin, bool value) */ bool GPIO_Read(int pin) { - if (pin < 0 || pin >= GPIO_INDEX_SIZE || g_gpio_index[pin] == 128) + if (pin < 0 || pin >= GPIO_INDEX_SIZE || g_gpio_to_index[pin] == 128) { - Log(LOGERR, "Not a useable pin (number %d)", pin); + Log(LOGERR, "Not a useable pin number: %d", pin); return false; } - GPIO_Pin *gpio = &g_gpio[g_gpio_index[pin]]; + 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'; diff --git a/server/bbb_pin_defines.c b/server/bbb_pin_defines.c new file mode 100644 index 0000000..e89c6dc --- /dev/null +++ b/server/bbb_pin_defines.c @@ -0,0 +1,45 @@ +#include "bbb_pin_defines.h" + +/** + * 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_pin_to_gpio[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 for g_gpio, or 128 if no map. + */ +const unsigned char g_gpio_to_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 +}; + +/** + * Converts index number of g_gpio into the gpio number + */ +const unsigned char g_index_to_gpio[GPIO_NUM_PINS] = { + 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 +}; \ No newline at end of file diff --git a/server/bbb_pin_defines.h b/server/bbb_pin_defines.h index 661f969..844b406 100644 --- a/server/bbb_pin_defines.h +++ b/server/bbb_pin_defines.h @@ -114,6 +114,13 @@ /** Number of useable GPIO pins **/ #define GPIO_NUM_PINS 50 +/* Luts */ +#define GPIO_LUT_SIZE 93 +#define GPIO_INDEX_SIZE 128 +extern const unsigned char g_pin_to_gpio[GPIO_LUT_SIZE]; +extern const unsigned char g_gpio_to_index[GPIO_INDEX_SIZE]; +extern const unsigned char g_index_to_gpio[GPIO_NUM_PINS]; + /** Export path **/ #define GPIO_DEVICE_PATH "/sys/class/gpio" @@ -148,7 +155,6 @@ #define PWM_DEVICE_PATH "/sys/class/pwm" - #endif //_BBB_PIN_DEFINES_H diff --git a/server/pin_test.c b/server/pin_test.c index ca857ec..bcdf92e 100644 --- a/server/pin_test.c +++ b/server/pin_test.c @@ -12,8 +12,8 @@ */ void Pin_Init() { - for (int i = 0; i < 128; ++i) - GPIO_Export(i); + for (int i = 0; i < GPIO_NUM_PINS; ++i) + GPIO_Export(g_index_to_gpio[i]); for (int i = 0; i < ADC_NUM_PINS; ++i) ADC_Export(); @@ -28,7 +28,7 @@ void Pin_Init() void Pin_Close() { for (int i = 0; i < GPIO_NUM_PINS; ++i) - GPIO_Unexport(i); + GPIO_Unexport(g_index_to_gpio[i]); for (int i = 0; i < ADC_NUM_PINS; ++i) ADC_Unexport(i); -- 2.20.1