aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoic Poulain <loic.poulain@linaro.org>2020-02-21 15:38:04 +0100
committerLoic Poulain <loic.poulain@linaro.org>2020-02-21 16:32:38 +0100
commit9bdd38511c76482e2da615f0d85e72037b5ce6ae (patch)
treed2bda940c00c8002db30fe67bad9eed0f2291588
parentfead20b3448d04f7a59ff8c225d23ade5fc43996 (diff)
hw: misc: tmp105: Add temperature update timerHEADmasterlinaro_training
Increase temperature regularly, to simulate temperature changes. Signed-off-by: Loic Poulain <loic.poulain@linaro.org>
-rw-r--r--hw/misc/tmp105.c23
-rw-r--r--hw/misc/tmp105.h1
2 files changed, 24 insertions, 0 deletions
diff --git a/hw/misc/tmp105.c b/hw/misc/tmp105.c
index 75ddad3a12..60edcdc017 100644
--- a/hw/misc/tmp105.c
+++ b/hw/misc/tmp105.c
@@ -26,6 +26,7 @@
#include "qapi/error.h"
#include "qapi/visitor.h"
#include "qemu/module.h"
+#include "qemu/timer.h"
static void tmp105_interrupt_update(TMP105State *s)
{
@@ -213,6 +214,7 @@ static const VMStateDescription vmstate_tmp105 = {
VMSTATE_INT16(temperature, TMP105State),
VMSTATE_INT16_ARRAY(limit, TMP105State, 2),
VMSTATE_UINT8(alarm, TMP105State),
+ VMSTATE_TIMER_PTR(timer, TMP105State),
VMSTATE_I2C_SLAVE(i2c, TMP105State),
VMSTATE_END_OF_LIST()
}
@@ -241,11 +243,32 @@ static void tmp105_realize(DeviceState *dev, Error **errp)
tmp105_reset(&s->i2c);
}
+static void tmp105_timer_tick(void *opaque)
+{
+ TMP105State *s = TMP105(opaque);
+ int64_t temp = s->temperature * 1000 / 256;
+
+ /* only increase for now from 0 to 128 */
+ temp += 250 + rand() % 1000;
+
+ if (temp >= 128000)
+ temp = 0;
+
+ s->temperature = (int16_t) (temp * 256 / 1000);
+ tmp105_alarm_update(s);
+
+ /* new tick in 500ms + 0->2000ms */
+ timer_mod(s->timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500 + rand() % 2000);
+}
+
static void tmp105_initfn(Object *obj)
{
+ TMP105State *s = TMP105(obj);
object_property_add(obj, "temperature", "int",
tmp105_get_temperature,
tmp105_set_temperature, NULL, NULL, NULL);
+ s->timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, tmp105_timer_tick, obj);
+ tmp105_timer_tick(obj);
}
static void tmp105_class_init(ObjectClass *klass, void *data)
diff --git a/hw/misc/tmp105.h b/hw/misc/tmp105.h
index 9ba05ecc9c..b279c6f286 100644
--- a/hw/misc/tmp105.h
+++ b/hw/misc/tmp105.h
@@ -42,6 +42,7 @@ typedef struct TMP105State {
int16_t limit[2];
int faults;
uint8_t alarm;
+ QEMUTimer *timer;
} TMP105State;
#endif