/* GPIO.c */ #include #include #include #include #include #include #include "GPIO.h" /*============================================================================*/ typedef enum { GPFSEL0, //pin function select GPFSEL1, GPFSEL2, GPSET0 = 7, //(write only) set pin high GPSET1, GPCLR0 = 10, //(write only) set pin low GPCLR1, GPLEV0 = 13, //(read only) read current level GPLEV1, GPPUD = 37 //pull-up/down enable }gpioReg_t; static bool isInit = false; static volatile uint32_t *gpioReg = MAP_FAILED; static gpioPinFunction_t shadowPinFunction[28]; static char* errorStrings[] = { "WARNING: attempting to set GPIO pin type without calling gpioInit\n\0", "WARNING: attempting to read GPIO pin without calling gpioInit\n\0", "WARNING: attempting to write GPIO pin without calling gpioInit\n\0", }; /*============================================================================*/ uint32_t gpioGetRawLogicLevels(void){ if(!isInit){ fprintf(stderr,errorStrings[1]); return 0; } return 0x0FFFFFFF & gpioReg[GPLEV0]; } /*============================================================================*/ void gpioSetRawLogicLevels(uint32_t bitmask){ uint32_t temp; if(!isInit){ fprintf(stderr,errorStrings[2]); return; } gpioReg[GPCLR0] = 0x0FFFFFFF & ~bitmask; gpioReg[GPSET0] = 0x0FFFFFFF & bitmask; } /*============================================================================*/ void gpioSetPinType(gpioPin_t pinNumber, gpioPinFunction_t mode){ uint8_t regNum, numShifts; if(!isInit){ fprintf(stderr,errorStrings[0]); return; } shadowPinFunction[pinNumber] = mode; regNum = pinNumber/10; // 10 pins per register numShifts = pinNumber%10 * 3; // 3 bits per pin gpioReg[regNum] = (gpioReg[regNum] & ~(7<>= pinNumber; return input % 2; } /*============================================================================*/ void gpioWritePin(gpioPin_t pinNumber, bool logicHigh){ gpioReg_t regNumber; uint32_t pinSelect; if(!isInit){ fprintf(stderr,errorStrings[2]); return; } regNumber = logicHigh ? GPSET0 : GPCLR0; pinSelect = 1; pinSelect <<= pinNumber; gpioReg[regNumber] = pinSelect; } /*============================================================================*/ bool gpioInit(void){ int i, fileDescriptor; bool result; gpioReg = MAP_FAILED; result = false; //get this resource with read/write access with sync fileDescriptor = open("/dev/gpiomem", O_RDWR | O_SYNC) ; if(fileDescriptor){ gpioReg = (uint32_t *)mmap(NULL, 0xB4, PROT_READ|PROT_WRITE, MAP_SHARED, fileDescriptor, 0); close(fileDescriptor); //this will not invalidate the mmap //if that mmap does not fail, set all 28 pins to output and set them logic high if(gpioReg != MAP_FAILED){ gpioReg[GPFSEL0] = 0x09249249; gpioReg[GPFSEL1] = 0x09249249; gpioReg[GPFSEL2] &= 0xff000000; gpioReg[GPFSEL2] |= 0x00249249; gpioReg[GPSET0] = 0x0FFFFFFF; for( i = 0; i < 28; i++) shadowPinFunction[i] = PIN_OUTPUT; result = true; isInit = true; }else fprintf(stderr, "failed to mmap /dev/gpiomem\n"); } else fprintf(stderr, "failed to open /dev/gpiomem\n"); return result; } /*============================================================================*/