1 简介
使用创龙板卡的KEY2按键通过中断的方式控制LED3的亮灭
2 中断学习
在C665x设备上,CPU中断是通过C66x CorePac中断控制器进行配置的。该中断控制器允许最多128个系统事件被编程到任意12个CPU可屏蔽中断输入(CPUINT4至CPUINT15)、CPU异常输入(EXCEP)或高级仿真逻辑中。这128个系统事件包括了内部生成的事件(在CorePac内)和芯片级事件。
此实验将事件91通过中断控制器连接到可屏蔽中断CPUINT12达到目的。
3 参考代码
main.c
#include <stdio.h>
/* Compiler Header files */
#include <stdint.h>
/* CSL Header file */
#include <ti/csl/csl_chipAux.h>
#include <ti/csl/src/intc/csl_intc.h>/* Driver utilities include */
#include "driver/c66x_gpio.h"#define PIN_CONTROL_0 0x02620580 // GPIO控制寄存器基地址
#define LED1 GPIO_19
#define LED2 GPIO_22
#define LED3 GPIO_23
#define KEY2 GPIO_0/* INTC Objects */
CSL_IntcObj gpioIntcObj;
CSL_IntcContext context;
CSL_IntcEventHandlerRecord EventHandler[30];static void test_isr_handler (void *arg)
{printf("succeeded!\n");if (gpio_read_input(KEY2) == GPIO_LOW)//按键真的被按下了{if (gpio_read_input(LED3) == GPIO_LOW) //LED3翻转gpio_set_output(LED3);//GPIO23设置为高电平,LED3熄灭elsegpio_clear_output(LED3);//GPIO23设置为低电平,LED3点亮}
}int Interrupts_Init(void)
{CSL_IntcGlobalEnableState state;CSL_Status intStat;CSL_IntcEventHandlerRecord EventRecord;CSL_IntcParam vectId;CSL_IntcHandle gpioIntcHandle;/* INTC module initialization */context.eventhandlerRecord = EventHandler;context.numEvtEntries = 1 ;//中断个数if (CSL_intcInit(&context) != CSL_SOK)return -1;/* Enable NMIs */if (CSL_intcGlobalNmiEnable() != CSL_SOK)return -1;/* Enable global interrupts */intStat = CSL_intcGlobalEnable(&state);/* Open INTC */vectId = CSL_INTC_VECTID_12;//此处选择使用十二个可屏蔽中断gpioIntcHandle = CSL_intcOpen(&gpioIntcObj, CSL_GEM_GPINTN, &vectId, &intStat);//第二项参数为128个中断事件的某一个,此处为事件91if (gpioIntcHandle == NULL)return -1;/* Bind ISR to Interrupt */EventRecord.handler = (CSL_IntcEventHandler)&test_isr_handler;//此处关联中断处理函数EventRecord.arg = gpioIntcHandle;CSL_intcPlugEventHandler(gpioIntcHandle, &EventRecord);/* Event Enable */CSL_intcHwControl(gpioIntcHandle, CSL_INTC_CMD_EVTENABLE, NULL);gpio_enable_global_interrupt();gpio_set_fallingedge_interrupt(KEY2);printf("interrupt init succeeded!\n");return 0;
}//基于CPU周期的延迟函数,100000000=100ms
void cpu_delaycycles(uint32_t cycles) {uint32_t start_val;/* Start TCSL so its free running */CSL_chipWriteTSCL(0);start_val = CSL_chipReadTSCL();while ((CSL_chipReadTSCL() - start_val) < cycles);
}int main(void) {/* Set pin as GPIO mode */*((uint32_t *) PIN_CONTROL_0) |= ((1 << LED1)|(1 << LED2) |(1 << KEY2 ) |(1 << LED3));/* Set GPIO as output mode */gpio_set_direction(LED1, GPIO_OUT);gpio_set_direction(LED2, GPIO_OUT);gpio_set_direction(LED3, GPIO_OUT);gpio_set_direction(KEY2, GPIO_IN);Interrupts_Init();while (1) ;
}