From f578fd2854a3effa1364d5dd6af7962cf20a25b9 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Wed, 17 May 2023 11:31:39 +0300 Subject: [PATCH 01/10] temperature: Base for temperature thread --- source/shoh/.cproject | 6 ++++ source/shoh/src/peripherals/I2C.h | 5 ++- source/shoh/src/threads/master/Master.cpp | 4 +++ .../threads/temperature/SensorTempTC74.cpp | 11 +++++++ .../src/threads/temperature/SensorTempTC74.h | 21 ++++++++++++ .../src/threads/temperature/Temperature.cpp | 32 +++++++++++++++++++ .../src/threads/temperature/Temperature.h | 28 ++++++++++++++++ 7 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 source/shoh/src/threads/temperature/SensorTempTC74.cpp create mode 100644 source/shoh/src/threads/temperature/SensorTempTC74.h create mode 100644 source/shoh/src/threads/temperature/Temperature.cpp create mode 100644 source/shoh/src/threads/temperature/Temperature.h diff --git a/source/shoh/.cproject b/source/shoh/.cproject index 1f9b41f..7436f16 100644 --- a/source/shoh/.cproject +++ b/source/shoh/.cproject @@ -55,6 +55,7 @@ + @@ -208,6 +211,7 @@ + @@ -238,6 +242,7 @@ + diff --git a/source/shoh/src/peripherals/I2C.h b/source/shoh/src/peripherals/I2C.h index 34a856b..6bf5016 100644 --- a/source/shoh/src/peripherals/I2C.h +++ b/source/shoh/src/peripherals/I2C.h @@ -14,9 +14,8 @@ struct I2C_config { unsigned int device_number; unsigned int speed; - unsigned int clock_divider; - unsigned int i2c_mode; - I2C_config(unsigned int dn, unsigned int sp, unsigned int cd): device_number(dn), speed(sp), clock_divider(cd), i2c_mode(IOCON_SFI2C_EN) {}; + unsigned int i2c_mode = IOCON_SFI2C_EN; +// I2C_config(unsigned int dn, unsigned int sp, unsigned int cd): device_number(dn), speed(sp), i2c_mode(IOCON_SFI2C_EN) {}; }; class I2C { diff --git a/source/shoh/src/threads/master/Master.cpp b/source/shoh/src/threads/master/Master.cpp index a431323..040028a 100644 --- a/source/shoh/src/threads/master/Master.cpp +++ b/source/shoh/src/threads/master/Master.cpp @@ -13,6 +13,7 @@ #include "Manager.h" #include "Logging.h" #include "UserInterface.h" +#include "Temperature.h" #include "queue.h" static const char* rotary_direction[] = @@ -131,6 +132,9 @@ void thread_master(void* pvParams) { manager->tm->createTask(thread_relay, "relay", configMINIMAL_STACK_SIZE * 9,tskIDLE_PRIORITY + 1UL, static_cast(manager)); + manager->tm->createTask(thread_temperature, "temperature", + configMINIMAL_STACK_SIZE * 10,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 new file mode 100644 index 0000000..be4ee44 --- /dev/null +++ b/source/shoh/src/threads/temperature/SensorTempTC74.cpp @@ -0,0 +1,11 @@ +/* + * SensorTempTC74.cpp + * + * Created on: 16 May 2023 + */ + +#include "SensorTempTC74.h" + +SensorTempTC74::SensorTempTC74(I2C* pi2c) : _pi2c(pi2c) {} + +SensorTempTC74::~SensorTempTC74() {} diff --git a/source/shoh/src/threads/temperature/SensorTempTC74.h b/source/shoh/src/threads/temperature/SensorTempTC74.h new file mode 100644 index 0000000..c597b5b --- /dev/null +++ b/source/shoh/src/threads/temperature/SensorTempTC74.h @@ -0,0 +1,21 @@ +/* + * SensorTempTC74.h + * + * Created on: 16 May 2023 + */ + +#ifndef THREADS_TEMPERATURE_SENSORTEMPTC74_H_ +#define THREADS_TEMPERATURE_SENSORTEMPTC74_H_ + +#include "I2C.h" + +class SensorTempTC74 { +public: + SensorTempTC74(I2C* pi2c); + virtual ~SensorTempTC74(); + bool read(); +private: + I2C* _pi2c; +}; + +#endif /* THREADS_TEMPERATURE_SENSORTEMPTC74_H_ */ diff --git a/source/shoh/src/threads/temperature/Temperature.cpp b/source/shoh/src/threads/temperature/Temperature.cpp new file mode 100644 index 0000000..3159962 --- /dev/null +++ b/source/shoh/src/threads/temperature/Temperature.cpp @@ -0,0 +1,32 @@ +/* + * Temperature.cpp + * + * Created on: 16 May 2023 + */ + +#include "Temperature.h" +#include "SensorTempTC74.h" +#include "Log.h" + +Temperature::Temperature(ThreadCommon::QueueManager* qm, I2C* pi2c) : _qm(qm), _pi2c(pi2c) {} + +Temperature::~Temperature() {} + +void Temperature::taskFunction() +{ + SensorTempTC74 ext_temp_sensor(this->_pi2c); + for (;;) + { + + vTaskDelay(5000); + } +} + +void thread_temperature(void* pvParams) +{ + ThreadCommon::CommonManagers * manager = static_cast(pvParams); + I2C_config conf{0x4a, 55000}; + I2C i2c(conf); + Temperature t(manager->qm, &i2c); + t.taskFunction(); +} diff --git a/source/shoh/src/threads/temperature/Temperature.h b/source/shoh/src/threads/temperature/Temperature.h new file mode 100644 index 0000000..1fdbe7a --- /dev/null +++ b/source/shoh/src/threads/temperature/Temperature.h @@ -0,0 +1,28 @@ +/* + * Temperature.h + * + * Created on: 16 May 2023 + */ + +#ifndef THREADS_TEMPERATURE_TEMPERATURE_H_ +#define THREADS_TEMPERATURE_TEMPERATURE_H_ + +#include "FreeRTOS.h" +#include "task.h" +#include "ThreadCommon.h" +#include "DigitalIoPin.h" +#include "I2C.h" + +class Temperature { +public: + Temperature(ThreadCommon::QueueManager* qm, I2C* pi2c); + virtual ~Temperature(); + void taskFunction(); +private: + ThreadCommon::QueueManager* _qm; + I2C* _pi2c; +}; + +void thread_temperature(void* pvParams); + +#endif /* THREADS_TEMPERATURE_TEMPERATURE_H_ */ From fc2a05a9d886d4179381fb366a7ef758f4b5ac94 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Wed, 17 May 2023 14:16:23 +0300 Subject: [PATCH 02/10] 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); } From b891695bfc55e4121e23e31a2021b9faef31e9e3 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Wed, 17 May 2023 17:21:55 +0300 Subject: [PATCH 03/10] temperature: Fixing issues. *We are running out of memory. *Less safe I2C transaction. *Discovered a bug in clock. --- source/shoh/src/peripherals/Clock.cpp | 2 +- source/shoh/src/peripherals/I2C.cpp | 2 +- source/shoh/src/threads/master/Master.cpp | 10 +++++----- source/shoh/src/threads/temperature/SensorTempTC74.cpp | 4 ++-- source/shoh/src/threads/temperature/Temperature.cpp | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/source/shoh/src/peripherals/Clock.cpp b/source/shoh/src/peripherals/Clock.cpp index 6366182..9a3e764 100644 --- a/source/shoh/src/peripherals/Clock.cpp +++ b/source/shoh/src/peripherals/Clock.cpp @@ -93,7 +93,7 @@ void Clock::updateClock() diff_overflows = (old_overflows <= this->_overflows) //Usually it is new amount of overflows - old. ? (this->_overflows - old_overflows) - //It is possible that overflows counter will overflow. + //It is possible that overflows counter will overflow. (Seems that it causes the timer to get insane count when it works.) : (0xffffffffffffffff - old_overflows + this->_overflows); //First case -> no overflow diff --git a/source/shoh/src/peripherals/I2C.cpp b/source/shoh/src/peripherals/I2C.cpp index 2104c06..5f9c753 100644 --- a/source/shoh/src/peripherals/I2C.cpp +++ b/source/shoh/src/peripherals/I2C.cpp @@ -103,7 +103,7 @@ I2C::transaction (uint8_t devAddr, uint8_t *txBuffPtr, uint16_t txSize, I2CM_XFER_T i2cmXferRec; // make sure that master is idle - while (Chip_I2CM_StateChanged(this->device) == 0); + //while (Chip_I2CM_StateChanged(this->device) == 0); /* Setup I2C transfer record */ i2cmXferRec.slaveAddr = devAddr; diff --git a/source/shoh/src/threads/master/Master.cpp b/source/shoh/src/threads/master/Master.cpp index f70014a..1a562d2 100644 --- a/source/shoh/src/threads/master/Master.cpp +++ b/source/shoh/src/threads/master/Master.cpp @@ -121,19 +121,19 @@ void thread_master(void* pvParams) { LOG_INFO("Master is creating tasks"); manager->tm->createTask(thread_manager, "manager", - configMINIMAL_STACK_SIZE * 14,tskIDLE_PRIORITY + 1UL, + configMINIMAL_STACK_SIZE * 13,tskIDLE_PRIORITY + 1UL, static_cast(manager)); manager->tm->createTask(thread_rotary, "rotary", - configMINIMAL_STACK_SIZE * 9,tskIDLE_PRIORITY + 1UL, + configMINIMAL_STACK_SIZE * 8,tskIDLE_PRIORITY + 1UL, static_cast(manager)); manager->tm->createTask(thread_user_interface, "user_interface", - configMINIMAL_STACK_SIZE * 9,tskIDLE_PRIORITY + 1UL, + configMINIMAL_STACK_SIZE * 8,tskIDLE_PRIORITY + 1UL, static_cast(manager)); manager->tm->createTask(thread_relay, "relay", - configMINIMAL_STACK_SIZE * 9,tskIDLE_PRIORITY + 1UL, + configMINIMAL_STACK_SIZE * 8,tskIDLE_PRIORITY + 1UL, static_cast(manager)); manager->tm->createTask(thread_temperature, "temperature", - configMINIMAL_STACK_SIZE * 9,tskIDLE_PRIORITY + 1UL, + configMINIMAL_STACK_SIZE * 8,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 6425f39..1817a86 100644 --- a/source/shoh/src/threads/temperature/SensorTempTC74.cpp +++ b/source/shoh/src/threads/temperature/SensorTempTC74.cpp @@ -51,7 +51,7 @@ uint8_t SensorTempTC74::read() if (this->on_standby()) { this->remove_standby(); - vTaskDelay(1); + vTaskDelay(3000); } //if ready and up - read @@ -64,7 +64,7 @@ uint8_t SensorTempTC74::read() LOG_WARNING("Unable to read temperature sensor [%d] value.", this->_dev_addr); //set standy. - this->set_standby(); + //this->set_standby(); return data; } diff --git a/source/shoh/src/threads/temperature/Temperature.cpp b/source/shoh/src/threads/temperature/Temperature.cpp index f5fdd12..210f9d8 100644 --- a/source/shoh/src/threads/temperature/Temperature.cpp +++ b/source/shoh/src/threads/temperature/Temperature.cpp @@ -33,7 +33,7 @@ void Temperature::taskFunction() void thread_temperature(void* pvParams) { ThreadCommon::CommonManagers * manager = static_cast(pvParams); - I2C_config conf{0x4a, 55000}; + I2C_config conf{0x4a, 100000}; I2C i2c(conf); Temperature t(manager->qm, &i2c); t.taskFunction(); From 599be89871cfa215a96aba9085ff0f6abdff7625 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Thu, 18 May 2023 11:41:09 +0300 Subject: [PATCH 04/10] clock: Fixed the bug with multitask. --- source/shoh/src/peripherals/Clock.cpp | 13 ++++++++----- .../shoh/src/threads/temperature/SensorTempTC74.cpp | 4 +++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/source/shoh/src/peripherals/Clock.cpp b/source/shoh/src/peripherals/Clock.cpp index 9a3e764..298d495 100644 --- a/source/shoh/src/peripherals/Clock.cpp +++ b/source/shoh/src/peripherals/Clock.cpp @@ -7,6 +7,7 @@ #include "Clock.h" #include "FreeRTOS.h" #include "task.h" +#include "Log.h" static const uint64_t max_counter_value = 0xffffffff; @@ -71,14 +72,16 @@ Clock::~Clock() {} * large, if it will be enough to get overflow amount of overflows * 2 times between the function calls the clock will desync. * *Barely possible with counter overflows every 89 sec, but still. + * *Is fully guarded by mutex, since it is expected to + * send log messages one after another, but it can happen simultaneously. */ void Clock::updateClock() { + this->_guard.lock(); uint64_t diff_overflows = 0; //Remember old number of overflows. uint64_t old_overflows = this->_overflows; - this->_guard.lock(); //Stop the counter. Chip_SCT_SetControl(LPC_SCT1, 0x1 << 1); //Capture number of counter overflows. @@ -86,14 +89,13 @@ void Clock::updateClock() //Capture the counter value. uint64_t cur_count_u = LPC_SCT1->COUNT_U; //Resume the counter. - Chip_SCT_ClearControl(LPC_SCT1, 0x1 << 1); - this->_guard.unlock(); + Chip_SCT_ClearControl(LPC_SCT1, 0x1 << 1); //Handle overflows. diff_overflows = (old_overflows <= this->_overflows) //Usually it is new amount of overflows - old. ? (this->_overflows - old_overflows) - //It is possible that overflows counter will overflow. (Seems that it causes the timer to get insane count when it works.) + //It is possible that overflows counter will overflow. : (0xffffffffffffffff - old_overflows + this->_overflows); //First case -> no overflow @@ -110,7 +112,7 @@ void Clock::updateClock() //Add full counter values for all overflows except one. (max_counter_value * (diff_overflows - 1) //Add the difference between counter values having overflow in mind. - + (cur_count_u + (max_counter_value - _last_counter_value))) + + (cur_count_u + (max_counter_value - this->_last_counter_value))) //Convert to milliseconds. * 1000) / (double)(Chip_Clock_GetMainClockRate()); @@ -119,6 +121,7 @@ void Clock::updateClock() //Remember last counter value. //It is important if we won't have an overflow next time. this->_last_counter_value = cur_count_u; + this->_guard.unlock(); } TimeFromStart Clock::getTimeFromStart() diff --git a/source/shoh/src/threads/temperature/SensorTempTC74.cpp b/source/shoh/src/threads/temperature/SensorTempTC74.cpp index 1817a86..4af8f9d 100644 --- a/source/shoh/src/threads/temperature/SensorTempTC74.cpp +++ b/source/shoh/src/threads/temperature/SensorTempTC74.cpp @@ -57,7 +57,9 @@ uint8_t SensorTempTC74::read() //if ready and up - read if (this->is_ready() && this->_up_flag) { - this->read_reg(this->_temp_reg, &data, 1); + this->_up_flag = this->read_reg(this->_temp_reg, &data, 1); + if(!this->_up_flag) + LOG_WARNING("I2C transaction for getting the temperature failed."); LOG_DEBUG("Temperature sensor returned: %x", data); } else From ef32a5077b35e287d284492cb3c25e6481383607 Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Thu, 18 May 2023 11:45:34 +0300 Subject: [PATCH 05/10] temperature: [#9] [#11] send Event to master --- .../src/threads/temperature/Temperature.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/source/shoh/src/threads/temperature/Temperature.cpp b/source/shoh/src/threads/temperature/Temperature.cpp index 210f9d8..941dfdf 100644 --- a/source/shoh/src/threads/temperature/Temperature.cpp +++ b/source/shoh/src/threads/temperature/Temperature.cpp @@ -6,6 +6,7 @@ #include "Temperature.h" #include "SensorTempTC74.h" +#include "Event.h" #include "Log.h" Temperature::Temperature(ThreadCommon::QueueManager* qm, I2C* pi2c) : _qm(qm), _pi2c(pi2c) {} @@ -15,18 +16,22 @@ Temperature::~Temperature() {} void Temperature::taskFunction() { SensorTempTC74 ext_temp_sensor(this->_pi2c, 0x4a); + Event t (Event::ExternalTemp, -10); int8_t temp_value = 0; for (;;) { if (ext_temp_sensor.is_up()) temp_value = ext_temp_sensor.getTemperature(); - + + if(temp_value == -10) + { + LOG_ERROR("Failed to get temperature."); + continue; + } + LOG_DEBUG("External temperature is: %d", temp_value); - - //Send temperature on queue. As event. :( - - - vTaskDelay(5000); + t.setDataOf(Event::ExternalTemp, temp_value); + _qm->send(ThreadCommon::QueueManager::master_event_all, &t, 5000); } } From 1b3a7f4f753c017781148a27369cf79a6e0cd62d Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Thu, 18 May 2023 11:45:34 +0300 Subject: [PATCH 06/10] temperature: [#9] [#11] fix temp_value with error value --- source/shoh/src/threads/temperature/Temperature.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/shoh/src/threads/temperature/Temperature.cpp b/source/shoh/src/threads/temperature/Temperature.cpp index 941dfdf..1880b54 100644 --- a/source/shoh/src/threads/temperature/Temperature.cpp +++ b/source/shoh/src/threads/temperature/Temperature.cpp @@ -17,7 +17,7 @@ void Temperature::taskFunction() { SensorTempTC74 ext_temp_sensor(this->_pi2c, 0x4a); Event t (Event::ExternalTemp, -10); - int8_t temp_value = 0; + int8_t temp_value = -10; for (;;) { if (ext_temp_sensor.is_up()) From 1ce12cfd028d423550a1013ae8edef907398af0e Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Thu, 18 May 2023 13:02:43 +0300 Subject: [PATCH 07/10] master: add more strict debug to queue send --- source/shoh/src/threads/master/Master.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/source/shoh/src/threads/master/Master.cpp b/source/shoh/src/threads/master/Master.cpp index 1a562d2..ea1d38f 100644 --- a/source/shoh/src/threads/master/Master.cpp +++ b/source/shoh/src/threads/master/Master.cpp @@ -40,30 +40,31 @@ Master::~Master() void Master::HandleEventType(Event* e) { EventRawData rd = e->getData(); - switch (e->getType()) + bool send = false; + switch (e->getType()) { case Event::Null: LOG_ERROR("Master recieved Event::Null with data: %d", rd); break; case Event::Rotary: //Comes from rotary, goes to manager - _qm->send(ThreadCommon::QueueManager::manager_event_master, e, 0); + send = _qm->send(ThreadCommon::QueueManager::manager_event_master, e, 0); //LOG_WARNING("Timestamp: %zus, Clock: %zu, Chip freq: %zu", LPC_SCT1->COUNT_U / Chip_Clock_GetMainClockRate(), LPC_SCT1->COUNT_U, Chip_Clock_GetMainClockRate()); - LOG_DEBUG("Rotary: %s has been forwarded to manager", rotary_direction[rd]); + if (send) LOG_DEBUG("Rotary: %s has been forwarded to manager", rotary_direction[rd]); break; case Event::InternalTemp: // TODO remove (deprecated) break; case Event::ExternalTemp: //Comes from sensors, goes to relay & manager - _qm->send(ThreadCommon::QueueManager::relay_event_master, e, 0); - _qm->send(ThreadCommon::QueueManager::manager_event_master, e, 0); - LOG_DEBUG("ExtTemp: %d has been forwarded to manager and relay", rd); + send = _qm->send(ThreadCommon::QueueManager::relay_event_master, e, 0); + send = _qm->send(ThreadCommon::QueueManager::manager_event_master, e, 0); + if (send) LOG_DEBUG("ExtTemp: %d has been forwarded to manager and relay", rd); break; case Event::SetPoint: //Comes from manager, goes to relay - _qm->send(ThreadCommon::QueueManager::relay_event_master, e, 0); - LOG_DEBUG("SetPoint: %d has been forwarded to relay", rd); + send = _qm->send(ThreadCommon::QueueManager::relay_event_master, e, 0); + if (send) LOG_DEBUG("SetPoint: %d has been forwarded to relay", rd); break; default: LOG_ERROR("Unknown EventType"); From 85520b6fad862b1e9d22f5029f85e49219a38d6e Mon Sep 17 00:00:00 2001 From: RedHawk Date: Thu, 18 May 2023 13:29:34 +0300 Subject: [PATCH 08/10] temperature: Sensor-specific handling. Proper delay. --- source/shoh/src/threads/temperature/SensorTempTC74.cpp | 2 +- source/shoh/src/threads/temperature/Temperature.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/source/shoh/src/threads/temperature/SensorTempTC74.cpp b/source/shoh/src/threads/temperature/SensorTempTC74.cpp index 4af8f9d..b8d47dd 100644 --- a/source/shoh/src/threads/temperature/SensorTempTC74.cpp +++ b/source/shoh/src/threads/temperature/SensorTempTC74.cpp @@ -19,7 +19,7 @@ SensorTempTC74::~SensorTempTC74() int8_t SensorTempTC74::getTemperature() { - return static_cast(this->read()); + return static_cast(this->read()) - 6; } bool SensorTempTC74::is_up() diff --git a/source/shoh/src/threads/temperature/Temperature.cpp b/source/shoh/src/threads/temperature/Temperature.cpp index 1880b54..50c8da4 100644 --- a/source/shoh/src/threads/temperature/Temperature.cpp +++ b/source/shoh/src/threads/temperature/Temperature.cpp @@ -31,7 +31,8 @@ void Temperature::taskFunction() LOG_DEBUG("External temperature is: %d", temp_value); t.setDataOf(Event::ExternalTemp, temp_value); - _qm->send(ThreadCommon::QueueManager::master_event_all, &t, 5000); + _qm->send(ThreadCommon::QueueManager::master_event_all, &t, 0); + vTaskDelay(5000); } } From 05037d2677e6622db2be16584d9c221200887994 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Thu, 18 May 2023 13:30:05 +0300 Subject: [PATCH 09/10] Relay: Missed break; --- source/shoh/src/threads/relay/Relay.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/source/shoh/src/threads/relay/Relay.cpp b/source/shoh/src/threads/relay/Relay.cpp index 12a4640..3647798 100644 --- a/source/shoh/src/threads/relay/Relay.cpp +++ b/source/shoh/src/threads/relay/Relay.cpp @@ -56,6 +56,7 @@ void Relay::setPowerMode(PowerMode pm) relays[INF_RELAY].RelayOff(); relays[SUP_RELAY].RelayOff(); LOG_INFO("Heater is turned OFF"); + break; case PowerMode::POWER_1: relays[INF_RELAY].RelayOn(); relays[SUP_RELAY].RelayOff(); From 353fea22558aa3e00b7560acf190497157da66b2 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Fri, 19 May 2023 00:10:47 +0300 Subject: [PATCH 10/10] temperature: Change to new Event. --- source/shoh/src/threads/manager/Menu.cpp | 4 +++- source/shoh/src/threads/relay/Relay.cpp | 5 ++++- source/shoh/src/threads/temperature/Temperature.cpp | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/source/shoh/src/threads/manager/Menu.cpp b/source/shoh/src/threads/manager/Menu.cpp index f2086f4..6a3afb7 100644 --- a/source/shoh/src/threads/manager/Menu.cpp +++ b/source/shoh/src/threads/manager/Menu.cpp @@ -32,9 +32,11 @@ Menu::readSetPointFromEEPROM (void) { EventRawData *data = (EventRawData *)eeprom.read_from (EEPROM_START_ADDR, sizeof(EventRawData)); - if ((*data) > 0 && (*data) < 120) + if ((*data) > 0 && (*data) < 100) { set_point.setCurrent(*data); + Event e(Event::EventType::SetPoint, set_point.getCurrent()); + _qm->send(ThreadCommon::QueueManager::master_event_all, &e , 1); } } diff --git a/source/shoh/src/threads/relay/Relay.cpp b/source/shoh/src/threads/relay/Relay.cpp index 3647798..d548802 100644 --- a/source/shoh/src/threads/relay/Relay.cpp +++ b/source/shoh/src/threads/relay/Relay.cpp @@ -38,7 +38,8 @@ void inline RelayDevice::RelayOff() } -Relay::Relay(ThreadCommon::QueueManager* qm): _qm(qm) +Relay::Relay(ThreadCommon::QueueManager* qm): + _qm(qm), ext_temp(0x7f), setpoint(0) { LOG_DEBUG("Creating Relay"); } @@ -95,9 +96,11 @@ void Relay::parseEvent(Event* e) { case Event::ExternalTemp: ext_temp = rd; + LOG_DEBUG("Relay got ext_temp: %d", rd); break; case Event::SetPoint: setpoint = rd; + LOG_DEBUG("Relay got setpoint: %d", rd); break; default: assert(0); diff --git a/source/shoh/src/threads/temperature/Temperature.cpp b/source/shoh/src/threads/temperature/Temperature.cpp index 50c8da4..9c3692d 100644 --- a/source/shoh/src/threads/temperature/Temperature.cpp +++ b/source/shoh/src/threads/temperature/Temperature.cpp @@ -30,7 +30,7 @@ void Temperature::taskFunction() } LOG_DEBUG("External temperature is: %d", temp_value); - t.setDataOf(Event::ExternalTemp, temp_value); + t.setEvent(Event::ExternalTemp, temp_value); _qm->send(ThreadCommon::QueueManager::master_event_all, &t, 0); vTaskDelay(5000); }