pca9552-test.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * QTest testcase for the PCA9552 LED blinker
  3. *
  4. * Copyright (c) 2017-2018, IBM Corporation.
  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.h"
  11. #include "libqos/qgraph.h"
  12. #include "libqos/i2c.h"
  13. #include "hw/misc/pca9552_regs.h"
  14. #define PCA9552_TEST_ID "pca9552-test"
  15. #define PCA9552_TEST_ADDR 0x60
  16. static void pca9552_init(QI2CDevice *i2cdev)
  17. {
  18. /* Switch on LEDs 0 and 12 */
  19. i2c_set8(i2cdev, PCA9552_LS0, 0x54);
  20. i2c_set8(i2cdev, PCA9552_LS3, 0x54);
  21. }
  22. static void receive_autoinc(void *obj, void *data, QGuestAllocator *alloc)
  23. {
  24. QI2CDevice *i2cdev = (QI2CDevice *)obj;
  25. uint8_t resp;
  26. uint8_t reg = PCA9552_LS0 | PCA9552_AUTOINC;
  27. pca9552_init(i2cdev);
  28. i2c_send(i2cdev, &reg, 1);
  29. /* PCA9552_LS0 */
  30. i2c_recv(i2cdev, &resp, 1);
  31. g_assert_cmphex(resp, ==, 0x54);
  32. /* PCA9552_LS1 */
  33. i2c_recv(i2cdev, &resp, 1);
  34. g_assert_cmphex(resp, ==, 0x55);
  35. /* PCA9552_LS2 */
  36. i2c_recv(i2cdev, &resp, 1);
  37. g_assert_cmphex(resp, ==, 0x55);
  38. /* PCA9552_LS3 */
  39. i2c_recv(i2cdev, &resp, 1);
  40. g_assert_cmphex(resp, ==, 0x54);
  41. }
  42. static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
  43. {
  44. QI2CDevice *i2cdev = (QI2CDevice *)obj;
  45. uint8_t value;
  46. value = i2c_get8(i2cdev, PCA9552_LS0);
  47. g_assert_cmphex(value, ==, 0x55);
  48. value = i2c_get8(i2cdev, PCA9552_INPUT0);
  49. g_assert_cmphex(value, ==, 0x0);
  50. pca9552_init(i2cdev);
  51. value = i2c_get8(i2cdev, PCA9552_LS0);
  52. g_assert_cmphex(value, ==, 0x54);
  53. value = i2c_get8(i2cdev, PCA9552_INPUT0);
  54. g_assert_cmphex(value, ==, 0x01);
  55. value = i2c_get8(i2cdev, PCA9552_LS3);
  56. g_assert_cmphex(value, ==, 0x54);
  57. value = i2c_get8(i2cdev, PCA9552_INPUT1);
  58. g_assert_cmphex(value, ==, 0x10);
  59. }
  60. static void pca9552_register_nodes(void)
  61. {
  62. QOSGraphEdgeOptions opts = {
  63. .extra_device_opts = "address=0x60"
  64. };
  65. add_qi2c_address(&opts, &(QI2CAddress) { 0x60 });
  66. qos_node_create_driver("pca9552", i2c_device_create);
  67. qos_node_consumes("pca9552", "i2c-bus", &opts);
  68. qos_add_test("tx-rx", "pca9552", send_and_receive, NULL);
  69. qos_add_test("rx-autoinc", "pca9552", receive_autoinc, NULL);
  70. }
  71. libqos_init(pca9552_register_nodes);