From 3bacb654b3471617c2e2ef7c338fef0b497de7fa Mon Sep 17 00:00:00 2001 From: Ian Sutton Date: Tue, 18 Dec 2018 07:13:11 -0600 Subject: [PATCH] new stuff --- Makefile | 11 ++++-- hex2bin.sh | 4 ++ pins.c | 40 +++++++++++++++++++ pins.h | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++ seg.c | 55 +++++++++++++++++++++++--- seg.h | 72 +++++++++++++++++++++++++++++++-- sr.sh | 7 ++++ 7 files changed, 291 insertions(+), 12 deletions(-) create mode 100755 hex2bin.sh create mode 100644 pins.c create mode 100644 pins.h create mode 100755 sr.sh diff --git a/Makefile b/Makefile index 866a278..8d4f562 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,9 @@ -SANITY=-Wno-unused-variable -Wno-unused-parameter +SANITY=-Wno-unused-variable -Wno-unused-parameter -Wno-language-extension-token -all: seg.c - cc -Wall -Werror -Wextra -pedantic -std=c99 ${SANITY} seg.c -o seg +all: seg.c pins.c + cc -Wall -Werror -Wextra -pedantic -std=gnu99 -pthread ${SANITY} seg.c -o seg + cc -Wall -Werror -Wextra -pedantic -std=gnu99 -pthread ${SANITY} pins.c -o pins + +debug: seg.c pins.c + cc -Wall -Werror -Wextra -pedantic -std=gnu99 -pthread -g ${SANITY} seg.c -o seg + cc -Wall -Werror -Wextra -pedantic -std=gnu99 -pthread -g ${SANITY} pins.c -o pins diff --git a/hex2bin.sh b/hex2bin.sh new file mode 100755 index 0000000..dea4f91 --- /dev/null +++ b/hex2bin.sh @@ -0,0 +1,4 @@ +#!/bin/ksh + +echo $1 | hexdump | cut -b9- | cut -d"|" -f1 | tr -d ' \t\n\r' +echo '' diff --git a/pins.c b/pins.c new file mode 100644 index 0000000..1c60712 --- /dev/null +++ b/pins.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "pins.h" + +#define UP(N) set_pin(N, 1) +#define DOWN(N) set_pin(N, 0) + +#define HI(N) set(N, 1) +#define LO(N) set(N, 0) + +int main(int argc, char *argv[]) { + pins_init(); + config_pins(); + pincnt(); + + sleep(1); + + pcfg(1, 6, -1, INPUT | PULLDOWN, "ser"); + pcfg(1, 13, 0, OUTPUT, "clk"); + pcfg(1, 15, 0, OUTPUT, "shld"); + pcfg(0, 27, 0, OUTPUT, "clr"); + + for(;;) { + HI("shld"); + usleep(1); + LO("shld"); + usleep(1); + } +} diff --git a/pins.h b/pins.h new file mode 100644 index 0000000..fe53ef5 --- /dev/null +++ b/pins.h @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "seg.h" + +#define INPUT 1 << 0 +#define OUTPUT 1 << 1 +#define PULLUP 1 << 2 +#define PULLDOWN 1 << 3 + +struct pin2dev { + int pin; + int dev; + char *name; +}; + +int pin_cnt; +struct pin2dev *pinmap; +volatile int g0, g1, g2; + +int num2fd(int i) { + if (i == 0) + return g0; + else if (i == 1) + return g1; + else if (i == 2) + return g2; + + return 0; +} + +struct pin2dev *pin(char *name) { + for(int i = 0; i < pin_cnt; i++) { + if (! strncmp(name, pinmap[i].name, GPIOPINMAXNAME)) + return &pinmap[i]; + } + + return NULL; +} + +void set(char *name, int val) { + struct pin2dev *p; + struct gpio_pin_op op; + int fd; + + p = pin(name); + op.gp_pin = p->pin; + op.gp_value = val; + fd = num2fd(p->dev); + + //printf("given name: %s\nderived name: %s\nderived pin: %d\nval: %d\nfd: %d\nreal fd: %d\n", name, p->name, + // p->pin, op.gp_value, fd, g1); + +// printf("g0: %d g1: %d g2: %d\n", g0, g1, g2); + + ioctl(fd, GPIOPINWRITE, &op); + +// printf("old: %d new: %d\n", op.gp_value, val); +} + +void pins_init() { + pinmap = calloc(100, sizeof(struct pin2dev)); + pin_cnt = 0; +} + +void pcfg(int devno, int pin, int val, uint8_t mode, char *name) { + char *fill = calloc(1, 255); + char *pill = calloc(1, 255); + char *vl = calloc(1, 255); + char *nm = calloc(1, 255); + char *md = calloc(1, 255); + + usleep(1000 * 500); + strlcpy(nm, name, GPIOPINMAXNAME); + + if (mode & OUTPUT) + strlcpy(md, "out", 255); + else if (mode & INPUT) { + if (mode & PULLUP) + strlcpy(md, "in,pu", 255); + else if (mode & PULLDOWN) + strlcpy(md, "in,pd", 255); + else + strlcpy(md, "in", 255); + } + + pinmap[pin_cnt].name = calloc(1, GPIOPINMAXNAME); + strlcpy(pinmap[pin_cnt].name, name, GPIOPINMAXNAME); + strlcpy(nm, name, GPIOPINMAXNAME); + pinmap[pin_cnt].pin = pin; + pinmap[pin_cnt].dev = devno; + pin_cnt++; + + sprintf(fill, "gpioctl gpio%d %d set %s", devno, pin, nm); + sprintf(pill, "gpioctl gpio%d %s set %s", devno, nm, md); + sprintf(vl, "gpioctl gpio%d %d %d", devno, pin, val); + printf("%s\n%s\n", fill, pill); + system(fill); + system(pill); + if (val < 0) + return; + printf("%s\n", vl); + system(vl); +} diff --git a/seg.c b/seg.c index 8af863e..7eef5f8 100644 --- a/seg.c +++ b/seg.c @@ -21,29 +21,74 @@ int main(int argc, char *argv[]) { uint8_t ent[4]; signal(SIGINT, inth); + signal(SIGALRM, SIG_DFL); config_pins(); pincnt(); + start(); + byte(0x8f); + stop(); + start(); byte(0x40); stop(); start(); - byte(0xc0 | 0x01); - -for(;;) { + byte(0xc0 | 0x00); + byte(0x00); + byte(0x00); + byte(0x00); + byte(0x00); + byte(0x00); + byte(0x00); +/*for(;;) { arc4random_buf(ent, 32); byte(ent[0]); byte(ent[1]); byte(ent[2]); byte(ent[3]); usleep(DEL); +}*/ + +for(;;) { + UP("clk"); + kd(); + DOWN("clk"); + kd(); } - stop(); + +for(;;) { + byte(dig(0x0c)); + byte(dig(0x0a)); + byte(dig(0x0f)); + byte(dig(0x0e)); + byte(0x00); + byte(0x00); + usleep(1000 * 350); + byte(dig(0x0b)); + byte(dig(0x0a)); + byte(dig(0x0b)); + byte(dig(0x0e)); + byte(0x00); + byte(0x00); + usleep(1000*350); +} +/* uint8_t inc = 0; + uint8_t c = 0; + for(;;) { + if (c == 4) { byte(0x00); byte(0x00); c = 0; continue;} + byte(dig(inc)); + if (inc == 0xf) { inc = 0; usleep(1000 * 2000); c++; continue; } + inc++; + usleep(1000 * 700); + c++; + } */ +/* stop(); start(); byte(DISP_ON); - stop(); + stop();*/ + inth(0); return 0; diff --git a/seg.h b/seg.h index a831f58..d3c5679 100644 --- a/seg.h +++ b/seg.h @@ -4,10 +4,12 @@ #include #include #include +#include #include #include #include +#include #define UP(N) set_pin(N, 1) #define DOWN(N) set_pin(N, 0) @@ -28,10 +30,64 @@ #define DISP_7 0x8e #define DISP_8 0x8f - volatile int g0, g1, g2; +const uint8_t d2seg[] = { + 0x3F + , 0x06 + , 0x5B + , 0x4F + , 0x66 + , 0x6D + , 0x7D + , 0x07 + , 0x7F + , 0x6F + , 0x77 + , 0x7C + , 0x39 + , 0x5E + , 0x79 + , 0x71 +}; + +uint8_t dig(uint8_t d) { + return d2seg[d]; +} + +/*void timer(long usec) { + sigset_t sigs; + int s; + + sigemptyset(&sigs); + sigaddset(&sigs, SIGALRM); + + ualarm(usec, 0); + sigwait(&sigs, &s); +}*/ + +/*void timer(long usec) { + struct timespec t; + t.tv_nsec = usec; + t.tv_sec = 0; + + nanosleep(&t, NULL); +}*/ + void kd() { + + asm("movs r0, #0"); + + for(int i = 0; i < 10; i++) { + asm("movs r0, #10\n\t" + "1: subs r0, r0, #1\n\t" + "bne 1b"); + } + +/* volatile int t = 0; + + while(t != 80000) + t++; */ /* struct timespec ts, rem; ts.tv_sec = 0; @@ -40,8 +96,7 @@ void kd() { rem.tv_sec = 0; rem.tv_nsec = 0; - nanosleep(&ts, &rem); - */ + nanosleep(&ts, &rem); */ } int name2dev(char *s) { @@ -59,6 +114,16 @@ int name2dev(char *s) { return g2; else if (! strncmp(s, "clk", GPIOPINMAXNAME)) return g2; + else if (! strncmp(s, "ser2", GPIOPINMAXNAME)) + return g1; + else if (! strncmp(s, "clk-sr", GPIOPINMAXNAME)) + return g1; + else if (! strncmp(s, "shld", GPIOPINMAXNAME)) + return g1; + else if (! strncmp(s, "clr", GPIOPINMAXNAME)) + return g0; + + else { printf("no gpio pin w/ name %s, exiting..\n", s); exit(2); @@ -231,4 +296,3 @@ void blink(int c, int ms) { } } - diff --git a/sr.sh b/sr.sh new file mode 100755 index 0000000..97d1b88 --- /dev/null +++ b/sr.sh @@ -0,0 +1,7 @@ +#!/bin/ksh + + +gpioctl gpio1 13 set output clk +gpioctl gpio1 6 set input pd srser +gpioctl gpio1 15 set output shld +gpioctl gpio0 27 set output clr -- 2.41.0