Advertisement
Sothian

Untitled

Oct 29th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.65 KB | None | 0 0
  1. /*
  2.  * "Hello World" example.
  3.  *
  4.  * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
  5.  * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
  6.  * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
  7.  * device in your system's hardware.
  8.  * The memory footprint of this hosted application is ~69 kbytes by default
  9.  * using the standard reference design.
  10.  *
  11.  * For a reduced footprint version of this template, and an explanation of how
  12.  * to reduce the memory footprint for a given application, see the
  13.  * "small_hello_world" template.
  14.  *
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <io.h>
  19. #include <system.h>
  20. #include "alt_types.h"
  21. #include "altera_avalon_pio_regs.h"
  22. #include "sys/alt_irq.h"
  23. #include <unistd.h>
  24.  
  25. //        sw_sliders
  26. #define  SW0 0x00000001
  27. #define  SW1 0x00000002
  28. #define  SW2 0x00000004
  29. #define  SW3 0x00000008
  30. #define  SW4 0x00000010
  31. #define  SW5 0x00000020
  32. #define  SW6 0x00000040
  33. #define  SW7 0x00000080
  34. #define  SW8 0x00000100
  35. #define  SW9 0x00000200
  36. #define  SW10 0x00000400
  37. #define  SW11 0x00000800
  38. #define  SW12 0x00001000
  39. #define  SW13 0x00002000
  40. #define  SW14 0x00004000
  41. #define  SW15 0x00008000
  42. #define  SW16 0x00010000
  43. #define  SW17 0x00020000
  44.  
  45. //      pushbuttons
  46. #define  KEY1 0x00000002
  47. #define  KEY2 0x00000004
  48. #define  KEY3 0x00000008
  49. #define  KEY4 0x00000010
  50.  
  51. //      leds
  52. #define  LED0 0x00000001
  53. #define  LED1 0x00000002
  54. #define  LED2 0x00000004
  55. #define  LED3 0x00000008
  56. #define  LED4 0x00000010
  57. #define  LED5 0x00000020
  58. #define  LED6 0x00000040
  59. #define  LED7 0x00000080
  60. #define  LED8 0x00000100
  61. #define  LED9 0x00000200
  62. #define  LED10 0x00000400
  63. #define  LED11 0x00000800
  64. #define  LED12 0x00001000
  65. #define  LED13 0x00002000
  66. #define  LED14 0x00004000
  67. #define  LED15 0x00008000
  68. #define  LED16 0x00010000
  69. #define  LED17 0x00020000
  70.  
  71. //      hex
  72. #define  SEGA 0x00001
  73. #define  SEGB 0x00002
  74. #define  SEGC 0x00004
  75. #define  SEGD 0x00008
  76. #define  SEGE 0x00010
  77. #define  SEGF 0x00020
  78. #define  SEGG 0x00040
  79.  
  80. //     hex - numbers
  81. #define ZERO SEGA | SEGB | SEGC | SEGD |SEGE | SEGF
  82. #define ONE  SEGB | SEGC
  83. #define TWO  SEGA | SEGB | SEGG | SEGE | SEGD
  84. #define THREE SEGA | SEGB | SEGC | SEGD | SEGG
  85. #define FOUR SEGF | SEGG | SEGC | SEGB
  86. #define FIVE SEGA | SEGC | SEGD | SEGG | SEGF
  87. #define SIX SEGA | SEGC | SEGD | SEGE | SEGF | SEGG
  88. #define SEVEN SEGA | SEGB | SEGC
  89. #define EIGHT SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG
  90. #define E SEGA | SEGD | SEGE | SEGF | SEGG
  91. #define R SEGE | SEGG
  92.  
  93.  
  94. struct interrupt_data  
  95. {
  96.     int volatile leds_addr;
  97.     int volatile sw_addr;
  98.     int volatile pb_addr;
  99.  
  100. };
  101.  
  102.  
  103.  static void handle_sliders_interrupt(struct interrupt_data *data)
  104.  {
  105.  
  106.     int state=0;
  107.  
  108.    
  109.     if(IORD(data->sw_addr,0)&SW0){
  110.         state+=1;
  111.     }
  112.     if(IORD(data->sw_addr,0)&SW1){
  113.         state+=2;
  114.     }
  115.     if(IORD(data->sw_addr,0)&SW2){
  116.         state+=4;
  117.     }
  118.     if(IORD(data->sw_addr,0)&SW3){
  119.         state+=8;
  120.     }
  121.     if(IORD(data->sw_addr,0)&SW4){
  122.         state+=16;
  123.     }
  124.     if(IORD(data->sw_addr,0)&SW5){
  125.         state+=32;
  126.     }
  127.     if(IORD(data->sw_addr,0)&SW6){
  128.         state+=64;
  129.     }
  130.        
  131.     if(state==0 || state==1 || state==2 || state==4 || state==8 || state==16 || state==32 || state==64){
  132.         switch(state) {  
  133.             case 0:
  134.                 IOWR(data->leds_addr,0,0);
  135.                 break;
  136.             case 1:
  137.                 IOWR(data->leds_addr,0, LED0);
  138.                 break;
  139.             case 2:
  140.                 IOWR(data->leds_addr,0, LED1);
  141.                 break;
  142.             case 4:
  143.                 IOWR(data->leds_addr,0, LED2);
  144.                 break;
  145.             case 8:
  146.                 IOWR(data->leds_addr,0, LED3);
  147.                 break;
  148.             case 16:
  149.                 IOWR(data->leds_addr,0, LED4);
  150.                 break;
  151.             case 32:
  152.                 IOWR(data->leds_addr,0, LED5);
  153.                 break;
  154.             default:
  155.                 IOWR(data->leds_addr, 0, 0);
  156.                 break;
  157.         }
  158.     }
  159.     else if(state==0){
  160.         IOWR(data->leds_addr,0,0);
  161.     }
  162.     else{
  163.         IOWR(data->leds_addr,0,LED9);
  164.     }
  165. }
  166.  
  167. int main()
  168. {
  169.     while(1){
  170.         struct interrupt_data data;    
  171.         int *leds = (int*) (0x41040);
  172.         int *sw = (int*) (0x41030);
  173.         int *pb = (int*) (0x41020);
  174.         data.leds_addr = leds;
  175.         data.pb_addr = pb;
  176.         data.sw_addr = sw;
  177.        
  178.         IOWR_ALTERA_AVALON_PIO_IRQ_MASK(SW_SLIDERS_BASE, 0xFF);
  179.         alt_ic_isr_register(SW_SLIDERS_IRQ_INTERRUPT_CONTROLLER_ID,SW_SLIDERS_IRQ, handle_sliders_interrupt, &data, 0x0);
  180.     }
  181.   return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement