qemu/tests/pca9552-test.c
<<
>>
Prefs
   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
  10#include "qemu/osdep.h"
  11
  12#include "libqtest.h"
  13#include "libqos/i2c.h"
  14#include "hw/misc/pca9552_regs.h"
  15
  16#define PCA9552_TEST_ID   "pca9552-test"
  17#define PCA9552_TEST_ADDR 0x60
  18
  19static I2CAdapter *i2c;
  20
  21static uint8_t pca9552_get8(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
  22{
  23    uint8_t resp[1];
  24    i2c_send(i2c, addr, &reg, 1);
  25    i2c_recv(i2c, addr, resp, 1);
  26    return resp[0];
  27}
  28
  29static void pca9552_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
  30                         uint8_t value)
  31{
  32    uint8_t cmd[2];
  33    uint8_t resp[1];
  34
  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]);
  40}
  41
  42static void receive_autoinc(void)
  43{
  44    uint8_t resp;
  45    uint8_t reg = PCA9552_LS0 | PCA9552_AUTOINC;
  46
  47    i2c_send(i2c, PCA9552_TEST_ADDR, &reg, 1);
  48
  49    /* PCA9552_LS0 */
  50    i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
  51    g_assert_cmphex(resp, ==, 0x54);
  52
  53    /* PCA9552_LS1 */
  54    i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
  55    g_assert_cmphex(resp, ==, 0x55);
  56
  57    /* PCA9552_LS2 */
  58    i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
  59    g_assert_cmphex(resp, ==, 0x55);
  60
  61    /* PCA9552_LS3 */
  62    i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
  63    g_assert_cmphex(resp, ==, 0x54);
  64}
  65
  66static void send_and_receive(void)
  67{
  68    uint8_t value;
  69
  70    value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0);
  71    g_assert_cmphex(value, ==, 0x55);
  72
  73    value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0);
  74    g_assert_cmphex(value, ==, 0x0);
  75
  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);
  80
  81    value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0);
  82    g_assert_cmphex(value, ==, 0x01);
  83
  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);
  88
  89    value = pca9552_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT1);
  90    g_assert_cmphex(value, ==, 0x10);
  91}
  92
  93int main(int argc, char **argv)
  94{
  95    QTestState *s = NULL;
  96    int ret;
  97
  98    g_test_init(&argc, &argv, NULL);
  99
 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);
 104
 105    qtest_add_func("/pca9552/tx-rx", send_and_receive);
 106    qtest_add_func("/pca9552/rx-autoinc", receive_autoinc);
 107
 108    ret = g_test_run();
 109
 110    if (s) {
 111        qtest_quit(s);
 112    }
 113    g_free(i2c);
 114
 115    return ret;
 116}
 117