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

Compatible for Omega and Omega2

This commit is contained in:
ratatatatat 2016-08-15 15:29:18 -04:00
parent c278d8b45d
commit 4537514b55
12 changed files with 364 additions and 46 deletions

View File

@ -2,32 +2,32 @@
#define _FAST_GPIO_H_
#include <module.h>
#include <string.h>
//Define Macros in derived class.
// #define REGISTER_BLOCK_ADDR 0x18040000
// #define REGISTER_BLOCK_SIZE 0x30
#define REGISTER_BLOCK_ADDR 0x18040000
#define REGISTER_BLOCK_SIZE 0x30
// #define REGISTER_OE_OFFSET 0
// #define REGISTER_IN_OFFSET 1
// #define REGISTER_OUT_OFFSET 2
// #define REGISTER_SET_OFFSET 3
// #define REGISTER_CLEAR_OFFSET 4
#define REGISTER_OE_OFFSET 0
#define REGISTER_IN_OFFSET 1
#define REGISTER_OUT_OFFSET 2
#define REGISTER_SET_OFFSET 3
#define REGISTER_CLEAR_OFFSET 4
//Define DEVICE_TYPE Here for now.
#define DEVICE_TYPE "omega2"
class FastGpio : public Module {
public:
FastGpio(void);
~FastGpio(void);
virtual int SetDirection (int pinNum, int bOutput)=0;
virtual int GetDirection (int pinNum, int &bOutput)=0;
int SetDirection (int pinNum, int bOutput);
int GetDirection (int pinNum, int &bOutput);
int Set (int pinNum, int value);
int Read (int pinNum, int &value);
virtual int Set (int pinNum, int value)=0;
virtual int Read (int pinNum, int &value)=0;
private:
// private functions
int pinNumber;
};

34
include/fastgpioomega.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef _FAST_GPIO_OMEGA_H_
#define _FAST_GPIO_OMEGA_H_
#include <fastgpio.h>
//Define Macros in derived class.
#define REGISTER_BLOCK_ADDR 0x18040000
#define REGISTER_BLOCK_SIZE 0x30
#define REGISTER_OE_OFFSET 0
#define REGISTER_IN_OFFSET 1
#define REGISTER_OUT_OFFSET 2
#define REGISTER_SET_OFFSET 3
#define REGISTER_CLEAR_OFFSET 4
class FastGpioOmega : public FastGpio {
public:
FastGpioOmega(void);
~FastGpioOmega(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);
private:
// private functions
int pinNumber;
};
#endif // _FAST_GPIO_OMEGA_H_

67
include/fastgpioomega2.h Normal file
View File

@ -0,0 +1,67 @@
#ifndef _FAST_GPIO_OMEGA2_H_
#define _FAST_GPIO_OMEGA2_H_
#include <fastgpio.h>
//Define Macros in derived class.
#define REG_BLOCK_ADDR 0x10000000
#define REG_BLOCK_SIZE 0x6AC
//DIRECTION CONTROL REGISTERS
//GPIO_CTRL_0 10000600(Directions for GPIO0-GPIO31)
#define REGISTER_CTRL0_OFFSET 384
//GPIO_CTRL_1 10000604(Directions for GPIO32-GPIO63)
#define REGISTER_CTRL1_OFFSET 385
//GPIO_CTRL_2 10000608(Directions for GPIO64-GPIO95)
#define REGISTER_CTRL2_OFFSET 386
//DATA REGISTERS: STATES OF GPIOS
//GPIO_DATA_0 10000620(GPIO0-31)
#define REGISTER_DATA0_OFFSET 392
//GPIO_DATA_1 10000624(GPIO32-63)
#define REGISTER_DATA1_OFFSET 393
//GPIO_DATA_2 10000628(GPIO64-95)
#define REGISTER_DATA2_OFFSET 394
//DATA SET REGISTERS: SET STATES OF GPIO_DATA_x registers
//GPIO_DSET_0 10000630(GPIO0-31)
#define REGISTER_DSET0_OFFSET 396
//GPIO_DSET_1 10000634(GPIO31-63)
#define REGISTER_DSET1_OFFSET 397
//GPIO_DSET_2 10000638(GPIO64-95)
#define REGISTER_DSET2_OFFSET 398
//DATA CLEAR REGISTERS: CLEAR BITS OF GPIO_DATA_x registers
//GPIO_DCLR_0 10000640(GPIO0-31)
#define REGISTER_DCLR0_OFFSET 400
//GPIO_DCLR_1 10000644(GPIO31-63)
#define REGISTER_DCLR1_OFFSET 401
//GPIO_DCLR_2 10000648(GPIO64-95)
#define REGISTER_DCLR2_OFFSET 402
class FastGpioOmega2 : public FastGpio {
public:
FastGpioOmega2(void);
~FastGpioOmega2(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);
private:
// private functions
int pinNumber;
int ctrlOffset;
int dataOffset;
int dataSetOffset;
int dataClrOffset;
void setGpioOffset(int gpio);//Populates the offset private members above depending on selected GPIO
};
#endif // _FAST_GPIO_OMEGA2_H_

