【原创】zstack - 协议栈程序精简日志-按键驱动移植
闲来无事测试zstack协议栈的记录。
目的:去掉协议栈中无用的部分,精简协议栈程序。
我的板子上有两个按键,S1 对应P0.4,S2对应P0.5,正常状态下按键端口为高电平,按键按下时端口为低电平。本程序的按键检测使用P0外部中断的方式进行。配置为下降沿触发,当P0口出现下降沿中断时,中断程序内检测按键端口,如果端口输入低电平说明改端口的按键被按下。
具体修改如下:
OnBoard.c中使能按键中断:
void InitBoard( uint8 level )
{
。。。。。。
/* Initialize Key stuff */
OnboardKeyIntEnable = HAL_KEY_INTERRUPT_ENABLE;
HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);
。。。。。。
}
没有shift键的概念,可以做如下修改:
void OnBoard_KeyCallback ( uint8 keys, uint8 state )
{
uint8 shift;
(void)state;
/* Get shift key status */
//shift = ((keys & HAL_KEY_SW_1) ? true : false);
shift = true; //本系统没有shift键的概念,只有s1,s2.。。
。。。
}
hal_board_cfg.h文件中做如下修改:
/* S1 */
#define PUSH1_BV BV(4)
#define PUSH1_SBIT P0_4
/* S2 */
#define PUSH2_BV BV(5)
#define PUSH2_SBIT P0_5
//具体是低电平有效还是高电平有效需要根据板子上的原理图具体情况具体分析
#define PUSH1_POLARITY ACTIVE_LOW //低电平有效
#define PUSH2_POLARITY ACTIVE_LOW
其他没用的按键定义均可删掉。
注释掉函数HAL_BOARD_INIT中的红色行:
#define HAL_BOARD_INIT() \
{
\
。。。。。。 \
/* configure tristates */ \
/*P0INP |= PUSH2_BV; */ \
。。。
}
修改hal_key.c文件:
删除没用的按键定义,我的按键定义最终如下(S1-P0.4 S2-P0.5):
红色部分均有修改,具体见下面程序:
/*
整两个标志在其他地方有用到,删除后会造成联网不成功
*/
#define HAL_KEY_RISING_EDGE 0
#define HAL_KEY_FALLING_EDGE 1
#define HAL_KEY_DEBOUNCE_VALUE 25
#define HAL_KEY_POLLING_VALUE 100
/* CPU port interrupt (key P0.1 J-STICK P2.0)*/
#define HAL_KEY_CPU_PORT_0_IF P0IF
#define HAL_KEY_CPU_PORT_2_IF P2IF
/* SW_1 is at P0.4 */
#define HAL_KEY_SW_1_PORT P0
#define HAL_KEY_SW_1_BIT BV(4)
#define HAL_KEY_SW_1_SEL P0SEL
#define HAL_KEY_SW_1_DIR P0DIR
/* SW_2 is at P0.5 */
#define HAL_KEY_SW_2_PORT P0
#define HAL_KEY_SW_2_BIT BV(5)
#define HAL_KEY_SW_2_SEL P0SEL
#define HAL_KEY_SW_2_DIR P0DIR
/* edge interrupt */
#define HAL_KEY_SW_1_EDGEBIT BV(0) //bit 0 :Port 0, inputs 7 to 0 interrupt configuration. This bit selects the interrupt request condition for all Port 0 inputs.
#define HAL_KEY_SW_1_EDGE 0x01 //HAL_KEY_FALLING_EDGE //下降沿触发--p0的下降沿为1 上升沿为1 ,p1的下降沿为0x02
// 这部分需要根据电路进行配置,本电路按键按下为低电平,正常为高电平。下降沿触发后,cpu读取到的为低电平,所以,PUSH1_POLARITY 要设置为 ACTIVE_LOW
#define HAL_KEY_SW_2_EDGEBIT BV(0) //bit 0 :Port 0, inputs 7 to 0 interrupt configuration.
#define HAL_KEY_SW_2_EDGE 0x01 //HAL_KEY_FALLING_EDGE //下降沿触发
/* SW_1 interrupts */
#define HAL_KEY_SW_1_IEN IEN1 /* CPU interrupt mask register ,总中断控制位*/
#define HAL_KEY_SW_1_IENBIT BV(5) /* Mask bit for all of Port_0 */
#define HAL_KEY_SW_1_ICTL P0IEN /* Port Interrupt Control register */
#define HAL_KEY_SW_1_ICTLBIT BV(4) /* P0IEN - P0.4 enable/disable bit */
#define HAL_KEY_SW_1_PXIFG P0IFG /* Interrupt flag at source */
/* SW_2 interrupts */
#define HAL_KEY_SW_2_IEN IEN1 /* CPU interrupt mask register ,总中断控制位*/
#define HAL_KEY_SW_2_IENBIT BV(5) /* Mask bit for all of Port_0 */
#define HAL_KEY_SW_2_ICTL P0IEN /* Port Interrupt Control register */
#define HAL_KEY_SW_2_ICTLBIT BV(5) /* P0IEN - P0.5 enable/disable bit */
#define HAL_KEY_SW_2_PXIFG P0IFG /* Interrupt flag at source */
/**************************************************************************************************
* TYPEDEFS
**********************************************************************