CrazyBingo

关于一种兼容nios2 irq的代码设计

0
阅读(5176)

nios2 开始,irq有了增强型,但其实增强型只是个外壳,内部还是原始的irq,一摸一样晕得很。。。

此处不对此做任何主观见解,也许altera要升级,先封装在发挥呢。

此处为了达到altera的要求,我们可以为所欲为的设计兼容普通和增强型irq的代码

关于是否是增强型,定义在system.h里面


nios2 91会自动帮你设计为增强型,如果你非要用原始的中断,先把“#define ALT_ENHANCED_INTERRUPT_API_PRESENT”注释掉,如上

代码设计如下

(1)key_scan.h

(2)key_scan.c

(3)sys_main.c

//---------------------------------------------------------------

/*

* key_scan.h

*

* Created on: 2011-4-1

* Author: CrazyBingo

*/

#ifndef KEY_SCAN_H_

#define KEY_SCAN_H_

void key_interrupt_init(void);//按键中断服务程序

//void key_interrupt(void * key_isr_context); //按键中断初始化

#ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT //nios2 91 edition or later

void key_interrupt(void * key_isr_context);

#else //before nios2 91 edition

void key_interrupt(void * key_isr_context, alt_u32 id);

#endif

#endif /* KEY_SCAN_H_ */

//----------------------------------------------------------------------

/*
* key_scan.c
*
* Created on: 2011-4-1
* Author: CrazyBingo
*/
//http://blog.chinaaet.com/detail/18477.html
#include "system.h"
#include "sys/alt_irq.h"
#include "altera_avalon_pio_regs.h"

#include "../inc/key_scan.h"
#include "../inc/my_sopc.h"

volatile int key_edge;

//* 按键中断初始化 */
void key_interrupt_init(void)
{
/**//* Recast the edge_capture pointer to match the alt_irq_register() function
* prototype. */
void* key_edge_ptr = (void*) &key_edge;
/**//* Enable all 2 button interrupts. */
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY_DATA_BASE, 0x03);
/**//* Reset the edge capture register.Enable bit_clearing turn0 off, write any
* vaule will take effect ,but active High always take effect*/
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_DATA_BASE, 0x03);
/**//* Register the interrupt handler. */
#ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT //nios2 91 edition or later
alt_ic_isr_register
(
KEY_DATA_IRQ_INTERRUPT_CONTROLLER_ID, // 中断控制器标号,从system.h复制
KEY_DATA_IRQ, // 硬件中断号,从system.h复制
key_interrupt, // 中断服务子函数
key_edge_ptr, // 指向与设备驱动实例相关的数据结构体
0 // flags,保留未用
);
#else //before nios2 91 edition
alt_irq_register
(
KEY_DATA_IRQ, // 硬件中断号,从system.h复制
key_edge_ptr, // 指向与设备驱动实例相关的数据结构体
key_interrupt // 中断服务子函数
);
#endif
}

//按键中断服务程序(Interrupt service routime), fall_edge enable
#ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT //nios2 91 edition or later
void key_interrupt(void * key_isr_context)
#else //before nios2 91 edition
void key_interrupt(void * key_isr_context, alt_u32 id)
#endif

{
/**//* Cast context to edge_capture's type. It is important that this be
* declared volatile to avoid unwanted compiler optimization.*/
volatile int* key_edge_ptr = (volatile int*) key_isr_context;
/**//* Store the value in the Button's edge capture register in *context. */
*key_edge_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEY_DATA_BASE);
/**//* Reset the edge capture register. */
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_DATA_BASE,0x03);
}

//------------------------------------------------------------------------

/*
* sys_main.c
*
* Created on: 2011-4-1
* Author: CrazyBingo
*/

#include
#include "system.h"
#include "altera_avalon_pio_regs.h"


#include "../inc/key_scan.h"

extern volatile int key_edge;

int main(void)
{
key_interrupt_init(); //按键中断初始化

while(1)
{
IOWR_ALTERA_AVALON_PIO_DATA(LED_DATA_BASE, key_edge);
}
return 0;
}

搞定

其实nios2 91 不管是增强型还是普通型的中断都是可以应用的,只是你要么顺着他来用增强型,要么自己修改system.h来用普通型,反正我都随便了,兼容了久no mater了。。。Go on!

Baidu
map