11 #include <sys/ioctl.h>
14 #define UP(N) set_pin(N, 1)
15 #define DOWN(N) set_pin(N, 0)
20 #define ADDR_AUTO 0x44
33 volatile int g0
, g1
, g2
;
35 const uint8_t d2seg
[] = {
54 uint8_t dig(uint8_t d
) {
58 /*void timer(long usec) {
63 sigaddset(&sigs, SIGALRM);
69 /*void timer(long usec) {
83 for(int i = 0; i < 10; i++) {
84 asm("movs r0, #10\n\t"
85 "1: subs r0, r0, #1\n\t"
89 /* volatile int t = 0;
93 /* struct timespec ts, rem;
101 nanosleep(&ts, &rem); */
104 int name2dev(char *s
) {
105 if (! strncmp(s
, "ser", GPIOPINMAXNAME
))
107 else if (! strncmp(s
, "oe", GPIOPINMAXNAME
))
109 else if (! strncmp(s
, "rclk", GPIOPINMAXNAME
))
111 else if (! strncmp(s
, "srclk", GPIOPINMAXNAME
))
113 else if (! strncmp(s
, "srclr", GPIOPINMAXNAME
))
115 else if (! strncmp(s
, "dio", GPIOPINMAXNAME
))
117 else if (! strncmp(s
, "clk", GPIOPINMAXNAME
))
119 else if (! strncmp(s
, "ser2", GPIOPINMAXNAME
))
121 else if (! strncmp(s
, "clk-sr", GPIOPINMAXNAME
))
123 else if (! strncmp(s
, "shld", GPIOPINMAXNAME
))
125 else if (! strncmp(s
, "clr", GPIOPINMAXNAME
))
130 printf("no gpio pin w/ name %s, exiting..\n", s
);
144 struct gpio_pin_set p
;
145 struct gpio_pin_op o
;
150 p
.gp_flags
= GPIO_PIN_INPUT
| GPIO_PIN_PULLDOWN
;
153 ioctl(name2dev("dio"), GPIOPINWRITE
, &o
);
154 ioctl(name2dev("dio"), GPIOPINSET
, &p
);
157 printf("waiting..\n");
158 ioctl(name2dev("dio"), GPIOPINREAD
, &o
);
162 p
.gp_flags
= GPIO_PIN_OUTPUT
;
163 ioctl(name2dev("dio"), GPIOPINREAD
, &o
);
166 void set_pin(char *name
, int state
) {
167 struct gpio_pin_op op
;
169 strlcpy(op
.gp_name
, name
, GPIOPINMAXNAME
);
172 ioctl(name2dev(name
), GPIOPINWRITE
, &op
);
176 struct gpio_pin_set
*ser
, *oe
, *rclk
, *srclk
, *srclr
, *dio
, *clk
;
179 system("gpioctl gpio2 6 set out clk");
180 system("gpioctl gpio2 8 set out dio");
182 ser
= calloc(1, sizeof(struct gpio_pin_set
));
183 oe
= calloc(1, sizeof(struct gpio_pin_set
));
184 rclk
= calloc(1, sizeof(struct gpio_pin_set
));
185 srclk
= calloc(1, sizeof(struct gpio_pin_set
));
186 srclr
= calloc(1, sizeof(struct gpio_pin_set
));
187 dio
= calloc(1, sizeof(struct gpio_pin_set
));
188 clk
= calloc(1, sizeof(struct gpio_pin_set
));
191 ser
->gp_flags
= GPIO_PIN_OUTPUT
;
192 strlcpy(ser
->gp_name2
, "ser", GPIOPINMAXNAME
);
195 oe
->gp_flags
= GPIO_PIN_OUTPUT
;
196 strlcpy(oe
->gp_name2
, "oe", GPIOPINMAXNAME
);
199 rclk
->gp_flags
= GPIO_PIN_OUTPUT
;
200 strlcpy(rclk
->gp_name2
, "rclk", GPIOPINMAXNAME
);
203 srclk
->gp_flags
= GPIO_PIN_OUTPUT
;
204 strlcpy(srclk
->gp_name2
, "srclk", GPIOPINMAXNAME
);
207 srclr
->gp_flags
= GPIO_PIN_OUTPUT
;
208 strlcpy(srclr
->gp_name2
, "srclr", GPIOPINMAXNAME
);
211 dio
->gp_flags
= GPIO_PIN_OUTPUT
;
212 strlcpy(dio
->gp_name2
, "dio", GPIOPINMAXNAME
);
215 clk
->gp_flags
= GPIO_PIN_OUTPUT
;
216 strlcpy(clk
->gp_name2
, "clk", GPIOPINMAXNAME
);
218 ioctl(name2dev("ser"), GPIOPINSET
, ser
);
219 ioctl(name2dev("oe"), GPIOPINSET
, oe
);
220 ioctl(name2dev("rclk"), GPIOPINSET
, rclk
);
221 ioctl(name2dev("srclk"), GPIOPINSET
, srclk
);
222 ioctl(name2dev("srclr"), GPIOPINSET
, srclr
);
223 ioctl(name2dev("dio"), GPIOPINSET
, dio
);
224 ioctl(name2dev("clk"), GPIOPINSET
, clk
);
236 g0
= open("/dev/gpio0", O_RDWR
);
237 g1
= open("/dev/gpio1", O_RDWR
);
238 g2
= open("/dev/gpio2", O_RDWR
);
240 struct gpio_info
*inf0
, *inf1
, *inf2
;
241 inf0
= calloc(1, sizeof(struct gpio_info
));
242 inf1
= calloc(1, sizeof(struct gpio_info
));
243 inf2
= calloc(1, sizeof(struct gpio_info
));
245 ioctl(g0
, GPIOINFO
, inf0
);
246 ioctl(g1
, GPIOINFO
, inf1
);
247 ioctl(g2
, GPIOINFO
, inf2
);
249 printf("gpio0: %d\ngpio1: %d\ngpio2: %d\n", inf0
->gpio_npins
, inf1
->gpio_npins
, inf2
->gpio_npins
);
270 void byte(uint8_t b
) {
271 for(int i
= 0; i
< 8; i
++) {
274 (b
& 0x1) ? UP("dio") : DOWN("dio");
292 void blink(int c
, int ms
) {
293 for(int i
= 0; i
< c
; i
++) {