Holla. I have a question about the void OnBoard_KeyCallBack, is this an interrupt? If yes how can I use it? Since my teacher told me configure the switch using this OnBoard_KeyCallBack, is it possible to do that?
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Holla. I have a question about the void OnBoard_KeyCallBack, is this an interrupt? If yes how can I use it? Since my teacher told me configure the switch using this OnBoard_KeyCallBack, is it possible to do that?
Ah, so it cannot use as interrupt something like that? And does it mean it basically serve only the key event for hal_key.c?
You can refer to the red codes in InitBoard(). You should use HalKeyConfig to enable interrupt and connect OnBoard_KeyCallback.
void InitBoard( uint8 level )
{
if ( level == OB_COLD )
{
// Interrupts off
osal_int_disable( INTS_ALL );
// Turn all LEDs off
HalLedSet( HAL_LED_ALL, HAL_LED_MODE_OFF );
// Check for Brown-Out reset
// ChkReset();
}
else // !OB_COLD
{
/* Initialize Key stuff */
OnboardKeyIntEnable = HAL_KEY_INTERRUPT_ENABLE;
HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);
}
}
Sorry for the late reply and I have few questions, hope you can help me a bit.
1. Since the "HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);" and " OnboardKeyIntEnable = HAL_KEY_INTERRUPT_ENABLE;" is inside Onboard.c , that means I can only use this function in Onboard.c only right?
2. void OnBoard_KeyCallback ( uint8 keys, uint8 state )
{
uint8 shift;
(void)state;
// shift key (S1) is used to generate key interrupt
// applications should not use S1 when key interrupt is enabled
shift = (OnboardKeyIntEnable == HAL_KEY_INTERRUPT_ENABLE) ? false : ((keys & HAL_KEY_SW_6) ? true : false);
if ( OnBoard_SendKeys( keys, shift ) != SUCCESS )
{
// Process SW1 here
if ( keys & HAL_KEY_SW_1 ) // Switch 1
{
}
// Process SW2 here
if ( keys & HAL_KEY_SW_2 ) // Switch 2
{
}
// Process SW3 here
if ( keys & HAL_KEY_SW_3 ) // Switch 3
{
}
// Process SW4 here
if ( keys & HAL_KEY_SW_4 ) // Switch 4
{
}
// Process SW5 here
if ( keys & HAL_KEY_SW_5 ) // Switch 5
{
}
// Process SW6 here
if ( keys & HAL_KEY_SW_6 ) // Switch 6
{
}
}
/* If any key is currently pressed down and interrupt
is still enabled, disable interrupt and switch to polling */
if( keys != 0 )
{
if( OnboardKeyIntEnable == HAL_KEY_INTERRUPT_ENABLE )
{
OnboardKeyIntEnable = HAL_KEY_INTERRUPT_DISABLE;
HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);
}
}
/* If no key is currently pressed down and interrupt
is disabled, enable interrupt and turn off polling */
else
{
if( OnboardKeyIntEnable == HAL_KEY_INTERRUPT_DISABLE )
{
OnboardKeyIntEnable = HAL_KEY_INTERRUPT_ENABLE;
HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);
}
}
}
There are few questions regarding the code above, the 1st question is the blue code. Izzit the code means if the input cannot be detected , it will be process under the if ( keys & HAL_KEY_SW_4 ) there? But why inside it is empty?
Then the red code, why we need to disable the interrupt if the key is pressed down and enable the interrupt when there is no key is pressed down?
Hope you can answer me and sorry for those silly questions >.<
Thanks for your reply and your answer is clear for me!!!
One more thing, if I want to use OnboardKeyIntEnable = HAL_KEY_INTERRUPT_ENABLE; and HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback); for my switch, means I can only used it in Onboard.c since it is declare inside Onboard.c ?
Hey Yikai, I am not sure whether the interrupt will work or not if I put like this. My code is like this and I hope you can gimme some answer, I put the code in Onboard.c
void InitBoard( uint8 level )
{
P1SEL = 0x00;
P1DIR = 0x03;
P2SEL = 0x00;
P2DIR = 0x00;
if ( level == OB_COLD )
{
// Interrupts off
osal_int_disable( INTS_ALL );
// Turn all LEDs off
#ifndef FEATURE_OAD
HalLedSet( HAL_LED_ALL, HAL_LED_MODE_OFF );
#endif
// Check for Brown-Out reset
// ChkReset();
}
else // !OB_COLD
{
#ifndef FEATURE_OAD
/* Initialize Key stuff */
while(1)
{
if( P2_0 == 0 )
{
OnboardKeyIntEnable = HAL_KEY_INTERRUPT_ENABLE;
HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);
if ( P2_0 == 0 )
{
P1_0 = 1;
P1_1 = 1;
}
else
{
P1_0 = 0;
P1_1 = 0;
}
}
else
{
P1_0 = 0;
P1_1 = 0;
}
}
#endif
}
}
The blue code is the code I type. The port 1 here is LED where port is for my switch. My switch is 0 when I pressed down and 1 when the switch is not pressed.
#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"
/*#ifdef FOBO_ENABLED
#include "isr.h"
#include "key.h"
#endif
*/
#if (defined HAL_KEY) && (HAL_KEY == TRUE)
#define HAL_KEY_RISING_EDGE 0
#define HAL_KEY_FALLING_EDGE 1
#define HAL_KEY_DEBOUNCE_VALUE 50
/* CPU port interrupt */
#define HAL_KEY_CPU_PORT_0_IF P0IF
#define HAL_KEY_CPU_PORT_1_IF P1IF
#define HAL_KEY_CPU_PORT_2_IF P2IF
#define HAL_KEY_SW3_PORT P2
#define HAL_KEY_SW3_BIT BV(0)
#define HAL_KEY_SW3_SEL P2SEL
#define HAL_KEY_SW3_DIR P2DIR
#define HAL_KEY_SW3_EDGE HAL_KEY_FALLING_EDGE
#define HAL_KEY_SW3_EDGEBIT BV(3)
#define HAL_KEY_SW3_IEN IEN2
#define HAL_KEY_SW3_IENBIT BV(1)
#define HAL_KEY_SW3_ICTL P2IEN
#define HAL_KEY_SW3_ICTLBIT BV(0)
#define HAL_KEY_SW3_PXIFG P2IFG
static uint8 halKeySavedKeys; /* used to store previous key state in polling mode */
static halKeyCBack_t pHalKeyProcessFunction;
bool Hal_KeyIntEnable; /* interrupt enable/disable flag */
void halProcessKeyInterrupt(void);
___________________________________________________________________________________________________
void HalKeyInit( void )
{
halKeySavedKeys = 0; // Initialize previous key to 0.
HAL_KEY_SW3_SEL &= ~(HAL_KEY_SW3_BIT);
HAL_KEY_SW3_DIR &= ~(HAL_KEY_SW3_BIT);
P2INP = 0x01;
/* Initialize callback function */
pHalKeyProcessFunction = NULL;
}
______________________________________________________________________________________________________
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)
{
HAL_KEY_SW3_ICTL &= ~(HAL_KEY_SW3_EDGEBIT);
#if(HAL_KEY_SW3_EDGE == HAL_KEY_FALLING_EDGE)
HAL_KEY_SW3_ICTL |= HAL_KEY_SW3_EDGEBIT;
#endif
HAL_KEY_SW3_ICTL |= HAL_KEY_SW3_ICTLBIT;
HAL_KEY_SW3_IEN |= HAL_KEY_SW3_IENBIT;
HAL_KEY_SW3_PXIFG = ~(HAL_KEY_SW3_BIT);
}
else /* Interrupts NOT enabled */
{
HAL_KEY_SW3_ICTL &= ~(HAL_KEY_SW3_ICTLBIT);
HAL_KEY_SW3_IEN &= ~(HAL_KEY_SW3_IENBIT);
HAL_KEY_SW3_PXIFG = ~(HAL_KEY_SW3_BIT);
}
}
_______________________________________________________________________________________________
uint8 HalKeyRead ( void )
{
uint8 keys = 0;
if((HAL_KEY_SW3_PORT & HAL_KEY_SW3_BIT))
{
keys |= HAL_KEY_SW3_BIT;
}
return keys;
}
_______________________________________________________________________________________________
void HalKeyPoll (void)
{
uint8 keys = 0;
uint8 notify = 0;
if(!(HAL_KEY_SW3_PORT & HAL_KEY_SW3_BIT))
{
keys |= HAL_KEY_SW3_BIT;;
}
/* 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;
}
else
{
notify = 1;
}
}
else
{
/* Key interrupt handled here */
if (keys)
{
notify = 1;
}
}
/* Store the current keys for comparation next time */
halKeySavedKeys = keys;
/* Invoke Callback if new keys were depressed */
if (notify && (pHalKeyProcessFunction))
{
(pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);
}
}
________________________________________________________________________________________________
void halProcessKeyInterrupt (void)
{
bool valid = FALSE;
if(HAL_KEY_SW3_PXIFG & HAL_KEY_SW3_BIT)
{
HAL_KEY_SW3_PXIFG = ~(HAL_KEY_SW3_BIT);
valid = TRUE;
}
if(valid)
{
osal_start_timerEx( Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE );
}
}
___________________________________________________________________________________________________
void HalKeyEnterSleep ( void )
{
return;
}
__________________________________________________________________________________________________
uint8 HalKeyExitSleep ( void )
{
/* Wake up and read keys */
return ( HalKeyRead () );
}
__________________________________________________________________________________________________
//ISR
HAL_ISR_FUNCTION( halKeyPort2Isr, P2INT_VECTOR )
{
HAL_ENTER_ISR();
if( HAL_KEY_SW3_PXIFG & HAL_KEY_SW3_BIT )
{
halProcessKeyInterrupt();
}
HAL_KEY_SW3_PXIFG = 0;
HAL_KEY_CPU_PORT_2_IF = 0;
CLEAR_SLEEP_MODE();
HAL_EXIT_ISR();
return;
}
#else
void HalKeyInit(void){}
void HalKeyConfig(bool interruptEnable, halKeyCBack_t cback){}
uint8 HalKeyRead(void){ return 0;}
void HalKeyPoll(void){}
#endif
The blue code was typed by me. Sorry for disturbing.