diff options
author | Randall Spangler <rspangler@chromium.org> | 2012-10-26 14:31:47 -0700 |
---|---|---|
committer | Gerrit <chrome-bot@google.com> | 2012-10-29 16:52:49 -0700 |
commit | 1d916d7c6b25de10c870cf7f35a44d3654d9f099 (patch) | |
tree | 5d7337654d86766324103217ab61ae0b71d65e0f | |
parent | 67aadcf614513353d7c9a0216fc70f7af18d8d05 (diff) | |
download | chrome-ec-1d916d7c6b25de10c870cf7f35a44d3654d9f099.tar.gz |
Use SECOND and MSEC constants
We'd defined them in a number of different files. This moves
definitions to timer.h, and uses them everywhere we have large delays
(since 10*SECOND is less typo-prone than 10000000).
Also add msleep() and sleep() inline functions. No need for mdelay()
or delay(), since any delays that long should use sleep funcs instead
of spin-waiting.
BUG=chrome-os-partner:15579
BRANCH=none
TEST=boot system; taskinfo displays similar numbers to before
Change-Id: I2a92a9f10f46b6b7b6571759b1f8ab4ecfbf8259
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/36726
33 files changed, 231 insertions, 211 deletions
diff --git a/chip/lm4/adc.c b/chip/lm4/adc.c index 254b0903af..b2d3479eef 100644 --- a/chip/lm4/adc.c +++ b/chip/lm4/adc.c @@ -96,7 +96,7 @@ int lm4_adc_flush_and_read(enum lm4_adc_sequencer seq) LM4_ADC_ADCPSSI |= 0x01 << seq; /* Wait for interrupt */ - event = task_wait_event(1000000); + event = task_wait_event(SECOND); task_waiting_on_ss[seq] = TASK_ID_INVALID; if (event == TASK_EVENT_TIMER) return ADC_READ_ERROR; diff --git a/chip/lm4/eeprom.c b/chip/lm4/eeprom.c index ff414ede24..7b28712afd 100644 --- a/chip/lm4/eeprom.c +++ b/chip/lm4/eeprom.c @@ -19,7 +19,6 @@ /* Count of EEPROM blocks */ static int block_count; - /* * Wait for the current EEPROM operation to finish; all operations but write * should normally finish in 4 system clocks, but worst case is up to @@ -32,7 +31,7 @@ static int wait_for_done(void) int j; for (j = 0; j < 20; j++) { /* 20 * 100 ms = 2000 ms */ - uint64_t tstop = get_time().val + 100000; /* 100ms from now */ + uint64_t tstop = get_time().val + 100 * MSEC; while (get_time().val < tstop) { if (!(LM4_EEPROM_EEDONE & 0x01)) return EC_SUCCESS; diff --git a/chip/lm4/flash.c b/chip/lm4/flash.c index 8a9c3dc946..e06246b7ee 100644 --- a/chip/lm4/flash.c +++ b/chip/lm4/flash.c @@ -243,7 +243,7 @@ int flash_physical_erase(int offset, int size) #ifdef CONFIG_TASK_WATCHDOG /* Reload the watchdog timer, so that erasing many flash pages * doesn't cause a watchdog reset. May not need this now that - * we're using usleep() below. */ + * we're using msleep() below. */ watchdog_reload(); #endif @@ -254,7 +254,7 @@ int flash_physical_erase(int offset, int size) for (t = 0; LM4_FLASH_FMC & 0x02; t++) { if (t > ERASE_TIMEOUT_MS) return EC_ERROR_TIMEOUT; - usleep(1000); + msleep(1); } /* Check for error conditions - erase failed, voltage error, diff --git a/chip/lm4/hwtimer.c b/chip/lm4/hwtimer.c index 45166f2194..cbf28f5294 100644 --- a/chip/lm4/hwtimer.c +++ b/chip/lm4/hwtimer.c @@ -5,14 +5,13 @@ /* Hardware timers driver */ -#include "board.h" #include "clock.h" +#include "common.h" #include "hooks.h" #include "hwtimer.h" #include "registers.h" #include "task.h" - -#define US_PER_SECOND 1000000 +#include "timer.h" void __hw_clock_event_set(uint32_t deadline) { @@ -22,63 +21,60 @@ void __hw_clock_event_set(uint32_t deadline) LM4_TIMER_IMR(6) |= 0x10; } - uint32_t __hw_clock_event_get(void) { return 0xffffffff - LM4_TIMER_TAMATCHR(6); } - void __hw_clock_event_clear(void) { /* Disable the match interrupt */ LM4_TIMER_IMR(6) &= ~0x10; } - uint32_t __hw_clock_source_read(void) { return 0xffffffff - LM4_TIMER_TAV(6); } - void __hw_clock_source_set(uint32_t ts) { LM4_TIMER_TAV(6) = 0xffffffff - ts; } - static void __hw_clock_source_irq(void) { uint32_t status = LM4_TIMER_RIS(6); - /* clear interrupt */ + /* Clear interrupt */ LM4_TIMER_ICR(6) = status; /* - * Find expired timers and set the new timer deadline - * get from the IRQ status if the free running counter as overflowed + * Find expired timers and set the new timer deadline; check the IRQ + * status to determine if the free-running counter overflowed. */ process_timers(status & 0x01); } DECLARE_IRQ(LM4_IRQ_TIMERW0A, __hw_clock_source_irq, 1); - static void update_prescaler(void) { - /* Set the prescaler to increment every microsecond. This takes - * effect immediately, because the TAILD bit in TAMR is clear. */ - LM4_TIMER_TAPR(6) = clock_get_freq() / US_PER_SECOND; + /* + * Set the prescaler to increment every microsecond. This takes + * effect immediately, because the TAILD bit in TAMR is clear. + */ + LM4_TIMER_TAPR(6) = clock_get_freq() / SECOND; } DECLARE_HOOK(HOOK_FREQ_CHANGE, update_prescaler, HOOK_PRIO_DEFAULT); - int __hw_clock_source_init(uint32_t start_t) { volatile uint32_t scratch __attribute__((unused)); - /* Use WTIMER0 (timer 6) configured as a free running counter with 1 us - * period */ + /* + * Use WTIMER0 (timer 6) configured as a free running counter with 1 us + * period. + */ /* Enable WTIMER0 clock */ LM4_SYSTEM_RCGCWTIMER |= 1; @@ -101,8 +97,11 @@ int __hw_clock_source_init(uint32_t start_t) LM4_TIMER_TAILR(6) = 0xffffffff; /* Starts counting in timer A */ LM4_TIMER_CTL(6) |= 0x1; - /* Override the count with the start value now that counting has - * started. */ + + /* + * Override the count with the start value now that counting has + * started. + */ __hw_clock_source_set(start_t); /* Enable interrupt */ diff --git a/chip/lm4/i2c.c b/chip/lm4/i2c.c index 0fdefc15d9..7f0fa19c95 100644 --- a/chip/lm4/i2c.c +++ b/chip/lm4/i2c.c @@ -61,7 +61,7 @@ static int wait_idle(int port) * the I2C is either completed or timed out. Refer to the * implementation of usleep() for a similar situation. */ - event |= (task_wait_event(1000000) & ~TASK_EVENT_I2C_IDLE); + event |= (task_wait_event(SECOND) & ~TASK_EVENT_I2C_IDLE); LM4_I2C_MIMR(port) = 0x00; task_waiting_on_port[port] = TASK_ID_INVALID; if (event & TASK_EVENT_TIMER) { diff --git a/chip/lm4/keyboard_scan.c b/chip/lm4/keyboard_scan.c index a07170dcfe..1d25b1937b 100644 --- a/chip/lm4/keyboard_scan.c +++ b/chip/lm4/keyboard_scan.c @@ -24,15 +24,15 @@ #define CPRINTF(format, args...) cprintf(CC_KEYSCAN, format, ## args) /* Time constants */ -#define POLLING_MODE_TIMEOUT 1000000 /* Max time to poll if no keys are down */ -#define DEBOUNCE_UP_US 30000 /* Debounce time for key-up */ -#define DEBOUNCE_DOWN_US 6000 /* Debounce time for key-down */ -#define SCAN_LOOP_DELAY 1000 /* Delay in scan loop */ +#define POLLING_MODE_TIMEOUT SECOND /* Max time to poll if no keys are down */ +#define DEBOUNCE_UP_US (30 * MSEC) /* Debounce time for key-up */ +#define DEBOUNCE_DOWN_US (6 * MSEC) /* Debounce time for key-down */ +#define SCAN_LOOP_DELAY MSEC /* Delay in scan loop */ #define COLUMN_CHARGE_US 40 /* Column charge time in usec */ #define KB_COLS 13 -#define SCAN_TIME_COUNT 32 +#define SCAN_TIME_COUNT 32 /* Number of last scan times to track */ /* Boot key list. Must be in same order as enum boot_key. */ struct boot_key_entry { diff --git a/chip/lm4/lpc.c b/chip/lm4/lpc.c index cc77984638..7563fe1875 100644 --- a/chip/lm4/lpc.c +++ b/chip/lm4/lpc.c @@ -801,7 +801,7 @@ DECLARE_HOOK(HOOK_CHIPSET_RESUME, lpc_resume, HOOK_PRIO_DEFAULT); void lpc_task(void) { while (1) { - usleep(250000); + msleep(250); /* * Make sure pending LPC interrupts have been processed. * This works around a LM4 bug where host writes sometimes diff --git a/chip/lm4/mock_pwm.c b/chip/lm4/mock_pwm.c index c7f8fabe0f..b269b8f116 100644 --- a/chip/lm4/mock_pwm.c +++ b/chip/lm4/mock_pwm.c @@ -65,5 +65,5 @@ void pwm_task(void) { /* Do nothing */ while (1) - usleep(5000000); + sleep(5); } diff --git a/chip/lm4/power_button.c b/chip/lm4/power_button.c index ede3c3d938..d3e130e8f7 100644 --- a/chip/lm4/power_button.c +++ b/chip/lm4/power_button.c @@ -41,17 +41,17 @@ * @S0 make code break code */ /* TODO: link to full power button / lid switch state machine description. */ -#define PWRBTN_DEBOUNCE_US 30000 /* Debounce time for power button */ -#define PWRBTN_DELAY_T0 32000 /* 32ms (PCH requires >16ms) */ -#define PWRBTN_DELAY_T1 (4000000 - PWRBTN_DELAY_T0) /* 4 secs - t0 */ -#define PWRBTN_INITIAL_US 200000 /* Length of time to stretch initial power - * button press to give chipset a chance to - * wake up (~100ms) and react to the press - * (~16ms). Also used as pulse length for - * simulated power button presses when the - * system is off. */ - -#define LID_DEBOUNCE_US 30000 /* Debounce time for lid switch */ +#define PWRBTN_DEBOUNCE_US (30 * MSEC) /* Debounce time for power button */ +#define PWRBTN_DELAY_T0 (32 * MSEC) /* 32ms (PCH requires >16ms) */ +#define PWRBTN_DELAY_T1 (4 * SECOND - PWRBTN_DELAY_T0) /* 4 secs - t0 */ +/* + * Length of time to stretch initial power button press to give chipset a + * chance to wake up (~100ms) and react to the press (~16ms). Also used as + * pulse length for simulated power button presses when the system is off. + */ +#define PWRBTN_INITIAL_US (200 * MSEC) + +#define LID_DEBOUNCE_US (30 * MSEC) /* Debounce time for lid switch */ enum power_button_state { /* Button up; state machine idle */ @@ -591,7 +591,7 @@ void power_button_interrupt(enum gpio_signal signal) static int command_powerbtn(int argc, char **argv) { - int ms = PWRBTN_INITIAL_US / 1000; /* Press duration in ms */ + int ms = PWRBTN_INITIAL_US / MSEC; /* Press duration in ms */ char *e; if (argc > 1) { @@ -605,7 +605,7 @@ static int command_powerbtn(int argc, char **argv) tdebounce_pwr = get_time().val + PWRBTN_DEBOUNCE_US; task_wake(TASK_ID_POWERBTN); - usleep(ms * 1000); + msleep(ms); ccprintf("Simulating power button release.\n"); simulate_power_pressed = 0; diff --git a/chip/lm4/pwm.c b/chip/lm4/pwm.c index 372e008943..e3f359c389 100644 --- a/chip/lm4/pwm.c +++ b/chip/lm4/pwm.c @@ -199,7 +199,7 @@ void pwm_task(void) mapped[0] = pwm_get_fan_rpm(); /* Update about once a second */ - usleep(1000000); + sleep(1); } } diff --git a/chip/stm32/dma.h b/chip/stm32/dma.h index c9832fcfe3..4aed23ff05 100644 --- a/chip/stm32/dma.h +++ b/chip/stm32/dma.h @@ -91,7 +91,7 @@ enum { #define DMA_PSIZE_WORD (2 << 8) #define DMA_POLLING_INTERVAL_US 100 /* us */ -#define DMA_TRANSFER_TIMEOUT_US 100000 /* us */ +#define DMA_TRANSFER_TIMEOUT_US (100 * MSEC) /* us */ /* * Certain DMA channels must be used for certain peripherals and transfer diff --git a/chip/stm32/flash-stm32f100.c b/chip/stm32/flash-stm32f100.c index 0801066b9d..52e267255d 100644 --- a/chip/stm32/flash-stm32f100.c +++ b/chip/stm32/flash-stm32f100.c @@ -17,8 +17,6 @@ #include "util.h" #include "watchdog.h" -#define US_PER_SECOND 1000000 - /* * Approximate number of CPU cycles per iteration of the loop when polling * the flash status @@ -28,7 +26,7 @@ /* Flash page programming timeout. This is 2x the datasheet max. */ #define FLASH_TIMEOUT_US 16000 #define FLASH_TIMEOUT_LOOP \ - (FLASH_TIMEOUT_US * (CPU_CLOCK / US_PER_SECOND) / CYCLE_PER_FLASH_LOOP) + (FLASH_TIMEOUT_US * (CPU_CLOCK / SECOND) / CYCLE_PER_FLASH_LOOP) /* Flash unlocking keys */ #define KEY1 0x45670123 diff --git a/chip/stm32/flash-stm32l15x.c b/chip/stm32/flash-stm32l15x.c index 98d1d7b2d6..cbe7953797 100644 --- a/chip/stm32/flash-stm32l15x.c +++ b/chip/stm32/flash-stm32l15x.c @@ -13,8 +13,6 @@ #include "util.h" #include "watchdog.h" -#define US_PER_SECOND 1000000 - /* * Approximate number of CPU cycles per iteration of the loop when polling * the flash status. @@ -24,7 +22,7 @@ /* Flash page programming timeout. This is 2x the datasheet max. */ #define FLASH_TIMEOUT_US 16000 #define FLASH_TIMEOUT_LOOP \ - (FLASH_TIMEOUT_US * (CPU_CLOCK / US_PER_SECOND) / CYCLE_PER_FLASH_LOOP) + (FLASH_TIMEOUT_US * (CPU_CLOCK / SECOND) / CYCLE_PER_FLASH_LOOP) /* Flash unlocking keys */ #define PEKEY1 0x89ABCDEF diff --git a/chip/stm32/hwtimer.c b/chip/stm32/hwtimer.c index 0fe2168f55..3a58e9dcd1 100644 --- a/chip/stm32/hwtimer.c +++ b/chip/stm32/hwtimer.c @@ -5,20 +5,16 @@ /* Hardware timers driver */ -#include <stdint.h> - -#include "board.h" #include "common.h" #include "hwtimer.h" #include "panic.h" #include "registers.h" #include "task.h" +#include "timer.h" #include "watchdog.h" -#define US_PER_SECOND 1000000 - /* Divider to get microsecond for the clock */ -#define CLOCKSOURCE_DIVIDER (CPU_CLOCK/US_PER_SECOND) +#define CLOCKSOURCE_DIVIDER (CPU_CLOCK / SECOND) #ifdef CHIP_VARIANT_stm32f100 #define TIM_WD_IRQ STM32_IRQ_TIM1_UP_TIM16 @@ -47,10 +43,10 @@ void __hw_clock_event_set(uint32_t deadline) STM32_TIM_DIER(3) |= 2; } /* - * In the unlikely case where the MSB on TIM3 has increased and - * matched the deadline MSB before we set the match interrupt, - * as the STM hardware timer won't trigger an interrupt, we fall back - * to the following LSB event code to set another interrupt. + * In the unlikely case where the MSB on TIM3 has increased and matched + * the deadline MSB before we set the match interrupt, as the STM + * hardware timer won't trigger an interrupt, we fall back to the + * following LSB event code to set another interrupt. */ if ((deadline >> 16) == STM32_TIM_CNT(3)) { /* we can set a match on the LSB only */ @@ -64,10 +60,10 @@ void __hw_clock_event_set(uint32_t deadline) STM32_TIM_DIER(4) |= 2; } /* - * if the LSB deadline is already in the past and won't trigger - * an interrupt, the common code in process_timers will deal with - * the expired timer and automatically set the next deadline, we - * don't need to do anything here. + * If the LSB deadline is already in the past and won't trigger an + * interrupt, the common code in process_timers will deal with the + * expired timer and automatically set the next deadline, we don't need + * to do anything here. */ } @@ -88,7 +84,7 @@ uint32_t __hw_clock_source_read(void) uint32_t hi; uint32_t lo; - /* ensure the two half-words are coherent */ + /* Ensure the two half-words are coherent */ do { hi = STM32_TIM_CNT(3); lo = STM32_TIM_CNT(4); @@ -107,7 +103,7 @@ static void __hw_clock_source_irq(void) { uint32_t stat_tim3 = STM32_TIM_SR(3); - /* clear status */ + /* Clear status */ STM32_TIM_SR(4) = 0; STM32_TIM_SR(3) = 0; @@ -176,10 +172,9 @@ int __hw_clock_source_init(uint32_t start_t) } /* - * We don't have TIM1 on STM32L, so don't support this function for now. - * TIM5 doesn't appear to exist in either variant, and TIM9 cannot be - * triggered as a slave from TIM4. We could perhaps use TIM9 as our - * fast counter on STM32L. + * We don't have TIM1 on STM32L, so don't support this function for now. TIM5 + * doesn't appear to exist in either variant, and TIM9 cannot be triggered as a + * slave from TIM4. We could perhaps use TIM9 as our fast counter on STM32L. */ #ifdef CHIP_VARIANT_stm32f100 @@ -233,11 +228,10 @@ void hwtimer_setup_watchdog(void) timer->smcr = 0x0037; /* - * The auto-reload value is based on the period between rollovers - * for TIM4. Since TIM4 runs at 1MHz, it will overflow in 65.536ms. - * We divide our required watchdog period by this amount to obtain - * the number of times TIM4 can overflow before we generate an - * interrupt. + * The auto-reload value is based on the period between rollovers for + * TIM4. Since TIM4 runs at 1MHz, it will overflow in 65.536ms. We + * divide our required watchdog period by this amount to obtain the + * number of times TIM4 can overflow before we generate an interrupt. */ timer->arr = timer->cnt = WATCHDOG_PERIOD_MS * 1000 / (1 << 16); diff --git a/chip/stm32/i2c.c b/chip/stm32/i2c.c index 4c83ad9678..b42f415273 100644 --- a/chip/stm32/i2c.c +++ b/chip/stm32/i2c.c @@ -28,10 +28,10 @@ #define I2C_FREQ 100000 /* Hz */ /* I2C bit period in microseconds */ -#define I2C_PERIOD_US (1000000 / I2C_FREQ) +#define I2C_PERIOD_US (SECOND / I2C_FREQ) /* Clock divider for I2C controller */ -#define I2C_CCR (CPU_CLOCK/(2 * I2C_FREQ)) +#define I2C_CCR (CPU_CLOCK / (2 * I2C_FREQ)) /* * Transmit timeout in microseconds @@ -41,8 +41,8 @@ * ...but we're going to keep the timeout to make sure we're robust. It may in * fact be needed if the host resets itself mid-read. */ -#define I2C_TX_TIMEOUT_SLAVE 100000 /* us */ -#define I2C_TX_TIMEOUT_MASTER 10000 /* us */ +#define I2C_TX_TIMEOUT_SLAVE (100 * MSEC) +#define I2C_TX_TIMEOUT_MASTER (10 * MSEC) /* * We delay 5us in bitbang mode. That gives us 5us low and 5us high or diff --git a/chip/stm32/power_led.c b/chip/stm32/power_led.c index 09d9d426cd..f5448c0b82 100644 --- a/chip/stm32/power_led.c +++ b/chip/stm32/power_led.c @@ -22,8 +22,8 @@ #include "timer.h" #include "util.h" -#define LED_STATE_TIMEOUT_MIN 15000 /* minimum of 15ms per step */ -#define LED_HOLD_TIME 330000 /* hold for 330ms at min/max */ +#define LED_STATE_TIMEOUT_MIN (15 * MSEC) /* Minimum of 15ms per step */ +#define LED_HOLD_TIME (330 * MSEC) /* Hold for 330ms at min/max */ #define LED_STEP_PERCENT 4 /* incremental value of each step */ static enum powerled_state led_state = POWERLED_STATE_ON; diff --git a/common/battery_precharge.c b/common/battery_precharge.c index 1f8d732f46..10cae97269 100644 --- a/common/battery_precharge.c +++ b/common/battery_precharge.c @@ -86,7 +86,7 @@ static enum power_state go_next_level(struct power_state_context *ctx) * Battery chemical reaction lags behind the charging voltage * change. Delay the charging state machine 2 seconds. */ - usleep(SECOND * 2); + sleep(2); charger_get_voltage(&baseline_voltage); return PWR_STATE_UNCHANGE; @@ -165,7 +165,7 @@ enum power_state trickle_charge(struct power_state_context *ctx) batt->voltage > (binfo->voltage_min * 105 / 100)) { kicking_count++; charger_set_voltage(batt->desired_voltage); - usleep(5 * SECOND); + sleep(5); desired_curr = 0; battery_desired_current(&desired_curr); if (desired_curr >= cinfo->current_min) { @@ -189,7 +189,7 @@ enum power_state trickle_charge(struct power_state_context *ctx) dec_voltage(ctx); if (baseline_voltage > ctx->curr.charging_voltage) baseline_voltage = ctx->curr.charging_voltage; - usleep(SECOND); + sleep(1); reset_data_log(); return PWR_STATE_UNCHANGE; } @@ -225,14 +225,14 @@ enum power_state trickle_charge(struct power_state_context *ctx) if (desired_volt > baseline_voltage) { if (desired_volt > curr->charging_voltage) { inc_voltage(ctx); - usleep(SECOND); + sleep(1); return PWR_STATE_UNCHANGE; } if (desired_volt < (curr->charging_voltage - cinfo->voltage_step)) { dec_voltage(ctx); - usleep(SECOND); + sleep(1); return PWR_STATE_UNCHANGE; } } diff --git a/common/charge_state.c b/common/charge_state.c index 8f82656b7e..09efefffe1 100644 --- a/common/charge_state.c +++ b/common/charge_state.c @@ -194,7 +194,7 @@ static int state_common(struct power_state_context *ctx) charger_set_voltage(ctx->battery->voltage_max); charger_set_current(ctx->charger->current_min); for (d = 0; d < 30; d++) { - usleep(SECOND); + sleep(1); rv = battery_temperature(&batt->temperature); if (rv == 0) { ctx->battery_present = 1; diff --git a/common/gaia_power.c b/common/gaia_power.c index 922b07afbb..20875cb0ce 100644 --- a/common/gaia_power.c +++ b/common/gaia_power.c @@ -42,13 +42,13 @@ #define CPRINTF(format, args...) cprintf(CC_CHIPSET, format, ## args) /* Time necessary for the 5v regulator output to stabilize */ -#define DELAY_5V_SETUP 1000 /* 1ms */ +#define DELAY_5V_SETUP MSEC /* 1ms */ /* Delay between 1.35v and 3.3v rails startup */ #define DELAY_RAIL_STAGGERING 100 /* 100us */ /* Long power key press to force shutdown */ -#define DELAY_FORCE_SHUTDOWN 8000000 /* 8s */ +#define DELAY_FORCE_SHUTDOWN (8 * SECOND) /* * If the power key is pressed to turn on, then held for this long, we @@ -68,11 +68,11 @@ * see XPSHOLD, it waits up to 16sec for an event. If no event occurs * within 16sec, EC powers off AP. */ -#define DELAY_SHUTDOWN_ON_POWER_HOLD (8 * 1000000) -#define DELAY_SHUTDOWN_ON_USB_BOOT (16 * 1000000) +#define DELAY_SHUTDOWN_ON_POWER_HOLD (8 * SECOND) +#define DELAY_SHUTDOWN_ON_USB_BOOT (16 * SECOND) /* Maximum delay after power button press before we release GPIO_PMIC_PWRON_L */ -#define DELAY_RELEASE_PWRON 1000000 /* 1s */ +#define DELAY_RELEASE_PWRON SECOND /* 1s */ /* debounce time to prevent accidental power-on after keyboard power off */ #define KB_PWR_ON_DEBOUNCE 250 /* 250us */ @@ -81,10 +81,10 @@ #define LID_SWITCH_DEBOUNCE 250 /* 250us */ /* PMIC fails to set the LDO2 output */ -#define PMIC_TIMEOUT 100000 /* 100ms */ +#define PMIC_TIMEOUT (100 * MSEC) /* 100ms */ /* Default timeout for input transition */ -#define FAIL_TIMEOUT 500000 /* 500ms */ +#define FAIL_TIMEOUT (500 * MSEC) /* 500ms */ /* Application processor power state */ @@ -538,9 +538,9 @@ static int command_force_power(int argc, char **argv) force_value = 1; /* Wake up the task */ task_wake(TASK_ID_GAIAPOWER); - /* wait 100 ms */ - usleep(100000); - /* release power button */ + /* Wait 100 ms */ + msleep(100); + /* Release power button */ force_signal = -1; force_value = 0; diff --git a/common/lightbar.c b/common/lightbar.c index e2d1b50162..dad881e18e 100644 --- a/common/lightbar.c +++ b/common/lightbar.c @@ -183,7 +183,7 @@ static const struct lightbar_params default_params = { .s0_tick_delay = { 45000, 30000 }, /* battery, AC */ .s0a_tick_delay = { 5000, 3000 }, /* battery, AC */ .s0s3_ramp_down = 2000, - .s3_sleep_for = 5000000, /* between checks */ + .s3_sleep_for = 5 * SECOND, /* between checks */ .s3_ramp_up = 2500, .s3_ramp_down = 10000, @@ -464,11 +464,6 @@ static uint32_t pending_msg; if (TASK_EVENT_CUSTOM(msg) == PENDING_MSG) \ return PENDING_MSG; } while (0) -/* Handy conversions */ -#define MSECS(a) ((a) * 1000) -#define SEC(a) ((a) * 1000000) - - /******************************************************************************/ /* Here are the preprogrammed sequences. */ /******************************************************************************/ @@ -569,7 +564,7 @@ static uint32_t sequence_S0(void) /* Only check the battery state every few seconds. The battery * charging task doesn't update as quickly as we do, and isn't * always valid for a bit after jumping from RO->RW. */ - tick = (now.le.lo - start.le.lo) / SEC(1); + tick = (now.le.lo - start.le.lo) / SECOND; if (tick % 4 == 3 && tick != last_tick) { get_battery_level(); last_tick = tick; @@ -845,7 +840,7 @@ static uint32_t sequence_ERROR(void) lightbar_setrgb(2, 0, 255, 255); lightbar_setrgb(3, 255, 255, 255); - WAIT_OR_RET(SEC(10)); + WAIT_OR_RET(10 * SECOND); return 0; } diff --git a/common/mock_i8042.c b/common/mock_i8042.c index a8c1a01447..01d258c3c0 100644 --- a/common/mock_i8042.c +++ b/common/mock_i8042.c @@ -19,7 +19,7 @@ void i8042_command_task(void) { /* Do nothing */ while (1) - usleep(5000000); + sleep(5); } enum ec_error_list i8042_send_to_host(int len, const uint8_t *bytes) diff --git a/common/mock_temp_sensor.c b/common/mock_temp_sensor.c index 7f2a6b1dd9..3373e299a5 100644 --- a/common/mock_temp_sensor.c +++ b/common/mock_temp_sensor.c @@ -5,13 +5,14 @@ /* Mock temperature sensor module for Chrome EC */ -#include "board.h" +#include "common.h" #include "console.h" #include "temp_sensor.h" #include "timer.h" #include "util.h" -/* Defined in board_temp_sensor.c. Must be in the same order as +/* + * 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]; @@ -35,7 +36,7 @@ void temp_sensor_task(void) { /* Do nothing */ while (1) - usleep(5000000); + sleep(5); } diff --git a/common/mock_x86_power.c b/common/mock_x86_power.c index ac7f969bbc..ddf99127e3 100644 --- a/common/mock_x86_power.c +++ b/common/mock_x86_power.c @@ -84,7 +84,7 @@ void x86_power_task(void) { /* Do nothing */ while (1) - usleep(5000000); + sleep(5); } diff --git a/common/pmu_tps65090_charger.c b/common/pmu_tps65090_charger.c index cfc218d7e5..a0575820a0 100644 --- a/common/pmu_tps65090_charger.c +++ b/common/pmu_tps65090_charger.c @@ -30,10 +30,10 @@ /* Maximum retry count to revive a extremely low charge battery */ #define PRE_CHARGING_RETRY 3 -/* Time delay in usec for idle, charging and discharging. - * Defined in battery charging flow. +/* + * Time delay in usec for idle, charging and discharging. Defined in battery + * charging flow. */ -#define SECOND (1000 * 1000) #define T1_OFF_USEC (60 * SECOND) #define T1_SUSPEND_USEC (60 * SECOND) #define T1_USEC (5 * SECOND) @@ -120,7 +120,7 @@ static int notify_battery_low(void) if (chipset_in_state(CHIPSET_STATE_ON)) { now = get_time(); - if (now.val - last_notify_time.val > 60000000) { + if (now.val - last_notify_time.val > MINUTE) { CPUTS("[pmu] notify battery low (< 10%)\n"); last_notify_time = now; /* TODO(rongchang): notify AP ? */ @@ -467,12 +467,11 @@ void pmu_charger_task(void) } /* - * Throttle the charging loop. If previous loop was waked up - * by an event, sleep 0.5 seconds instead of wait for next - * event. + * Throttle the charging loop. If previous loop awakened due to + * an event, sleep 500 ms instead of waiting for next event. */ if (event & TASK_EVENT_WAKE) { - usleep(0.5 * SECOND); + msleep(500); event = 0; } else { event = task_wait_event(wait_time); diff --git a/common/smart_battery.c b/common/smart_battery.c index ee5c138946..0438d6309f 100644 --- a/common/smart_battery.c +++ b/common/smart_battery.c @@ -79,7 +79,7 @@ int battery_time_at_rate(int rate, int *minutes) return EC_SUCCESS; } else { /* wait 10ms for AT_RATE_OK */ - usleep(10000); + msleep(10); } } return EC_ERROR_TIMEOUT; diff --git a/common/temp_sensor.c b/common/temp_sensor.c index ecbe6c0825..6cfbb8cfee 100644 --- a/common/temp_sensor.c +++ b/common/temp_sensor.c @@ -119,7 +119,7 @@ void temp_sensor_task(void) while (1) { for (i = 0; i < 4; ++i) { - usleep(250000); + msleep(250); poll_fast_sensors(); } poll_slow_sensors(); diff --git a/common/thermal.c b/common/thermal.c index d280c4614c..907743bcdd 100644 --- a/common/thermal.c +++ b/common/thermal.c @@ -208,7 +208,7 @@ void thermal_task(void) { while (1) { thermal_process(); - usleep(1000000); + usleep(SECOND); } } diff --git a/common/x86_power.c b/common/x86_power.c index 2424508396..7b8b492067 100644 --- a/common/x86_power.c +++ b/common/x86_power.c @@ -26,10 +26,10 @@ * Default timeout in us; if we've been waiting this long for an input * transition, just jump to the next state. */ -#define DEFAULT_TIMEOUT 1000000 +#define DEFAULT_TIMEOUT SECOND /* Timeout for dropping back from S5 to G3 */ -#define S5_INACTIVITY_TIMEOUT 10000000 +#define S5_INACTIVITY_TIMEOUT (10 * SECOND) enum x86_state { X86_G3 = 0, /* @@ -512,14 +512,14 @@ void x86_power_task(void) * Wait 10ms after +3VALW good, since that powers * VccDSW and VccSUS. */ - usleep(10000); + msleep(10); /* Assert DPWROK, deassert RSMRST# */ gpio_set_level(GPIO_PCH_DPWROK, 1); gpio_set_level(GPIO_PCH_RSMRSTn, 1); /* Wait 5ms for SUSCLK to stabilize */ - usleep(5000); + msleep(5); state = X86_S5; break; @@ -581,7 +581,7 @@ void x86_power_task(void) hook_notify(HOOK_CHIPSET_RESUME); /* Wait 99ms after all voltages good */ - usleep(99000); + msleep(99); /* * Throttle CPU if necessary. This should only be @@ -737,7 +737,7 @@ static int command_hibernation_delay(int argc, char **argv) { char *e; uint32_t time_g3 = ((uint32_t)(get_time().val - last_shutdown_time)) - / 1000000; + / SECOND; if (argc >= 2) { uint32_t s = strtoi(argv[1], &e, 0); diff --git a/core/cortex-m/timer.c b/core/cortex-m/timer.c index 5038706a24..de4310a88e 100644 --- a/core/cortex-m/timer.c +++ b/core/cortex-m/timer.c @@ -15,24 +15,25 @@ #include "task.h" #include "timer.h" -/* high word of the 64-bit timestamp counter */ +#define TIMER_SYSJUMP_TAG 0x4d54 /* "TM" */ + +/* High word of the 64-bit timestamp counter */ static volatile uint32_t clksrc_high; -/* bitmap of currently running timers */ +/* Bitmap of currently running timers */ static uint32_t timer_running = 0; -/* deadlines of all timers */ +/* Deadlines of all timers */ static timestamp_t timer_deadline[TASK_ID_COUNT]; static uint32_t next_deadline = 0xffffffff; /* Hardware timer routine IRQ number */ static int timer_irq; - static void expire_timer(task_id_t tskid) { /* we are done with this timer */ - atomic_clear(&timer_running, 1<<tskid); + atomic_clear(&timer_running, 1 << tskid); /* wake up the taks waiting for this timer */ task_set_event(tskid, TASK_EVENT_TIMER, 0); } @@ -59,7 +60,7 @@ void process_timers(int overflow) clksrc_high++; do { - next.val = 0xffffffffffffffff; + next.val = -1ull; now = get_time(); do { /* read atomically the current state of timer running */ @@ -93,7 +94,6 @@ void process_timers(int overflow) } while (next.val <= get_time().val); } - void udelay(unsigned us) { timestamp_t deadline = get_time(); @@ -102,7 +102,6 @@ void udelay(unsigned us) while (get_time().val < deadline.val) {} } - int timer_arm(timestamp_t tstamp, task_id_t tskid) { ASSERT(tskid < TASK_ID_COUNT); @@ -113,7 +112,7 @@ int timer_arm(timestamp_t tstamp, task_id_t tskid) timer_deadline[tskid] = tstamp; atomic_or(&timer_running, 1<<tskid); - /* modify the next event if needed */ + /* Modify the next event if needed */ if ((tstamp.le.hi < clksrc_high) || ((tstamp.le.hi == clksrc_high) && (tstamp.le.lo <= next_deadline))) task_trigger_irq(timer_irq); @@ -121,20 +120,17 @@ int timer_arm(timestamp_t tstamp, task_id_t tskid) return EC_SUCCESS; } - -int timer_cancel(task_id_t tskid) +void timer_cancel(task_id_t tskid) { ASSERT(tskid < TASK_ID_COUNT); - atomic_clear(&timer_running, 1<<tskid); - /* don't bother about canceling the interrupt: - * it would be slow, just do it on the next IT + atomic_clear(&timer_running, 1 << tskid); + /* + * Don't need to cancel the interrupt: it would be slow, just do it on + * the next IT */ - - return EC_SUCCESS; } - void usleep(unsigned us) { uint32_t evt = 0; @@ -149,13 +145,13 @@ void usleep(unsigned us) do { evt |= task_wait_event(us); } while (!(evt & TASK_EVENT_TIMER)); - /* re-queue other events which happened in the meanwhile */ + + /* Re-queue other events which happened in the meanwhile */ if (evt) atomic_or(task_get_event_bitmap(task_get_current()), evt & ~TASK_EVENT_TIMER); } - timestamp_t get_time(void) { timestamp_t ts; @@ -168,7 +164,6 @@ timestamp_t get_time(void) return ts; } - void force_time(timestamp_t ts) { clksrc_high = ts.le.hi; @@ -200,6 +195,34 @@ void timer_print_info(void) } } +void timer_init(void) +{ + const timestamp_t *ts; + int size, version; + + BUILD_ASSERT(TASK_ID_COUNT < sizeof(timer_running) * 8); + + /* Restore time from before sysjump */ + ts = (const timestamp_t *)system_get_jump_tag(TIMER_SYSJUMP_TAG, + &version, &size); + if (ts && version == 1 && size == sizeof(timestamp_t)) { + clksrc_high = ts->le.hi; + timer_irq = __hw_clock_source_init(ts->le.lo); + } else { + clksrc_high = 0; + timer_irq = __hw_clock_source_init(0); + } +} + +/* Preserve time across a sysjump */ +static void timer_sysjump(void) +{ + timestamp_t ts = get_time(); + + system_add_jump_tag(TIMER_SYSJUMP_TAG, 1, sizeof(ts), &ts); +} +DECLARE_HOOK(HOOK_SYSJUMP, timer_sysjump, HOOK_PRIO_DEFAULT); + static int command_wait(int argc, char **argv) { char *e; @@ -221,7 +244,6 @@ DECLARE_CONSOLE_COMMAND(waitms, command_wait, "Busy-wait for msec", NULL); - static int command_get_time(int argc, char **argv) { timestamp_t ts = get_time(); @@ -234,45 +256,13 @@ DECLARE_CONSOLE_COMMAND(gettime, command_get_time, "Print current time", NULL); - int command_timer_info(int argc, char **argv) { timer_print_info(); + return EC_SUCCESS; } DECLARE_CONSOLE_COMMAND(timerinfo, command_timer_info, NULL, "Print timer info", NULL); - - -#define TIMER_SYSJUMP_TAG 0x4d54 /* "TM" */ - -/* Preserve time across a sysjump */ -static void timer_sysjump(void) -{ - timestamp_t ts = get_time(); - system_add_jump_tag(TIMER_SYSJUMP_TAG, 1, sizeof(ts), &ts); -} -DECLARE_HOOK(HOOK_SYSJUMP, timer_sysjump, HOOK_PRIO_DEFAULT); - -int timer_init(void) -{ - const timestamp_t *ts; - int size, version; - - BUILD_ASSERT(TASK_ID_COUNT < sizeof(timer_running) * 8); - - /* Restore time from before sysjump */ - ts = (const timestamp_t *)system_get_jump_tag(TIMER_SYSJUMP_TAG, - &version, &size); - if (ts && version == 1 && size == sizeof(timestamp_t)) { - clksrc_high = ts->le.hi; - timer_irq = __hw_clock_source_init(ts->le.lo); - } else { - clksrc_high = 0; - timer_irq = __hw_clock_source_init(0); - } - - return EC_SUCCESS; -} diff --git a/core/cortex-m/watchdog.c b/core/cortex-m/watchdog.c index 36e0ae43e1..fe8ba776bf 100644 --- a/core/cortex-m/watchdog.c +++ b/core/cortex-m/watchdog.c @@ -44,7 +44,7 @@ void watchdog_trace(uint32_t excep_lr, uint32_t excep_sp) void watchdog_task(void) { while (1) { - usleep(WATCHDOG_RELOAD_MS * 1000); + msleep(WATCHDOG_RELOAD_MS); watchdog_reload(); } } diff --git a/include/charge_state.h b/include/charge_state.h index eda78abfd9..0a4cd869da 100644 --- a/include/charge_state.h +++ b/include/charge_state.h @@ -9,12 +9,6 @@ #ifndef __CROS_EC_CHARGE_STATE_H #define __CROS_EC_CHARGE_STATE_H -/* Time constants */ -#define MSEC (1000ULL) -#define SECOND (MSEC * 1000) -#define MINUTE (SECOND * 60) -#define HOUR (MINUTE * 60) - /* Update period to prevent charger watchdog timeout */ #define CHARGER_UPDATE_PERIOD (SECOND * 10) diff --git a/include/timer.h b/include/timer.h index 0a4273ff25..b13e0a62a0 100644 --- a/include/timer.h +++ b/include/timer.h @@ -11,6 +11,12 @@ #include "common.h" #include "task_id.h" +/* Time units in microseconds */ +#define MSEC 1000 +#define SECOND 1000000 +#define MINUTE 60000000 +#define HOUR 3600000000ull /* Too big to fit in a signed int */ + /* Microsecond timestamp. */ typedef union { uint64_t val; @@ -20,15 +26,27 @@ typedef union { } le /* little endian words */; } timestamp_t; -/* Initializes the Timer module. */ -int timer_init(void); +/** + * Initialize the timer module. + */ +void timer_init(void); -/* Launch a one-shot timer for task <tskid> which expires at timestamp - * <tstamp>. */ +/** + * Launch a one-shot timer for a task. + * + * Note that each task can have only a single active timer. + * + * @param tstamp Expiration timestamp for timer + * @param tskid Task to set timer for + * + * @return EC_SUCCESS, or non-zero if error. + */ int timer_arm(timestamp_t tstamp, task_id_t tskid); -/* Cancel a running timer for the specified task id. */ -int timer_cancel(task_id_t tskid); +/** + * Cancel a running timer for the specified task id. + */ +void timer_cancel(task_id_t tskid); /** * Check if a timestamp has passed / expired @@ -39,30 +57,67 @@ int timer_cancel(task_id_t tskid); */ int timestamp_expired(timestamp_t deadline, const timestamp_t *now); -/* Busy-wait the selected number of microseconds. Note that calling this - * with us>1000 may impact system performance; use usleep for longer delays. */ +/** + * Busy-wait. + * + * Note that calling this with us>1000 may impact system performance; use + * usleep() for longer delays. + * + * @param us Number of microseconds to delay. + */ void udelay(unsigned us); -/* Sleep during the selected number of microseconds. The current task will be - * de-scheduled until the delay expires. +/** + * Sleep. + * + * The current task will be de-scheduled for at least the specified delay (and + * perhaps longer, if a higher-priority task is running when the delay + * expires). * - * Note: if an event happens before the end of sleep, the function will return. + * @param us Number of microseconds to sleep. */ void usleep(unsigned us); -/* Get the current timestamp from the system timer. */ +/** + * Sleep for milliseconds + * + * @param ms Number of milliseconds to sleep. + */ +static inline void msleep(unsigned ms) +{ + usleep(ms * MSEC); +} + +/** + * Sleep for seconds + * + * @param sec Number of seconds to sleep. + */ +static inline void sleep(unsigned sec) +{ + usleep(sec * SECOND); +} + +/** + * Get the current timestamp from the system timer. + */ timestamp_t get_time(void); -/* Force the current value of the system timer. +/** + * Force the current value of the system timer. * * This function is for the power management implementation which wants to fix * the system time when waking up from a mode with clocks turned off. + * * Note: must be called with interrupts disabled. */ void force_time(timestamp_t ts); -/* Print the current timer information using the command output channel. This - * may be called from interrupt level. */ +/** + * Print the current timer information using the command output channel. + * + * This may be called from interrupt level. + */ void timer_print_info(void); /** diff --git a/test/powerdemo.c b/test/powerdemo.c index 496459f9f7..a5e79e0366 100644 --- a/test/powerdemo.c +++ b/test/powerdemo.c @@ -5,16 +5,14 @@ /* Power state machine demo module for Chrome EC */ -#include "board.h" #include "clock.h" +#include "common.h" #include "powerdemo.h" #include "task.h" #include "timer.h" #include "uart.h" #include "registers.h" -#define US_PER_SECOND 1000000 - static volatile enum { POWER_STATE_IDLE = 0, /* Idle */ POWER_STATE_DOWN1, /* Assert output for 1ms */ @@ -75,7 +73,7 @@ int power_demo_init(void) /* 32-bit timer mode */ LM4_TIMER_CFG(7) = 4; /* Set the prescaler to increment every microsecond */ - LM4_TIMER_TAPR(7) = clock_get_freq() / US_PER_SECOND; + LM4_TIMER_TAPR(7) = clock_get_freq() / SECOND; /* One-shot, counting down */ LM4_TIMER_TAMR(7) = 0x01; /* Set overflow interrupt */ |