/* * bbl.c - Produce a "Knight Rider" effect on the LED board * * Written 2010 by Werner Almesberger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. */ #include #include #include #include #include #include #include #define PDDATS 0x10010314 #define PDDATC 0x10010318 #define PDFUNC 0x10010348 #define PDDIRS 0x10010364 #define PDDIRC 0x10010368 #define LEDS 10 #define LED_MASK 0x3f00 #define LED_SEL 11 #define DELAY_US 50000 #define PAGE_SIZE 4096 #define GPIO_BASE 0x10010000 #define REG(n) (*(uint32_t *) (mem+(n)-GPIO_BASE)) static volatile void *mem; /* ----- Command-line parsing ---------------------------------------------- */ static int map(int g) { switch (g) { case 0: return 12; case 1: return 13; case 2: return 8; case 3: return 9; case 4: return 10; default: abort(); } } static void set(int n) { if (n & 1) { REG(PDDATC) = LED_MASK; REG(PDDATS) = 1 << map(n >> 1); } else { REG(PDDATS) = LED_MASK; REG(PDDATC) = 1 << map(n >> 1); } } static void die(int sig) { REG(PDDIRC) = LED_MASK; _exit(0); } int main(int argc, char **argv) { int fd, i; fd = open("/dev/mem", O_RDWR); if (fd < 0) { perror("/dev/mem"); exit(1); } mem = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO_BASE); if (mem == MAP_FAILED) { perror("mmap"); exit(1); } signal(SIGINT, die); REG(PDFUNC) = LED_MASK; REG(PDDIRS) = LED_MASK; while (1) { for (i = 0; i != LEDS-1; i++) { set(i); usleep(DELAY_US); } for (i = LEDS-1; i != 0; i--) { set(i); usleep(DELAY_US); } } return 0; }