1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "qemu/osdep.h"
21#include "libqtest.h"
22#include "libqos/i2c.h"
23
24#include <glib.h>
25
26#define IMX25_I2C_0_BASE 0x43F80000
27
28#define DS1338_ADDR 0x68
29
30static I2CAdapter *i2c;
31static uint8_t addr;
32
33static inline uint8_t bcd2bin(uint8_t x)
34{
35 return ((x) & 0x0f) + ((x) >> 4) * 10;
36}
37
38static void send_and_receive(void)
39{
40 uint8_t cmd[1];
41 uint8_t resp[7];
42 time_t now = time(NULL);
43 struct tm *tm_ptr = gmtime(&now);
44
45
46 cmd[0] = 0;
47 i2c_send(i2c, addr, cmd, 1);
48
49
50 i2c_recv(i2c, addr, resp, 7);
51
52
53 g_assert_cmpuint(bcd2bin(resp[4]), == , tm_ptr->tm_mday);
54 g_assert_cmpuint(bcd2bin(resp[5]), == , 1 + tm_ptr->tm_mon);
55 g_assert_cmpuint(2000 + bcd2bin(resp[6]), == , 1900 + tm_ptr->tm_year);
56}
57
58int main(int argc, char **argv)
59{
60 QTestState *s = NULL;
61 int ret;
62
63 g_test_init(&argc, &argv, NULL);
64
65 s = qtest_start("-display none -machine imx25-pdk");
66 i2c = imx_i2c_create(IMX25_I2C_0_BASE);
67 addr = DS1338_ADDR;
68
69 qtest_add_func("/ds1338/tx-rx", send_and_receive);
70
71 ret = g_test_run();
72
73 if (s) {
74 qtest_quit(s);
75 }
76 g_free(i2c);
77
78 return ret;
79}
80