diff options
-rw-r--r-- | hw/misc/tmp105.c | 23 | ||||
-rw-r--r-- | hw/misc/tmp105.h | 1 |
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 |