gt64120: convert to realize()
[qemu/cris-port.git] / tests / ds1338-test.c
blob7d513d89724c273fba27621ff0479953d980f09d
1 /*
2 * QTest testcase for the DS1338 RTC
4 * Copyright (c) 2013 Jean-Christophe Dubois
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "libqtest.h"
21 #include "libqos/i2c.h"
23 #include <glib.h>
25 #define IMX25_I2C_0_BASE 0x43F80000
27 #define DS1338_ADDR 0x68
29 static I2CAdapter *i2c;
30 static uint8_t addr;
32 static inline uint8_t bcd2bin(uint8_t x)
34 return ((x) & 0x0f) + ((x) >> 4) * 10;
37 static void send_and_receive(void)
39 uint8_t cmd[1];
40 uint8_t resp[7];
41 time_t now = time(NULL);
42 struct tm *tm_ptr = gmtime(&now);
44 /* reset the index in the RTC memory */
45 cmd[0] = 0;
46 i2c_send(i2c, addr, cmd, 1);
48 /* retrieve the date */
49 i2c_recv(i2c, addr, resp, 7);
51 /* check retrieved time againt local time */
52 g_assert_cmpuint(bcd2bin(resp[4]), == , tm_ptr->tm_mday);
53 g_assert_cmpuint(bcd2bin(resp[5]), == , 1 + tm_ptr->tm_mon);
54 g_assert_cmpuint(2000 + bcd2bin(resp[6]), == , 1900 + tm_ptr->tm_year);
57 int main(int argc, char **argv)
59 QTestState *s = NULL;
60 int ret;
62 g_test_init(&argc, &argv, NULL);
64 s = qtest_start("-display none -machine imx25-pdk");
65 i2c = imx_i2c_create(IMX25_I2C_0_BASE);
66 addr = DS1338_ADDR;
68 qtest_add_func("/ds1338/tx-rx", send_and_receive);
70 ret = g_test_run();
72 if (s) {
73 qtest_quit(s);
75 g_free(i2c);
77 return ret;