Switch temp sensor polling to use hooks instead of task

This reduces memory / code size, and gets rid of ifdefs in temp_sensor.c.

BUG=chrome-os-partner:15714
BRANCH=none
TEST=boot system and run 'ectool temps all' every few seconds
    - ectool temps all
The numbers should update over time.

Change-Id: Idaac7e6e4cbc1d6689f5d3b607c623a5cc536a4f
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/36940
This commit is contained in:
Randall Spangler
2012-10-26 16:33:30 -07:00
committed by Gerrit
parent 742ec5a1ff
commit d83f42bdc8
14 changed files with 61 additions and 104 deletions

View File

@@ -25,6 +25,7 @@
#define CONFIG_POWER_LED
#define CONFIG_PWM
#define CONFIG_TASK_PROFILING
#define CONFIG_TEMP_SENSOR
#define CONFIG_TMP006
#define CONFIG_USB_CHARGE

View File

@@ -35,7 +35,7 @@ const struct temp_sensor_t temp_sensors[TEMP_SENSOR_COUNT] = {
{"I2C-Charger D-Die", TEMP_SENSOR_TYPE_BOARD, tmp006_get_val, 6, 7},
{"I2C-Charger D-Object", TEMP_SENSOR_TYPE_CASE, tmp006_get_val, 7, 7},
#endif
#ifdef CONFIG_TASK_TEMPSENSOR
#ifdef CONFIG_ADC
{"ECInternal", TEMP_SENSOR_TYPE_BOARD, chip_temp_sensor_get_val, 0, 4},
#endif
#ifdef CONFIG_PECI

View File

@@ -19,7 +19,6 @@
TASK(VBOOTHASH, vboot_hash_task, NULL, LARGER_TASK_STACK_SIZE) \
TASK(LIGHTBAR, lightbar_task, NULL, TASK_STACK_SIZE) \
TASK(CHARGER, charge_state_machine_task, NULL, TASK_STACK_SIZE) \
TASK(TEMPSENSOR, temp_sensor_task, NULL, TASK_STACK_SIZE) \
TASK(THERMAL, thermal_task, NULL, TASK_STACK_SIZE) \
TASK(TYPEMATIC, keyboard_typematic_task, NULL, TASK_STACK_SIZE) \
TASK(X86POWER, x86_power_task, NULL, TASK_STACK_SIZE) \

View File

@@ -13,7 +13,7 @@ CORE:=cortex-m
chip-y=clock.o gpio.o hwtimer.o jtag.o system.o uart.o
# Optional chip modules
chip-$(CONFIG_ADC)+=adc.o
chip-$(CONFIG_ADC)+=adc.o chip_temp_sensor.o
chip-$(CONFIG_EEPROM)+=eeprom.o
chip-$(CONFIG_FLASH)+=flash.o
chip-$(CONFIG_I2C)+=i2c.o
@@ -24,5 +24,4 @@ chip-$(CONFIG_PWM)+=pwm.o
chip-$(CONFIG_SPI)+=spi.o
chip-$(CONFIG_TASK_KEYSCAN)+=keyboard_scan.o keyboard_scan_stub.o
chip-$(CONFIG_TASK_SWITCH)+=switch.o
chip-$(CONFIG_TASK_TEMPSENSOR)+=chip_temp_sensor.o
chip-$(CONFIG_WATCHDOG)+=watchdog.o

View File

@@ -7,18 +7,17 @@
#include "adc.h"
#include "common.h"
#include "hooks.h"
#include "lm4_adc.h"
#include "temp_sensor.h"
/* Initialize temperature reading to a sane value (27 C) */
static int last_val = 300;
int chip_temp_sensor_poll(void)
static void chip_temp_sensor_poll(void)
{
last_val = adc_read_channel(ADC_CH_EC_TEMP);
return EC_SUCCESS;
}
DECLARE_HOOK(HOOK_SECOND, chip_temp_sensor_poll, HOOK_PRIO_DEFAULT);
int chip_temp_sensor_get_val(int idx, int *temp_ptr)
{

View File

@@ -40,7 +40,9 @@
static int temp_vals[TEMP_AVG_LENGTH];
static int temp_idx = 0;
/* Configures the GPIOs for the PECI module. */
/**
* Configure the GPIOs for the PECI module.
*/
static void configure_gpios(void)
{
/* PJ6 alternate function 1 = PECI Tx */
@@ -50,7 +52,6 @@ static void configure_gpios(void)
LM4_GPIO_DEN(LM4_GPIO_J) &= ~0x80;
}
int peci_get_cpu_temp(void)
{
int v = LM4_PECI_M0D0 & 0xffff;
@@ -61,15 +62,6 @@ int peci_get_cpu_temp(void)
return v >> 6;
}
int peci_temp_sensor_poll(void)
{
temp_vals[temp_idx] = peci_get_cpu_temp();
temp_idx = (temp_idx + 1) & (TEMP_AVG_LENGTH - 1);
return EC_SUCCESS;
}
int peci_temp_sensor_get_val(int idx, int *temp_ptr)
{
int sum = 0;
@@ -93,6 +85,13 @@ int peci_temp_sensor_get_val(int idx, int *temp_ptr)
return EC_SUCCESS;
}
static void peci_temp_sensor_poll(void)
{
temp_vals[temp_idx] = peci_get_cpu_temp();
temp_idx = (temp_idx + 1) & (TEMP_AVG_LENGTH - 1);
}
DECLARE_HOOK(HOOK_TICK, peci_temp_sensor_poll, HOOK_PRIO_DEFAULT);
static void peci_freq_changed(void)
{
int freq = clock_get_freq();
@@ -101,8 +100,10 @@ static void peci_freq_changed(void)
/* Disable polling while reconfiguring */
LM4_PECI_CTL = 0;
/* Calculate baud setting from desired rate, compensating for internal
* and external delays. */
/*
* Calculate baud setting from desired rate, compensating for internal
* and external delays.
*/
baud = freq / (4 * PECI_BAUD_RATE) - 2;
baud -= (freq / 1000000) * (PECI_TD_FET_NS + PECI_TD_INT_NS) / 1000;
@@ -117,27 +118,6 @@ static void peci_freq_changed(void)
}
DECLARE_HOOK(HOOK_FREQ_CHANGE, peci_freq_changed, HOOK_PRIO_DEFAULT - 1);
/*****************************************************************************/
/* Console commands */
static int command_peci_temp(int argc, char **argv)
{
int t = peci_get_cpu_temp();
if (t == -1) {
ccprintf("PECI error 0x%04x\n", LM4_PECI_M0D0 & 0xffff);
return EC_ERROR_UNKNOWN;
}
ccprintf("CPU temp = %d K = %d C\n", t, t - 273);
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(pecitemp, command_peci_temp,
NULL,
"Print CPU temperature",
NULL);
/*****************************************************************************/
/* Initialization */
static void peci_init(void)
{
volatile uint32_t scratch __attribute__((unused));
@@ -158,3 +138,21 @@ static void peci_init(void)
temp_vals[i] = 300; /* 27 C */
}
DECLARE_HOOK(HOOK_INIT, peci_init, HOOK_PRIO_DEFAULT);
/*****************************************************************************/
/* Console commands */
static int command_peci_temp(int argc, char **argv)
{
int t = peci_get_cpu_temp();
if (t == -1) {
ccprintf("PECI error 0x%04x\n", LM4_PECI_M0D0 & 0xffff);
return EC_ERROR_UNKNOWN;
}
ccprintf("CPU temp = %d K = %d C\n", t, t - 273);
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(pecitemp, command_peci_temp,
NULL,
"Print CPU temperature",
NULL);

View File

@@ -28,9 +28,9 @@ common-$(CONFIG_TASK_GAIAPOWER)+=gaia_power.o
common-$(CONFIG_TASK_HOSTCMD)+=host_command.o host_event_commands.o
common-$(CONFIG_TASK_I8042CMD)+=i8042.o keyboard.o
common-$(CONFIG_TASK_LIGHTBAR)+=lightbar.o
common-$(CONFIG_TASK_TEMPSENSOR)+=temp_sensor.o
common-$(CONFIG_TASK_THERMAL)+=thermal.o
common-$(CONFIG_TASK_VBOOTHASH)+=sha256.o vboot_hash.o
common-$(CONFIG_TASK_X86POWER)+=x86_power.o
common-$(CONFIG_TEMP_SENSOR)+=temp_sensor.o
common-$(CONFIG_TMP006)+=tmp006.o
common-$(CONFIG_USB_CHARGE)+=usb_charge.o

View File

@@ -11,6 +11,7 @@
#include "console.h"
#include "gpio.h"
#include "i2c.h"
#include "hooks.h"
#include "host_command.h"
#include "peci.h"
#include "task.h"
@@ -34,25 +35,6 @@ int temp_sensor_read(enum temp_sensor_id id, int *temp_ptr)
return sensor->read(sensor->idx, temp_ptr);
}
void poll_slow_sensors(void)
{
/* Poll every second */
#ifdef CONFIG_TMP006
tmp006_poll();
#endif
#ifdef CHIP_lm4
chip_temp_sensor_poll();
#endif
}
static void poll_fast_sensors(void)
{
/* Poll every 1/4 second */
#ifdef CONFIG_PECI
peci_temp_sensor_poll();
#endif
}
static void update_mapped_memory(void)
{
int i, t;
@@ -84,8 +66,10 @@ static void update_mapped_memory(void)
}
}
}
/* Run after other tick tasks, so sensors will have updated first. */
DECLARE_HOOK(HOOK_SECOND, update_mapped_memory, HOOK_PRIO_DEFAULT + 1);
void temp_sensor_task(void)
static void temp_sensor_init(void)
{
int i;
uint8_t *base, *base_b;
@@ -116,16 +100,8 @@ void temp_sensor_task(void)
/* Temp sensor data is present, with B range supported. */
*host_get_memmap(EC_MEMMAP_THERMAL_VERSION) = 2;
while (1) {
for (i = 0; i < 4; ++i) {
msleep(250);
poll_fast_sensors();
}
poll_slow_sensors();
update_mapped_memory();
}
}
DECLARE_HOOK(HOOK_INIT, temp_sensor_init, HOOK_PRIO_DEFAULT);
/*****************************************************************************/
/* Console commands */

View File

@@ -232,24 +232,18 @@ int tmp006_get_val(int idx, int *temp_ptr)
return tmp006_read_object_temp(tdata, temp_ptr);
}
int tmp006_poll(void)
{
int i;
int rv;
int rv1 = EC_SUCCESS;
for (i = 0; i < TMP006_COUNT; ++i) {
rv = tmp006_poll_sensor(i);
if (rv != EC_SUCCESS)
rv1 = rv;
}
return rv1;
}
/*****************************************************************************/
/* Hooks */
static void tmp006_poll(void)
{
int i;
for (i = 0; i < TMP006_COUNT; ++i)
tmp006_poll_sensor(i);
}
DECLARE_HOOK(HOOK_SECOND, tmp006_poll, HOOK_PRIO_DEFAULT);
static void tmp006_init(void)
{
int i;

View File

@@ -8,11 +8,6 @@
#ifndef __CROS_EC_CHIP_TEMP_SENSOR_H
#define __CROS_EC_CHIP_TEMP_SENSOR_H
struct temp_sensor_t;
/* Temperature polling function. */
int chip_temp_sensor_poll(void);
/**
* Get the last polled value of the sensor.
*

View File

@@ -10,10 +10,14 @@
#include "common.h"
/* Return the current CPU temperature in degrees K, or -1 if error.
/**
* Get the current CPU temperature.
*
* Note that the PECI interface is currently a little flaky; if you get an
* error, retry a bit later. */
* error, retry a bit later.
*
* @return the CPU temperature in degrees K, or -1 if error.
*/
int peci_get_cpu_temp(void);
/**
@@ -26,7 +30,4 @@ int peci_get_cpu_temp(void);
*/
int peci_temp_sensor_get_val(int idx, int *temp_ptr);
/* Temperature polling of CPU temperature sensor via PECI. */
int peci_temp_sensor_poll(void);
#endif /* __CROS_EC_PECI_H */

View File

@@ -41,11 +41,13 @@ struct temp_sensor_t {
int action_delay_sec;
};
#ifdef CONFIG_TEMP_SENSOR
/*
* Defined in board_temp_sensor.c. Must be in the same order as
* in enum temp_sensor_id.
*/
extern const struct temp_sensor_t temp_sensors[TEMP_SENSOR_COUNT];
#endif
/**
* Get the most recently measured temperature for the sensor.

View File

@@ -17,12 +17,6 @@ struct tmp006_t {
int addr; /* I2C address formed by TMP006_ADDR macro. */
};
/**
* Poll all TMP006 sensors.
*
* @return 0 if successful, non-zero if error. */
int tmp006_poll(void);
/**
* Get the last polled value of a sensor.
*

View File

@@ -17,7 +17,6 @@
#define CONFIG_TASK_LIST \
TASK(TICK, hook_task, NULL, TASK_STACK_SIZE) \
TASK(VBOOTHASH, vboot_hash_task, NULL, TASK_STACK_SIZE) \
TASK(TEMPSENSOR, temp_sensor_task, NULL, TASK_STACK_SIZE) \
TASK(THERMAL, thermal_task, NULL, TASK_STACK_SIZE) \
TASK(X86POWER, x86_power_task, NULL, TASK_STACK_SIZE) \
TASK(HOSTCMD, host_command_task, NULL, TASK_STACK_SIZE) \