在Pin_init() 函数中构建了 Hwi 和Swi 线程
// Setup HWI handler
Hwi_Params_init(&hwiParams);
hwiParams.priority = PINCC26XX_hwAttrs.intPriority;
Hwi_construct(&PinHwi, INT_AON_GPIO_EDGE, PIN_hwi,&hwiParams, NULL);
// Setup SWI handler
Swi_Params_init(&(swiParams));
swiParams.priority = PINCC26XX_hwAttrs.swiPriority;
swiParams.trigger = 0;
Swi_construct(&PinSwi, PIN_swi, &(swiParams), NULL);
Hwi的中断号对应着GPIO的中断向量号
中断发生时,Hwi响应。清除中断标准位,并唤醒Swi线程。
// I/O HWI service routine
static void PIN_hwi(UArg arg) {
uint32_t iEvent;
// Get event flag with lowest index (also pin ID)
iEvent = HWREG(GPIO_BASE + GPIO_O_EVFLAGS31_0);
// Clear this event flag
HWREG(GPIO_NONBUF_BASE + GPIO_O_EVFLAGS31_0) = iEvent;
// Include all GPIO's currently triggered in the SWI
Swi_or(Swi_handle(&(PinSwi)), iEvent);
}
Swi查询中断来源并执行应用层程序传来的回调函数。
// I/O SWI service routine that posts the callback in a SWI context
static void PIN_swi(UArg arg0, UArg arg1){
uint32_t iEvent;
unsigned char iEventCounter;
PIN_Handle handle;
PIN_IntCb pCb;
// Get the OR'd trigger value representing all iEvent values prior to running the SWI
iEvent = Swi_getTrigger();
// iEventCounter cycles through all pins on the device up to the max number of pins
for(iEventCounter = 0; iEventCounter < PIN_NumPins; iEventCounter++){
// Check if current iEventCounter bit is set in iEvent
if(iEvent & (1 << iEventCounter)){
// Get pin handle and registered callback function
// Double paranthesis to supress GCC warning
// Intentional assignment is intended, not the equality comparison
if((handle = PIN_HandleTable[iEventCounter])) {
if((pCb = handle->pCbFunc)) {
// Event from existing pin, with an associated handle and a
// registered callback -> call callback
// Run the callback function in a SWI context.
pCb(handle, iEventCounter);
}
}
}
}
}
应用层完成的就是回调函数的注册
PIN_registerIntCb(PIN_Handle,*callback);
注册工作就是完成Pin_Handle结构体的的pCbFunc域赋值。
PIN_Status PIN_registerIntCb(PIN_Handle handle, PIN_IntCb pCb) {
if (handle) {
handle->pCbFunc = pCb;
return PIN_SUCCESS;
} else {
return PIN_NO_ACCESS;
}
}
另外按键PIO的配置和中断使能是在Pin_open()函数中实现的。