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) {
81 for(int i
= 0; i
< 10; i
++) {
82 asm("movs r0, #10\n\t"
83 "1: subs r0, r0, #1\n\t"
87 /* volatile int t = 0;
91 /* struct timespec ts, rem;
99 nanosleep(&ts, &rem); */
102 int name2dev(char *s
) {
103 if (! strncmp(s
, "ser", GPIOPINMAXNAME
))
105 else if (! strncmp(s
, "oe", GPIOPINMAXNAME
))
107 else if (! strncmp(s
, "rclk", GPIOPINMAXNAME
))
109 else if (! strncmp(s
, "srclk", GPIOPINMAXNAME
))
111 else if (! strncmp(s
, "srclr", GPIOPINMAXNAME
))
113 else if (! strncmp(s
, "dio", GPIOPINMAXNAME
))
115 else if (! strncmp(s
, "clk", GPIOPINMAXNAME
))
117 else if (! strncmp(s
, "ser2", GPIOPINMAXNAME
))
119 else if (! strncmp(s
, "clk-sr", GPIOPINMAXNAME
))
121 else if (! strncmp(s
, "shld", GPIOPINMAXNAME
))
123 else if (! strncmp(s
, "clr", GPIOPINMAXNAME
))
128 printf("no gpio pin w/ name %s, exiting..\n", s
);
142 struct gpio_pin_set p
;
143 struct gpio_pin_op o
;
148 p
.gp_flags
= GPIO_PIN_INPUT
| GPIO_PIN_PULLDOWN
;
151 ioctl(name2dev("dio"), GPIOPINWRITE
, &o
);
152 ioctl(name2dev("dio"), GPIOPINSET
, &p
);
155 printf("waiting..\n");
156 ioctl(name2dev("dio"), GPIOPINREAD
, &o
);
160 p
.gp_flags
= GPIO_PIN_OUTPUT
;
161 ioctl(name2dev("dio"), GPIOPINREAD
, &o
);
164 void set_pin(char *name
, int state
) {
165 struct gpio_pin_op op
;
167 strlcpy(op
.gp_name
, name
, GPIOPINMAXNAME
);
170 ioctl(name2dev(name
), GPIOPINWRITE
, &op
);
174 struct gpio_pin_set
*ser
, *oe
, *rclk
, *srclk
, *srclr
, *dio
, *clk
;
177 system("gpioctl gpio2 6 set out clk");
178 system("gpioctl gpio2 8 set out dio");
180 ser
= calloc(1, sizeof(struct gpio_pin_set
));
181 oe
= calloc(1, sizeof(struct gpio_pin_set
));
182 rclk
= calloc(1, sizeof(struct gpio_pin_set
));
183 srclk
= calloc(1, sizeof(struct gpio_pin_set
));
184 srclr
= calloc(1, sizeof(struct gpio_pin_set
));
185 dio
= calloc(1, sizeof(struct gpio_pin_set
));
186 clk
= calloc(1, sizeof(struct gpio_pin_set
));
189 ser
->gp_flags
= GPIO_PIN_OUTPUT
;
190 strlcpy(ser
->gp_name2
, "ser", GPIOPINMAXNAME
);
193 oe
->gp_flags
= GPIO_PIN_OUTPUT
;
194 strlcpy(oe
->gp_name2
, "oe", GPIOPINMAXNAME
);
197 rclk
->gp_flags
= GPIO_PIN_OUTPUT
;
198 strlcpy(rclk
->gp_name2
, "rclk", GPIOPINMAXNAME
);
201 srclk
->gp_flags
= GPIO_PIN_OUTPUT
;
202 strlcpy(srclk
->gp_name2
, "srclk", GPIOPINMAXNAME
);
205 srclr
->gp_flags
= GPIO_PIN_OUTPUT
;
206 strlcpy(srclr
->gp_name2
, "srclr", GPIOPINMAXNAME
);
209 dio
->gp_flags
= GPIO_PIN_OUTPUT
;
210 strlcpy(dio
->gp_name2
, "dio", GPIOPINMAXNAME
);
213 clk
->gp_flags
= GPIO_PIN_OUTPUT
;
214 strlcpy(clk
->gp_name2
, "clk", GPIOPINMAXNAME
);
216 ioctl(name2dev("ser"), GPIOPINSET
, ser
);
217 ioctl(name2dev("oe"), GPIOPINSET
, oe
);
218 ioctl(name2dev("rclk"), GPIOPINSET
, rclk
);
219 ioctl(name2dev("srclk"), GPIOPINSET
, srclk
);
220 ioctl(name2dev("srclr"), GPIOPINSET
, srclr
);
221 ioctl(name2dev("dio"), GPIOPINSET
, dio
);
222 ioctl(name2dev("clk"), GPIOPINSET
, clk
);
234 g0
= open("/dev/gpio0", O_RDWR
);
235 g1
= open("/dev/gpio1", O_RDWR
);
236 g2
= open("/dev/gpio2", O_RDWR
);
238 struct gpio_info
*inf0
, *inf1
, *inf2
;
239 inf0
= calloc(1, sizeof(struct gpio_info
));
240 inf1
= calloc(1, sizeof(struct gpio_info
));
241 inf2
= calloc(1, sizeof(struct gpio_info
));
243 ioctl(g0
, GPIOINFO
, inf0
);
244 ioctl(g1
, GPIOINFO
, inf1
);
245 ioctl(g2
, GPIOINFO
, inf2
);
247 printf("gpio0: %d\ngpio1: %d\ngpio2: %d\n", inf0
->gpio_npins
, inf1
->gpio_npins
, inf2
->gpio_npins
);
268 void byte(uint8_t b
) {
269 for(int i
= 0; i
< 8; i
++) {
272 (b
& 0x1) ? UP("dio") : DOWN("dio");
290 void blink(int c
, int ms
) {
291 for(int i
= 0; i
< c
; i
++) {