View File

@ -3,7 +3,8 @@
#include <module.h>
#include <fastgpio.h>
#include <fastgpioomega.h>
#include <fastgpioomega2.h>
#include <unistd.h>
@ -31,7 +32,12 @@ private:
void _Pwm (int pinNum);
// private members
FastGpio gpio;
// The way it was before
// FastGpio gpio;
//Create a pointer to the base class
//Instantiate it in the constructor
FastGpio * gpio;
int bSetup;
double freq;

View File

@ -11,6 +11,8 @@
#include <fastgpio.h>
#include <fastpwm.h>
#include <fastgpioomega.h>
#include <fastgpioomega2.h>
#define FASTGPIO_VERBOSITY_QUIET (0)
#define FASTGPIO_VERBOSITY_NORMAL (1)

View File

@ -38,7 +38,7 @@ protected:
int verbosityLevel;
int debugLevel;
volatile unsigned long int *regAddress;
unsigned long int *regAddress;
};
#endif // _MODULE_H_

85
src/fastgpioomega.cpp Normal file
View File

@ -0,0 +1,85 @@
#include <fastgpioomega.h>
FastGpioOmega::FastGpioOmega(void)
{
// setup the memory address space
_SetupAddress(REGISTER_BLOCK_ADDR, REGISTER_BLOCK_SIZE);
}
FastGpioOmega::~FastGpioOmega(void)
{
// nothing for now
}
// public functions
int FastGpioOmega::SetDirection(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);
// set the OE for this pin
_SetBit(regVal, pinNum, bOutput);
if (verbosityLevel > 0) printf("Direction setting write: 0x%08lx\n", regVal);
// write the new register value
_WriteReg(REGISTER_OE_OFFSET, regVal);
return (EXIT_SUCCESS);
}
int FastGpioOmega::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 FastGpioOmega::Set(int pinNum, int value)
{
unsigned long int regAddr;
unsigned long int regVal;
if (value == 0 ) {
// write to the clear register
regAddr = REGISTER_CLEAR_OFFSET;
}
else {
// write to the set register
regAddr = REGISTER_SET_OFFSET;
}
// put the desired pin value into the register
regVal = (0x1 << pinNum);
// write to the register
_WriteReg (regAddr, regVal);
return EXIT_SUCCESS;
}
int FastGpioOmega::Read(int pinNum, int &value)
{
unsigned long int regVal;
// read the current value of all pins
regVal = _ReadReg (REGISTER_IN_OFFSET);
// find the value of the specified pin
value = _GetBit(regVal, pinNum);
return EXIT_SUCCESS;
}

112
src/fastgpioomega2.cpp Normal file
View File

