z-stac配置按键
生活随笔
收集整理的這篇文章主要介紹了
z-stac配置按键
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
按鍵的定義一般有兩種,開關和搖桿。這里只討論開關的定義。
根據硬件在hal_board_cfg.h中定義
在hal_key.h中定義宏位
在hal_key.c中需要初始化相關寄存器
附錄:
hal_board_cfg.h
/**************************************************************************************************Filename: hal_board_cfg.hRevised: $Date: 2009-08-24 10:47:59 -0700 (Mon, 24 Aug 2009) $Revision: $Revision: 20635 $Description: Describe the purpose and contents of the file.Copyright 2006-2009 Texas Instruments Incorporated. All rights reserved.IMPORTANT: Your use of this Software is limited to those specific rightsgranted under the terms of a software license agreement between the userwho downloaded the software, his/her employer (which must be your employer)and Texas Instruments Incorporated (the "License"). You may not use thisSoftware unless you agree to abide by the terms of the License. The Licenselimits your use, and you acknowledge, that the Software may not be modified,copied or distributed unless embedded on a Texas Instruments microcontrolleror used solely and exclusively in conjunction with a Texas Instruments radiofrequency transceiver, which is integrated into your product. Other than forthe foregoing purpose, you may not use, reproduce, copy, prepare derivativeworks of, modify, distribute, perform, display or sell this Software and/orits documentation for any purpose.YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION AREPROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALLTEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHERLEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSESINCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVEOR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENTOF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.Should you have any questions regarding your right to use this Software,contact Texas Instruments Incorporated at www.TI.com. **************************************************************************************************/#ifndef HAL_BOARD_CFG_H #define HAL_BOARD_CFG_H/** =============================================================* | Chipcon SmartRF05EB Evaluation Board, plus |* | Texas Instruments CC2530EM Evaluation Module |* | --------------------------------------------------------- |* | mcu : 8051 core |* | clock : 32MHz |* =============================================================*//* ------------------------------------------------------------------------------------------------* Includes* ------------------------------------------------------------------------------------------------*/#include "hal_mcu.h" #include "hal_defs.h" #include "hal_types.h"/* ------------------------------------------------------------------------------------------------* CC2590/CC2591 support** Define HAL_PA_LNA_CC2590 if CC2530+CC2590EM is used* Define HAL_PA_LNA if CC2530+CC2591EM is used* Note that only one of them can be defined* ------------------------------------------------------------------------------------------------*/ #define xHAL_PA_LNA #define xHAL_PA_LNA_CC2590/* ------------------------------------------------------------------------------------------------* Board Indentifier** Define the Board Identifier to HAL_BOARD_CC2530EB_REV13 for SmartRF05 EB 1.3 board* ------------------------------------------------------------------------------------------------*/#if !defined (HAL_BOARD_CC2530EB_REV17) && !defined (HAL_BOARD_CC2530EB_REV13)#define HAL_BOARD_CC2530EB_REV17 #endif/* ------------------------------------------------------------------------------------------------* Clock Speed* ------------------------------------------------------------------------------------------------*/ #define HAL_CPU_CLOCK_MHZ 32/* 32 kHz clock source select in CLKCONCMD */ #if !defined (OSC32K_CRYSTAL_INSTALLED) || (defined (OSC32K_CRYSTAL_INSTALLED) && (OSC32K_CRYSTAL_INSTALLED == TRUE))#define OSC_32KHZ 0x00 /* external 32 KHz xosc */ #else#define OSC_32KHZ 0x80 /* internal 32 KHz rcosc */ #endif/* ------------------------------------------------------------------------------------------------* LED Configuration* ------------------------------------------------------------------------------------------------*/#if defined (HAL_BOARD_CC2530EB_REV17) && !defined (HAL_PA_LNA) && !defined (HAL_PA_LNA_CC2590)#define HAL_NUM_LEDS 3 #elif defined (HAL_BOARD_CC2530EB_REV13) || defined (HAL_PA_LNA) || defined (HAL_PA_LNA_CC2590)#define HAL_NUM_LEDS 1 #else#error Unknown Board Indentifier #endif#define HAL_LED_BLINK_DELAY() st( { volatile uint32 i; for (i=0; i<0x5800; i++) { }; } )/* 1 - Green */ /*官方 #define LED1_BV BV(0) #define LED1_SBIT P1_0 #define LED1_DDR P1DIR */ /*D2 Luke */ #define LED1_BV BV(0) #define LED1_SBIT P0_0 #define LED1_DDR P0DIR#define LED1_POLARITY ACTIVE_HIGH/*D3 Luke*/#define LED2_BV BV(0)#define LED2_SBIT P2_0#define LED2_DDR P2DIR#define LED2_POLARITY ACTIVE_HIGH#ifdef HAL_BOARD_CC2530EB_REV17/* 2 - Red */ /*官方#define LED2_BV BV(1)#define LED2_SBIT P1_1#define LED2_DDR P1DIR *//* 3 - Yellow */#define LED3_BV BV(4)#define LED3_SBIT P1_4#define LED3_DDR P1DIR#define LED3_POLARITY ACTIVE_HIGH #endif/* ------------------------------------------------------------------------------------------------* Push Button Configuration* ------------------------------------------------------------------------------------------------*/#define ACTIVE_LOW ! #define ACTIVE_HIGH !! /* double negation forces result to be '1' *//* S1 官方 #define PUSH1_BV BV(1) #define PUSH1_SBIT P0_1 */ /*S1 luke */ #define PUSH1_BV BV(4) #define PUSH1_SBIT P0_4#ifdef HAL_BOARD_CC2530EB_REV17#define PUSH1_POLARITY ACTIVE_HIGH #elif defined (HAL_BOARD_CC2530EB_REV13)#define PUSH1_POLARITY ACTIVE_LOW #else#error Unknown Board Indentifier #endif/* Joystick Center Press */ #define PUSH2_BV BV(0) //#define PUSH2_SBIT P2_0 #define PUSH2_POLARITY ACTIVE_HIGH /* ------------------------------------------------------------------------------------------------* OSAL NV implemented by internal flash pages.* ------------------------------------------------------------------------------------------------*/// Flash is partitioned into 8 banks of 32 KB or 16 pages. #define HAL_FLASH_PAGE_PER_BANK 16 // Flash is constructed of 128 pages of 2 KB. #define HAL_FLASH_PAGE_SIZE 2048 #define HAL_FLASH_WORD_SIZE 4// CODE banks get mapped into the XDATA range 8000-FFFF. #define HAL_FLASH_PAGE_MAP 0x8000// The last 16 bytes of the last available page are reserved for flash lock bits. #if defined NON_BANKED #define HAL_FLASH_LOCK_BITS 16 #define HAL_NV_PAGE_END 30 #else #define HAL_FLASH_LOCK_BITS 16 #define HAL_NV_PAGE_END 126 #endif// Re-defining Z_EXTADDR_LEN here so as not to include a Z-Stack .h file. #define HAL_FLASH_IEEE_SIZE 8 #define HAL_FLASH_IEEE_PAGE (HAL_NV_PAGE_END+1) #define HAL_FLASH_IEEE_OSET (HAL_FLASH_PAGE_SIZE - HAL_FLASH_LOCK_BITS - HAL_FLASH_IEEE_SIZE) #define HAL_INFOP_IEEE_OSET 0xC#define HAL_NV_PAGE_CNT 6 #define HAL_NV_PAGE_BEG (HAL_NV_PAGE_END-HAL_NV_PAGE_CNT+1)// Used by DMA macros to shift 1 to create a mask for DMA registers. #define HAL_NV_DMA_CH 0 #define HAL_DMA_CH_RX 3 #define HAL_DMA_CH_TX 4#define HAL_NV_DMA_GET_DESC() HAL_DMA_GET_DESC0() #define HAL_NV_DMA_SET_ADDR(a) HAL_DMA_SET_ADDR_DESC0((a))/* ------------------------------------------------------------------------------------------------* Macros* ------------------------------------------------------------------------------------------------*//* ----------- Cache Prefetch control ---------- */ #define PREFETCH_ENABLE() st( FCTL = 0x08; ) #define PREFETCH_DISABLE() st( FCTL = 0x04; )/* ----------- Board Initialization ---------- */ #if defined (HAL_BOARD_CC2530EB_REV17) && !defined (HAL_PA_LNA) && !defined (HAL_PA_LNA_CC2590)#define HAL_BOARD_INIT() \ { \uint16 i; \\SLEEPCMD &= ~OSC_PD; /* turn on 16MHz RC and 32MHz XOSC */ \while (!(SLEEPSTA & XOSC_STB)); /* wait for 32MHz XOSC stable */ \asm("NOP"); /* chip bug workaround */ \for (i=0; i<504; i++) asm("NOP"); /* Require 63us delay for all revs */ \CLKCONCMD = (CLKCONCMD_32MHZ | OSC_32KHZ); /* Select 32MHz XOSC and the source for 32K clock */ \while (CLKCONSTA != (CLKCONCMD_32MHZ | OSC_32KHZ)); /* Wait for the change to be effective */ \SLEEPCMD |= OSC_PD; /* turn off 16MHz RC */ \\/* Turn on cache prefetch mode */ \PREFETCH_ENABLE(); \\/* set direction for GPIO outputs */ \LED1_DDR |= LED1_BV; \LED2_DDR |= LED2_BV; \LED3_DDR |= LED3_BV; \\/* configure tristates */ \/*P0INP |= PUSH2_BV; luke*/ \ }#elif defined (HAL_BOARD_CC2530EB_REV13) || defined (HAL_PA_LNA) || defined (HAL_PA_LNA_CC2590)#define HAL_BOARD_INIT() \ { \uint16 i; \\SLEEPCMD &= ~OSC_PD; /* turn on 16MHz RC and 32MHz XOSC */ \while (!(SLEEPSTA & XOSC_STB)); /* wait for 32MHz XOSC stable */ \asm("NOP"); /* chip bug workaround */ \for (i=0; i<504; i++) asm("NOP"); /* Require 63us delay for all revs */ \CLKCONCMD = (CLKCONCMD_32MHZ | OSC_32KHZ); /* Select 32MHz XOSC and the source for 32K clock */ \while (CLKCONSTA != (CLKCONCMD_32MHZ | OSC_32KHZ)); /* Wait for the change to be effective */ \SLEEPCMD |= OSC_PD; /* turn off 16MHz RC */ \\/* Turn on cache prefetch mode */ \PREFETACH_ENABLE(); \\/* set direction for GPIO outputs */ \LED1_DDR |= LED1_BV; \\/* Set PA/LNA HGM control P0_7 */ \P0DIR |= BV(7); \\/* configure tristates */ \/* luke P0INP |= PUSH2_BV; */ \ }#endif/* ----------- Debounce ---------- */ #define HAL_DEBOUNCE(expr) { int i; for (i=0; i<500; i++) { if (!(expr)) i = 0; } }/* ----------- Push Buttons ---------- */ #define HAL_PUSH_BUTTON1() (PUSH1_POLARITY (PUSH1_SBIT)) #define HAL_PUSH_BUTTON2() (PUSH2_POLARITY (PUSH2_SBIT)) #define HAL_PUSH_BUTTON3() (0) #define HAL_PUSH_BUTTON4() (0) #define HAL_PUSH_BUTTON5() (0) #define HAL_PUSH_BUTTON6() (0)/* ----------- LED's ---------- */ #if defined (HAL_BOARD_CC2530EB_REV17) && !defined (HAL_PA_LNA) && !defined (HAL_PA_LNA_CC2590)#define HAL_TURN_OFF_LED1() st( LED1_SBIT = LED1_POLARITY (0); )#define HAL_TURN_OFF_LED2() st( LED2_SBIT = LED2_POLARITY (0); )#define HAL_TURN_OFF_LED3() st( LED3_SBIT = LED3_POLARITY (0); )#define HAL_TURN_OFF_LED4() HAL_TURN_OFF_LED1()#define HAL_TURN_ON_LED1() st( LED1_SBIT = LED1_POLARITY (1); )#define HAL_TURN_ON_LED2() st( LED2_SBIT = LED2_POLARITY (1); )#define HAL_TURN_ON_LED3() st( LED3_SBIT = LED3_POLARITY (1); )#define HAL_TURN_ON_LED4() HAL_TURN_ON_LED1()#define HAL_TOGGLE_LED1() st( if (LED1_SBIT) { LED1_SBIT = 0; } else { LED1_SBIT = 1;} )#define HAL_TOGGLE_LED2() st( if (LED2_SBIT) { LED2_SBIT = 0; } else { LED2_SBIT = 1;} )#define HAL_TOGGLE_LED3() st( if (LED3_SBIT) { LED3_SBIT = 0; } else { LED3_SBIT = 1;} )#define HAL_TOGGLE_LED4() HAL_TOGGLE_LED1()#define HAL_STATE_LED1() (LED1_POLARITY (LED1_SBIT))#define HAL_STATE_LED2() (LED2_POLARITY (LED2_SBIT))#define HAL_STATE_LED3() (LED3_POLARITY (LED3_SBIT))#define HAL_STATE_LED4() HAL_STATE_LED1()#elif defined (HAL_BOARD_CC2530EB_REV13) || defined (HAL_PA_LNA) || defined (HAL_PA_LNA_CC2590)#define HAL_TURN_OFF_LED1() st( LED1_SBIT = LED1_POLARITY (0); )#define HAL_TURN_OFF_LED2() HAL_TURN_OFF_LED1()#define HAL_TURN_OFF_LED3() HAL_TURN_OFF_LED1()#define HAL_TURN_OFF_LED4() HAL_TURN_OFF_LED1()#define HAL_TURN_ON_LED1() st( LED1_SBIT = LED1_POLARITY (1); )#define HAL_TURN_ON_LED2() HAL_TURN_ON_LED1()#define HAL_TURN_ON_LED3() HAL_TURN_ON_LED1()#define HAL_TURN_ON_LED4() HAL_TURN_ON_LED1()#define HAL_TOGGLE_LED1() st( if (LED1_SBIT) { LED1_SBIT = 0; } else { LED1_SBIT = 1;} )#define HAL_TOGGLE_LED2() HAL_TOGGLE_LED1()#define HAL_TOGGLE_LED3() HAL_TOGGLE_LED1()#define HAL_TOGGLE_LED4() HAL_TOGGLE_LED1()#define HAL_STATE_LED1() (LED1_POLARITY (LED1_SBIT))#define HAL_STATE_LED2() HAL_STATE_LED1()#define HAL_STATE_LED3() HAL_STATE_LED1()#define HAL_STATE_LED4() HAL_STATE_LED1()#endif/* ----------- XNV ---------- */ #define XNV_SPI_BEGIN() st(P1_3 = 0;) #define XNV_SPI_TX(x) st(U1CSR &= ~0x02; U1DBUF = (x);) #define XNV_SPI_RX() U1DBUF #define XNV_SPI_WAIT_RXRDY() st(while (!(U1CSR & 0x02));) #define XNV_SPI_END() st(P1_3 = 1;)// The TI reference design uses UART1 Alt. 2 in SPI mode. #define XNV_SPI_INIT() \ st( \/* Mode select UART1 SPI Mode as master. */\U1CSR = 0; \\/* Setup for 115200 baud. */\U1GCR = 11; \U1BAUD = 216; \\/* Set bit order to MSB */\U1GCR |= BV(5); \\/* Set UART1 I/O to alternate 2 location on P1 pins. */\PERCFG |= 0x02; /* U1CFG */\\/* Select peripheral function on I/O pins but SS is left as GPIO for separate control. */\P1SEL |= 0xE0; /* SELP1_[7:4] */\/* P1.1,2,3: reset, LCD CS, XNV CS. */\P1SEL &= ~0x0E; \P1 |= 0x0E; \P1_1 = 0; \P1DIR |= 0x0E; \\/* Give UART1 priority over Timer3. */\P2SEL &= ~0x20; /* PRI2P1 */\\/* When SPI config is complete, enable it. */\U1CSR |= 0x40; \/* Release XNV reset. */\P1_1 = 1; \ )/* ------------------------------------------------------------------------------------------------* Driver Configuration* ------------------------------------------------------------------------------------------------*//* Set to TRUE enable H/W TIMER usage, FALSE disable it */ #ifndef HAL_TIMER #define HAL_TIMER FALSE #endif/* Set to TRUE enable ADC usage, FALSE disable it */ #ifndef HAL_ADC #define HAL_ADC TRUE #endif/* Set to TRUE enable DMA usage, FALSE disable it */ #ifndef HAL_DMA #define HAL_DMA TRUE #endif/* Set to TRUE enable Flash access, FALSE disable it */ #ifndef HAL_FLASH #define HAL_FLASH TRUE #endif/* Set to TRUE enable AES usage, FALSE disable it */ #ifndef HAL_AES #define HAL_AES TRUE #endif#ifndef HAL_AES_DMA #define HAL_AES_DMA TRUE #endif/* Set to TRUE enable LCD usage, FALSE disable it */ #ifndef HAL_LCD #define HAL_LCD TRUE #endif/* Set to TRUE enable LED usage, FALSE disable it */ #ifndef HAL_LED #define HAL_LED TRUE #endif #if (!defined BLINK_LEDS) && (HAL_LED == TRUE) #define BLINK_LEDS #endif/* Set to TRUE enable KEY usage, FALSE disable it */ #ifndef HAL_KEY #define HAL_KEY TRUE #endif/* Set to TRUE enable UART usage, FALSE disable it */ #ifndef HAL_UART #if (defined ZAPP_P1) || (defined ZAPP_P2) || (defined ZTOOL_P1) || (defined ZTOOL_P2) #define HAL_UART TRUE #else #define HAL_UART FALSE #endif #endif#if HAL_UART // Always prefer to use DMA over ISR. #if HAL_DMA #ifndef HAL_UART_DMA #if (defined ZAPP_P1) || (defined ZTOOL_P1) #define HAL_UART_DMA 1 #elif (defined ZAPP_P2) || (defined ZTOOL_P2) #define HAL_UART_DMA 2 #else #define HAL_UART_DMA 1 #endif #endif #define HAL_UART_ISR 0 #else #ifndef HAL_UART_ISR #if (defined ZAPP_P1) || (defined ZTOOL_P1) #define HAL_UART_ISR 1 #elif (defined ZAPP_P2) || (defined ZTOOL_P2) #define HAL_UART_ISR 2 #else #define HAL_UART_ISR 1 #endif #endif #define HAL_UART_DMA 0 #endif// Used to set P2 priority - USART0 over USART1 if both are defined. #if ((HAL_UART_DMA == 1) || (HAL_UART_ISR == 1)) #define HAL_UART_PRIPO 0x00 #else #define HAL_UART_PRIPO 0x40 #endif#else #define HAL_UART_DMA 0 #define HAL_UART_ISR 0 #endif/******************************************************************************************************* */ #endifhal_key.h
/**************************************************************************************************Filename: hal_key.hRevised: $Date: 2007-07-06 10:42:24 -0700 (Fri, 06 Jul 2007) $Revision: $Revision: 13579 $Description: This file contains the interface to the KEY Service.Copyright 2005-2007 Texas Instruments Incorporated. All rights reserved.IMPORTANT: Your use of this Software is limited to those specific rightsgranted under the terms of a software license agreement between the userwho downloaded the software, his/her employer (which must be your employer)and Texas Instruments Incorporated (the "License"). You may not use thisSoftware unless you agree to abide by the terms of the License. The Licenselimits your use, and you acknowledge, that the Software may not be modified,copied or distributed unless embedded on a Texas Instruments microcontrolleror used solely and exclusively in conjunction with a Texas Instruments radiofrequency transceiver, which is integrated into your product. Other than forthe foregoing purpose, you may not use, reproduce, copy, prepare derivativeworks of, modify, distribute, perform, display or sell this Software and/orits documentation for any purpose.YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION AREPROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALLTEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHERLEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSESINCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVEOR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENTOF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.Should you have any questions regarding your right to use this Software,contact Texas Instruments Incorporated at www.TI.com. **************************************************************************************************/#ifndef HAL_KEY_H #define HAL_KEY_H#ifdef __cplusplus extern "C" { #endif/*************************************************************************************************** INCLUDES**************************************************************************************************/ #include "hal_board.h"/*************************************************************************************************** MACROS**************************************************************************************************//*************************************************************************************************** CONSTANTS**************************************************************************************************//* Interrupt option - Enable or disable */ #define HAL_KEY_INTERRUPT_DISABLE 0x00 #define HAL_KEY_INTERRUPT_ENABLE 0x01/* Key state - shift or nornal */ #define HAL_KEY_STATE_NORMAL 0x00 #define HAL_KEY_STATE_SHIFT 0x01/* Switches (keys) */ #define HAL_KEY_SW_1 0x01 // Joystick up #define HAL_KEY_SW_2 0x02 // Joystick right #define HAL_KEY_SW_5 0x04 // Joystick center #define HAL_KEY_SW_4 0x08 // Joystick left #define HAL_KEY_SW_3 0x10 // Joystick down #define HAL_KEY_SW_6 0x20 // Button S1 if available #define HAL_KEY_SW_7 0x40 // Button S2 if available/* Joystick */ #define HAL_KEY_UP 0x01 // Joystick up #define HAL_KEY_RIGHT 0x02 // Joystick right #define HAL_KEY_CENTER 0x04 // Joystick center #define HAL_KEY_LEFT 0x08 // Joystick left #define HAL_KEY_DOWN 0x10 // Joystick down/*************************************************************************************************** TYPEDEFS**************************************************************************************************/ typedef void (*halKeyCBack_t) (uint8 keys, uint8 state);/*************************************************************************************************** GLOBAL VARIABLES**************************************************************************************************/ extern bool Hal_KeyIntEnable;/*************************************************************************************************** FUNCTIONS - API**************************************************************************************************//** Initialize the Key Service*/ extern void HalKeyInit( void );/** Configure the Key Service*/ extern void HalKeyConfig( bool interruptEnable, const halKeyCBack_t cback);/** Read the Key status*/ extern uint8 HalKeyRead( void);/** Enter sleep mode, store important values*/ extern void HalKeyEnterSleep ( void );/** Exit sleep mode, retore values*/ extern uint8 HalKeyExitSleep ( void );/** This is for internal used by hal_driver*/ extern void HalKeyPoll ( void );/** This is for internal used by hal_sleep*/ extern bool HalKeyPressed( void );/************************************************************************************************** **************************************************************************************************/#ifdef __cplusplus } #endif#endifhal_key.c
/**************************************************************************************************Filename: hal_key.cRevised: $Date: 2008-12-15 07:19:03 -0800 (Mon, 15 Dec 2008) $Revision: $Revision: 18611 $Description: This file contains the interface to the HAL KEY Service.Copyright 2006-2007 Texas Instruments Incorporated. All rights reserved.IMPORTANT: Your use of this Software is limited to those specific rightsgranted under the terms of a software license agreement between the userwho downloaded the software, his/her employer (which must be your employer)and Texas Instruments Incorporated (the "License"). You may not use thisSoftware unless you agree to abide by the terms of the License. The Licenselimits your use, and you acknowledge, that the Software may not be modified,copied or distributed unless embedded on a Texas Instruments microcontrolleror used solely and exclusively in conjunction with a Texas Instruments radiofrequency transceiver, which is integrated into your product. Other than forthe foregoing purpose, you may not use, reproduce, copy, prepare derivativeworks of, modify, distribute, perform, display or sell this Software and/orits documentation for any purpose.YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION AREPROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALLTEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHERLEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSESINCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVEOR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENTOF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.Should you have any questions regarding your right to use this Software,contact Texas Instruments Incorporated at www.TI.com. **************************************************************************************************/ /*********************************************************************NOTE: If polling is used, the hal_driver task schedules the KeyRead()to occur every 100ms. This should be long enough to naturallydebounce the keys. The KeyRead() function remembers the keystate of the previous poll and will only return a non-zerovalue if the key state changes.NOTE: If interrupts are used, the KeyRead() function is scheduled25ms after the interrupt occurs by the ISR. This delay is usedfor key debouncing. The ISR disables any further Key interruptuntil KeyRead() is executed. KeyRead() will re-enable Keyinterrupts after executing. Unlike polling, when interruptsare enabled, the previous key state is not remembered. Thismeans that KeyRead() will return the current state of the keys(not a change in state of the keys).NOTE: If interrupts are used, the KeyRead() fucntion is scheduled bythe ISR. Therefore, the joystick movements will only be detectedduring a pushbutton interrupt caused by S1 or the center joystickpushbutton.NOTE: When a switch like S1 is pushed, the S1 signal goes from a normallyhigh state to a low state. This transition is typically clean. Theduration of the low state is around 200ms. When the signal returnsto the high state, there is a high likelihood of signal bounce, whichcauses a unwanted interrupts. Normally, we would set the interruptedge to falling edge to generate an interrupt when S1 is pushed, butbecause of the signal bounce, it is better to set the edge to risingedge to generate an interrupt when S1 is released. The debounce logiccan then filter out the signal bounce. The result is that we typicallyget only 1 interrupt per button push. This mechanism is not totallyfoolproof because occasionally, signal bound occurs during the fallingedge as well. A similar mechanism is used to handle the joystickpushbutton on the DB. For the EB, we do not have independent controlof the interrupt edge for the S1 and center joystick pushbutton. Asa result, only one or the other pushbuttons work reasonably well withinterrupts. The default is the make the S1 switch on the EB work morereliably.*********************************************************************//*************************************************************************************************** INCLUDES**************************************************************************************************/ #include "hal_mcu.h" #include "hal_defs.h" #include "hal_types.h" #include "hal_drivers.h" #include "hal_adc.h" #include "hal_key.h" #include "osal.h"#if (defined HAL_KEY) && (HAL_KEY == TRUE)/*************************************************************************************************** MACROS**************************************************************************************************//*************************************************************************************************** CONSTANTS**************************************************************************************************/ #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 */ #define HAL_KEY_CPU_PORT_0_IF P0IF #define HAL_KEY_CPU_PORT_2_IF P2IF/* SW_6 is at P0.1 官方 #define HAL_KEY_SW_6_PORT P0 #define HAL_KEY_SW_6_BIT BV(1) #define HAL_KEY_SW_6_SEL P0SEL #define HAL_KEY_SW_6_DIR P0DIR*//* SW_6 is at P0.4 luke */ #define HAL_KEY_SW_6_PORT P0 #define HAL_KEY_SW_6_BIT BV(4) #define HAL_KEY_SW_6_SEL P0SEL #define HAL_KEY_SW_6_DIR P0DIR/* edge interrupt */ #define HAL_KEY_SW_6_EDGEBIT BV(0) #define HAL_KEY_SW_6_EDGE HAL_KEY_FALLING_EDGE/* SW_6 interrupts */ #define HAL_KEY_SW_6_IEN IEN1 /* CPU interrupt mask register */ #define HAL_KEY_SW_6_IENBIT BV(5) /* Mask bit for all of Port_0 */ #define HAL_KEY_SW_6_ICTL P0IEN /* Port Interrupt Control register */ #define HAL_KEY_SW_6_ICTLBIT BV(1) /* P0IEN - P0.1 enable/disable bit */ #define HAL_KEY_SW_6_PXIFG P0IFG /* Interrupt flag at source *//* Joy stick move at P2.0 luke*/ #define HAL_KEY_JOY_MOVE_PORT 0//P2 #define HAL_KEY_JOY_MOVE_BIT 0//BV(0) #define HAL_KEY_JOY_MOVE_SEL 0//P2SEL #define HAL_KEY_JOY_MOVE_DIR 0//P2DIR/* edge interrupt */ #define HAL_KEY_JOY_MOVE_EDGEBIT BV(3) #define HAL_KEY_JOY_MOVE_EDGE HAL_KEY_FALLING_EDGE/* Joy move interrupts */ #define HAL_KEY_JOY_MOVE_IEN IEN2 /* CPU interrupt mask register */ #define HAL_KEY_JOY_MOVE_IENBIT BV(1) /* Mask bit for all of Port_2 */ #define HAL_KEY_JOY_MOVE_ICTL P2IEN /* Port Interrupt Control register */ #define HAL_KEY_JOY_MOVE_ICTLBIT BV(0) /* P2IENL - P2.0<->P2.3 enable/disable bit */ #define HAL_KEY_JOY_MOVE_PXIFG P2IFG /* Interrupt flag at source */#define HAL_KEY_JOY_CHN HAL_ADC_CHANNEL_6/*************************************************************************************************** TYPEDEFS**************************************************************************************************//*************************************************************************************************** GLOBAL VARIABLES**************************************************************************************************/ static uint8 halKeySavedKeys; /* used to store previous key state in polling mode */ static halKeyCBack_t pHalKeyProcessFunction; static uint8 HalKeyConfigured; bool Hal_KeyIntEnable; /* interrupt enable/disable flag *//*************************************************************************************************** FUNCTIONS - Local**************************************************************************************************/ void halProcessKeyInterrupt(void); uint8 halGetJoyKeyInput(void);/*************************************************************************************************** FUNCTIONS - API**************************************************************************************************//*************************************************************************************************** @fn HalKeyInit** @brief Initilize Key Service** @param none** @return None**************************************************************************************************/ void HalKeyInit( void ) {/* Initialize previous key to 0 */halKeySavedKeys = 0;HAL_KEY_SW_6_SEL &= ~(HAL_KEY_SW_6_BIT); /* Set pin function to GPIO */HAL_KEY_SW_6_DIR &= ~(HAL_KEY_SW_6_BIT); /* Set pin direction to Input *///HAL_KEY_JOY_MOVE_SEL &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin function to GPIO *///HAL_KEY_JOY_MOVE_DIR &= ~(HAL_KEY_JOY_MOVE_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_6_EDGEBIT); /* Clear the edge bit *//* For falling edge, the bit must be set. */#if (HAL_KEY_SW_6_EDGE == HAL_KEY_FALLING_EDGE)PICTL |= HAL_KEY_SW_6_EDGEBIT;#endif/* Interrupt configuration:* - Enable interrupt generation at the port* - Enable CPU interrupt* - Clear any pending interrupt*/HAL_KEY_SW_6_ICTL |= HAL_KEY_SW_6_ICTLBIT;HAL_KEY_SW_6_IEN |= HAL_KEY_SW_6_IENBIT;HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT);/* Rising/Falling edge configuratinn */HAL_KEY_JOY_MOVE_ICTL &= ~(HAL_KEY_JOY_MOVE_EDGEBIT); /* Clear the edge bit *//* For falling edge, the bit must be set. */#if (HAL_KEY_JOY_MOVE_EDGE == HAL_KEY_FALLING_EDGE)HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_EDGEBIT;#endif/* Interrupt configuration:* - Enable interrupt generation at the port* - Enable CPU interrupt* - Clear any pending interrupt*/HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_ICTLBIT;HAL_KEY_JOY_MOVE_IEN |= HAL_KEY_JOY_MOVE_IENBIT;HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_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_6_ICTL &= ~(HAL_KEY_SW_6_ICTLBIT); /* don't generate interrupt */HAL_KEY_SW_6_IEN &= ~(HAL_KEY_SW_6_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;#ifdef HAL_BOARD_CC2530EB_REV17if ( (HAL_KEY_SW_6_PORT & HAL_KEY_SW_6_BIT)) /* Key is active high */ #elif defined (HAL_BOARD_CC2530EB_REV13)if (!(HAL_KEY_SW_6_PORT & HAL_KEY_SW_6_BIT)) /* Key is active low */ #endif{keys |= HAL_KEY_SW_6;}if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT)) /* Key is active low */{//keys |= halGetJoyKeyInput();}return keys; }/*************************************************************************************************** @fn HalKeyPoll** @brief Called by hal_driver to poll the keys** @param None** @return None**************************************************************************************************/ void HalKeyPoll (void) {uint8 keys = 0;if (!(HAL_KEY_SW_6_PORT & HAL_KEY_SW_6_BIT)) /* Key is active low */{keys |= HAL_KEY_SW_6;}if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT)) /* Key is active HIGH */{//keys = halGetJoyKeyInput();}/* 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 */}/* 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;/* Keep on reading the ADC until two consecutive key decisions are the same. */do{ksave1 = ksave0; /* save previouse key reading */adc = HalAdcRead (HAL_KEY_JOY_CHN, HAL_ADC_RESOLUTION_8);if ((adc >= 2) && (adc <= 38)){ksave0 |= HAL_KEY_UP;}else if ((adc >= 74) && (adc <= 88)){ksave0 |= HAL_KEY_RIGHT;}else if ((adc >= 60) && (adc <= 73)){ksave0 |= HAL_KEY_LEFT;}else if ((adc >= 39) && (adc <= 59)){ksave0 |= HAL_KEY_DOWN;}else if ((adc >= 89) && (adc <= 100)){ksave0 |= HAL_KEY_CENTER;}} while (ksave0 != 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_6_PXIFG & HAL_KEY_SW_6_BIT) /* Interrupt Flag has been set */{HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT); /* Clear Interrupt Flag */valid = TRUE;}if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT) /* Interrupt Flag has been set */{HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_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_6_PXIFG & HAL_KEY_SW_6_BIT){halProcessKeyInterrupt();}/*Clear the CPU interrupt flag for Port_0PxIFG has to be cleared before PxIF*/HAL_KEY_SW_6_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_2PxIFG has to be cleared before PxIFNotes: P2_1 and P2_2 are debug lines.*/HAL_KEY_JOY_MOVE_PXIFG = 0;HAL_KEY_CPU_PORT_2_IF = 0; }#elsevoid HalKeyInit(void){} void HalKeyConfig(bool interruptEnable, halKeyCBack_t cback){} uint8 HalKeyRead(void){ return 0;} void HalKeyPoll(void){}#endif /* HAL_KEY *//************************************************************************************************** **************************************************************************************************/總結
以上是生活随笔為你收集整理的z-stac配置按键的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu18.4使用SystemMo
- 下一篇: 反物质猜想