Add ec_keyboard_test.c

This CL also includes 4 pieces:
  1. main files: environment-independent (main.c) and
                            -dependent (ec_keyboard_test.c).
  2. stub files: chip_stub/keyboard.c simulates the real hardware.
                 chip_stub/keyboard.h is the header of testing functions.
  3. initial keyboard lib code: ec_keyboard.h
  4. rename Cr* function name to Ec* under chip_interface/.

BUG=None
TEST=make && make runtests
Expect seeing the output debug message from cros_ec/lib/ec_keyboard.c.

Change-Id: Ia7e8022060eb0a5d0a11abcbdb3f524942f5c68d
This commit is contained in:
Louis Yung-Chieh Lo
2011-10-21 22:10:24 +08:00
parent 360d484d3a
commit 91daca6939
17 changed files with 265 additions and 70 deletions

View File

@@ -1,7 +1,10 @@
/* power_button.h - Power button function.
* Hides chip-specific implementation behind this interface.
/* Copyright (c) 2011 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.
*
* (Chromium license) */
* power_button.h - Power button function.
* Hides chip-specific implementation behind this interface.
*/
#ifndef __CHIP_INTERFACE_AC_PRESENT_H
#define __CHIP_INTERFACE_AC_PRESENT_H
@@ -10,18 +13,18 @@
* Initialize the AC present as GPIO input pin and enable interrupt for
* callback.
*/
EcError CrAcPresentInit(void);
EcError EcAcPresentInit(void);
/* Calls GPIOPinRead() to read the GPIO state. */
/* TODO: has the state been debounced? */
EcError CrAcPrensentState(void);
EcError EcAcPrensentState(void);
/* Register a calback function. It is called while AC is plugged in or
* unplugged.
*/
EcError CrAcPresentRegister(void (*callback)(void));
EcError EcAcPresentRegister(void (*callback)(void));
/* Below is the example code to register this function. */
#if 0

View File

