qapi: add 'if' to alternate members
[qemu/ar7.git] / tests / pca9552-test.c
blob5466a67ed700e4d3cff531c3c0d32578d1b023e6
1 /*
2 * QTest testcase for the PCA9552 LED blinker
4 * Copyright (c) 2017-2018, IBM Corporation.
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 */
10 #include "qemu/osdep.h"
12 #include "libqtest.h"
13 #include "libqos/i2c.h"
14 #include "hw/misc/pca9552_regs.h"
16 #define PCA9552_TEST_ID "pca9552-test"
17 #define PCA9552_TEST_ADDR 0x60
19 static I2CAdapter *i2c;
21 static uint8_t pca9552_get8(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
23 uint8_t resp[1];
24 i2c_send(i2c, addr, &reg, 1);
25 i2c_recv(i2c, addr, resp, 1);
26 return resp[0];
29 static void pca9552_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
30 uint8_t value)
32 uint8_t cmd[2];
33 uint8_t resp[1];
35 cmd[0] = reg;
36 cmd[1] = value;
37 i2c_send(i2c, addr, cmd, 2);
38 i2c_recv(i2c, addr, resp, 1);
39 g_assert_cmphex(resp[0], ==, cmd[1]);
42 static void receive_autoinc(void)
44 uint8_t resp;
45 uint8_t reg = PCA9552_LS0 | PCA9552_AUTOINC;
47 i2c_send(i2c, PCA9552_TEST_ADDR, &reg, 1);
49 /* PCA9552_LS0 */
50 i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
51 g_assert_cmphex(resp, ==, 0x54);
53 /* PCA9552_LS1 */
54 i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
55 g_assert_cmphex(resp, ==, 0x55);
57 /* PCA9552_LS2 */
58 i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
59 g_assert_cmphex(resp, ==, 0x55);
61 /* PCA9552_LS3 */
62 i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
63 g_assert_cmphex(resp, ==, 0x54);
66 static void send_and_receive(void)
68 uint8_t value;
70 value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0);
71 g_assert_cmphex(value, ==, 0x55);
73 value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0);
74 g_assert_cmphex(value, ==, 0x0);
76 /* Switch on LED 0 */
77 pca9552_set8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0, 0x54);
78 value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0);
79 g_assert_cmphex(value, ==, 0x54);
81 value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0);
82 g_assert_cmphex(value, ==, 0x01);
84 /* Switch on LED 12 */
85 pca9552_set8(i2c, PCA9552_TEST_ADDR, PCA9552_LS3, 0x54);
86 value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS3);
87 g_assert_cmphex(value, ==, 0x54);
89 value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT1);
90 g_assert_cmphex(value, ==, 0x10);
93 int main(int argc, char **argv)
95 QTestState *s = NULL;
96 int ret;
98 g_test_init(&argc, &argv, NULL);
100 s = qtest_start("-machine n800 "
101 "-device pca9552,bus=i2c-bus.0,id=" PCA9552_TEST_ID
102 ",address=0x60");
103 i2c = omap_i2c_create(s, OMAP2_I2C_1_BASE);
105 qtest_add_func("/pca9552/tx-rx", send_and_receive);
106 qtest_add_func("/pca9552/rx-autoinc", receive_autoinc);
108 ret = g_test_run();
110 if (s) {
111 qtest_quit(s);
113 g_free(i2c);
115 return ret;