summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTing Shen <phoenixshen@google.com>2022-03-18 17:44:19 +0800
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-03-23 08:06:08 +0000
commita1f098ecd727634a7f7e243234d659d7dc919db6 (patch)
treee3dbaf34b5bcc2f05e558c87f871037628e75c7f
parentb83014e44a3dad7ced86c8c327eaa99140d604a7 (diff)
downloadchrome-ec-a1f098ecd727634a7f7e243234d659d7dc919db6.tar.gz
util: align the IN_RANGE implementation with zephyr
The design of IN_RANGE() is slightly different between CrosEC and Zephyr's. Zephyr IN_RANGE() includes both upper and lower bound. (Ref: https://docs.zephyrproject.org/latest/reference/util/index.html#c.IN_RANGE) Update legacy code to work with CONFIG_ZEPHYR. BUG=none TEST=manually verify that all occurarence of IN_RANGE is updated BRANCH=none Signed-off-by: Ting Shen <phoenixshen@google.com> Change-Id: I67e83a92be574be96535a3fe1b14454754e32072 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3534749 Tested-by: Ting Shen <phoenixshen@chromium.org> Reviewed-by: Eric Yilun Lin <yllin@google.com> Commit-Queue: Ting Shen <phoenixshen@chromium.org>
-rw-r--r--baseboard/asurada/board_id.c2
-rw-r--r--baseboard/goroh/board_id.c2
-rw-r--r--chip/stm32/usb_hid_keyboard.c6
-rw-r--r--common/uart_buffering.c4
-rw-r--r--driver/bc12/mt6360.c6
-rw-r--r--driver/charger/rt9490.c8
-rw-r--r--include/util.h2
7 files changed, 15 insertions, 15 deletions
diff --git a/baseboard/asurada/board_id.c b/baseboard/asurada/board_id.c
index a4590f3199..642785034c 100644
--- a/baseboard/asurada/board_id.c
+++ b/baseboard/asurada/board_id.c
@@ -79,7 +79,7 @@ static int adc_value_to_numeric_id(enum adc_channel ch)
for (int i = 0; i < ARRAY_SIZE(voltage_map); i++) {
if (IN_RANGE(mv, voltage_map[i] - threshold_mv,
- voltage_map[i] + threshold_mv))
+ voltage_map[i] + threshold_mv - 1))
return i;
}
diff --git a/baseboard/goroh/board_id.c b/baseboard/goroh/board_id.c
index e90da11f57..fd2001d8a3 100644
--- a/baseboard/goroh/board_id.c
+++ b/baseboard/goroh/board_id.c
@@ -79,7 +79,7 @@ static int adc_value_to_numeric_id(enum adc_channel ch)
for (int i = 0; i < ARRAY_SIZE(voltage_map); i++) {
if (IN_RANGE(mv, voltage_map[i] - threshold_mv,
- voltage_map[i] + threshold_mv))
+ voltage_map[i] + threshold_mv - 1))
return i;
}
diff --git a/chip/stm32/usb_hid_keyboard.c b/chip/stm32/usb_hid_keyboard.c
index b4561d591f..99775fd7fb 100644
--- a/chip/stm32/usb_hid_keyboard.c
+++ b/chip/stm32/usb_hid_keyboard.c
@@ -528,7 +528,7 @@ static void hid_keyboard_feature_init(void)
for (int i = 0; i < CONFIG_USB_HID_KB_NUM_TOP_ROW_KEYS; i++) {
int key = config->action_keys[i];
- if (IN_RANGE(key, 0, ARRAY_SIZE(action_key)))
+ if (IN_RANGE(key, 0, ARRAY_SIZE(action_key) - 1))
feature_report[i] = action_key[key].usage;
}
}
@@ -636,9 +636,9 @@ static uint32_t maybe_convert_function_key(int keycode)
if (!IS_ENABLED(CONFIG_USB_HID_KEYBOARD_VIVALDI) || !config)
return 0;
- if (IN_RANGE(keycode, HID_F1, HID_F12 + 1))
+ if (IN_RANGE(keycode, HID_F1, HID_F12))
index = keycode - HID_F1;
- else if (IN_RANGE(keycode, HID_F13, HID_F15 + 1))
+ else if (IN_RANGE(keycode, HID_F13, HID_F15))
index = keycode - HID_F13 + 12;
else
return 0; /* not a function key */
diff --git a/common/uart_buffering.c b/common/uart_buffering.c
index a6798bb671..d993eab345 100644
--- a/common/uart_buffering.c
+++ b/common/uart_buffering.c
@@ -63,8 +63,8 @@ static int uart_buffer_calc_checksum(void)
void uart_init_buffer(void)
{
if (tx_checksum != uart_buffer_calc_checksum() ||
- !IN_RANGE(tx_buf_head, 0, CONFIG_UART_TX_BUF_SIZE) ||
- !IN_RANGE(tx_buf_tail, 0, CONFIG_UART_TX_BUF_SIZE)) {
+ !IN_RANGE(tx_buf_head, 0, CONFIG_UART_TX_BUF_SIZE - 1) ||
+ !IN_RANGE(tx_buf_tail, 0, CONFIG_UART_TX_BUF_SIZE - 1)) {
/*
* NOTE:
* We are here because EC cold reset or RO/RW's preserve_logs
diff --git a/driver/bc12/mt6360.c b/driver/bc12/mt6360.c
index c84cb36ff3..5addba7a0c 100644
--- a/driver/bc12/mt6360.c
+++ b/driver/bc12/mt6360.c
@@ -550,7 +550,7 @@ DECLARE_HOOK(HOOK_INIT, mt6360_led_init, HOOK_PRIO_DEFAULT);
int mt6360_led_enable(enum mt6360_led_id led_id, int enable)
{
- if (!IN_RANGE(led_id, 0, MT6360_LED_COUNT))
+ if (!IN_RANGE(led_id, 0, MT6360_LED_COUNT - 1))
return EC_ERROR_INVAL;
if (enable)
@@ -563,9 +563,9 @@ int mt6360_led_set_brightness(enum mt6360_led_id led_id, int brightness)
{
int val;
- if (!IN_RANGE(led_id, 0, MT6360_LED_COUNT))
+ if (!IN_RANGE(led_id, 0, MT6360_LED_COUNT - 1))
return EC_ERROR_INVAL;
- if (!IN_RANGE(brightness, 0, 16))
+ if (!IN_RANGE(brightness, 0, 15))
return EC_ERROR_INVAL;
RETURN_ERROR(mt6360_read8(MT6360_REG_RGB_ISINK(led_id), &val));
diff --git a/driver/charger/rt9490.c b/driver/charger/rt9490.c
index df12e141b9..42428458b5 100644
--- a/driver/charger/rt9490.c
+++ b/driver/charger/rt9490.c
@@ -149,7 +149,7 @@ static enum ec_error_list rt9490_set_current(int chgnum, int current)
if (current == 0)
current = info->current_min;
- if (!IN_RANGE(current, info->current_min, info->current_max + 1))
+ if (!IN_RANGE(current, info->current_min, info->current_max))
return EC_ERROR_PARAM2;
reg_ichg = current / info->current_step;
@@ -178,7 +178,7 @@ static enum ec_error_list rt9490_set_voltage(int chgnum, int voltage)
if (voltage == 0)
voltage = info->voltage_min;
- if (!IN_RANGE(voltage, info->voltage_min, info->voltage_max + 1))
+ if (!IN_RANGE(voltage, info->voltage_min, info->voltage_max))
return EC_ERROR_PARAM2;
reg_cv = voltage / info->voltage_step;
@@ -198,9 +198,9 @@ enum ec_error_list rt9490_set_otg_current_voltage(int chgnum,
{
uint16_t reg_cur, reg_vol;
- if (!IN_RANGE(output_current, RT9490_IOTG_MIN, RT9490_IOTG_MAX + 1))
+ if (!IN_RANGE(output_current, RT9490_IOTG_MIN, RT9490_IOTG_MAX))
return EC_ERROR_PARAM2;
- if (!IN_RANGE(output_voltage, RT9490_VOTG_MIN, RT9490_VOTG_MAX + 1))
+ if (!IN_RANGE(output_voltage, RT9490_VOTG_MIN, RT9490_VOTG_MAX))
return EC_ERROR_PARAM3;
reg_cur = (output_current - RT9490_IOTG_MIN) / RT9490_IOTG_STEP;
diff --git a/include/util.h b/include/util.h
index ed1961e887..3601ca6668 100644
--- a/include/util.h
+++ b/include/util.h
@@ -77,7 +77,7 @@ extern "C" {
/* Macro to check if the value is in range */
#ifndef CONFIG_ZEPHYR
-#define IN_RANGE(x, min, max) ((x) >= (min) && (x) < (max))
+#define IN_RANGE(x, min, max) ((x) >= (min) && (x) <= (max))
#endif
/*