diff options
Diffstat (limited to 'hw/tmp105.c')
-rw-r--r-- | hw/tmp105.c | 77 |
1 files changed, 46 insertions, 31 deletions
diff --git a/hw/tmp105.c b/hw/tmp105.c index 0ade4eb..3ad2d2f 100644 --- a/hw/tmp105.c +++ b/hw/tmp105.c @@ -21,20 +21,7 @@ #include "hw.h" #include "i2c.h" #include "tmp105.h" - -typedef struct { - I2CSlave i2c; - uint8_t len; - uint8_t buf[2]; - qemu_irq pin; - - uint8_t pointer; - uint8_t config; - int16_t temperature; - int16_t limit[2]; - int faults; - uint8_t alarm; -} TMP105State; +#include "qapi/visitor.h" static void tmp105_interrupt_update(TMP105State *s) { @@ -65,15 +52,30 @@ static void tmp105_alarm_update(TMP105State *s) tmp105_interrupt_update(s); } +static void tmp105_get_temperature(Object *obj, Visitor *v, void *opaque, + const char *name, Error **errp) +{ + TMP105State *s = TMP105(obj); + int64_t value = s->temperature; + + visit_type_int(v, &value, name, errp); +} + /* Units are 0.001 centigrades relative to 0 C. */ -void tmp105_set(I2CSlave *i2c, int temp) +static void tmp105_set_temperature(Object *obj, Visitor *v, void *opaque, + const char *name, Error **errp) { - TMP105State *s = (TMP105State *) i2c; + TMP105State *s = TMP105(obj); + int64_t temp; + visit_type_int(v, &temp, name, errp); + if (error_is_set(errp)) { + return; + } if (temp >= 128000 || temp < -128000) { - fprintf(stderr, "%s: values is out of range (%i.%03i C)\n", - __FUNCTION__, temp / 1000, temp % 1000); - exit(-1); + error_setg(errp, "value %" PRId64 ".%03" PRIu64 " °C is out of range", + temp / 1000, temp % 1000); + return; } s->temperature = ((int16_t) (temp * 0x800 / 128000)) << 4; @@ -141,23 +143,27 @@ static void tmp105_write(TMP105State *s) static int tmp105_rx(I2CSlave *i2c) { - TMP105State *s = (TMP105State *) i2c; + TMP105State *s = TMP105(i2c); - if (s->len < 2) + if (s->len < 2) { return s->buf[s->len ++]; - else + } else { return 0xff; + } } static int tmp105_tx(I2CSlave *i2c, uint8_t data) { - TMP105State *s = (TMP105State *) i2c; + TMP105State *s = TMP105(i2c); - if (!s->len ++) + if (s->len == 0) { s->pointer = data; - else { - if (s->len <= 2) + s->len++; + } else { + if (s->len <= 2) { s->buf[s->len - 1] = data; + } + s->len++; tmp105_write(s); } @@ -166,10 +172,11 @@ static int tmp105_tx(I2CSlave *i2c, uint8_t data) static void tmp105_event(I2CSlave *i2c, enum i2c_event event) { - TMP105State *s = (TMP105State *) i2c; + TMP105State *s = TMP105(i2c); - if (event == I2C_START_RECV) + if (event == I2C_START_RECV) { tmp105_read(s); + } s->len = 0; } @@ -205,7 +212,7 @@ static const VMStateDescription vmstate_tmp105 = { static void tmp105_reset(I2CSlave *i2c) { - TMP105State *s = (TMP105State *) i2c; + TMP105State *s = TMP105(i2c); s->temperature = 0; s->pointer = 0; @@ -218,7 +225,7 @@ static void tmp105_reset(I2CSlave *i2c) static int tmp105_init(I2CSlave *i2c) { - TMP105State *s = FROM_I2C_SLAVE(TMP105State, i2c); + TMP105State *s = TMP105(i2c); qdev_init_gpio_out(&i2c->qdev, &s->pin, 1); @@ -227,6 +234,13 @@ static int tmp105_init(I2CSlave *i2c) return 0; } +static void tmp105_initfn(Object *obj) +{ + object_property_add(obj, "temperature", "int", + tmp105_get_temperature, + tmp105_set_temperature, NULL, NULL, NULL); +} + static void tmp105_class_init(ObjectClass *klass, void *data) { DeviceClass *dc = DEVICE_CLASS(klass); @@ -240,9 +254,10 @@ static void tmp105_class_init(ObjectClass *klass, void *data) } static const TypeInfo tmp105_info = { - .name = "tmp105", + .name = TYPE_TMP105, .parent = TYPE_I2C_SLAVE, .instance_size = sizeof(TMP105State), + .instance_init = tmp105_initfn, .class_init = tmp105_class_init, }; |