@ -0,0 +1,112 @@
#include <fastgpioomega2.h>
FastGpioOmega2::FastGpioOmega2(void)
{
// setup the memory address space
_SetupAddress(REG_BLOCK_ADDR, REG_BLOCK_SIZE);
}
FastGpioOmega2::~FastGpioOmega2(void)
{
// nothing for now
}
void FastGpioOmega2::setGpioOffset(int gpio){
int mod;
mod = gpio / 32;
if(mod == 0){
this->ctrlOffset = REGISTER_CTRL0_OFFSET;
this->dataOffset = REGISTER_DATA0_OFFSET;
this->dataSetOffset = REGISTER_DSET0_OFFSET;
this->dataClrOffset = REGISTER_DCLR0_OFFSET;
}
else if(mod == 1){
this->ctrlOffset = REGISTER_CTRL1_OFFSET;
this->dataOffset = REGISTER_DATA1_OFFSET;
this->dataSetOffset = REGISTER_DSET1_OFFSET;
this->dataClrOffset = REGISTER_DCLR1_OFFSET;
} else{
this->ctrlOffset = REGISTER_CTRL2_OFFSET;
this->dataOffset = REGISTER_DATA2_OFFSET;
this->dataSetOffset = REGISTER_DSET2_OFFSET;
this->dataClrOffset = REGISTER_DCLR2_OFFSET;
}
}
// public functions
int FastGpioOmega2::SetDirection(int pinNum, int bOutput)
{
unsigned long int regVal;
setGpioOffset(pinNum);
int gpio;
gpio = pinNum % 32;
// read the current input and output settings
regVal = _ReadReg(ctrlOffset);
if (verbosityLevel > 0) printf("Direction setting read: 0x%08lx\n", regVal);
// set the OE for this pin
_SetBit(regVal, gpio, bOutput);
if (verbosityLevel > 0) printf("Direction setting write: 0x%08lx\n", regVal);
// write the new register value
_WriteReg(ctrlOffset, regVal);
return (EXIT_SUCCESS);
}
int FastGpioOmega2::GetDirection(int pinNum, int &bOutput)
{
unsigned long int regVal;
setGpioOffset(pinNum);
int gpio;
gpio = pinNum % 32;
// read the current input and output settings
regVal = _ReadReg(ctrlOffset);
if (verbosityLevel > 0) printf("Direction setting read: 0x%08lx\n", regVal);
bOutput = _GetBit(regVal, gpio);
return (EXIT_SUCCESS);
}
int FastGpioOmega2::Set(int pinNum, int value)
{
unsigned long int regAddr;
unsigned long int regVal;
setGpioOffset(pinNum);
int gpio;
gpio = pinNum % 32;
if (value == 0 ) {
// write to the clear register
regAddr = dataClrOffset;
}
else {
// write to the set register
regAddr = dataSetOffset;
}
// put the desired pin value into the register
regVal = (0x1 << gpio);
// write to the register
_WriteReg (regAddr, regVal);
return EXIT_SUCCESS;
}
int FastGpioOmega2::Read(int pinNum, int &value)
{
unsigned long int regVal;
setGpioOffset(pinNum);
int gpio;
gpio = pinNum % 32;
// read the current value of all pins
regVal = _ReadReg (dataOffset);
// find the value of the specified pin
value = _GetBit(regVal, gpio);
return EXIT_SUCCESS;
}

View File

@ -3,6 +3,12 @@
FastPwm::FastPwm(void)
{
Reset();
if (strcmp(DEVICE_TYPE, "omega") == 0) {
gpio = new FastGpioOmega();
} else {
gpio = new FastGpioOmega2();
}
}
FastPwm::FastPwm(int freq, int duty)
@ -11,6 +17,13 @@ FastPwm::FastPwm(int freq, int duty)
// setup the pwm info
_SetupPeriods(freq, duty);
//Instantiate the GPIO object
// object setup
if (strcmp(DEVICE_TYPE, "omega") == 0) {
gpio = new FastGpioOmega();
} else {
gpio = new FastGpioOmega2();
}
}
FastPwm::~FastPwm(void)
@ -74,16 +87,16 @@ void FastPwm::Pwm (int pinNum, int freq, int duty)
void FastPwm::_Pwm (int pinNum)
{
// set the pin to output
gpio.SetDirection(pinNum, 1);
gpio->SetDirection(pinNum, 1);
// start the loop
while (1) {
//// HIGH part of cycle
gpio.Set(pinNum, 1);
gpio->Set(pinNum, 1);
_Sleep(periodHigh);
// LOW part of cycle
gpio.Set(pinNum, 0);
gpio->Set(pinNum, 0);
_Sleep(periodLow);
}
}

View File

