Code design compatible with nios2 IRQ

Source: Internet
Author: User

At the beginning of nios2, IRQ was enhanced, but in fact the Enhanced type was only a shell, and the internal IRQ was still the original, and it was dizzy as a touch...

We do not provide any subjective opinions here. Maybe we want to upgrade Altera and encapsulate it in the future.

To meet the requirements of Altera, we can design compatible common and enhanced IRQCode

 

Whether it is an enhanced type is defined in system. h.

Nios2 91 will automatically help you design an enhanced type. If you need to use the original interrupt, comment out "# define alt_enhanced_interrupt_api_present" first, as shown above

 

The Code is designed as follows:

(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);// Press the button to interrupt the serviceProgram

// Void key_interrupt (void * key_isr_context); // key interruption Initialization

# 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://www.cnblogs.com/crazybingo/archive/2011/04/03/2004477.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;

// * Key interrupt initialization */
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, // Number of the interrupt controller, copied from system. h
Key_data_irq, // hardware interrupt number, copied from system. h
Key_interrupt, // interrupt service subfunction
Key_edge_ptr, // point to the data structure related to the device driver instance
0 // flags, reserved for unused
);
# Else // before nios2 91 Edition
Alt_irq_register
(
Key_data_irq, // hardware interrupt number, copied from system. h
Key_edge_ptr, // point to the data structure related to the device driver instance
Key_interrupt // interrupt service subfunction
);
# 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 <stdio. h>
# 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 (); // key interrupt Initialization

While (1)
{
Iowr_altera_avalon_pio_data (led_data_base, key_edge );
}
Return 0;
}

 

Done

 

In fact, nios2 91 can be used for both enhanced and common interruptions, but you can either use enhanced or modify the system by yourself. h to use the common model, anyway, I am casual, compatible for a long time no mater... Go on!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.