From fc2a05a9d886d4179381fb366a7ef758f4b5ac94 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Wed, 17 May 2023 14:16:23 +0300 Subject: [PATCH] temperature: Temperature sensor handling. --- source/shoh/src/threads/master/Master.cpp | 4 +- .../threads/temperature/SensorTempTC74.cpp | 96 ++++++++++++++++++- .../src/threads/temperature/SensorTempTC74.h | 22 ++++- .../src/threads/temperature/Temperature.cpp | 10 +- 4 files changed, 125 insertions(+), 7 deletions(-) diff --git a/source/shoh/src/threads/master/Master.cpp b/source/shoh/src/threads/master/Master.cpp index 040028a..f70014a 100644 --- a/source/shoh/src/threads/master/Master.cpp +++ b/source/shoh/src/threads/master/Master.cpp @@ -121,7 +121,7 @@ void thread_master(void* pvParams) { LOG_INFO("Master is creating tasks"); manager->tm->createTask(thread_manager, "manager", - configMINIMAL_STACK_SIZE * 15,tskIDLE_PRIORITY + 1UL, + configMINIMAL_STACK_SIZE * 14,tskIDLE_PRIORITY + 1UL, static_cast(manager)); manager->tm->createTask(thread_rotary, "rotary", configMINIMAL_STACK_SIZE * 9,tskIDLE_PRIORITY + 1UL, @@ -133,7 +133,7 @@ void thread_master(void* pvParams) { configMINIMAL_STACK_SIZE * 9,tskIDLE_PRIORITY + 1UL, static_cast(manager)); manager->tm->createTask(thread_temperature, "temperature", - configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL, + configMINIMAL_STACK_SIZE * 9,tskIDLE_PRIORITY + 1UL, static_cast(manager)); LOG_INFO("Master created tasks"); m.taskFunction(); diff --git a/source/shoh/src/threads/temperature/SensorTempTC74.cpp b/source/shoh/src/threads/temperature/SensorTempTC74.cpp index be4ee44..6425f39 100644 --- a/source/shoh/src/threads/temperature/SensorTempTC74.cpp +++ b/source/shoh/src/threads/temperature/SensorTempTC74.cpp @@ -5,7 +5,99 @@ */ #include "SensorTempTC74.h" +#include "FreeRTOS.h" +#include "task.h" +#include "Log.h" -SensorTempTC74::SensorTempTC74(I2C* pi2c) : _pi2c(pi2c) {} +SensorTempTC74::SensorTempTC74(I2C* pi2c, const uint8_t dev_addr) +: _pi2c(pi2c), _dev_addr(dev_addr), _temp_reg(0x00), _ctrl_reg(0x01), + _ready_bit(0x40), _standby_bit(0x80), _up_flag(false) +{} -SensorTempTC74::~SensorTempTC74() {} +SensorTempTC74::~SensorTempTC74() +{} + +int8_t SensorTempTC74::getTemperature() +{ + return static_cast(this->read()); +} + +bool SensorTempTC74::is_up() +{ + this->on_standby(); + if (!this->_up_flag) + LOG_WARNING("Unable to reach temperature sensor [%d]", this->_dev_addr); + return this->_up_flag; +} + +bool SensorTempTC74::on_standby() +{ + uint8_t data = 0x00; + this->_up_flag = this->read_reg(this->_ctrl_reg, &data, 1); + return data & this->_standby_bit; +} + +bool SensorTempTC74::is_ready() +{ + uint8_t data = 0x00; + this->_up_flag = this->read_reg(this->_ctrl_reg, &data, 1); + return data & this->_ready_bit; +} + +uint8_t SensorTempTC74::read() +{ + uint8_t data = 0x80; // b(1000 0000) + //if on standby - remove standby and wait for 1 ms. + if (this->on_standby()) + { + this->remove_standby(); + vTaskDelay(1); + } + + //if ready and up - read + if (this->is_ready() && this->_up_flag) + { + this->read_reg(this->_temp_reg, &data, 1); + LOG_DEBUG("Temperature sensor returned: %x", data); + } + else + LOG_WARNING("Unable to read temperature sensor [%d] value.", this->_dev_addr); + + //set standy. + this->set_standby(); + + return data; +} + +void SensorTempTC74::remove_standby() +{ + uint8_t data = 0x00; + this->_up_flag = this->write_reg(this->_ctrl_reg, &data, 1); + if(!this->_up_flag) + LOG_WARNING("Unable to remove standby for temperature sensor [%d].", this->_dev_addr); +} + +void SensorTempTC74::set_standby() +{ + uint8_t data = this->_standby_bit; + this->_up_flag = this->write_reg(this->_ctrl_reg, &data, 1); + if(!this->_up_flag) + LOG_WARNING("Unable to set standby for temperature sensor [%d].", this->_dev_addr); +} + +bool SensorTempTC74::write_reg(uint8_t com, uint8_t *trdata, const uint16_t size) +{ + vTaskDelay(1); //Not sure if it is needed. + uint8_t arr[size + 1]; + arr[0] = com; + for (unsigned int i = 1; i < (unsigned int)size + 1; i++) { + arr[i] = trdata[i - 1]; + } + return this->_pi2c->write(this->_dev_addr, arr, size + 1); +} + +bool SensorTempTC74::read_reg(uint8_t com, uint8_t *rdata, uint16_t size) +{ + vTaskDelay(1); //Not sure if it is needed. + return this->_pi2c->transaction(this->_dev_addr, &com, 1, rdata, size); +} diff --git a/source/shoh/src/threads/temperature/SensorTempTC74.h b/source/shoh/src/threads/temperature/SensorTempTC74.h index c597b5b..c30478b 100644 --- a/source/shoh/src/threads/temperature/SensorTempTC74.h +++ b/source/shoh/src/threads/temperature/SensorTempTC74.h @@ -8,14 +8,32 @@ #define THREADS_TEMPERATURE_SENSORTEMPTC74_H_ #include "I2C.h" +#include "chip.h" class SensorTempTC74 { public: - SensorTempTC74(I2C* pi2c); + SensorTempTC74(I2C* pi2c, const uint8_t dev_addr); virtual ~SensorTempTC74(); - bool read(); + int8_t getTemperature(); + bool is_up(); private: + uint8_t read(); + bool on_standby(); + bool is_ready(); + + void remove_standby(); + void set_standby(); + + bool write_reg(uint8_t com, uint8_t *trdata, const uint16_t size); + bool read_reg(uint8_t com, uint8_t *rdata, uint16_t size); + I2C* _pi2c; + const uint8_t _dev_addr; + const uint8_t _temp_reg; + const uint8_t _ctrl_reg; + const uint8_t _ready_bit; + uint8_t _standby_bit; + bool _up_flag; }; #endif /* THREADS_TEMPERATURE_SENSORTEMPTC74_H_ */ diff --git a/source/shoh/src/threads/temperature/Temperature.cpp b/source/shoh/src/threads/temperature/Temperature.cpp index 3159962..f5fdd12 100644 --- a/source/shoh/src/threads/temperature/Temperature.cpp +++ b/source/shoh/src/threads/temperature/Temperature.cpp @@ -14,9 +14,17 @@ Temperature::~Temperature() {} void Temperature::taskFunction() { - SensorTempTC74 ext_temp_sensor(this->_pi2c); + SensorTempTC74 ext_temp_sensor(this->_pi2c, 0x4a); + int8_t temp_value = 0; for (;;) { + if (ext_temp_sensor.is_up()) + temp_value = ext_temp_sensor.getTemperature(); + + LOG_DEBUG("External temperature is: %d", temp_value); + + //Send temperature on queue. As event. :( + vTaskDelay(5000); }