Backlight dimming in C example
From wiki.gp2x.org
This is some C code which when run, dims the backlight slightly. It is based upon the PWM fader example source and various other bits and pieces from around the wiki. Sadly, any heavy CPU usage makes the backlight flicker, so this really needs to be moved onto the 940 to be of any use, but then it could interfere with some programs.
/*Copyright (c) 2006, Orkie All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the Orkie nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #include <sys/mman.h> #include <linux/fb.h> #include <unistd.h> #include <stropts.h> #include <errno.h> #include <time.h> #include <string.h> extern int errno; unsigned long *memregs32; unsigned short *memregs16; int memfd; #define GPIOAPINLVL 0x1180 #define GPIOBPINLVL 0x1182 #define GPIOCPINLVL 0x1184 #define GPIODPINLVL 0x1186 #define GPIOEPINLVL 0x1188 #define GPIOFPINLVL 0x118A #define GPIOGPINLVL 0x118C #define GPIOHPINLVL 0x118E #define GPIOIPINLVL 0x1190 #define GPIOJPINLVL 0x1192 #define GPIOKPINLVL 0x1194 #define GPIOLPINLVL 0x1196 #define GPIOMPINLVL 0x1198 #define GPIONPINLVL 0x119A #define GPIOOPINLVL 0x119C #define GPIOAOUT 0x1060 #define GPIOBOUT 0x1062 #define GPIOCOUT 0x1064 #define GPIODOUT 0x1066 #define GPIOEOUT 0x1068 #define GPIOFOUT 0x106A #define GPIOGOUT 0x106C #define GPIOHOUT 0x106E #define GPIOIOUT 0x1070 #define GPIOJOUT 0x1072 #define GPIOKOUT 0x1074 #define GPIOLOUT 0x1076 #define GPIOMOUT 0x1078 #define GPIONOUT 0x107A #define GPIOOOUT 0x107C void *trymmap (void *start, size_t length, int prot, int flags, int fd, off_t offset) { char *p; int aa; printf ("mmap(%X, %X, %X, %X, %X, %X) ... ", (unsigned int)start, length, prot, flags, fd, (unsigned int)offset); p = mmap (start, length, prot, flags, fd, offset); if (p == (char *)0xFFFFFFFF) { aa = errno; printf ("failed. errno = %d\n", aa); } else { printf ("OK! (%X)\n", (unsigned int)p); } return p; } unsigned char initphys (void) { memfd = open("/dev/mem", O_RDWR); if (memfd == -1) { printf ("Open failed\n"); return 0; } printf ("/dev/mem opened successfully - fd = %d\n", memfd); memregs32 = trymmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memfd, 0xc0000000); if (memregs32 == (unsigned long *)0xFFFFFFFF) return 0; memregs16 = (unsigned short *)memregs32; return 1; } void closephys (void) { close (memfd); } int main(int argc, char *argv[]) { if (!initphys()) return 0; short unsigned int run = 1; while(run) { usleep(0); memregs16[GPIOHOUT >> 1] ^= 4; } closephys(); return 0; }
Note For the F200:
#define GPIOHOUT 0x1076 ... memregs16[GPIOHOUT >> 1] ^= 1 << 11; //0x0800;