summaryrefslogtreecommitdiff
path: root/zephyr/test/i2c/src/main.c
blob: 364353f06d67f092af7ab6cd22bfd6e46b0df8cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* Copyright 2020 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <zephyr/devicetree.h>
#include <zephyr/ztest.h>

#include "common.h"
#include "i2c/i2c.h"
#include "i2c.h"

/* Unused: required for shimming i2c. */
void watchdog_reload(void)
{
}

static void test_i2c_port_count(void)
{
	zassert_equal(I2C_PORT_COUNT, 2,
		      "I2C_PORT_COUNT expected to be 2 but was %d",
		      I2C_PORT_COUNT);
}

static void test_i2c_lock(void)
{
	i2c_lock(I2C_PORT_ACCEL, 1);
	zassert_equal(i2c_port_is_locked(I2C_PORT_EEPROM), 1,
		      "I2C_PORT_EEPROM not locked");
	zassert_equal(i2c_port_is_locked(I2C_PORT_ACCEL), 1,
		      "I2C_PORT_ACCEL not locked");

	/* Unlock different enum pointing the same i2c device */
	i2c_lock(I2C_PORT_EEPROM, 0);
	zassert_equal(i2c_port_is_locked(I2C_PORT_EEPROM), 0,
		      "I2C_PORT_EEPROM not locked");
	zassert_equal(i2c_port_is_locked(I2C_PORT_ACCEL), 0,
		      "I2C_PORT_ACCEL not locked");

	i2c_lock(I2C_PORT_EEPROM, 1);
	/* Verify different i2c device */
	zassert_equal(i2c_port_is_locked(I2C_PORT_USB_C1), 0,
		      "I2C_PORT_USB_C1 locked");

	i2c_lock(I2C_PORT_USB_C1, 1);
	/* Make sure i2c device is locked*/
	zassert_equal(i2c_port_is_locked(I2C_PORT_USB_C1), 1,
		      "I2C_PORT_USB_C1 locked");

	/* Another i2c device is still locked */
	i2c_lock(I2C_PORT_USB_C1, 0);
	zassert_equal(i2c_port_is_locked(I2C_PORT_EEPROM), 1,
		      "I2C_PORT_EEPROM not locked");
	i2c_lock(I2C_PORT_EEPROM, 0);
}

/* Test case main entry. */
void test_main(void)
{
	ztest_test_suite(test_i2c, ztest_user_unit_test(test_i2c_port_count),
			 ztest_user_unit_test(test_i2c_lock));
	ztest_run_test_suite(test_i2c);
}