diff --git a/developpment/interfacer.cpp b/developpment/interfacer.cpp index 49f33eb..1103570 100644 --- a/developpment/interfacer.cpp +++ b/developpment/interfacer.cpp @@ -45,7 +45,10 @@ int initPlatform() { lcd_init(&i2c); Gpio gpio; - + //gpio.setFunction(17,Gpio::gpio); + //gpio.setMode(17,Gpio::output); + //gpio.writePin(17,1); + //gpio.pinUninitiate(17); return 0; } @@ -53,12 +56,21 @@ int initPlatform() void pca9555_test() { uint8_t pinState = 0; + uint8_t bank = 0; + uint8_t pinNo = 0; + uint8_t inOut = 0; + Pca9555 gpioExpander(&i2c); gpioExpander.setAllOut(); gpioExpander.setAllHigh(); usleep(1000000); - gpioExpander.writePin(1,7,0); - gpioExpander.setPin(0,3,1); + bank = 1; + pinNo = 7; + gpioExpander.writePin(bank,pinNo,0); + bank = 0; + pinNo = 3; + inOut = 1; //input + gpioExpander.setPin(bank,pinNo,inOut); while(1) { @@ -89,7 +101,6 @@ void pca9685_test() - void pca9685_motor() { Pca9685 pwmGenarator(&i2c); diff --git a/developpment/periferals/gpio/gpio.cpp b/developpment/periferals/gpio/gpio.cpp index dbd24b2..c31eed3 100644 --- a/developpment/periferals/gpio/gpio.cpp +++ b/developpment/periferals/gpio/gpio.cpp @@ -1,88 +1,102 @@ #include "gpio.hpp" #include "../../systems/systemCall.h" -#include <stdint.h> +#include <fstream> + +#define PIN_COUNT 26 -#define PIN_COUNT 27 class Gpio::gpioImpl { public: uint8_t pinCount = PIN_COUNT; - + char buffer[50]; uint8_t currentPin = 0; + int ck = 0; - enum functionality - { - undefined, - gpio, - i2c, - spi, - pwm, - pcm, - uart, - eeprom, - clk - }; - functionality pins[PIN_COUNT + 1]; + pinDefinition pins[PIN_COUNT + 1]; gpioImpl() { - printf("implementation of Gpio Sucsessfull\n"); - pins[1] = eeprom; - pins[2] = i2c; - pins[3] = i2c; - pins[4] = clk; - pins[5] = gpio; - pins[6] = gpio; - pins[7] = spi; - pins[8] = spi; - pins[9] = spi; - pins[10] = spi; - pins[11] = spi; - pins[12] = pwm; - pins[13] = pwm; - pins[14] = uart; - pins[15] = uart; - pins[16] = gpio; - pins[17] = gpio; - pins[18] = pcm; - pins[19] = pcm; - pins[20] = pcm; - pins[21] = pcm; - pins[22] = gpio; - pins[23] = gpio; - pins[24] = gpio; - pins[25] = gpio; - pins[26] = gpio; - pins[27] = gpio; + checkCurrentPins(); + } + + void setFunction(uint8_t gpioNo, Gpio::pinFunction function) + { + pinInitiate(gpioNo); - for(currentPin = 0; currentPin <= pinCount; currentPin ++) + switch(pins[gpioNo].function) { - printf("Pin %d %d\n",currentPin, pins[currentPin]); + case passive: + pins[gpioNo].function = function; + break; + case gpio: + pins[gpioNo].function = function; + break; + case i2c: + pins[gpioNo].function = function; + break; + case spi: + pins[gpioNo].function = function; + break; + case pwm: + pins[gpioNo].function = function; + break; + case pcm: + pins[gpioNo].function = function; + break; + case uart: + pins[gpioNo].function = function; + break; + case eeprom: + pins[gpioNo].function = function; + break; + case clk: + pins[gpioNo].function = function; + break; } - - } - + void setMode(uint8_t gpioNo, Gpio::pinMode mode) { - + switch(mode) + { + case input: + sprintf(buffer, "echo \"in\" > /sys/class/gpio/gpio%d/direction",gpioNo); + pins[gpioNo].mode = mode; + system(buffer); + pinPrintInfo(gpioNo); + break; + case output: + sprintf(buffer, "echo \"out\" > /sys/class/gpio/gpio%d/direction",gpioNo); + pins[gpioNo].mode = mode; + system(buffer); + pinPrintInfo(gpioNo); + break; + case analog: + std::cout << "No analog pins are awailable for raspberry" << std::endl; + break; + case alternate: + default : + throwError(notValidMode); + break; + + } } - + void setOutputState(uint8_t gpioNo, Gpio::pinState state) - { - + { + std::cout << "This version of the code doesn't allow pin output Stage configuration for the raspberry" << std::endl; } void setPullUpDonw(uint8_t gpioNo, Gpio::pinPullUpDown resistance) { - + std::cout << "This version of the code doesn't allow Pull up or Pull donw configurations for the raspberry" << std::endl; } void setSpeed(uint8_t gpioNo, Gpio::pinSpeed speed) { - + std::cout << "This version of the code doesn't allow pin speed changes for the raspberry" << std::endl; } void config(uint8_t gpioNo, Gpio::pinMode mode, Gpio::pinState state, Gpio::pinPullUpDown resistance, Gpio::pinSpeed speed) @@ -98,7 +112,20 @@ class Gpio::gpioImpl void writePin(uint8_t gpioNo, uint8_t value) { - + if(value == 0) + { + sprintf(buffer, "echo \"%d\" > /sys/class/gpio/gpio%d/value",value,gpioNo); + system(buffer); + } + else if (value == 1) + { + sprintf(buffer, "echo \"%d\" > /sys/class/gpio/gpio%d/value",value,gpioNo); + system(buffer); + } + else + { + throwError(notValidOut); + } } @@ -113,19 +140,186 @@ class Gpio::gpioImpl } - void pinBlock(uint8_t gpioNo) + void pinInitiate(uint8_t gpioNo) { - + if(gpioNo < PIN_COUNT) + { + sprintf(buffer,"[ -d /sys/class/gpio/gpio%d ]",gpioNo); // retunrs null if directory exists + ck = system(buffer); + if( ck != 0) + { + sprintf(buffer, "echo \"%d\" > /sys/class/gpio/export",gpioNo); + ck = system(buffer); + } + else + { + std::cout << "Pin already initiated"; + pinPrintInfo(gpioNo); + } + } + else + { + throwError(pinOutOfRange); + } } - void pinUnblock(uint8_t gpioNo) + void pinUninitiate(uint8_t gpioNo) { - + if(gpioNo < PIN_COUNT) + { + sprintf(buffer,"[ -d /sys/class/gpio/gpio%d ]",gpioNo); // retunrs null if directory exists + ck = system(buffer); + if( ck == 0) + { + sprintf(buffer, "echo \"%d\" > /sys/class/gpio/unexport",gpioNo); + system(buffer); + } + else + { + throwError(pinNotDeclared); + } + } + else + { + throwError(pinOutOfRange); + } } - void throwError(Gpio::errors errNo) + void checkCurrentPins() + { + uint8_t i = 0; + for( i = 0; i < PIN_COUNT; i++) + { + sprintf(buffer,"[ -d /sys/class/gpio/gpio%d ]",i); // retunrs null if directory exists + ck = system(buffer); + if( ck == 0) + { + std::cout << "Pin: "<< unsigned(i) << " Exists "; + } + } + } + + void pinPrintInfo(uint8_t gpioNo) { + std::cout << "Pin No: " << unsigned(gpioNo); + switch(pins[gpioNo].mode) + { + + case undefined: + std::cout << "| Mode: Undefined\t"; + break; + case input: + std::cout << "| Mode: Input\t"; + break; + case output: + std::cout << "| Mode: Output\t"; + break; + case analog: + std::cout << "| Mode: Analog\t"; + break; + case alternate: + std::cout << "| Mode: Alternate\t"; + break; + + } + + switch(pins[gpioNo].state) + { + + case floating: + std::cout << "| State: Floating "; + break; + case pushPull: + std::cout << "| State: Push-Pull "; + break; + case openDrain: + std::cout << "| State: Open Drain "; + break; + + } + + switch(pins[gpioNo].pud) + { + + case none: + std::cout << "| Pud: None "; + break; + case pullUp: + std::cout << "| Pud: Pull-up "; + break; + case pullDown: + std::cout << "| Pud: Pull-down "; + break; + + } + + switch(pins[gpioNo].speed) + { + + case slow: + std::cout << "| Speed: Slow "; + break; + case normal: + std::cout << "| Speed: Normal "; + break; + case fast: + std::cout << "| Speed: Fast "; + break; + case veryFast: + std::cout << "| Speed: Very Fast "; + break; + + } + switch(pins[gpioNo].interrupt) + { + + case disabled: + std::cout << "| Interrupt: No "; + break; + case enabled: + std::cout << "| Interrupt: Yes "; + break; + + } + + switch(pins[gpioNo].function) + { + case passive: + std::cout << "| Function: NONE"; + break; + case gpio: + std::cout << "| Function: GPIO"; + break; + case i2c: + std::cout << "| Function: I2C"; + break; + case spi: + std::cout << "| Function: SPI"; + break; + case pwm: + std::cout << "| Function: PWM"; + break; + case pcm: + std::cout << "| Function: PCM"; + break; + case uart: + std::cout << "| Function: UART"; + break; + case eeprom: + std::cout << "| Function: EEPROM"; + break; + case clk: + std::cout << "| Function: CLK"; + break; + + } + std::cout << "|" << std::endl; + } + + void throwError(Gpio::errors errNo) + { + std::cout << "Error >>GPIO.C<< No >>"<< errNo <<"<<"<< std::endl; } }; @@ -133,6 +327,10 @@ class Gpio::gpioImpl Gpio::Gpio():gpioPimpl(new gpioImpl()){} Gpio::~Gpio(){} +void Gpio::setFunction(uint8_t gpioNo, Gpio::pinFunction function) +{ + return gpioPimpl->setFunction(gpioNo, function); +} void Gpio::setMode(uint8_t gpioNo, Gpio::pinMode mode) { @@ -179,14 +377,24 @@ void Gpio::writeRange(uint8_t start, uint8_t stop, uint16_t value) return gpioPimpl->writeRange(start,stop,value); } -void Gpio::pinBlock(uint8_t gpioNo) +void Gpio::pinInitiate(uint8_t gpioNo) +{ + return gpioPimpl->pinInitiate(gpioNo); +} + +void Gpio::pinUninitiate(uint8_t gpioNo) +{ + return gpioPimpl->pinUninitiate(gpioNo); +} + +void Gpio::checkCurrentPins() { - return gpioPimpl->pinBlock(gpioNo); + return gpioPimpl->checkCurrentPins(); } -void Gpio::pinUnblock(uint8_t gpioNo) +void Gpio::pinPrintInfo(uint8_t gpioNo) { - return gpioPimpl->pinUnblock(gpioNo); + return gpioPimpl->pinPrintInfo(gpioNo); } void Gpio::throwError(Gpio::errors error) diff --git a/developpment/periferals/gpio/gpio.hpp b/developpment/periferals/gpio/gpio.hpp index 33e3255..78c53ab 100644 --- a/developpment/periferals/gpio/gpio.hpp +++ b/developpment/periferals/gpio/gpio.hpp @@ -13,21 +13,23 @@ class Gpio public: enum pinMode { + undefined, input, - output + output, + analog, + alternate }; enum pinState { - pushPull, - openDrain, floating, - analog + pushPull, + openDrain }; enum pinPullUpDown { - none, + none, pullUp, pullDown }; @@ -39,9 +41,42 @@ class Gpio fast, veryFast }; + + enum pinInterrupt + { + disabled, + enabled + }; + + enum pinFunction + { + passive, + gpio, + i2c, + spi, + pwm, + pcm, + uart, + eeprom, + clk + }; + + struct pinDefinition + { + uint8_t no; + pinMode mode; + pinState state; + pinPullUpDown pud; + pinSpeed speed; + pinInterrupt interrupt; + pinFunction function; + }; enum errors { + notValidMode, + notValidOut, + pinOutOfRange, pinNotDeclared, pinNotReachable, pinNoPullUpDown, @@ -55,6 +90,7 @@ class Gpio Gpio(); ~Gpio(); + void setFunction(uint8_t gpioNo, Gpio::pinFunction function); void setMode(uint8_t gpioNo, pinMode mode); void setOutputState(uint8_t gpioNo, pinState state); void setPullUpDonw(uint8_t gpioNo, pinPullUpDown resistance); @@ -67,8 +103,11 @@ class Gpio uint16_t readRange(uint8_t start, uint8_t stop); void writeRange(uint8_t start, uint8_t stop, uint16_t value); - void pinBlock(uint8_t gpioNo); - void pinUnblock(uint8_t gpioNo); + void pinInitiate(uint8_t gpioNo); + void pinUninitiate(uint8_t gpioNo); + + void checkCurrentPins(); + void pinPrintInfo(uint8_t gpioNo); private: void throwError(errors errNo); diff --git a/developpment/periferals/gpio/rpiPinMap.hpp b/developpment/periferals/gpio/rpiPinMap.hpp new file mode 100644 index 0000000..b5cbb4a --- /dev/null +++ b/developpment/periferals/gpio/rpiPinMap.hpp @@ -0,0 +1,216 @@ + +pins[0].no = 27; +pins[0].mode = alternate; +pins[0].state = floating; +pins[0].pud = none; +pins[0].speed = normal; +pins[0].interrupt = disabled; +pins[0].function = eeprom; + +pins[1].no = 28; +pins[1].mode = alternate; +pins[1].state = openDrain; +pins[1].pud = pullUp; +pins[1].speed = normal; +pins[1].interrupt = disabled; +pins[1].function = i2c; + +pins[2].no = 3; +pins[2].mode = alternate; +pins[2].state = openDrain; +pins[2].pud = pullUp; +pins[2].speed = normal; +pins[2].interrupt = disabled; +pins[2].function = i2c; + +pins[3].no = 5; +pins[3].mode = alternate; +pins[3].state = openDrain; +pins[3].pud = none; +pins[3].speed = fast; +pins[3].interrupt = disabled; +pins[3].function = clk; + +pins[4].no = 7; +pins[4].mode = output; +pins[4].state = pushPull; +pins[4].pud = none; +pins[4].speed = normal; +pins[4].interrupt = disabled; +pins[4].function = gpio; + +pins[5].no = 29; +pins[5].mode = output; +pins[5].state = pushPull; +pins[5].pud = none; +pins[5].speed = normal; +pins[5].interrupt = disabled; +pins[5].function = gpio; + +pins[6].no = 31; +pins[6].mode = alternate; +pins[6].state = openDrain; +pins[6].pud = none; +pins[6].speed = normal; +pins[6].interrupt = disabled; +pins[6].function = spi; + +pins[7].no = 26; +pins[7].mode = alternate; +pins[7].state = openDrain; +pins[7].pud = none; +pins[7].speed = normal; +pins[7].interrupt = disabled; +pins[7].function = spi; + +pins[8].no = 24; +pins[8].mode = alternate; +pins[8].state = openDrain; +pins[8].pud = none; +pins[8].speed = normal; +pins[8].interrupt = disabled; +pins[8].function = spi; + +pins[9].no = 21; +pins[9].mode = alternate; +pins[9].state = openDrain; +pins[9].pud = none; +pins[9].speed = normal; +pins[9].interrupt = disabled; +pins[9].function = spi; + +pins[10].no = 19; +pins[10].mode = alternate; +pins[10].state = openDrain; +pins[10].pud = none; +pins[10].speed = normal; +pins[10].interrupt = disabled; +pins[10].function = spi; + +pins[11].no = 23; +pins[11].mode = alternate; +pins[11].state = openDrain; +pins[11].pud = none; +pins[11].speed = normal; +pins[11].interrupt = disabled; +pins[11].function = pwm; + +pins[12].no = 32; +pins[12].mode = alternate; +pins[12].state = openDrain; +pins[12].pud = none; +pins[12].speed = normal; +pins[12].interrupt = disabled; +pins[12].function = pwm; + +pins[13].no = 33; +pins[13].mode = alternate; +pins[13].state = openDrain; +pins[13].pud = none; +pins[13].speed = normal; +pins[13].interrupt = disabled; +pins[13].function = uart; + +pins[14].no = 8; +pins[14].mode = alternate; +pins[14].state = openDrain; +pins[14].pud = none; +pins[14].speed = normal; +pins[14].interrupt = disabled; +pins[14].function = uart; + +pins[15].no = 10; +pins[15].mode = output; +pins[15].state = pushPull; +pins[15].pud = none; +pins[15].speed = normal; +pins[15].interrupt = disabled; +pins[15].function = gpio; + +pins[16].no = 36; +pins[16].mode = output; +pins[16].state = pushPull; +pins[16].pud = none; +pins[16].speed = normal; +pins[16].interrupt = disabled; +pins[16].function = gpio; + +pins[17].no = 11; +pins[17].mode = alternate; +pins[17].state = openDrain; +pins[17].pud = none; +pins[17].speed = normal; +pins[17].interrupt = disabled; +pins[17].function = pcm; + +pins[18].no = 12; +pins[18].mode = alternate; +pins[18].state = openDrain; +pins[18].pud = none; +pins[18].speed = normal; +pins[18].interrupt = disabled; +pins[18].function = pcm; + +pins[19].no = 35; +pins[19].mode = alternate; +pins[19].state = openDrain; +pins[19].pud = none; +pins[19].speed = normal; +pins[19].interrupt = disabled; +pins[19].function = pcm; + +pins[20].no = 38; +pins[20].mode = alternate; +pins[20].state = openDrain; +pins[20].pud = none; +pins[20].speed = normal; +pins[20].interrupt = disabled; +pins[20].function = pcm; + +pins[21].no = 40; +pins[21].mode = output; +pins[21].state = pushPull; +pins[21].pud = none; +pins[21].speed = normal; +pins[21].interrupt = disabled; +pins[21].function = gpio; + +pins[22].no = 15; +pins[22].mode = output; +pins[22].state = pushPull; +pins[22].pud = none; +pins[22].speed = normal; +pins[22].interrupt = disabled; +pins[22].function = gpio; + +pins[23].no = 16; +pins[23].mode = output; +pins[23].state = pushPull; +pins[23].pud = none; +pins[23].speed = normal; +pins[23].interrupt = disabled; +pins[23].function = gpio; + +pins[24].no = 18; +pins[24].mode = output; +pins[24].state = pushPull; +pins[24].pud = none; +pins[24].speed = normal; +pins[24].interrupt = disabled; +pins[24].function = gpio; + +pins[25].no = 22; +pins[25].mode = output; +pins[25].state = pushPull; +pins[25].pud = none; +pins[25].speed = normal; +pins[25].interrupt = disabled; +pins[25].function = gpio; + +pins[26].no = 37; +pins[26].mode = output; +pins[26].state = pushPull; +pins[26].pud = none; +pins[26].speed = normal; +pins[26].interrupt = disabled; +pins[26].function = gpio; diff --git a/developpment/runtest b/developpment/runtest index 781a2c3..0ddd734 100755 Binary files a/developpment/runtest and b/developpment/runtest differ