summaryrefslogtreecommitdiff
path: root/zephyr/test/i2c/src/main.c
blob: 759fe0115160b18fd86eb3ef03ea151106191688 (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
64
65
66
67
68
/* 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 "common.h"
#include "i2c.h"
#include "i2c/i2c.h"

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

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

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

ZTEST_USER(i2c, test_i2c_lock_invalid_port)
{
	i2c_lock(-1, 1);
	zassert_equal(i2c_port_is_locked(-1), 0,
		      "Negative I2C port locked, but should have failed");

	i2c_lock(INT_MAX, 1);
	zassert_equal(i2c_port_is_locked(INT_MAX), 0,
		      "MAX_INT I2C port locked, but should have failed");
}

ZTEST_USER(i2c, test_i2c_lock)
{
	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);
}

ZTEST_SUITE(i2c, NULL, NULL, NULL, NULL, NULL);