2
0

tmp105-test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * QTest testcase for the TMP105 temperature sensor
  3. *
  4. * Copyright (c) 2012 Andreas Färber
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "libqtest-single.h"
  11. #include "libqos/qgraph.h"
  12. #include "libqos/i2c.h"
  13. #include "qapi/qmp/qdict.h"
  14. #include "hw/misc/tmp105_regs.h"
  15. #define TMP105_TEST_ID "tmp105-test"
  16. #define TMP105_TEST_ADDR 0x49
  17. static int qmp_tmp105_get_temperature(const char *id)
  18. {
  19. QDict *response;
  20. int ret;
  21. response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
  22. "'property': 'temperature' } }", id);
  23. g_assert(qdict_haskey(response, "return"));
  24. ret = qdict_get_int(response, "return");
  25. qobject_unref(response);
  26. return ret;
  27. }
  28. static void qmp_tmp105_set_temperature(const char *id, int value)
  29. {
  30. QDict *response;
  31. response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
  32. "'property': 'temperature', 'value': %d } }", id, value);
  33. g_assert(qdict_haskey(response, "return"));
  34. qobject_unref(response);
  35. }
  36. #define TMP105_PRECISION (1000/16)
  37. static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
  38. {
  39. uint16_t value;
  40. QI2CDevice *i2cdev = (QI2CDevice *)obj;
  41. value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
  42. g_assert_cmpuint(value, ==, 0);
  43. value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
  44. g_assert_cmphex(value, ==, 0);
  45. qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000);
  46. value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
  47. g_assert_cmpuint(value, ==, 20000);
  48. value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
  49. g_assert_cmphex(value, ==, 0x1400);
  50. qmp_tmp105_set_temperature(TMP105_TEST_ID, 20938); /* 20 + 15/16 */
  51. value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
  52. g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
  53. g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
  54. /* Set config */
  55. i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x60);
  56. value = i2c_get8(i2cdev, TMP105_REG_CONFIG);
  57. g_assert_cmphex(value, ==, 0x60);
  58. value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
  59. g_assert_cmphex(value, ==, 0x14f0);
  60. /* Set precision to 9, 10, 11 bits. */
  61. i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x00);
  62. g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x00);
  63. value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
  64. g_assert_cmphex(value, ==, 0x1480);
  65. i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x20);
  66. g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x20);
  67. value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
  68. g_assert_cmphex(value, ==, 0x14c0);
  69. i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x40);
  70. g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x40);
  71. value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
  72. g_assert_cmphex(value, ==, 0x14e0);
  73. /* stored precision remains the same */
  74. value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
  75. g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
  76. g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
  77. i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x60);
  78. g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x60);
  79. value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
  80. g_assert_cmphex(value, ==, 0x14f0);
  81. i2c_set16(i2cdev, TMP105_REG_T_LOW, 0x1234);
  82. g_assert_cmphex(i2c_get16(i2cdev, TMP105_REG_T_LOW), ==, 0x1234);
  83. i2c_set16(i2cdev, TMP105_REG_T_HIGH, 0x4231);
  84. g_assert_cmphex(i2c_get16(i2cdev, TMP105_REG_T_HIGH), ==, 0x4231);
  85. }
  86. static void tmp105_register_nodes(void)
  87. {
  88. QOSGraphEdgeOptions opts = {
  89. .extra_device_opts = "id=" TMP105_TEST_ID ",address=0x49"
  90. };
  91. add_qi2c_address(&opts, &(QI2CAddress) { 0x49 });
  92. qos_node_create_driver("tmp105", i2c_device_create);
  93. qos_node_consumes("tmp105", "i2c-bus", &opts);
  94. qos_add_test("tx-rx", "tmp105", send_and_receive, NULL);
  95. }
  96. libqos_init(tmp105_register_nodes);