From d4f74ab927eb225b96a2480140ba832c018b7e0c Mon Sep 17 00:00:00 2001 From: atomega Date: Sun, 28 Mar 2021 22:24:19 +0100 Subject: [PATCH] I2c Implementation is working, but error management is not clear, must chekc writeBuffer & readBuffer functions --- developpment/Makefile | 11 +- developpment/communication/i2c/$ | 40 ++++++ developpment/communication/i2c/i2c.cpp | 52 ++++++++ developpment/communication/i2c/i2c.hpp | 40 ++++++ developpment/communication/i2c/i2c_linux.hpp | 126 +++++++++++++++++++ developpment/config.h | 1 - developpment/drivers/bh1750/bh1750.cpp | 2 +- developpment/drivers/bh1750/bh1750.h | 6 +- developpment/drivers/pf8574/pf8574lcd.c | 4 +- developpment/drivers/pf8574/pf8574lcd.h | 7 +- developpment/interfacer.cpp | 27 +++- developpment/periferals/i2c/i2c_driver.o | Bin 5280 -> 0 bytes 12 files changed, 294 insertions(+), 22 deletions(-) create mode 100644 developpment/communication/i2c/$ create mode 100644 developpment/communication/i2c/i2c.cpp create mode 100644 developpment/communication/i2c/i2c.hpp create mode 100644 developpment/communication/i2c/i2c_linux.hpp delete mode 100644 developpment/periferals/i2c/i2c_driver.o diff --git a/developpment/Makefile b/developpment/Makefile index 73f5358..71993ff 100644 --- a/developpment/Makefile +++ b/developpment/Makefile @@ -1,11 +1,14 @@ -cpp_src += $(wildcard ./pimpl/*.cpp) cpp_src += $(wildcard *.cpp) -cpp_src += $(wildcard ./management/*.cpp) $(wildcard ./systems/*.cpp) +cpp_src += $(wildcard ./pimpl/*.cpp) +cpp_src += $(wildcard ./communication/i2c/*.cpp) +cpp_src += $(wildcard ./management/*.cpp) +cpp_src += $(wildcard ./drivers/bh1750/*.cpp) +cpp_src += $(wildcard ./systems/*.cpp) c_src += $(wildcard ./algorithms/*.c) +c_src += $(wildcard ./drivers/pf8574/*.c) cpp_obj = $(cpp_src:.cpp=.o) -impl_obj = $(impl_src:.cpp=.o) c_obj = $(c_src:.c=.o) CC = g++ CFLAGS = -Wall -pedantic -li2c @@ -15,7 +18,7 @@ EXEC = runtest all : $(EXEC) -$(EXEC): $(impl_obj) $(cpp_obj) $(c_obj) +$(EXEC): $(cpp_obj) $(c_obj) $(CC) -o $@ $^ $(LDFLAGS) clean: diff --git a/developpment/communication/i2c/$ b/developpment/communication/i2c/$ new file mode 100644 index 0000000..59a1e39 --- /dev/null +++ b/developpment/communication/i2c/$ @@ -0,0 +1,40 @@ +#ifndef _I2C_H_ +#define _I2C_H_ + +#include +#include +#include +#include +#include +#include "../../config.h" + +class I2C +{ + public: + enum errors + { + noError, + writeFailed, + readFailed, + bufferFull, + fasleAddrs, + initFailed + }; + + I2C(); + ~I2C(); + uint8_t readByte(const uint8_t& address, const uint8_t& reg); + uint16_t readWord(const uint8_t& address, const uint8_t& reg); + uint8_t writeByte(const uint8_t& address, const uint8_t& data); + uint8_t writeWord(const uint8_t& address, const uint8_t& reg, const uint8_t& data); + uint8_t writeBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len); + uint8_t readBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len); + int8_t getError(); + + private: + struct i2cImpl; + std::unique_ptr i2cPimpl; + +}; +#endif // _I2C_H_ +#define _I2C_H_ diff --git a/developpment/communication/i2c/i2c.cpp b/developpment/communication/i2c/i2c.cpp new file mode 100644 index 0000000..4f0c6e4 --- /dev/null +++ b/developpment/communication/i2c/i2c.cpp @@ -0,0 +1,52 @@ +#include "i2c.hpp" + +/* +#if PLATFORM == LINUX +#include "i2c_linux.hpp" +#endif + +#if PLATFORM == LINUX +#include "i2c_stm.hpp" +#endif +*/ + +#include "i2c_linux.hpp" + +I2C::I2C():i2cPimpl(new i2cImpl()){} + +I2C::~I2C(){} + +uint8_t I2C::readByte(const uint8_t& address, const uint8_t& reg) +{ + return i2cPimpl->readByte(address,reg); +} + +uint16_t I2C::readWord(const uint8_t& address, const uint8_t& reg) +{ + return i2cPimpl->readWord(address,reg); +} + +uint8_t I2C::writeByte(const uint8_t& address, const uint8_t& data) +{ + return i2cPimpl->writeByte(address,data); +} + +uint8_t I2C::writeWord(const uint8_t& address, const uint8_t& reg, const uint8_t& data) +{ + return i2cPimpl->writeWord(address,reg,data); +} + +uint8_t I2C::writeBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len) +{ + return i2cPimpl->writeBuffer(address,buffer,len); +} + +uint8_t I2C::readBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len) +{ + return i2cPimpl->readBuffer(address,buffer,len); +} + +int8_t I2C::getError() +{ + return i2cPimpl->getError(); +} diff --git a/developpment/communication/i2c/i2c.hpp b/developpment/communication/i2c/i2c.hpp new file mode 100644 index 0000000..be2c3e1 --- /dev/null +++ b/developpment/communication/i2c/i2c.hpp @@ -0,0 +1,40 @@ +#ifndef _I2C_H_ +#define _I2C_H_ + +#include +#include +#include +#include +#include +#include "../../config.h" + +class I2C +{ + public: + enum errors + { + noError, + writeFailed, + readFailed, + bufferFull, + falseAddrs, + initFailed + }; + + I2C(); + ~I2C(); + uint8_t readByte(const uint8_t& address, const uint8_t& reg); + uint16_t readWord(const uint8_t& address, const uint8_t& reg); + uint8_t writeByte(const uint8_t& address, const uint8_t& data); + uint8_t writeWord(const uint8_t& address, const uint8_t& reg, const uint8_t& data); + uint8_t writeBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len); + uint8_t readBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len); + int8_t getError(); + + private: + struct i2cImpl; + std::unique_ptr i2cPimpl; + +}; +#endif // _I2C_H_ +#define _I2C_H_ diff --git a/developpment/communication/i2c/i2c_linux.hpp b/developpment/communication/i2c/i2c_linux.hpp new file mode 100644 index 0000000..de31a7d --- /dev/null +++ b/developpment/communication/i2c/i2c_linux.hpp @@ -0,0 +1,126 @@ +#ifndef _I2C_LINUX_H_ +#define _I2C_LINUX_H_ +#include "i2c.hpp" + +#include +#include +#include +#include +#include + +struct I2C::i2cImpl +{ + int16_t deviceDescriptor; + uint8_t device_address; + uint8_t send_buffer[32]; + uint8_t recieve_buffer[32]; + uint8_t blocks; + uint8_t channel; + uint8_t mode; + int8_t error; + + i2cImpl() + { + char filename[20]; + errors currentError = noError; + snprintf(filename, 19, "/dev/i2c-%d", 1); + + deviceDescriptor = open(filename, O_RDWR); + if (deviceDescriptor < 0) { + error = initFailed; + } + } + uint8_t readByte(const uint8_t& address, const uint8_t& reg) + { + if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0) + { + error = falseAddrs; + } + + writeByte(address,reg); //Initiate a write to indicate the desired register to read + + if (read(deviceDescriptor, recieve_buffer, 1) != 1) // An then initare a read request of 1 byte + { + error = readFailed; + } + return recieve_buffer[0] ; + } + + uint16_t readWord(const uint8_t& address, const uint8_t& reg) + { + uint16_t result = 0 ; + if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0) + { + error = falseAddrs; + } + + writeByte(address,reg); //Initiate a write to indicate the desired register to read + + if (read(deviceDescriptor, recieve_buffer, 2) != 2) // An then initare a read request of 2 bytes + { + error = readFailed; + } + result = (recieve_buffer[0] << 8) + recieve_buffer[1] ; + return result ; + } + + uint8_t writeByte(const uint8_t& address, const uint8_t& data) + { + if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0) + { + error = falseAddrs; + } + + send_buffer[0] = data; + + if ((write(deviceDescriptor, send_buffer, 1)) != 1) + { + error = writeFailed; + } + return 0; + } + + uint8_t writeWord(const uint8_t& address, const uint8_t& reg, const uint8_t& data) + { + if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0) + { + error = falseAddrs; + } + + send_buffer[0] = reg; + send_buffer[1] = data; + + if ((write(deviceDescriptor, send_buffer, 2)) != 2) + { + error = writeFailed; + } + return 0; + } + + uint8_t writeBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len) + { + if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0) + { + error = falseAddrs; + } + + send_buffer[0] = buffer; + + if ((write(deviceDescriptor, send_buffer, len)) != len) + { + error = writeFailed; + } + return 0; + } + + uint8_t readBuffer(const uint8_t& address, const uint8_t& buffer, uint8_t len) + { + return 0; + } + + int8_t getError() + { + return 0; + } +}; +#endif // _I2C_LINUX_H_ diff --git a/developpment/config.h b/developpment/config.h index 8648b52..19d3590 100644 --- a/developpment/config.h +++ b/developpment/config.h @@ -1,7 +1,6 @@ #define STM 0 #define LINUX 1 - #define PLATFORM STM diff --git a/developpment/drivers/bh1750/bh1750.cpp b/developpment/drivers/bh1750/bh1750.cpp index 83fb57c..765227e 100644 --- a/developpment/drivers/bh1750/bh1750.cpp +++ b/developpment/drivers/bh1750/bh1750.cpp @@ -2,7 +2,7 @@ -Bh1750::Bh1750(i2c_ch1_pImpL *i2c) +Bh1750::Bh1750(I2C *i2c) { i2c_bh1750 = i2c; currentMode = 0; // no mode selected diff --git a/developpment/drivers/bh1750/bh1750.h b/developpment/drivers/bh1750/bh1750.h index ffab7c3..2ad6be9 100644 --- a/developpment/drivers/bh1750/bh1750.h +++ b/developpment/drivers/bh1750/bh1750.h @@ -5,7 +5,7 @@ #include #include #include -#include "../../periferals/i2c/i2c_ch1_pImpL.hpp" +#include "../../communication/i2c/i2c.hpp" //Start measurement at 4lx resolution. Time typically 16ms. @@ -33,14 +33,14 @@ class Bh1750 { public: - Bh1750(i2c_ch1_pImpL *i2c); + Bh1750(I2C *i2c); uint8_t sleep(); // To be testes uint8_t wake(); // To be tested uint8_t reset(); // To be tested float oneShot(uint8_t mode); // ok float continious(uint8_t mode, uint8_t delayMs); // IMplment delay or make a delay class ??? private: - i2c_ch1_pImpL* i2c_bh1750; + I2C* i2c_bh1750; uint8_t high; uint8_t low; uint8_t currentMode; diff --git a/developpment/drivers/pf8574/pf8574lcd.c b/developpment/drivers/pf8574/pf8574lcd.c index 105af1a..1810eb4 100755 --- a/developpment/drivers/pf8574/pf8574lcd.c +++ b/developpment/drivers/pf8574/pf8574lcd.c @@ -12,10 +12,10 @@ #include "pf8574lcd.h" -i2c_ch1_pImpL *i2c_pf8574; +I2C *i2c_pf8574; // Fonction pour initialiser l'écran vide en mode 4 bits -int lcd_init(i2c_ch1_pImpL* i2c) +int lcd_init(I2C* i2c) { i2c_pf8574 = i2c; lcd_write(0x03,CMD_MODE); // Mise en mode 4 bit avec 4 essai conssecutif diff --git a/developpment/drivers/pf8574/pf8574lcd.h b/developpment/drivers/pf8574/pf8574lcd.h index ae5e0e8..bc6dbd0 100755 --- a/developpment/drivers/pf8574/pf8574lcd.h +++ b/developpment/drivers/pf8574/pf8574lcd.h @@ -7,9 +7,7 @@ #include #include #include -#include "../../periferals/i2c/i2c_ch1_pImpL.hpp" - -//#include "i2c_ch1_pImpL.cpp" +#include "../../communication/i2c/i2c.hpp" // commandes #define LCD_CLEARDISPLAY 0x01 @@ -70,11 +68,10 @@ #define TOTAL_CHAR_CAP 20 -int lcd_init(i2c_ch1_pImpL* i2c); +int lcd_init(I2C *i2c); void lcd_write_char( unsigned char charvalue); void lcd_display_string(unsigned char line, unsigned char pos, char* charvalue, unsigned char lenght); void ldc_pulse_En(unsigned char data); void lcd_write(unsigned char cmd, unsigned char mode); void lcd_write_4bits(unsigned char data); - diff --git a/developpment/interfacer.cpp b/developpment/interfacer.cpp index c844927..544dea1 100644 --- a/developpment/interfacer.cpp +++ b/developpment/interfacer.cpp @@ -13,23 +13,23 @@ #include #include "./management/errorHandling.h" #include "./management/commandManager.h" -//#include "./drivers/bh1750/bh1750.h" -//#include "./drivers/pf8574/pf8574lcd.h" +#include "./drivers/bh1750/bh1750.h" +#include "./drivers/pf8574/pf8574lcd.h" #include "./algorithms/bitgestion.h" -//#include "./periferals/i2c/i2c.hpp" -//#include "./pimpl/implementation.hpp" #include "./pimpl/interface.hpp" - +#include "./communication/i2c/i2c.hpp" ErrorHandler errorHandle; CommandManager commander; Device pimplTest; +I2C i2c; + +char char_array[TOTAL_CHAR_CAP]; //i2c_ch1_pImpL i2c(1, &errorHandle); /* -char char_array[TOTAL_CHAR_CAP]; int freq = 0; int initPlatform() @@ -106,6 +106,7 @@ int main(int argc, char *argv[]) */ int initPlatform() { + lcd_init(&i2c); return 0; } @@ -114,12 +115,26 @@ void dummy() pimplTest.printNumber(3); } +void bh1750_test() +{ + Bh1750 lightSens(&i2c); + while(1) + { +// std::cout << "value "<< lightSens.continious(BH1750_CONTINUOUS_HIGH_RES_MODE_1,1) << " Lux" <gG2-R-aDo`m#>O&tQ%sDf6 zCfB1lN_pT~v*(`sefRsjKeOJS4~;%;7>2~nkZ!rpL_XUUN?Q6t=$2iQ7dh4QkKFuk zR=Ss-Sutftvn#)&*=+)?j;2_*n(Z>Oa@D-n+>@XC@IUM8|7y2JRx;>kWp0^Vthwce z`Il7_`t#;{%fvP7*0RX>hr31IKAXSRJS*3lp{_ZX$g}ym<&0)y0D8L30~$9Z@=e>+ z$t{hn$g6+;7|~0oO4d>s=aWZ1t;lC5^2sBgRuvcL??j)uWf9K@@Du0pyQh$gb$3<8 zzWpX-H?`Q4d8ydn(qd&j-q3g)d!9bcTVWhI^Sur6Ft(ia(u!fCuCN`)W1xnHHMa*g zvvFLlBHNF`z7@v>e|oJ%)e3XMHHvGM-74~As#a&o)%rhby*`5PIKFhf79m@V<3epW ztJfd2`mkPe8c*tFc3|x~nyu0i`qS5s{jSeZuM@D}7smyE)&u-SHnAtP{q>-l)gDr_ zHf!^J_9=XBR<}El-HGFSNZoE|akFmo8c*t$+GlOx^Ei(U^X@8hY}sV*Wm0=D^MBlX z?KrD>oYgj*)pnd!?%j8+k!7A4-X&ZU<`JLS)?J&|at$?E(`uQ_%Wu4ImbBWjmJ1q> zYuS!H*p}~T=G7k5<6)nB@KNhF1^dZ82!9g~@E6&<)ofVntJyx@tLv*A+lRX<9J5~h zfOUDt`cYVuhn;PGpSEguA@>09m2Dz>c|N<%YfY^6atpYr>eh*|PQ;gw>!_WH^>uZL z#GvkvR_L>C%X}x`ZjSZz_s_wSck!U#>-Ov`$#lh;EPG~9HLEqRVjeMJ&-{k@lW9Nj zE9XR>P2_dYai>g7^<6L0yDz&`_X97PbblOkO3APseckxQ{QTvY8S>}ta}L`Qo}KJu zMA`v;S&=T_3po+Kd+wls%=PG&2+>hI(wMpNpm;;4F1*@n3iF(a1J{~bS zR)C)4Ig`E^wAa!vTeh?s+l=j*-MKd7%f?r-?Z#J)y_rtqYg@jlSCbAjU+CxL-y2pZ zyW#CGlEt6ivY4T_thDeOf!11Nk7D#}K+bU4BA!d>=j6MP(JCM6mwA#-h zZ|fZWcXiI%;XbrWb^oGQFSxaubd|*Z;dn4wJY4fV*Sq95Jlm~S8bN*94I@IwIzhE=JN0wXV84H^ zQmuQ^<$(8Lej{)K-?eGocgp@x!KxM8sgz(jihs*tL3*&NgIKjI&A!4u>zI#W6M#?h-wT(2%A=8{@%%Uv1bZzt=a6wI3`FT}*1?1l|lcn_X&D z?J4YqviG1lCpbqAU`uyC7X7c$*)1|f{G7)}XQ2YuJeqSTP@0-M;$O5rn-^L2a_8nYJyXA&6HBbeYxG8+)bz8D~;2qJC*6qP%!6pfu4&A`Y3DII`Fv-&(V;Hy!=J%2h<1<&Zk~0o_&gA%Z2T# zW{U&##fD`M^>w{iMtSU)HRd>6g^l)JQGG(JH-Nsb*RK7xq3fND^lC415N!f_@m%uQ z-XQvVzW1<6cyBRZObPXv@AuVMoW~UiV;c~Keey9py1f#9cjJ88<2ZH5C-E>3=FN5$ z_PU~O_(>U`^9sX3d|YQxTB8oz&+s6&T1fCgJsgFk#d^fJ#uR=8y(LV<_LpzrVSFe- f7;Bs>s9fJ6@;7Z_Y>z)H>Uw1i{F)O~i}n5sdO*s%