diff --git a/README.md b/README.md index 20b7d95..8b82b80 100644 --- a/README.md +++ b/README.md @@ -51,16 +51,12 @@ Set the functional mode, Tos and Thyst: if that functionally desired. **Connections** The Sensor uses I2C for communication's, data is outputted to a PC. -Pins and I2C port can be set in the main.cpp. Default is I2C0 and GPIO16(data) GPIO17(clock). -If you want to use the other I2C port (I2C1) in addition to changing in the main.ccp, -The user must change it in library (lm75.h) as well: -* i2c_inst_t *i2c = i2c0 to i2c_inst_t *i2c = i2c1 -Could not find a way around this. +Pins and I2C port(I2C0 or I2C1) can be set in the main.cpp. Default is I2C0 and GPIO16(data) GPIO17(clock). **Files** The main.cpp contains tests showing library functions -There is also the library files(LM75A.cpp and LM75A.h), +There is also the library files(LM75A.cpp and LM75A.hpp), **Output** diff --git a/extra/doc/CHANGELOG.md b/extra/doc/CHANGELOG.md index dff3768..59c8f48 100644 --- a/extra/doc/CHANGELOG.md +++ b/extra/doc/CHANGELOG.md @@ -2,5 +2,7 @@ * version 1.0.0 September 2022 * first release +* version 1.0.1 December 2022 + * Change so user can pick any I2C interface (IC20 or IC21) diff --git a/include/lm75/lm75.h b/include/lm75/lm75.hpp similarity index 85% rename from include/lm75/lm75.h rename to include/lm75/lm75.hpp index 3291d04..55a7c39 100644 --- a/include/lm75/lm75.h +++ b/include/lm75/lm75.hpp @@ -1,6 +1,6 @@ /* * Project Name: Library for the LM75A temperature sensor by NXP and Texas Instruments. - * File: lm75.h + * File: lm75.hpp * Description: library header file * Author: Gavin Lyons. * IDE: Rpi=PICo rp2040 C++ @@ -53,7 +53,11 @@ class LIB_LM75A { private: // Private variables - uint8_t _i2cAddress; + uint8_t _i2cAddress; + i2c_inst_t *i2c = i2c0; // i2C port number, i2c1 or i2c0 + uint8_t _SDataPin; + uint8_t _SClkPin; + uint16_t _CLKSpeed = 100; //I2C bus speed in khz typically 100-400 // Private functions uint8_t read8bitRegister(const uint8_t reg); @@ -64,11 +68,11 @@ class LIB_LM75A public: // Constructor - LIB_LM75A(uint8_t address); + LIB_LM75A(uint8_t address, i2c_inst_t* i2c_type, uint8_t SDApin, uint8_t SCLKpin, uint16_t CLKspeed); //I2c init & deinit - void initLM75A(i2c_inst_t* i2c_type, uint8_t SDApin, uint8_t SCLKpin, uint16_t CLKspeed); - void deinitLM75A(i2c_inst_t* i2c_type); + void initLM75A(); + void deinitLM75A(); // Power management void shutdown(); @@ -96,8 +100,7 @@ class LIB_LM75A uint8_t getConfig(); float getProdId(); - i2c_inst_t *i2c = i2c0; // i2C port number - int16_t return_value = 0; //return value + int16_t return_value = 0; //return value, I2C routines }; #endif diff --git a/src/lm75/lm75.cpp b/src/lm75/lm75.cpp index 5cef26f..1a4a36d 100644 --- a/src/lm75/lm75.cpp +++ b/src/lm75/lm75.cpp @@ -10,14 +10,22 @@ */ #include "pico/stdlib.h" -#include "../include/lm75/lm75.h" +#include "../include/lm75/lm75.hpp" // Constructor -// Param 1 I2C address (7-bit) -LIB_LM75A::LIB_LM75A(uint8_t i2cAddress) +// Param1 : I2C address (7-bit) +// Param2 : I2C instance of port IC20 or I2C1 +// Param3 : I2C Data pin +// Param4 : I2C Clock pin +// Param5 : I2C Bus Clock speed in KHz. Typically 100-400 +LIB_LM75A::LIB_LM75A(uint8_t i2cAddress, i2c_inst_t* i2c_type, uint8_t SDApin, uint8_t SCLKpin, uint16_t CLKspeed) { - _i2cAddress = i2cAddress; + _i2cAddress = i2cAddress; + _SClkPin = SCLKpin; + _SDataPin = SDApin; + _CLKSpeed = CLKspeed; + i2c = i2c_type; } // Power management @@ -25,15 +33,15 @@ LIB_LM75A::LIB_LM75A(uint8_t i2cAddress) // Func Desc : enter Shutdown mode : 4 μA (Typical) current draw. void LIB_LM75A::shutdown() { - uint8_t config = read8bitRegister(LM75A_REGISTER_CONFIG); - write8bitRegister(LM75A_REGISTER_CONFIG, (config & 0b11111110) | 0b1); + uint8_t config = read8bitRegister(LM75A_REGISTER_CONFIG); + write8bitRegister(LM75A_REGISTER_CONFIG, (config & 0b11111110) | 0b1); } // Func Desc : exit Shutdown mode for Operating mode: 280 μA (Typical) void LIB_LM75A::wakeup() { - uint8_t config = read8bitRegister(LM75A_REGISTER_CONFIG); - write8bitRegister(LM75A_REGISTER_CONFIG, config & 0b11111110); + uint8_t config = read8bitRegister(LM75A_REGISTER_CONFIG); + write8bitRegister(LM75A_REGISTER_CONFIG, config & 0b11111110); } // Fucn Desc get power mode // Return bool @@ -41,7 +49,7 @@ void LIB_LM75A::wakeup() // 0 operating mode bool LIB_LM75A::isShutdown() { - return (read8bitRegister(LM75A_REGISTER_CONFIG) & 0b1) == 1; + return (read8bitRegister(LM75A_REGISTER_CONFIG) & 0b1) == 1; } @@ -51,84 +59,84 @@ bool LIB_LM75A::isShutdown() // returns float float LIB_LM75A::getTemperature() { - uint16_t result; - if (!read16bitRegister(LM75A_REGISTER_TEMP, result)) - { - return LM75A_INVALID_TEMPERATURE; - } - return (float)result / 256.0f; + uint16_t result; + if (!read16bitRegister(LM75A_REGISTER_TEMP, result)) + { + return LM75A_INVALID_TEMPERATURE; + } + return (float)result / 256.0f; } // Get temperature Farenheit( // returns float float LIB_LM75A::getTemperatureInFarenheit() { - return getTemperature() * 1.8f + 32.0f; + return getTemperature() * 1.8f + 32.0f; } // Configuration functions float LIB_LM75A::getHysterisisTemperature() { - uint16_t result; - if (!read16bitRegister(LM75A_REGISTER_THYST, result)) - { - return LM75A_INVALID_TEMPERATURE; - } - return (float)result / 256.0f; + uint16_t result; + if (!read16bitRegister(LM75A_REGISTER_THYST, result)) + { + return LM75A_INVALID_TEMPERATURE; + } + return (float)result / 256.0f; } FaultQueueValue LIB_LM75A::getFaultQueueValue() { - return (FaultQueueValue)(read8bitRegister(LM75A_REGISTER_CONFIG) & 0b00011000); + return (FaultQueueValue)(read8bitRegister(LM75A_REGISTER_CONFIG) & 0b00011000); } float LIB_LM75A::getOSTripTemperature() { - uint16_t result; - if (!read16bitRegister(LM75A_REGISTER_TOS, result)) - { - return LM75A_INVALID_TEMPERATURE; - } - return (float)result / 256.0f; + uint16_t result; + if (!read16bitRegister(LM75A_REGISTER_TOS, result)) + { + return LM75A_INVALID_TEMPERATURE; + } + return (float)result / 256.0f; } OsPolarity LIB_LM75A::getOsPolarity() { - return (OsPolarity)(read8bitRegister(LM75A_REGISTER_CONFIG) & 0b100); + return (OsPolarity)(read8bitRegister(LM75A_REGISTER_CONFIG) & 0b100); } DeviceMode LIB_LM75A::getDeviceMode() { - return (DeviceMode)(read8bitRegister(LM75A_REGISTER_CONFIG) & 0b010); + return (DeviceMode)(read8bitRegister(LM75A_REGISTER_CONFIG) & 0b010); } void LIB_LM75A::setHysterisisTemperature(float temperature) { - write16bitRegister(LM75A_REGISTER_THYST, temperature * 256); + write16bitRegister(LM75A_REGISTER_THYST, temperature * 256); } void LIB_LM75A::setOsTripTemperature(float temperature) { - write16bitRegister(LM75A_REGISTER_TOS, temperature * 256); + write16bitRegister(LM75A_REGISTER_TOS, temperature * 256); } void LIB_LM75A::setFaultQueueValue(FaultQueueValue value) { - uint8_t config = read8bitRegister(LM75A_REGISTER_CONFIG); - write8bitRegister(LM75A_REGISTER_CONFIG, (config & 0b11100111) | value); + uint8_t config = read8bitRegister(LM75A_REGISTER_CONFIG); + write8bitRegister(LM75A_REGISTER_CONFIG, (config & 0b11100111) | value); } void LIB_LM75A::setOsPolarity(OsPolarity polarity) { - uint8_t config = read8bitRegister(LM75A_REGISTER_CONFIG); - write8bitRegister(LM75A_REGISTER_CONFIG, (config & 0b11111011) | polarity); + uint8_t config = read8bitRegister(LM75A_REGISTER_CONFIG); + write8bitRegister(LM75A_REGISTER_CONFIG, (config & 0b11111011) | polarity); } void LIB_LM75A::setDeviceMode(DeviceMode deviceMode) { - uint8_t config = read8bitRegister(LM75A_REGISTER_CONFIG); - write8bitRegister(LM75A_REGISTER_CONFIG, (config & 0b11111101) | deviceMode); + uint8_t config = read8bitRegister(LM75A_REGISTER_CONFIG); + write8bitRegister(LM75A_REGISTER_CONFIG, (config & 0b11111101) | deviceMode); } @@ -137,114 +145,113 @@ void LIB_LM75A::setDeviceMode(DeviceMode deviceMode) // Returns 1 for success 0 if not. bool LIB_LM75A::isConnected() { - uint8_t oldValue = read8bitRegister(LM75A_REGISTER_CONFIG); - write8bitRegister(LM75A_REGISTER_CONFIG, 0x0f); - uint8_t newValue = read8bitRegister(LM75A_REGISTER_CONFIG); - write8bitRegister(LM75A_REGISTER_CONFIG, oldValue); - return newValue == 0x0f; + uint8_t oldValue = read8bitRegister(LM75A_REGISTER_CONFIG); + write8bitRegister(LM75A_REGISTER_CONFIG, 0x0f); + uint8_t newValue = read8bitRegister(LM75A_REGISTER_CONFIG); + write8bitRegister(LM75A_REGISTER_CONFIG, oldValue); + return newValue == 0x0f; } // Func Desc : reads config register // Returns Byte with values of config register uint8_t LIB_LM75A::getConfig() { - return read8bitRegister(LM75A_REGISTER_CONFIG); + return read8bitRegister(LM75A_REGISTER_CONFIG); } // Func Desc : reads value of product ID register float LIB_LM75A::getProdId() { - uint8_t value = read8bitRegister(LM75A_REGISTER_PRODID); - return (float)(value >> 4) + (value & 0x0F) / 10.0f; + uint8_t value = read8bitRegister(LM75A_REGISTER_PRODID); + return (float)(value >> 4) + (value & 0x0F) / 10.0f; } // *************** Private function ************************ uint8_t LIB_LM75A::read8bitRegister(const uint8_t reg) { - uint8_t result; - uint8_t BufTx[1]; - BufTx[0] = reg; - - return_value = i2c_write_blocking(i2c, _i2cAddress, BufTx, 1 , true); - if (return_value < 1) - { - return 0xFF; - } - - return_value = i2c_read_blocking(i2c, _i2cAddress, &result, 1, false); - if (return_value < 1) - { - return 0xFF; - } - return result; + uint8_t result; + uint8_t BufTx[1]; + BufTx[0] = reg; + + return_value = i2c_write_blocking(i2c, _i2cAddress, BufTx, 1 , true); + if (return_value < 1) + { + return 0xFF; + } + + return_value = i2c_read_blocking(i2c, _i2cAddress, &result, 1, false); + if (return_value < 1) + { + return 0xFF; + } + return result; } bool LIB_LM75A::read16bitRegister(const uint8_t reg, uint16_t& response) { - uint8_t bufRX[3]; - uint8_t bufTX[1]; - bufTX[0] = reg; - - return_value = i2c_write_blocking(i2c, _i2cAddress, bufTX, 1 , true); - if (return_value < 1) - { - return false; - } - - return_value = i2c_read_blocking(i2c, _i2cAddress, bufRX, 2, false); - if (return_value < 1) - { - return false; - } - - response = bufRX[0] << 8 | bufRX[1]; - return true; + uint8_t bufRX[3]; + uint8_t bufTX[1]; + bufTX[0] = reg; + + return_value = i2c_write_blocking(i2c, _i2cAddress, bufTX, 1 , true); + if (return_value < 1) + { + return false; + } + + return_value = i2c_read_blocking(i2c, _i2cAddress, bufRX, 2, false); + if (return_value < 1) + { + return false; + } + + response = bufRX[0] << 8 | bufRX[1]; + return true; } bool LIB_LM75A::write16bitRegister(const uint8_t reg, const uint16_t value) { - uint8_t bufTX[3]; - bufTX[0] = reg; - bufTX[1] = value >> 8; - bufTX[2] = value; - - return_value = i2c_write_blocking(i2c, _i2cAddress, bufTX, 3 , false); - if (return_value < 1) - { - return false; - } - - return true; + uint8_t bufTX[3]; + bufTX[0] = reg; + bufTX[1] = value >> 8; + bufTX[2] = value; + + return_value = i2c_write_blocking(i2c, _i2cAddress, bufTX, 3 , false); + if (return_value < 1) + { + return false; + } + + return true; } bool LIB_LM75A::write8bitRegister(const uint8_t reg, const uint8_t value) { - uint8_t bufTX[2]; - bufTX[0] = reg; - bufTX[1] = value; - - return_value = i2c_write_blocking(i2c, _i2cAddress, bufTX, 2 ,false); - if (return_value < 1) - { - return false; - } - return true; + uint8_t bufTX[2]; + bufTX[0] = reg; + bufTX[1] = value; + + return_value = i2c_write_blocking(i2c, _i2cAddress, bufTX, 2 ,false); + if (return_value < 1) + { + return false; + } + return true; } -void LIB_LM75A::initLM75A(i2c_inst_t* i2c_type, uint8_t SDApin, uint8_t SCLKpin, uint16_t CLKspeed) +void LIB_LM75A::initLM75A() { - i2c_inst_t *i2c = i2c_type; - //init I2C - i2c_init(i2c, CLKspeed * 1000); - gpio_set_function(SDApin, GPIO_FUNC_I2C); - gpio_set_function(SCLKpin, GPIO_FUNC_I2C); - gpio_pull_up(SDApin); - gpio_pull_up(SCLKpin); + gpio_set_function(_SDataPin, GPIO_FUNC_I2C); + gpio_set_function(_SClkPin, GPIO_FUNC_I2C); + gpio_pull_up(_SDataPin); + gpio_pull_up(_SClkPin); + i2c_init(i2c, _CLKSpeed * 1000); } -void LIB_LM75A::deinitLM75A(i2c_inst_t* i2c_type) +void LIB_LM75A::deinitLM75A() { - i2c_inst_t *i2c = i2c_type; - i2c_deinit(i2c); + gpio_set_function(_SDataPin, GPIO_FUNC_NULL); + gpio_set_function(_SClkPin, GPIO_FUNC_NULL); + i2c_deinit(i2c); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5cdd277..817791a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,7 @@ // *** Libraries *** #include #include "pico/stdlib.h" -#include "lm75/lm75.h" +#include "lm75/lm75.hpp" // *** Mode select *** // mode 1 or mode 2 @@ -20,7 +20,7 @@ //#define LIB_TEST // Mode 2 :: Full library test example // *** Globals *** -LIB_LM75A lm75a(LM75A_DEFAULT_ADDRESS); +LIB_LM75A lm75a(LM75A_DEFAULT_ADDRESS, i2c0, 16, 17, 100); uint8_t is_connected = 0; // test control uint16_t test_count = 0 ; // test control @@ -59,11 +59,10 @@ DeviceMode newDeviceMode = DeviceMode::DEVICE_MODE_INTERRUPT; // *** Main *** int main() { - stdio_init_all(); // Initialize chosen serial port - lm75a.initLM75A(i2c0, 16, 17, 100); // initLM75A(port, data pin, clock pin, clock speed(KHZ)); - + stdio_init_all(); // Initialize chosen serial port busy_wait_ms(1000); printf("LM75 : Start!\r\n"); + lm75a.initLM75A(); // Check for connection is_connected = lm75a.isConnected();