summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'zephyr/test/drivers')
-rw-r--r--zephyr/test/drivers/CMakeLists.txt17
-rw-r--r--zephyr/test/drivers/README.md50
-rw-r--r--zephyr/test/drivers/include/gpio_map.h25
-rw-r--r--zephyr/test/drivers/include/stubs.h10
-rw-r--r--zephyr/test/drivers/overlay.dts561
-rw-r--r--zephyr/test/drivers/prj.conf90
-rw-r--r--zephyr/test/drivers/src/battery.c35
-rw-r--r--zephyr/test/drivers/src/bb_retimer.c528
-rw-r--r--zephyr/test/drivers/src/bc12.c280
-rw-r--r--zephyr/test/drivers/src/bma2x2.c926
-rw-r--r--zephyr/test/drivers/src/bmi160.c1873
-rw-r--r--zephyr/test/drivers/src/bmi260.c1864
-rw-r--r--zephyr/test/drivers/src/espi.c35
-rw-r--r--zephyr/test/drivers/src/lis2dw12.c119
-rw-r--r--zephyr/test/drivers/src/ln9310.c50
-rw-r--r--zephyr/test/drivers/src/main.c50
-rw-r--r--zephyr/test/drivers/src/ppc.c84
-rw-r--r--zephyr/test/drivers/src/smart.c281
-rw-r--r--zephyr/test/drivers/src/stm_mems_common.c56
-rw-r--r--zephyr/test/drivers/src/stubs.c233
-rw-r--r--zephyr/test/drivers/src/tcs3400.c622
-rw-r--r--zephyr/test/drivers/src/temp_sensor.c139
-rw-r--r--zephyr/test/drivers/src/thermistor.c295
-rw-r--r--zephyr/test/drivers/zmake.yaml14
24 files changed, 0 insertions, 8237 deletions
diff --git a/zephyr/test/drivers/CMakeLists.txt b/zephyr/test/drivers/CMakeLists.txt
deleted file mode 100644
index c90b334e60..0000000000
--- a/zephyr/test/drivers/CMakeLists.txt
+++ /dev/null
@@ -1,17 +0,0 @@
-# Copyright 2021 The Chromium OS Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style license that can be
-# found in the LICENSE file.
-
-cmake_minimum_required(VERSION 3.13.1)
-find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
-project(drivers)
-
-# Include the local test directory for shimmed_test_tasks.h
-zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
-zephyr_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
-zephyr_include_directories("${PLATFORM_EC}/driver/ppc/")
-
-FILE(GLOB test_sources src/*.c)
-target_sources(app PRIVATE ${test_sources})
-
-target_sources(app PRIVATE "${PLATFORM_EC}/test/cbi.c")
diff --git a/zephyr/test/drivers/README.md b/zephyr/test/drivers/README.md
deleted file mode 100644
index 8ea3dcdde1..0000000000
--- a/zephyr/test/drivers/README.md
+++ /dev/null
@@ -1,50 +0,0 @@
-This is the combined driver test. The goal is to have many driver test suites
-in one binary, so that compile time will be faster than many small tests, and
-so we can test interactions between different subsystems easily.
-
-## Run all the test suites
-
-```bash
-(chroot) zmake configure --test zephyr/test/drivers
-```
-
-To see all the output of zmake (for example if the build fails)
-
-```bash
-(chroot) zmake -l DEBUG -j 1 configure --test zephyr/test/drivers
-```
-
-## Code coverage
-
-To calculate code coverage for this test only
-
-```bash
-(chroot) zmake configure --coverage --test zephyr/test/drivers
-(chroot) lcov --gcov-tool ~/trunk/src/platform/ec/util/llvm-gcov.sh -q \
- -o build/zephyr/test/drivers/lcov.info -c -d build/zephyr/test/drivers
-(chroot) genhtml -q -o build/zephyr/test/drivers/coverage_rpt \
- build/zephyr/test/drivers/lcov.info
-```
-
-The report will be in build/zephyr/test/drivers/coverage_rpt/index.html
-
-## Debugging
-
-You need the host version of gdb:
-
-```bash
-(chroot) sudo emerge -j sys-devel/gdb
-```
-
-Build the test
-```bash
-(chroot) zmake configure --build zephyr/test/drivers
-```
-
-Then run gdb
-
-```
-(chroot) gdb build/zephyr/test/drivers/build-singleimage/zephyr/zephyr.exe
-# Set breakpoints, run, etc.
-```
-
diff --git a/zephyr/test/drivers/include/gpio_map.h b/zephyr/test/drivers/include/gpio_map.h
deleted file mode 100644
index 1f67138bd2..0000000000
--- a/zephyr/test/drivers/include/gpio_map.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef __ZEPHYR_GPIO_MAP_H
-#define __ZEPHYR_GPIO_MAP_H
-
-#include <devicetree.h>
-#include <gpio_signal.h>
-
-/*
- * Without https://github.com/zephyrproject-rtos/zephyr/pull/29282, we need
- * to manually link GPIO_ defines that platform/ec code expects to the
- * enum gpio_signal values that are generated by device tree bindings.
- *
- * Note we only need to create aliases for GPIOs that are referenced in common
- * platform/ec code.
- */
-#define CONFIG_TEMP_SENSOR_POWER_GPIO GPIO_PG_EC_DSW_PWROK
-
-#define EC_CROS_GPIO_INTERRUPTS \
- GPIO_INT(GPIO_AC_PRESENT, GPIO_INT_EDGE_BOTH, extpower_interrupt)
-
-#endif /* __ZEPHYR_GPIO_MAP_H */
diff --git a/zephyr/test/drivers/include/stubs.h b/zephyr/test/drivers/include/stubs.h
deleted file mode 100644
index 1ae0f3a50d..0000000000
--- a/zephyr/test/drivers/include/stubs.h
+++ /dev/null
@@ -1,10 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "power.h"
-
-enum usbc_port { USBC_PORT_C0 = 0, USBC_PORT_C1, USBC_PORT_COUNT };
-
-void set_mock_power_state(enum power_state state);
diff --git a/zephyr/test/drivers/overlay.dts b/zephyr/test/drivers/overlay.dts
deleted file mode 100644
index b211d77caa..0000000000
--- a/zephyr/test/drivers/overlay.dts
+++ /dev/null
@@ -1,561 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <dt-bindings/gpio_defines.h>
-#include <cros/thermistor/thermistor.dtsi>
-
-/ {
- aliases {
- bmi260-int = &ms_bmi260_accel;
- bmi160-int = &ms_bmi160_accel;
- lis2dw12-int = &ms_lis2dw12_accel;
- tcs3400-int = &tcs3400_clear;
- };
- named-gpios {
- compatible = "named-gpios";
-
- ec_batt_pres_odl {
- gpios = <&gpio0 1 GPIO_INPUT>;
- enum-name = "GPIO_BATT_PRES_ODL";
- label = "EC_BATT_PRES_ODL";
- };
- acok_od {
- gpios = <&gpio0 2 GPIO_INPUT>;
- enum-name = "GPIO_AC_PRESENT";
- label = "ACOK_OD";
- };
- /* In test WP is output because CBI use it, but it is also
- * input, because test_all_tags set it to enable write
- * protection.
- */
- gpio_wp_l: wp_l {
- #gpio-cells = <0>;
- gpios = <&gpio0 3 (GPIO_INPUT | GPIO_OUTPUT)>;
- enum-name = "GPIO_WP_L";
- label = "WP_L";
- };
- pg_ec_dsw_pwrok {
- gpios = <&gpio0 4 GPIO_INPUT>;
- enum-name = "GPIO_PG_EC_DSW_PWROK";
- label = "PG_EC_DSW_PWROK";
- };
- ec_pch_wake_odl {
- gpios = <&gpio0 5 GPIO_OUT_HIGH>;
- enum-name = "GPIO_EC_PCH_WAKE_ODL";
- label = "EC_PCH_WAKE_ODL";
- };
- /* Setup USB C1 pin as output to check their state in test */
- usb_c1_ls_en {
- gpios = <&gpio0 6 (GPIO_INPUT | GPIO_PULL_UP |
- GPIO_OUTPUT)>;
- enum-name = "GPIO_USB_C1_LS_EN";
- label = "USB_C1_LS_EN";
- };
- usb_c1_rt_rst_odl {
- gpios = <&gpio0 7 (GPIO_OUTPUT | GPIO_INPUT)>;
- enum-name = "GPIO_USB_C1_RT_RST_ODL";
- label = "USB_C1_RT_RST_ODL";
- };
- };
- named-i2c-ports {
- compatible = "named-i2c-ports";
-
- usb-c0 {
- i2c-port = <&i2c0>;
- enum-name = "I2C_PORT_USB_C0";
- label = "USB_C0";
- };
- usb-c1 {
- i2c-port = <&i2c0>;
- enum-name = "I2C_PORT_USB_C1";
- label = "USB_C1";
- };
- battery {
- i2c-port = <&i2c0>;
- enum-name = "I2C_PORT_BATTERY";
- label = "BATTERY";
- };
- power {
- i2c-port = <&i2c0>;
- enum-name = "I2C_PORT_POWER";
- label = "POWER";
- };
- charger {
- i2c-port = <&i2c0>;
- enum-name = "I2C_PORT_CHARGER";
- label = "CHARGER";
- };
- eeprom {
- i2c-port = <&i2c0>;
- enum-name = "I2C_PORT_EEPROM";
- label = "EEPROM";
- };
- i2c_accel: accel {
- i2c-port = <&i2c0>;
- enum-name = "I2C_PORT_ACCEL";
- label = "ACCEL";
- };
- i2c_sensor: sensor {
- i2c-port = <&i2c1>;
- enum-name = "I2C_PORT_SENSOR";
- label = "SENSOR";
- };
- };
-
- named-batteries {
- compatible = "named-batteries";
-
- lgc011 {
- enum-name = "lgc011";
- };
- };
-
- adc0: adc {
- compatible = "zephyr,adc-emul";
- nchannels = <4>;
- ref-internal-mv = <3300>;
- #io-channel-cells = <1>;
- label = "ADC_0";
- status = "okay";
- };
-
- named-adc-channels {
- compatible = "named-adc-channels";
-
- adc_charger: charger {
- label = "ADC_TEMP_SENSOR_CHARGER";
- enum-name = "ADC_TEMP_SENSOR_CHARGER";
- channel = <0>;
- };
- adc_pp3300_regulator: pp3300-regulator {
- label = "ADC_TEMP_SENSOR_PP3300_REGULATOR";
- enum-name = "ADC_TEMP_SENSOR_PP3300_REGULATOR";
- channel = <1>;
- };
- adc_ddr_soc: ddr-soc {
- label = "ADC_TEMP_SENSOR_DDR_SOC";
- enum-name = "ADC_TEMP_SENSOR_DDR_SOC";
- channel = <2>;
- };
- adc_fan: fan {
- label = "ADC_TEMP_SENSOR_FAN";
- enum-name = "ADC_TEMP_SENSOR_FAN";
- channel = <3>;
- };
- };
-
- named-temp-sensors {
- charger {
- thermistor = <&thermistor_3V3_13K7_47K_4050B>;
- status = "okay";
- compatible = "cros-ec,temp-sensor";
- label = "TEMP_SENSOR_CHARGER";
- enum-name = "TEMP_SENSOR_CHARGER";
- temp_fan_off = <40>;
- temp_fan_max = <55>;
- temp_host_high = <75>;
- temp_host_halt = <80>;
- temp_host_release_high = <65>;
- adc = <&adc_charger>;
- };
- pp3300-regulator {
- thermistor = <&thermistor_3V3_30K9_47K_4050B>;
- status = "okay";
- compatible = "cros-ec,temp-sensor";
- label = "TEMP_SENSOR_PP3300_REGULATOR";
- enum-name = "TEMP_SENSOR_PP3300_REGULATOR";
- temp_fan_off = <40>;
- temp_fan_max = <55>;
- temp_host_high = <75>;
- temp_host_halt = <80>;
- temp_host_release_high = <65>;
- adc = <&adc_pp3300_regulator>;
- };
- ddr-soc {
- thermistor = <&thermistor_3V3_51K1_47K_4050B>;
- status = "okay";
- compatible = "cros-ec,temp-sensor";
- label = "TEMP_SENSOR_DDR_SOC";
- enum-name = "TEMP_SENSOR_DDR_SOC";
- temp_fan_off = <35>;
- temp_fan_max = <50>;
- temp_host_high = <70>;
- temp_host_halt = <80>;
- temp_host_release_high = <65>;
- adc = <&adc_ddr_soc>;
- };
- fan {
- thermistor = <&thermistor_3V0_22K6_47K_4050B>;
- status = "okay";
- compatible = "cros-ec,temp-sensor";
- label = "TEMP_SENSOR_FAN";
- enum-name = "TEMP_SENSOR_FAN";
- temp_fan_off = <35>;
- temp_fan_max = <50>;
- temp_host_high = <70>;
- temp_host_halt = <80>;
- temp_host_release_high = <65>;
- adc = <&adc_fan>;
- };
- };
-
- /*
- * Declare mutexes used by sensor drivers.
- * A mutex node is used to create an instance of mutex_t.
- * A mutex node is referenced by a sensor node if the
- * corresponding sensor driver needs to use the
- * instance of the mutex.
- */
- motionsense-mutex {
- compatible = "cros-ec,motionsense-mutex";
- mutex_bmi260: bmi260-mutex {
- label = "BMI260_MUTEX";
- };
-
- mutex_bmi160: bmi160-mutex {
- label = "BMI160_MUTEX";
- };
-
- mutex_lis2dw12: lis2dw12-mutex {
- label = "LIS2DW12_MUTEX";
- };
- };
-
- /*
- * Driver specific data. A driver-specific data can be shared with
- * different motion sensors while they are using the same driver.
- */
- motionsense-sensor-data {
- bmi260_data: bmi260-drv-data {
- compatible = "cros-ec,drvdata-bmi260";
- status = "okay";
- };
-
- bmi160_data: bmi160-drv-data {
- compatible = "cros-ec,drvdata-bmi160";
- status = "okay";
- };
-
- lis2dw12_data: lis2dw12-drv-data {
- compatible = "cros-ec,drvdata-lis2dw12";
- status = "okay";
- };
-
- tcs_clear_data: tcs3400-clear-drv-data {
- compatible = "cros-ec,drvdata-tcs3400-clear";
- status = "okay";
-
- als-drv-data {
- compatible = "cros-ec,accelgyro-als-drv-data";
- als-cal {
- scale = <1>;
- uscale = <0>;
- offset = <0>;
- als-channel-scale {
- compatible =
- "cros-ec,accelgyro-als-channel-scale";
- k-channel-scale = <1>;
- cover-scale = <1>;
- };
- };
- };
- };
-
- tcs_rgb_data: tcs3400-rgb-drv-data {
- compatible = "cros-ec,drvdata-tcs3400-rgb";
- status = "okay";
-
- /* node for rgb_calibration_t defined in accelgyro.h */
- rgb_calibration {
- compatible =
- "cros-ec,accelgyro-rgb-calibration";
-
- irt = <1>;
-
- rgb-cal-x {
- offset = <0>;
- coeff = <0 1 0 0>;
- als-channel-scale {
- compatible =
- "cros-ec,accelgyro-als-channel-scale";
- k-channel-scale = <1>;
- cover-scale = <1>;
- };
- };
- rgb-cal-y {
- offset = <0>;
- coeff = <0 0 1 0>;
- als-channel-scale {
- compatible =
- "cros-ec,accelgyro-als-channel-scale";
- k-channel-scale = <1>;
- cover-scale = <1>;
- };
- };
- rgb-cal-z {
- offset = <0>;
- coeff = <0 0 0 1>;
- als-channel-scale {
- compatible =
- "cros-ec,accelgyro-als-channel-scale";
- k-channel-scale = <1>;
- cover-scale = <1>;
- };
- };
- };
- };
- };
-
- /*
- * List of motion sensors that creates motion_sensors array.
- * The label "lid_accel" and "base_accel" are used to indicate
- * motion sensor IDs for lid angle calculation.
- */
- motionsense-sensor {
- ms_bmi260_accel: ms-bmi260-accel {
- compatible = "cros-ec,bmi260-accel";
- status = "okay";
-
- label = "BMI260 emul accel";
- location = "MOTIONSENSE_LOC_BASE";
- mutex = <&mutex_bmi260>;
- port = <&i2c_accel>;
- drv-data = <&bmi260_data>;
- default-range = <4>;
- i2c-spi-addr-flags = "BMI260_ADDR0_FLAGS";
- };
-
- ms_bmi260_gyro: ms-bmi260-gyro {
- compatible = "cros-ec,bmi260-gyro";
- status = "okay";
-
- label = "BMI260 emul gyro";
- location = "MOTIONSENSE_LOC_BASE";
- mutex = <&mutex_bmi260>;
- port = <&i2c_accel>;
- drv-data = <&bmi260_data>;
- default-range = <1000>; /* dps */
- i2c-spi-addr-flags = "BMI260_ADDR0_FLAGS";
- };
-
- ms_bmi160_accel: ms-bmi160-accel {
- compatible = "cros-ec,bmi160-accel";
- status = "okay";
-
- label = "BMI160 emul accel";
- location = "MOTIONSENSE_LOC_BASE";
- mutex = <&mutex_bmi160>;
- port = <&i2c_sensor>;
- drv-data = <&bmi160_data>;
- default-range = <4>;
- i2c-spi-addr-flags = "BMI160_ADDR0_FLAGS";
- };
-
- ms_bmi160_gyro: ms-bmi160-gyro {
- compatible = "cros-ec,bmi160-gyro";
- status = "okay";
-
- label = "BMI160 emul gyro";
- location = "MOTIONSENSE_LOC_BASE";
- mutex = <&mutex_bmi160>;
- port = <&i2c_sensor>;
- drv-data = <&bmi160_data>;
- default-range = <1000>; /* dps */
- i2c-spi-addr-flags = "BMI160_ADDR0_FLAGS";
- };
-
- ms_lis2dw12_accel: ms-lis2dw12-accel {
- compatible = "cros-ec,lis2dw12";
- status = "okay";
-
- label = "LIS2DW12";
- location = "MOTIONSENSE_LOC_BASE";
- mutex = <&mutex_lis2dw12>;
- port = <&i2c_accel>;
- drv-data = <&lis2dw12_data>;
- default-range = <2>;
- i2c-spi-addr-flags = "LIS2DWL_ADDR1_FLAGS";
- };
-
- tcs3400_clear: tcs3400-clear {
- compatible = "cros-ec,tcs3400-clear";
- status = "okay";
-
- label = "Clear Light";
- location = "MOTIONSENSE_LOC_BASE";
- port = <&i2c_sensor>;
- default-range = <0x10000>;
- drv-data = <&tcs_clear_data>;
- i2c-spi-addr-flags = "TCS3400_I2C_ADDR_FLAGS";
- configs {
- compatible =
- "cros-ec,motionsense-sensor-config";
- ec-s0 {
- /* Run ALS sensor in S0 */
- label = "SENSOR_CONFIG_EC_S0";
- odr = <1000>;
- };
- };
- };
-
- tcs3400_rgb: tcs3400-rgb {
- compatible = "cros-ec,tcs3400-rgb";
- status = "okay";
-
- label = "RGB Light";
- location = "MOTIONSENSE_LOC_BASE";
- default-range = <0x10000>; /* scale = 1x, uscale = 0 */
- drv-data = <&tcs_rgb_data>;
- };
- };
-
- /*
- * Second i2c bus is required, because there is already device with
- * address 0x68 on the first bus
- */
- i2c1: i2c@400 {
- status = "okay";
- compatible = "zephyr,i2c-emul-controller";
- clock-frequency = <I2C_BITRATE_STANDARD>;
- #address-cells = <1>;
- #size-cells = <0>;
- reg = <0x400 4>;
- label = "I2C_1";
-
- accel_bmi160: bmi160@68 {
- compatible = "zephyr,bmi";
- reg = <0x68>;
- label = "BMI160";
- device-model = "BMI_EMUL_160";
- error-on-ro-write;
- error-on-wo-read;
- error-on-reserved-bit-write;
- simulate-command-exec-time;
- };
-
- tcs_emul: tcs@39 {
- compatible = "zephyr,tcs3400";
- reg = <0x39>;
- label = "TCS_EMUL";
- error-on-ro-write;
- error-on-reserved-bit-write;
- error-on-msb-first-access;
- };
- };
-};
-
-&espi0 {
- espi-host@0 {
- status = "okay";
- compatible = "zephyr,espi-emul-espi-host";
- reg = <0x0>;
- label = "ESPI_HOST";
- };
-};
-
-&gpio0 {
- ngpios = <8>;
-};
-
-&i2c0 {
- cbi_eeprom: eeprom@56 {
- compatible = "atmel,at24";
- reg = <0x56>;
- label = "EEPROM_CBI";
- size = <512>;
- pagesize = <8>;
- address-width = <8>;
- timeout = <5>;
- wp-gpios = <&gpio_wp_l>;
- };
-
- battery: sb@b {
- compatible = "zephyr,smart-battery";
- reg = <0xb>;
- label = "BATTERY";
- cycle-count = <99>;
- version = "BATTERY_SPEC_VER_1_1_WITH_PEC";
- };
-
- bma_emul: bma@18 {
- compatible = "zephyr,bma255";
- reg = <0x18>;
- label = "BMA_EMUL";
- error-on-compensation-not-ready;
- error-on-ro-write;
- error-on-reserved-bit-write;
- error-on-msb-first-access;
- };
-
- pi3usb9201_emul: pi3usb9201@5f {
- compatible = "zephyr,pi3usb9201-emul";
- reg = <0x5f>;
- label = "PI3USB9201_EMUL";
- };
-
- syv682x_emul: syv682x@41 {
- compatible = "zephyr,syv682x-emul";
- reg = <0x41>;
- label = "SYV682X_EMUL";
- };
-
- accel_bmi260: bmi260@68 {
- compatible = "zephyr,bmi";
- reg = <0x68>;
- label = "BMI260";
- device-model = "BMI_EMUL_260";
- error-on-ro-write;
- error-on-wo-read;
- error-on-reserved-bit-write;
- simulate-command-exec-time;
- };
-
- usb_c1_bb_retimer_emul: bbretimer@42 {
- compatible = "cros,bb-retimer-emul";
- reg = <0x42>;
- label = "USB_C1_BB_RETIMER";
- vendor = "BB_RETIMER_VENDOR_ID_1";
- error-on-ro-write;
- error-on-reserved-bit-write;
- };
-
- ln9310: ln9310@80 {
- compatible = "cros,ln9310-emul";
- status = "okay";
- reg = <0x80>;
- label = "LN9310";
- };
-
- lis2dw12_emul: lis2dw12@19 {
- compatible = "cros,lis2dw12-emul";
- status = "okay";
- reg = <0x19>;
- label = "LIS2DW12_EMUL";
- };
-
- i2c_mock: i2c_mock@84 {
- compatible = "cros,i2c-mock";
- status = "okay";
- reg = <0x84>;
- label = "I2C_MOCK";
- };
-};
-
-/* Enable all thermistors for testing */
-&thermistor_3V3_30K9_47K_4050B {
- status = "okay";
-};
-
-&thermistor_3V0_22K6_47K_4050B {
- status = "okay";
-};
-
-&thermistor_3V3_13K7_47K_4050B {
- status = "okay";
-};
-
-&thermistor_3V3_51K1_47K_4050B {
- status = "okay";
-};
diff --git a/zephyr/test/drivers/prj.conf b/zephyr/test/drivers/prj.conf
deleted file mode 100644
index 99a275f9e2..0000000000
--- a/zephyr/test/drivers/prj.conf
+++ /dev/null
@@ -1,90 +0,0 @@
-# Copyright 2021 The Chromium OS Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style license that can be
-# found in the LICENSE file.
-
-# Enabling this config will show all I2C traffic, do not commit with this line
-# uncommented, it is here to make it easy to find/enable.
-# CONFIG_I2C_LOG_LEVEL_DBG=y
-
-CONFIG_ZTEST=y
-CONFIG_ZTEST_ASSERT_VERBOSE=1
-CONFIG_ZTEST_MOCKING=y
-CONFIG_ZTEST_PARAMETER_COUNT=5
-CONFIG_PLATFORM_EC=y
-CONFIG_CROS_EC=y
-CONFIG_SHIMMED_TASKS=y
-CONFIG_EMUL=y
-CONFIG_LOG=y
-CONFIG_I2C=y
-CONFIG_I2C_EMUL=y
-CONFIG_GPIO=y
-CONFIG_GPIO_EMUL=y
-CONFIG_EMUL_EEPROM_AT2X=y
-CONFIG_EMUL_SMART_BATTERY=y
-CONFIG_EMUL_BC12_DETECT_PI3USB9201=y
-CONFIG_EMUL_PPC_SYV682X=y
-CONFIG_ADC=y
-CONFIG_ADC_EMUL=y
-CONFIG_HEAP_MEM_POOL_SIZE=1024
-CONFIG_EMUL_BMA255=y
-CONFIG_EMUL_BMI=y
-CONFIG_EMUL_TCS3400=y
-CONFIG_EMUL_BB_RETIMER=y
-
-CONFIG_PLATFORM_EC_POWERSEQ=y
-
-CONFIG_HAS_TASK_USB_CHG_P0=y
-CONFIG_HAS_TASK_USB_CHG_P1=y
-CONFIG_HAS_TASK_PD_C1=y
-
-CONFIG_PLATFORM_EC_BATTERY_PRESENT_GPIO=y
-CONFIG_PLATFORM_EC_EXTPOWER_GPIO=y
-
-CONFIG_PLATFORM_EC_USB_PID=0x5000
-CONFIG_PLATFORM_EC_USBC=y
-CONFIG_PLATFORM_EC_USB_CHARGER=y
-CONFIG_PLATFORM_EC_BC12_DETECT_PI3USB9201=y
-CONFIG_PLATFORM_EC_USB_POWER_DELIVERY=y
-CONFIG_PLATFORM_EC_USB_PD_5V_EN_CUSTOM=y
-CONFIG_PLATFORM_EC_I2C=y
-CONFIG_PLATFORM_EC_BATTERY=y
-CONFIG_PLATFORM_EC_BATTERY_SMART=y
-CONFIG_PLATFORM_EC_CHARGER_SENSE_RESISTOR=10
-CONFIG_PLATFORM_EC_CHARGER_SENSE_RESISTOR_AC=10
-CONFIG_PLATFORM_EC_CHARGER_ISL9241=y
-CONFIG_PLATFORM_EC_USB_PD_VBUS_MEASURE_CHARGER=y
-CONFIG_PLATFORM_EC_BATTERY_FUEL_GAUGE=y
-CONFIG_PLATFORM_EC_HOSTCMD=y
-CONFIG_PLATFORM_EC_USB_PD_TCPM_TUSB422=y
-CONFIG_PLATFORM_EC_USB_MUX_VIRTUAL=y
-CONFIG_PLATFORM_EC_USBC_PPC_SN5S330=y
-CONFIG_PLATFORM_EC_CBI_EEPROM=y
-CONFIG_PLATFORM_EC_ADC=y
-CONFIG_PLATFORM_EC_TEMP_SENSOR=y
-CONFIG_PLATFORM_EC_THERMISTOR=y
-CONFIG_PLATFORM_EC_SWITCHCAP_LN9310=y
-CONFIG_PLATFORM_EC_ACCEL_BMA255=y
-CONFIG_PLATFORM_EC_MOTIONSENSE=y
-CONFIG_PLATFORM_EC_ACCELGYRO_BMI160=y
-CONFIG_PLATFORM_EC_ACCELGYRO_BMI260=y
-CONFIG_PLATFORM_EC_ACCELGYRO_BMI_COMM_I2C=y
-CONFIG_PLATFORM_EC_ACCEL_INTERRUPTS=y
-CONFIG_PLATFORM_EC_ACCEL_FIFO=y
-CONFIG_PLATFORM_EC_SENSOR_TIGHT_TIMESTAMPS=y
-CONFIG_PLATFORM_EC_ALS_TCS3400=y
-CONFIG_PLATFORM_EC_USBC_RETIMER_INTEL_BB=y
-
-CONFIG_ESPI=y
-CONFIG_ESPI_EMUL=y
-CONFIG_EMUL_ESPI_HOST=y
-CONFIG_PLATFORM_EC_ESPI=y
-CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION=y
-CONFIG_ESPI_PERIPHERAL_CUSTOM_OPCODE=y
-CONFIG_ESPI_PERIPHERAL_EC_HOST_CMD=y
-
-# Things that default to on, but aren't working yet
-CONFIG_PLATFORM_EC_BACKLIGHT_LID=n
-CONFIG_PLATFORM_EC_SWITCH=n
-CONFIG_PLATFORM_EC_VBOOT_HASH=n
-CONFIG_PLATFORM_EC_POWERSEQ_INTEL=n
-CONFIG_PLATFORM_EC_POWERSEQ_PP5000_CONTROL=n
diff --git a/zephyr/test/drivers/src/battery.c b/zephyr/test/drivers/src/battery.c
deleted file mode 100644
index 315f822985..0000000000
--- a/zephyr/test/drivers/src/battery.c
+++ /dev/null
@@ -1,35 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <zephyr.h>
-#include <ztest.h>
-#include <drivers/gpio.h>
-#include <drivers/gpio/gpio_emul.h>
-
-#include "battery.h"
-
-#define GPIO_BATT_PRES_ODL_PATH DT_PATH(named_gpios, ec_batt_pres_odl)
-#define GPIO_BATT_PRES_ODL_PORT DT_GPIO_PIN(GPIO_BATT_PRES_ODL_PATH, gpios)
-
-static void test_battery_is_present_gpio(void)
-{
- const struct device *dev =
- DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_BATT_PRES_ODL_PATH, gpios));
-
- zassert_not_null(dev, NULL);
- /* ec_batt_pres_odl = 0 means battery present. */
- zassert_ok(gpio_emul_input_set(dev, GPIO_BATT_PRES_ODL_PORT, 0), NULL);
- zassert_equal(BP_YES, battery_is_present(), NULL);
- /* ec_batt_pres_odl = 1 means battery missing. */
- zassert_ok(gpio_emul_input_set(dev, GPIO_BATT_PRES_ODL_PORT, 1), NULL);
- zassert_equal(BP_NO, battery_is_present(), NULL);
-}
-
-void test_suite_battery(void)
-{
- ztest_test_suite(battery,
- ztest_user_unit_test(test_battery_is_present_gpio));
- ztest_run_test_suite(battery);
-}
diff --git a/zephyr/test/drivers/src/bb_retimer.c b/zephyr/test/drivers/src/bb_retimer.c
deleted file mode 100644
index cecee6020f..0000000000
--- a/zephyr/test/drivers/src/bb_retimer.c
+++ /dev/null
@@ -1,528 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <zephyr.h>
-#include <ztest.h>
-#include <drivers/gpio.h>
-#include <drivers/gpio/gpio_emul.h>
-
-#include "common.h"
-#include "ec_tasks.h"
-#include "emul/emul_bb_retimer.h"
-#include "emul/emul_common_i2c.h"
-#include "hooks.h"
-#include "i2c.h"
-#include "stubs.h"
-#include "usb_prl_sm.h"
-#include "usb_tc_sm.h"
-
-#include "driver/retimer/bb_retimer.h"
-
-#define GPIO_USB_C1_LS_EN_PATH DT_PATH(named_gpios, usb_c1_ls_en)
-#define GPIO_USB_C1_LS_EN_PORT DT_GPIO_PIN(GPIO_USB_C1_LS_EN_PATH, gpios)
-#define GPIO_USB_C1_RT_RST_ODL_PATH DT_PATH(named_gpios, usb_c1_rt_rst_odl)
-#define GPIO_USB_C1_RT_RST_ODL_PORT \
- DT_GPIO_PIN(GPIO_USB_C1_RT_RST_ODL_PATH, gpios)
-#define EMUL_LABEL DT_NODELABEL(usb_c1_bb_retimer_emul)
-
-#define BB_RETIMER_ORD DT_DEP_ORD(EMUL_LABEL)
-
-/** Test is retimer fw update capable function. */
-static void test_bb_is_fw_update_capable(void)
-{
- /* BB retimer is fw update capable */
- zassert_true(bb_usb_retimer.is_retimer_fw_update_capable(), NULL);
-}
-
-/** Test is retimer fw update capable function. */
-static void test_bb_set_state(void)
-{
- struct pd_discovery *disc;
- uint32_t conn, exp_conn;
- struct i2c_emul *emul;
- bool ack_required;
-
- emul = bb_emul_get(BB_RETIMER_ORD);
-
- set_test_runner_tid();
-
- /* Setup emulator fail on write */
- i2c_common_emul_set_write_fail_reg(emul,
- BB_RETIMER_REG_CONNECTION_STATE);
-
- /* Test fail on reset register write */
- zassert_equal(EC_ERROR_INVAL,
- bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_NONE, &ack_required),
- NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
-
- /* Do not fail on write */
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Set UFP role for whole test */
- tc_set_data_role(USBC_PORT_C1, PD_ROLE_UFP);
-
- /* Test none mode */
- bb_emul_set_reg(emul, BB_RETIMER_REG_CONNECTION_STATE, 0x12144678);
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_NONE,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- /* Only UFP mode is set */
- exp_conn = BB_RETIMER_USB_DATA_ROLE;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test USB3 gen1 mode */
- prl_set_rev(USBC_PORT_C1, TCPCI_MSG_SOP_PRIME, PD_REV10);
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_USB_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_USB_DATA_ROLE |
- BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_USB_3_CONNECTION;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test USB3 gen2 mode */
- disc = pd_get_am_discovery_and_notify_access(
- USBC_PORT_C1, TCPCI_MSG_SOP_PRIME);
- disc->identity.product_t1.p_rev20.ss = USB_R20_SS_U31_GEN1_GEN2;
- prl_set_rev(USBC_PORT_C1, TCPCI_MSG_SOP_PRIME, PD_REV30);
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_USB_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_USB_DATA_ROLE |
- BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_USB_3_CONNECTION |
- BB_RETIMER_USB_3_SPEED;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test TBT mode */
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_TBT_COMPAT_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_USB_DATA_ROLE |
- BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_TBT_CONNECTION;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test USB4 mode */
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_USB4_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_USB_DATA_ROLE |
- BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_USB4_ENABLED;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test USB4 mode with polarity inverted */
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_USB4_ENABLED |
- USB_PD_MUX_POLARITY_INVERTED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_USB_DATA_ROLE |
- BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_CONNECTION_ORIENTATION |
- BB_RETIMER_USB4_ENABLED;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test DP mode */
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_DP_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_USB_DATA_ROLE |
- BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_DP_CONNECTION;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_DP_ENABLED |
- USB_PD_MUX_HPD_IRQ,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_USB_DATA_ROLE |
- BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_DP_CONNECTION |
- BB_RETIMER_IRQ_HPD;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_DP_ENABLED |
- USB_PD_MUX_HPD_LVL,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_USB_DATA_ROLE |
- BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_DP_CONNECTION |
- BB_RETIMER_HPD_LVL;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-}
-
-/** Test setting different options for DFP role */
-static void test_bb_set_dfp_state(void)
-{
- union tbt_mode_resp_device device_resp;
- union tbt_mode_resp_cable cable_resp;
- struct pd_discovery *disc, *dev_disc;
- uint32_t conn, exp_conn;
- struct i2c_emul *emul;
- bool ack_required;
-
- emul = bb_emul_get(BB_RETIMER_ORD);
-
- set_test_runner_tid();
-
- tc_set_data_role(USBC_PORT_C1, PD_ROLE_DFP);
-
- /* Test PD mux none mode with DFP should clear all bits in state */
- bb_emul_set_reg(emul, BB_RETIMER_REG_CONNECTION_STATE, 0x12144678);
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_NONE,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = 0;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Set active cable type */
- disc = pd_get_am_discovery_and_notify_access(
- USBC_PORT_C1, TCPCI_MSG_SOP_PRIME);
- disc->identity.idh.product_type = IDH_PTYPE_ACABLE;
- disc->identity.product_t2.a2_rev30.active_elem = ACTIVE_RETIMER;
- prl_set_rev(USBC_PORT_C1, TCPCI_MSG_SOP_PRIME, PD_REV30);
-
- /* Set cable VDO */
- disc->svid_cnt = 1;
- disc->svids[0].svid = USB_VID_INTEL;
- disc->svids[0].discovery = PD_DISC_COMPLETE;
- disc->svids[0].mode_cnt = 1;
- cable_resp.tbt_alt_mode = TBT_ALTERNATE_MODE;
- cable_resp.tbt_cable_speed = TBT_SS_RES_0;
- cable_resp.tbt_rounded = TBT_GEN3_NON_ROUNDED;
- cable_resp.tbt_cable = TBT_CABLE_NON_OPTICAL;
- cable_resp.retimer_type = USB_NOT_RETIMER;
- cable_resp.lsrx_comm = BIDIR_LSRX_COMM;
- cable_resp.tbt_active_passive = TBT_CABLE_PASSIVE;
- disc->svids[0].mode_vdo[0] = cable_resp.raw_value;
-
- /* Set device VDO */
- dev_disc = pd_get_am_discovery_and_notify_access(
- USBC_PORT_C1, TCPCI_MSG_SOP);
- dev_disc->svid_cnt = 1;
- dev_disc->svids[0].svid = USB_VID_INTEL;
- dev_disc->svids[0].discovery = PD_DISC_COMPLETE;
- dev_disc->svids[0].mode_cnt = 1;
- device_resp.tbt_alt_mode = TBT_ALTERNATE_MODE;
- device_resp.tbt_adapter = TBT_ADAPTER_TBT3;
- device_resp.intel_spec_b0 = VENDOR_SPECIFIC_NOT_SUPPORTED;
- device_resp.vendor_spec_b0 = VENDOR_SPECIFIC_NOT_SUPPORTED;
- device_resp.vendor_spec_b1 = VENDOR_SPECIFIC_NOT_SUPPORTED;
- dev_disc->svids[0].mode_vdo[0] = device_resp.raw_value;
-
- /* Test USB mode with active cable */
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_USB_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_USB_3_CONNECTION |
- BB_RETIMER_USB_3_SPEED |
- BB_RETIMER_RE_TIMER_DRIVER |
- BB_RETIMER_ACTIVE_PASSIVE;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test TBT mode with active cable */
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_TBT_COMPAT_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_TBT_CONNECTION |
- BB_RETIMER_ACTIVE_PASSIVE;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test TBT mode with retimer */
- cable_resp.retimer_type = USB_RETIMER;
- disc->svids[0].mode_vdo[0] = cable_resp.raw_value;
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_TBT_COMPAT_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_TBT_CONNECTION |
- BB_RETIMER_RE_TIMER_DRIVER |
- BB_RETIMER_ACTIVE_PASSIVE;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test TBT mode with optical cable */
- cable_resp.retimer_type = USB_NOT_RETIMER;
- cable_resp.tbt_cable = TBT_CABLE_OPTICAL;
- disc->svids[0].mode_vdo[0] = cable_resp.raw_value;
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_TBT_COMPAT_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_TBT_CONNECTION |
- BB_RETIMER_TBT_CABLE_TYPE |
- BB_RETIMER_ACTIVE_PASSIVE;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test TBT mode with active link training */
- cable_resp.tbt_cable = TBT_CABLE_NON_OPTICAL;
- cable_resp.lsrx_comm = UNIDIR_LSRX_COMM;
- disc->svids[0].mode_vdo[0] = cable_resp.raw_value;
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_TBT_COMPAT_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_TBT_CONNECTION |
- BB_RETIMER_TBT_ACTIVE_LINK_TRAINING |
- BB_RETIMER_ACTIVE_PASSIVE;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test TBT mode with different cable speeds */
- cable_resp.lsrx_comm = BIDIR_LSRX_COMM;
- cable_resp.tbt_cable_speed = TBT_SS_U31_GEN1;
- disc->svids[0].mode_vdo[0] = cable_resp.raw_value;
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_TBT_COMPAT_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_TBT_CONNECTION |
- BB_RETIMER_USB4_TBT_CABLE_SPEED_SUPPORT(1) |
- BB_RETIMER_ACTIVE_PASSIVE;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- cable_resp.tbt_cable_speed = TBT_SS_U32_GEN1_GEN2;
- disc->svids[0].mode_vdo[0] = cable_resp.raw_value;
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_TBT_COMPAT_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_TBT_CONNECTION |
- BB_RETIMER_USB4_TBT_CABLE_SPEED_SUPPORT(2) |
- BB_RETIMER_ACTIVE_PASSIVE;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- cable_resp.tbt_cable_speed = TBT_SS_TBT_GEN3;
- disc->svids[0].mode_vdo[0] = cable_resp.raw_value;
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_TBT_COMPAT_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_TBT_CONNECTION |
- BB_RETIMER_USB4_TBT_CABLE_SPEED_SUPPORT(3) |
- BB_RETIMER_ACTIVE_PASSIVE;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test TBT mode with TBT gen4 cable */
- cable_resp.tbt_cable_speed = TBT_SS_RES_0;
- cable_resp.tbt_rounded = TBT_GEN3_GEN4_ROUNDED_NON_ROUNDED;
- disc->svids[0].mode_vdo[0] = cable_resp.raw_value;
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_TBT_COMPAT_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_TBT_CONNECTION |
- BB_RETIMER_TBT_CABLE_GENERATION(1) |
- BB_RETIMER_ACTIVE_PASSIVE;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test TBT mode with legacy TBT adapter */
- cable_resp.tbt_rounded = TBT_GEN3_NON_ROUNDED;
- disc->svids[0].mode_vdo[0] = cable_resp.raw_value;
- device_resp.tbt_adapter = TBT_ADAPTER_TBT2_LEGACY;
- dev_disc->svids[0].mode_vdo[0] = device_resp.raw_value;
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_TBT_COMPAT_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_TBT_CONNECTION |
- BB_RETIMER_TBT_TYPE |
- BB_RETIMER_ACTIVE_PASSIVE;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test TBT mode with Intel specific b0 */
- device_resp.tbt_adapter = TBT_ADAPTER_TBT3;
- device_resp.intel_spec_b0 = VENDOR_SPECIFIC_SUPPORTED;
- dev_disc->svids[0].mode_vdo[0] = device_resp.raw_value;
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_TBT_COMPAT_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_TBT_CONNECTION |
- BB_RETIMER_VPRO_DOCK_DP_OVERDRIVE |
- BB_RETIMER_ACTIVE_PASSIVE;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-
- /* Test TBT mode with vendor specific b1 */
- device_resp.intel_spec_b0 = VENDOR_SPECIFIC_NOT_SUPPORTED;
- device_resp.vendor_spec_b1 = VENDOR_SPECIFIC_SUPPORTED;
- dev_disc->svids[0].mode_vdo[0] = device_resp.raw_value;
- zassert_equal(EC_SUCCESS, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1],
- USB_PD_MUX_TBT_COMPAT_ENABLED,
- &ack_required), NULL);
- zassert_false(ack_required, "ACK is never required for BB retimer");
- conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE);
- exp_conn = BB_RETIMER_DATA_CONNECTION_PRESENT |
- BB_RETIMER_TBT_CONNECTION |
- BB_RETIMER_VPRO_DOCK_DP_OVERDRIVE |
- BB_RETIMER_ACTIVE_PASSIVE;
- zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx",
- exp_conn, conn);
-}
-
-/** Test BB retimer init */
-static void test_bb_init(void)
-{
- const struct device *gpio_dev =
- DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_USB_C1_LS_EN_PATH, gpios));
- struct i2c_emul *emul;
-
- zassert_not_null(gpio_dev, "Cannot get GPIO device");
-
- emul = bb_emul_get(BB_RETIMER_ORD);
-
- /* Set AP to normal state and wait for chipset task */
- set_mock_power_state(POWER_S0);
- k_msleep(1);
-
- /* Setup emulator fail on read */
- i2c_common_emul_set_read_fail_reg(emul, BB_RETIMER_REG_VENDOR_ID);
- /* Test fail on vendor ID read */
- zassert_equal(EC_ERROR_INVAL,
- bb_usb_retimer.init(&usb_muxes[USBC_PORT_C1]), NULL);
- /* Enable pins should be set always after init, when AP is on */
- zassert_equal(1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_LS_EN_PORT),
- NULL);
- zassert_equal(1, gpio_emul_output_get(gpio_dev,
- GPIO_USB_C1_RT_RST_ODL_PORT),
- NULL);
-
- /* Setup wrong vendor ID */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- bb_emul_set_reg(emul, BB_RETIMER_REG_VENDOR_ID, 0x12144678);
- /* Test fail on wrong vendor ID */
- zassert_equal(EC_ERROR_INVAL,
- bb_usb_retimer.init(&usb_muxes[USBC_PORT_C1]), NULL);
- zassert_equal(1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_LS_EN_PORT),
- NULL);
- zassert_equal(1, gpio_emul_output_get(gpio_dev,
- GPIO_USB_C1_RT_RST_ODL_PORT),
- NULL);
-
- /* Setup emulator fail on device ID read */
- i2c_common_emul_set_read_fail_reg(emul, BB_RETIMER_REG_DEVICE_ID);
- bb_emul_set_reg(emul, BB_RETIMER_REG_VENDOR_ID, BB_RETIMER_VENDOR_ID_1);
- /* Test fail on device ID read */
- zassert_equal(EC_ERROR_INVAL,
- bb_usb_retimer.init(&usb_muxes[USBC_PORT_C1]), NULL);
- zassert_equal(1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_LS_EN_PORT),
- NULL);
- zassert_equal(1, gpio_emul_output_get(gpio_dev,
- GPIO_USB_C1_RT_RST_ODL_PORT),
- NULL);
-
- /* Setup wrong device ID */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- bb_emul_set_reg(emul, BB_RETIMER_REG_DEVICE_ID, 0x12144678);
- /* Test fail on wrong device ID */
- zassert_equal(EC_ERROR_INVAL,
- bb_usb_retimer.init(&usb_muxes[USBC_PORT_C1]), NULL);
- zassert_equal(1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_LS_EN_PORT),
- NULL);
- zassert_equal(1, gpio_emul_output_get(gpio_dev,
- GPIO_USB_C1_RT_RST_ODL_PORT),
- NULL);
-
- /* Test successful init */
- bb_emul_set_reg(emul, BB_RETIMER_REG_DEVICE_ID, BB_RETIMER_DEVICE_ID);
- zassert_equal(EC_SUCCESS, bb_usb_retimer.init(&usb_muxes[USBC_PORT_C1]),
- NULL);
- zassert_equal(1, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_LS_EN_PORT),
- NULL);
- zassert_equal(1, gpio_emul_output_get(gpio_dev,
- GPIO_USB_C1_RT_RST_ODL_PORT),
- NULL);
-
- /* Set AP to off state and wait for chipset task */
- set_mock_power_state(POWER_G3);
- k_msleep(1);
-
- /* With AP off, init should fail and pins should be unset */
- zassert_equal(EC_ERROR_NOT_POWERED,
- bb_usb_retimer.init(&usb_muxes[USBC_PORT_C1]), NULL);
- zassert_equal(0, gpio_emul_output_get(gpio_dev, GPIO_USB_C1_LS_EN_PORT),
- NULL);
- zassert_equal(0, gpio_emul_output_get(gpio_dev,
- GPIO_USB_C1_RT_RST_ODL_PORT),
- NULL);
-}
-
-
-void test_suite_bb_retimer(void)
-{
- ztest_test_suite(bb_retimer,
- ztest_user_unit_test(test_bb_is_fw_update_capable),
- ztest_user_unit_test(test_bb_set_state),
- ztest_user_unit_test(test_bb_set_dfp_state),
- ztest_user_unit_test(test_bb_init));
- ztest_run_test_suite(bb_retimer);
-}
diff --git a/zephyr/test/drivers/src/bc12.c b/zephyr/test/drivers/src/bc12.c
deleted file mode 100644
index 4251448f2c..0000000000
--- a/zephyr/test/drivers/src/bc12.c
+++ /dev/null
@@ -1,280 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <zephyr.h>
-#include <ztest.h>
-#include <drivers/gpio.h>
-#include <drivers/gpio/gpio_emul.h>
-
-#include "emul/emul_pi3usb9201.h"
-
-#include "timer.h"
-#include "usb_charge.h"
-#include "battery.h"
-#include "extpower.h"
-#include "stubs.h"
-
-#include <logging/log.h>
-LOG_MODULE_REGISTER(test_drivers_bc12, LOG_LEVEL_DBG);
-
-#define EMUL_LABEL DT_NODELABEL(pi3usb9201_emul)
-
-#define PI3USB9201_ORD DT_DEP_ORD(EMUL_LABEL)
-
-/* Control_1 register bit definitions */
-#define PI3USB9201_REG_CTRL_1_INT_MASK BIT(0)
-#define PI3USB9201_REG_CTRL_1_MODE_SHIFT 1
-#define PI3USB9201_REG_CTRL_1_MODE_MASK (0x7 << \
- PI3USB9201_REG_CTRL_1_MODE_SHIFT)
-
-/* Control_2 register bit definitions */
-#define PI3USB9201_REG_CTRL_2_AUTO_SW BIT(1)
-#define PI3USB9201_REG_CTRL_2_START_DET BIT(3)
-
-/* Host status register bit definitions */
-#define PI3USB9201_REG_HOST_STS_BC12_DET BIT(0)
-#define PI3USB9201_REG_HOST_STS_DEV_PLUG BIT(1)
-#define PI3USB9201_REG_HOST_STS_DEV_UNPLUG BIT(2)
-
-enum pi3usb9201_mode {
- PI3USB9201_POWER_DOWN,
- PI3USB9201_SDP_HOST_MODE,
- PI3USB9201_DCP_HOST_MODE,
- PI3USB9201_CDP_HOST_MODE,
- PI3USB9201_CLIENT_MODE,
- PI3USB9201_RESERVED_1,
- PI3USB9201_RESERVED_2,
- PI3USB9201_USB_PATH_ON,
-};
-
-enum pi3usb9201_client_sts {
- CHG_OTHER = 0,
- CHG_2_4A,
- CHG_2_0A,
- CHG_1_0A,
- CHG_RESERVED,
- CHG_CDP,
- CHG_SDP,
- CHG_DCP,
-};
-
-struct bc12_status {
- enum charge_supplier supplier;
- int current_limit;
-};
-
-static const struct bc12_status bc12_chg_limits[] = {
- [CHG_OTHER] = { .supplier = CHARGE_SUPPLIER_OTHER,
- .current_limit = 500 },
- [CHG_2_4A] = { .supplier = CHARGE_SUPPLIER_PROPRIETARY,
- .current_limit = USB_CHARGER_MAX_CURR_MA },
- [CHG_2_0A] = { .supplier = CHARGE_SUPPLIER_PROPRIETARY,
- .current_limit = USB_CHARGER_MAX_CURR_MA },
- [CHG_1_0A] = { .supplier = CHARGE_SUPPLIER_PROPRIETARY,
- .current_limit = 1000 },
- [CHG_RESERVED] = { .supplier = CHARGE_SUPPLIER_NONE,
- .current_limit = 0 },
- [CHG_CDP] = { .supplier = CHARGE_SUPPLIER_BC12_CDP,
- .current_limit = USB_CHARGER_MAX_CURR_MA },
- [CHG_SDP] = { .supplier = CHARGE_SUPPLIER_BC12_SDP,
- .current_limit = 500 },
-#if defined(CONFIG_CHARGE_RAMP_SW) || defined(CONFIG_CHARGE_RAMP_HW)
- [CHG_DCP] = { .supplier = CHARGE_SUPPLIER_BC12_DCP,
- .current_limit = USB_CHARGER_MAX_CURR_MA },
-#else
- [CHG_DCP] = { .supplier = CHARGE_SUPPLIER_BC12_DCP,
- .current_limit = 500 },
-#endif
-};
-
-#define GPIO_BATT_PRES_ODL_PATH DT_PATH(named_gpios, ec_batt_pres_odl)
-#define GPIO_BATT_PRES_ODL_PORT DT_GPIO_PIN(GPIO_BATT_PRES_ODL_PATH, gpios)
-
-#define GPIO_ACOK_OD_PATH DT_PATH(named_gpios, acok_od)
-#define GPIO_ACOK_OD_PORT DT_GPIO_PIN(GPIO_ACOK_OD_PATH, gpios)
-
-static void test_bc12_pi3usb9201_host_mode(void)
-{
- struct i2c_emul *emul = pi3usb9201_emul_get(PI3USB9201_ORD);
- uint8_t a, b;
-
- /*
- * Pretend that the USB-C Port Manager (TCPMv2) has set the port data
- * role to DFP.
- */
- task_set_event(TASK_ID_USB_CHG_P0, USB_CHG_EVENT_DR_DFP);
- msleep(1);
- /*
- * Expect the pi3usb9201 driver to configure CDP host mode and unmask
- * interrupts.
- */
- pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_1, &a);
- b = PI3USB9201_CDP_HOST_MODE << PI3USB9201_REG_CTRL_1_MODE_SHIFT;
- zassert_equal(a, b, NULL);
-
- /* Pretend that a device has been plugged in. */
- msleep(500);
- pi3usb9201_emul_set_reg(emul, PI3USB9201_REG_HOST_STS,
- PI3USB9201_REG_HOST_STS_DEV_PLUG);
- task_set_event(TASK_ID_USB_CHG_P0, USB_CHG_EVENT_BC12);
- msleep(1);
- /* Expect the pi3usb9201 driver to configure SDP host mode. */
- pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_1, &a);
- b = PI3USB9201_SDP_HOST_MODE << PI3USB9201_REG_CTRL_1_MODE_SHIFT;
- zassert_equal(a, b, NULL);
- pi3usb9201_emul_set_reg(emul, PI3USB9201_REG_HOST_STS, 0);
-
- /* Pretend that a device has been unplugged. */
- msleep(500);
- pi3usb9201_emul_set_reg(emul, PI3USB9201_REG_HOST_STS,
- PI3USB9201_REG_HOST_STS_DEV_UNPLUG);
- task_set_event(TASK_ID_USB_CHG_P0, USB_CHG_EVENT_BC12);
- msleep(1);
- /* Expect the pi3usb9201 driver to configure CDP host mode. */
- pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_1, &a);
- b = PI3USB9201_CDP_HOST_MODE << PI3USB9201_REG_CTRL_1_MODE_SHIFT;
- zassert_equal(a, b, NULL);
- pi3usb9201_emul_set_reg(emul, PI3USB9201_REG_HOST_STS, 0);
-}
-
-static void test_bc12_pi3usb9201_client_mode(
- enum pi3usb9201_client_sts detect_result,
- enum charge_supplier supplier, int current_limit)
-{
- struct i2c_emul *emul = pi3usb9201_emul_get(PI3USB9201_ORD);
- uint8_t a, b;
- int port, voltage;
-
- /*
- * Pretend that the USB-C Port Manager (TCPMv2) has set the port data
- * role to UFP and decided charging from the port is allowed.
- */
- msleep(500);
- task_set_event(TASK_ID_USB_CHG_P0, USB_CHG_EVENT_DR_UFP);
- charge_manager_update_dualrole(USBC_PORT_C0, CAP_DEDICATED);
- msleep(1);
- /*
- * Expect the pi3usb9201 driver to configure client mode and start
- * detection.
- */
- pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_1, &a);
- b = PI3USB9201_CLIENT_MODE << PI3USB9201_REG_CTRL_1_MODE_SHIFT;
- zassert_equal(a, b, NULL);
- pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_2, &a);
- b = PI3USB9201_REG_CTRL_2_START_DET;
- zassert_equal(a, b, NULL);
-
- /* Pretend that detection completed. */
- msleep(500);
- pi3usb9201_emul_set_reg(emul, PI3USB9201_REG_CLIENT_STS,
- 1 << detect_result);
- task_set_event(TASK_ID_USB_CHG_P0, USB_CHG_EVENT_BC12);
- msleep(1);
- /* Expect the pi3usb9201 driver to clear the start bit. */
- pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_2, &a);
- zassert_equal(a, 0, NULL);
- pi3usb9201_emul_set_reg(emul, PI3USB9201_REG_CLIENT_STS, 0);
- /*
- * Expect the charge manager to select the detected BC1.2 supplier.
- */
- port = CHARGE_PORT_NONE;
- voltage = 0;
- if (supplier != CHARGE_SUPPLIER_NONE) {
- port = USBC_PORT_C0;
- voltage = USB_CHARGER_VOLTAGE_MV;
- }
- zassert_equal(charge_manager_get_active_charge_port(),
- port, NULL);
- zassert_equal(charge_manager_get_supplier(),
- supplier, NULL);
- zassert_equal(charge_manager_get_charger_current(),
- current_limit, NULL);
- zassert_equal(charge_manager_get_charger_voltage(),
- voltage, NULL);
-
- /*
- * Pretend that the USB-C Port Manager (TCPMv2) has set the port data
- * role to disconnected.
- */
- msleep(500);
- task_set_event(TASK_ID_USB_CHG_P0, USB_CHG_EVENT_CC_OPEN);
- msleep(1);
- /*
- * Expect the pi3usb9201 driver to configure power down mode and mask
- * interrupts.
- */
- pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_1, &a);
- b = PI3USB9201_POWER_DOWN << PI3USB9201_REG_CTRL_1_MODE_SHIFT;
- b |= PI3USB9201_REG_CTRL_1_INT_MASK;
- zassert_equal(a, b, NULL);
- /* Expect the charge manager to have no active supplier. */
- zassert_equal(charge_manager_get_active_charge_port(),
- CHARGE_PORT_NONE, NULL);
- zassert_equal(charge_manager_get_supplier(),
- CHARGE_SUPPLIER_NONE, NULL);
- zassert_equal(charge_manager_get_charger_current(), 0, NULL);
- zassert_equal(charge_manager_get_charger_voltage(), 0, NULL);
-}
-
-/*
- * PI3USB9201 is a dual-role BC1.2 charger detector/advertiser used on USB
- * ports. It can be programmed to operate in host mode or client mode through
- * I2C. When operating as a host, PI3USB9201 enables BC1.2 SDP/CDP/DCP
- * advertisement to the attached USB devices via the D+/- connection. When
- * operating as a client, PI3USB9201 starts BC1.2 detection to detect the
- * attached host type. In both host mode and client mode, the detection results
- * are reported through I2C to the controller.
- */
-static void test_bc12_pi3usb9201(void)
-{
- const struct device *batt_pres_dev =
- DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_BATT_PRES_ODL_PATH, gpios));
- const struct device *acok_dev =
- DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_ACOK_OD_PATH, gpios));
- struct i2c_emul *emul = pi3usb9201_emul_get(PI3USB9201_ORD);
- uint8_t a, b;
-
- /* Pretend we have battery and AC so charging works normally. */
- zassert_ok(gpio_emul_input_set(batt_pres_dev,
- GPIO_BATT_PRES_ODL_PORT, 0), NULL);
- zassert_equal(BP_YES, battery_is_present(), NULL);
- zassert_ok(gpio_emul_input_set(acok_dev, GPIO_ACOK_OD_PORT, 1), NULL);
- msleep(CONFIG_EXTPOWER_DEBOUNCE_MS + 1);
- zassert_equal(1, extpower_is_present(), NULL);
-
- /* Wait long enough for TCPMv2 to be idle. */
- msleep(2000);
-
- /*
- * Pretend that the USB-C Port Manager (TCPMv2) has set the port data
- * role to disconnected.
- */
- task_set_event(TASK_ID_USB_CHG_P0, USB_CHG_EVENT_CC_OPEN);
- task_set_event(TASK_ID_USB_CHG_P1, USB_CHG_EVENT_CC_OPEN);
- msleep(1);
- /*
- * Expect the pi3usb9201 driver to configure power down mode and mask
- * interrupts.
- */
- pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_1, &a);
- b = PI3USB9201_POWER_DOWN << PI3USB9201_REG_CTRL_1_MODE_SHIFT;
- b |= PI3USB9201_REG_CTRL_1_INT_MASK;
- zassert_equal(a, b, NULL);
-
- test_bc12_pi3usb9201_host_mode();
-
- for (int c = CHG_OTHER; c <= CHG_DCP; c++) {
- test_bc12_pi3usb9201_client_mode(c,
- bc12_chg_limits[c].supplier,
- bc12_chg_limits[c].current_limit);
- }
-}
-
-void test_suite_bc12(void)
-{
- ztest_test_suite(bc12,
- ztest_user_unit_test(test_bc12_pi3usb9201));
- ztest_run_test_suite(bc12);
-}
diff --git a/zephyr/test/drivers/src/bma2x2.c b/zephyr/test/drivers/src/bma2x2.c
deleted file mode 100644
index 8b77464b48..0000000000
--- a/zephyr/test/drivers/src/bma2x2.c
+++ /dev/null
@@ -1,926 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <zephyr.h>
-#include <ztest.h>
-
-#include "common.h"
-#include "i2c.h"
-#include "emul/emul_bma255.h"
-#include "emul/emul_common_i2c.h"
-
-#include "accelgyro.h"
-#include "motion_sense.h"
-#include "driver/accel_bma2x2.h"
-
-/** How accurate comparision of vectors should be. */
-#define V_EPS 8
-
-#define EMUL_LABEL DT_NODELABEL(bma_emul)
-
-#define BMA_ORD DT_DEP_ORD(EMUL_LABEL)
-
-/** Mutex for test motion sensor */
-static mutex_t sensor_mutex;
-
-/** Rotation used in some tests */
-static const mat33_fp_t test_rotation = {
- { 0, FLOAT_TO_FP(1), 0},
- { FLOAT_TO_FP(-1), 0, 0},
- { 0, 0, FLOAT_TO_FP(-1)}
-};
-
-/** Rotate given vector by test rotation */
-void rotate_int3v_by_test_rotation(int16_t *v)
-{
- int16_t t;
-
- t = v[0];
- v[0] = -v[1];
- v[1] = t;
- v[2] = -v[2];
-}
-
-static struct accelgyro_saved_data_t acc_data;
-
-/** Mock minimal motion sensor setup required for bma2x2 driver test */
-static struct motion_sensor_t ms = {
- .name = "bma_emul",
- .type = MOTIONSENSE_TYPE_ACCEL,
- .drv = &bma2x2_accel_drv,
- .mutex = &sensor_mutex,
- .drv_data = &acc_data,
- .port = NAMED_I2C(accel),
- .i2c_spi_addr_flags = DT_REG_ADDR(EMUL_LABEL),
- .rot_standard_ref = NULL,
- .current_range = 0,
-};
-
-/** Set emulator offset values to vector of three int16_t */
-static void set_emul_offset(struct i2c_emul *emul, int16_t *offset)
-{
- bma_emul_set_off(emul, BMA_EMUL_AXIS_X, offset[0]);
- bma_emul_set_off(emul, BMA_EMUL_AXIS_Y, offset[1]);
- bma_emul_set_off(emul, BMA_EMUL_AXIS_Z, offset[2]);
-}
-
-/** Save emulator offset values to vector of three int16_t */
-static void get_emul_offset(struct i2c_emul *emul, int16_t *offset)
-{
- offset[0] = bma_emul_get_off(emul, BMA_EMUL_AXIS_X);
- offset[1] = bma_emul_get_off(emul, BMA_EMUL_AXIS_Y);
- offset[2] = bma_emul_get_off(emul, BMA_EMUL_AXIS_Z);
-}
-
-/** Set emulator accelerometer values to vector of three int16_t */
-static void set_emul_acc(struct i2c_emul *emul, int16_t *acc)
-{
- bma_emul_set_acc(emul, BMA_EMUL_AXIS_X, acc[0]);
- bma_emul_set_acc(emul, BMA_EMUL_AXIS_Y, acc[1]);
- bma_emul_set_acc(emul, BMA_EMUL_AXIS_Z, acc[2]);
-}
-
-/** Convert accelerometer read to units used by emulator */
-static void drv_acc_to_emul(intv3_t drv, int range, int16_t *out)
-{
- const int scale = MOTION_SCALING_FACTOR / BMA_EMUL_1G;
-
- out[0] = drv[0] * range / scale;
- out[1] = drv[1] * range / scale;
- out[2] = drv[2] * range / scale;
-}
-
-/** Compare two vectors of three int16_t */
-static void compare_int3v_f(int16_t *exp_v, int16_t *v, int line)
-{
- int i;
-
- for (i = 0; i < 3; i++) {
- zassert_within(exp_v[i], v[i], V_EPS,
- "Expected [%d; %d; %d], got [%d; %d; %d]; line: %d",
- exp_v[0], exp_v[1], exp_v[2], v[0], v[1], v[2], line);
- }
-}
-#define compare_int3v(exp_v, v) compare_int3v_f(exp_v, v, __LINE__)
-
-/** Data for reset fail function */
-struct reset_func_data {
- /** Fail for given attempts */
- int fail_attempts;
- /** Do not fail for given attempts */
- int ok_before_fail;
- /** Reset register value after given attempts */
- int reset_value;
-};
-
-/**
- * Custom emulator function used in init test. It returns cmd soft when reset
- * register is accessed data.reset_value times. Error is returned after
- * accessing register data.ok_before_fail times. Error is returned during next
- * data.fail_attempts times.
- */
-static int emul_read_reset(struct i2c_emul *emul, int reg, uint8_t *buf,
- int bytes, void *data)
-{
- struct reset_func_data *d = data;
-
- reg = bma_emul_access_reg(emul, reg, bytes, true /* = read */);
- if (reg != BMA2x2_RST_ADDR) {
- return 1;
- }
-
- if (d->reset_value > 0) {
- d->reset_value--;
- bma_emul_set_reg(emul, BMA2x2_RST_ADDR, BMA2x2_CMD_SOFT_RESET);
- } else {
- bma_emul_set_reg(emul, BMA2x2_RST_ADDR, 0);
- }
-
- if (d->ok_before_fail > 0) {
- d->ok_before_fail--;
- return 1;
- }
-
- if (d->fail_attempts > 0) {
- d->fail_attempts--;
- return -EIO;
- }
-
- return 1;
-}
-
-/**
- * Test get offset with and without rotation. Also test behaviour on I2C error.
- */
-static void test_bma_get_offset(void)
-{
- struct i2c_emul *emul;
- int16_t ret_offset[3];
- int16_t exp_offset[3];
- int16_t temp;
-
- emul = bma_emul_get(BMA_ORD);
-
- /* Test fail on each axis */
- i2c_common_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_X_AXIS_ADDR);
- zassert_equal(EC_ERROR_INVAL,
- ms.drv->get_offset(&ms, ret_offset, &temp), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_Y_AXIS_ADDR);
- zassert_equal(EC_ERROR_INVAL,
- ms.drv->get_offset(&ms, ret_offset, &temp), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_Z_AXIS_ADDR);
- zassert_equal(EC_ERROR_INVAL,
- ms.drv->get_offset(&ms, ret_offset, &temp), NULL);
-
- /* Do not fail on read */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Set emulator offset */
- exp_offset[0] = BMA_EMUL_1G / 10;
- exp_offset[1] = BMA_EMUL_1G / 20;
- exp_offset[2] = -(int)BMA_EMUL_1G / 30;
- set_emul_offset(emul, exp_offset);
- /* Disable rotation */
- ms.rot_standard_ref = NULL;
-
- /* Test get offset without rotation */
- zassert_equal(EC_SUCCESS, ms.drv->get_offset(&ms, ret_offset, &temp),
- NULL);
- zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL);
- compare_int3v(exp_offset, ret_offset);
-
- /* Setup rotation and rotate expected offset */
- ms.rot_standard_ref = &test_rotation;
- rotate_int3v_by_test_rotation(exp_offset);
-
- /* Test get offset with rotation */
- zassert_equal(EC_SUCCESS, ms.drv->get_offset(&ms, ret_offset, &temp),
- NULL);
- zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL);
- compare_int3v(exp_offset, ret_offset);
-}
-
-/**
- * Test set offset with and without rotation. Also test behaviour on I2C error.
- */
-static void test_bma_set_offset(void)
-{
- struct i2c_emul *emul;
- int16_t ret_offset[3];
- int16_t exp_offset[3];
- int16_t temp = 0;
-
- emul = bma_emul_get(BMA_ORD);
-
- /* Test fail on each axis */
- i2c_common_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_X_AXIS_ADDR);
- zassert_equal(EC_ERROR_INVAL, ms.drv->set_offset(&ms, exp_offset, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_Y_AXIS_ADDR);
- zassert_equal(EC_ERROR_INVAL, ms.drv->set_offset(&ms, exp_offset, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_Z_AXIS_ADDR);
- zassert_equal(EC_ERROR_INVAL, ms.drv->set_offset(&ms, exp_offset, temp),
- NULL);
-
- /* Do not fail on write */
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Set input offset */
- exp_offset[0] = BMA_EMUL_1G / 10;
- exp_offset[1] = BMA_EMUL_1G / 20;
- exp_offset[2] = -(int)BMA_EMUL_1G / 30;
- /* Disable rotation */
- ms.rot_standard_ref = NULL;
-
- /* Test set offset without rotation */
- zassert_equal(EC_SUCCESS, ms.drv->set_offset(&ms, exp_offset, temp),
- NULL);
- get_emul_offset(emul, ret_offset);
- compare_int3v(exp_offset, ret_offset);
-
- /* Setup rotation and rotate input for set_offset function */
- ms.rot_standard_ref = &test_rotation;
- ret_offset[0] = exp_offset[0];
- ret_offset[1] = exp_offset[1];
- ret_offset[2] = exp_offset[2];
- rotate_int3v_by_test_rotation(ret_offset);
-
- /* Test set offset with rotation */
- zassert_equal(EC_SUCCESS, ms.drv->set_offset(&ms, ret_offset, temp),
- NULL);
- get_emul_offset(emul, ret_offset);
- compare_int3v(exp_offset, ret_offset);
-}
-
-/*
- * Try to set range and check if expected range was set in driver and in
- * emulator.
- */
-static void check_set_range_f(struct i2c_emul *emul, int range, int rnd,
- int exp_range, int line)
-{
- uint8_t exp_range_reg;
- uint8_t range_reg;
-
- zassert_equal(EC_SUCCESS, ms.drv->set_range(&ms, range, rnd),
- "set_range failed; line: %d", line);
- zassert_equal(exp_range, ms.current_range,
- "Expected range %d, got %d; line %d",
- exp_range, ms.current_range, line);
- range_reg = bma_emul_get_reg(emul, BMA2x2_RANGE_SELECT_ADDR);
- range_reg &= BMA2x2_RANGE_SELECT_MSK;
-
- switch (exp_range) {
- case 2:
- exp_range_reg = BMA2x2_RANGE_2G;
- break;
- case 4:
- exp_range_reg = BMA2x2_RANGE_4G;
- break;
- case 8:
- exp_range_reg = BMA2x2_RANGE_8G;
- break;
- case 16:
- exp_range_reg = BMA2x2_RANGE_16G;
- break;
- default:
- /* Unknown expected range */
- zassert_unreachable(
- "Expected range %d not supported by device; line %d",
- exp_range, line);
- return;
- }
-
- zassert_equal(exp_range_reg, range_reg,
- "Expected range reg 0x%x, got 0x%x; line %d",
- exp_range_reg, range_reg, line);
-}
-#define check_set_range(emul, range, rnd, exp_range) \
- check_set_range_f(emul, range, rnd, exp_range, __LINE__)
-
-/** Test set range with and without I2C errors. */
-static void test_bma_set_range(void)
-{
- struct i2c_emul *emul;
- int start_range;
-
- emul = bma_emul_get(BMA_ORD);
-
- /* Setup starting range, shouldn't be changed on error */
- start_range = 2;
- ms.current_range = start_range;
- bma_emul_set_reg(emul, BMA2x2_RANGE_SELECT_ADDR, BMA2x2_RANGE_2G);
- /* Setup emulator fail on read */
- i2c_common_emul_set_read_fail_reg(emul, BMA2x2_RANGE_SELECT_ADDR);
-
- /* Test fail on read */
- zassert_equal(EC_ERROR_INVAL, ms.drv->set_range(&ms, 12, 0), NULL);
- zassert_equal(start_range, ms.current_range, NULL);
- zassert_equal(BMA2x2_RANGE_2G,
- bma_emul_get_reg(emul, BMA2x2_RANGE_SELECT_ADDR), NULL);
- zassert_equal(EC_ERROR_INVAL, ms.drv->set_range(&ms, 12, 1), NULL);
- zassert_equal(start_range, ms.current_range, NULL);
- zassert_equal(BMA2x2_RANGE_2G,
- bma_emul_get_reg(emul, BMA2x2_RANGE_SELECT_ADDR), NULL);
-
- /* Do not fail on read */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Setup emulator fail on write */
- i2c_common_emul_set_write_fail_reg(emul, BMA2x2_RANGE_SELECT_ADDR);
-
- /* Test fail on write */
- zassert_equal(EC_ERROR_INVAL, ms.drv->set_range(&ms, 12, 0), NULL);
- zassert_equal(start_range, ms.current_range, NULL);
- zassert_equal(BMA2x2_RANGE_2G,
- bma_emul_get_reg(emul, BMA2x2_RANGE_SELECT_ADDR), NULL);
- zassert_equal(EC_ERROR_INVAL, ms.drv->set_range(&ms, 12, 1), NULL);
- zassert_equal(start_range, ms.current_range, NULL);
- zassert_equal(BMA2x2_RANGE_2G,
- bma_emul_get_reg(emul, BMA2x2_RANGE_SELECT_ADDR), NULL);
-
- /* Do not fail on write */
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test setting range with rounding down */
- check_set_range(emul, 1, 0, 2);
- check_set_range(emul, 2, 0, 2);
- check_set_range(emul, 3, 0, 2);
- check_set_range(emul, 4, 0, 4);
- check_set_range(emul, 5, 0, 4);
- check_set_range(emul, 6, 0, 4);
- check_set_range(emul, 7, 0, 4);
- check_set_range(emul, 8, 0, 8);
- check_set_range(emul, 9, 0, 8);
- check_set_range(emul, 15, 0, 8);
- check_set_range(emul, 16, 0, 16);
- check_set_range(emul, 17, 0, 16);
-
- /* Test setting range with rounding up */
- check_set_range(emul, 1, 1, 2);
- check_set_range(emul, 2, 1, 2);
- check_set_range(emul, 3, 1, 4);
- check_set_range(emul, 4, 1, 4);
- check_set_range(emul, 5, 1, 8);
- check_set_range(emul, 6, 1, 8);
- check_set_range(emul, 7, 1, 8);
- check_set_range(emul, 8, 1, 8);
- check_set_range(emul, 9, 1, 16);
- check_set_range(emul, 15, 1, 16);
- check_set_range(emul, 16, 1, 16);
- check_set_range(emul, 17, 1, 16);
-}
-
-/** Test init with and without I2C errors. */
-static void test_bma_init(void)
-{
- struct reset_func_data reset_func_data;
- struct i2c_emul *emul;
-
- emul = bma_emul_get(BMA_ORD);
-
- /* Setup emulator fail read function */
- i2c_common_emul_set_read_fail_reg(emul, BMA2x2_CHIP_ID_ADDR);
-
- /* Test fail on chip id read */
- zassert_equal(EC_ERROR_UNKNOWN, ms.drv->init(&ms), NULL);
-
- /* Disable failing on chip id read, but set wrong value */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- bma_emul_set_reg(emul, BMA2x2_CHIP_ID_ADDR, 23);
-
- /* Test wrong chip id */
- zassert_equal(EC_ERROR_ACCESS_DENIED, ms.drv->init(&ms), NULL);
-
- /* Set correct chip id, but fail on reset reg read */
- i2c_common_emul_set_read_fail_reg(emul, BMA2x2_RST_ADDR);
- bma_emul_set_reg(emul, BMA2x2_CHIP_ID_ADDR, BMA255_CHIP_ID_MAJOR);
-
- /* Test fail on reset register read */
- zassert_equal(EC_ERROR_INVAL, ms.drv->init(&ms), NULL);
-
- /* Do not fail on read */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Setup emulator fail on write */
- i2c_common_emul_set_write_fail_reg(emul, BMA2x2_RST_ADDR);
-
- /* Test fail on reset register write */
- zassert_equal(EC_ERROR_INVAL, ms.drv->init(&ms), NULL);
-
- /* Do not fail on write */
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Setup emulator fail reset read function */
- reset_func_data.ok_before_fail = 1;
- reset_func_data.fail_attempts = 100;
- reset_func_data.reset_value = 0;
- i2c_common_emul_set_read_func(emul, emul_read_reset, &reset_func_data);
-
- /* Test fail on too many reset read errors */
- zassert_equal(EC_ERROR_TIMEOUT, ms.drv->init(&ms), NULL);
-
- /* Test success after reset read errors */
- reset_func_data.ok_before_fail = 1;
- reset_func_data.fail_attempts = 3;
- zassert_equal(EC_RES_SUCCESS, ms.drv->init(&ms), NULL);
-
- /* Test success without read errors */
- reset_func_data.fail_attempts = 0;
- zassert_equal(EC_RES_SUCCESS, ms.drv->init(&ms), NULL);
-
- /* Test fail on too many reset read wrong value */
- reset_func_data.fail_attempts = 0;
- reset_func_data.reset_value = 100;
- zassert_equal(EC_ERROR_TIMEOUT, ms.drv->init(&ms), NULL);
-
- /* Test success on few reset read wrong value */
- reset_func_data.fail_attempts = 0;
- reset_func_data.reset_value = 4;
- zassert_equal(EC_RES_SUCCESS, ms.drv->init(&ms), NULL);
-
- /* Remove custom emulator read function */
- i2c_common_emul_set_read_func(emul, NULL, NULL);
-}
-
-/*
- * Try to set data rate and check if expected rate was set in driver and in
- * emulator.
- */
-static void check_set_rate_f(struct i2c_emul *emul, int rate, int rnd,
- int exp_rate, int line)
-{
- uint8_t exp_rate_reg;
- uint8_t rate_reg;
- int drv_rate;
-
- zassert_equal(EC_SUCCESS, ms.drv->set_data_rate(&ms, rate, rnd),
- "set_data_rate failed; line: %d", line);
- drv_rate = ms.drv->get_data_rate(&ms);
- zassert_equal(exp_rate, drv_rate, "Expected rate %d, got %d; line %d",
- exp_rate, drv_rate, line);
- rate_reg = bma_emul_get_reg(emul, BMA2x2_BW_SELECT_ADDR);
- rate_reg &= BMA2x2_BW_MSK;
-
- switch (exp_rate) {
- case 7812:
- exp_rate_reg = BMA2x2_BW_7_81HZ;
- break;
- case 15625:
- exp_rate_reg = BMA2x2_BW_15_63HZ;
- break;
- case 31250:
- exp_rate_reg = BMA2x2_BW_31_25HZ;
- break;
- case 62500:
- exp_rate_reg = BMA2x2_BW_62_50HZ;
- break;
- case 125000:
- exp_rate_reg = BMA2x2_BW_125HZ;
- break;
- case 250000:
- exp_rate_reg = BMA2x2_BW_250HZ;
- break;
- case 500000:
- exp_rate_reg = BMA2x2_BW_500HZ;
- break;
- case 1000000:
- exp_rate_reg = BMA2x2_BW_1000HZ;
- break;
- default:
- /* Unknown expected rate */
- zassert_unreachable(
- "Expected rate %d not supported by device; line %d",
- exp_rate, line);
- return;
- }
-
- zassert_equal(exp_rate_reg, rate_reg,
- "Expected rate reg 0x%x, got 0x%x; line %d",
- exp_rate_reg, rate_reg, line);
-}
-#define check_set_rate(emul, rate, rnd, exp_rate) \
- check_set_rate_f(emul, rate, rnd, exp_rate, __LINE__)
-
-/** Test set and get rate with and without I2C errors. */
-static void test_bma_rate(void)
-{
- struct i2c_emul *emul;
- uint8_t reg_rate;
- int drv_rate;
-
- emul = bma_emul_get(BMA_ORD);
-
- /* Test setting rate with rounding down */
- check_set_rate(emul, 1, 0, 7812);
- check_set_rate(emul, 1, 0, 7812);
- check_set_rate(emul, 7811, 0, 7812);
- check_set_rate(emul, 7812, 0, 7812);
- check_set_rate(emul, 7813, 0, 7812);
- check_set_rate(emul, 15624, 0, 7812);
- check_set_rate(emul, 15625, 0, 15625);
- check_set_rate(emul, 15626, 0, 15625);
- check_set_rate(emul, 31249, 0, 15625);
- check_set_rate(emul, 31250, 0, 31250);
- check_set_rate(emul, 31251, 0, 31250);
- check_set_rate(emul, 62499, 0, 31250);
- check_set_rate(emul, 62500, 0, 62500);
- check_set_rate(emul, 62501, 0, 62500);
- check_set_rate(emul, 124999, 0, 62500);
- check_set_rate(emul, 125000, 0, 125000);
- check_set_rate(emul, 125001, 0, 125000);
- check_set_rate(emul, 249999, 0, 125000);
- check_set_rate(emul, 250000, 0, 250000);
- check_set_rate(emul, 250001, 0, 250000);
- check_set_rate(emul, 499999, 0, 250000);
- check_set_rate(emul, 500000, 0, 500000);
- check_set_rate(emul, 500001, 0, 500000);
- check_set_rate(emul, 999999, 0, 500000);
- check_set_rate(emul, 1000000, 0, 1000000);
- check_set_rate(emul, 1000001, 0, 1000000);
- check_set_rate(emul, 2000000, 0, 1000000);
-
- /* Test setting rate with rounding up */
- check_set_rate(emul, 1, 1, 7812);
- check_set_rate(emul, 1, 1, 7812);
- check_set_rate(emul, 7811, 1, 7812);
- check_set_rate(emul, 7812, 1, 7812);
- check_set_rate(emul, 7813, 1, 15625);
- check_set_rate(emul, 15624, 1, 15625);
- check_set_rate(emul, 15625, 1, 15625);
- check_set_rate(emul, 15626, 1, 31250);
- check_set_rate(emul, 31249, 1, 31250);
- check_set_rate(emul, 31250, 1, 31250);
- check_set_rate(emul, 31251, 1, 62500);
- check_set_rate(emul, 62499, 1, 62500);
- check_set_rate(emul, 62500, 1, 62500);
- check_set_rate(emul, 62501, 1, 125000);
- check_set_rate(emul, 124999, 1, 125000);
- check_set_rate(emul, 125000, 1, 125000);
- check_set_rate(emul, 125001, 1, 250000);
- check_set_rate(emul, 249999, 1, 250000);
- check_set_rate(emul, 250000, 1, 250000);
- check_set_rate(emul, 250001, 1, 500000);
- check_set_rate(emul, 499999, 1, 500000);
- check_set_rate(emul, 500000, 1, 500000);
- check_set_rate(emul, 500001, 1, 1000000);
- check_set_rate(emul, 999999, 1, 1000000);
- check_set_rate(emul, 1000000, 1, 1000000);
- check_set_rate(emul, 1000001, 1, 1000000);
- check_set_rate(emul, 2000000, 1, 1000000);
-
- /* Current rate shouldn't be changed on error */
- drv_rate = ms.drv->get_data_rate(&ms);
- reg_rate = bma_emul_get_reg(emul, BMA2x2_BW_SELECT_ADDR);
-
- /* Setup emulator fail on read */
- i2c_common_emul_set_read_fail_reg(emul, BMA2x2_BW_SELECT_ADDR);
-
- /* Test fail on read */
- zassert_equal(EC_ERROR_INVAL, ms.drv->set_data_rate(&ms, 15625, 0),
- NULL);
- zassert_equal(drv_rate, ms.drv->get_data_rate(&ms), NULL);
- zassert_equal(reg_rate, bma_emul_get_reg(emul, BMA2x2_BW_SELECT_ADDR),
- NULL);
- zassert_equal(EC_ERROR_INVAL, ms.drv->set_data_rate(&ms, 15625, 1),
- NULL);
- zassert_equal(drv_rate, ms.drv->get_data_rate(&ms), NULL);
- zassert_equal(reg_rate,
- bma_emul_get_reg(emul, BMA2x2_BW_SELECT_ADDR), NULL);
-
- /* Do not fail on read */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Setup emulator fail on write */
- i2c_common_emul_set_write_fail_reg(emul, BMA2x2_BW_SELECT_ADDR);
-
- /* Test fail on write */
- zassert_equal(EC_ERROR_INVAL, ms.drv->set_data_rate(&ms, 15625, 0),
- NULL);
- zassert_equal(drv_rate, ms.drv->get_data_rate(&ms), NULL);
- zassert_equal(reg_rate, bma_emul_get_reg(emul, BMA2x2_BW_SELECT_ADDR),
- NULL);
- zassert_equal(EC_ERROR_INVAL, ms.drv->set_data_rate(&ms, 15625, 1),
- NULL);
- zassert_equal(drv_rate, ms.drv->get_data_rate(&ms), NULL);
- zassert_equal(reg_rate, bma_emul_get_reg(emul, BMA2x2_BW_SELECT_ADDR),
- NULL);
-
- /* Do not fail on write */
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-}
-
-/** Test read with and without I2C errors. */
-static void test_bma_read(void)
-{
- struct i2c_emul *emul;
- int16_t ret_acc[3];
- int16_t exp_acc[3];
- intv3_t ret_acc_v;
-
- emul = bma_emul_get(BMA_ORD);
-
- /* Set offset 0 to simplify test */
- bma_emul_set_off(emul, BMA_EMUL_AXIS_X, 0);
- bma_emul_set_off(emul, BMA_EMUL_AXIS_Y, 0);
- bma_emul_set_off(emul, BMA_EMUL_AXIS_Z, 0);
-
- /* Test fail on each axis */
- i2c_common_emul_set_read_fail_reg(emul, BMA2x2_X_AXIS_LSB_ADDR);
- zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMA2x2_X_AXIS_MSB_ADDR);
- zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Y_AXIS_LSB_ADDR);
- zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Y_AXIS_MSB_ADDR);
- zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Z_AXIS_LSB_ADDR);
- zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Z_AXIS_MSB_ADDR);
- zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL);
-
- /* Do not fail on read */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Set input accelerometer values */
- exp_acc[0] = BMA_EMUL_1G / 10;
- exp_acc[1] = BMA_EMUL_1G / 20;
- exp_acc[2] = -(int)BMA_EMUL_1G / 30;
- set_emul_acc(emul, exp_acc);
- /* Disable rotation */
- ms.rot_standard_ref = NULL;
- /* Set range to 2G */
- zassert_equal(EC_SUCCESS, ms.drv->set_range(&ms, 2, 0), NULL);
-
- /* Test read without rotation */
- zassert_equal(EC_SUCCESS, ms.drv->read(&ms, ret_acc_v), NULL);
- drv_acc_to_emul(ret_acc_v, 2, ret_acc);
- compare_int3v(exp_acc, ret_acc);
-
- /* Set range to 4G */
- zassert_equal(EC_SUCCESS, ms.drv->set_range(&ms, 4, 0), NULL);
-
- /* Test read without rotation */
- zassert_equal(EC_SUCCESS, ms.drv->read(&ms, ret_acc_v), NULL);
- drv_acc_to_emul(ret_acc_v, 4, ret_acc);
- compare_int3v(exp_acc, ret_acc);
-
- /* Setup rotation and rotate expected vector */
- ms.rot_standard_ref = &test_rotation;
- rotate_int3v_by_test_rotation(exp_acc);
- /* Set range to 2G */
- zassert_equal(EC_SUCCESS, ms.drv->set_range(&ms, 2, 0), NULL);
-
- /* Test read with rotation */
- zassert_equal(EC_SUCCESS, ms.drv->read(&ms, ret_acc_v), NULL);
- drv_acc_to_emul(ret_acc_v, 2, ret_acc);
- compare_int3v(exp_acc, ret_acc);
-
- /* Set range to 4G */
- zassert_equal(EC_SUCCESS, ms.drv->set_range(&ms, 4, 0), NULL);
-
- /* Test read with rotation */
- zassert_equal(EC_SUCCESS, ms.drv->read(&ms, ret_acc_v), NULL);
- drv_acc_to_emul(ret_acc_v, 4, ret_acc);
- compare_int3v(exp_acc, ret_acc);
-}
-
-/** Data for functions used in perform_calib test */
-struct calib_func_data {
- /** Time when offset compensation where triggered */
- int calib_start;
- /** Time how long offset cal ready should be unset */
- int time;
- /** Flag indicate if read should fail after compensation is triggered */
- int read_fail;
-};
-
-/**
- * Custom emulator read function used in perform_calib test. It controls if
- * cal ready bit in offset control register should be set. It is set after
- * data.time miliseconds passed from data.calib_start time. Function returns
- * error when offset control register is accessed when cal ready bit is not set
- * and data.read_fail is not zero.
- */
-static int emul_read_calib_func(struct i2c_emul *emul, int reg, uint8_t *val,
- int bytes, void *data)
-{
- struct calib_func_data *d = data;
- uint8_t reg_val;
- int cur_time;
-
- reg = bma_emul_access_reg(emul, reg, bytes, true /* = read */);
- if (reg != BMA2x2_OFFSET_CTRL_ADDR) {
- return 1;
- }
-
- reg_val = bma_emul_get_reg(emul, BMA2x2_OFFSET_CTRL_ADDR);
- cur_time = k_uptime_get_32();
- if (cur_time - d->calib_start < d->time) {
- if (d->read_fail) {
- return -EIO;
- }
- reg_val &= ~BMA2x2_OFFSET_CAL_READY;
- } else {
- reg_val |= BMA2x2_OFFSET_CAL_READY;
- }
- bma_emul_set_reg(emul, BMA2x2_OFFSET_CTRL_ADDR, reg_val);
-
- return 1;
-}
-
-/**
- * Custom emulator write function used in perform_calib test. It sets
- * calib_start field in data with time when offset compensation process was
- * triggerd.
- */
-static int emul_write_calib_func(struct i2c_emul *emul, int reg, uint8_t val,
- int bytes, void *data)
-{
- struct calib_func_data *d = data;
-
- reg = bma_emul_access_reg(emul, reg, bytes, false /* = read */);
- if (reg != BMA2x2_OFFSET_CTRL_ADDR) {
- return 1;
- }
-
- if (val & BMA2x2_OFFSET_TRIGGER_MASK) {
- d->calib_start = k_uptime_get_32();
- }
-
- return 1;
-}
-
-/** Test offset compensation with and without I2C errors. */
-static void test_bma_perform_calib(void)
-{
- struct calib_func_data func_data;
- struct i2c_emul *emul;
- int16_t start_off[3];
- int16_t exp_off[3];
- int16_t ret_off[3];
- int range;
- int rate;
- mat33_fp_t rot = {
- { FLOAT_TO_FP(1), 0, 0},
- { 0, FLOAT_TO_FP(1), 0},
- { 0, 0, FLOAT_TO_FP(-1)}
- };
-
- emul = bma_emul_get(BMA_ORD);
-
- /* Range and rate cannot change after calibration */
- range = 4;
- rate = 125000;
- zassert_equal(EC_SUCCESS, ms.drv->set_range(&ms, range, 0), NULL);
- zassert_equal(EC_SUCCESS, ms.drv->set_data_rate(&ms, rate, 0), NULL);
-
- /* Set offset 0 */
- start_off[0] = 0;
- start_off[1] = 0;
- start_off[2] = 0;
- set_emul_offset(emul, start_off);
-
- /* Set input accelerometer values */
- exp_off[0] = BMA_EMUL_1G / 10;
- exp_off[1] = BMA_EMUL_1G / 20;
- exp_off[2] = -(int)BMA_EMUL_1G / 30;
- set_emul_acc(emul, exp_off);
-
- /*
- * Expected offset is [-X, -Y, 1G - Z] for no rotation or positive
- * rotation on Z axis
- */
- exp_off[0] = -exp_off[0];
- exp_off[1] = -exp_off[1];
- exp_off[2] = BMA_EMUL_1G - exp_off[2];
-
- /* Setup emulator calibration functions */
- i2c_common_emul_set_read_func(emul, emul_read_calib_func, &func_data);
- i2c_common_emul_set_write_func(emul, emul_write_calib_func, &func_data);
-
- /* Setup emulator to fail on first access to offset control register */
- func_data.calib_start = k_uptime_get_32();
- func_data.read_fail = 1;
- func_data.time = 1000000;
-
- /* Test success on disabling calibration */
- zassert_equal(EC_SUCCESS, ms.drv->perform_calib(&ms, 0), NULL);
- zassert_equal(range, ms.current_range, NULL);
- zassert_equal(rate, ms.drv->get_data_rate(&ms), NULL);
-
- /* Test fail on first access to offset control register */
- zassert_equal(EC_ERROR_INVAL, ms.drv->perform_calib(&ms, 1), NULL);
- zassert_equal(range, ms.current_range, NULL);
- zassert_equal(rate, ms.drv->get_data_rate(&ms), NULL);
-
- /* Setup emulator to return cal not ready */
- func_data.calib_start = k_uptime_get_32();
- func_data.read_fail = 0;
- func_data.time = 1000000;
-
- /* Test fail on cal not ready */
- zassert_equal(EC_ERROR_ACCESS_DENIED, ms.drv->perform_calib(&ms, 1),
- NULL);
- zassert_equal(range, ms.current_range, NULL);
- zassert_equal(rate, ms.drv->get_data_rate(&ms), NULL);
-
- /*
- * Setup emulator to fail on access to offset control register after
- * triggering offset compensation
- */
- func_data.calib_start = 0;
- func_data.read_fail = 1;
- func_data.time = 160;
-
- /* Test fail on read during offset compensation */
- zassert_equal(EC_ERROR_INVAL, ms.drv->perform_calib(&ms, 1), NULL);
- zassert_equal(range, ms.current_range, NULL);
- zassert_equal(rate, ms.drv->get_data_rate(&ms), NULL);
-
- /*
- * Setup emulator to return cal not ready for 1s after triggering
- * offset compensation
- */
- func_data.calib_start = 0;
- func_data.read_fail = 0;
- func_data.time = 1000;
-
- zassert_equal(EC_RES_TIMEOUT, ms.drv->perform_calib(&ms, 1), NULL);
- zassert_equal(range, ms.current_range, NULL);
- zassert_equal(rate, ms.drv->get_data_rate(&ms), NULL);
-
- /*
- * Setup emulator to return cal not ready for 160ms after triggering
- * offset compensation
- */
- func_data.calib_start = 0;
- func_data.read_fail = 0;
- func_data.time = 160;
- /* Disable rotation */
- ms.rot_standard_ref = NULL;
-
- /* Test successful offset compenastion without rotation */
- zassert_equal(EC_SUCCESS, ms.drv->perform_calib(&ms, 1), NULL);
- zassert_equal(range, ms.current_range, NULL);
- zassert_equal(rate, ms.drv->get_data_rate(&ms), NULL);
- get_emul_offset(emul, ret_off);
- compare_int3v(exp_off, ret_off);
-
- func_data.calib_start = 0;
- /* Enable rotation with negative value on Z axis */
- ms.rot_standard_ref = &rot;
- /* Expected offset -1G - accelerometer[Z] */
- exp_off[2] = -((int)BMA_EMUL_1G) - bma_emul_get_acc(emul,
- BMA_EMUL_AXIS_Z);
-
- /* Test successful offset compenastion with negative Z rotation */
- zassert_equal(EC_SUCCESS, ms.drv->perform_calib(&ms, 1), NULL);
- zassert_equal(range, ms.current_range, NULL);
- zassert_equal(rate, ms.drv->get_data_rate(&ms), NULL);
- get_emul_offset(emul, ret_off);
- compare_int3v(exp_off, ret_off);
-
- func_data.calib_start = 0;
- /* Set positive rotation on Z axis */
- rot[2][2] = FLOAT_TO_FP(1);
- /* Expected offset 1G - accelerometer[Z] */
- exp_off[2] = BMA_EMUL_1G - bma_emul_get_acc(emul, BMA_EMUL_AXIS_Z);
-
- /* Test successful offset compenastion with positive Z rotation */
- zassert_equal(EC_SUCCESS, ms.drv->perform_calib(&ms, 1), NULL);
- zassert_equal(range, ms.current_range, NULL);
- zassert_equal(rate, ms.drv->get_data_rate(&ms), NULL);
- get_emul_offset(emul, ret_off);
- compare_int3v(exp_off, ret_off);
-
- /* Remove custom emulator functions */
- i2c_common_emul_set_read_func(emul, NULL, NULL);
- i2c_common_emul_set_write_func(emul, NULL, NULL);
-}
-
-/** Test get resolution. */
-static void test_bma_get_resolution(void)
-{
- /* Resolution should be always 12 bits */
- zassert_equal(12, ms.drv->get_resolution(&ms), NULL);
-}
-
-void test_suite_bma2x2(void)
-{
- k_mutex_init(&sensor_mutex);
-
- ztest_test_suite(bma2x2,
- ztest_user_unit_test(test_bma_get_offset),
- ztest_user_unit_test(test_bma_set_offset),
- ztest_user_unit_test(test_bma_set_range),
- ztest_user_unit_test(test_bma_init),
- ztest_user_unit_test(test_bma_rate),
- ztest_user_unit_test(test_bma_read),
- ztest_user_unit_test(test_bma_perform_calib),
- ztest_user_unit_test(test_bma_get_resolution));
- ztest_run_test_suite(bma2x2);
-}
diff --git a/zephyr/test/drivers/src/bmi160.c b/zephyr/test/drivers/src/bmi160.c
deleted file mode 100644
index ceb55896eb..0000000000
--- a/zephyr/test/drivers/src/bmi160.c
+++ /dev/null
@@ -1,1873 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <zephyr.h>
-#include <ztest.h>
-
-#include "common.h"
-#include "i2c.h"
-#include "emul/emul_bmi.h"
-#include "emul/emul_common_i2c.h"
-
-#include "motion_sense_fifo.h"
-#include "driver/accelgyro_bmi160.h"
-#include "driver/accelgyro_bmi_common.h"
-
-#define BMI_ORD DT_DEP_ORD(DT_NODELABEL(accel_bmi160))
-#define BMI_ACC_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi160_accel))
-#define BMI_GYR_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi160_gyro))
-#define BMI_INT_EVENT \
- TASK_EVENT_MOTION_SENSOR_INTERRUPT(SENSOR_ID(DT_ALIAS(bmi160_int)))
-
-/** How accurate comparision of vectors should be */
-#define V_EPS 8
-
-/** Convert from one type of vector to another */
-#define convert_int3v_int16(v, r) do { \
- r[0] = v[0]; \
- r[1] = v[1]; \
- r[2] = v[2]; \
- } while (0)
-
-/** Rotation used in some tests */
-static const mat33_fp_t test_rotation = {
- { 0, FLOAT_TO_FP(1), 0},
- { FLOAT_TO_FP(-1), 0, 0},
- { 0, 0, FLOAT_TO_FP(-1)}
-};
-/** Rotate given vector by test rotation */
-static void rotate_int3v_by_test_rotation(intv3_t v)
-{
- int16_t t;
-
- t = v[0];
- v[0] = -v[1];
- v[1] = t;
- v[2] = -v[2];
-}
-
-/** Set emulator accelerometer offset values to intv3_t vector */
-static void set_emul_acc_offset(struct i2c_emul *emul, intv3_t offset)
-{
- bmi_emul_set_off(emul, BMI_EMUL_ACC_X, offset[0]);
- bmi_emul_set_off(emul, BMI_EMUL_ACC_Y, offset[1]);
- bmi_emul_set_off(emul, BMI_EMUL_ACC_Z, offset[2]);
-}
-
-/** Save emulator accelerometer offset values to intv3_t vector */
-static void get_emul_acc_offset(struct i2c_emul *emul, intv3_t offset)
-{
- offset[0] = bmi_emul_get_off(emul, BMI_EMUL_ACC_X);
- offset[1] = bmi_emul_get_off(emul, BMI_EMUL_ACC_Y);
- offset[2] = bmi_emul_get_off(emul, BMI_EMUL_ACC_Z);
-}
-
-/** Set emulator accelerometer values to intv3_t vector */
-static void set_emul_acc(struct i2c_emul *emul, intv3_t acc)
-{
- bmi_emul_set_value(emul, BMI_EMUL_ACC_X, acc[0]);
- bmi_emul_set_value(emul, BMI_EMUL_ACC_Y, acc[1]);
- bmi_emul_set_value(emul, BMI_EMUL_ACC_Z, acc[2]);
-}
-
-/** Set emulator gyroscope offset values to intv3_t vector */
-static void set_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset)
-{
- bmi_emul_set_off(emul, BMI_EMUL_GYR_X, offset[0]);
- bmi_emul_set_off(emul, BMI_EMUL_GYR_Y, offset[1]);
- bmi_emul_set_off(emul, BMI_EMUL_GYR_Z, offset[2]);
-}
-
-/** Save emulator gyroscope offset values to intv3_t vector */
-static void get_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset)
-{
- offset[0] = bmi_emul_get_off(emul, BMI_EMUL_GYR_X);
- offset[1] = bmi_emul_get_off(emul, BMI_EMUL_GYR_Y);
- offset[2] = bmi_emul_get_off(emul, BMI_EMUL_GYR_Z);
-}
-
-/** Set emulator gyroscope values to vector of three int16_t */
-static void set_emul_gyr(struct i2c_emul *emul, intv3_t gyr)
-{
- bmi_emul_set_value(emul, BMI_EMUL_GYR_X, gyr[0]);
- bmi_emul_set_value(emul, BMI_EMUL_GYR_Y, gyr[1]);
- bmi_emul_set_value(emul, BMI_EMUL_GYR_Z, gyr[2]);
-}
-
-/** Convert accelerometer read to units used by emulator */
-static void drv_acc_to_emul(intv3_t drv, int range, intv3_t out)
-{
- const int scale = MOTION_SCALING_FACTOR / BMI_EMUL_1G;
-
- out[0] = drv[0] * range / scale;
- out[1] = drv[1] * range / scale;
- out[2] = drv[2] * range / scale;
-}
-
-/** Convert gyroscope read to units used by emulator */
-static void drv_gyr_to_emul(intv3_t drv, int range, intv3_t out)
-{
- const int scale = MOTION_SCALING_FACTOR / BMI_EMUL_125_DEG_S;
-
- range /= 125;
- out[0] = drv[0] * range / scale;
- out[1] = drv[1] * range / scale;
- out[2] = drv[2] * range / scale;
-}
-
-/** Compare two vectors of intv3_t type */
-static void compare_int3v_f(intv3_t exp_v, intv3_t v, int eps, int line)
-{
- int i;
-
- for (i = 0; i < 3; i++) {
- zassert_within(exp_v[i], v[i], eps,
- "Expected [%d; %d; %d], got [%d; %d; %d]; line: %d",
- exp_v[0], exp_v[1], exp_v[2], v[0], v[1], v[2], line);
- }
-}
-#define compare_int3v_eps(exp_v, v, e) compare_int3v_f(exp_v, v, e, __LINE__)
-#define compare_int3v(exp_v, v) compare_int3v_eps(exp_v, v, V_EPS)
-
-/** Test get accelerometer offset with and without rotation */
-static void test_bmi_acc_get_offset(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- int16_t ret[3];
- intv3_t ret_v;
- intv3_t exp_v;
- int16_t temp;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- /* Set emulator offset */
- exp_v[0] = BMI_EMUL_1G / 10;
- exp_v[1] = BMI_EMUL_1G / 20;
- exp_v[2] = -(int)BMI_EMUL_1G / 30;
- set_emul_acc_offset(emul, exp_v);
- /* BMI driver returns value in mg units */
- exp_v[0] = 1000 / 10;
- exp_v[1] = 1000 / 20;
- exp_v[2] = -1000 / 30;
-
- /* Test fail on offset read */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70);
- zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 1);
- zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 2);
- zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Disable rotation */
- ms->rot_standard_ref = NULL;
-
- /* Test get offset without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL);
- convert_int3v_int16(ret, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Setup rotation and rotate expected offset */
- ms->rot_standard_ref = &test_rotation;
- rotate_int3v_by_test_rotation(exp_v);
-
- /* Test get offset with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL);
- convert_int3v_int16(ret, ret_v);
- compare_int3v(exp_v, ret_v);
-}
-
-/** Test get gyroscope offset with and without rotation */
-static void test_bmi_gyr_get_offset(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- int16_t ret[3];
- intv3_t ret_v;
- intv3_t exp_v;
- int16_t temp;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Do not fail on read */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Set emulator offset */
- exp_v[0] = BMI_EMUL_125_DEG_S / 100;
- exp_v[1] = BMI_EMUL_125_DEG_S / 200;
- exp_v[2] = -(int)BMI_EMUL_125_DEG_S / 300;
- set_emul_gyr_offset(emul, exp_v);
- /* BMI driver returns value in mdeg/s units */
- exp_v[0] = 125000 / 100;
- exp_v[1] = 125000 / 200;
- exp_v[2] = -125000 / 300;
-
- /* Test fail on offset read */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70);
- zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 1);
- zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 2);
- zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98);
- zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Disable rotation */
- ms->rot_standard_ref = NULL;
-
- /* Test get offset without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL);
- convert_int3v_int16(ret, ret_v);
- compare_int3v_eps(exp_v, ret_v, 64);
-
- /* Setup rotation and rotate expected offset */
- ms->rot_standard_ref = &test_rotation;
- rotate_int3v_by_test_rotation(exp_v);
-
- /* Test get offset with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL);
- convert_int3v_int16(ret, ret_v);
- compare_int3v_eps(exp_v, ret_v, 64);
-}
-
-/**
- * Test set accelerometer offset with and without rotation. Also test behaviour
- * on I2C error.
- */
-static void test_bmi_acc_set_offset(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- int16_t input_v[3];
- int16_t temp = 0;
- intv3_t ret_v;
- intv3_t exp_v;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- /* Test fail on OFFSET EN GYR98 register read and write */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_EN_GYR98);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test fail on offset write */
- i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 1);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 2);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Set input offset */
- exp_v[0] = BMI_EMUL_1G / 10;
- exp_v[1] = BMI_EMUL_1G / 20;
- exp_v[2] = -(int)BMI_EMUL_1G / 30;
- /* BMI driver accept value in mg units */
- input_v[0] = 1000 / 10;
- input_v[1] = 1000 / 20;
- input_v[2] = -1000 / 30;
- /* Disable rotation */
- ms->rot_standard_ref = NULL;
-
- /* Test set offset without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->set_offset(ms, input_v, temp), NULL);
- get_emul_acc_offset(emul, ret_v);
- /*
- * Depending on used range, accelerometer values may be up to 6 bits
- * more accurate then offset value resolution.
- */
- compare_int3v_eps(exp_v, ret_v, 64);
- /* Accelerometer offset should be enabled */
- zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) &
- BMI160_OFFSET_ACC_EN, NULL);
-
- /* Setup rotation and rotate input for set_offset function */
- ms->rot_standard_ref = &test_rotation;
- convert_int3v_int16(input_v, ret_v);
- rotate_int3v_by_test_rotation(ret_v);
- convert_int3v_int16(ret_v, input_v);
-
- /* Test set offset with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->set_offset(ms, input_v, temp), NULL);
- get_emul_acc_offset(emul, ret_v);
- compare_int3v_eps(exp_v, ret_v, 64);
- /* Accelerometer offset should be enabled */
- zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) &
- BMI160_OFFSET_ACC_EN, NULL);
-}
-
-/**
- * Test set gyroscope offset with and without rotation. Also test behaviour
- * on I2C error.
- */
-static void test_bmi_gyr_set_offset(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- int16_t input_v[3];
- int16_t temp = 0;
- intv3_t ret_v;
- intv3_t exp_v;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Test fail on OFFSET EN GYR98 register read and write */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_EN_GYR98);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test fail on offset write */
- i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70 + 1);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70 + 2);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Set input offset */
- exp_v[0] = BMI_EMUL_125_DEG_S / 100;
- exp_v[1] = BMI_EMUL_125_DEG_S / 200;
- exp_v[2] = -(int)BMI_EMUL_125_DEG_S / 300;
- /* BMI driver accept value in mdeg/s units */
- input_v[0] = 125000 / 100;
- input_v[1] = 125000 / 200;
- input_v[2] = -125000 / 300;
- /* Disable rotation */
- ms->rot_standard_ref = NULL;
-
- /* Test set offset without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->set_offset(ms, input_v, temp), NULL);
- get_emul_gyr_offset(emul, ret_v);
- compare_int3v(exp_v, ret_v);
- /* Gyroscope offset should be enabled */
- zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) &
- BMI160_OFFSET_GYRO_EN, NULL);
-
- /* Setup rotation and rotate input for set_offset function */
- ms->rot_standard_ref = &test_rotation;
- convert_int3v_int16(input_v, ret_v);
- rotate_int3v_by_test_rotation(ret_v);
- convert_int3v_int16(ret_v, input_v);
-
- /* Test set offset with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->set_offset(ms, input_v, temp), NULL);
- get_emul_gyr_offset(emul, ret_v);
- compare_int3v(exp_v, ret_v);
- zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) &
- BMI160_OFFSET_GYRO_EN, NULL);
-}
-
-/**
- * Try to set accelerometer range and check if expected range was set
- * in driver and in emulator.
- */
-static void check_set_acc_range_f(struct i2c_emul *emul,
- struct motion_sensor_t *ms, int range,
- int rnd, int exp_range, int line)
-{
- uint8_t exp_range_reg;
- uint8_t range_reg;
-
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, range, rnd),
- "set_range failed; line: %d", line);
- zassert_equal(exp_range, ms->current_range,
- "Expected range %d, got %d; line %d",
- exp_range, ms->current_range, line);
- range_reg = bmi_emul_get_reg(emul, BMI160_ACC_RANGE);
-
- switch (exp_range) {
- case 2:
- exp_range_reg = BMI160_GSEL_2G;
- break;
- case 4:
- exp_range_reg = BMI160_GSEL_4G;
- break;
- case 8:
- exp_range_reg = BMI160_GSEL_8G;
- break;
- case 16:
- exp_range_reg = BMI160_GSEL_16G;
- break;
- default:
- /* Unknown expected range */
- zassert_unreachable(
- "Expected range %d not supported by device; line %d",
- exp_range, line);
- return;
- }
-
- zassert_equal(exp_range_reg, range_reg,
- "Expected range reg 0x%x, got 0x%x; line %d",
- exp_range_reg, range_reg, line);
-}
-#define check_set_acc_range(emul, ms, range, rnd, exp_range) \
- check_set_acc_range_f(emul, ms, range, rnd, exp_range, __LINE__)
-
-/** Test set accelerometer range with and without I2C errors */
-static void test_bmi_acc_set_range(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- int start_range;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- /* Setup starting range, shouldn't be changed on error */
- start_range = 2;
- ms->current_range = start_range;
- bmi_emul_set_reg(emul, BMI160_ACC_RANGE, BMI160_GSEL_2G);
- /* Setup emulator fail on write */
- i2c_common_emul_set_write_fail_reg(emul, BMI160_ACC_RANGE);
-
- /* Test fail on write */
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 12, 0), NULL);
- zassert_equal(start_range, ms->current_range, NULL);
- zassert_equal(BMI160_GSEL_2G,
- bmi_emul_get_reg(emul, BMI160_ACC_RANGE), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 12, 1), NULL);
- zassert_equal(start_range, ms->current_range, NULL);
- zassert_equal(BMI160_GSEL_2G,
- bmi_emul_get_reg(emul, BMI160_ACC_RANGE), NULL);
-
- /* Do not fail on write */
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test setting range with rounding down */
- check_set_acc_range(emul, ms, 1, 0, 2);
- check_set_acc_range(emul, ms, 2, 0, 2);
- check_set_acc_range(emul, ms, 3, 0, 2);
- check_set_acc_range(emul, ms, 4, 0, 4);
- check_set_acc_range(emul, ms, 5, 0, 4);
- check_set_acc_range(emul, ms, 6, 0, 4);
- check_set_acc_range(emul, ms, 7, 0, 4);
- check_set_acc_range(emul, ms, 8, 0, 8);
- check_set_acc_range(emul, ms, 9, 0, 8);
- check_set_acc_range(emul, ms, 15, 0, 8);
- check_set_acc_range(emul, ms, 16, 0, 16);
- check_set_acc_range(emul, ms, 17, 0, 16);
-
- /* Test setting range with rounding up */
- check_set_acc_range(emul, ms, 1, 1, 2);
- check_set_acc_range(emul, ms, 2, 1, 2);
- check_set_acc_range(emul, ms, 3, 1, 4);
- check_set_acc_range(emul, ms, 4, 1, 4);
- check_set_acc_range(emul, ms, 5, 1, 8);
- check_set_acc_range(emul, ms, 6, 1, 8);
- check_set_acc_range(emul, ms, 7, 1, 8);
- check_set_acc_range(emul, ms, 8, 1, 8);
- check_set_acc_range(emul, ms, 9, 1, 16);
- check_set_acc_range(emul, ms, 15, 1, 16);
- check_set_acc_range(emul, ms, 16, 1, 16);
- check_set_acc_range(emul, ms, 17, 1, 16);
-}
-
-/**
- * Try to set gyroscope range and check if expected range was set in driver and
- * in emulator.
- */
-static void check_set_gyr_range_f(struct i2c_emul *emul,
- struct motion_sensor_t *ms, int range,
- int rnd, int exp_range, int line)
-{
- uint8_t exp_range_reg;
- uint8_t range_reg;
-
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, range, rnd),
- "set_range failed; line: %d", line);
- zassert_equal(exp_range, ms->current_range,
- "Expected range %d, got %d; line %d",
- exp_range, ms->current_range, line);
- range_reg = bmi_emul_get_reg(emul, BMI160_GYR_RANGE);
-
- switch (exp_range) {
- case 125:
- exp_range_reg = BMI160_DPS_SEL_125;
- break;
- case 250:
- exp_range_reg = BMI160_DPS_SEL_250;
- break;
- case 500:
- exp_range_reg = BMI160_DPS_SEL_500;
- break;
- case 1000:
- exp_range_reg = BMI160_DPS_SEL_1000;
- break;
- case 2000:
- exp_range_reg = BMI160_DPS_SEL_2000;
- break;
- default:
- /* Unknown expected range */
- zassert_unreachable(
- "Expected range %d not supported by device; line %d",
- exp_range, line);
- return;
- }
-
- zassert_equal(exp_range_reg, range_reg,
- "Expected range reg 0x%x, got 0x%x; line %d",
- exp_range_reg, range_reg, line);
-}
-#define check_set_gyr_range(emul, ms, range, rnd, exp_range) \
- check_set_gyr_range_f(emul, ms, range, rnd, exp_range, __LINE__)
-
-/** Test set gyroscope range with and without I2C errors */
-static void test_bmi_gyr_set_range(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- int start_range;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Setup starting range, shouldn't be changed on error */
- start_range = 250;
- ms->current_range = start_range;
- bmi_emul_set_reg(emul, BMI160_GYR_RANGE, BMI160_DPS_SEL_250);
- /* Setup emulator fail on write */
- i2c_common_emul_set_write_fail_reg(emul, BMI160_GYR_RANGE);
-
- /* Test fail on write */
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 125, 0), NULL);
- zassert_equal(start_range, ms->current_range, NULL);
- zassert_equal(BMI160_DPS_SEL_250,
- bmi_emul_get_reg(emul, BMI160_GYR_RANGE), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 125, 1), NULL);
- zassert_equal(start_range, ms->current_range, NULL);
- zassert_equal(BMI160_DPS_SEL_250,
- bmi_emul_get_reg(emul, BMI160_GYR_RANGE), NULL);
-
- /* Do not fail on write */
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test setting range with rounding down */
- check_set_gyr_range(emul, ms, 1, 0, 125);
- check_set_gyr_range(emul, ms, 124, 0, 125);
- check_set_gyr_range(emul, ms, 125, 0, 125);
- check_set_gyr_range(emul, ms, 126, 0, 125);
- check_set_gyr_range(emul, ms, 249, 0, 125);
- check_set_gyr_range(emul, ms, 250, 0, 250);
- check_set_gyr_range(emul, ms, 251, 0, 250);
- check_set_gyr_range(emul, ms, 499, 0, 250);
- check_set_gyr_range(emul, ms, 500, 0, 500);
- check_set_gyr_range(emul, ms, 501, 0, 500);
- check_set_gyr_range(emul, ms, 999, 0, 500);
- check_set_gyr_range(emul, ms, 1000, 0, 1000);
- check_set_gyr_range(emul, ms, 1001, 0, 1000);
- check_set_gyr_range(emul, ms, 1999, 0, 1000);
- check_set_gyr_range(emul, ms, 2000, 0, 2000);
- check_set_gyr_range(emul, ms, 2001, 0, 2000);
-
- /* Test setting range with rounding up */
- check_set_gyr_range(emul, ms, 1, 1, 125);
- check_set_gyr_range(emul, ms, 124, 1, 125);
- check_set_gyr_range(emul, ms, 125, 1, 125);
- check_set_gyr_range(emul, ms, 126, 1, 250);
- check_set_gyr_range(emul, ms, 249, 1, 250);
- check_set_gyr_range(emul, ms, 250, 1, 250);
- check_set_gyr_range(emul, ms, 251, 1, 500);
- check_set_gyr_range(emul, ms, 499, 1, 500);
- check_set_gyr_range(emul, ms, 500, 1, 500);
- check_set_gyr_range(emul, ms, 501, 1, 1000);
- check_set_gyr_range(emul, ms, 999, 1, 1000);
- check_set_gyr_range(emul, ms, 1000, 1, 1000);
- check_set_gyr_range(emul, ms, 1001, 1, 2000);
- check_set_gyr_range(emul, ms, 1999, 1, 2000);
- check_set_gyr_range(emul, ms, 2000, 1, 2000);
- check_set_gyr_range(emul, ms, 2001, 1, 2000);
-}
-
-/** Test get resolution of acclerometer and gyroscope sensor */
-static void test_bmi_get_resolution(void)
-{
- struct motion_sensor_t *ms;
-
- /* Test accelerometer */
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- /* Resolution should be always 16 bits */
- zassert_equal(16, ms->drv->get_resolution(ms), NULL);
-
- /* Test gyroscope */
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Resolution should be always 16 bits */
- zassert_equal(16, ms->drv->get_resolution(ms), NULL);
-}
-
-/**
- * Try to set accelerometer data rate and check if expected rate was set
- * in driver and in emulator.
- */
-static void check_set_acc_rate_f(struct i2c_emul *emul,
- struct motion_sensor_t *ms, int rate, int rnd,
- int exp_rate, int line)
-{
- uint8_t exp_rate_reg;
- uint8_t rate_reg;
- int drv_rate;
-
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, rate, rnd),
- "set_data_rate failed; line: %d", line);
- drv_rate = ms->drv->get_data_rate(ms);
- zassert_equal(exp_rate, drv_rate, "Expected rate %d, got %d; line %d",
- exp_rate, drv_rate, line);
- rate_reg = bmi_emul_get_reg(emul, BMI160_ACC_CONF);
- rate_reg &= BMI_ODR_MASK;
-
- switch (exp_rate) {
- case 12500:
- exp_rate_reg = 0x5;
- break;
- case 25000:
- exp_rate_reg = 0x6;
- break;
- case 50000:
- exp_rate_reg = 0x7;
- break;
- case 100000:
- exp_rate_reg = 0x8;
- break;
- case 200000:
- exp_rate_reg = 0x9;
- break;
- case 400000:
- exp_rate_reg = 0xa;
- break;
- case 800000:
- exp_rate_reg = 0xb;
- break;
- case 1600000:
- exp_rate_reg = 0xc;
- break;
- default:
- /* Unknown expected rate */
- zassert_unreachable(
- "Expected rate %d not supported by device; line %d",
- exp_rate, line);
- return;
- }
-
- zassert_equal(exp_rate_reg, rate_reg,
- "Expected rate reg 0x%x, got 0x%x; line %d",
- exp_rate_reg, rate_reg, line);
-}
-#define check_set_acc_rate(emul, ms, rate, rnd, exp_rate) \
- check_set_acc_rate_f(emul, ms, rate, rnd, exp_rate, __LINE__)
-
-/** Test set and get accelerometer rate with and without I2C errors */
-static void test_bmi_acc_rate(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- uint8_t reg_rate;
- int pmu_status;
- int drv_rate;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- /* Test setting rate with rounding down */
- check_set_acc_rate(emul, ms, 12500, 0, 12500);
- check_set_acc_rate(emul, ms, 12501, 0, 12500);
- check_set_acc_rate(emul, ms, 24999, 0, 12500);
- check_set_acc_rate(emul, ms, 25000, 0, 25000);
- check_set_acc_rate(emul, ms, 25001, 0, 25000);
- check_set_acc_rate(emul, ms, 49999, 0, 25000);
- check_set_acc_rate(emul, ms, 50000, 0, 50000);
- check_set_acc_rate(emul, ms, 50001, 0, 50000);
- check_set_acc_rate(emul, ms, 99999, 0, 50000);
- check_set_acc_rate(emul, ms, 100000, 0, 100000);
- check_set_acc_rate(emul, ms, 100001, 0, 100000);
- check_set_acc_rate(emul, ms, 199999, 0, 100000);
- check_set_acc_rate(emul, ms, 200000, 0, 200000);
- check_set_acc_rate(emul, ms, 200001, 0, 200000);
- check_set_acc_rate(emul, ms, 399999, 0, 200000);
- /*
- * We cannot test frequencies from 400000 to 1600000 because
- * CONFIG_EC_MAX_SENSOR_FREQ_MILLIHZ is set to 250000
- */
-
- /* Test setting rate with rounding up */
- check_set_acc_rate(emul, ms, 6251, 1, 12500);
- check_set_acc_rate(emul, ms, 12499, 1, 12500);
- check_set_acc_rate(emul, ms, 12500, 1, 12500);
- check_set_acc_rate(emul, ms, 12501, 1, 25000);
- check_set_acc_rate(emul, ms, 24999, 1, 25000);
- check_set_acc_rate(emul, ms, 25000, 1, 25000);
- check_set_acc_rate(emul, ms, 25001, 1, 50000);
- check_set_acc_rate(emul, ms, 49999, 1, 50000);
- check_set_acc_rate(emul, ms, 50000, 1, 50000);
- check_set_acc_rate(emul, ms, 50001, 1, 100000);
- check_set_acc_rate(emul, ms, 99999, 1, 100000);
- check_set_acc_rate(emul, ms, 100000, 1, 100000);
- check_set_acc_rate(emul, ms, 100001, 1, 200000);
- check_set_acc_rate(emul, ms, 199999, 1, 200000);
- check_set_acc_rate(emul, ms, 200000, 1, 200000);
-
- /* Test out of range rate with rounding down */
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 1, 0), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 12499, 0), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 400000, 0), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 2000000, 0), NULL);
-
- /* Test out of range rate with rounding up */
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 1, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 6250, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 200001, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 400000, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 2000000, 1), NULL);
-
- /* Current rate shouldn't be changed on error */
- drv_rate = ms->drv->get_data_rate(ms);
- reg_rate = bmi_emul_get_reg(emul, BMI160_ACC_CONF);
-
- /* Setup emulator fail on read */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_CONF);
-
- /* Test fail on read */
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_ACC_CONF), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 1),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_ACC_CONF), NULL);
-
- /* Do not fail on read */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Setup emulator fail on write */
- i2c_common_emul_set_write_fail_reg(emul, BMI160_ACC_CONF);
-
- /* Test fail on write */
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_ACC_CONF), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 1),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_ACC_CONF), NULL);
-
- /* Do not fail on write */
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test disabling sensor */
- pmu_status = BMI160_PMU_NORMAL << BMI160_PMU_ACC_OFFSET;
- pmu_status |= BMI160_PMU_NORMAL << BMI160_PMU_GYR_OFFSET;
- bmi_emul_set_reg(emul, BMI160_PMU_STATUS, pmu_status);
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 0, 0), NULL);
-
- bmi_read8(ms->port, ms->i2c_spi_addr_flags, BMI160_PMU_STATUS,
- &pmu_status);
- zassert_equal(BMI160_PMU_NORMAL << BMI160_PMU_GYR_OFFSET, pmu_status,
- "Gyroscope should be still enabled");
-
- /* Test enabling sensor */
- bmi_emul_set_reg(emul, BMI160_PMU_STATUS, 0);
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 50000, 0), NULL);
-
- bmi_read8(ms->port, ms->i2c_spi_addr_flags, BMI160_PMU_STATUS,
- &pmu_status);
- zassert_equal(BMI160_PMU_NORMAL << BMI160_PMU_ACC_OFFSET, pmu_status,
- "Accelerometer should be enabled");
-}
-
-/**
- * Try to set gyroscope data rate and check if expected rate was set
- * in driver and in emulator.
- */
-static void check_set_gyr_rate_f(struct i2c_emul *emul,
- struct motion_sensor_t *ms, int rate, int rnd,
- int exp_rate, int line)
-{
- uint8_t exp_rate_reg;
- uint8_t rate_reg;
- int drv_rate;
-
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, rate, rnd),
- "set_data_rate failed; line: %d", line);
- drv_rate = ms->drv->get_data_rate(ms);
- zassert_equal(exp_rate, drv_rate, "Expected rate %d, got %d; line %d",
- exp_rate, drv_rate, line);
- rate_reg = bmi_emul_get_reg(emul, BMI160_GYR_CONF);
- rate_reg &= BMI_ODR_MASK;
-
- switch (exp_rate) {
- case 25000:
- exp_rate_reg = 0x6;
- break;
- case 50000:
- exp_rate_reg = 0x7;
- break;
- case 100000:
- exp_rate_reg = 0x8;
- break;
- case 200000:
- exp_rate_reg = 0x9;
- break;
- case 400000:
- exp_rate_reg = 0xa;
- break;
- case 800000:
- exp_rate_reg = 0xb;
- break;
- case 1600000:
- exp_rate_reg = 0xc;
- break;
- case 3200000:
- exp_rate_reg = 0xc;
- break;
- default:
- /* Unknown expected rate */
- zassert_unreachable(
- "Expected rate %d not supported by device; line %d",
- exp_rate, line);
- return;
- }
-
- zassert_equal(exp_rate_reg, rate_reg,
- "Expected rate reg 0x%x, got 0x%x; line %d",
- exp_rate_reg, rate_reg, line);
-}
-#define check_set_gyr_rate(emul, ms, rate, rnd, exp_rate) \
- check_set_gyr_rate_f(emul, ms, rate, rnd, exp_rate, __LINE__)
-
-/** Test set and get gyroscope rate with and without I2C errors */
-static void test_bmi_gyr_rate(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- uint8_t reg_rate;
- int pmu_status;
- int drv_rate;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Test setting rate with rounding down */
- check_set_gyr_rate(emul, ms, 25000, 0, 25000);
- check_set_gyr_rate(emul, ms, 25001, 0, 25000);
- check_set_gyr_rate(emul, ms, 49999, 0, 25000);
- check_set_gyr_rate(emul, ms, 50000, 0, 50000);
- check_set_gyr_rate(emul, ms, 50001, 0, 50000);
- check_set_gyr_rate(emul, ms, 99999, 0, 50000);
- check_set_gyr_rate(emul, ms, 100000, 0, 100000);
- check_set_gyr_rate(emul, ms, 100001, 0, 100000);
- check_set_gyr_rate(emul, ms, 199999, 0, 100000);
- check_set_gyr_rate(emul, ms, 200000, 0, 200000);
- check_set_gyr_rate(emul, ms, 200001, 0, 200000);
- check_set_gyr_rate(emul, ms, 399999, 0, 200000);
- /*
- * We cannot test frequencies from 400000 to 3200000 because
- * CONFIG_EC_MAX_SENSOR_FREQ_MILLIHZ is set to 250000
- */
-
- /* Test setting rate with rounding up */
- check_set_gyr_rate(emul, ms, 12501, 1, 25000);
- check_set_gyr_rate(emul, ms, 24999, 1, 25000);
- check_set_gyr_rate(emul, ms, 25000, 1, 25000);
- check_set_gyr_rate(emul, ms, 25001, 1, 50000);
- check_set_gyr_rate(emul, ms, 49999, 1, 50000);
- check_set_gyr_rate(emul, ms, 50000, 1, 50000);
- check_set_gyr_rate(emul, ms, 50001, 1, 100000);
- check_set_gyr_rate(emul, ms, 99999, 1, 100000);
- check_set_gyr_rate(emul, ms, 100000, 1, 100000);
- check_set_gyr_rate(emul, ms, 100001, 1, 200000);
- check_set_gyr_rate(emul, ms, 199999, 1, 200000);
- check_set_gyr_rate(emul, ms, 200000, 1, 200000);
-
- /* Test out of range rate with rounding down */
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 1, 0), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 24999, 0), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 400000, 0), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 4000000, 0), NULL);
-
- /* Test out of range rate with rounding up */
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 1, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 12499, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 200001, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 400000, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 4000000, 1), NULL);
-
- /* Current rate shouldn't be changed on error */
- drv_rate = ms->drv->get_data_rate(ms);
- reg_rate = bmi_emul_get_reg(emul, BMI160_GYR_CONF);
-
- /* Setup emulator fail on read */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_CONF);
-
- /* Test fail on read */
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_GYR_CONF), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 1),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_GYR_CONF), NULL);
-
- /* Do not fail on read */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Setup emulator fail on write */
- i2c_common_emul_set_write_fail_reg(emul, BMI160_GYR_CONF);
-
- /* Test fail on write */
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_GYR_CONF), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 1),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_GYR_CONF), NULL);
-
- /* Do not fail on write */
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test disabling sensor */
- pmu_status = BMI160_PMU_NORMAL << BMI160_PMU_ACC_OFFSET;
- pmu_status |= BMI160_PMU_NORMAL << BMI160_PMU_GYR_OFFSET;
- bmi_emul_set_reg(emul, BMI160_PMU_STATUS, pmu_status);
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 0, 0), NULL);
-
- bmi_read8(ms->port, ms->i2c_spi_addr_flags, BMI160_PMU_STATUS,
- &pmu_status);
- zassert_equal(BMI160_PMU_NORMAL << BMI160_PMU_ACC_OFFSET, pmu_status,
- "Accelerometer should be still enabled");
-
- /* Test enabling sensor */
- bmi_emul_set_reg(emul, BMI160_PMU_STATUS, 0);
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 50000, 0), NULL);
-
- bmi_read8(ms->port, ms->i2c_spi_addr_flags, BMI160_PMU_STATUS,
- &pmu_status);
- zassert_equal(BMI160_PMU_NORMAL << BMI160_PMU_GYR_OFFSET, pmu_status,
- "Gyroscope should be enabled");
-}
-
-/**
- * Test setting and getting scale in accelerometer and gyroscope sensors.
- * Correct appling scale to results is checked in "read" test.
- */
-static void test_bmi_scale(void)
-{
- struct motion_sensor_t *ms;
- int16_t ret_scale[3];
- int16_t exp_scale[3] = {100, 231, 421};
- int16_t t;
-
- /* Test accelerometer */
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- zassert_equal(EC_SUCCESS, ms->drv->set_scale(ms, exp_scale, 0), NULL);
- zassert_equal(EC_SUCCESS, ms->drv->get_scale(ms, ret_scale, &t), NULL);
-
- zassert_equal(t, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL);
- zassert_equal(exp_scale[0], ret_scale[0], NULL);
- zassert_equal(exp_scale[1], ret_scale[1], NULL);
- zassert_equal(exp_scale[2], ret_scale[2], NULL);
-
- /* Test gyroscope */
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- zassert_equal(EC_SUCCESS, ms->drv->set_scale(ms, exp_scale, 0), NULL);
- zassert_equal(EC_SUCCESS, ms->drv->get_scale(ms, ret_scale, &t), NULL);
-
- zassert_equal(t, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL);
- zassert_equal(exp_scale[0], ret_scale[0], NULL);
- zassert_equal(exp_scale[1], ret_scale[1], NULL);
- zassert_equal(exp_scale[2], ret_scale[2], NULL);
-}
-
-/** Test reading temperature using accelerometer and gyroscope sensors */
-static void test_bmi_read_temp(void)
-{
- struct motion_sensor_t *ms_acc, *ms_gyr;
- struct i2c_emul *emul;
- int ret_temp;
- int exp_temp;
-
- emul = bmi_emul_get(BMI_ORD);
- ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID];
- ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Setup emulator fail on read */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_TEMPERATURE_0);
- zassert_equal(EC_ERROR_NOT_POWERED,
- ms_acc->drv->read_temp(ms_acc, &ret_temp), NULL);
- zassert_equal(EC_ERROR_NOT_POWERED,
- ms_gyr->drv->read_temp(ms_gyr, &ret_temp), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_TEMPERATURE_1);
- zassert_equal(EC_ERROR_NOT_POWERED,
- ms_acc->drv->read_temp(ms_acc, &ret_temp), NULL);
- zassert_equal(EC_ERROR_NOT_POWERED,
- ms_gyr->drv->read_temp(ms_gyr, &ret_temp), NULL);
- /* Do not fail on read */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Fail on invalid temperature */
- bmi_emul_set_reg(emul, BMI160_TEMPERATURE_0, 0x00);
- bmi_emul_set_reg(emul, BMI160_TEMPERATURE_1, 0x80);
- zassert_equal(EC_ERROR_NOT_POWERED,
- ms_acc->drv->read_temp(ms_acc, &ret_temp), NULL);
- zassert_equal(EC_ERROR_NOT_POWERED,
- ms_gyr->drv->read_temp(ms_gyr, &ret_temp), NULL);
-
- /*
- * Test correct values. Both motion sensors should return the same
- * temperature.
- */
- exp_temp = C_TO_K(23);
- bmi_emul_set_reg(emul, BMI160_TEMPERATURE_0, 0x00);
- bmi_emul_set_reg(emul, BMI160_TEMPERATURE_1, 0x00);
- zassert_equal(EC_SUCCESS, ms_acc->drv->read_temp(ms_acc, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
- zassert_equal(EC_SUCCESS, ms_gyr->drv->read_temp(ms_gyr, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
-
- exp_temp = C_TO_K(87);
- bmi_emul_set_reg(emul, BMI160_TEMPERATURE_0, 0xff);
- bmi_emul_set_reg(emul, BMI160_TEMPERATURE_1, 0x7f);
- zassert_equal(EC_SUCCESS, ms_acc->drv->read_temp(ms_acc, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
- zassert_equal(EC_SUCCESS, ms_gyr->drv->read_temp(ms_gyr, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
-
- exp_temp = C_TO_K(-41);
- bmi_emul_set_reg(emul, BMI160_TEMPERATURE_0, 0x01);
- bmi_emul_set_reg(emul, BMI160_TEMPERATURE_1, 0x80);
- zassert_equal(EC_SUCCESS, ms_acc->drv->read_temp(ms_acc, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
- zassert_equal(EC_SUCCESS, ms_gyr->drv->read_temp(ms_gyr, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
-
- exp_temp = C_TO_K(47);
- bmi_emul_set_reg(emul, BMI160_TEMPERATURE_0, 0x00);
- bmi_emul_set_reg(emul, BMI160_TEMPERATURE_1, 0x30);
- zassert_equal(EC_SUCCESS, ms_acc->drv->read_temp(ms_acc, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
- zassert_equal(EC_SUCCESS, ms_gyr->drv->read_temp(ms_gyr, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
-}
-
-/** Test reading accelerometer sensor data */
-static void test_bmi_acc_read(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- intv3_t ret_v;
- intv3_t exp_v;
- int16_t scale[3] = {MOTION_SENSE_DEFAULT_SCALE,
- MOTION_SENSE_DEFAULT_SCALE,
- MOTION_SENSE_DEFAULT_SCALE};
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- /* Set offset 0 to simplify test */
- bmi_emul_set_off(emul, BMI_EMUL_ACC_X, 0);
- bmi_emul_set_off(emul, BMI_EMUL_ACC_Y, 0);
- bmi_emul_set_off(emul, BMI_EMUL_ACC_Z, 0);
-
- /* Fail on read status */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
-
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* When not ready, driver should return saved raw value */
- exp_v[0] = 100;
- exp_v[1] = 200;
- exp_v[2] = 300;
- ms->raw_xyz[0] = exp_v[0];
- ms->raw_xyz[1] = exp_v[1];
- ms->raw_xyz[2] = exp_v[2];
-
- /* Status not ready */
- bmi_emul_set_reg(emul, BMI160_STATUS, 0);
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- compare_int3v(exp_v, ret_v);
-
- /* Status only GYR ready */
- bmi_emul_set_reg(emul, BMI160_STATUS, BMI160_DRDY_GYR);
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- compare_int3v(exp_v, ret_v);
-
- /* Status ACC ready */
- bmi_emul_set_reg(emul, BMI160_STATUS, BMI160_DRDY_ACC);
-
- /* Set input accelerometer values */
- exp_v[0] = BMI_EMUL_1G / 10;
- exp_v[1] = BMI_EMUL_1G / 20;
- exp_v[2] = -(int)BMI_EMUL_1G / 30;
- set_emul_acc(emul, exp_v);
- /* Disable rotation */
- ms->rot_standard_ref = NULL;
- /* Set scale */
- zassert_equal(EC_SUCCESS, ms->drv->set_scale(ms, scale, 0), NULL);
- /* Set range to 2G */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 2, 0), NULL);
-
- /* Test read without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_acc_to_emul(ret_v, 2, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Set range to 4G */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 4, 0), NULL);
-
- /* Test read without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_acc_to_emul(ret_v, 4, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Setup rotation and rotate expected vector */
- ms->rot_standard_ref = &test_rotation;
- rotate_int3v_by_test_rotation(exp_v);
- /* Set range to 2G */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 2, 0), NULL);
-
- /* Test read with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_acc_to_emul(ret_v, 2, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Set range to 4G */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 4, 0), NULL);
-
- /* Test read with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_acc_to_emul(ret_v, 4, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Fail on read of data registers */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_X_L_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_X_H_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Y_L_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Y_H_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Z_L_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Z_H_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
-
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- ms->rot_standard_ref = NULL;
-}
-
-/** Test reading gyroscope sensor data */
-static void test_bmi_gyr_read(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- intv3_t ret_v;
- intv3_t exp_v;
- int16_t scale[3] = {MOTION_SENSE_DEFAULT_SCALE,
- MOTION_SENSE_DEFAULT_SCALE,
- MOTION_SENSE_DEFAULT_SCALE};
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Set offset 0 to simplify test */
- bmi_emul_set_off(emul, BMI_EMUL_GYR_X, 0);
- bmi_emul_set_off(emul, BMI_EMUL_GYR_Y, 0);
- bmi_emul_set_off(emul, BMI_EMUL_GYR_Z, 0);
-
- /* Fail on read status */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
-
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* When not ready, driver should return saved raw value */
- exp_v[0] = 100;
- exp_v[1] = 200;
- exp_v[2] = 300;
- ms->raw_xyz[0] = exp_v[0];
- ms->raw_xyz[1] = exp_v[1];
- ms->raw_xyz[2] = exp_v[2];
-
- /* Status not ready */
- bmi_emul_set_reg(emul, BMI160_STATUS, 0);
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- compare_int3v(exp_v, ret_v);
-
- /* Status only ACC ready */
- bmi_emul_set_reg(emul, BMI160_STATUS, BMI160_DRDY_ACC);
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- compare_int3v(exp_v, ret_v);
-
- /* Status GYR ready */
- bmi_emul_set_reg(emul, BMI160_STATUS, BMI160_DRDY_GYR);
-
- /* Set input accelerometer values */
- exp_v[0] = BMI_EMUL_125_DEG_S / 10;
- exp_v[1] = BMI_EMUL_125_DEG_S / 20;
- exp_v[2] = -(int)BMI_EMUL_125_DEG_S / 30;
- set_emul_gyr(emul, exp_v);
- /* Disable rotation */
- ms->rot_standard_ref = NULL;
- /* Set scale */
- zassert_equal(EC_SUCCESS, ms->drv->set_scale(ms, scale, 0), NULL);
- /* Set range to 125°/s */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 125, 0), NULL);
-
- /* Test read without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_gyr_to_emul(ret_v, 125, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Set range to 1000°/s */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 1000, 0), NULL);
-
- /* Test read without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_gyr_to_emul(ret_v, 1000, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Setup rotation and rotate expected vector */
- ms->rot_standard_ref = &test_rotation;
- rotate_int3v_by_test_rotation(exp_v);
- /* Set range to 125°/s */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 125, 0), NULL);
-
- /* Test read with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_gyr_to_emul(ret_v, 125, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Set range to 1000°/s */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 1000, 0), NULL);
-
- /* Test read with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_gyr_to_emul(ret_v, 1000, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Fail on read of data registers */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_X_L_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_X_H_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Y_L_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Y_H_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Z_L_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Z_H_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
-
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- ms->rot_standard_ref = NULL;
-}
-
-/**
- * Custom emulatro read function which always return not ready STATUS register.
- * Used in calibration test.
- */
-static int emul_nrdy(struct i2c_emul *emul, int reg, uint8_t *val, int byte,
- void *data)
-{
- if (reg == BMI160_STATUS) {
- bmi_emul_set_reg(emul, BMI160_STATUS, 0);
- *val = 0;
-
- return 0;
- }
-
- return 1;
-}
-
-/** Test acceleromtere calibration */
-static void test_bmi_acc_perform_calib(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- uint8_t pmu_status;
- intv3_t start_off;
- intv3_t exp_off;
- intv3_t ret_off;
- int range;
- int rate;
- mat33_fp_t rot = {
- { FLOAT_TO_FP(1), 0, 0},
- { 0, FLOAT_TO_FP(1), 0},
- { 0, 0, FLOAT_TO_FP(-1)}
- };
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- /* Enable sensors */
- pmu_status = BMI160_PMU_NORMAL << BMI160_PMU_ACC_OFFSET;
- pmu_status |= BMI160_PMU_NORMAL << BMI160_PMU_GYR_OFFSET;
- bmi_emul_set_reg(emul, BMI160_PMU_STATUS, pmu_status);
-
- /* Range and rate cannot change after calibration */
- range = 4;
- rate = 50000;
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, range, 0), NULL);
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, rate, 0), NULL);
-
- /* Set offset 0 */
- start_off[0] = 0;
- start_off[1] = 0;
- start_off[2] = 0;
- set_emul_acc_offset(emul, start_off);
-
- /* Set input accelerometer values */
- exp_off[0] = BMI_EMUL_1G / 10;
- exp_off[1] = BMI_EMUL_1G / 20;
- exp_off[2] = BMI_EMUL_1G - (int)BMI_EMUL_1G / 30;
- set_emul_acc(emul, exp_off);
-
- /*
- * Expected offset is [-X, -Y, 1G - Z] for no rotation or positive
- * rotation on Z axis
- */
- exp_off[0] = -exp_off[0];
- exp_off[1] = -exp_off[1];
- exp_off[2] = BMI_EMUL_1G - exp_off[2];
-
- /* Test fail on rate set */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_CONF);
- zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- /* Test fail on status read */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS);
- zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
- /* Stop fast offset compensation before next test */
- bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP);
-
- /* Test fail on data not ready */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- i2c_common_emul_set_read_func(emul, emul_nrdy, NULL);
- zassert_equal(EC_RES_TIMEOUT, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
- /* Remove custom emulator read function */
- i2c_common_emul_set_read_func(emul, NULL, NULL);
- /* Stop fast offset compensation before next test */
- bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP);
-
- /* Disable rotation */
- ms->rot_standard_ref = NULL;
- /* Test successful offset compenastion without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
- get_emul_acc_offset(emul, ret_off);
- /*
- * Depending on used range, accelerometer values may be up to 6 bits
- * more accurate then offset value resolution.
- */
- compare_int3v_eps(exp_off, ret_off, 64);
- /* Acelerometer offset should be enabled */
- zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) &
- BMI160_OFFSET_ACC_EN, NULL);
-
- /* Enable rotation with negative value on Z axis */
- ms->rot_standard_ref = &rot;
- /* Expected offset -1G - accelerometer[Z] */
- bmi_emul_set_value(emul, BMI_EMUL_ACC_Z, -(int)BMI_EMUL_1G - 1234);
- exp_off[2] = 1234;
-
- /* Test successful offset compenastion with negative Z rotation */
- zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
- get_emul_acc_offset(emul, ret_off);
- compare_int3v_eps(exp_off, ret_off, 64);
- /* Acelerometer offset should be enabled */
- zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) &
- BMI160_OFFSET_ACC_EN, NULL);
-
- /* Set positive rotation on Z axis */
- rot[2][2] = FLOAT_TO_FP(1);
- /* Expected offset 1G - accelerometer[Z] */
- bmi_emul_set_value(emul, BMI_EMUL_ACC_Z, BMI_EMUL_1G - 1234);
- exp_off[2] = 1234;
-
- /* Test successful offset compenastion with positive Z rotation */
- zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
- get_emul_acc_offset(emul, ret_off);
- compare_int3v_eps(exp_off, ret_off, 64);
- /* Acelerometer offset should be enabled */
- zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) &
- BMI160_OFFSET_ACC_EN, NULL);
- /* Disable rotation */
- ms->rot_standard_ref = NULL;
-}
-
-/** Test gyroscope calibration */
-static void test_bmi_gyr_perform_calib(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- uint8_t pmu_status;
- intv3_t start_off;
- intv3_t exp_off;
- intv3_t ret_off;
- int range;
- int rate;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Enable sensors */
- pmu_status = BMI160_PMU_NORMAL << BMI160_PMU_ACC_OFFSET;
- pmu_status |= BMI160_PMU_NORMAL << BMI160_PMU_GYR_OFFSET;
- bmi_emul_set_reg(emul, BMI160_PMU_STATUS, pmu_status);
-
- /* Range and rate cannot change after calibration */
- range = 250;
- rate = 50000;
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, range, 0), NULL);
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, rate, 0), NULL);
-
- /* Set offset 0 */
- start_off[0] = 0;
- start_off[1] = 0;
- start_off[2] = 0;
- set_emul_gyr_offset(emul, start_off);
-
- /* Set input accelerometer values */
- exp_off[0] = BMI_EMUL_125_DEG_S / 100;
- exp_off[1] = BMI_EMUL_125_DEG_S / 200;
- exp_off[2] = -(int)BMI_EMUL_125_DEG_S / 300;
- set_emul_gyr(emul, exp_off);
-
- /* Expected offset is [-X, -Y, -Z] */
- exp_off[0] = -exp_off[0];
- exp_off[1] = -exp_off[1];
- exp_off[2] = -exp_off[2];
-
- /* Test success on disabling calibration */
- zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 0), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- /* Test fail on rate set */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_CONF);
- zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- /* Test fail on status read */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS);
- zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
- /* Stop fast offset compensation before next test */
- bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP);
-
- /* Test fail on data not ready */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- i2c_common_emul_set_read_func(emul, emul_nrdy, NULL);
- zassert_equal(EC_RES_TIMEOUT, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
- /* Remove custom emulator read function */
- i2c_common_emul_set_read_func(emul, NULL, NULL);
- /* Stop fast offset compensation before next test */
- bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP);
-
- /* Test successful offset compenastion */
- zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
- get_emul_gyr_offset(emul, ret_off);
- /*
- * Depending on used range, gyroscope values may be up to 4 bits
- * more accurate then offset value resolution.
- */
- compare_int3v_eps(exp_off, ret_off, 32);
- /* Gyroscope offset should be enabled */
- zassert_true(bmi_emul_get_reg(emul, BMI160_OFFSET_EN_GYR98) &
- BMI160_OFFSET_GYRO_EN, NULL);
-}
-
-/** Test init function of BMI160 accelerometer and gyroscope sensors */
-static void test_bmi_init(void)
-{
- struct motion_sensor_t *ms_acc, *ms_gyr;
- struct i2c_emul *emul;
-
- emul = bmi_emul_get(BMI_ORD);
- ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID];
- ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Test successful init */
- zassert_equal(EC_RES_SUCCESS, ms_acc->drv->init(ms_acc), NULL);
-
- zassert_equal(EC_RES_SUCCESS, ms_gyr->drv->init(ms_gyr), NULL);
-}
-
-/** Data for custom emulator read function used in FIFO test */
-struct fifo_func_data {
- uint16_t interrupts;
-};
-
-/**
- * Custom emulator read function used in FIFO test. It sets interrupt registers
- * to value passed as additional data. It sets interrupt registers to 0 after
- * access.
- */
-static int emul_fifo_func(struct i2c_emul *emul, int reg, uint8_t *val,
- int byte, void *data)
-{
- struct fifo_func_data *d = data;
-
- if (reg + byte == BMI160_INT_STATUS_0) {
- bmi_emul_set_reg(emul, BMI160_INT_STATUS_0,
- d->interrupts & 0xff);
- d->interrupts &= 0xff00;
- } else if (reg + byte == BMI160_INT_STATUS_1) {
- bmi_emul_set_reg(emul, BMI160_INT_STATUS_1,
- (d->interrupts >> 8) & 0xff);
- d->interrupts &= 0xff;
- }
-
- return 1;
-}
-
-/**
- * Run irq handler on accelerometer sensor and check if committed data in FIFO
- * match what was set in FIFO frames in emulator.
- */
-static void check_fifo_f(struct motion_sensor_t *ms_acc,
- struct motion_sensor_t *ms_gyr,
- struct bmi_emul_frame *frame,
- int acc_range, int gyr_range,
- int line)
-{
- struct ec_response_motion_sensor_data vector;
- struct bmi_emul_frame *f_acc, *f_gyr;
- uint32_t event = BMI_INT_EVENT;
- uint16_t size;
- intv3_t exp_v;
- intv3_t ret_v;
-
- /* Find first frame of acc and gyr type */
- f_acc = frame;
- while (f_acc != NULL && !(f_acc->type & BMI_EMUL_FRAME_ACC)) {
- f_acc = f_acc->next;
- }
-
- f_gyr = frame;
- while (f_gyr != NULL && !(f_gyr->type & BMI_EMUL_FRAME_GYR)) {
- f_gyr = f_gyr->next;
- }
-
- /* Read FIFO in driver */
- zassert_equal(EC_SUCCESS, ms_acc->drv->irq_handler(ms_acc, &event),
- NULL);
-
- /* Read all data committed to FIFO */
- while (motion_sense_fifo_read(sizeof(vector), 1, &vector, &size)) {
- /* Ignore timestamp frames */
- if (vector.flags == MOTIONSENSE_SENSOR_FLAG_TIMESTAMP) {
- continue;
- }
-
- /* Check acclerometer frames */
- if (ms_acc - motion_sensors == vector.sensor_num) {
- if (f_acc == NULL) {
- zassert_unreachable(
- "Not expected acclerometer data in FIFO, line %d",
- line);
- }
-
- convert_int3v_int16(vector.data, ret_v);
- drv_acc_to_emul(ret_v, acc_range, ret_v);
- exp_v[0] = f_acc->acc_x;
- exp_v[1] = f_acc->acc_y;
- exp_v[2] = f_acc->acc_z;
- compare_int3v_f(exp_v, ret_v, V_EPS, line);
- f_acc = f_acc->next;
- }
-
- /* Check gyroscope frames */
- if (ms_gyr - motion_sensors == vector.sensor_num) {
- if (f_gyr == NULL) {
- zassert_unreachable(
- "Not expected gyroscope data in FIFO, line %d",
- line);
- }
-
- convert_int3v_int16(vector.data, ret_v);
- drv_gyr_to_emul(ret_v, gyr_range, ret_v);
- exp_v[0] = f_gyr->gyr_x;
- exp_v[1] = f_gyr->gyr_y;
- exp_v[2] = f_gyr->gyr_z;
- compare_int3v_f(exp_v, ret_v, V_EPS, line);
- f_gyr = f_gyr->next;
- }
- }
-
- /* Skip frames of different type at the end */
- while (f_acc != NULL && !(f_acc->type & BMI_EMUL_FRAME_ACC)) {
- f_acc = f_acc->next;
- }
-
- while (f_gyr != NULL && !(f_gyr->type & BMI_EMUL_FRAME_GYR)) {
- f_gyr = f_gyr->next;
- }
-
- /* All frames are readed */
- zassert_is_null(f_acc, "Not all accelerometer frames are read, line %d",
- line);
- zassert_is_null(f_gyr, "Not all gyroscope frames are read, line %d",
- line);
-}
-#define check_fifo(ms_acc, ms_gyr, frame, acc_range, gyr_range) \
- check_fifo_f(ms_acc, ms_gyr, frame, acc_range, gyr_range, __LINE__)
-
-/** Test irq handler of accelerometer sensor */
-static void test_bmi_acc_fifo(void)
-{
- struct motion_sensor_t *ms, *ms_gyr;
- struct fifo_func_data func_data;
- struct bmi_emul_frame f[3];
- struct i2c_emul *emul;
- int gyr_range = 125;
- int acc_range = 2;
- int event;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
- ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Need to be set to collect all data in FIFO */
- ms->oversampling_ratio = 1;
- ms_gyr->oversampling_ratio = 1;
- /* Only BMI event should be handled */
- event = 0x1234 & ~BMI_INT_EVENT;
- zassert_equal(EC_ERROR_NOT_HANDLED, ms->drv->irq_handler(ms, &event),
- NULL);
-
- event = BMI_INT_EVENT;
-
- /* Test fail to read interrupt status registers */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_INT_STATUS_0);
- zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_INT_STATUS_1);
- zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test no interrupt */
- bmi_emul_set_reg(emul, BMI160_INT_STATUS_0, 0);
- bmi_emul_set_reg(emul, BMI160_INT_STATUS_1, 0);
-
- /* Trigger irq handler and check results */
- check_fifo(ms, ms_gyr, NULL, acc_range, gyr_range);
-
- /* Set custom function for FIFO test */
- i2c_common_emul_set_read_func(emul, emul_fifo_func, &func_data);
- /* Enable sensor FIFO */
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 50000, 0), NULL);
- /* Set range */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, acc_range, 0), NULL);
- zassert_equal(EC_SUCCESS, ms_gyr->drv->set_range(ms_gyr, gyr_range, 0),
- NULL);
- /* Setup single accelerometer frame */
- f[0].type = BMI_EMUL_FRAME_ACC;
- f[0].acc_x = BMI_EMUL_1G / 10;
- f[0].acc_y = BMI_EMUL_1G / 20;
- f[0].acc_z = -(int)BMI_EMUL_1G / 30;
- f[0].next = NULL;
- bmi_emul_append_frame(emul, f);
- /* Setup interrupts register */
- func_data.interrupts = BMI160_FWM_INT;
-
- /* Trigger irq handler and check results */
- check_fifo(ms, ms_gyr, f, acc_range, gyr_range);
-
- /* Setup second accelerometer frame */
- f[1].type = BMI_EMUL_FRAME_ACC;
- f[1].acc_x = -(int)BMI_EMUL_1G / 40;
- f[1].acc_y = BMI_EMUL_1G / 50;
- f[1].acc_z = BMI_EMUL_1G / 60;
- f[0].next = &(f[1]);
- f[1].next = NULL;
- bmi_emul_append_frame(emul, f);
- /* Setup interrupts register */
- func_data.interrupts = BMI160_FWM_INT;
-
- /* Trigger irq handler and check results */
- check_fifo(ms, ms_gyr, f, acc_range, gyr_range);
-
- /* Enable sensor FIFO */
- zassert_equal(EC_SUCCESS, ms_gyr->drv->set_data_rate(ms_gyr, 50000, 0),
- NULL);
-
- /* Setup first gyroscope frame (after two accelerometer frames) */
- f[2].type = BMI_EMUL_FRAME_GYR;
- f[2].gyr_x = -(int)BMI_EMUL_125_DEG_S / 100;
- f[2].gyr_y = BMI_EMUL_125_DEG_S / 200;
- f[2].gyr_z = BMI_EMUL_125_DEG_S / 300;
- f[1].next = &(f[2]);
- f[2].next = NULL;
- bmi_emul_append_frame(emul, f);
- /* Setup interrupts register */
- func_data.interrupts = BMI160_FWM_INT;
-
- /* Trigger irq handler and check results */
- check_fifo(ms, ms_gyr, f, acc_range, gyr_range);
-
- /* Setup second accelerometer frame to by gyroscope frame too */
- f[1].type |= BMI_EMUL_FRAME_GYR;
- f[1].gyr_x = -(int)BMI_EMUL_125_DEG_S / 300;
- f[1].gyr_y = BMI_EMUL_125_DEG_S / 400;
- f[1].gyr_z = BMI_EMUL_125_DEG_S / 500;
- bmi_emul_append_frame(emul, f);
- /* Setup interrupts register */
- func_data.interrupts = BMI160_FWM_INT;
-
- /* Trigger irq handler and check results */
- check_fifo(ms, ms_gyr, f, acc_range, gyr_range);
-
- /* Skip frame should be ignored by driver */
- bmi_emul_set_skipped_frames(emul, 8);
- bmi_emul_append_frame(emul, f);
- /* Setup interrupts register */
- func_data.interrupts = BMI160_FWM_INT;
-
- /* Trigger irq handler and check results */
- check_fifo(ms, ms_gyr, f, acc_range, gyr_range);
-
- /* Setup second frame as an config frame */
- f[1].type = BMI_EMUL_FRAME_CONFIG;
- /* Indicate that accelerometer range changed */
- f[1].config = 0x1;
- bmi_emul_append_frame(emul, f);
- /* Setup interrupts register */
- func_data.interrupts = BMI160_FWM_INT;
-
- /* Trigger irq handler and check results */
- check_fifo(ms, ms_gyr, f, acc_range, gyr_range);
-
- /* Remove custom emulator read function */
- i2c_common_emul_set_read_func(emul, NULL, NULL);
-}
-
-/** Test irq handler of gyroscope sensor */
-static void test_bmi_gyr_fifo(void)
-{
- struct motion_sensor_t *ms;
- uint32_t event;
-
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Interrupt shuldn't be triggered for gyroscope motion sense */
- event = BMI_INT_EVENT;
- zassert_equal(EC_ERROR_NOT_HANDLED, ms->drv->irq_handler(ms, &event),
- NULL);
-}
-
-void test_suite_bmi160(void)
-{
- ztest_test_suite(bmi160,
- ztest_user_unit_test(test_bmi_acc_get_offset),
- ztest_user_unit_test(test_bmi_gyr_get_offset),
- ztest_user_unit_test(test_bmi_acc_set_offset),
- ztest_user_unit_test(test_bmi_gyr_set_offset),
- ztest_user_unit_test(test_bmi_acc_set_range),
- ztest_user_unit_test(test_bmi_gyr_set_range),
- ztest_user_unit_test(test_bmi_get_resolution),
- ztest_user_unit_test(test_bmi_acc_rate),
- ztest_user_unit_test(test_bmi_gyr_rate),
- ztest_user_unit_test(test_bmi_scale),
- ztest_user_unit_test(test_bmi_read_temp),
- ztest_user_unit_test(test_bmi_acc_read),
- ztest_user_unit_test(test_bmi_gyr_read),
- ztest_user_unit_test(test_bmi_acc_perform_calib),
- ztest_user_unit_test(test_bmi_gyr_perform_calib),
- ztest_user_unit_test(test_bmi_init),
- ztest_user_unit_test(test_bmi_acc_fifo),
- ztest_user_unit_test(test_bmi_gyr_fifo));
- ztest_run_test_suite(bmi160);
-}
diff --git a/zephyr/test/drivers/src/bmi260.c b/zephyr/test/drivers/src/bmi260.c
deleted file mode 100644
index 637e5f353b..0000000000
--- a/zephyr/test/drivers/src/bmi260.c
+++ /dev/null
@@ -1,1864 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <zephyr.h>
-#include <ztest.h>
-
-#include "common.h"
-#include "i2c.h"
-#include "emul/emul_bmi.h"
-#include "emul/emul_common_i2c.h"
-
-#include "motion_sense_fifo.h"
-#include "driver/accelgyro_bmi260.h"
-#include "driver/accelgyro_bmi_common.h"
-
-#define BMI_ORD DT_DEP_ORD(DT_NODELABEL(accel_bmi260))
-#define BMI_ACC_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi260_accel))
-#define BMI_GYR_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi260_gyro))
-#define BMI_INT_EVENT \
- TASK_EVENT_MOTION_SENSOR_INTERRUPT(SENSOR_ID(DT_ALIAS(bmi260_int)))
-
-/** How accurate comparision of vectors should be */
-#define V_EPS 8
-
-/** Convert from one type of vector to another */
-#define convert_int3v_int16(v, r) do { \
- r[0] = v[0]; \
- r[1] = v[1]; \
- r[2] = v[2]; \
- } while (0)
-
-/** Rotation used in some tests */
-static const mat33_fp_t test_rotation = {
- { 0, FLOAT_TO_FP(1), 0},
- { FLOAT_TO_FP(-1), 0, 0},
- { 0, 0, FLOAT_TO_FP(-1)}
-};
-/** Rotate given vector by test rotation */
-static void rotate_int3v_by_test_rotation(intv3_t v)
-{
- int16_t t;
-
- t = v[0];
- v[0] = -v[1];
- v[1] = t;
- v[2] = -v[2];
-}
-
-/** Set emulator accelerometer offset values to intv3_t vector */
-static void set_emul_acc_offset(struct i2c_emul *emul, intv3_t offset)
-{
- bmi_emul_set_off(emul, BMI_EMUL_ACC_X, offset[0]);
- bmi_emul_set_off(emul, BMI_EMUL_ACC_Y, offset[1]);
- bmi_emul_set_off(emul, BMI_EMUL_ACC_Z, offset[2]);
-}
-
-/** Save emulator accelerometer offset values to intv3_t vector */
-static void get_emul_acc_offset(struct i2c_emul *emul, intv3_t offset)
-{
- offset[0] = bmi_emul_get_off(emul, BMI_EMUL_ACC_X);
- offset[1] = bmi_emul_get_off(emul, BMI_EMUL_ACC_Y);
- offset[2] = bmi_emul_get_off(emul, BMI_EMUL_ACC_Z);
-}
-
-/** Set emulator accelerometer values to intv3_t vector */
-static void set_emul_acc(struct i2c_emul *emul, intv3_t acc)
-{
- bmi_emul_set_value(emul, BMI_EMUL_ACC_X, acc[0]);
- bmi_emul_set_value(emul, BMI_EMUL_ACC_Y, acc[1]);
- bmi_emul_set_value(emul, BMI_EMUL_ACC_Z, acc[2]);
-}
-
-/** Set emulator gyroscope offset values to intv3_t vector */
-static void set_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset)
-{
- bmi_emul_set_off(emul, BMI_EMUL_GYR_X, offset[0]);
- bmi_emul_set_off(emul, BMI_EMUL_GYR_Y, offset[1]);
- bmi_emul_set_off(emul, BMI_EMUL_GYR_Z, offset[2]);
-}
-
-/** Save emulator gyroscope offset values to intv3_t vector */
-static void get_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset)
-{
- offset[0] = bmi_emul_get_off(emul, BMI_EMUL_GYR_X);
- offset[1] = bmi_emul_get_off(emul, BMI_EMUL_GYR_Y);
- offset[2] = bmi_emul_get_off(emul, BMI_EMUL_GYR_Z);
-}
-
-/** Set emulator gyroscope values to vector of three int16_t */
-static void set_emul_gyr(struct i2c_emul *emul, intv3_t gyr)
-{
- bmi_emul_set_value(emul, BMI_EMUL_GYR_X, gyr[0]);
- bmi_emul_set_value(emul, BMI_EMUL_GYR_Y, gyr[1]);
- bmi_emul_set_value(emul, BMI_EMUL_GYR_Z, gyr[2]);
-}
-
-/** Convert accelerometer read to units used by emulator */
-static void drv_acc_to_emul(intv3_t drv, int range, intv3_t out)
-{
- const int scale = MOTION_SCALING_FACTOR / BMI_EMUL_1G;
-
- out[0] = drv[0] * range / scale;
- out[1] = drv[1] * range / scale;
- out[2] = drv[2] * range / scale;
-}
-
-/** Convert gyroscope read to units used by emulator */
-static void drv_gyr_to_emul(intv3_t drv, int range, intv3_t out)
-{
- const int scale = MOTION_SCALING_FACTOR / BMI_EMUL_125_DEG_S;
-
- range /= 125;
- out[0] = drv[0] * range / scale;
- out[1] = drv[1] * range / scale;
- out[2] = drv[2] * range / scale;
-}
-
-/** Compare two vectors of intv3_t type */
-static void compare_int3v_f(intv3_t exp_v, intv3_t v, int eps, int line)
-{
- int i;
-
- for (i = 0; i < 3; i++) {
- zassert_within(exp_v[i], v[i], eps,
- "Expected [%d; %d; %d], got [%d; %d; %d]; line: %d",
- exp_v[0], exp_v[1], exp_v[2], v[0], v[1], v[2], line);
- }
-}
-#define compare_int3v_eps(exp_v, v, e) compare_int3v_f(exp_v, v, e, __LINE__)
-#define compare_int3v(exp_v, v) compare_int3v_eps(exp_v, v, V_EPS)
-
-/** Test get accelerometer offset with and without rotation */
-static void test_bmi_acc_get_offset(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- int16_t ret[3];
- intv3_t ret_v;
- intv3_t exp_v;
- int16_t temp;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- /* Set emulator offset */
- exp_v[0] = BMI_EMUL_1G / 10;
- exp_v[1] = BMI_EMUL_1G / 20;
- exp_v[2] = -(int)BMI_EMUL_1G / 30;
- set_emul_acc_offset(emul, exp_v);
- /* BMI driver returns value in mg units */
- exp_v[0] = 1000 / 10;
- exp_v[1] = 1000 / 20;
- exp_v[2] = -1000 / 30;
-
- /* Test fail on offset read */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70);
- zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 1);
- zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 2);
- zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Disable rotation */
- ms->rot_standard_ref = NULL;
-
- /* Test get offset without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL);
- convert_int3v_int16(ret, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Setup rotation and rotate expected offset */
- ms->rot_standard_ref = &test_rotation;
- rotate_int3v_by_test_rotation(exp_v);
-
- /* Test get offset with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL);
- convert_int3v_int16(ret, ret_v);
- compare_int3v(exp_v, ret_v);
-}
-
-/** Test get gyroscope offset with and without rotation */
-static void test_bmi_gyr_get_offset(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- int16_t ret[3];
- intv3_t ret_v;
- intv3_t exp_v;
- int16_t temp;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Do not fail on read */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Set emulator offset */
- exp_v[0] = BMI_EMUL_125_DEG_S / 100;
- exp_v[1] = BMI_EMUL_125_DEG_S / 200;
- exp_v[2] = -(int)BMI_EMUL_125_DEG_S / 300;
- set_emul_gyr_offset(emul, exp_v);
- /* BMI driver returns value in mdeg/s units */
- exp_v[0] = 125000 / 100;
- exp_v[1] = 125000 / 200;
- exp_v[2] = -125000 / 300;
-
- /* Test fail on offset read */
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70);
- zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 1);
- zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 2);
- zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98);
- zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Disable rotation */
- ms->rot_standard_ref = NULL;
-
- /* Test get offset without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL);
- convert_int3v_int16(ret, ret_v);
- compare_int3v_eps(exp_v, ret_v, 64);
-
- /* Setup rotation and rotate expected offset */
- ms->rot_standard_ref = &test_rotation;
- rotate_int3v_by_test_rotation(exp_v);
-
- /* Test get offset with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->get_offset(ms, ret, &temp),
- NULL);
- zassert_equal(temp, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL);
- convert_int3v_int16(ret, ret_v);
- compare_int3v_eps(exp_v, ret_v, 64);
-}
-
-/**
- * Test set accelerometer offset with and without rotation. Also test behaviour
- * on I2C error.
- */
-static void test_bmi_acc_set_offset(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- int16_t input_v[3];
- int16_t temp = 0;
- intv3_t ret_v;
- intv3_t exp_v;
- uint8_t nv_c;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- /* Test fail on NV CONF register read and write */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_NV_CONF);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- i2c_common_emul_set_write_fail_reg(emul, BMI260_NV_CONF);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test fail on offset write */
- i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 1);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 2);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Setup NV_CONF register value */
- bmi_emul_set_reg(emul, BMI260_NV_CONF, 0x7);
- /* Set input offset */
- exp_v[0] = BMI_EMUL_1G / 10;
- exp_v[1] = BMI_EMUL_1G / 20;
- exp_v[2] = -(int)BMI_EMUL_1G / 30;
- /* BMI driver accept value in mg units */
- input_v[0] = 1000 / 10;
- input_v[1] = 1000 / 20;
- input_v[2] = -1000 / 30;
- /* Disable rotation */
- ms->rot_standard_ref = NULL;
-
- /* Test set offset without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->set_offset(ms, input_v, temp), NULL);
- get_emul_acc_offset(emul, ret_v);
- /*
- * Depending on used range, accelerometer values may be up to 6 bits
- * more accurate then offset value resolution.
- */
- compare_int3v_eps(exp_v, ret_v, 64);
- nv_c = bmi_emul_get_reg(emul, BMI260_NV_CONF);
- /* Only ACC_OFFSET_EN bit should be changed */
- zassert_equal(0x7 | BMI260_ACC_OFFSET_EN, nv_c,
- "Expected 0x%x, got 0x%x",
- 0x7 | BMI260_ACC_OFFSET_EN, nv_c);
-
- /* Setup NV_CONF register value */
- bmi_emul_set_reg(emul, BMI260_NV_CONF, 0);
- /* Setup rotation and rotate input for set_offset function */
- ms->rot_standard_ref = &test_rotation;
- convert_int3v_int16(input_v, ret_v);
- rotate_int3v_by_test_rotation(ret_v);
- convert_int3v_int16(ret_v, input_v);
-
- /* Test set offset with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->set_offset(ms, input_v, temp), NULL);
- get_emul_acc_offset(emul, ret_v);
- compare_int3v_eps(exp_v, ret_v, 64);
- nv_c = bmi_emul_get_reg(emul, BMI260_NV_CONF);
- /* Only ACC_OFFSET_EN bit should be changed */
- zassert_equal(BMI260_ACC_OFFSET_EN, nv_c, "Expected 0x%x, got 0x%x",
- BMI260_ACC_OFFSET_EN, nv_c);
-}
-
-/**
- * Test set gyroscope offset with and without rotation. Also test behaviour
- * on I2C error.
- */
-static void test_bmi_gyr_set_offset(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- int16_t input_v[3];
- int16_t temp = 0;
- intv3_t ret_v;
- intv3_t exp_v;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Test fail on OFFSET EN GYR98 register read and write */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_OFFSET_EN_GYR98);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_EN_GYR98);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test fail on offset write */
- i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70 + 1);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70 + 2);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp),
- NULL);
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Set input offset */
- exp_v[0] = BMI_EMUL_125_DEG_S / 100;
- exp_v[1] = BMI_EMUL_125_DEG_S / 200;
- exp_v[2] = -(int)BMI_EMUL_125_DEG_S / 300;
- /* BMI driver accept value in mdeg/s units */
- input_v[0] = 125000 / 100;
- input_v[1] = 125000 / 200;
- input_v[2] = -125000 / 300;
- /* Disable rotation */
- ms->rot_standard_ref = NULL;
-
- /* Test set offset without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->set_offset(ms, input_v, temp), NULL);
- get_emul_gyr_offset(emul, ret_v);
- /*
- * Depending on used range, gyroscope values may be up to 4 bits
- * more accurate then offset value resolution.
- */
- compare_int3v_eps(exp_v, ret_v, 32);
- /* Gyroscope offset should be enabled */
- zassert_true(bmi_emul_get_reg(emul, BMI260_OFFSET_EN_GYR98) &
- BMI260_OFFSET_GYRO_EN, NULL);
-
- /* Setup rotation and rotate input for set_offset function */
- ms->rot_standard_ref = &test_rotation;
- convert_int3v_int16(input_v, ret_v);
- rotate_int3v_by_test_rotation(ret_v);
- convert_int3v_int16(ret_v, input_v);
-
- /* Test set offset with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->set_offset(ms, input_v, temp), NULL);
- get_emul_gyr_offset(emul, ret_v);
- compare_int3v_eps(exp_v, ret_v, 32);
- zassert_true(bmi_emul_get_reg(emul, BMI260_OFFSET_EN_GYR98) &
- BMI260_OFFSET_GYRO_EN, NULL);
-}
-
-/**
- * Try to set accelerometer range and check if expected range was set
- * in driver and in emulator.
- */
-static void check_set_acc_range_f(struct i2c_emul *emul,
- struct motion_sensor_t *ms, int range,
- int rnd, int exp_range, int line)
-{
- uint8_t exp_range_reg;
- uint8_t range_reg;
-
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, range, rnd),
- "set_range failed; line: %d", line);
- zassert_equal(exp_range, ms->current_range,
- "Expected range %d, got %d; line %d",
- exp_range, ms->current_range, line);
- range_reg = bmi_emul_get_reg(emul, BMI260_ACC_RANGE);
-
- switch (exp_range) {
- case 2:
- exp_range_reg = BMI260_GSEL_2G;
- break;
- case 4:
- exp_range_reg = BMI260_GSEL_4G;
- break;
- case 8:
- exp_range_reg = BMI260_GSEL_8G;
- break;
- case 16:
- exp_range_reg = BMI260_GSEL_16G;
- break;
- default:
- /* Unknown expected range */
- zassert_unreachable(
- "Expected range %d not supported by device; line %d",
- exp_range, line);
- return;
- }
-
- zassert_equal(exp_range_reg, range_reg,
- "Expected range reg 0x%x, got 0x%x; line %d",
- exp_range_reg, range_reg, line);
-}
-#define check_set_acc_range(emul, ms, range, rnd, exp_range) \
- check_set_acc_range_f(emul, ms, range, rnd, exp_range, __LINE__)
-
-/** Test set accelerometer range with and without I2C errors */
-static void test_bmi_acc_set_range(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- int start_range;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- /* Setup starting range, shouldn't be changed on error */
- start_range = 2;
- ms->current_range = start_range;
- bmi_emul_set_reg(emul, BMI260_ACC_RANGE, BMI260_GSEL_2G);
- /* Setup emulator fail on write */
- i2c_common_emul_set_write_fail_reg(emul, BMI260_ACC_RANGE);
-
- /* Test fail on write */
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 12, 0), NULL);
- zassert_equal(start_range, ms->current_range, NULL);
- zassert_equal(BMI260_GSEL_2G,
- bmi_emul_get_reg(emul, BMI260_ACC_RANGE), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 12, 1), NULL);
- zassert_equal(start_range, ms->current_range, NULL);
- zassert_equal(BMI260_GSEL_2G,
- bmi_emul_get_reg(emul, BMI260_ACC_RANGE), NULL);
-
- /* Do not fail on write */
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test setting range with rounding down */
- check_set_acc_range(emul, ms, 1, 0, 2);
- check_set_acc_range(emul, ms, 2, 0, 2);
- check_set_acc_range(emul, ms, 3, 0, 2);
- check_set_acc_range(emul, ms, 4, 0, 4);
- check_set_acc_range(emul, ms, 5, 0, 4);
- check_set_acc_range(emul, ms, 6, 0, 4);
- check_set_acc_range(emul, ms, 7, 0, 4);
- check_set_acc_range(emul, ms, 8, 0, 8);
- check_set_acc_range(emul, ms, 9, 0, 8);
- check_set_acc_range(emul, ms, 15, 0, 8);
- check_set_acc_range(emul, ms, 16, 0, 16);
- check_set_acc_range(emul, ms, 17, 0, 16);
-
- /* Test setting range with rounding up */
- check_set_acc_range(emul, ms, 1, 1, 2);
- check_set_acc_range(emul, ms, 2, 1, 2);
- check_set_acc_range(emul, ms, 3, 1, 4);
- check_set_acc_range(emul, ms, 4, 1, 4);
- check_set_acc_range(emul, ms, 5, 1, 8);
- check_set_acc_range(emul, ms, 6, 1, 8);
- check_set_acc_range(emul, ms, 7, 1, 8);
- check_set_acc_range(emul, ms, 8, 1, 8);
- check_set_acc_range(emul, ms, 9, 1, 16);
- check_set_acc_range(emul, ms, 15, 1, 16);
- check_set_acc_range(emul, ms, 16, 1, 16);
- check_set_acc_range(emul, ms, 17, 1, 16);
-}
-
-/**
- * Try to set gyroscope range and check if expected range was set in driver and
- * in emulator.
- */
-static void check_set_gyr_range_f(struct i2c_emul *emul,
- struct motion_sensor_t *ms, int range,
- int rnd, int exp_range, int line)
-{
- uint8_t exp_range_reg;
- uint8_t range_reg;
-
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, range, rnd),
- "set_range failed; line: %d", line);
- zassert_equal(exp_range, ms->current_range,
- "Expected range %d, got %d; line %d",
- exp_range, ms->current_range, line);
- range_reg = bmi_emul_get_reg(emul, BMI260_GYR_RANGE);
-
- switch (exp_range) {
- case 125:
- exp_range_reg = BMI260_DPS_SEL_125;
- break;
- case 250:
- exp_range_reg = BMI260_DPS_SEL_250;
- break;
- case 500:
- exp_range_reg = BMI260_DPS_SEL_500;
- break;
- case 1000:
- exp_range_reg = BMI260_DPS_SEL_1000;
- break;
- case 2000:
- exp_range_reg = BMI260_DPS_SEL_2000;
- break;
- default:
- /* Unknown expected range */
- zassert_unreachable(
- "Expected range %d not supported by device; line %d",
- exp_range, line);
- return;
- }
-
- zassert_equal(exp_range_reg, range_reg,
- "Expected range reg 0x%x, got 0x%x; line %d",
- exp_range_reg, range_reg, line);
-}
-#define check_set_gyr_range(emul, ms, range, rnd, exp_range) \
- check_set_gyr_range_f(emul, ms, range, rnd, exp_range, __LINE__)
-
-/** Test set gyroscope range with and without I2C errors */
-static void test_bmi_gyr_set_range(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- int start_range;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Setup starting range, shouldn't be changed on error */
- start_range = 250;
- ms->current_range = start_range;
- bmi_emul_set_reg(emul, BMI260_GYR_RANGE, BMI260_DPS_SEL_250);
- /* Setup emulator fail on write */
- i2c_common_emul_set_write_fail_reg(emul, BMI260_GYR_RANGE);
-
- /* Test fail on write */
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 125, 0), NULL);
- zassert_equal(start_range, ms->current_range, NULL);
- zassert_equal(BMI260_DPS_SEL_250,
- bmi_emul_get_reg(emul, BMI260_GYR_RANGE), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 125, 1), NULL);
- zassert_equal(start_range, ms->current_range, NULL);
- zassert_equal(BMI260_DPS_SEL_250,
- bmi_emul_get_reg(emul, BMI260_GYR_RANGE), NULL);
-
- /* Do not fail on write */
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test setting range with rounding down */
- check_set_gyr_range(emul, ms, 1, 0, 125);
- check_set_gyr_range(emul, ms, 124, 0, 125);
- check_set_gyr_range(emul, ms, 125, 0, 125);
- check_set_gyr_range(emul, ms, 126, 0, 125);
- check_set_gyr_range(emul, ms, 249, 0, 125);
- check_set_gyr_range(emul, ms, 250, 0, 250);
- check_set_gyr_range(emul, ms, 251, 0, 250);
- check_set_gyr_range(emul, ms, 499, 0, 250);
- check_set_gyr_range(emul, ms, 500, 0, 500);
- check_set_gyr_range(emul, ms, 501, 0, 500);
- check_set_gyr_range(emul, ms, 999, 0, 500);
- check_set_gyr_range(emul, ms, 1000, 0, 1000);
- check_set_gyr_range(emul, ms, 1001, 0, 1000);
- check_set_gyr_range(emul, ms, 1999, 0, 1000);
- check_set_gyr_range(emul, ms, 2000, 0, 2000);
- check_set_gyr_range(emul, ms, 2001, 0, 2000);
-
- /* Test setting range with rounding up */
- check_set_gyr_range(emul, ms, 1, 1, 125);
- check_set_gyr_range(emul, ms, 124, 1, 125);
- check_set_gyr_range(emul, ms, 125, 1, 125);
- check_set_gyr_range(emul, ms, 126, 1, 250);
- check_set_gyr_range(emul, ms, 249, 1, 250);
- check_set_gyr_range(emul, ms, 250, 1, 250);
- check_set_gyr_range(emul, ms, 251, 1, 500);
- check_set_gyr_range(emul, ms, 499, 1, 500);
- check_set_gyr_range(emul, ms, 500, 1, 500);
- check_set_gyr_range(emul, ms, 501, 1, 1000);
- check_set_gyr_range(emul, ms, 999, 1, 1000);
- check_set_gyr_range(emul, ms, 1000, 1, 1000);
- check_set_gyr_range(emul, ms, 1001, 1, 2000);
- check_set_gyr_range(emul, ms, 1999, 1, 2000);
- check_set_gyr_range(emul, ms, 2000, 1, 2000);
- check_set_gyr_range(emul, ms, 2001, 1, 2000);
-}
-
-/** Test get resolution of acclerometer and gyroscope sensor */
-static void test_bmi_get_resolution(void)
-{
- struct motion_sensor_t *ms;
-
- /* Test accelerometer */
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- /* Resolution should be always 16 bits */
- zassert_equal(16, ms->drv->get_resolution(ms), NULL);
-
- /* Test gyroscope */
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Resolution should be always 16 bits */
- zassert_equal(16, ms->drv->get_resolution(ms), NULL);
-}
-
-/**
- * Try to set accelerometer data rate and check if expected rate was set
- * in driver and in emulator.
- */
-static void check_set_acc_rate_f(struct i2c_emul *emul,
- struct motion_sensor_t *ms, int rate, int rnd,
- int exp_rate, int line)
-{
- uint8_t exp_rate_reg;
- uint8_t rate_reg;
- int drv_rate;
-
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, rate, rnd),
- "set_data_rate failed; line: %d", line);
- drv_rate = ms->drv->get_data_rate(ms);
- zassert_equal(exp_rate, drv_rate, "Expected rate %d, got %d; line %d",
- exp_rate, drv_rate, line);
- rate_reg = bmi_emul_get_reg(emul, BMI260_ACC_CONF);
- rate_reg &= BMI_ODR_MASK;
-
- switch (exp_rate) {
- case 12500:
- exp_rate_reg = 0x5;
- break;
- case 25000:
- exp_rate_reg = 0x6;
- break;
- case 50000:
- exp_rate_reg = 0x7;
- break;
- case 100000:
- exp_rate_reg = 0x8;
- break;
- case 200000:
- exp_rate_reg = 0x9;
- break;
- case 400000:
- exp_rate_reg = 0xa;
- break;
- case 800000:
- exp_rate_reg = 0xb;
- break;
- case 1600000:
- exp_rate_reg = 0xc;
- break;
- default:
- /* Unknown expected rate */
- zassert_unreachable(
- "Expected rate %d not supported by device; line %d",
- exp_rate, line);
- return;
- }
-
- zassert_equal(exp_rate_reg, rate_reg,
- "Expected rate reg 0x%x, got 0x%x; line %d",
- exp_rate_reg, rate_reg, line);
-}
-#define check_set_acc_rate(emul, ms, rate, rnd, exp_rate) \
- check_set_acc_rate_f(emul, ms, rate, rnd, exp_rate, __LINE__)
-
-/** Test set and get accelerometer rate with and without I2C errors */
-static void test_bmi_acc_rate(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- uint8_t reg_rate;
- uint8_t pwr_ctrl;
- int drv_rate;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- /* Test setting rate with rounding down */
- check_set_acc_rate(emul, ms, 12500, 0, 12500);
- check_set_acc_rate(emul, ms, 12501, 0, 12500);
- check_set_acc_rate(emul, ms, 24999, 0, 12500);
- check_set_acc_rate(emul, ms, 25000, 0, 25000);
- check_set_acc_rate(emul, ms, 25001, 0, 25000);
- check_set_acc_rate(emul, ms, 49999, 0, 25000);
- check_set_acc_rate(emul, ms, 50000, 0, 50000);
- check_set_acc_rate(emul, ms, 50001, 0, 50000);
- check_set_acc_rate(emul, ms, 99999, 0, 50000);
- check_set_acc_rate(emul, ms, 100000, 0, 100000);
- check_set_acc_rate(emul, ms, 100001, 0, 100000);
- check_set_acc_rate(emul, ms, 199999, 0, 100000);
- check_set_acc_rate(emul, ms, 200000, 0, 200000);
- check_set_acc_rate(emul, ms, 200001, 0, 200000);
- check_set_acc_rate(emul, ms, 399999, 0, 200000);
- /*
- * We cannot test frequencies from 400000 to 1600000 because
- * CONFIG_EC_MAX_SENSOR_FREQ_MILLIHZ is set to 250000
- */
-
- /* Test setting rate with rounding up */
- check_set_acc_rate(emul, ms, 6251, 1, 12500);
- check_set_acc_rate(emul, ms, 12499, 1, 12500);
- check_set_acc_rate(emul, ms, 12500, 1, 12500);
- check_set_acc_rate(emul, ms, 12501, 1, 25000);
- check_set_acc_rate(emul, ms, 24999, 1, 25000);
- check_set_acc_rate(emul, ms, 25000, 1, 25000);
- check_set_acc_rate(emul, ms, 25001, 1, 50000);
- check_set_acc_rate(emul, ms, 49999, 1, 50000);
- check_set_acc_rate(emul, ms, 50000, 1, 50000);
- check_set_acc_rate(emul, ms, 50001, 1, 100000);
- check_set_acc_rate(emul, ms, 99999, 1, 100000);
- check_set_acc_rate(emul, ms, 100000, 1, 100000);
- check_set_acc_rate(emul, ms, 100001, 1, 200000);
- check_set_acc_rate(emul, ms, 199999, 1, 200000);
- check_set_acc_rate(emul, ms, 200000, 1, 200000);
-
- /* Test out of range rate with rounding down */
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 1, 0), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 12499, 0), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 400000, 0), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 2000000, 0), NULL);
-
- /* Test out of range rate with rounding up */
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 1, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 6250, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 200001, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 400000, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 2000000, 1), NULL);
-
- /* Current rate shouldn't be changed on error */
- drv_rate = ms->drv->get_data_rate(ms);
- reg_rate = bmi_emul_get_reg(emul, BMI260_ACC_CONF);
-
- /* Setup emulator fail on read */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_CONF);
-
- /* Test fail on read */
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_ACC_CONF), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 1),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_ACC_CONF), NULL);
-
- /* Do not fail on read */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Setup emulator fail on write */
- i2c_common_emul_set_write_fail_reg(emul, BMI260_ACC_CONF);
-
- /* Test fail on write */
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_ACC_CONF), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 1),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_ACC_CONF), NULL);
-
- /* Do not fail on write */
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test disabling sensor */
- bmi_emul_set_reg(emul, BMI260_PWR_CTRL,
- BMI260_AUX_EN | BMI260_GYR_EN | BMI260_ACC_EN);
- bmi_emul_set_reg(emul, BMI260_ACC_CONF, BMI260_FILTER_PERF);
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 0, 0), NULL);
-
- pwr_ctrl = bmi_emul_get_reg(emul, BMI260_PWR_CTRL);
- reg_rate = bmi_emul_get_reg(emul, BMI260_ACC_CONF);
- zassert_equal(BMI260_AUX_EN | BMI260_GYR_EN, pwr_ctrl, NULL);
- zassert_true(!(reg_rate & BMI260_FILTER_PERF), NULL);
-
- /* Test enabling sensor */
- bmi_emul_set_reg(emul, BMI260_PWR_CTRL, 0);
- bmi_emul_set_reg(emul, BMI260_ACC_CONF, 0);
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 50000, 0), NULL);
-
- pwr_ctrl = bmi_emul_get_reg(emul, BMI260_PWR_CTRL);
- reg_rate = bmi_emul_get_reg(emul, BMI260_ACC_CONF);
- zassert_equal(BMI260_ACC_EN, pwr_ctrl, NULL);
- zassert_true(reg_rate & BMI260_FILTER_PERF, NULL);
-}
-
-/**
- * Try to set gyroscope data rate and check if expected rate was set
- * in driver and in emulator.
- */
-static void check_set_gyr_rate_f(struct i2c_emul *emul,
- struct motion_sensor_t *ms, int rate, int rnd,
- int exp_rate, int line)
-{
- uint8_t exp_rate_reg;
- uint8_t rate_reg;
- int drv_rate;
-
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, rate, rnd),
- "set_data_rate failed; line: %d", line);
- drv_rate = ms->drv->get_data_rate(ms);
- zassert_equal(exp_rate, drv_rate, "Expected rate %d, got %d; line %d",
- exp_rate, drv_rate, line);
- rate_reg = bmi_emul_get_reg(emul, BMI260_GYR_CONF);
- rate_reg &= BMI_ODR_MASK;
-
- switch (exp_rate) {
- case 25000:
- exp_rate_reg = 0x6;
- break;
- case 50000:
- exp_rate_reg = 0x7;
- break;
- case 100000:
- exp_rate_reg = 0x8;
- break;
- case 200000:
- exp_rate_reg = 0x9;
- break;
- case 400000:
- exp_rate_reg = 0xa;
- break;
- case 800000:
- exp_rate_reg = 0xb;
- break;
- case 1600000:
- exp_rate_reg = 0xc;
- break;
- case 3200000:
- exp_rate_reg = 0xc;
- break;
- default:
- /* Unknown expected rate */
- zassert_unreachable(
- "Expected rate %d not supported by device; line %d",
- exp_rate, line);
- return;
- }
-
- zassert_equal(exp_rate_reg, rate_reg,
- "Expected rate reg 0x%x, got 0x%x; line %d",
- exp_rate_reg, rate_reg, line);
-}
-#define check_set_gyr_rate(emul, ms, rate, rnd, exp_rate) \
- check_set_gyr_rate_f(emul, ms, rate, rnd, exp_rate, __LINE__)
-
-/** Test set and get gyroscope rate with and without I2C errors */
-static void test_bmi_gyr_rate(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- uint8_t reg_rate;
- uint8_t pwr_ctrl;
- int drv_rate;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Test setting rate with rounding down */
- check_set_gyr_rate(emul, ms, 25000, 0, 25000);
- check_set_gyr_rate(emul, ms, 25001, 0, 25000);
- check_set_gyr_rate(emul, ms, 49999, 0, 25000);
- check_set_gyr_rate(emul, ms, 50000, 0, 50000);
- check_set_gyr_rate(emul, ms, 50001, 0, 50000);
- check_set_gyr_rate(emul, ms, 99999, 0, 50000);
- check_set_gyr_rate(emul, ms, 100000, 0, 100000);
- check_set_gyr_rate(emul, ms, 100001, 0, 100000);
- check_set_gyr_rate(emul, ms, 199999, 0, 100000);
- check_set_gyr_rate(emul, ms, 200000, 0, 200000);
- check_set_gyr_rate(emul, ms, 200001, 0, 200000);
- check_set_gyr_rate(emul, ms, 399999, 0, 200000);
- /*
- * We cannot test frequencies from 400000 to 3200000 because
- * CONFIG_EC_MAX_SENSOR_FREQ_MILLIHZ is set to 250000
- */
-
- /* Test setting rate with rounding up */
- check_set_gyr_rate(emul, ms, 12501, 1, 25000);
- check_set_gyr_rate(emul, ms, 24999, 1, 25000);
- check_set_gyr_rate(emul, ms, 25000, 1, 25000);
- check_set_gyr_rate(emul, ms, 25001, 1, 50000);
- check_set_gyr_rate(emul, ms, 49999, 1, 50000);
- check_set_gyr_rate(emul, ms, 50000, 1, 50000);
- check_set_gyr_rate(emul, ms, 50001, 1, 100000);
- check_set_gyr_rate(emul, ms, 99999, 1, 100000);
- check_set_gyr_rate(emul, ms, 100000, 1, 100000);
- check_set_gyr_rate(emul, ms, 100001, 1, 200000);
- check_set_gyr_rate(emul, ms, 199999, 1, 200000);
- check_set_gyr_rate(emul, ms, 200000, 1, 200000);
-
- /* Test out of range rate with rounding down */
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 1, 0), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 24999, 0), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 400000, 0), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 4000000, 0), NULL);
-
- /* Test out of range rate with rounding up */
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 1, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 12499, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 200001, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 400000, 1), NULL);
- zassert_equal(EC_RES_INVALID_PARAM,
- ms->drv->set_data_rate(ms, 4000000, 1), NULL);
-
- /* Current rate shouldn't be changed on error */
- drv_rate = ms->drv->get_data_rate(ms);
- reg_rate = bmi_emul_get_reg(emul, BMI260_GYR_CONF);
-
- /* Setup emulator fail on read */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_CONF);
-
- /* Test fail on read */
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_GYR_CONF), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 1),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_GYR_CONF), NULL);
-
- /* Do not fail on read */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Setup emulator fail on write */
- i2c_common_emul_set_write_fail_reg(emul, BMI260_GYR_CONF);
-
- /* Test fail on write */
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_GYR_CONF), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 1),
- NULL);
- zassert_equal(drv_rate, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_GYR_CONF), NULL);
-
- /* Do not fail on write */
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test disabling sensor */
- bmi_emul_set_reg(emul, BMI260_PWR_CTRL,
- BMI260_AUX_EN | BMI260_GYR_EN | BMI260_ACC_EN);
- bmi_emul_set_reg(emul, BMI260_GYR_CONF,
- BMI260_FILTER_PERF | BMI260_GYR_NOISE_PERF);
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 0, 0), NULL);
-
- pwr_ctrl = bmi_emul_get_reg(emul, BMI260_PWR_CTRL);
- reg_rate = bmi_emul_get_reg(emul, BMI260_GYR_CONF);
- zassert_equal(BMI260_AUX_EN | BMI260_ACC_EN, pwr_ctrl, NULL);
- zassert_true(!(reg_rate & (BMI260_FILTER_PERF | BMI260_GYR_NOISE_PERF)),
- NULL);
-
- /* Test enabling sensor */
- bmi_emul_set_reg(emul, BMI260_PWR_CTRL, 0);
- bmi_emul_set_reg(emul, BMI260_GYR_CONF, 0);
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 50000, 0), NULL);
-
- pwr_ctrl = bmi_emul_get_reg(emul, BMI260_PWR_CTRL);
- reg_rate = bmi_emul_get_reg(emul, BMI260_GYR_CONF);
- zassert_equal(BMI260_GYR_EN, pwr_ctrl, NULL);
- zassert_true(reg_rate & (BMI260_FILTER_PERF | BMI260_GYR_NOISE_PERF),
- NULL);
-}
-
-/**
- * Test setting and getting scale in accelerometer and gyroscope sensors.
- * Correct appling scale to results is checked in "read" test.
- */
-static void test_bmi_scale(void)
-{
- struct motion_sensor_t *ms;
- int16_t ret_scale[3];
- int16_t exp_scale[3] = {100, 231, 421};
- int16_t t;
-
- /* Test accelerometer */
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- zassert_equal(EC_SUCCESS, ms->drv->set_scale(ms, exp_scale, 0), NULL);
- zassert_equal(EC_SUCCESS, ms->drv->get_scale(ms, ret_scale, &t), NULL);
-
- zassert_equal(t, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL);
- zassert_equal(exp_scale[0], ret_scale[0], NULL);
- zassert_equal(exp_scale[1], ret_scale[1], NULL);
- zassert_equal(exp_scale[2], ret_scale[2], NULL);
-
- /* Test gyroscope */
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- zassert_equal(EC_SUCCESS, ms->drv->set_scale(ms, exp_scale, 0), NULL);
- zassert_equal(EC_SUCCESS, ms->drv->get_scale(ms, ret_scale, &t), NULL);
-
- zassert_equal(t, (int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, NULL);
- zassert_equal(exp_scale[0], ret_scale[0], NULL);
- zassert_equal(exp_scale[1], ret_scale[1], NULL);
- zassert_equal(exp_scale[2], ret_scale[2], NULL);
-}
-
-/** Test reading temperature using accelerometer and gyroscope sensors */
-static void test_bmi_read_temp(void)
-{
- struct motion_sensor_t *ms_acc, *ms_gyr;
- struct i2c_emul *emul;
- int ret_temp;
- int exp_temp;
-
- emul = bmi_emul_get(BMI_ORD);
- ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID];
- ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Setup emulator fail on read */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_TEMPERATURE_0);
- zassert_equal(EC_ERROR_NOT_POWERED,
- ms_acc->drv->read_temp(ms_acc, &ret_temp), NULL);
- zassert_equal(EC_ERROR_NOT_POWERED,
- ms_gyr->drv->read_temp(ms_gyr, &ret_temp), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI260_TEMPERATURE_1);
- zassert_equal(EC_ERROR_NOT_POWERED,
- ms_acc->drv->read_temp(ms_acc, &ret_temp), NULL);
- zassert_equal(EC_ERROR_NOT_POWERED,
- ms_gyr->drv->read_temp(ms_gyr, &ret_temp), NULL);
- /* Do not fail on read */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Fail on invalid temperature */
- bmi_emul_set_reg(emul, BMI260_TEMPERATURE_0, 0x00);
- bmi_emul_set_reg(emul, BMI260_TEMPERATURE_1, 0x80);
- zassert_equal(EC_ERROR_NOT_POWERED,
- ms_acc->drv->read_temp(ms_acc, &ret_temp), NULL);
- zassert_equal(EC_ERROR_NOT_POWERED,
- ms_gyr->drv->read_temp(ms_gyr, &ret_temp), NULL);
-
- /*
- * Test correct values. Both motion sensors should return the same
- * temperature.
- */
- exp_temp = C_TO_K(23);
- bmi_emul_set_reg(emul, BMI260_TEMPERATURE_0, 0x00);
- bmi_emul_set_reg(emul, BMI260_TEMPERATURE_1, 0x00);
- zassert_equal(EC_SUCCESS, ms_acc->drv->read_temp(ms_acc, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
- zassert_equal(EC_SUCCESS, ms_gyr->drv->read_temp(ms_gyr, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
-
- exp_temp = C_TO_K(87);
- bmi_emul_set_reg(emul, BMI260_TEMPERATURE_0, 0xff);
- bmi_emul_set_reg(emul, BMI260_TEMPERATURE_1, 0x7f);
- zassert_equal(EC_SUCCESS, ms_acc->drv->read_temp(ms_acc, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
- zassert_equal(EC_SUCCESS, ms_gyr->drv->read_temp(ms_gyr, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
-
- exp_temp = C_TO_K(-41);
- bmi_emul_set_reg(emul, BMI260_TEMPERATURE_0, 0x01);
- bmi_emul_set_reg(emul, BMI260_TEMPERATURE_1, 0x80);
- zassert_equal(EC_SUCCESS, ms_acc->drv->read_temp(ms_acc, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
- zassert_equal(EC_SUCCESS, ms_gyr->drv->read_temp(ms_gyr, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
-
- exp_temp = C_TO_K(47);
- bmi_emul_set_reg(emul, BMI260_TEMPERATURE_0, 0x00);
- bmi_emul_set_reg(emul, BMI260_TEMPERATURE_1, 0x30);
- zassert_equal(EC_SUCCESS, ms_acc->drv->read_temp(ms_acc, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
- zassert_equal(EC_SUCCESS, ms_gyr->drv->read_temp(ms_gyr, &ret_temp),
- NULL);
- zassert_equal(exp_temp, ret_temp, NULL);
-}
-
-/** Test reading accelerometer sensor data */
-static void test_bmi_acc_read(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- intv3_t ret_v;
- intv3_t exp_v;
- int16_t scale[3] = {MOTION_SENSE_DEFAULT_SCALE,
- MOTION_SENSE_DEFAULT_SCALE,
- MOTION_SENSE_DEFAULT_SCALE};
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- /* Set offset 0 to simplify test */
- bmi_emul_set_off(emul, BMI_EMUL_ACC_X, 0);
- bmi_emul_set_off(emul, BMI_EMUL_ACC_Y, 0);
- bmi_emul_set_off(emul, BMI_EMUL_ACC_Z, 0);
-
- /* Fail on read status */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
-
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* When not ready, driver should return saved raw value */
- exp_v[0] = 100;
- exp_v[1] = 200;
- exp_v[2] = 300;
- ms->raw_xyz[0] = exp_v[0];
- ms->raw_xyz[1] = exp_v[1];
- ms->raw_xyz[2] = exp_v[2];
-
- /* Status not ready */
- bmi_emul_set_reg(emul, BMI260_STATUS, 0);
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- compare_int3v(exp_v, ret_v);
-
- /* Status only GYR ready */
- bmi_emul_set_reg(emul, BMI260_STATUS, BMI260_DRDY_GYR);
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- compare_int3v(exp_v, ret_v);
-
- /* Status ACC ready */
- bmi_emul_set_reg(emul, BMI260_STATUS, BMI260_DRDY_ACC);
-
- /* Set input accelerometer values */
- exp_v[0] = BMI_EMUL_1G / 10;
- exp_v[1] = BMI_EMUL_1G / 20;
- exp_v[2] = -(int)BMI_EMUL_1G / 30;
- set_emul_acc(emul, exp_v);
- /* Disable rotation */
- ms->rot_standard_ref = NULL;
- /* Set scale */
- zassert_equal(EC_SUCCESS, ms->drv->set_scale(ms, scale, 0), NULL);
- /* Set range to 2G */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 2, 0), NULL);
-
- /* Test read without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_acc_to_emul(ret_v, 2, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Set range to 4G */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 4, 0), NULL);
-
- /* Test read without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_acc_to_emul(ret_v, 4, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Setup rotation and rotate expected vector */
- ms->rot_standard_ref = &test_rotation;
- rotate_int3v_by_test_rotation(exp_v);
- /* Set range to 2G */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 2, 0), NULL);
-
- /* Test read with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_acc_to_emul(ret_v, 2, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Set range to 4G */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 4, 0), NULL);
-
- /* Test read with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_acc_to_emul(ret_v, 4, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Fail on read of data registers */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_X_L_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_X_H_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Y_L_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Y_H_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Z_L_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Z_H_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
-
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- ms->rot_standard_ref = NULL;
-}
-
-/** Test reading gyroscope sensor data */
-static void test_bmi_gyr_read(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- intv3_t ret_v;
- intv3_t exp_v;
- int16_t scale[3] = {MOTION_SENSE_DEFAULT_SCALE,
- MOTION_SENSE_DEFAULT_SCALE,
- MOTION_SENSE_DEFAULT_SCALE};
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Set offset 0 to simplify test */
- bmi_emul_set_off(emul, BMI_EMUL_GYR_X, 0);
- bmi_emul_set_off(emul, BMI_EMUL_GYR_Y, 0);
- bmi_emul_set_off(emul, BMI_EMUL_GYR_Z, 0);
-
- /* Fail on read status */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
-
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* When not ready, driver should return saved raw value */
- exp_v[0] = 100;
- exp_v[1] = 200;
- exp_v[2] = 300;
- ms->raw_xyz[0] = exp_v[0];
- ms->raw_xyz[1] = exp_v[1];
- ms->raw_xyz[2] = exp_v[2];
-
- /* Status not ready */
- bmi_emul_set_reg(emul, BMI260_STATUS, 0);
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- compare_int3v(exp_v, ret_v);
-
- /* Status only ACC ready */
- bmi_emul_set_reg(emul, BMI260_STATUS, BMI260_DRDY_ACC);
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- compare_int3v(exp_v, ret_v);
-
- /* Status GYR ready */
- bmi_emul_set_reg(emul, BMI260_STATUS, BMI260_DRDY_GYR);
-
- /* Set input accelerometer values */
- exp_v[0] = BMI_EMUL_125_DEG_S / 10;
- exp_v[1] = BMI_EMUL_125_DEG_S / 20;
- exp_v[2] = -(int)BMI_EMUL_125_DEG_S / 30;
- set_emul_gyr(emul, exp_v);
- /* Disable rotation */
- ms->rot_standard_ref = NULL;
- /* Set scale */
- zassert_equal(EC_SUCCESS, ms->drv->set_scale(ms, scale, 0), NULL);
- /* Set range to 125°/s */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 125, 0), NULL);
-
- /* Test read without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_gyr_to_emul(ret_v, 125, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Set range to 1000°/s */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 1000, 0), NULL);
-
- /* Test read without rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_gyr_to_emul(ret_v, 1000, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Setup rotation and rotate expected vector */
- ms->rot_standard_ref = &test_rotation;
- rotate_int3v_by_test_rotation(exp_v);
- /* Set range to 125°/s */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 125, 0), NULL);
-
- /* Test read with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_gyr_to_emul(ret_v, 125, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Set range to 1000°/s */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 1000, 0), NULL);
-
- /* Test read with rotation */
- zassert_equal(EC_SUCCESS, ms->drv->read(ms, ret_v), NULL);
- drv_gyr_to_emul(ret_v, 1000, ret_v);
- compare_int3v(exp_v, ret_v);
-
- /* Fail on read of data registers */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_X_L_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_X_H_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Y_L_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Y_H_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Z_L_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Z_H_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL);
-
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- ms->rot_standard_ref = NULL;
-}
-
-/** Test acceleromtere calibration */
-static void test_bmi_acc_perform_calib(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- intv3_t start_off;
- intv3_t exp_off;
- intv3_t ret_off;
- int range;
- int rate;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
-
- /* Range and rate cannot change after calibration */
- range = 4;
- rate = 50000;
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, range, 0), NULL);
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, rate, 0), NULL);
-
- /* Set offset 0 */
- start_off[0] = 0;
- start_off[1] = 0;
- start_off[2] = 0;
- set_emul_acc_offset(emul, start_off);
-
- /* Set input accelerometer values */
- exp_off[0] = BMI_EMUL_1G / 10;
- exp_off[1] = BMI_EMUL_1G / 20;
- exp_off[2] = BMI_EMUL_1G - (int)BMI_EMUL_1G / 30;
- set_emul_acc(emul, exp_off);
-
- /* Expected offset is [-X, -Y, 1G - Z] */
- exp_off[0] = -exp_off[0];
- exp_off[1] = -exp_off[1];
- exp_off[2] = BMI_EMUL_1G - exp_off[2];
-
- /* Test success on disabling calibration */
- zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 0), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- /* Test fail on rate read */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_CONF);
- zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- /* Test fail on status read */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS);
- zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- /* Test fail on data not ready */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- bmi_emul_set_reg(emul, BMI260_STATUS, 0);
- zassert_equal(EC_ERROR_TIMEOUT, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- /* Setup data status ready for rest of the test */
- bmi_emul_set_reg(emul, BMI260_STATUS, BMI260_DRDY_ACC);
-
- /* Test fail on data read */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_X_L_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- /* Test fail on setting offset */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_NV_CONF);
- zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test successful offset compenastion */
- zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
- get_emul_acc_offset(emul, ret_off);
- /*
- * Depending on used range, accelerometer values may be up to 6 bits
- * more accurate then offset value resolution.
- */
- compare_int3v_eps(exp_off, ret_off, 64);
-}
-
-/** Test gyroscope calibration */
-static void test_bmi_gyr_perform_calib(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- intv3_t start_off;
- intv3_t exp_off;
- intv3_t ret_off;
- int range;
- int rate;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Range and rate cannot change after calibration */
- range = 125;
- rate = 50000;
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, range, 0), NULL);
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, rate, 0), NULL);
-
- /* Set offset 0 */
- start_off[0] = 0;
- start_off[1] = 0;
- start_off[2] = 0;
- set_emul_gyr_offset(emul, start_off);
-
- /* Set input accelerometer values */
- exp_off[0] = BMI_EMUL_125_DEG_S / 100;
- exp_off[1] = BMI_EMUL_125_DEG_S / 200;
- exp_off[2] = -(int)BMI_EMUL_125_DEG_S / 300;
- set_emul_gyr(emul, exp_off);
-
- /* Expected offset is [-X, -Y, -Z] */
- exp_off[0] = -exp_off[0];
- exp_off[1] = -exp_off[1];
- exp_off[2] = -exp_off[2];
-
- /* Test success on disabling calibration */
- zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 0), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- /* Test fail on rate read */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_CONF);
- zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- /* Test fail on status read */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS);
- zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- /* Test fail on data not ready */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- bmi_emul_set_reg(emul, BMI260_STATUS, 0);
- zassert_equal(EC_ERROR_TIMEOUT, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- /*
- * Setup data status ready for rest of the test. Gyroscope calibration
- * should check DRDY_GYR bit, but current driver check only for ACC.
- */
- bmi_emul_set_reg(emul, BMI260_STATUS,
- BMI260_DRDY_ACC | BMI260_DRDY_GYR);
-
- /* Test fail on data read */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_X_L_G);
- zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- /* Test fail on setting offset */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_OFFSET_EN_GYR98);
- zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
-
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test successful offset compenastion */
- zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(range, ms->current_range, NULL);
- zassert_equal(rate, ms->drv->get_data_rate(ms), NULL);
- get_emul_gyr_offset(emul, ret_off);
- /*
- * Depending on used range, gyroscope values may be up to 4 bits
- * more accurate then offset value resolution.
- */
- compare_int3v_eps(exp_off, ret_off, 32);
-}
-
-/**
- * Custom emulatro read function which always return INIT OK status in
- * INTERNAL STATUS register. Used in init test.
- */
-static int emul_init_ok(struct i2c_emul *emul, int reg, uint8_t *val, int byte,
- void *data)
-{
- bmi_emul_set_reg(emul, BMI260_INTERNAL_STATUS, BMI260_INIT_OK);
-
- return 1;
-}
-
-/** Test init function of BMI260 accelerometer and gyroscope sensors */
-static void test_bmi_init(void)
-{
- struct motion_sensor_t *ms_acc, *ms_gyr;
- struct i2c_emul *emul;
-
- emul = bmi_emul_get(BMI_ORD);
- ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID];
- ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /*
- * Test successful init. It is needed custom function to set value of
- * BMI260_INTERNAL_STATUS register, because init function triggers reset
- * which clears value set in this register before test.
- */
- i2c_common_emul_set_read_func(emul, emul_init_ok, NULL);
- zassert_equal(EC_RES_SUCCESS, ms_acc->drv->init(ms_acc), NULL);
-
- zassert_equal(EC_RES_SUCCESS, ms_gyr->drv->init(ms_gyr), NULL);
-
- /* Remove custom emulator read function */
- i2c_common_emul_set_read_func(emul, NULL, NULL);
-}
-
-/** Data for custom emulator read function used in FIFO test */
-struct fifo_func_data {
- uint16_t interrupts;
-};
-
-/**
- * Custom emulator read function used in FIFO test. It sets interrupt registers
- * to value passed as additional data. It sets interrupt registers to 0 after
- * access.
- */
-static int emul_fifo_func(struct i2c_emul *emul, int reg, uint8_t *val,
- int byte, void *data)
-{
- struct fifo_func_data *d = data;
-
- if (reg + byte == BMI260_INT_STATUS_0) {
- bmi_emul_set_reg(emul, BMI260_INT_STATUS_0,
- d->interrupts & 0xff);
- d->interrupts &= 0xff00;
- } else if (reg + byte == BMI260_INT_STATUS_1) {
- bmi_emul_set_reg(emul, BMI260_INT_STATUS_1,
- (d->interrupts >> 8) & 0xff);
- d->interrupts &= 0xff;
- }
-
- return 1;
-}
-
-/**
- * Run irq handler on accelerometer sensor and check if committed data in FIFO
- * match what was set in FIFO frames in emulator.
- */
-static void check_fifo_f(struct motion_sensor_t *ms_acc,
- struct motion_sensor_t *ms_gyr,
- struct bmi_emul_frame *frame,
- int acc_range, int gyr_range,
- int line)
-{
- struct ec_response_motion_sensor_data vector;
- struct bmi_emul_frame *f_acc, *f_gyr;
- uint32_t event = BMI_INT_EVENT;
- uint16_t size;
- intv3_t exp_v;
- intv3_t ret_v;
-
- /* Find first frame of acc and gyr type */
- f_acc = frame;
- while (f_acc != NULL && !(f_acc->type & BMI_EMUL_FRAME_ACC)) {
- f_acc = f_acc->next;
- }
-
- f_gyr = frame;
- while (f_gyr != NULL && !(f_gyr->type & BMI_EMUL_FRAME_GYR)) {
- f_gyr = f_gyr->next;
- }
-
- /* Read FIFO in driver */
- zassert_equal(EC_SUCCESS, ms_acc->drv->irq_handler(ms_acc, &event),
- NULL);
-
- /* Read all data committed to FIFO */
- while (motion_sense_fifo_read(sizeof(vector), 1, &vector, &size)) {
- /* Ignore timestamp frames */
- if (vector.flags == MOTIONSENSE_SENSOR_FLAG_TIMESTAMP) {
- continue;
- }
-
- /* Check acclerometer frames */
- if (ms_acc - motion_sensors == vector.sensor_num) {
- if (f_acc == NULL) {
- zassert_unreachable(
- "Not expected acclerometer data in FIFO, line %d",
- line);
- }
-
- convert_int3v_int16(vector.data, ret_v);
- drv_acc_to_emul(ret_v, acc_range, ret_v);
- exp_v[0] = f_acc->acc_x;
- exp_v[1] = f_acc->acc_y;
- exp_v[2] = f_acc->acc_z;
- compare_int3v_f(exp_v, ret_v, V_EPS, line);
- f_acc = f_acc->next;
- }
-
- /* Check gyroscope frames */
- if (ms_gyr - motion_sensors == vector.sensor_num) {
- if (f_gyr == NULL) {
- zassert_unreachable(
- "Not expected gyroscope data in FIFO, line %d",
- line);
- }
-
- convert_int3v_int16(vector.data, ret_v);
- drv_gyr_to_emul(ret_v, gyr_range, ret_v);
- exp_v[0] = f_gyr->gyr_x;
- exp_v[1] = f_gyr->gyr_y;
- exp_v[2] = f_gyr->gyr_z;
- compare_int3v_f(exp_v, ret_v, V_EPS, line);
- f_gyr = f_gyr->next;
- }
- }
-
- /* Skip frames of different type at the end */
- while (f_acc != NULL && !(f_acc->type & BMI_EMUL_FRAME_ACC)) {
- f_acc = f_acc->next;
- }
-
- while (f_gyr != NULL && !(f_gyr->type & BMI_EMUL_FRAME_GYR)) {
- f_gyr = f_gyr->next;
- }
-
- /* All frames are readed */
- zassert_is_null(f_acc, "Not all accelerometer frames are read, line %d",
- line);
- zassert_is_null(f_gyr, "Not all gyroscope frames are read, line %d",
- line);
-}
-#define check_fifo(ms_acc, ms_gyr, frame, acc_range, gyr_range) \
- check_fifo_f(ms_acc, ms_gyr, frame, acc_range, gyr_range, __LINE__)
-
-/** Test irq handler of accelerometer sensor */
-static void test_bmi_acc_fifo(void)
-{
- struct motion_sensor_t *ms, *ms_gyr;
- struct fifo_func_data func_data;
- struct bmi_emul_frame f[3];
- struct i2c_emul *emul;
- int gyr_range = 125;
- int acc_range = 2;
- int event;
-
- emul = bmi_emul_get(BMI_ORD);
- ms = &motion_sensors[BMI_ACC_SENSOR_ID];
- ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Need to be set to collect all data in FIFO */
- ms->oversampling_ratio = 1;
- ms_gyr->oversampling_ratio = 1;
- /* Only BMI event should be handled */
- event = 0x1234 & ~BMI_INT_EVENT;
- zassert_equal(EC_ERROR_NOT_HANDLED, ms->drv->irq_handler(ms, &event),
- NULL);
-
- event = BMI_INT_EVENT;
-
- /* Test fail to read interrupt status registers */
- i2c_common_emul_set_read_fail_reg(emul, BMI260_INT_STATUS_0);
- zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL);
- i2c_common_emul_set_read_fail_reg(emul, BMI260_INT_STATUS_1);
- zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test no interrupt */
- bmi_emul_set_reg(emul, BMI260_INT_STATUS_0, 0);
- bmi_emul_set_reg(emul, BMI260_INT_STATUS_1, 0);
-
- /* Trigger irq handler and check results */
- check_fifo(ms, ms_gyr, NULL, acc_range, gyr_range);
-
- /* Set custom function for FIFO test */
- i2c_common_emul_set_read_func(emul, emul_fifo_func, &func_data);
- /* Enable sensor FIFO */
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 50000, 0), NULL);
- /* Set range */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, acc_range, 0), NULL);
- zassert_equal(EC_SUCCESS, ms_gyr->drv->set_range(ms_gyr, gyr_range, 0),
- NULL);
- /* Setup single accelerometer frame */
- f[0].type = BMI_EMUL_FRAME_ACC;
- f[0].acc_x = BMI_EMUL_1G / 10;
- f[0].acc_y = BMI_EMUL_1G / 20;
- f[0].acc_z = -(int)BMI_EMUL_1G / 30;
- f[0].next = NULL;
- bmi_emul_append_frame(emul, f);
- /* Setup interrupts register */
- func_data.interrupts = BMI260_FWM_INT;
-
- /* Trigger irq handler and check results */
- check_fifo(ms, ms_gyr, f, acc_range, gyr_range);
-
- /* Setup second accelerometer frame */
- f[1].type = BMI_EMUL_FRAME_ACC;
- f[1].acc_x = -(int)BMI_EMUL_1G / 40;
- f[1].acc_y = BMI_EMUL_1G / 50;
- f[1].acc_z = BMI_EMUL_1G / 60;
- f[0].next = &(f[1]);
- f[1].next = NULL;
- bmi_emul_append_frame(emul, f);
- /* Setup interrupts register */
- func_data.interrupts = BMI260_FWM_INT;
-
- /* Trigger irq handler and check results */
- check_fifo(ms, ms_gyr, f, acc_range, gyr_range);
-
- /* Enable sensor FIFO */
- zassert_equal(EC_SUCCESS, ms_gyr->drv->set_data_rate(ms_gyr, 50000, 0),
- NULL);
-
- /* Setup first gyroscope frame (after two accelerometer frames) */
- f[2].type = BMI_EMUL_FRAME_GYR;
- f[2].gyr_x = -(int)BMI_EMUL_125_DEG_S / 100;
- f[2].gyr_y = BMI_EMUL_125_DEG_S / 200;
- f[2].gyr_z = BMI_EMUL_125_DEG_S / 300;
- f[1].next = &(f[2]);
- f[2].next = NULL;
- bmi_emul_append_frame(emul, f);
- /* Setup interrupts register */
- func_data.interrupts = BMI260_FWM_INT;
-
- /* Trigger irq handler and check results */
- check_fifo(ms, ms_gyr, f, acc_range, gyr_range);
-
- /* Setup second accelerometer frame to by gyroscope frame too */
- f[1].type |= BMI_EMUL_FRAME_GYR;
- f[1].gyr_x = -(int)BMI_EMUL_125_DEG_S / 300;
- f[1].gyr_y = BMI_EMUL_125_DEG_S / 400;
- f[1].gyr_z = BMI_EMUL_125_DEG_S / 500;
- bmi_emul_append_frame(emul, f);
- /* Setup interrupts register */
- func_data.interrupts = BMI260_FWM_INT;
-
- /* Trigger irq handler and check results */
- check_fifo(ms, ms_gyr, f, acc_range, gyr_range);
-
- /* Skip frame should be ignored by driver */
- bmi_emul_set_skipped_frames(emul, 8);
- bmi_emul_append_frame(emul, f);
- /* Setup interrupts register */
- func_data.interrupts = BMI260_FWM_INT;
-
- /* Trigger irq handler and check results */
- check_fifo(ms, ms_gyr, f, acc_range, gyr_range);
-
- /* Setup second frame as an config frame */
- f[1].type = BMI_EMUL_FRAME_CONFIG;
- /* Indicate that accelerometer range changed */
- f[1].config = 0x1;
- bmi_emul_append_frame(emul, f);
- /* Setup interrupts register */
- func_data.interrupts = BMI260_FWM_INT;
-
- /* Trigger irq handler and check results */
- check_fifo(ms, ms_gyr, f, acc_range, gyr_range);
-
- /* Remove custom emulator read function */
- i2c_common_emul_set_read_func(emul, NULL, NULL);
-}
-
-/** Test irq handler of gyroscope sensor */
-static void test_bmi_gyr_fifo(void)
-{
- struct motion_sensor_t *ms;
- uint32_t event;
-
- ms = &motion_sensors[BMI_GYR_SENSOR_ID];
-
- /* Interrupt shuldn't be triggered for gyroscope motion sense */
- event = BMI_INT_EVENT;
- zassert_equal(EC_ERROR_NOT_HANDLED, ms->drv->irq_handler(ms, &event),
- NULL);
-}
-
-void test_suite_bmi260(void)
-{
- ztest_test_suite(bmi260,
- ztest_user_unit_test(test_bmi_acc_get_offset),
- ztest_user_unit_test(test_bmi_gyr_get_offset),
- ztest_user_unit_test(test_bmi_acc_set_offset),
- ztest_user_unit_test(test_bmi_gyr_set_offset),
- ztest_user_unit_test(test_bmi_acc_set_range),
- ztest_user_unit_test(test_bmi_gyr_set_range),
- ztest_user_unit_test(test_bmi_get_resolution),
- ztest_user_unit_test(test_bmi_acc_rate),
- ztest_user_unit_test(test_bmi_gyr_rate),
- ztest_user_unit_test(test_bmi_scale),
- ztest_user_unit_test(test_bmi_read_temp),
- ztest_user_unit_test(test_bmi_acc_read),
- ztest_user_unit_test(test_bmi_gyr_read),
- ztest_user_unit_test(test_bmi_acc_perform_calib),
- ztest_user_unit_test(test_bmi_gyr_perform_calib),
- ztest_user_unit_test(test_bmi_init),
- ztest_user_unit_test(test_bmi_acc_fifo),
- ztest_user_unit_test(test_bmi_gyr_fifo));
- ztest_run_test_suite(bmi260);
-}
diff --git a/zephyr/test/drivers/src/espi.c b/zephyr/test/drivers/src/espi.c
deleted file mode 100644
index c852f1b771..0000000000
--- a/zephyr/test/drivers/src/espi.c
+++ /dev/null
@@ -1,35 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <zephyr.h>
-#include <ztest.h>
-
-#include "ec_commands.h"
-#include "host_command.h"
-
-static void test_host_command_get_protocol_info(void)
-{
- struct ec_response_get_protocol_info response;
- struct host_cmd_handler_args args =
- BUILD_HOST_COMMAND(EC_CMD_GET_PROTOCOL_INFO, 0, response);
-
- zassert_ok(host_command_process(&args), NULL);
- zassert_ok(args.result, NULL);
- zassert_equal(args.response_size, sizeof(response), NULL);
- zassert_equal(response.protocol_versions, BIT(3), NULL);
- zassert_equal(response.max_request_packet_size, EC_LPC_HOST_PACKET_SIZE,
- NULL);
- zassert_equal(response.max_response_packet_size,
- EC_LPC_HOST_PACKET_SIZE, NULL);
- zassert_equal(response.flags, 0, NULL);
-}
-
-void test_suite_espi(void)
-{
- ztest_test_suite(espi,
- ztest_user_unit_test(
- test_host_command_get_protocol_info));
- ztest_run_test_suite(espi);
-}
diff --git a/zephyr/test/drivers/src/lis2dw12.c b/zephyr/test/drivers/src/lis2dw12.c
deleted file mode 100644
index 287430e65b..0000000000
--- a/zephyr/test/drivers/src/lis2dw12.c
+++ /dev/null
@@ -1,119 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <ztest.h>
-#include <drivers/emul.h>
-#include "driver/accel_lis2dw12.h"
-#include "emul/emul_common_i2c.h"
-#include "emul/emul_lis2dw12.h"
-
-#define LIS2DW12_NODELABEL DT_NODELABEL(ms_lis2dw12_accel)
-#define LIS2DW12_SENSOR_ID SENSOR_ID(LIS2DW12_NODELABEL)
-#define EMUL_LABEL DT_LABEL(DT_NODELABEL(lis2dw12_emul))
-
-#include <stdio.h>
-static void lis2dw12_setup(void)
-{
- lis2dw12_emul_reset(emul_get_binding(EMUL_LABEL));
-}
-
-static void test_lis2dw12_init__fail_read_who_am_i(void)
-{
- const struct emul *emul = emul_get_binding(EMUL_LABEL);
- struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID];
- int rv;
-
- i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul),
- LIS2DW12_WHO_AM_I_REG);
- rv = ms->drv->init(ms);
- zassert_equal(EC_ERROR_INVAL, rv, NULL);
-}
-
-static void test_lis2dw12_init__fail_who_am_i(void)
-{
- const struct emul *emul = emul_get_binding(EMUL_LABEL);
- struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID];
- int rv;
-
- lis2dw12_emul_set_who_am_i(emul, ~LIS2DW12_WHO_AM_I);
-
- rv = ms->drv->init(ms);
- zassert_equal(EC_ERROR_ACCESS_DENIED, rv,
- "init returned %d but was expecting %d", rv,
- EC_ERROR_ACCESS_DENIED);
-}
-
-static void test_lis2dw12_init__fail_write_soft_reset(void)
-{
- const struct emul *emul = emul_get_binding(EMUL_LABEL);
- struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID];
- int rv;
-
- i2c_common_emul_set_write_fail_reg(lis2dw12_emul_to_i2c_emul(emul),
- LIS2DW12_SOFT_RESET_ADDR);
- rv = ms->drv->init(ms);
- zassert_equal(EC_ERROR_INVAL, rv, NULL);
-}
-
-static void test_lis2dw12_init__timeout_read_soft_reset(void)
-{
- const struct emul *emul = emul_get_binding(EMUL_LABEL);
- struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID];
- int rv;
-
- i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul),
- LIS2DW12_SOFT_RESET_ADDR);
- rv = ms->drv->init(ms);
- zassert_equal(EC_ERROR_TIMEOUT, rv, "init returned %d but expected %d",
- rv, EC_ERROR_TIMEOUT);
-}
-
-static int lis2dw12_test_mock_write_fail_set_bdu(struct i2c_emul *emul, int reg,
- uint8_t val, int bytes,
- void *data)
-{
- if (reg == LIS2DW12_BDU_ADDR && bytes == 1 &&
- (val & LIS2DW12_BDU_MASK) != 0) {
- return -EIO;
- }
- return 1;
-}
-
-static void test_lis2dw12_init__fail_set_bdu(void)
-{
- const struct emul *emul = emul_get_binding(EMUL_LABEL);
- struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID];
- int rv;
-
- i2c_common_emul_set_write_func(lis2dw12_emul_to_i2c_emul(emul),
- lis2dw12_test_mock_write_fail_set_bdu,
- NULL);
- rv = ms->drv->init(ms);
- zassert_equal(EC_ERROR_INVAL, rv, "init returned %d but expected %d",
- rv, EC_ERROR_INVAL);
- zassert_true(lis2dw12_emul_get_soft_reset_count(emul) > 0,
- "expected at least one soft reset");
-}
-
-void test_suite_lis2dw12(void)
-{
- ztest_test_suite(lis2dw12,
- ztest_unit_test_setup_teardown(
- test_lis2dw12_init__fail_read_who_am_i,
- lis2dw12_setup, unit_test_noop),
- ztest_unit_test_setup_teardown(
- test_lis2dw12_init__fail_who_am_i,
- lis2dw12_setup, unit_test_noop),
- ztest_unit_test_setup_teardown(
- test_lis2dw12_init__fail_write_soft_reset,
- lis2dw12_setup, unit_test_noop),
- ztest_unit_test_setup_teardown(
- test_lis2dw12_init__timeout_read_soft_reset,
- lis2dw12_setup, unit_test_noop),
- ztest_unit_test_setup_teardown(
- test_lis2dw12_init__fail_set_bdu,
- lis2dw12_setup, unit_test_noop));
- ztest_run_test_suite(lis2dw12);
-}
diff --git a/zephyr/test/drivers/src/ln9310.c b/zephyr/test/drivers/src/ln9310.c
deleted file mode 100644
index 0033931039..0000000000
--- a/zephyr/test/drivers/src/ln9310.c
+++ /dev/null
@@ -1,50 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <ztest.h>
-#include <drivers/emul.h>
-#include "driver/ln9310.h"
-#include "emul/emul_ln9310.h"
-
-void test_ln9310_2s_no_startup__passes_init(void)
-{
- const struct emul *emulator =
- emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310)));
-
- zassert_not_null(emulator, NULL);
-
- ln9310_emul_set_context(emulator);
- ln9310_emul_reset(emulator);
- ln9310_emul_set_battery_cell_type(emulator, BATTERY_CELL_TYPE_2S);
- ln9310_emul_set_version(emulator, LN9310_BC_STS_C_CHIP_REV_FIXED);
-
- zassert_ok(ln9310_init(), NULL);
- zassert_true(ln9310_emul_is_init(emulator), NULL);
-}
-
-void test_ln9310_3s_no_startup__passes_init(void)
-{
- const struct emul *emulator =
- emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310)));
-
- zassert_not_null(emulator, NULL);
-
- ln9310_emul_set_context(emulator);
- ln9310_emul_reset(emulator);
- ln9310_emul_set_battery_cell_type(emulator, BATTERY_CELL_TYPE_3S);
- ln9310_emul_set_version(emulator, LN9310_BC_STS_C_CHIP_REV_FIXED);
-
- zassert_ok(ln9310_init(), NULL);
- zassert_true(ln9310_emul_is_init(emulator), NULL);
-}
-
-void test_suite_ln9310(void)
-{
- ztest_test_suite(
- ln9310,
- ztest_unit_test(test_ln9310_2s_no_startup__passes_init),
- ztest_unit_test(test_ln9310_3s_no_startup__passes_init));
- ztest_run_test_suite(ln9310);
-}
diff --git a/zephyr/test/drivers/src/main.c b/zephyr/test/drivers/src/main.c
deleted file mode 100644
index 8d40bc4373..0000000000
--- a/zephyr/test/drivers/src/main.c
+++ /dev/null
@@ -1,50 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <zephyr.h>
-#include <ztest.h>
-#include "ec_app_main.h"
-
-extern void test_suite_battery(void);
-extern void test_suite_cbi(void);
-extern void test_suite_smart_battery(void);
-extern void test_suite_thermistor(void);
-extern void test_suite_temp_sensor(void);
-extern void test_suite_bma2x2(void);
-extern void test_suite_bc12(void);
-extern void test_suite_ppc(void);
-extern void test_suite_bmi260(void);
-extern void test_suite_bmi160(void);
-extern void test_suite_tcs3400(void);
-extern void test_suite_espi(void);
-extern void test_suite_bb_retimer(void);
-extern void test_suite_ln9310(void);
-extern void test_suite_lis2dw12(void);
-extern void test_suite_stm_mems_common(void);
-
-void test_main(void)
-{
- /* Test suites to run before ec_app_main.*/
-
- ec_app_main();
-
- /* Test suites to run after ec_app_main.*/
- test_suite_battery();
- test_suite_cbi();
- test_suite_smart_battery();
- test_suite_thermistor();
- test_suite_temp_sensor();
- test_suite_bma2x2();
- test_suite_bc12();
- test_suite_ppc();
- test_suite_bmi260();
- test_suite_bmi160();
- test_suite_tcs3400();
- test_suite_espi();
- test_suite_bb_retimer();
- test_suite_ln9310();
- test_suite_lis2dw12();
- test_suite_stm_mems_common();
-}
diff --git a/zephyr/test/drivers/src/ppc.c b/zephyr/test/drivers/src/ppc.c
deleted file mode 100644
index dced25c227..0000000000
--- a/zephyr/test/drivers/src/ppc.c
+++ /dev/null
@@ -1,84 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <zephyr.h>
-#include <ztest.h>
-#include <ztest_assert.h>
-
-#include "emul/emul_syv682x.h"
-
-#include "stubs.h"
-#include "syv682x.h"
-#include "timer.h"
-#include "usbc_ppc.h"
-
-#define SYV682X_ORD DT_DEP_ORD(DT_NODELABEL(syv682x_emul))
-
-static const int syv682x_port = 1;
-
-static void test_ppc_syv682x_vbus_enable(void)
-{
- struct i2c_emul *emul = syv682x_emul_get(SYV682X_ORD);
- uint8_t reg;
-
- zassert_ok(syv682x_emul_get_reg(emul, SYV682X_CONTROL_1_REG, &reg),
- "Reading CONTROL_1 failed");
- zassert_equal(reg & SYV682X_CONTROL_1_PWR_ENB,
- SYV682X_CONTROL_1_PWR_ENB, "VBUS sourcing disabled");
- zassert_false(ppc_is_sourcing_vbus(syv682x_port),
- "PPC sourcing VBUS at beginning of test");
-
- zassert_ok(ppc_vbus_source_enable(syv682x_port, true),
- "VBUS enable failed");
- zassert_ok(syv682x_emul_get_reg(emul, SYV682X_CONTROL_1_REG, &reg),
- "Reading CONTROL_1 failed");
- zassert_equal(reg & SYV682X_CONTROL_1_PWR_ENB, 0,
- "VBUS sourcing disabled");
- zassert_true(ppc_is_sourcing_vbus(syv682x_port),
- "PPC is not sourcing VBUS after VBUS enabled");
-}
-
-static void test_ppc_syv682x_interrupt(void)
-{
- struct i2c_emul *emul = syv682x_emul_get(SYV682X_ORD);
-
- syv682x_emul_set_status(emul, SYV682X_STATUS_OC_5V);
- syv682x_interrupt(syv682x_port);
-
- /* An OC event less than 100 ms should not cause VBUS to turn off. */
- msleep(50);
- syv682x_interrupt(syv682x_port);
- zassert_true(ppc_is_sourcing_vbus(syv682x_port),
- "PPC is not sourcing VBUS after 50 ms OC");
- /* But one greater than 100 ms should. */
- msleep(60);
- syv682x_interrupt(syv682x_port);
- zassert_false(ppc_is_sourcing_vbus(syv682x_port),
- "PPC is sourcing VBUS after 100 ms OC");
-
- syv682x_emul_set_status(emul, 0x0);
- /*
- * TODO(b/190519131): Organize the tests to be more hermetic and avoid
- * the following issue: The driver triggers overcurrent protection. If
- * overcurrent protection is triggered 3 times, the TC won't turn the
- * port back on without a detach. This could frustrate efforts to test
- * the TC.
- */
-}
-
-static void test_ppc_syv682x(void)
-{
- zassert_ok(ppc_init(syv682x_port), "PPC init failed");
-
- test_ppc_syv682x_vbus_enable();
- test_ppc_syv682x_interrupt();
-}
-
-void test_suite_ppc(void)
-{
- ztest_test_suite(ppc,
- ztest_user_unit_test(test_ppc_syv682x));
- ztest_run_test_suite(ppc);
-}
diff --git a/zephyr/test/drivers/src/smart.c b/zephyr/test/drivers/src/smart.c
deleted file mode 100644
index 7c053f1c23..0000000000
--- a/zephyr/test/drivers/src/smart.c
+++ /dev/null
@@ -1,281 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <zephyr.h>
-#include <ztest.h>
-
-#include "common.h"
-#include "i2c.h"
-#include "emul/emul_common_i2c.h"
-#include "emul/emul_smart_battery.h"
-
-#include "battery.h"
-#include "battery_smart.h"
-
-#define BATTERY_ORD DT_DEP_ORD(DT_NODELABEL(battery))
-
-/** Test all simple getters */
-static void test_battery_getters(void)
-{
- struct sbat_emul_bat_data *bat;
- struct i2c_emul *emul;
- char block[32];
- int expected;
- int word;
-
- emul = sbat_emul_get_ptr(BATTERY_ORD);
- bat = sbat_emul_get_bat_data(emul);
-
- zassert_equal(EC_SUCCESS, battery_get_mode(&word), NULL);
- zassert_equal(bat->mode, word, "%d != %d", bat->mode, word);
-
- expected = 100 * bat->cap / bat->design_cap;
- zassert_equal(EC_SUCCESS, battery_state_of_charge_abs(&word), NULL);
- zassert_equal(expected, word, "%d != %d", expected, word);
-
- zassert_equal(EC_SUCCESS, battery_remaining_capacity(&word), NULL);
- zassert_equal(bat->cap, word, "%d != %d", bat->cap, word);
- zassert_equal(EC_SUCCESS, battery_full_charge_capacity(&word), NULL);
- zassert_equal(bat->full_cap, word, "%d != %d", bat->full_cap, word);
- zassert_equal(EC_SUCCESS, battery_cycle_count(&word), NULL);
- zassert_equal(bat->cycle_count, word, "%d != %d",
- bat->cycle_count, word);
- zassert_equal(EC_SUCCESS, battery_design_capacity(&word), NULL);
- zassert_equal(bat->design_cap, word, "%d != %d", bat->design_cap, word);
- zassert_equal(EC_SUCCESS, battery_design_voltage(&word), NULL);
- zassert_equal(bat->design_mv, word, "%d != %d", bat->design_mv, word);
- zassert_equal(EC_SUCCESS, battery_serial_number(&word), NULL);
- zassert_equal(bat->sn, word, "%d != %d", bat->sn, word);
- zassert_equal(EC_SUCCESS, get_battery_manufacturer_name(block, 32),
- NULL);
- zassert_mem_equal(block, bat->mf_name, bat->mf_name_len,
- "%s != %s", block, bat->mf_name);
- zassert_equal(EC_SUCCESS, battery_device_name(block, 32), NULL);
- zassert_mem_equal(block, bat->dev_name, bat->dev_name_len,
- "%s != %s", block, bat->dev_name);
- zassert_equal(EC_SUCCESS, battery_device_chemistry(block, 32), NULL);
- zassert_mem_equal(block, bat->dev_chem, bat->dev_chem_len,
- "%s != %s", block, bat->dev_chem);
- word = battery_get_avg_current();
- zassert_equal(bat->avg_cur, word, "%d != %d", bat->avg_cur, word);
-
- bat->avg_cur = 200;
- expected = (bat->full_cap - bat->cap) * 60 / bat->avg_cur;
- zassert_equal(EC_SUCCESS, battery_time_to_full(&word), NULL);
- zassert_equal(expected, word, "%d != %d", expected, word);
-
- bat->cur = -200;
- expected = bat->cap * 60 / (-bat->cur);
- zassert_equal(EC_SUCCESS, battery_run_time_to_empty(&word), NULL);
- zassert_equal(expected, word, "%d != %d", expected, word);
-
- bat->avg_cur = -200;
- expected = bat->cap * 60 / (-bat->avg_cur);
- zassert_equal(EC_SUCCESS, battery_time_to_empty(&word), NULL);
- zassert_equal(expected, word, "%d != %d", expected, word);
-}
-
-/** Test battery status */
-static void test_battery_status(void)
-{
- struct sbat_emul_bat_data *bat;
- struct i2c_emul *emul;
- int expected;
- int status;
-
- emul = sbat_emul_get_ptr(BATTERY_ORD);
- bat = sbat_emul_get_bat_data(emul);
-
- bat->status = 0;
- bat->cur = -200;
- bat->cap_alarm = 0;
- bat->time_alarm = 0;
- bat->cap = bat->full_cap / 2;
- bat->error_code = STATUS_CODE_OVERUNDERFLOW;
-
- expected = 0;
- expected |= STATUS_DISCHARGING;
- expected |= STATUS_CODE_OVERUNDERFLOW;
-
- zassert_equal(EC_SUCCESS, battery_status(&status), NULL);
- zassert_equal(expected, status, "%d != %d", expected, status);
-}
-
-/** Test wait for stable function */
-static void test_battery_wait_for_stable(void)
-{
- struct i2c_emul *emul;
-
- emul = sbat_emul_get_ptr(BATTERY_ORD);
-
- /* Should fail when read function always fail */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_FAIL_ALL_REG);
- zassert_equal(EC_ERROR_NOT_POWERED, battery_wait_for_stable(), NULL);
-
- /* Should be ok with default handler */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- zassert_equal(EC_SUCCESS, battery_wait_for_stable(), NULL);
-}
-
-/** Test manufacture date */
-static void test_battery_manufacture_date(void)
-{
- struct sbat_emul_bat_data *bat;
- struct i2c_emul *emul;
- int day, month, year;
- int exp_month = 5;
- int exp_year = 2018;
- int exp_day = 19;
- uint16_t date;
-
- emul = sbat_emul_get_ptr(BATTERY_ORD);
- bat = sbat_emul_get_bat_data(emul);
-
- date = sbat_emul_date_to_word(exp_day, exp_month, exp_year);
- bat->mf_date = date;
-
- zassert_equal(EC_SUCCESS, battery_manufacture_date(&year, &month, &day),
- NULL);
- zassert_equal(exp_day, day, "%d != %d", exp_day, day);
- zassert_equal(exp_month, month, "%d != %d", exp_month, month);
- zassert_equal(exp_year, year, "%d != %d", exp_year, year);
-}
-
-/** Test time at rate */
-static void test_battery_time_at_rate(void)
-{
- struct sbat_emul_bat_data *bat;
- struct i2c_emul *emul;
- int expect_time;
- int minutes;
- int rate;
-
- emul = sbat_emul_get_ptr(BATTERY_ORD);
- bat = sbat_emul_get_bat_data(emul);
-
- /* 3000mAh at rate 300mA will be discharged in 10h */
- bat->cap = 3000;
- rate = -300;
- expect_time = 600;
-
- zassert_equal(EC_SUCCESS, battery_time_at_rate(rate, &minutes), NULL);
- zassert_equal(expect_time, minutes, "%d != %d", expect_time, minutes);
-
- /* 1000mAh at rate 1000mA will be charged in 1h */
- bat->cap = bat->full_cap - 1000;
- rate = 1000;
- /* battery_time_at_rate report time to full as negative number */
- expect_time = -60;
-
- zassert_equal(EC_SUCCESS, battery_time_at_rate(rate, &minutes), NULL);
- zassert_equal(expect_time, minutes, "%d != %d", expect_time, minutes);
-}
-
-/** Test battery get params */
-static void test_battery_get_params(void)
-{
- struct sbat_emul_bat_data *bat;
- struct batt_params batt;
- struct i2c_emul *emul;
- int flags;
-
- emul = sbat_emul_get_ptr(BATTERY_ORD);
- bat = sbat_emul_get_bat_data(emul);
-
- /* Battery wants to charge */
- bat->desired_charg_cur = 1000;
- bat->desired_charg_volt = 5000;
-
- /* Fail temperature read */
- i2c_common_emul_set_read_fail_reg(emul, SB_TEMPERATURE);
- flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE |
- BATT_FLAG_BAD_TEMPERATURE;
- battery_get_params(&batt);
- zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags);
-
- /* Fail state of charge read; want charge cannot be set */
- i2c_common_emul_set_read_fail_reg(emul, SB_RELATIVE_STATE_OF_CHARGE);
- flags = BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_STATE_OF_CHARGE;
- battery_get_params(&batt);
- zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags);
-
- /* Fail voltage read */
- i2c_common_emul_set_read_fail_reg(emul, SB_VOLTAGE);
- flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE |
- BATT_FLAG_BAD_VOLTAGE;
- battery_get_params(&batt);
- zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags);
-
- /* Fail current read */
- i2c_common_emul_set_read_fail_reg(emul, SB_CURRENT);
- flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE |
- BATT_FLAG_BAD_CURRENT;
- battery_get_params(&batt);
- zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags);
-
- /* Fail average current read */
- i2c_common_emul_set_read_fail_reg(emul, SB_AVERAGE_CURRENT);
- flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE |
- BATT_FLAG_BAD_AVERAGE_CURRENT;
- battery_get_params(&batt);
- zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags);
-
- /* Fail charging voltage read; want charge cannot be set */
- i2c_common_emul_set_read_fail_reg(emul, SB_CHARGING_VOLTAGE);
- flags = BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_DESIRED_VOLTAGE;
- battery_get_params(&batt);
- zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags);
-
- /* Fail charging voltage read; want charge cannot be set */
- i2c_common_emul_set_read_fail_reg(emul, SB_CHARGING_CURRENT);
- flags = BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_DESIRED_CURRENT;
- battery_get_params(&batt);
- zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags);
-
- /* Fail remaining capacity read */
- i2c_common_emul_set_read_fail_reg(emul, SB_REMAINING_CAPACITY);
- flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE |
- BATT_FLAG_BAD_REMAINING_CAPACITY;
- battery_get_params(&batt);
- zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags);
-
- /* Fail full capacity read */
- i2c_common_emul_set_read_fail_reg(emul, SB_FULL_CHARGE_CAPACITY);
- flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE |
- BATT_FLAG_BAD_FULL_CAPACITY;
- battery_get_params(&batt);
- zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags);
-
- /* Fail status read */
- i2c_common_emul_set_read_fail_reg(emul, SB_BATTERY_STATUS);
- flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE |
- BATT_FLAG_BAD_STATUS;
- battery_get_params(&batt);
- zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags);
-
- /* Fail all */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_FAIL_ALL_REG);
- flags = BATT_FLAG_BAD_ANY;
- battery_get_params(&batt);
- zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags);
-
- /* Use default handler, everything should be ok */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE;
- battery_get_params(&batt);
- zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags);
-}
-
-void test_suite_smart_battery(void)
-{
- ztest_test_suite(smart_battery,
- ztest_user_unit_test(test_battery_getters),
- ztest_user_unit_test(test_battery_status),
- ztest_user_unit_test(test_battery_wait_for_stable),
- ztest_user_unit_test(test_battery_manufacture_date),
- ztest_user_unit_test(test_battery_time_at_rate),
- ztest_user_unit_test(test_battery_get_params));
- ztest_run_test_suite(smart_battery);
-}
diff --git a/zephyr/test/drivers/src/stm_mems_common.c b/zephyr/test/drivers/src/stm_mems_common.c
deleted file mode 100644
index 3085007f4d..0000000000
--- a/zephyr/test/drivers/src/stm_mems_common.c
+++ /dev/null
@@ -1,56 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <ztest.h>
-#include <device.h>
-#include <devicetree.h>
-#include <errno.h>
-
-#include "common.h"
-#include "driver/stm_mems_common.h"
-#include "emul/emul_common_i2c.h"
-#include "emul/i2c_mock.h"
-#include "i2c/i2c.h"
-
-#define MOCK_EMUL emul_get_binding(DT_LABEL(DT_NODELABEL(i2c_mock)))
-
-static void setup(void)
-{
- i2c_mock_reset(MOCK_EMUL);
-}
-
-static int mock_read_fn(struct i2c_emul *emul, int reg, uint8_t *val, int bytes,
- void *data)
-{
- ztest_check_expected_value(reg);
- ztest_check_expected_value(bytes);
- return ztest_get_return_value();
-}
-
-static void test_st_raw_read_n(void)
-{
- const struct emul *emul = MOCK_EMUL;
- struct i2c_emul *i2c_emul = i2c_mock_to_i2c_emul(emul);
- int rv;
-
- i2c_common_emul_set_read_func(i2c_emul, mock_read_fn, NULL);
- ztest_expect_value(mock_read_fn, reg, 0x80);
- ztest_expect_value(mock_read_fn, bytes, 0);
- ztest_returns_value(mock_read_fn, -EIO);
-
- rv = st_raw_read_n(I2C_PORT_POWER, i2c_mock_get_addr(emul), 0, NULL, 2);
- /* The shim layer translates -EIO to EC_ERROR_INVAL. */
- zassert_equal(rv, EC_ERROR_INVAL, "rv was %d but expected %d", rv,
- EC_ERROR_INVAL);
-}
-
-void test_suite_stm_mems_common(void)
-{
- ztest_test_suite(stm_mems_common,
- ztest_unit_test_setup_teardown(
- test_st_raw_read_n,
- setup, unit_test_noop));
- ztest_run_test_suite(stm_mems_common);
-}
diff --git a/zephyr/test/drivers/src/stubs.c b/zephyr/test/drivers/src/stubs.c
deleted file mode 100644
index 6dab320b9a..0000000000
--- a/zephyr/test/drivers/src/stubs.c
+++ /dev/null
@@ -1,233 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "battery.h"
-#include "battery_fuel_gauge.h"
-#include "bc12/pi3usb9201_public.h"
-#include "charge_ramp.h"
-#include "charger.h"
-#include "charger/isl9241_public.h"
-#include "config.h"
-#include "i2c/i2c.h"
-#include "power.h"
-#include "ppc/sn5s330_public.h"
-#include "ppc/syv682x_public.h"
-#include "retimer/bb_retimer_public.h"
-#include "stubs.h"
-#include "tcpm/tusb422_public.h"
-#include "tcpm/tusb422_public.h"
-#include "usb_mux.h"
-#include "usb_pd_tcpm.h"
-#include "usbc_ppc.h"
-
-/* All of these definitions are just to get the test to link. None of these
- * functions are useful or behave as they should. Please remove them once the
- * real code is able to be added. Most of the things here should either be
- * in emulators or in the native_posix board-specific code or part of the
- * device tree.
- */
-
-/* BC1.2 charger detect configuration */
-const struct pi3usb9201_config_t pi3usb9201_bc12_chips[] = {
- [USBC_PORT_C0] = {
- .i2c_port = I2C_PORT_USB_C0,
- .i2c_addr_flags = PI3USB9201_I2C_ADDR_3_FLAGS,
- },
- [USBC_PORT_C1] = {
- .i2c_port = I2C_PORT_USB_C1,
- .i2c_addr_flags = PI3USB9201_I2C_ADDR_1_FLAGS,
- },
-};
-BUILD_ASSERT(ARRAY_SIZE(pi3usb9201_bc12_chips) == USBC_PORT_COUNT);
-
-/* Charger Chip Configuration */
-const struct charger_config_t chg_chips[] = {
- {
- .i2c_port = I2C_PORT_CHARGER,
- .i2c_addr_flags = ISL9241_ADDR_FLAGS,
- .drv = &isl9241_drv,
- },
-};
-
-const struct board_batt_params board_battery_info[] = {
- /* LGC\011 L17L3PB0 Battery Information */
- /*
- * Battery info provided by ODM on b/143477210, comment #11
- */
- [BATTERY_LGC011] = {
- .fuel_gauge = {
- .manuf_name = "LGC",
- .ship_mode = {
- .reg_addr = 0x00,
- .reg_data = { 0x10, 0x10 },
- },
- .fet = {
- .reg_addr = 0x0,
- .reg_mask = 0x6000,
- .disconnect_val = 0x6000,
- }
- },
- .batt_info = {
- .voltage_max = TARGET_WITH_MARGIN(13200, 5),
- .voltage_normal = 11550, /* mV */
- .voltage_min = 9000, /* mV */
- .precharge_current = 256, /* mA */
- .start_charging_min_c = 0,
- .start_charging_max_c = 45,
- .charging_min_c = 0,
- .charging_max_c = 60,
- .discharging_min_c = 0,
- .discharging_max_c = 75,
- },
- },
-};
-BUILD_ASSERT(ARRAY_SIZE(board_battery_info) == BATTERY_TYPE_COUNT);
-
-const enum battery_type DEFAULT_BATTERY_TYPE = BATTERY_LGC011;
-
-int board_set_active_charge_port(int port)
-{
- return EC_SUCCESS;
-}
-
-int board_is_vbus_too_low(int port, enum chg_ramp_vbus_state ramp_state)
-{
- return 0;
-}
-
-void board_set_charge_limit(int port, int supplier, int charge_ma, int max_ma,
- int charge_mv)
-{
-}
-
-struct tcpc_config_t tcpc_config[] = {
- [USBC_PORT_C0] = {
- .bus_type = EC_BUS_TYPE_I2C,
- .i2c_info = {
- .port = I2C_PORT_USB_C0,
- .addr_flags = TUSB422_I2C_ADDR_FLAGS,
- },
- .drv = &tusb422_tcpm_drv,
- },
- [USBC_PORT_C1] = {
- .bus_type = EC_BUS_TYPE_I2C,
- .i2c_info = {
- .port = I2C_PORT_USB_C1,
- .addr_flags = TUSB422_I2C_ADDR_FLAGS,
- },
- .drv = &tusb422_tcpm_drv,
- },
-};
-BUILD_ASSERT(ARRAY_SIZE(tcpc_config) == USBC_PORT_COUNT);
-BUILD_ASSERT(CONFIG_USB_PD_PORT_MAX_COUNT == USBC_PORT_COUNT);
-
-int board_is_sourcing_vbus(int port)
-{
- return 0;
-}
-
-struct usb_mux usbc1_virtual_usb_mux = {
- .usb_port = USBC_PORT_C1,
- .driver = &virtual_usb_mux_driver,
- .hpd_update = &virtual_hpd_update,
-};
-
-struct usb_mux usb_muxes[] = {
- [USBC_PORT_C0] = {
- .usb_port = USBC_PORT_C0,
- .driver = &virtual_usb_mux_driver,
- .hpd_update = &virtual_hpd_update,
- },
- [USBC_PORT_C1] = {
- .usb_port = USBC_PORT_C1,
- .driver = &bb_usb_retimer,
- .hpd_update = bb_retimer_hpd_update,
- .next_mux = &usbc1_virtual_usb_mux,
- .i2c_port = I2C_PORT_USB_C1,
- .i2c_addr_flags = DT_REG_ADDR(DT_NODELABEL(
- usb_c1_bb_retimer_emul)),
- },
-};
-BUILD_ASSERT(ARRAY_SIZE(usb_muxes) == USBC_PORT_COUNT);
-
-struct bb_usb_control bb_controls[] = {
- [USBC_PORT_C0] = {
- /* USB-C port 0 doesn't have a retimer */
- },
- [USBC_PORT_C1] = {
- .usb_ls_en_gpio = GPIO_USB_C1_LS_EN,
- .retimer_rst_gpio = GPIO_USB_C1_RT_RST_ODL,
- },
-};
-BUILD_ASSERT(ARRAY_SIZE(bb_controls) == USBC_PORT_COUNT);
-
-void pd_power_supply_reset(int port)
-{
-}
-
-int pd_check_vconn_swap(int port)
-{
- return 0;
-}
-
-int pd_set_power_supply_ready(int port)
-{
- return EC_SUCCESS;
-}
-
-/* USBC PPC configuration */
-struct ppc_config_t ppc_chips[] = {
- [USBC_PORT_C0] = {
- .i2c_port = I2C_PORT_USB_C0,
- .i2c_addr_flags = SN5S330_ADDR0_FLAGS,
- .drv = &sn5s330_drv,
- },
- [USBC_PORT_C1] = {
- .i2c_port = I2C_PORT_USB_C1,
- .i2c_addr_flags = SYV682X_ADDR1_FLAGS,
- /* TODO(b/190519131): Add FRS GPIO, test FRS */
- .drv = &syv682x_drv,
- },
-};
-BUILD_ASSERT(ARRAY_SIZE(ppc_chips) == USBC_PORT_COUNT);
-unsigned int ppc_cnt = ARRAY_SIZE(ppc_chips);
-
-void system_hibernate(uint32_t seconds, uint32_t microseconds)
-{
-}
-
-uint16_t tcpc_get_alert_status(void)
-{
- return 0;
-}
-
-enum power_state power_chipset_init(void)
-{
- return POWER_G3;
-}
-
-enum power_state mock_state = POWER_G3;
-
-void set_mock_power_state(enum power_state state)
-{
- mock_state = state;
- task_wake(TASK_ID_CHIPSET);
-}
-
-enum power_state power_handle_state(enum power_state state)
-{
- return mock_state;
-}
-
-void chipset_reset(enum chipset_reset_reason reason)
-{
-}
-
-void chipset_force_shutdown(enum chipset_shutdown_reason reason)
-{
-}
-
-/* Power signals list. Must match order of enum power_signal. */
-const struct power_signal_info power_signal_list[] = {};
diff --git a/zephyr/test/drivers/src/tcs3400.c b/zephyr/test/drivers/src/tcs3400.c
deleted file mode 100644
index c20137c1ad..0000000000
--- a/zephyr/test/drivers/src/tcs3400.c
+++ /dev/null
@@ -1,622 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <zephyr.h>
-#include <ztest.h>
-
-#include "common.h"
-#include "i2c.h"
-#include "emul/emul_tcs3400.h"
-#include "emul/emul_common_i2c.h"
-
-#include "motion_sense.h"
-#include "motion_sense_fifo.h"
-#include "driver/als_tcs3400.h"
-
-#define TCS_ORD DT_DEP_ORD(DT_NODELABEL(tcs_emul))
-#define TCS_CLR_SENSOR_ID SENSOR_ID(DT_NODELABEL(tcs3400_clear))
-#define TCS_RGB_SENSOR_ID SENSOR_ID(DT_NODELABEL(tcs3400_rgb))
-#define TCS_INT_EVENT \
- TASK_EVENT_MOTION_SENSOR_INTERRUPT(SENSOR_ID(DT_ALIAS(tcs3400_int)))
-
-/** How accurate comparision of rgb sensors should be */
-#define V_EPS 8
-
-/** Test initialization of light sensor driver and device */
-static void test_tcs_init(void)
-{
- struct motion_sensor_t *ms, *ms_rgb;
- struct i2c_emul *emul;
-
- emul = tcs_emul_get(TCS_ORD);
- ms = &motion_sensors[TCS_CLR_SENSOR_ID];
- ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID];
-
- /* RGB sensor initialization is always successful */
- zassert_equal(EC_SUCCESS, ms_rgb->drv->init(ms_rgb), NULL);
-
- /* Fail init on communication errors */
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_FAIL_ALL_REG);
- zassert_equal(EC_ERROR_INVAL, ms->drv->init(ms), NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Fail on bad ID */
- tcs_emul_set_reg(emul, TCS_I2C_ID, 0);
- zassert_equal(EC_ERROR_ACCESS_DENIED, ms->drv->init(ms), NULL);
- /* Restore ID */
- tcs_emul_set_reg(emul, TCS_I2C_ID,
- DT_STRING_TOKEN(DT_NODELABEL(tcs_emul), device_id));
-
- /* Test successful init. ATIME and AGAIN should be changed on init */
- zassert_equal(EC_SUCCESS, ms->drv->init(ms), NULL);
- zassert_equal(TCS_DEFAULT_ATIME,
- tcs_emul_get_reg(emul, TCS_I2C_ATIME), NULL);
- zassert_equal(TCS_DEFAULT_AGAIN,
- tcs_emul_get_reg(emul, TCS_I2C_CONTROL), NULL);
-}
-
-/** Test if read function leaves device in correct mode to accuire data */
-static void test_tcs_read(void)
-{
- struct motion_sensor_t *ms;
- struct i2c_emul *emul;
- uint8_t enable;
- intv3_t v;
-
- emul = tcs_emul_get(TCS_ORD);
- ms = &motion_sensors[TCS_CLR_SENSOR_ID];
-
- /* Test error on writing registers */
- i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_ATIME);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, v), NULL);
- i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_CONTROL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, v), NULL);
- i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_ENABLE);
- zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, v), NULL);
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test starting read with calibration */
- tcs_emul_set_reg(emul, TCS_I2C_ATIME, 0);
- tcs_emul_set_reg(emul, TCS_I2C_CONTROL, 0);
- tcs_emul_set_reg(emul, TCS_I2C_ENABLE, 0);
- zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 1), NULL);
- zassert_equal(EC_RES_IN_PROGRESS, ms->drv->read(ms, v), NULL);
- zassert_equal(TCS_CALIBRATION_ATIME,
- tcs_emul_get_reg(emul, TCS_I2C_ATIME), NULL);
- zassert_equal(TCS_CALIBRATION_AGAIN,
- tcs_emul_get_reg(emul, TCS_I2C_CONTROL), NULL);
- enable = tcs_emul_get_reg(emul, TCS_I2C_ENABLE);
- zassert_true(enable & TCS_I2C_ENABLE_POWER_ON, NULL);
- zassert_true(enable & TCS_I2C_ENABLE_ADC_ENABLE, NULL);
- zassert_true(enable & TCS_I2C_ENABLE_INT_ENABLE, NULL);
-
- /* Test starting read without calibration */
- tcs_emul_set_reg(emul, TCS_I2C_ATIME, 0);
- tcs_emul_set_reg(emul, TCS_I2C_CONTROL, 0);
- tcs_emul_set_reg(emul, TCS_I2C_ENABLE, 0);
- zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 0), NULL);
- zassert_equal(EC_RES_IN_PROGRESS, ms->drv->read(ms, v), NULL);
- enable = tcs_emul_get_reg(emul, TCS_I2C_ENABLE);
- zassert_true(enable & TCS_I2C_ENABLE_POWER_ON, NULL);
- zassert_true(enable & TCS_I2C_ENABLE_ADC_ENABLE, NULL);
- zassert_true(enable & TCS_I2C_ENABLE_INT_ENABLE, NULL);
-}
-
-/** Check if FIFO for RGB and clear sensor is empty */
-static void check_fifo_empty_f(struct motion_sensor_t *ms,
- struct motion_sensor_t *ms_rgb, int line)
-{
- struct ec_response_motion_sensor_data vector;
- uint16_t size;
-
- /* Read all data committed to FIFO */
- while (motion_sense_fifo_read(sizeof(vector), 1, &vector, &size)) {
- /* Ignore timestamp frames */
- if (vector.flags == MOTIONSENSE_SENSOR_FLAG_TIMESTAMP) {
- continue;
- }
-
- if (ms - motion_sensors == vector.sensor_num) {
- zassert_unreachable(
- "Unexpected frame for clear sensor");
- }
-
- if (ms_rgb - motion_sensors == vector.sensor_num) {
- zassert_unreachable("Unexpected frame for rgb sensor");
- }
- }
-}
-#define check_fifo_empty(ms, ms_rgb) \
- check_fifo_empty_f(ms, ms_rgb, __LINE__)
-
-/**
- * Test different conditions where irq handler fail or commit no data
- * to fifo
- */
-static void test_tcs_irq_handler_fail(void)
-{
- struct motion_sensor_t *ms, *ms_rgb;
- struct i2c_emul *emul;
- uint32_t event;
-
- emul = tcs_emul_get(TCS_ORD);
- ms = &motion_sensors[TCS_CLR_SENSOR_ID];
- ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID];
-
- /* Fail on wrong event */
- event = 0x1234 & ~TCS_INT_EVENT;
- zassert_equal(EC_ERROR_NOT_HANDLED, ms->drv->irq_handler(ms, &event),
- NULL);
- check_fifo_empty(ms, ms_rgb);
-
- event = TCS_INT_EVENT;
- /* Test error on reading status */
- i2c_common_emul_set_read_fail_reg(emul, TCS_I2C_STATUS);
- zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- check_fifo_empty(ms, ms_rgb);
-
- /* Test fail on changing device power state */
- i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_ENABLE);
- zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL);
- i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
- check_fifo_empty(ms, ms_rgb);
-
- /* Test that no data is committed when status is 0 */
- tcs_emul_set_reg(emul, TCS_I2C_STATUS, 0);
- zassert_equal(EC_SUCCESS, ms->drv->irq_handler(ms, &event), NULL);
- check_fifo_empty(ms, ms_rgb);
-}
-
-/**
- * Check if last data committed to FIFO for RGB and clear sensor equals to
- * expected value.
- */
-static void check_fifo_f(struct motion_sensor_t *ms,
- struct motion_sensor_t *ms_rgb,
- int *exp_v, int eps, int line)
-{
- struct ec_response_motion_sensor_data vector;
- uint16_t size;
- int ret_v[4] = {-1, -1, -1, -1};
- int i;
-
- /* Read all data committed to FIFO */
- while (motion_sense_fifo_read(sizeof(vector), 1, &vector, &size)) {
- /* Ignore timestamp frames */
- if (vector.flags == MOTIONSENSE_SENSOR_FLAG_TIMESTAMP) {
- continue;
- }
-
- /* Get clear frame */
- if (ms - motion_sensors == vector.sensor_num) {
- ret_v[0] = vector.udata[0];
- }
-
- /* Get rgb frame */
- if (ms_rgb - motion_sensors == vector.sensor_num) {
- ret_v[1] = vector.udata[0];
- ret_v[2] = vector.udata[1];
- ret_v[3] = vector.udata[2];
- }
- }
-
- if (ret_v[0] == -1) {
- zassert_unreachable("No frame for clear sensor, line %d", line);
- }
-
- if (ret_v[1] == -1) {
- zassert_unreachable("No frame for rgb sensor, line %d", line);
- }
-
- /* Compare with last committed data */
- for (i = 0; i < 4; i++) {
- zassert_within(exp_v[i], ret_v[i], eps,
- "Expected [%d; %d; %d; %d], got [%d; %d; %d; %d]; line: %d",
- exp_v[0], exp_v[1], exp_v[2], exp_v[3],
- ret_v[0], ret_v[1], ret_v[2], ret_v[3], line);
- }
-}
-#define check_fifo(ms, ms_rgb, exp_v, eps) \
- check_fifo_f(ms, ms_rgb, exp_v, eps, __LINE__)
-
-/** Test calibration mode reading of light sensor values */
-static void test_tcs_read_calibration(void)
-{
- struct motion_sensor_t *ms, *ms_rgb;
- struct i2c_emul *emul;
- uint32_t event = TCS_INT_EVENT;
- int emul_v[4];
- int exp_v[4];
- intv3_t v;
-
- emul = tcs_emul_get(TCS_ORD);
- ms = &motion_sensors[TCS_CLR_SENSOR_ID];
- ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID];
-
- /* Need to be set to collect all data in FIFO */
- ms->oversampling_ratio = 1;
- ms_rgb->oversampling_ratio = 1;
- /* Enable calibration mode */
- zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 1), NULL);
- /* Setup AGAIN and ATIME for calibration */
- zassert_equal(EC_RES_IN_PROGRESS, ms->drv->read(ms, v), NULL);
-
- /* Test data that are in calibration range */
- exp_v[0] = 12;
- exp_v[1] = 123;
- exp_v[2] = 1234;
- exp_v[3] = 12345;
- /*
- * Emulator value is with gain 64, while expected value is
- * with gain 16
- */
- emul_v[0] = exp_v[0] * 64 / 16;
- emul_v[1] = exp_v[1] * 64 / 16;
- emul_v[2] = exp_v[2] * 64 / 16;
- emul_v[3] = exp_v[3] * 64 / 16;
- tcs_emul_set_val(emul, TCS_EMUL_C, emul_v[0]);
- tcs_emul_set_val(emul, TCS_EMUL_R, emul_v[1]);
- tcs_emul_set_val(emul, TCS_EMUL_G, emul_v[2]);
- tcs_emul_set_val(emul, TCS_EMUL_B, emul_v[3]);
- /* Set status to show valid data */
- tcs_emul_set_reg(emul, TCS_I2C_STATUS, TCS_I2C_STATUS_RGBC_VALID);
-
- zassert_equal(EC_SUCCESS, ms->drv->irq_handler(ms, &event), NULL);
- /* In calibration mode check for exact match */
- check_fifo(ms, ms_rgb, exp_v, 1);
-
- /* Test data that are outside of calibration range */
- exp_v[0] = 0;
- exp_v[1] = UINT16_MAX;
- exp_v[2] = UINT16_MAX;
- exp_v[3] = 213;
- /*
- * Emulator value is with gain 64, while expected value is
- * with gain 16
- */
- emul_v[0] = 0;
- emul_v[1] = exp_v[1] * 64 / 16;
- emul_v[2] = (UINT16_MAX + 23) * 64 / 16;
- emul_v[3] = exp_v[3] * 64 / 16;
- tcs_emul_set_val(emul, TCS_EMUL_C, emul_v[0]);
- tcs_emul_set_val(emul, TCS_EMUL_R, emul_v[1]);
- tcs_emul_set_val(emul, TCS_EMUL_G, emul_v[2]);
- tcs_emul_set_val(emul, TCS_EMUL_B, emul_v[3]);
- /* Set status to show valid data */
- tcs_emul_set_reg(emul, TCS_I2C_STATUS, TCS_I2C_STATUS_RGBC_VALID);
-
- zassert_equal(EC_SUCCESS, ms->drv->irq_handler(ms, &event), NULL);
- /* In calibration mode check for exact match */
- check_fifo(ms, ms_rgb, exp_v, 1);
-}
-
-/**
- * Set emulator internal value using expected output value returned by
- * the driver. First element of expected vector is IR value used in
- * calculations. Based on that clear light value is calculated.
- * First element of expected vector is updated by this function.
- */
-static void set_emul_val_from_exp(int *exp_v, uint16_t *scale,
- struct i2c_emul *emul)
-{
- int emul_v[4];
- int ir;
-
- /* We use exp_v[0] as IR value */
- ir = exp_v[0];
- /* Driver will return lux value as calculated blue light value */
- exp_v[0] = exp_v[2];
-
- /*
- * Driver takes care of different ATIME and AGAIN value, so expected
- * value is always normalized to ATIME 256 and AGAIN 16. Convert it
- * to internal emulator value (ATIME 256, AGAIN 64) and add expected IR
- * value. Clear light is the sum of rgb light and IR component.
- */
- emul_v[1] = (exp_v[1] + ir) * 64 / 16;
- emul_v[2] = (exp_v[2] + ir) * 64 / 16;
- emul_v[3] = (exp_v[3] + ir) * 64 / 16;
- emul_v[0] = (exp_v[1] + exp_v[2] + exp_v[3] + ir) * 64 / 16;
-
- /* Apply scale, driver should divide by this value */
- emul_v[0] = SENSOR_APPLY_SCALE(emul_v[0], scale[0]);
- emul_v[1] = SENSOR_APPLY_SCALE(emul_v[1], scale[1]);
- emul_v[2] = SENSOR_APPLY_SCALE(emul_v[2], scale[2]);
- emul_v[3] = SENSOR_APPLY_SCALE(emul_v[3], scale[3]);
-
- /* Set emulator values */
- tcs_emul_set_val(emul, TCS_EMUL_C, emul_v[0]);
- tcs_emul_set_val(emul, TCS_EMUL_R, emul_v[1]);
- tcs_emul_set_val(emul, TCS_EMUL_G, emul_v[2]);
- tcs_emul_set_val(emul, TCS_EMUL_B, emul_v[3]);
-}
-
-/** Test normal mode reading of light sensor values */
-static void test_tcs_read_xyz(void)
-{
- struct motion_sensor_t *ms, *ms_rgb;
- struct i2c_emul *emul;
- uint32_t event = TCS_INT_EVENT;
- /* Expected data to test: IR, R, G, B */
- int exp_v[][4] = {
- {200, 1110, 870, 850},
- {300, 1110, 10000, 8500},
- {600, 50000, 40000, 30000},
- {1000, 3000, 40000, 2000},
- {1000, 65000, 65000, 65000},
- {100, 214, 541, 516},
- {143, 2141, 5414, 5163},
- {100, 50000, 40000, 30000},
- {1430, 2141, 5414, 5163},
- {10000, 50000, 40000, 30000},
- {10000, 214, 541, 516},
- {15000, 50000, 40000, 30000},
- };
- uint16_t scale[4] = {
- MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE,
- MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE
- };
- int i, test;
- intv3_t v;
-
- emul = tcs_emul_get(TCS_ORD);
- ms = &motion_sensors[TCS_CLR_SENSOR_ID];
- ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID];
-
- /* Need to be set to collect all data in FIFO */
- ms->oversampling_ratio = 1;
- ms_rgb->oversampling_ratio = 1;
- /* Disable calibration mode */
- zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 0), NULL);
- /* Setup AGAIN and ATIME for normal mode */
- zassert_equal(EC_RES_IN_PROGRESS, ms->drv->read(ms, v), NULL);
-
- /* Test different data in supported range */
- for (test = 0; test < ARRAY_SIZE(exp_v); test++) {
- set_emul_val_from_exp(exp_v[test], scale, emul);
-
- /* Run few times to allow driver change gain */
- for (i = 0; i < 5; i++) {
- tcs_emul_set_reg(emul, TCS_I2C_STATUS,
- TCS_I2C_STATUS_RGBC_VALID);
- zassert_equal(EC_SUCCESS,
- ms->drv->irq_handler(ms, &event), NULL);
- }
- check_fifo(ms, ms_rgb, exp_v[test], V_EPS);
- }
-
- /* Test data that are outside of supported range */
- exp_v[0][0] = 3000;
- exp_v[0][1] = UINT16_MAX;
- exp_v[0][2] = UINT16_MAX * 32;
- exp_v[0][3] = 200;
- set_emul_val_from_exp(exp_v[0], scale, emul);
-
- /* Run few times to allow driver change gain */
- for (i = 0; i < 10; i++) {
- tcs_emul_set_reg(emul, TCS_I2C_STATUS,
- TCS_I2C_STATUS_RGBC_VALID);
- zassert_equal(EC_SUCCESS, ms->drv->irq_handler(ms, &event),
- NULL);
- }
- /*
- * If saturation value is exceeded on any rgb sensor, than data
- * shouldn't be committed to FIFO.
- */
- check_fifo_empty(ms, ms_rgb);
-}
-
-/**
- * Test getting and setting scale of light sensor. Checks if collected values
- * are scaled properly.
- */
-static void test_tcs_scale(void)
-{
- struct motion_sensor_t *ms, *ms_rgb;
- struct i2c_emul *emul;
- uint32_t event = TCS_INT_EVENT;
- /* Expected data to test: IR, R, G, B */
- int exp_v[][4] = {
- {200, 1110, 870, 850},
- {300, 1110, 10000, 8500},
- {600, 5000, 4000, 3000},
- {100, 3000, 4000, 2000},
- {100, 1000, 1000, 1000},
- };
- /* Scale for each test */
- uint16_t exp_scale[][4] = {
- {MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE,
- MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE},
- {MOTION_SENSE_DEFAULT_SCALE + 300,
- MOTION_SENSE_DEFAULT_SCALE + 300,
- MOTION_SENSE_DEFAULT_SCALE + 300,
- MOTION_SENSE_DEFAULT_SCALE + 300},
- {MOTION_SENSE_DEFAULT_SCALE - 300,
- MOTION_SENSE_DEFAULT_SCALE - 300,
- MOTION_SENSE_DEFAULT_SCALE - 300,
- MOTION_SENSE_DEFAULT_SCALE - 300},
- {MOTION_SENSE_DEFAULT_SCALE + 345,
- MOTION_SENSE_DEFAULT_SCALE - 5423,
- MOTION_SENSE_DEFAULT_SCALE - 30,
- MOTION_SENSE_DEFAULT_SCALE + 400},
- {MOTION_SENSE_DEFAULT_SCALE - 345,
- MOTION_SENSE_DEFAULT_SCALE + 5423,
- MOTION_SENSE_DEFAULT_SCALE + 30,
- MOTION_SENSE_DEFAULT_SCALE - 400},
- {MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE,
- MOTION_SENSE_DEFAULT_SCALE, MOTION_SENSE_DEFAULT_SCALE}
- };
- uint16_t scale[3];
- int16_t temp;
- int i, test;
- intv3_t v;
-
- emul = tcs_emul_get(TCS_ORD);
- ms = &motion_sensors[TCS_CLR_SENSOR_ID];
- ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID];
-
- /* Need to be set to collect all data in FIFO */
- ms->oversampling_ratio = 1;
- ms_rgb->oversampling_ratio = 1;
- /* Disable calibration mode */
- zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 0), NULL);
- /* Setup AGAIN and ATIME for normal mode */
- zassert_equal(EC_RES_IN_PROGRESS, ms->drv->read(ms, v), NULL);
-
- /* Test different data in supported range */
- for (test = 0; test < ARRAY_SIZE(exp_v); test++) {
- /* Set and test clear sensor scale */
- zassert_equal(EC_SUCCESS,
- ms->drv->set_scale(ms, exp_scale[test], 0),
- "test %d", test);
- zassert_equal(EC_SUCCESS,
- ms->drv->get_scale(ms, scale, &temp),
- "test %d", test);
- zassert_equal((int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, temp,
- "test %d, %d", test, temp);
- zassert_equal(exp_scale[test][0], scale[0], "test %d", test);
-
- /* Set and test RGB sensor scale */
- zassert_equal(EC_SUCCESS, ms_rgb->drv->set_scale(ms_rgb,
- &(exp_scale[test][1]), 0),
- "test %d", test);
- zassert_equal(EC_SUCCESS,
- ms_rgb->drv->get_scale(ms_rgb, scale, &temp),
- "test %d", test);
- zassert_equal((int16_t)EC_MOTION_SENSE_INVALID_CALIB_TEMP, temp,
- "test %d", test);
- zassert_equal(exp_scale[test][1], scale[0], "test %d", test);
- zassert_equal(exp_scale[test][2], scale[1], "test %d", test);
- zassert_equal(exp_scale[test][3], scale[2], "test %d", test);
-
- set_emul_val_from_exp(exp_v[test], exp_scale[test], emul);
-
- /* Run few times to allow driver change gain */
- for (i = 0; i < 5; i++) {
- tcs_emul_set_reg(emul, TCS_I2C_STATUS,
- TCS_I2C_STATUS_RGBC_VALID);
- zassert_equal(EC_SUCCESS,
- ms->drv->irq_handler(ms, &event), NULL);
- }
- check_fifo(ms, ms_rgb, exp_v[test], V_EPS);
- }
-
- /* Test fail if scale equals 0 */
- scale[0] = 0;
- scale[1] = MOTION_SENSE_DEFAULT_SCALE;
- scale[2] = MOTION_SENSE_DEFAULT_SCALE;
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_scale(ms, scale, 0), NULL);
-
- zassert_equal(EC_ERROR_INVAL, ms_rgb->drv->set_scale(ms_rgb, scale, 0),
- NULL);
- scale[0] = MOTION_SENSE_DEFAULT_SCALE;
- scale[1] = 0;
- scale[2] = MOTION_SENSE_DEFAULT_SCALE;
- zassert_equal(EC_ERROR_INVAL, ms_rgb->drv->set_scale(ms_rgb, scale, 0),
- NULL);
- scale[0] = MOTION_SENSE_DEFAULT_SCALE;
- scale[1] = MOTION_SENSE_DEFAULT_SCALE;
- scale[2] = 0;
- zassert_equal(EC_ERROR_INVAL, ms_rgb->drv->set_scale(ms_rgb, scale, 0),
- NULL);
-}
-
-/** Test setting and getting data rate of light sensor */
-static void test_tcs_data_rate(void)
-{
- struct motion_sensor_t *ms, *ms_rgb;
- struct i2c_emul *emul;
- uint8_t enable;
-
- emul = tcs_emul_get(TCS_ORD);
- ms = &motion_sensors[TCS_CLR_SENSOR_ID];
- /* RGB sensor doesn't set rate, but return rate of clear sesnor */
- ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID];
-
- /* Test fail on reading device power state */
- i2c_common_emul_set_read_fail_reg(emul, TCS_I2C_ENABLE);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 0, 0), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 0, 1), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 100, 0), NULL);
- zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 100, 1), NULL);
- i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG);
-
- /* Test setting 0 rate disables device */
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 0, 0), NULL);
- zassert_equal(0, tcs_emul_get_reg(emul, TCS_I2C_ENABLE), NULL);
- zassert_equal(0, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(0, ms_rgb->drv->get_data_rate(ms_rgb), NULL);
-
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 0, 1), NULL);
- zassert_equal(0, tcs_emul_get_reg(emul, TCS_I2C_ENABLE), NULL);
- zassert_equal(0, tcs_emul_get_reg(emul, TCS_I2C_ENABLE), NULL);
- zassert_equal(0, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(0, ms_rgb->drv->get_data_rate(ms_rgb), NULL);
-
-
- /* Test setting non-zero rate enables device */
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 100, 0), NULL);
- enable = tcs_emul_get_reg(emul, TCS_I2C_ENABLE);
- zassert_true(enable & TCS_I2C_ENABLE_POWER_ON, NULL);
- zassert_true(enable & TCS_I2C_ENABLE_ADC_ENABLE, NULL);
- zassert_true(enable & TCS_I2C_ENABLE_INT_ENABLE, NULL);
- zassert_equal(100, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(100, ms_rgb->drv->get_data_rate(ms_rgb), NULL);
-
- zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 100, 1), NULL);
- enable = tcs_emul_get_reg(emul, TCS_I2C_ENABLE);
- zassert_true(enable & TCS_I2C_ENABLE_POWER_ON, NULL);
- zassert_true(enable & TCS_I2C_ENABLE_ADC_ENABLE, NULL);
- zassert_true(enable & TCS_I2C_ENABLE_INT_ENABLE, NULL);
- zassert_equal(100, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(100, ms_rgb->drv->get_data_rate(ms_rgb), NULL);
-
- /* Test RGB sensor doesn't change data rate */
- zassert_equal(EC_SUCCESS, ms_rgb->drv->set_data_rate(ms_rgb, 300, 0),
- NULL);
- zassert_equal(100, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(100, ms_rgb->drv->get_data_rate(ms_rgb), NULL);
-
- zassert_equal(EC_SUCCESS, ms_rgb->drv->set_data_rate(ms_rgb, 300, 1),
- NULL);
- zassert_equal(100, ms->drv->get_data_rate(ms), NULL);
- zassert_equal(100, ms_rgb->drv->get_data_rate(ms_rgb), NULL);
-}
-
-/** Test set range function of clear and RGB sensors */
-static void test_tcs_set_range(void)
-{
- struct motion_sensor_t *ms, *ms_rgb;
- struct i2c_emul *emul;
-
- emul = tcs_emul_get(TCS_ORD);
- ms = &motion_sensors[TCS_CLR_SENSOR_ID];
- ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID];
-
- /* RGB sensor doesn't set anything */
- zassert_equal(EC_SUCCESS, ms_rgb->drv->set_range(ms_rgb, 1, 0), NULL);
-
- /* Clear sensor doesn't change anything on device to set range */
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 0x12300, 1), NULL);
- zassert_equal(0x12300, ms->current_range, NULL);
-
- zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, 0x10000, 0), NULL);
- zassert_equal(0x10000, ms->current_range, NULL);
-}
-
-void test_suite_tcs3400(void)
-{
- ztest_test_suite(tcs3400,
- ztest_user_unit_test(test_tcs_init),
- ztest_user_unit_test(test_tcs_read),
- ztest_user_unit_test(test_tcs_irq_handler_fail),
- ztest_user_unit_test(test_tcs_read_calibration),
- ztest_user_unit_test(test_tcs_read_xyz),
- ztest_user_unit_test(test_tcs_scale),
- ztest_user_unit_test(test_tcs_data_rate),
- ztest_user_unit_test(test_tcs_set_range));
- ztest_run_test_suite(tcs3400);
-}
diff --git a/zephyr/test/drivers/src/temp_sensor.c b/zephyr/test/drivers/src/temp_sensor.c
deleted file mode 100644
index 83a343e572..0000000000
--- a/zephyr/test/drivers/src/temp_sensor.c
+++ /dev/null
@@ -1,139 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <zephyr.h>
-#include <ztest.h>
-#include <drivers/adc.h>
-#include <drivers/adc/adc_emul.h>
-#include <drivers/gpio.h>
-#include <drivers/gpio/gpio_emul.h>
-
-#include <math.h>
-
-#include "common.h"
-#include "temp_sensor.h"
-#include "temp_sensor/temp_sensor.h"
-
-#define GPIO_PG_EC_DSW_PWROK_PATH DT_PATH(named_gpios, pg_ec_dsw_pwrok)
-#define GPIO_PG_EC_DSW_PWROK_PORT DT_GPIO_PIN(GPIO_PG_EC_DSW_PWROK_PATH, gpios)
-
-#define ADC_DEVICE_NODE DT_NODELABEL(adc0)
-#define ADC_CHANNELS_NUM DT_PROP(DT_NODELABEL(adc0), nchannels)
-
-/** Test error code when invalid sensor is passed to temp_sensor_read() */
-static void test_temp_sensor_wrong_id(void)
-{
- int temp;
-
- zassert_equal(EC_ERROR_INVAL, temp_sensor_read(TEMP_SENSOR_COUNT,
- &temp),
- NULL);
-}
-
-/** Test error code when temp_sensor_read() is called with powered off ADC */
-static void test_temp_sensor_adc_error(void)
-{
- const struct device *gpio_dev =
- DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_PG_EC_DSW_PWROK_PATH, gpios));
- int temp;
-
- zassert_not_null(gpio_dev, "Cannot get GPIO device");
-
- /*
- * pg_ec_dsw_pwrok = 0 means ADC is not powered.
- * adc_read will return error
- */
- zassert_ok(gpio_emul_input_set(gpio_dev, GPIO_PG_EC_DSW_PWROK_PORT, 0),
- NULL);
-
- zassert_equal(EC_ERROR_NOT_POWERED,
- temp_sensor_read(TEMP_SENSOR_CHARGER, &temp), NULL);
- zassert_equal(EC_ERROR_NOT_POWERED,
- temp_sensor_read(TEMP_SENSOR_DDR_SOC, &temp), NULL);
- zassert_equal(EC_ERROR_NOT_POWERED,
- temp_sensor_read(TEMP_SENSOR_FAN, &temp), NULL);
- zassert_equal(EC_ERROR_NOT_POWERED,
- temp_sensor_read(TEMP_SENSOR_PP3300_REGULATOR, &temp),
- NULL);
-
- /* power ADC */
- zassert_ok(gpio_emul_input_set(gpio_dev, GPIO_PG_EC_DSW_PWROK_PORT, 1),
- NULL);
-}
-
-/** Simple ADC emulator custom function which always return error */
-static int adc_error_func(const struct device *dev, unsigned int channel,
- void *param, uint32_t *result)
-{
- return -EINVAL;
-}
-
-/**
- * Set valid response only for ADC channel connected with tested sensor.
- * Check if temp_sensor_read() from tested sensor returns EC_SUCCESS and
- * valid temperature. Set invalid response on ADC channel for next test.
- */
-static void check_valid_temperature(const struct device *adc_dev, int sensor)
-{
- int temp;
-
- /* ADC channel of tested sensor return valid value */
- zassert_ok(adc_emul_const_value_set(adc_dev, temp_sensors[sensor].idx,
- 1000),
- "adc_emul_const_value_set() failed (sensor %d)", sensor);
- zassert_equal(EC_SUCCESS, temp_sensor_read(sensor, &temp), NULL);
- zassert_within(temp, 273 + 50, 51,
- "Expected temperature in 0*C-100*C, got %d*C (sensor %d)",
- temp - 273, sensor);
- /* Return error on ADC channel of tested sensor */
- zassert_ok(adc_emul_value_func_set(adc_dev, temp_sensors[sensor].idx,
- adc_error_func, NULL),
- "adc_emul_value_func_set() failed (sensor %d)", sensor);
-}
-
-/** Test if temp_sensor_read() returns temperature on success */
-static void test_temp_sensor_read(void)
-{
- const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE);
- int chan;
-
- zassert_not_null(adc_dev, "Cannot get ADC device");
-
- /* Return error on all ADC channels */
- for (chan = 0; chan < ADC_CHANNELS_NUM; chan++) {
- zassert_ok(adc_emul_value_func_set(adc_dev, chan,
- adc_error_func, NULL),
- "channel %d adc_emul_value_func_set() failed", chan);
- }
-
- check_valid_temperature(adc_dev, TEMP_SENSOR_CHARGER);
- check_valid_temperature(adc_dev, TEMP_SENSOR_DDR_SOC);
- check_valid_temperature(adc_dev, TEMP_SENSOR_FAN);
- check_valid_temperature(adc_dev, TEMP_SENSOR_PP3300_REGULATOR);
-
- /* Return correct value on all ADC channels */
- for (chan = 0; chan < ADC_CHANNELS_NUM; chan++) {
- zassert_ok(adc_emul_const_value_set(adc_dev, chan, 1000),
- "channel %d adc_emul_const_value_set() failed",
- chan);
- }
-}
-
-void test_suite_temp_sensor(void)
-{
- const struct device *dev =
- DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_PG_EC_DSW_PWROK_PATH, gpios));
-
- zassert_not_null(dev, NULL);
- /* Before tests make sure that power pin is set. */
- zassert_ok(gpio_emul_input_set(dev, GPIO_PG_EC_DSW_PWROK_PORT, 1),
- NULL);
-
- ztest_test_suite(temp_sensor,
- ztest_user_unit_test(test_temp_sensor_wrong_id),
- ztest_user_unit_test(test_temp_sensor_adc_error),
- ztest_user_unit_test(test_temp_sensor_read));
- ztest_run_test_suite(temp_sensor);
-}
diff --git a/zephyr/test/drivers/src/thermistor.c b/zephyr/test/drivers/src/thermistor.c
deleted file mode 100644
index a7137d5f19..0000000000
--- a/zephyr/test/drivers/src/thermistor.c
+++ /dev/null
@@ -1,295 +0,0 @@
-/* Copyright 2021 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include <zephyr.h>
-#include <ztest.h>
-#include <drivers/adc.h>
-#include <drivers/adc/adc_emul.h>
-#include <drivers/gpio.h>
-#include <drivers/gpio/gpio_emul.h>
-#include <temp_sensor.h>
-
-#include "common.h"
-#include "../driver/temp_sensor/thermistor.h"
-#include "temp_sensor/temp_sensor.h"
-
-
-#define GPIO_PG_EC_DSW_PWROK_PATH DT_PATH(named_gpios, pg_ec_dsw_pwrok)
-#define GPIO_PG_EC_DSW_PWROK_PORT DT_GPIO_PIN(GPIO_PG_EC_DSW_PWROK_PATH, gpios)
-
-#define ADC_DEVICE_NODE DT_NODELABEL(adc0)
-
-/* TODO replace counting macros with DT macro when
- * https://github.com/zephyrproject-rtos/zephyr/issues/38715 lands
- */
-#define _ACCUMULATOR(x)
-#define NAMED_TEMP_SENSORS_SIZE \
- DT_FOREACH_CHILD(DT_PATH(named_temp_sensors), _ACCUMULATOR) \
- 0
-#define TEMP_SENSORS_ENABLED_SIZE \
- DT_FOREACH_STATUS_OKAY(cros_ec_temp_sensor, _ACCUMULATOR) 0
-
-/* Conversion of temperature doesn't need to be 100% accurate */
-#define TEMP_EPS 2
-
-#define A_VALID_VOLTAGE 1000
-/**
- * Test if get temp function return expected error when ADC is not powered
- * (indicated as GPIO pin set to low) and return success after powering on ADC.
- */
-static void test_thermistor_power_pin(void)
-{
- int temp;
- int sensor_idx;
-
- const struct device *gpio_dev =
- DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_PG_EC_DSW_PWROK_PATH, gpios));
- const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE);
-
- zassert_not_null(gpio_dev, "Cannot get GPIO device");
- zassert_not_null(adc_dev, "Cannot get ADC device");
-
- /* Make sure that ADC return a valid value */
- for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE;
- sensor_idx++) {
- const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx];
-
- zassert_ok(adc_emul_const_value_set(adc_dev,
- sensor->idx,
- A_VALID_VOLTAGE),
- "adc_emul_value_func_set() failed on %s",
- sensor->name);
- }
-
- /* pg_ec_dsw_pwrok = 0 means ADC is not powered. */
- zassert_ok(gpio_emul_input_set(gpio_dev, GPIO_PG_EC_DSW_PWROK_PORT, 0),
- NULL);
-
- for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE;
- sensor_idx++) {
- const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx];
-
- zassert_equal(EC_ERROR_NOT_POWERED, sensor->read(sensor, &temp),
- "%s failed", sensor->name);
- }
-
- /* pg_ec_dsw_pwrok = 1 means ADC is powered. */
- zassert_ok(gpio_emul_input_set(gpio_dev, GPIO_PG_EC_DSW_PWROK_PORT, 1),
- NULL);
-
- for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE;
- sensor_idx++) {
- const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx];
-
- zassert_equal(EC_SUCCESS, sensor->read(sensor, &temp),
- "%s failed", sensor->name);
- }
-}
-
-/* Simple ADC emulator custom function which always return error */
-static int adc_error_func(const struct device *dev, unsigned int channel,
- void *param, uint32_t *result)
-{
- return -EINVAL;
-}
-
-/** Test if get temp function return expected error on ADC malfunction */
-static void test_thermistor_adc_read_error(void)
-{
- int temp;
- int sensor_idx;
-
- const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE);
-
- zassert_not_null(adc_dev, "Cannot get ADC device");
-
- /* Return error on all ADC channels */
- for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE;
- sensor_idx++) {
- const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx];
-
- zassert_ok(adc_emul_value_func_set(adc_dev, sensor->idx,
- adc_error_func, NULL),
- "adc_emul_value_func_set() failed on %s",
- sensor->name);
- }
-
- for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE;
- sensor_idx++) {
- const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx];
-
- zassert_equal(EC_ERROR_UNKNOWN, sensor->read(sensor, &temp),
- "%s failed", sensor->name);
- }
-}
-
-/** Get resistance of thermistor for given temperature */
-static int resistance_47kohm_B4050(int t)
-{
- /* Thermistor manufacturer resistance lookup table*/
- int r_table[] = {
- 155700, 147900, 140600, 133700, 127200, /* 0*C - 4*C */
- 121000, 115100, 109600, 104300, 99310, /* 5*C - 9*C */
- 94600, 90130, 85890, 81870, 78070, /* 10*C - 14*C */
- 74450, 71020, 67770, 64680, 61750, /* 15*C - 19*C */
- 58970, 56320, 53810, 51430, 49160, /* 20*C - 24*C */
- 47000, 44950, 42990, 41130, 39360, /* 25*C - 29*C */
- 37680, 36070, 34540, 33080, 31690, /* 30*C - 34*C */
- 30360, 29100, 27900, 26750, 25650, /* 35*C - 39*C */
- 24610, 23610, 22660, 21750, 20880, /* 40*C - 44*C */
- 20050, 19260, 18500, 17780, 17090, /* 45*C - 49*C */
- 16430, 15800, 15200, 14620, 14070, /* 50*C - 54*C */
- 13540, 13030, 12550, 12090, 11640, /* 55*C - 59*C */
- 11210, 10800, 10410, 10040, 9676, /* 60*C - 64*C */
- 9331, 8999, 8680, 8374, 8081, /* 65*C - 69*C */
- 7799, 7528, 7268, 7018, 6777, /* 70*C - 74*C */
- 6546, 6324, 6111, 5906, 5708, /* 75*C - 79*C */
- 5518, 5335, 5160, 4990, 4827, /* 80*C - 84*C */
- 4671, 4519, 4374, 4233, 4098, /* 85*C - 89*C */
- 3968, 3842, 3721, 3605, 3492, /* 90*C - 94*C */
- 3384, 3279, 3179, 3082, 2988, /* 95*C - 99*C */
- 2898 /* 100*C */
- };
-
- t -= 273;
- if (t < 0)
- return r_table[0] + 10000;
-
- if (t >= ARRAY_SIZE(r_table))
- return r_table[ARRAY_SIZE(r_table) - 1] - 100;
-
- return r_table[t];
-}
-
-/**
- * Calculate output voltage in voltage divider circuit using formula
- * Vout = Vs * r2 / (r1 + r2)
- */
-static int volt_divider(int vs, int r1, int r2)
-{
- return vs * r2 / (r1 + r2);
-}
-
-struct thermistor_state {
- const int v;
- const int r;
- int temp_expected;
-};
-
-/** ADC emulator function which calculate output voltage for given thermistor */
-static int adc_temperature_func(const struct device *dev, unsigned int channel,
- void *param, uint32_t *result)
-{
- struct thermistor_state *s = (struct thermistor_state *)param;
-
- *result = volt_divider(s->v,
- s->r,
- resistance_47kohm_B4050(s->temp_expected));
-
- return 0;
-}
-
-/** Test conversion from ADC raw value to temperature */
-static void do_thermistor_test(const struct temp_sensor_t *temp_sensor,
- int reference_mv, int reference_ohms)
-{
- int temp_expected;
- int temp;
-
- const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE);
- struct thermistor_state state = {
- .v = reference_mv,
- .r = reference_ohms,
- };
-
- zassert_not_null(adc_dev, "Cannot get ADC device");
-
- /* Setup ADC channel */
- zassert_ok(adc_emul_value_func_set(adc_dev,
- temp_sensor->idx,
- adc_temperature_func, &state),
- "adc_emul_value_func_set() failed on %s", temp_sensor->name);
-
- /* Makes sure that reference voltage is correct for given thermistor */
- zassert_ok(adc_emul_ref_voltage_set(adc_dev, ADC_REF_INTERNAL, state.v),
- "adc_emul_ref_voltage_set() failed %s on ",
- temp_sensor->name);
-
- /* Test whole supported range from 0*C to 100*C (273*K to 373*K) */
- for (temp_expected = 273; temp_expected <= 373; temp_expected++) {
- state.temp_expected = temp_expected;
- zassert_equal(EC_SUCCESS, temp_sensor->read(temp_sensor, &temp),
- "failed on %s", temp_sensor->name);
- zassert_within(temp_expected, temp, TEMP_EPS,
- "Expected %d*K, got %d*K on %s", temp_expected,
- temp, temp_sensor->name);
- }
-
- /* Temperatures below 0*C should be reported as 0*C */
- state.temp_expected = -15 + 273;
- zassert_equal(EC_SUCCESS, temp_sensor->read(temp_sensor, &temp),
- "failed on %s", temp_sensor->name);
- zassert_equal(273, temp, "Expected %d*K, got %d*K on %s", 273, temp,
- temp_sensor->name);
-
- /* Temperatures above 100*C should be reported as 100*C */
- state.temp_expected = 115 + 273;
- zassert_equal(EC_SUCCESS, temp_sensor->read(temp_sensor, &temp),
- "failed on %s", temp_sensor->name);
- zassert_equal(373, temp, "Expected %d*K, got %d*K on %s", 373, temp,
- temp_sensor->name);
-}
-
-#define GET_THERMISTOR_REF_MV(node_id) \
- [ZSHIM_TEMP_SENSOR_ID(node_id)] = DT_PROP( \
- DT_PHANDLE(node_id, thermistor), steinhart_reference_mv),
-
-#define GET_THERMISTOR_REF_RES(node_id) \
- [ZSHIM_TEMP_SENSOR_ID(node_id)] = DT_PROP( \
- DT_PHANDLE(node_id, thermistor), steinhart_reference_res),
-
-static void test_thermistors_adc_temperature_conversion(void)
-{
- int sensor_idx;
-
- const static int reference_mv_arr[] = { DT_FOREACH_STATUS_OKAY(
- cros_temp_sensor, GET_THERMISTOR_REF_MV) };
- const static int reference_res_arr[] = { DT_FOREACH_STATUS_OKAY(
- cros_temp_sensor, GET_THERMISTOR_REF_RES) };
-
- for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE; sensor_idx++)
- do_thermistor_test(&temp_sensors[sensor_idx],
- reference_mv_arr[sensor_idx],
- reference_res_arr[sensor_idx]);
-}
-
-static void test_device_nodes_enabled(void)
-{
- zassert_equal(NAMED_TEMP_SENSORS_SIZE, TEMP_SENSORS_ENABLED_SIZE,
- "Temperature sensors in device tree and "
- "those enabled for test differ");
-
- /* Thermistor nodes being enabled are already tested by compilation. */
-}
-
-void test_suite_thermistor(void)
-{
- const struct device *dev =
- DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_PG_EC_DSW_PWROK_PATH, gpios));
-
- zassert_not_null(dev, NULL);
- /* Before tests make sure that power pin is set. */
- zassert_ok(gpio_emul_input_set(dev, GPIO_PG_EC_DSW_PWROK_PORT, 1),
- NULL);
-
- ztest_test_suite(thermistor,
- ztest_user_unit_test(test_device_nodes_enabled),
- ztest_user_unit_test(test_thermistor_power_pin),
- ztest_user_unit_test(test_thermistor_adc_read_error),
- ztest_user_unit_test(
- test_thermistors_adc_temperature_conversion));
-
- ztest_run_test_suite(thermistor);
-}
diff --git a/zephyr/test/drivers/zmake.yaml b/zephyr/test/drivers/zmake.yaml
deleted file mode 100644
index 31d8523e8e..0000000000
--- a/zephyr/test/drivers/zmake.yaml
+++ /dev/null
@@ -1,14 +0,0 @@
-# Copyright 2021 The Chromium OS Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style license that can be
-# found in the LICENSE file.
-
-board: native_posix
-supported-zephyr-versions:
- - v2.6
-supported-toolchains:
- - llvm
- - host
-output-type: elf
-is-test: true
-dts-overlays:
- - overlay.dts