@ -122,38 +122,44 @@ int parseArguments(const char* progName, int argc, char* argv[], gpioSetup *setu
int gpioRun(gpioSetup* setup)
{
int status = EXIT_SUCCESS;
FastGpio gpioObj;
FastGpio * gpioObj;
// object setup
if (strcmp(DEVICE_TYPE, "omega") == 0) {
gpioObj = new FastGpioOmega();
} else {
gpioObj = new FastGpioOmega2();
}
char* valString = new char[255];
// Modify here to point to Omega or Omega2 Object.
// object setup
gpioObj.SetVerbosity(setup->verbose == FASTGPIO_VERBOSITY_ALL ? 1 : 0);
gpioObj.SetDebugMode(setup->debug);
gpioObj->SetVerbosity(setup->verbose == FASTGPIO_VERBOSITY_ALL ? 1 : 0);
gpioObj->SetDebugMode(setup->debug);
// object operations
switch (setup->cmd) {
case GPIO_CMD_SET:
gpioObj.SetDirection(setup->pinNumber, 1); // set to output
gpioObj.Set(setup->pinNumber, setup->pinValue);
gpioObj->SetDirection(setup->pinNumber, 1); // set to output
gpioObj->Set(setup->pinNumber, setup->pinValue);
strcpy(valString, (setup->pinValue == 1 ? "1" : "0") );
break;
case GPIO_CMD_READ:
gpioObj.Read(setup->pinNumber, setup->pinValue);
gpioObj->Read(setup->pinNumber, setup->pinValue);
strcpy(valString, (setup->pinValue == 1 ? "1" : "0") );
break;
case GPIO_CMD_SET_DIRECTION:
gpioObj.SetDirection(setup->pinNumber, setup->pinDir); // set pin direction
gpioObj->SetDirection(setup->pinNumber, setup->pinDir); // set pin direction
strcpy(valString, (setup->pinDir == 1 ? "output" : "input") );
break;
case GPIO_CMD_GET_DIRECTION:
gpioObj.GetDirection(setup->pinNumber, setup->pinDir); // find pin direction
gpioObj->GetDirection(setup->pinNumber, setup->pinDir); // find pin direction
strcpy(valString, (setup->pinDir == 1 ? "output" : "input") );
break;
case GPIO_CMD_PULSES:
pulseGpio(&gpioObj,setup->pinNumber,setup->pathPulsesFile,setup->repeats);
pulseGpio(gpioObj,setup->pinNumber,setup->pathPulsesFile,setup->repeats);
break;
default:
@ -172,7 +178,7 @@ int gpioRun(gpioSetup* setup)
// function to run pwm commands
int pwmRun(gpioSetup* setup)
{
{ //Gotta change this to either reference the Omega or Omega 2 object
FastPwm pwmObj;
char* valString = new char[255];

View File

@ -41,23 +41,21 @@ void Module::SetDebugMode (bool input)
int Module::_SetupAddress(unsigned long int blockBaseAddr, unsigned long int blockSize)
{
int m_mfd;
int page_size;
if (debugLevel == 0)
{
if ((m_mfd = open("/dev/mem", O_RDWR)) < 0)
{
return EXIT_FAILURE; // maybe return -1
}
regAddress = (unsigned long*)mmap ( NULL,
blockSize,
regAddress = (unsigned long int*)mmap ( NULL,
1024,
PROT_READ|PROT_WRITE,
MAP_SHARED,
MAP_FILE|MAP_SHARED,
m_mfd,
blockBaseAddr
);
close(m_mfd);
if (regAddress == MAP_FAILED)
{
return EXIT_FAILURE; // maybe return -2
@ -77,7 +75,6 @@ void Module::_WriteReg(unsigned long int registerOffset, unsigned long int value
unsigned long int Module::_ReadReg(unsigned long int registerOffset)
{
unsigned long int value = 0x0;
// read the value
value = *(regAddress + registerOffset);

View File

@ -1,20 +1,16 @@
#!/bin/sh
## define the remote server and package
server="rajiv@build.onion.io"
remotePath="/home/rajiv/OpenWRT-Buildroot/openwrt/dl"
package="fast-gpio"
localPath="../$package"
localPath="/cygdrive/c/Users/Rajiv/Desktop/onion/fast-gpio"
## upload project to remote server
cmd="rsync -va --progress $localPath $server:$remotePath"
echo "$cmd"
echo "rsync command: $cmd"
eval "$cmd"
## create a tar from the file, run the compile
cmd="ssh $server \"cd $remotePath && tar -zcvf $package.tar.gz $package && cd .. && make package/feeds/onion/$package/compile V=99\""
echo "$cmd"
eval "$cmd"
ssh rajiv@build.onion.io "cd /home/rajiv/OpenWRT-Buildroot/openwrt/dl && tar -zcvf fast-gpio.tar.gz fast-gpio/ && cd .. && make package/feeds/onion/fast-gpio/compile V ==99"