mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-12-27 18:25:05 +00:00
Our code base contains a lot of debug messages in this pattern:
CPRINTF("[%T xxx]\n") or ccprintf("[%T xxx]\n")
The strings are taking up spaces in the EC binaries, so let's refactor
this by adding cprints() and ccprints().
cprints() is just like cprintf(), except that it adds the brackets
and the timestamp. ccprints() is equivalent to cprints(CC_CONSOLE, ...)
This saves us hundreds of bytes in EC binaries.
BUG=chromium:374575
TEST=Build and check flash size
BRANCH=None
Change-Id: Ifafe8dc1b80e698b28ed42b70518c7917b49ee51
Signed-off-by: Vic Yang <victoryang@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/200490
Reviewed-by: Randall Spangler <rspangler@chromium.org>
130 lines
3.1 KiB
C
130 lines
3.1 KiB
C
/* Copyright (c) 2014 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.
|
|
*/
|
|
|
|
/* Button module for Chrome EC */
|
|
|
|
#include "button.h"
|
|
#include "common.h"
|
|
#include "console.h"
|
|
#include "gpio.h"
|
|
#include "hooks.h"
|
|
#include "keyboard_protocol.h"
|
|
#include "timer.h"
|
|
#include "util.h"
|
|
|
|
/* Console output macro */
|
|
#define CPRINTS(format, args...) cprints(CC_SWITCH, format, ## args)
|
|
|
|
struct button_state_t {
|
|
uint64_t debounce_time;
|
|
int debounced_pressed;
|
|
};
|
|
|
|
static struct button_state_t state[CONFIG_BUTTON_COUNT];
|
|
|
|
static uint64_t next_deferred_time;
|
|
|
|
/*
|
|
* Whether a button is currently pressed.
|
|
*/
|
|
static int raw_button_pressed(const struct button_config *button)
|
|
{
|
|
int raw_value = gpio_get_level(button->gpio);
|
|
|
|
return button->flags & BUTTON_FLAG_ACTIVE_HIGH ?
|
|
raw_value : !raw_value;
|
|
}
|
|
|
|
/*
|
|
* Button initialization.
|
|
*/
|
|
static void button_init(void)
|
|
{
|
|
int i;
|
|
|
|
CPRINTS("(re)initializing buttons and interrupts");
|
|
next_deferred_time = 0;
|
|
for (i = 0; i < CONFIG_BUTTON_COUNT; i++) {
|
|
state[i].debounced_pressed = raw_button_pressed(&buttons[i]);
|
|
state[i].debounce_time = 0;
|
|
gpio_enable_interrupt(buttons[i].gpio);
|
|
}
|
|
}
|
|
DECLARE_HOOK(HOOK_INIT, button_init, HOOK_PRIO_DEFAULT);
|
|
|
|
/*
|
|
* Handle debounced button changing state.
|
|
*/
|
|
static void button_change_deferred(void)
|
|
{
|
|
int i;
|
|
int new_pressed;
|
|
uint64_t soonest_debounce_time = 0;
|
|
uint64_t time_now = get_time().val;
|
|
|
|
for (i = 0; i < CONFIG_BUTTON_COUNT; i++) {
|
|
/* Skip this button if we are not waiting to debounce */
|
|
if (state[i].debounce_time == 0)
|
|
continue;
|
|
|
|
if (state[i].debounce_time <= time_now) {
|
|
/* Check if the state has changed */
|
|
new_pressed = raw_button_pressed(&buttons[i]);
|
|
if (state[i].debounced_pressed != new_pressed) {
|
|
state[i].debounced_pressed = new_pressed;
|
|
CPRINTS("Button '%s' was %s",
|
|
buttons[i].name, new_pressed ?
|
|
"pressed" : "released");
|
|
#ifdef HAS_TASK_KEYPROTO
|
|
keyboard_update_button(buttons[i].type,
|
|
new_pressed);
|
|
#endif
|
|
}
|
|
|
|
/* Clear the debounce time to stop checking it */
|
|
state[i].debounce_time = 0;
|
|
} else {
|
|
/*
|
|
* Make sure the next deferred call happens on or before
|
|
* each button needs it.
|
|
*/
|
|
soonest_debounce_time = (soonest_debounce_time == 0) ?
|
|
state[i].debounce_time :
|
|
MIN(soonest_debounce_time,
|
|
state[i].debounce_time);
|
|
}
|
|
}
|
|
|
|
if (soonest_debounce_time != 0) {
|
|
next_deferred_time = soonest_debounce_time;
|
|
hook_call_deferred(button_change_deferred,
|
|
next_deferred_time - time_now);
|
|
}
|
|
}
|
|
DECLARE_DEFERRED(button_change_deferred);
|
|
|
|
/*
|
|
* Handle a button interrupt.
|
|
*/
|
|
void button_interrupt(enum gpio_signal signal)
|
|
{
|
|
int i;
|
|
uint64_t time_now = get_time().val;
|
|
|
|
for (i = 0; i < CONFIG_BUTTON_COUNT; i++) {
|
|
if (buttons[i].gpio != signal)
|
|
continue;
|
|
|
|
state[i].debounce_time = time_now + buttons[i].debounce_us;
|
|
if (next_deferred_time <= time_now ||
|
|
next_deferred_time > state[i].debounce_time) {
|
|
next_deferred_time = state[i].debounce_time;
|
|
hook_call_deferred(button_change_deferred,
|
|
next_deferred_time - time_now);
|
|
}
|
|
break;
|
|
}
|
|
}
|