Jean-Christophe Dubois | 7f39862 | 2015-09-07 10:39:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QTest testcase for the DS1338 RTC |
| 3 | * |
| 4 | * Copyright (c) 2013 Jean-Christophe Dubois |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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/>. |
| 18 | */ |
| 19 | |
Peter Maydell | 681c28a | 2016-02-08 18:08:51 +0000 | [diff] [blame] | 20 | #include "qemu/osdep.h" |
Jean-Christophe Dubois | 7f39862 | 2015-09-07 10:39:31 +0100 | [diff] [blame] | 21 | #include "libqtest.h" |
| 22 | #include "libqos/i2c.h" |
| 23 | |
Jean-Christophe Dubois | 7f39862 | 2015-09-07 10:39:31 +0100 | [diff] [blame] | 24 | #define IMX25_I2C_0_BASE 0x43F80000 |
| 25 | |
| 26 | #define DS1338_ADDR 0x68 |
| 27 | |
| 28 | static I2CAdapter *i2c; |
| 29 | static uint8_t addr; |
| 30 | |
| 31 | static inline uint8_t bcd2bin(uint8_t x) |
| 32 | { |
| 33 | return ((x) & 0x0f) + ((x) >> 4) * 10; |
| 34 | } |
| 35 | |
| 36 | static void send_and_receive(void) |
| 37 | { |
| 38 | uint8_t cmd[1]; |
| 39 | uint8_t resp[7]; |
| 40 | time_t now = time(NULL); |
| 41 | struct tm *tm_ptr = gmtime(&now); |
| 42 | |
| 43 | /* reset the index in the RTC memory */ |
| 44 | cmd[0] = 0; |
| 45 | i2c_send(i2c, addr, cmd, 1); |
| 46 | |
| 47 | /* retrieve the date */ |
| 48 | i2c_recv(i2c, addr, resp, 7); |
| 49 | |
| 50 | /* check retrieved time againt local time */ |
| 51 | g_assert_cmpuint(bcd2bin(resp[4]), == , tm_ptr->tm_mday); |
| 52 | g_assert_cmpuint(bcd2bin(resp[5]), == , 1 + tm_ptr->tm_mon); |
| 53 | g_assert_cmpuint(2000 + bcd2bin(resp[6]), == , 1900 + tm_ptr->tm_year); |
| 54 | } |
| 55 | |
| 56 | int main(int argc, char **argv) |
| 57 | { |
| 58 | QTestState *s = NULL; |
| 59 | int ret; |
| 60 | |
| 61 | g_test_init(&argc, &argv, NULL); |
| 62 | |
Peter Crosthwaite | b64d64d | 2015-10-16 11:14:53 +0100 | [diff] [blame] | 63 | s = qtest_start("-display none -machine imx25-pdk"); |
Jean-Christophe Dubois | 7f39862 | 2015-09-07 10:39:31 +0100 | [diff] [blame] | 64 | i2c = imx_i2c_create(IMX25_I2C_0_BASE); |
| 65 | addr = DS1338_ADDR; |
| 66 | |
| 67 | qtest_add_func("/ds1338/tx-rx", send_and_receive); |
| 68 | |
| 69 | ret = g_test_run(); |
| 70 | |
| 71 | if (s) { |
| 72 | qtest_quit(s); |
| 73 | } |
| 74 | g_free(i2c); |
| 75 | |
| 76 | return ret; |
| 77 | } |