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.
10 #include "qemu/osdep.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
)
24 i2c_send(i2c
, addr
, ®
, 1);
25 i2c_recv(i2c
, addr
, resp
, 1);
29 static void pca9552_set8(I2CAdapter
*i2c
, uint8_t addr
, uint8_t reg
,
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)
45 uint8_t reg
= PCA9552_LS0
| PCA9552_AUTOINC
;
47 i2c_send(i2c
, PCA9552_TEST_ADDR
, ®
, 1);
50 i2c_recv(i2c
, PCA9552_TEST_ADDR
, &resp
, 1);
51 g_assert_cmphex(resp
, ==, 0x54);
54 i2c_recv(i2c
, PCA9552_TEST_ADDR
, &resp
, 1);
55 g_assert_cmphex(resp
, ==, 0x55);
58 i2c_recv(i2c
, PCA9552_TEST_ADDR
, &resp
, 1);
59 g_assert_cmphex(resp
, ==, 0x55);
62 i2c_recv(i2c
, PCA9552_TEST_ADDR
, &resp
, 1);
63 g_assert_cmphex(resp
, ==, 0x54);
66 static void send_and_receive(void)
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);
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
)
98 g_test_init(&argc
, &argv
, NULL
);
100 s
= qtest_start("-machine n800 "
101 "-device pca9552,bus=i2c-bus.0,id=" PCA9552_TEST_ID
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
);