From 4537514b5546d3be0b9abda9177f9567e18f7de0 Mon Sep 17 00:00:00 2001 From: ratatatatat Date: Mon, 15 Aug 2016 15:29:18 -0400 Subject: [PATCH] Compatible for Omega and Omega2 --- include/fastgpio.h | 30 +++++------ include/fastgpioomega.h | 34 ++++++++++++ include/fastgpioomega2.h | 67 +++++++++++++++++++++++ include/fastpwm.h | 10 +++- include/main.h | 2 + include/module.h | 2 +- src/fastgpioomega.cpp | 85 +++++++++++++++++++++++++++++ src/fastgpioomega2.cpp | 112 +++++++++++++++++++++++++++++++++++++++ src/fastpwm.cpp | 19 +++++-- src/main.cpp | 28 ++++++---- src/module.cpp | 11 ++-- tools/remote_compile.sh | 10 ++-- 12 files changed, 364 insertions(+), 46 deletions(-) create mode 100644 include/fastgpioomega.h create mode 100644 include/fastgpioomega2.h create mode 100644 src/fastgpioomega.cpp create mode 100644 src/fastgpioomega2.cpp diff --git a/include/fastgpio.h b/include/fastgpio.h index b790ff5..0632b25 100644 --- a/include/fastgpio.h +++ b/include/fastgpio.h @@ -2,32 +2,32 @@ #define _FAST_GPIO_H_ #include +#include +//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; }; diff --git a/include/fastgpioomega.h b/include/fastgpioomega.h new file mode 100644 index 0000000..737cfac --- /dev/null +++ b/include/fastgpioomega.h @@ -0,0 +1,34 @@ +#ifndef _FAST_GPIO_OMEGA_H_ +#define _FAST_GPIO_OMEGA_H_ + +#include + +//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_ \ No newline at end of file diff --git a/include/fastgpioomega2.h b/include/fastgpioomega2.h new file mode 100644 index 0000000..6a6213c --- /dev/null +++ b/include/fastgpioomega2.h @@ -0,0 +1,67 @@ +#ifndef _FAST_GPIO_OMEGA2_H_ +#define _FAST_GPIO_OMEGA2_H_ + +#include + +//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_ \ No newline at end of file diff --git a/include/fastpwm.h b/include/fastpwm.h index 9c3a460..f55a296 100644 --- a/include/fastpwm.h +++ b/include/fastpwm.h @@ -3,7 +3,8 @@ #include #include - +#include +#include #include @@ -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; diff --git a/include/main.h b/include/main.h index 17321df..86d82e7 100644 --- a/include/main.h +++ b/include/main.h @@ -11,6 +11,8 @@ #include #include +#include +#include #define FASTGPIO_VERBOSITY_QUIET (0) #define FASTGPIO_VERBOSITY_NORMAL (1) diff --git a/include/module.h b/include/module.h index d8de84c..77b3700 100644 --- a/include/module.h +++ b/include/module.h @@ -38,7 +38,7 @@ protected: int verbosityLevel; int debugLevel; - volatile unsigned long int *regAddress; + unsigned long int *regAddress; }; #endif // _MODULE_H_ \ No newline at end of file diff --git a/src/fastgpioomega.cpp b/src/fastgpioomega.cpp new file mode 100644 index 0000000..e57a23a --- /dev/null +++ b/src/fastgpioomega.cpp @@ -0,0 +1,85 @@ +#include + +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; +} diff --git a/src/fastgpioomega2.cpp b/src/fastgpioomega2.cpp new file mode 100644 index 0000000..5a168b6 --- /dev/null +++ b/src/fastgpioomega2.cpp @@ -0,0 +1,112 @@ +#include + +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; +} diff --git a/src/fastpwm.cpp b/src/fastpwm.cpp index dec3382..828b3df 100644 --- a/src/fastpwm.cpp +++ b/src/fastpwm.cpp @@ -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); } } diff --git a/src/main.cpp b/src/main.cpp index c858c4f..0439855 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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]; diff --git a/src/module.cpp b/src/module.cpp index 5590846..d4ab1c2 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -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); diff --git a/tools/remote_compile.sh b/tools/remote_compile.sh index f8411b2..d0d8bef 100755 --- a/tools/remote_compile.sh +++ b/tools/remote_compile.sh @@ -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" \ No newline at end of file