summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/isl923x/src/console_cmd_amon_bmon.c
blob: f8cd76d524648ba8094c73cdcffb7d0e521739cd (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* Copyright 2022 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/drivers/adc.h>
#include <zephyr/drivers/adc/adc_emul.h>
#include <zephyr/shell/shell.h>
#include <zephyr/ztest.h>

#include "adc.h"
#include "console.h"
#include "driver/charger/isl923x.h"
#include "driver/charger/isl923x_public.h"
#include "ec_commands.h"
#include "emul/emul_common_i2c.h"
#include "emul/emul_isl923x.h"
#include "test/drivers/charger_utils.h"
#include "test/drivers/test_state.h"
#include "test/drivers/utils.h"

#define ADC_DEVICE_NODE DT_NODELABEL(adc0)
#define CHARGER_NUM get_charger_num(&isl923x_drv)
#define ISL923X_EMUL EMUL_DT_GET(DT_NODELABEL(isl923x_emul))

ZTEST_SUITE(console_cmd_amon_bmon, drivers_predicate_post_main, NULL, NULL,
	    NULL, NULL);

ZTEST(console_cmd_amon_bmon, test_isl923x_amonbmon_shell_cmd)
{
	/* Validate combinations of well formed shell commands */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "amonbmon a 0"));
	zassert_ok(shell_execute_cmd(get_ec_shell(), "amonbmon ac 0"));
	zassert_ok(shell_execute_cmd(get_ec_shell(), "amonbmon ad 0"));

	zassert_ok(shell_execute_cmd(get_ec_shell(), "amonbmon b 0"));
	zassert_ok(shell_execute_cmd(get_ec_shell(), "amonbmon bc 0"));
	zassert_ok(shell_execute_cmd(get_ec_shell(), "amonbmon bd 0"));

	/* Check error returned for malformed shell command */
	zassert_equal(EC_ERROR_PARAM2,
		      shell_execute_cmd(get_ec_shell(), "amonbmon a x"), NULL);
}

ZTEST(console_cmd_amon_bmon, test_isl923x_amonbmon_get_input_current)
{
	const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE);
	const uint16_t input_mv = 1000;
	int current_milli_amps, ret;

	ret = adc_emul_const_value_set(adc_dev, ADC_AMON_BMON, input_mv);
	zassert_ok(ret, "adc_emul_const_value_set() failed with code %d", ret);

	zassert_ok(isl923x_drv.get_input_current(CHARGER_NUM,
						 &current_milli_amps),
		   NULL);
	zassert_within(current_milli_amps, 5000, 10,
		       "Expected input current %dmA but got %dmA", 5000,
		       current_milli_amps);
}

ZTEST(console_cmd_amon_bmon,
      test_isl923x_amonbmon_get_input_current_read_fail_req1)
{
	const struct emul *isl923x_emul = ISL923X_EMUL;
	struct i2c_common_emul_data *common_data =
		emul_isl923x_get_i2c_common_data(isl923x_emul);
	int current_milli_amps;

	i2c_common_emul_set_read_fail_reg(common_data, ISL923X_REG_CONTROL1);
	zassert_equal(EC_ERROR_INVAL,
		      isl923x_drv.get_input_current(CHARGER_NUM,
						    &current_milli_amps),
		      NULL);
	zassert_equal(EC_ERROR_INVAL,
		      shell_execute_cmd(get_ec_shell(), "amonbmon a 0"), NULL);
}

ZTEST(console_cmd_amon_bmon,
      test_isl923x_amonbmon_get_input_current_read_fail_req3)
{
	const struct emul *isl923x_emul = ISL923X_EMUL;
	struct i2c_common_emul_data *common_data =
		emul_isl923x_get_i2c_common_data(isl923x_emul);
	int current_milli_amps;

	i2c_common_emul_set_read_fail_reg(common_data, ISL9238_REG_CONTROL3);
	zassert_equal(EC_ERROR_INVAL,
		      isl923x_drv.get_input_current(CHARGER_NUM,
						    &current_milli_amps),
		      NULL);
}

ZTEST(console_cmd_amon_bmon,
      test_isl923x_amonbmon_get_input_current_write_fail_req1)
{
	const struct emul *isl923x_emul = ISL923X_EMUL;
	struct i2c_common_emul_data *common_data =
		emul_isl923x_get_i2c_common_data(isl923x_emul);
	int current_milli_amps;

	i2c_common_emul_set_write_fail_reg(common_data, ISL923X_REG_CONTROL1);
	zassert_equal(EC_ERROR_INVAL,
		      isl923x_drv.get_input_current(CHARGER_NUM,
						    &current_milli_amps),
		      NULL);
}

ZTEST(console_cmd_amon_bmon,
      test_isl923x_amonbmon_get_input_current_write_fail_req3)
{
	const struct emul *isl923x_emul = ISL923X_EMUL;
	struct i2c_common_emul_data *common_data =
		emul_isl923x_get_i2c_common_data(isl923x_emul);
	int current_milli_amps;

	i2c_common_emul_set_write_fail_reg(common_data, ISL9238_REG_CONTROL3);
	zassert_equal(EC_ERROR_INVAL,
		      isl923x_drv.get_input_current(CHARGER_NUM,
						    &current_milli_amps),
		      NULL);
}