1、 InitBoard( OB_COLD );
2、void InitBoard( uint8 level )
{
if ( level == OB_COLD )
{
// IAR does not zero-out this byte below the XSTACK.
*(uint8 *)0x0 = 0;//不懂
// Interrupts off
osal_int_disable( INTS_ALL );
// Check for Brown-Out reset
ChkReset();
}
else // !OB_COLD
{
/* Initialize Key stuff */
HalKeyConfig(HAL_KEY_INTERRUPT_DISABLE, OnBoard_KeyCallback);
}
}
3、
void ChkReset( void )
{
uint8 rib;
// Isolate reset indicator bits
rib = SLEEPSTA & LRESET;//SLEEPSTA 睡眠模式控制状态,LRESET #define LRESET 0x18 // Last reset bit mask,时钟丢失复位
if ( rib == RESETPO )// /*#define RESETPO 0x00 // Power-On reset*/
/*#define RESETEX 0x08 // External reset*/
/*#define RESETWD 0x10 // WatchDog reset*/
{
// Put code here to handle Power-On reset
}
else if ( rib == RESETEX )
{
// Put code here to handle External reset
}
else if ( rib == RESETWD )
{
// Put code here to handle WatchDog reset
}
else // Unknown reason - not expected.
{
HAL_ASSERT(0);
}
}
4、
HalKeyConfig(HAL_KEY_INTERRUPT_DISABLE, OnBoard_KeyCallback);
其中:
/* Interrupt option - Enable or disable */
#define HAL_KEY_INTERRUPT_DISABLE 0x00
#define HAL_KEY_INTERRUPT_ENABLE 0x01
OnBoard_KeyCallback找到后竟然是个函数
typedef void (*halKeyCBack_t) (uint8 keys, uint8 state);//这个结构不太好理解啊
首先typedef是用来定义类型的,void (*halKeyCBack_t) (uint8 keys, uint8 state)是用来定义函数指针的,连在一起就是把这个函数指针定义为类型。 使用的时候 halKeyCBack OnBoard_KeyCallback;
函数原型:
/**************************************************************************************************
* @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 */
P0ICON是PICTL的第0位,P0ICON=1输入的下降沿引起中断,P0ICON=0输入的上升沿引起中断。
/* edge interrupt */
#define HAL_KEY_SW_6_EDGEBIT BV(0)
#define HAL_KEY_SW_6_EDGE HAL_KEY_FALLING_EDGE
#define HAL_KEY_RISING_EDGE 0
#define HAL_KEY_FALLING_EDGE 1
*/
/* 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
*/
/*
/*
* #define HAL_KEY_SW_6_IEN IEN1
* #define HAL_KEY_SW_6_IENBIT BV(5)
* #define HAL_KEY_SW_6_ICTL P0IEN
*#define HAL_KEY_SW_6_ICTLBIT BV(1)
*#define HAL_KEY_SW_6_PXIFG P0IFG
*/
HAL_KEY_SW_6_ICTL |= HAL_KEY_SW_6_ICTLBIT;// P0IEN 端口0中断使能,端口P0.7到P0.0中断使能,0:中断禁用,1:中断使能,P0_1中断使能
HAL_KEY_SW_6_IEN |= HAL_KEY_SW_6_IENBIT;//IEN1 (0xB8) – 中断使能1。第6位为1,即P0IE=1,端口0中断使能,0: 中断禁止,1: 中断使能,
HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT);//P0IFG 中断标志,将第二位置为0,清除中断标志
/*
/* 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
*/
/* 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
*/
/* Joy stick move at P2.0 */
#define HAL_KEY_JOY_MOVE_PORT P2
#define HAL_KEY_JOY_MOVE_BIT BV(0)
#define HAL_KEY_JOY_MOVE_SEL P2SEL
#define HAL_KEY_JOY_MOVE_DIR 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
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)//?????没看到HalKeyConfigured
{
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_set_event(Hal_TaskID, HAL_KEY_EVENT);
}
/* Key now is configured */
HalKeyConfigured = TRUE;
}
5、
/*********************************************************************
* @fn OnBoard_KeyCallback
*
* @brief Callback service for keys
*
* @param keys - keys that were pressed
* state - shifted
*
* @return void
*********************************************************************/
void OnBoard_KeyCallback ( uint8 keys, uint8 state )
{
uint8 shift;
(void)state;
shift = (keys & HAL_KEY_SW_6) ? true : false;//HAL_KEY_SW_6 #define HAL_KEY_SW_6 0x20 // Button S1 if available
if ( OnBoard_SendKeys( keys, shift ) != ZSuccess )
{
// 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
{
}
}
}
6、
/*********************************************************************
* @fn OnBoard_SendKeys
*
* @brief Send "Key Pressed" message to application.
*
* @param keys - keys that were pressed
* state - shifted
*
* @return status
*********************************************************************/
uint8 OnBoard_SendKeys( uint8 keys, uint8 state )
{
keyChange_t *msgPtr;
if ( registeredKeysTaskID != NO_TASK_ID )
{
// Send the address to the task
msgPtr = (keyChange_t *)osal_msg_allocate( sizeof(keyChange_t) );
if ( msgPtr )
{
msgPtr->hdr.event = KEY_CHANGE;
msgPtr->state = state;
msgPtr->keys = keys;
osal_msg_send( registeredKeysTaskID, (uint8 *)msgPtr );
}
return ( ZSuccess );
}
else
return ( ZFailure );
}
/*
typedef struct
{
osal_event_hdr_t hdr;
uint8 state; // shift
uint8 keys; // keys
} keyChange_t;
*/
// Registered keys task ID, initialized to NOT USED.
static uint8 registeredKeysTaskID = NO_TASK_ID;
7、
/*********************************************************************
* @fn osal_msg_send
*
* @brief
*
* This function is called by a task to send a command message to
* another task or processing element. The sending_task field must
* refer to a valid task, since the task ID will be used
* for the response message. This function will also set a message
* ready event in the destination tasks event list.
*
*
* @param uint8 destination task - Send msg to? Task ID
* @param uint8 *msg_ptr - pointer to new message buffer
* @param uint8 len - length of data in message
*
* @return SUCCESS, INVALID_TASK, INVALID_MSG_POINTER
*/
uint8 osal_msg_send( uint8 destination_task, uint8 *msg_ptr )
{
if ( msg_ptr == NULL )
return ( INVALID_MSG_POINTER );
if ( destination_task >= tasksCnt )
{
osal_msg_deallocate( msg_ptr );
return ( INVALID_TASK );
}
// Check the message header
if ( OSAL_MSG_NEXT( msg_ptr ) != NULL ||
OSAL_MSG_ID( msg_ptr ) != TASK_NO_TASK )
{
osal_msg_deallocate( msg_ptr );
return ( INVALID_MSG_POINTER );
}
OSAL_MSG_ID( msg_ptr ) = destination_task;
// queue message
osal_msg_enqueue( &osal_qHead, msg_ptr );
// Signal the task that a message is waiting
osal_set_event( destination_task, SYS_EVENT_MSG );
return ( SUCCESS );
}
8、
/*********************************************************************
* "Keyboard" Support
*********************************************************************/
/*********************************************************************
* Keyboard Register function
*
* The keyboard handler is setup to send all keyboard changes to
* one task (if a task is registered).
*
* If a task registers, it will get all the keys. You can change this
* to register for individual keys.
*********************************************************************/
uint8 RegisterForKeys( uint8 task_id )
{
// Allow only the first task
if ( registeredKeysTaskID == NO_TASK_ID )
{
registeredKeysTaskID = task_id;
return ( true );
}
else
return ( false );
}