12 #define PAGE_SIZE (4*1024)
13 #define BLOCK_SIZE (4*1024)
15 /* Direct access to GPIO hardware */
17 // Access from ARM Running Linux
18 // For Raspberry Pi 2 and Pi 3, change BCM2708_PERI_BASE to 0x3F000000 for the code to work.
20 //#define BCM2708_PERI_BASE 0x20000000
21 #define BCM2708_PERI_BASE 0x3F000000
22 #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
26 volatile unsigned *gpio;
29 // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
30 #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
31 #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
32 #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
34 #define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0
35 #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
37 #define GET_GPIO(g) (*(gpio+13)&(1<<g)) // 0 if LOW, (1<<g) if HIGH
39 #define GPIO_PULL *(gpio+37) // Pull up/pull down
40 #define GPIO_PULLCLK0 *(gpio+38) // Pull up/pull down clock
46 /* GPIO pin assignments */
52 #define TEST_CYCLES (60 * 5) // 1 minute
54 unsigned fir_record[TEST_CYCLES];
56 int main(int argc, char *argv[])
58 unsigned int fir; // A simple FIR filter
60 int old_state, new_state;
61 struct timespec interval;
64 interval.tv_nsec = 200000000;
66 // Set up gpi pointer for direct register access
69 // Initialize the IO pins.
73 // Initialize to switched off state
79 while (cycle < TEST_CYCLES)
81 unsigned input = GET_GPIO(SWITCH_IN) == 0 ? 0 : 1;
86 fprintf(stderr, "FIR = 0x%x\r", fir);
87 if ( (fir & FIRMASK) == 0)
91 if ( (fir & FIRMASK) == FIRMASK)
96 if (new_state != old_state)
98 //fprintf(stderr, "Changed state to %d\r", new_state);
101 system("lightcontrol -r 100 -g 100 -b 100 -w 100");
105 system("lightcontrol -r 0 -g 0 -b 0 -w 0");
108 old_state = new_state;
110 fir_record[cycle++] = fir;
112 nanosleep(&interval, NULL);
115 for (int i = 0; i < TEST_CYCLES; i++)
117 //printf("FIR = 0x%08x\n", fir_record[i]);
124 // Set up a memory regions to access GPIO
132 if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0)
134 printf("can't open /dev/mem \n");
140 NULL, //Any adddress in our space will do
141 BLOCK_SIZE, //Map length
142 PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory
143 MAP_SHARED, //Shared with other processes
144 mem_fd, //File to map
145 GPIO_BASE //Offset to GPIO peripheral
148 close(mem_fd); //No need to keep mem_fd open after mmap
150 if (gpio_map == MAP_FAILED)
152 printf("mmap error %d, errno = %d\n", gpio_map, errno);//errno also set!
156 // Always use volatile pointer!
157 gpio = (volatile unsigned *)gpio_map;