@@ -1,7 +1,10 @@
/* power_button.h - Power button function.
* Hides chip-specific implementation behind this interface.
/* Copyright (c) 2011 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.
*
* (Chromium license) */
* power_button.h - Power button function.
* Hides chip-specific implementation behind this interface.
*/
#ifndef __CHIP_INTERFACE_BATTERY_H
#define __CHIP_INTERFACE_BATTERY_H
@@ -9,26 +12,26 @@
/*********** Battery SMBus *********************************************/
/* Initialize the SMBus */
EcError CrBatteryInit(void);
EcError EcBatteryInit(void);
/* Send a command to battery. Blocking */
EcError CrBatterySendCommand(...);
EcError EcBatterySendCommand(...);
/* non-blocking read so that it can support both polling mode
* and interrupt callback.
*/
EcError CrBatteryRecvCommand(...);
EcError EcBatteryRecvCommand(...);
/* Register a callback when a packet comes from SMBus */
EcError CrRegisterBatteryInterrupt(void (*isr)(...));
EcError EcRegisterBatteryInterrupt(void (*isr)(...));
#if 0 /* example code */
void BatteryPacketArrive(...) {
CrBatteryRecvCommand(); // read the packet.
EcBatteryRecvCommand(); // read the packet.
}
... somewhere in code ...
CrRegisterBatteryInterrupt(BatteryPacketArrive);
EcRegisterBatteryInterrupt(BatteryPacketArrive);
#endif
@@ -38,22 +41,22 @@ void BatteryPacketArrive(...) {
* Initialize the battery present as GPIO input pin and enable interrupt for
* callback.
*/
EcError CrBatteryPresentInit(void);
EcError EcBatteryPresentInit(void);
/* Calls GPIOPinRead() to read the GPIO state. */
/* TODO: has the state been debounced? */
EcError CrBatteryPrensentState(void);
EcError EcBatteryPrensentState(void);
/* Register a calback function. It is called while AC is plugged in or
* unplugged.
*/
EcError CrBatteryPresentRegister(void (*callback)(void));
EcError EcBatteryPresentRegister(void (*callback)(void));
/* Below is the example code to register this function. */
#if 0
/* This callback function is implemented in Chrome OS features layer. */
void BatteryStateChanged(void) {
int battery_present = CrBatteryPresentState();
int battery_present = EcBatteryPresentState();
if (battery_present) {
// start to authenticate battery;
// once authenticated, charge the battery;
@@ -62,7 +65,7 @@ void BatteryStateChanged(void) {
}
}
... somewhere in init code ...
CrBatteryPresentRegister(BatteryStateChanged);
EcBatteryPresentRegister(BatteryStateChanged);
#endif /* #if 0 */

View File

@@ -1,29 +1,32 @@
/* flash.h - flash interface
/* Copyright (c) 2011 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.
*
* (Chromium license) */
* flash.h - flash interface
*/
#ifndef __CHIP_INTERFACE_FLASH_H
#define __CHIP_INTERFACE_FLASH_H
/* returns the flash size and erase clock structure. */
EcError CrFlashQuery(*size, *erase_block) {
EcError EcFlashQuery(*size, *erase_block) {
EcError CrFlashRead(addr_t offset, length, uint8_t *data);
EcError EcFlashRead(addr_t offset, length, uint8_t *data);
EcError CrFlashWrite(addr_t offset, length, uint8_t *data);
EcError EcFlashWrite(addr_t offset, length, uint8_t *data);
EcError CrFlashErase(addr_t offset, length);
EcError EcFlashErase(addr_t offset, length);
EcError CrFlashSetWriteProtectRange(addr_t offset, length);
EcError EcFlashSetWriteProtectRange(addr_t offset, length);
/* TODO: change to Randall's new model */
/* Set lock bit (the SRP bit in SPI) */
EcError CrFlashEnableWriteProtect(void);
EcError EcFlashEnableWriteProtect(void);
/* TODO: change to Randall's new model */
/* Always return FAILED because FMPPEn cannot be reset to 1
* until a power-on reset or after committed (see p. 577).
*/
int CrFlashDisableWriteProtect(void);
int EcFlashDisableWriteProtect(void);
#endif /* __CHIP_INTERFACE_FLASH_H */

View File

@@ -1,6 +1,9 @@
/* keyboard_backlight.h - Keyboard backlight
/* Copyright (c) 2011 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.
*
* (Chromium license) */
* keyboard_backlight.h - Keyboard backlight
*/
#ifndef __CHIP_INTERFACE_KEYBOARD_BACKLIGHT_H
#define __CHIP_INTERFACE_KEYBOARD_BACKLIGHT_H
@@ -14,9 +17,9 @@
*/
/* Configure PWM port and set the initial backlight value. */
EcError CrKeyboardBacklightInit(uint16_t init_lightness);
EcError EcKeyboardBacklightInit(uint16_t init_lightness);
/* Set the mapped PWM value */
EcError CrKeyboardBacklightSet(uint16_t lightness);
EcError EcKeyboardBacklightSet(uint16_t lightness);
#endif /* __CHIP_INTERFACE_KEYBOARD_BACKLIGHT_H */

View File

@@ -1,26 +1,29 @@
/* lid.h - handle lid open/close
/* Copyright (c) 2011 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.
*
* (Chromium license) */
* lid.h - handle lid open/close
*/
#ifndef __CHIP_INTERFACE_LID_H
#define __CHIP_INTERFACE_LID_H
/* Initialize the GPIO pin */
EcError CrLidSwitchInit(void);
EcError EcLidSwitchInit(void);
/* Calls GPIOPinRead() to read the GPIO state. */
/* TODO: has the state been debounced? */
EcError CrLidSwitchState(void);
EcError EcLidSwitchState(void);
/* Register a calback function. It is called while lid state is changed.
*/
EcError CrLidSwitchRegister(void (*callback)(void));
EcError EcLidSwitchRegister(void (*callback)(void));
/* Below is the example code to register this function. */
#if 0
/* This callback function is implemented in Chrome OS features layer. */
void LidSwitchChanged(void) {
int lid_open = CrLidSwitchState();
int lid_open = EcLidSwitchState();
if (lid_open) {
if (system is in S3) {
// resume system
@@ -32,7 +35,7 @@ void LidSwitchChanged(void) {
}
}
... somewhere in init code ...
CrLidSwitchRegister(LidSwitchChanged);
EcLidSwitchRegister(LidSwitchChanged);
#endif
#endif /* __CHIP_INTERFACE_LID_H */

View File

@@ -1,7 +1,10 @@
/* power_button.h - Power button function.
* Hides chip-specific implementation behind this interface.
/* Copyright (c) 2011 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.
*
* (Chromium license) */
* power_button.h - Power button function.
* Hides chip-specific implementation behind this interface.
*/
#ifndef __CHIP_INTERFACE_POWER_BUTTON_H
#define __CHIP_INTERFACE_POWER_BUTTON_H
@@ -10,24 +13,24 @@
* Initialize the power button as GPIO input pin and enable interrupt for
* keyboard scanner code.
*/
EcError CrPowerButtonInit(void);
EcError EcPowerButtonInit(void);
/* Calls GPIOPinRead() to read the GPIO state. */
/* TODO: has the state been debounced? */
EcError CrPowerButtonGetState(void);
EcError EcPowerButtonGetState(void);
/* Register a calback function. It is called while power button is changing its
* state (pressed or released ).
*/
EcError CrPowerButtonRegister(void (*callback)(void));
EcError EcPowerButtonRegister(void (*callback)(void));
/* Below is the example code to register this function. */
#if 0
/* This callback function is implemented in Chrome OS features layer. */
void PowerButtonCallback(void) {
int pressed = CrPowerButtonGetState();
int pressed = EcPowerButtonGetState();
if (!prev_status) {
if (pressed) {
// Power button is just pressed. Generate scan code,
@@ -42,7 +45,7 @@ void PowerButtonCallback(void) {
}
... somewhere in init code ...
CrPowerButtonRegister(PowerButtonCallback);
EcPowerButtonRegister(PowerButtonCallback);
#endif /* #if 0 */

View File

@@ -1,14 +1,17 @@
/* timer.h - periodical timer
/* Copyright (c) 2011 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.
*
* (Chromium license) */
* timer.h - periodical timer
*/
#ifndef __CHIP_INTERFACE_TIMER_H
#define __CHIP_INTERFACE_TIMER_H
/* init hardware and prepare ISR */
EcError CrPeriodicalTimerInit(void);
EcError EcPeriodicalTimerInit(void);
EcError CrPeriodicalTimerRegister(
EcError EcPeriodicalTimerRegister(
int interval /* ms */,
int (*timer)(int /* delta ms from last call */));

View File

@@ -9,7 +9,8 @@ TESTDIR = $(CROS_EC_TOP)/test
BUILD_ROOT := ${BUILD}/$(shell basename ${CROS_EC_TOP})
LIBS = $(CROS_EC_LIB) # Firmware library must be self-contained
INCLUDES += -I$(LIBDIR)/include
INCLUDES += -I$(LIBDIR)/include \
-I$(TOP)
ifeq ($(FIRMWARE_ARCH),)
INCLUDES += -I$(STUBDIR)/include
@@ -17,20 +18,28 @@ else
INCLUDES += -I$(FWDIR)/arch/$(FIRMWARE_ARCH)/include
endif
CORE_SRCS = \
main.c
# find ./lib -iname '*.c' | sort
CORE_OBJS = $(CORE_SRCS:%.c=${BUILD_ROOT}/%.o)
LIB_SRCS = \
./lib/ec_console.c \
./lib/ec_host_command.c
./lib/ec_host_command.c \
./lib/ec_keyboard.c
LIB_OBJS = $(LIB_SRCS:%.c=${BUILD_ROOT}/%.o)
STUB_SRCS = \
./chip_stub/ec_os.c \
./chip_stub/ec_uart.c
./chip_stub/ec_uart.c \
./chip_stub/keyboard.c
STUB_OBJS = $(STUB_SRCS:%.c=${BUILD_ROOT}/%.o)
ALL_SRCS = ${LIB_SRCS} ${STUB_SRCS}
ALL_SRCS = ${CORE_SRCS} ${LIB_SRCS} ${STUB_SRCS}
ifeq ($(FIRMWARE_ARCH),)
all : $(CROS_EC_LIB) $(CHIP_STUB_LIB)
@@ -40,7 +49,7 @@ endif
include ../common.mk
$(CROS_EC_LIB) : $(LIB_OBJS)
$(CROS_EC_LIB) : $(CORE_OBJS) $(LIB_OBJS)
rm -f $@
ar qc $@ $^

View File

@@ -0,0 +1,50 @@
/* Copyright (c) 2011 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.
*
* Chip stub code of keyboard. Implements the chip interface.
*/
#include <stdint.h>
#include "chip_interface/keyboard.h"
static EcKeyboardCallback core_keyboard_callback;
static uint8_t virtual_matrix[MAX_KEYBOARD_MATRIX_COLS];
EcError EcKeyboardRegisterCallback(EcKeyboardCallback cb) {
core_keyboard_callback = cb;
return EC_SUCCESS;
}
EcError EcKeyboardGetState(uint8_t *bit_array) {
/* TODO: implement later */
return EC_SUCCESS;
}
/* Called by test code. This simulates a key press or release.
* Usually, the test code would expect a scan code is received at host side.
*/
EcError SimulateKeyStateChange(int col, int row, int state) {
ASSERT(col < MAX_KEYBOARD_MATRIX_COLS);
ASSERT(row < MAX_KEYBOARD_MATRIX_ROWS);
if (!core_keyboard_callback) return EC_ERROR_UNKNOWN;
state = (state) ? 1 : 0;
int current_state = (virtual_matrix[col] >> row) & 1;
if (state && !current_state) {
/* key is just pressed down */
virtual_matrix[col] |= 1 << row;
core_keyboard_callback(col, row, state);
} else if (!state && current_state) {
virtual_matrix[col] &= ~(1 << row);
core_keyboard_callback(col, row, state);
} else {
/* Nothing happens if a key has been pressed or released. */
}
return EC_SUCCESS;
}

View File

@@ -0,0 +1,14 @@
/* Copyright (c) 2011 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.
*
* The keyboard stub header file for test code to include.
*/
#ifndef __EC_CHIP_STUB_KEYBOARD_H_
#define __EC_CHIP_STUB_KEYBOARD_H_
EcError SimulateKeyStateChange(int col, int row, int state);
#endif /* __EC_CHIP_STUB_KEYBOARD_H_ */

View File

@@ -1,16 +1,14 @@
/* Copyright (c) 2011 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 __CROS_EC_INCLUDE_CORE_H
#define __CROS_EC_INCLUDE_CORE_H
/* Fake main routine, to make sure lib functions link */
#include "ec_common.h"
#include "ec_console.h"
#include "ec_uart.h"
/* main.c */
EcError CoreMain();
int main(void) {
return 0;
}
#endif /* __CROS_EC_INCLUDE_CORE_H */

View File

@@ -9,6 +9,7 @@
#define __CROS_EC_COMMON_H
#include <stdint.h>
#include <stdio.h> /* FIXME: will be removed for portibility in the future */
/* Functions which return error return one of these. This is an
* integer instead of an enum to support module-internal error
@@ -33,4 +34,16 @@ enum EcErrorList {
EC_ERROR_INTERNAL_LAST = 0x1FFFF
};
/* TODO: move to a proper .h file */
#define PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
/* TODO: move to a proper .h file */
#define ASSERT(expr) do { \
if (!(expr)) { \
PRINTF("ASSERT(%s) failed at file %s:%d.\n", \
#expr, __FILE__, __LINE__); \
} \
} while (0)
#endif /* __CROS_EC_COMMON_H */

View File

@@ -8,13 +8,19 @@
#ifndef __CROS_EC_KEYBOARD_H
#define __CROS_EC_KEYBOARD_H
/* The initialize code of keyboard lib. Called by core main. */
EcError EcKeyboardInit();
/* Register the board-specific keyboard matrix translation function.
* The callback function accepts col/row and returns the scan code.
*/
EcError CrKeyboardMatrixRegister(
EcError EcKeyboardMatrixRegister(
int8_t col_num, int8_t row_num,
EcError (*callback)(
int8_t column, int8_t row, int8_t pressed,
uint8_t *scan_code, int32_t* len));
#endif /* __CROS_EC_KEYBOARD_H */

24
cros_ec/lib/ec_keyboard.c Normal file
View File

@@ -0,0 +1,24 @@
/* Copyright (c) 2011 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.
*
* Chrome OS EC keyboard code.
*/
#include "cros_ec/include/ec_common.h"
#include "chip_interface/keyboard.h"
static void KeyboardStateChanged(int col, int row, int is_pressed) {
PRINTF("File %s:%s(): col=%d row=%d is_pressed=%d\n",
__FILE__, __FUNCTION__, col, row, is_pressed);
}
EcError EcKeyboardInit() {
EcError ret;
ret = EcKeyboardRegisterCallback(KeyboardStateChanged);
if (ret != EC_SUCCESS) return ret;
return EC_SUCCESS;
}

View File

@@ -0,0 +1,24 @@
/* Copyright (c) 2011 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.
*
* This is the main function of Chrome OS EC core.
* Called by platform-dependent main function.
*
*/
#include "cros_ec/include/core.h"
#include "cros_ec/include/ec_common.h"
#include "cros_ec/include/ec_keyboard.h"
EcError CoreMain() {
EcError ret;
ret = EcKeyboardInit();
if (ret != EC_SUCCESS) {
printf("EcKeyboardInit() failed: %d\n", ret);
return ret;
}
return EC_SUCCESS;
}

View File

@@ -3,10 +3,11 @@
# found in the LICENSE file.
INCLUDES += -I./include \
-I$(CROS_EC_DIR)/lib/include
-I$(CROS_EC_DIR)/lib/include \
-I../../
BUILD_ROOT = ${BUILD}/cros_ec/test
TEST_NAMES = ec_os_test
TEST_NAMES = ec_os_test ec_keyboard_test
TEST_BINS = $(addprefix ${BUILD_ROOT}/,$(TEST_NAMES))
@@ -21,6 +22,9 @@ CFLAGS += -MMD -MF $@.d -Xlinker --allow-multiple-definition
LIBS := ${TEST_LIB} $(CROS_EC_LIB) $(CHIP_STUB_LIB)
.SUFFIXES:
.SUFFIXES: .o .c
ifneq (${RUNTESTS},)
EXTRA_TARGET = runtests
endif
@@ -37,9 +41,10 @@ ${BUILD_ROOT}/%.o : %.c
${BUILD_ROOT}/% : %.c ${LIBS}
$(CC) $(CFLAGS) $(INCLUDES) $< ${LIBS} -o $@ -lrt
ALLTESTS = ec_os_test
ALLTESTS = ec_os_test ec_keyboard_test
runtests:
${BUILD_ROOT}/ec_os_test
${BUILD_ROOT}/ec_keyboard_test
-include ${ALL_DEPS}

View File

@@ -0,0 +1,31 @@
/* Copyright (c) 2011 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.
*
* Testing code. Running at Linux environment.
*/
#include "cros_ec/include/ec_common.h"
#include "cros_ec/include/core.h"
#include "cros_ec/chip_stub/keyboard.h"
int run_test_cases() {
/* Just a simple test */
SimulateKeyStateChange(2, 3, 1);
/* Expect KeyboardStateChanged() in cros_ec/keyboard.c shows something. */
return 0;
}
int main(int argc, char **argv) {
EcError ret;
/* Call Google EC core initial code. */
ret = CoreMain();
if (ret != EC_SUCCESS)
return 1;
return run_test_cases();
}