diff options
-rw-r--r-- | board/ryu/board.c | 325 | ||||
-rw-r--r-- | board/ryu/board.h | 13 | ||||
-rw-r--r-- | common/motion_sense.c | 81 | ||||
-rw-r--r-- | include/config.h | 6 | ||||
-rw-r--r-- | include/motion_sense.h | 4 | ||||
-rw-r--r-- | test/motion_lid.c | 6 |
6 files changed, 245 insertions, 190 deletions
diff --git a/board/ryu/board.c b/board/ryu/board.c index 73a24a05cf..345105ae6f 100644 --- a/board/ryu/board.c +++ b/board/ryu/board.c @@ -282,170 +282,175 @@ struct motion_sensor_t motion_sensors[] = { * Requirement: accelerometer sensor must init before gyro sensor * DO NOT change the order of the following table. */ - {.name = "Accel", - .active_mask = SENSOR_ACTIVE_S0_S3_S5, - .chip = MOTIONSENSE_CHIP_BMI160, - .type = MOTIONSENSE_TYPE_ACCEL, - .location = MOTIONSENSE_LOC_LID, - .drv = &bmi160_drv, - .mutex = &g_mutex, - .drv_data = &g_bmi160_data, - .addr = BMI160_ADDR0, - .rot_standard_ref = &accelgyro_standard_ref, - .default_range = 8, /* g, use hifi requirements */ - .config = { - /* AP: by default shutdown all sensors */ - [SENSOR_CONFIG_AP] = { - .odr = 0, - .ec_rate = 0, - }, - /* Used for double tap */ - [SENSOR_CONFIG_EC_S0] = { - .odr = TAP_ODR, - /* Interrupt driven, no polling */ - .ec_rate = 0, - }, - [SENSOR_CONFIG_EC_S3] = { - .odr = TAP_ODR, - .ec_rate = 0, - }, - [SENSOR_CONFIG_EC_S5] = { - .odr = TAP_ODR, - .ec_rate = 0, - }, - }, + [RYU_LID_ACCEL] = { + .name = "Accel", + .active_mask = SENSOR_ACTIVE_S0_S3_S5, + .chip = MOTIONSENSE_CHIP_BMI160, + .type = MOTIONSENSE_TYPE_ACCEL, + .location = MOTIONSENSE_LOC_LID, + .drv = &bmi160_drv, + .mutex = &g_mutex, + .drv_data = &g_bmi160_data, + .addr = BMI160_ADDR0, + .rot_standard_ref = &accelgyro_standard_ref, + .default_range = 8, /* g, use hifi requirements */ + .config = { + /* AP: by default shutdown all sensors */ + [SENSOR_CONFIG_AP] = { + .odr = 0, + .ec_rate = 0, + }, + /* Used for double tap */ + [SENSOR_CONFIG_EC_S0] = { + .odr = TAP_ODR, + /* Interrupt driven, no polling */ + .ec_rate = 0, + }, + [SENSOR_CONFIG_EC_S3] = { + .odr = TAP_ODR, + .ec_rate = 0, + }, + [SENSOR_CONFIG_EC_S5] = { + .odr = TAP_ODR, + .ec_rate = 0, + }, + }, }, - {.name = "Gyro", - .active_mask = SENSOR_ACTIVE_S0_S3, - .chip = MOTIONSENSE_CHIP_BMI160, - .type = MOTIONSENSE_TYPE_GYRO, - .location = MOTIONSENSE_LOC_LID, - .drv = &bmi160_drv, - .mutex = &g_mutex, - .drv_data = &g_bmi160_data, - .addr = BMI160_ADDR0, - .default_range = 1000, /* dps, use hifi requirement */ - .rot_standard_ref = &accelgyro_standard_ref, - .config = { - /* AP: by default shutdown all sensors */ - [SENSOR_CONFIG_AP] = { - .odr = 0, - .ec_rate = 0, - }, - /* EC does not need gyro in S0 */ - [SENSOR_CONFIG_EC_S0] = { - .odr = 0, - .ec_rate = 0, - }, - /* Unused */ - [SENSOR_CONFIG_EC_S3] = { - .odr = 0, - .ec_rate = 0, - }, - [SENSOR_CONFIG_EC_S5] = { - .odr = 0, - .ec_rate = 0, - }, - }, + [RYU_LID_GYRO] = { + .name = "Gyro", + .active_mask = SENSOR_ACTIVE_S0_S3, + .chip = MOTIONSENSE_CHIP_BMI160, + .type = MOTIONSENSE_TYPE_GYRO, + .location = MOTIONSENSE_LOC_LID, + .drv = &bmi160_drv, + .mutex = &g_mutex, + .drv_data = &g_bmi160_data, + .addr = BMI160_ADDR0, + .default_range = 1000, /* dps, use hifi requirement */ + .rot_standard_ref = &accelgyro_standard_ref, + .config = { + /* AP: by default shutdown all sensors */ + [SENSOR_CONFIG_AP] = { + .odr = 0, + .ec_rate = 0, + }, + /* EC does not need gyro in S0 */ + [SENSOR_CONFIG_EC_S0] = { + .odr = 0, + .ec_rate = 0, + }, + /* Unused */ + [SENSOR_CONFIG_EC_S3] = { + .odr = 0, + .ec_rate = 0, + }, + [SENSOR_CONFIG_EC_S5] = { + .odr = 0, + .ec_rate = 0, + }, + }, }, - {.name = "Mag", - .active_mask = SENSOR_ACTIVE_S0_S3, - .chip = MOTIONSENSE_CHIP_BMI160, - .type = MOTIONSENSE_TYPE_MAG, - .location = MOTIONSENSE_LOC_LID, - .drv = &bmi160_drv, - .mutex = &g_mutex, - .drv_data = &g_bmi160_data, - .addr = BMI160_ADDR0, - .rot_standard_ref = &mag_standard_ref, - .default_range = 1 << 11, /* 16LSB / uT, fixed */ - .config = { - /* AP: by default shutdown all sensors */ - [SENSOR_CONFIG_AP] = { - .odr = 0, - .ec_rate = 0, - }, - /* EC does not need compass in S0 */ - [SENSOR_CONFIG_EC_S0] = { - .odr = 0, - .ec_rate = 0, - }, - /* Unused */ - [SENSOR_CONFIG_EC_S3] = { - .odr = 0, - .ec_rate = 0, - }, - [SENSOR_CONFIG_EC_S5] = { - .odr = 0, - .ec_rate = 0, - }, - }, + [RYU_LID_MAG] = { + .name = "Mag", + .active_mask = SENSOR_ACTIVE_S0_S3, + .chip = MOTIONSENSE_CHIP_BMI160, + .type = MOTIONSENSE_TYPE_MAG, + .location = MOTIONSENSE_LOC_LID, + .drv = &bmi160_drv, + .mutex = &g_mutex, + .drv_data = &g_bmi160_data, + .addr = BMI160_ADDR0, + .rot_standard_ref = &mag_standard_ref, + .default_range = 1 << 11, /* 16LSB / uT, fixed */ + .config = { + /* AP: by default shutdown all sensors */ + [SENSOR_CONFIG_AP] = { + .odr = 0, + .ec_rate = 0, + }, + /* EC does not need compass in S0 */ + [SENSOR_CONFIG_EC_S0] = { + .odr = 0, + .ec_rate = 0, + }, + /* Unused */ + [SENSOR_CONFIG_EC_S3] = { + .odr = 0, + .ec_rate = 0, + }, + [SENSOR_CONFIG_EC_S5] = { + .odr = 0, + .ec_rate = 0, + }, + }, }, - {.name = "Light", - .active_mask = SENSOR_ACTIVE_S0_S3_S5, - .chip = MOTIONSENSE_CHIP_SI1141, - .type = MOTIONSENSE_TYPE_LIGHT, - .location = MOTIONSENSE_LOC_LID, - .drv = &si114x_drv, - .mutex = &g_mutex, - .drv_data = &g_si114x_data, - .addr = SI114X_ADDR, - .rot_standard_ref = NULL, - .default_range = 9000, /* 90%: int = 0 - frac = 9000/10000 */ - .config = { - /* AP: by default shutdown all sensors */ - [SENSOR_CONFIG_AP] = { - .odr = 0, - .ec_rate = 0, - }, - /* EC needs sensor for light adaptive brightness */ - [SENSOR_CONFIG_EC_S0] = { - .odr = 1000, - .ec_rate = 1000, - }, - [SENSOR_CONFIG_EC_S3] = { - .odr = 1000, - /* Interrupt driven, for double tap */ - .ec_rate = 0, - }, - [SENSOR_CONFIG_EC_S5] = { - .odr = 1000, - .ec_rate = 0, - }, - }, + [RYU_LID_LIGHT] = { + .name = "Light", + .active_mask = SENSOR_ACTIVE_S0_S3_S5, + .chip = MOTIONSENSE_CHIP_SI1141, + .type = MOTIONSENSE_TYPE_LIGHT, + .location = MOTIONSENSE_LOC_LID, + .drv = &si114x_drv, + .mutex = &g_mutex, + .drv_data = &g_si114x_data, + .addr = SI114X_ADDR, + .rot_standard_ref = NULL, + .default_range = 9000, /* 90%: int = 0 - frac = 9000/10000 */ + .config = { + /* AP: by default shutdown all sensors */ + [SENSOR_CONFIG_AP] = { + .odr = 0, + .ec_rate = 0, + }, + /* EC needs sensor for light adaptive brightness */ + [SENSOR_CONFIG_EC_S0] = { + .odr = 1000, + .ec_rate = 1000, + }, + [SENSOR_CONFIG_EC_S3] = { + .odr = 1000, + /* Interrupt driven, for double tap */ + .ec_rate = 0, + }, + [SENSOR_CONFIG_EC_S5] = { + .odr = 1000, + .ec_rate = 0, + }, + }, }, - {.name = "Proxi", - .active_mask = SENSOR_ACTIVE_S0_S3_S5, - .chip = MOTIONSENSE_CHIP_SI1141, - .type = MOTIONSENSE_TYPE_PROX, - .location = MOTIONSENSE_LOC_LID, - .drv = &si114x_drv, - .mutex = &g_mutex, - .drv_data = &g_si114x_data, - .addr = SI114X_ADDR, - .rot_standard_ref = NULL, - .default_range = 7630, /* Upon testing at desk */ - .config = { - /* AP: by default shutdown all sensors */ - [SENSOR_CONFIG_AP] = { - .odr = 0, - .ec_rate = 0, - }, - /* EC does not need proximity in S0 */ - [SENSOR_CONFIG_EC_S0] = { - .odr = 0, - .ec_rate = 0, - }, - /* Unused */ - [SENSOR_CONFIG_EC_S3] = { - .odr = 0, - .ec_rate = 0, - }, - [SENSOR_CONFIG_EC_S5] = { - .odr = 0, - .ec_rate = 0, - }, - }, + [RYU_LID_PROX] = { + .name = "Prox", + .active_mask = SENSOR_ACTIVE_S0_S3_S5, + .chip = MOTIONSENSE_CHIP_SI1141, + .type = MOTIONSENSE_TYPE_PROX, + .location = MOTIONSENSE_LOC_LID, + .drv = &si114x_drv, + .mutex = &g_mutex, + .drv_data = &g_si114x_data, + .addr = SI114X_ADDR, + .rot_standard_ref = NULL, + .default_range = 7630, /* Upon testing at desk */ + .config = { + /* AP: by default shutdown all sensors */ + [SENSOR_CONFIG_AP] = { + .odr = 0, + .ec_rate = 0, + }, + /* EC does not need proximity in S0 */ + [SENSOR_CONFIG_EC_S0] = { + .odr = 0, + .ec_rate = 0, + }, + /* Unused */ + [SENSOR_CONFIG_EC_S3] = { + .odr = 0, + .ec_rate = 0, + }, + [SENSOR_CONFIG_EC_S5] = { + .odr = 0, + .ec_rate = 0, + }, + }, }, }; const unsigned int motion_sensor_count = ARRAY_SIZE(motion_sensors); diff --git a/board/ryu/board.h b/board/ryu/board.h index 45683d9d82..cad44bc3dd 100644 --- a/board/ryu/board.h +++ b/board/ryu/board.h @@ -218,6 +218,15 @@ enum power_signal { POWER_SIGNAL_COUNT }; +/* Sensor index defintion */ +enum sensor_id { + RYU_LID_ACCEL, + RYU_LID_GYRO, + RYU_LID_MAG, + RYU_LID_LIGHT, + RYU_LID_PROX +}; + /* ADC signal */ enum adc_channel { ADC_VBUS = 0, @@ -271,6 +280,10 @@ void board_set_charge_limit(int charge_ma); /* PP1800 transition GPIO interrupt handler */ void pp1800_on_off_evt(enum gpio_signal signal); +/* ALS sensor is in forced mode */ +#define CONFIG_ACCEL_FORCE_MODE_MASK \ + ((1 << RYU_LID_LIGHT) | (1 << RYU_LID_PROX)) + #endif /* !__ASSEMBLER__ */ #endif /* __CROS_EC_BOARD_H */ diff --git a/common/motion_sense.c b/common/motion_sense.c index 884adf195c..c392e58b03 100644 --- a/common/motion_sense.c +++ b/common/motion_sense.c @@ -34,7 +34,10 @@ /* * Sampling interval for measuring acceleration and calculating lid angle. */ -unsigned accel_interval; +unsigned int motion_interval; + +/* Delay between FIFO interruption. */ +static unsigned int motion_int_interval; #ifdef CONFIG_CMD_ACCEL_INFO static int accel_disp; @@ -46,6 +49,8 @@ static int accel_disp; #define UPDATE_HOST_MEM_MAP #endif + + /* * Mutex to protect sensor values between host command task and * motion sense task: @@ -232,6 +237,22 @@ int motion_sense_set_data_rate(struct motion_sensor_t *sensor) return sensor->drv->set_data_rate(sensor, odr, roundup); } +static inline int motion_sense_select_ec_rate( + const struct motion_sensor_t *sensor, + enum sensor_config config_id) +{ +#ifdef CONFIG_ACCEL_FORCE_MODE_MASK + if (CONFIG_ACCEL_FORCE_MODE_MASK & (1 << (sensor - motion_sensors))) + /* we have to run ec at the sensor frequency rate.*/ + if (sensor->config[config_id].odr > 0) + return 1000000 / sensor->config[config_id].odr; + else + return 0; + else +#endif + return sensor->config[config_id].ec_rate; +} + /* motion_sense_ec_rate * * Calculate the sensor ec rate. It will be use to set the motion task polling @@ -242,14 +263,14 @@ int motion_sense_set_data_rate(struct motion_sensor_t *sensor) static int motion_sense_ec_rate(struct motion_sensor_t *sensor) { int ec_rate = 0, ec_rate_from_cfg; - enum sensor_config config_id = SENSOR_CONFIG_AP; /* Check the AP setting first. */ if (sensor_active != SENSOR_ACTIVE_S5) - ec_rate = sensor->config[SENSOR_CONFIG_AP].ec_rate; + ec_rate = motion_sense_select_ec_rate(sensor, SENSOR_CONFIG_AP); + + ec_rate_from_cfg = motion_sense_select_ec_rate( + sensor, motion_sense_get_ec_config()); - config_id = motion_sense_get_ec_config(); - ec_rate_from_cfg = sensor->config[config_id].ec_rate; if ((ec_rate == 0 && ec_rate_from_cfg != 0) || (ec_rate_from_cfg != 0 && ec_rate_from_cfg < ec_rate)) ec_rate = ec_rate_from_cfg; @@ -257,18 +278,18 @@ static int motion_sense_ec_rate(struct motion_sensor_t *sensor) } /* - * motion_sense_set_accel_interval + * motion_sense_set_motion_intervals * * Set the wake up interval for the motion sense thread. * It is set to the highest frequency one of the sensors need to be polled at. * * Note: Not static to be tested. */ -int motion_sense_set_accel_interval(void) +int motion_sense_set_motion_intervals(void) { - int i, sensor_ec_rate, ec_rate, wake_up = 0; + int i, sensor_ec_rate, ec_rate = 0, ec_int_rate_ms = 0, wake_up = 0; struct motion_sensor_t *sensor; - for (i = 0, ec_rate = 0; i < motion_sensor_count; ++i) { + for (i = 0; i < motion_sensor_count; ++i) { sensor = &motion_sensors[i]; /* * If the sensor is sleeping, no need to check it periodicaly. @@ -278,21 +299,29 @@ int motion_sense_set_accel_interval(void) continue; sensor_ec_rate = motion_sense_ec_rate(sensor); - if ((ec_rate == 0 && sensor_ec_rate != 0) || - (sensor_ec_rate != 0 && sensor_ec_rate < ec_rate)) + if (sensor_ec_rate == 0) + continue; + if (ec_rate == 0 || sensor_ec_rate < ec_rate) ec_rate = sensor_ec_rate; + + sensor_ec_rate = sensor->config[SENSOR_CONFIG_AP].ec_rate; + if (ec_int_rate_ms == 0 || sensor_ec_rate < ec_int_rate_ms) + ec_int_rate_ms = sensor_ec_rate; } /* * Wake up the motion sense task: we want to sensor task to take * in account the new period right away. */ - if (accel_interval == 0 || - (ec_rate > 0 && accel_interval > ec_rate)) + if ((motion_interval == 0 || + (ec_rate > 0 && motion_interval > ec_rate)) || + (motion_int_interval == 0 || + (ec_int_rate_ms > 0 && motion_int_interval > ec_int_rate_ms))) wake_up = 1; - accel_interval = ec_rate; + motion_interval = ec_rate; + motion_int_interval = ec_int_rate_ms * MSEC; if (wake_up) task_wake(TASK_ID_MOTIONSENSE); - return accel_interval; + return motion_interval; } static inline int motion_sense_init(struct motion_sensor_t *sensor) @@ -346,7 +375,7 @@ static void motion_sense_switch_sensor_rate(void) sensor->state = SENSOR_NOT_INITIALIZED; } } - motion_sense_set_accel_interval(); + motion_sense_set_motion_intervals(); } static void motion_sense_shutdown(void) @@ -701,8 +730,9 @@ void motion_sense_task(void) if (fifo_flush_needed || wake_up_needed || event & TASK_EVENT_MOTION_ODR_CHANGE || queue_space(&motion_sense_fifo) < CONFIG_ACCEL_FIFO_THRES || - (accel_interval > 0 && - (ts_end_task.val - ts_last_int.val) > accel_interval)) { + (motion_int_interval > 0 && + time_after(ts_end_task.le.lo, + ts_last_int.le.lo + motion_int_interval))) { if (!fifo_flush_needed) motion_sense_insert_timestamp(); fifo_flush_needed = 0; @@ -721,12 +751,12 @@ void motion_sense_task(void) #endif } #endif - if (accel_interval > 0) { + if (motion_interval > 0) { /* * Delay appropriately to keep sampling time * consistent. */ - wait_us = accel_interval - + wait_us = motion_interval - (ts_end_task.val - ts_begin_task.val); /* @@ -881,7 +911,7 @@ static int host_cmd_motion_sense(struct host_cmd_handler_args *args) MIN_MOTION_SENSE_WAIT_TIME / MSEC); /* Bound the new sampling rate. */ - motion_sense_set_accel_interval(); + motion_sense_set_motion_intervals(); } out->ec_rate.ret = motion_sense_ec_rate(sensor) / MSEC; @@ -918,7 +948,7 @@ static int host_cmd_motion_sense(struct host_cmd_handler_args *args) * suspended, we have to recalculate the EC sampling * rate */ - motion_sense_set_accel_interval(); + motion_sense_set_motion_intervals(); } out->sensor_odr.ret = sensor->drv->get_data_rate(sensor); @@ -1236,13 +1266,14 @@ static int command_accel_data_rate(int argc, char **argv) if (ret) return EC_ERROR_PARAM2; /* Sensor might be out of suspend, check the ec_rate */ - motion_sense_set_accel_interval(); + motion_sense_set_motion_intervals(); } else { ccprintf("Data rate for sensor %d: %d\n", id, sensor->drv->get_data_rate(sensor)); ccprintf("EC rate for sensor %d: %d\n", id, motion_sense_ec_rate(sensor)); - ccprintf("Current EC rate: %d\n", accel_interval); + ccprintf("Current EC rate: %d\n", motion_interval); + ccprintf("Current Interupt rate: %d\n", motion_int_interval); } return EC_SUCCESS; @@ -1342,7 +1373,7 @@ static int command_display_accel_info(int argc, char **argv) if (*e) return EC_ERROR_PARAM2; - accel_interval = val * MSEC; + motion_interval = val * MSEC; task_wake(TASK_ID_MOTIONSENSE); } diff --git a/include/config.h b/include/config.h index 619dd2c04d..c5264c57b7 100644 --- a/include/config.h +++ b/include/config.h @@ -44,6 +44,12 @@ /* The amount of free entries that trigger an interrupt to the AP. */ #undef CONFIG_ACCEL_FIFO_THRES +/* + * Sensors in this mask are in forced mode: they needed to be polled + * at their data rate frequency. + */ +#undef CONFIG_ACCEL_FORCE_MODE_MASK + /* Specify type of accelerometers attached. */ #undef CONFIG_ACCEL_KXCJ9 #undef CONFIG_ACCEL_KX022 diff --git a/include/motion_sense.h b/include/motion_sense.h index ec2219a0e3..ff99c40af4 100644 --- a/include/motion_sense.h +++ b/include/motion_sense.h @@ -137,8 +137,8 @@ extern const unsigned motion_sensor_count; /* For testing purposes: export the sampling interval. */ extern enum chipset_state_mask sensor_active; -extern unsigned accel_interval; -int motion_sense_set_accel_interval(void); +extern unsigned motion_interval; +int motion_sense_set_motion_intervals(void); /* * Priority of the motion sense resume/suspend hooks, to be sure associated diff --git a/test/motion_lid.c b/test/motion_lid.c index ff97662843..ba6a179b1c 100644 --- a/test/motion_lid.c +++ b/test/motion_lid.c @@ -164,7 +164,7 @@ struct motion_sensor_t motion_sensors[] = { }, /* Used for double tap */ [SENSOR_CONFIG_EC_S3] = { - .odr = 119000 | ROUND_UP_FLAG, + .odr = 200000 | ROUND_UP_FLAG, .ec_rate = TEST_LID_EC_RATE * 100, }, [SENSOR_CONFIG_EC_S5] = { @@ -199,14 +199,14 @@ static int test_lid_angle(void) /* Go to S3 state */ TEST_ASSERT(sensor_active == SENSOR_ACTIVE_S5); TEST_ASSERT(accel_get_data_rate(lid) == 0); - TEST_ASSERT(accel_interval == 0); + TEST_ASSERT(motion_interval == 0); /* Go to S0 state */ hook_notify(HOOK_CHIPSET_RESUME); msleep(1000); TEST_ASSERT(sensor_active == SENSOR_ACTIVE_S0); TEST_ASSERT(accel_get_data_rate(lid) == (119000 | ROUND_UP_FLAG)); - TEST_ASSERT(accel_interval == TEST_LID_EC_RATE * MSEC); + TEST_ASSERT(motion_interval == TEST_LID_EC_RATE * MSEC); /* * Set the base accelerometer as if it were sitting flat on a desk |