1
0
mirror of https://github.com/danog/fast-gpio.git synced 2024-11-26 20:04:37 +01:00

Added functionality to set and get pin direction

This commit is contained in:
greenbreakfast 2015-10-02 19:51:02 +08:00
parent 5a8d56d555
commit 56cc6b4e2b
6 changed files with 61 additions and 4 deletions

View File

@ -20,6 +20,7 @@ public:
~FastGpio(void);
int SetDirection (int pinNum, int bOutput);
int GetDirection (int pinNum, int &bOutput);
int Set (int pinNum, int value);
int Read (int pinNum, int &value);

View File

@ -23,6 +23,8 @@
typedef enum e_GpioCmd {
GPIO_CMD_SET = 0,
GPIO_CMD_READ,
GPIO_CMD_SET_DIRECTION,
GPIO_CMD_GET_DIRECTION,
GPIO_CMD_PWM,
NUM_GPIO_CMD
} gpioCmd;
@ -32,6 +34,7 @@ struct gpioSetup {
int pinNumber;
int pinValue;
int pinDir; // 0 - input, 1 - output
// pwm options
int bPwm;

View File

@ -30,7 +30,9 @@ protected:
int _SetupAddress (unsigned long int blockBaseAddr, unsigned long int blockSize);
void _WriteReg (unsigned long int registerOffset, unsigned long int value);
unsigned long int _ReadReg (unsigned long int registerOffset);
void _SetBit (unsigned long int &regVal, int bitNum, int value);
int _GetBit (unsigned long int regVal, int bitNum);
// protected members
int verbosityLevel;

View File

@ -31,6 +31,21 @@ int FastGpio::SetDirection(int pinNum, int bOutput)
return (EXIT_SUCCESS);
}
int FastGpio::GetDirection(int pinNum, int &bOutput)
{
unsigned long int regVal;
// read the current input and output settings
regVal = _ReadReg(REGISTER_OE_OFFSET);
if (verbosityLevel > 0) printf("Direction setting read: 0x%08lx\n", regVal);
// find the OE for this pin
bOutput = _GetBit(regVal, pinNum);
return (EXIT_SUCCESS);
}
int FastGpio::Set(int pinNum, int value)
{
unsigned long int regAddr;
@ -63,7 +78,7 @@ int FastGpio::Read(int pinNum, int &value)
regVal = _ReadReg (REGISTER_IN_OFFSET);
// find the value of the specified pin
value = ((regVal >> pinNum) & 0x1);
value = _GetBit(regVal, pinNum);
return EXIT_SUCCESS;

View File

@ -5,6 +5,7 @@ void initGpioSetup (gpioSetup* obj)
{
obj->pinNumber = -1;
obj->pinValue = 0;
obj->pinDir = 0;
obj->bPwm = 0;
obj->pwmFreq = 0;
@ -13,6 +14,9 @@ void initGpioSetup (gpioSetup* obj)
void printUsage(char* progName) {
printf("Usage:\n");
printf("\t%s set-input <gpio>\n", progName);
printf("\t%s set-output <gpio>\n", progName);
printf("\t%s get-direction <gpio>\n", progName);
printf("\t%s read <gpio>\n", progName);
printf("\t%s set <gpio> <value: 0 or 1>\n", progName);
printf("\t%s pwm <gpio> <freq in Hz> <duty cycle percentage>\n", progName);
@ -38,7 +42,18 @@ int parseArguments(int argc, char* argv[], gpioSetup *setup)
// arg1 - command: read, set
// arg2 - gpio pin number
// arg3 - value to write in case of set
if (strcmp(argv[1], "set") == 0 ) {
if (strcmp(argv[1], "set-input") == 0 ) {
setup->cmd = GPIO_CMD_SET_DIRECTION;
setup->pinDir = 0;
}
else if (strcmp(argv[1], "set-output") == 0 ) {
setup->cmd = GPIO_CMD_SET_DIRECTION;
setup->pinDir = 1;
}
else if (strcmp(argv[1], "get-direction") == 0 ) {
setup->cmd = GPIO_CMD_GET_DIRECTION;
}
else if (strcmp(argv[1], "set") == 0 ) {
setup->cmd = GPIO_CMD_SET;
// get the write value
@ -78,10 +93,10 @@ int gpioRun(gpioSetup* setup)
// object operations
switch (setup->cmd) {
case GPIO_CMD_SET:
if (FASTGPIO_VERBOSE > 0) printf("Setting GPIO%d to %d\n", setup->pinNumber, setup->pinValue);
gpioObj.SetDirection(setup->pinNumber, 1); // set to output
gpioObj.Set(setup->pinNumber, setup->pinValue);
printf("Setting GPIO%d to %d\n", setup->pinNumber, setup->pinValue);
break;
case GPIO_CMD_READ:
@ -89,6 +104,16 @@ int gpioRun(gpioSetup* setup)
printf("Read GPIO%d: %d\n", setup->pinNumber, setup->pinValue);
break;
case GPIO_CMD_SET_DIRECTION:
gpioObj.SetDirection(setup->pinNumber, setup->pinDir); // set pin direction
printf("Setting GPIO%d to %s direction\n", setup->pinNumber, (setup->pinDir == 1 ? "OUTPUT" : "INPUT") );
break;
case GPIO_CMD_GET_DIRECTION:
gpioObj.GetDirection(setup->pinNumber, setup->pinDir); // find pin direction
printf("GPIO%d direction is \n", setup->pinNumber, (setup->pinDir == 1 ? "OUTPUT" : "INPUT") );
break;
default:
status = EXIT_FAILURE;
break;

View File

@ -99,3 +99,14 @@ void Module::_SetBit(unsigned long int &regVal, int bitNum, int value)
// try this out
// regVal ^= (-value ^ regVal) & (1 << bitNum);
}
// find the value of a single bit
int Module::_GetBit(unsigned long int regVal, int bitNum)
{
int value;
// isolate the specific bit
value = ((regVal >> bitNum) & 0x1);
return (value);
}