【原创】zstack - 协议栈程序精简日志-按键驱动移植

来源:互联网 发布:数据眼龙空 编辑:程序博客网 时间:2024/05/22 09:04

【原创】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**************************************************************************************************//************************************************************************************************** * @fn      HalKeyInit* * @brief   Initilize Key Service* * @param   none* * @return  None**************************************************************************************************/void HalKeyInit( void ){  /* Initialize previous key to 0 */  halKeySavedKeys = 0;  HAL_KEY_SW_1_SEL &= ~(HAL_KEY_SW_1_BIT);    /* Set pin function to GPIO */  HAL_KEY_SW_1_DIR &= ~(HAL_KEY_SW_1_BIT);    /* Set pin direction to Input */  HAL_KEY_SW_2_SEL &= ~(HAL_KEY_SW_2_BIT);    /* Set pin function to GPIO */  HAL_KEY_SW_2_DIR &= ~(HAL_KEY_SW_2_BIT);    /* Set pin direction to Input */  /* Initialize callback function */  pHalKeyProcessFunction  = NULL;  /* Start with key is not configured */  HalKeyConfigured = FALSE;}/************************************************************************************************** * @fn      HalKeyConfig* * @brief   Configure the Key serivce* * @param   interruptEnable - TRUE/FALSE, enable/disable interrupt *          cback - pointer to the CallBack function* * @return  None**************************************************************************************************/void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback){  /* Enable/Disable Interrupt or */  Hal_KeyIntEnable = interruptEnable;  /* Register the callback fucntion */  pHalKeyProcessFunction = cback;  /* Determine if interrupt is enable or not */  if (Hal_KeyIntEnable)  {    /* Rising/Falling edge configuratinn */    PICTL &= ~(HAL_KEY_SW_1_EDGEBIT | HAL_KEY_SW_2_EDGEBIT );    /* Clear the edge bit */    PICTL |= HAL_KEY_SW_1_EDGE | HAL_KEY_SW_2_EDGE;   /*原程序有问题,只是清除了bit,并没有控制*/    /* For falling edge, the bit must be set. */    /* Interrupt configuration:     * - Enable interrupt generation at the port     * - Enable CPU interrupt     * - Clear any pending interrupt     */    HAL_KEY_SW_1_ICTL |= HAL_KEY_SW_1_ICTLBIT;    HAL_KEY_SW_1_IEN |= HAL_KEY_SW_1_IENBIT;    HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_BIT);    HAL_KEY_SW_2_ICTL |= HAL_KEY_SW_2_ICTLBIT;    HAL_KEY_SW_2_IEN |= HAL_KEY_SW_2_IENBIT;    HAL_KEY_SW_2_PXIFG = ~(HAL_KEY_SW_2_BIT);    /* Do this only after the hal_key is configured - to work with sleep stuff */    if (HalKeyConfigured == TRUE)    {      osal_stop_timerEx( Hal_TaskID, HAL_KEY_EVENT);  /* Cancel polling if active */    }  }  else    /* Interrupts NOT enabled */  {    HAL_KEY_SW_1_ICTL &= ~(HAL_KEY_SW_1_ICTLBIT); /* don't generate interrupt */    HAL_KEY_SW_1_IEN &= ~(HAL_KEY_SW_1_IENBIT);   /* Clear interrupt enable bit */    HAL_KEY_SW_2_ICTL &= ~(HAL_KEY_SW_2_ICTLBIT); /* don't generate interrupt */    HAL_KEY_SW_2_IEN &= ~(HAL_KEY_SW_2_IENBIT);   /* Clear interrupt enable bit */    osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_POLLING_VALUE);    /* Kick off polling */  }  /* Key now is configured */  HalKeyConfigured = TRUE;}/************************************************************************************************** * @fn      HalKeyRead* * @brief   Read the current value of a key* * @param   None* * @return  keys - current keys status**************************************************************************************************/uint8 HalKeyRead ( void ){  uint8 keys = 0;  if( HAL_PUSH_BUTTON1() ) {      keys |= HAL_KEY_SW_1;   }  if( HAL_PUSH_BUTTON2() ) {      keys |= HAL_KEY_SW_2;   }  return keys;}/************************************************************************************************** * @fn      HalKeyPoll* * @brief   Called by hal_driver to poll the keys* * @param   None* * @return  None**************************************************************************************************/void HalKeyPoll (void){  uint8 keys = 0;  /* If interrupts are not enabled, previous key status and current key status   * are compared to find out if a key has changed status.   */  if (!Hal_KeyIntEnable)  {    if (keys == halKeySavedKeys)    {      /* Exit - since no keys have changed */      return;    }    /* Store the current keys for comparation next time */    halKeySavedKeys = keys;  }  else  {    /* Key interrupt handled here */  }/*  if (HAL_PUSH_BUTTON1())  {    HalKeyDelay(300);    if (HAL_PUSH_BUTTON1())      keys |= HAL_KEY_SW_1;  }  if (HAL_PUSH_BUTTON2())  {    HalKeyDelay(300);    if (HAL_PUSH_BUTTON2())      keys |= HAL_KEY_SW_2;  }*/  keys = HalKeyRead();   /* Invoke Callback if new keys were depressed */  if (keys && (pHalKeyProcessFunction))  {    (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);  }}/************************************************************************************************** * @fn      halGetJoyKeyInput* * @brief   Map the ADC value to its corresponding key.* * @param   None* * @return  keys - current joy key status**************************************************************************************************/uint8 halGetJoyKeyInput(void){  /* The joystick control is encoded as an analog voltage.   * Read the JOY_LEVEL analog value and map it to joy movement.   */  uint8 adc;  uint8 ksave0 = 0;  uint8 ksave1;  return ksave0;}/************************************************************************************************** * @fn      halProcessKeyInterrupt* * @brief   Checks to see if it's a valid key interrupt, saves interrupt driven key states for *          processing by HalKeyRead(), and debounces keys by scheduling HalKeyRead() 25ms later.** @param** @return**************************************************************************************************/void halProcessKeyInterrupt (void){  bool valid=FALSE;  if (HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT)  /* Interrupt Flag has been set */  {    HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_BIT); /* Clear Interrupt Flag */    valid = TRUE;  }  if (HAL_KEY_SW_2_PXIFG & HAL_KEY_SW_2_BIT)  /* Interrupt Flag has been set */  {    HAL_KEY_SW_2_PXIFG = ~(HAL_KEY_SW_2_BIT); /* Clear Interrupt Flag */    valid = TRUE;  }  if (valid)  {    osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE);  }}/************************************************************************************************** * @fn      HalKeyEnterSleep* * @brief  - Get called to enter sleep mode** @param** @return**************************************************************************************************/void HalKeyEnterSleep ( void ){}/************************************************************************************************** * @fn      HalKeyExitSleep* * @brief   - Get called when sleep is over** @param* * @return  - return saved keys**************************************************************************************************/uint8 HalKeyExitSleep ( void ){  /* Wake up and read keys */  return ( HalKeyRead () );}/*************************************************************************************************** *                                    INTERRUPT SERVICE ROUTINE***************************************************************************************************//************************************************************************************************** * @fn      halKeyPort0Isr* * @brief   Port0 ISR** @param** @return**************************************************************************************************/HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR ){  if ((HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT) || (HAL_KEY_SW_2_PXIFG & HAL_KEY_SW_2_BIT) )  {    halProcessKeyInterrupt();  }  /*    Clear the CPU interrupt flag for Port_0    PxIFG has to be cleared before PxIF  */  HAL_KEY_SW_1_PXIFG = 0;  HAL_KEY_SW_2_PXIFG = 0;//中断标志全部清零  HAL_KEY_CPU_PORT_0_IF = 0;}/************************************************************************************************** * @fn      halKeyPort2Isr* * @brief   Port2 ISR** @param** @return**************************************************************************************************/HAL_ISR_FUNCTION( halKeyPort2Isr, P2INT_VECTOR ){  /*  if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT)  {    halProcessKeyInterrupt();  }    Clear the CPU interrupt flag for Port_2    PxIFG has to be cleared before PxIF    Notes: P2_1 and P2_2 are debug lines.  */  //HAL_KEY_JOY_MOVE_PXIFG = 0;  HAL_KEY_CPU_PORT_2_IF = 0;}

hal_key.h文件内容修改如下:

/* Switches (keys) */#define HAL_KEY_SW_1 0x01  // Button S1 if available#define HAL_KEY_SW_2 0x02  // Button S2 if available#define HAL_KEY_SW_5 0x04  //#define HAL_KEY_SW_4 0x08  //#define HAL_KEY_SW_3 0x10  //#define HAL_KEY_SW_6 0x20  //#define HAL_KEY_SW_7 0x40  //

最后,在SampleApp.c中添加测试程序:

void SampleApp_HandleKeys( uint8 shift, uint8 keys ){  (void)shift;  // Intentionally unreferenced parameter  char tmp[]="key-test!\n";  if ( keys & HAL_KEY_SW_1 )  {    HalLcdWriteString("check key1", HAL_LCD_LINE_3 );//LCD显示    HalLedBlink( HAL_LED_1, 4, 50, 1000 );  }  if ( keys & HAL_KEY_SW_2 )  {    HalLcdWriteString("check key2", HAL_LCD_LINE_3 );//LCD显示    HalLedSet(HAL_LED_2, HAL_LED_MODE_TOGGLE);  }}
0 0
原创粉丝点击