diff --git a/Makefile b/Makefile index acf4a63..85a953d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,8 @@ cpp_src += $(wildcard *.cpp) cpp_src += $(wildcard ./utils/*.cpp) cpp_src += $(wildcard ./interfaces/*.cpp) +cpp_src += $(wildcard ./drivers/*.cpp) + #cpp_src += $(wildcard ./communication/i2c/*.cpp) #c_src += $(wildcard ./algorithms/*.c) diff --git a/bsl/csl/PC/peripherals/i2cDummy.hpp b/bsl/csl/PC/peripherals/i2cDummy.hpp index e74493a..b3fcd0d 100644 --- a/bsl/csl/PC/peripherals/i2cDummy.hpp +++ b/bsl/csl/PC/peripherals/i2cDummy.hpp @@ -31,7 +31,10 @@ struct I2CDummy : I2C void writeBufferImpl(const uint8_t& address, const uint8_t* buffer, uint8_t len) { - std::cout << "I2C writeBuffer" << std::endl; + std::cout << "I2C writeBuffer to address: " << +address << std::endl; + for(uint8_t i = 0; i < len; i++) { + std::cout << +buffer[i] << std::endl; + } } void readBufferImpl(const uint8_t& address, const uint8_t* buffer, uint8_t len) diff --git a/drivers/MCP4725/mcp4725.hpp b/drivers/MCP4725/mcp4725.hpp new file mode 100644 index 0000000..0808315 --- /dev/null +++ b/drivers/MCP4725/mcp4725.hpp @@ -0,0 +1,121 @@ +#ifndef _MCP4725_HPP_ +#define _MCP4725_HPP_ +#include +#include +#include +#include "../../interfaces/i2c.hpp" + +template +class MCP4725 +{ + public: + //typedef std::function i2c_write_n_t; + //typedef std::function i2c_write_n_t; + //typedef void(*i2c_write_n_t)(const uint8_t&, const uint8_t*, const uint8_t&); + + //typedef std::functioni2c_write_n_t; + // address list + enum i2c_addr + { + addr_0x60 = 0b01100000, + addr_0x61 = addr_0x60 + 1, + addr_0x62 = addr_0x60 + 2, + addr_0x63 = addr_0x60 + 3, + addr_0x64 = addr_0x60 + 4, + addr_0x65 = addr_0x60 + 5, + addr_0x66 = addr_0x60 + 6, + addr_0x67 = addr_0x60 + 7 + }; + + // power down impedance modes + enum pwrd_md + { + normal = 0x04, + ohm_1k = 0x01, + ohm_100k = 0x02, + ohm_500k = 0x03 + }; + + //using std::functional i2c_read_n_t; + //using i2c_write_n_t = std::functional; + + /** + * @brief Constructor of mcp4725 dac + * + * @param power_down_mode pwrd_md power down mode + * @param address i2c_addr i2c address of the dac + * @param i2c_write i2c_write_n_t callback for i2c writ + */ + + MCP4725(I2C& i2c) : + power_down_mode(pwrd_md::normal), + address(addr_0x60), + i2c(i2c), + dac_value(0), + eeprom_value(0) + { + uint8_t temp[3]; + this->dac_value = dac_value; + + temp[0] = cmd_write_dac | (power_down_mode << 1); + temp[1] = static_cast(dac_value >> 4); + temp[2] = static_cast(dac_value << 4); + + + i2c.writeBuffer(address, temp, 3); + } + + // ~MCP4725(); + + + + void operator=(uint16_t dac_value) + { + uint8_t temp[3]; + this->dac_value = dac_value; + + temp[0] = cmd_write_dac | (power_down_mode << 1); + temp[1] = static_cast(dac_value >> 4); + temp[2] = static_cast(dac_value << 4); + + i2c.writeBuffer(address, temp, 3); + } + + void operator==(uint16_t dac_and_eeprom_value) + { + uint8_t temp[6]; + dac_value = dac_and_eeprom_value; + eeprom_value = dac_value; + + temp[0] = cmd_write_dac_and_eeprom | (power_down_mode << 1); + temp[1] = static_cast(dac_and_eeprom_value >> 4); + temp[2] = static_cast(dac_and_eeprom_value << 4); + temp[3] = temp[0]; + temp[4] = temp[1]; + temp[5] = temp[2]; + + i2c.writeBuffer(address, temp, 6); + } + + //void set_power + + + private: + + enum commands_t + { + cmd_fast_mode = 0x00, + cmd_write_dac = 0x40, + cmd_write_dac_and_eeprom = 0x60 + }; + + I2C& i2c; + + i2c_addr address; + pwrd_md power_down_mode; + uint16_t dac_value; + uint16_t eeprom_value; + +}; + +#endif // _MCP4725_HPP_ \ No newline at end of file diff --git a/main.cpp b/main.cpp index c41ca16..2cc6c22 100644 --- a/main.cpp +++ b/main.cpp @@ -58,11 +58,20 @@ void foo() } +void dac() +{ + MCP4725 dac(dummyI2C); + dac = 10; + dac = 10; + +} + int main(int argc, char *argv[]) { std::cout << "Main Begin" << std::endl; commander.addNewCommand("dummy", "The test command for testing the test", dummy); commander.addNewCommand("foo", "The test command for foo was called", foo); + commander.addNewCommand("dac", "The test command for dac was called", dac); commander(argv[1]); std::cout << "Main End" << std::endl; return 1; diff --git a/main.hpp b/main.hpp index 54e1c5d..9c9dfe9 100644 --- a/main.hpp +++ b/main.hpp @@ -12,6 +12,8 @@ #include "./interfaces/spich.hpp" #include "./interfaces/i2c.hpp" +#include "./drivers/MCP4725/mcp4725.hpp" + #include "bsl/csl/raspberry/peripherals/pinRaspberry.hpp" #include "bsl/csl/PC/peripherals/pinPC.hpp" #include "bsl/csl/PC/peripherals/spiDummy.hpp" diff --git a/runtest b/runtest index 6c6cc52..79c1045 100755 Binary files a/runtest and b/runtest differ