Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.20 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. #include < stdio.h >
  17. #include < io.h >
  18. #include < system.h >
  19. #include "alt_types.h"
  20. #include "altera_avalon_pio_regs.h"
  21. #include "sys/alt_irq.h"
  22. #include < unistd.h >
  23.  
  24. #define SW0 0x00000001
  25. #define SW1 0x00000002
  26. #define SW2 0x00000004
  27. #define SW3 0x00000008
  28. #define SW4 0x00000010
  29. #define SW5 0x00000020
  30. #define SW6 0x00000040
  31. #define SW7 0x00000080
  32. #define SW8 0x00000100
  33. #define SW9 0x00000200
  34.  
  35. //      pushbuttons
  36. #define KEY1 0x0000000E
  37. #define KEY2 0x00000004
  38. #define KEY3 0x00000008
  39. #define KEY4 0x00000010
  40.  
  41. //      leds
  42. #define LED0 0x00000001
  43. #define LED1 0x00000002
  44. #define LED2 0x00000004
  45. #define LED3 0x00000008
  46. #define LED4 0x00000010
  47. #define LED5 0x00000020
  48. #define LED6 0x00000040
  49. #define LED7 0x00000080
  50. #define LED8 0x00000100
  51. #define LED9 0x00000200
  52.  
  53. //      hex
  54. #define SEGA 0x00001
  55. #define SEGB 0x00002
  56. #define SEGC 0x00004
  57. #define SEGD 0x00008
  58. #define SEGE 0x00010
  59. #define SEGF 0x00020
  60. #define SEGG 0x00040
  61.  
  62. //     hex - numbers
  63. #define ZERO SEGA | SEGB | SEGC | SEGD | SEGE | SEGF
  64. #define ONE SEGB | SEGC
  65. #define TWO SEGA | SEGB | SEGG | SEGE | SEGD
  66. #define THREE SEGA | SEGB | SEGC | SEGD | SEGG
  67. #define FOUR SEGA | SEGC | SEGG | SEGF
  68. #define FIVE SEGA | SEGC | SEGD | SEGF | SEGG
  69. #define SIX SEGA | SEGC | SEGD | SEGE | SEGF | SEGG
  70. #define SEVEN SEGA | SEGB | SEGC
  71. #define EIGHT SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG
  72. #define NINE SEGA | SEGB | SEGC | SEGD | SEGF
  73. #define E_HEX SEGA | SEGD | SEGE | SEGF | SEGG
  74. #define R_HEX SEGE | SEGG
  75.  
  76. int get_hex(int value) {
  77.   switch (value) {
  78.   case 0:
  79.     return ZERO;
  80.     break;
  81.   case 1:
  82.     return ONE;
  83.     break;
  84.   case 2:
  85.     return TWO;
  86.     break;
  87.   case 3:
  88.     return THREE;
  89.     break;
  90.   case 4:
  91.     return FOUR;
  92.     break;
  93.   case 5:
  94.     return FIVE;
  95.     break;
  96.   case 6:
  97.     return SIX;
  98.     break;
  99.   case 7:
  100.     return SEVEN;
  101.     break;
  102.   case 8:
  103.     return EIGHT;
  104.     break;
  105.   case 9:
  106.     return NINE;
  107.     break;
  108.   default:
  109.     return SEGA;
  110.   }
  111. }
  112.  
  113. struct interrupt_data {
  114.   volatile int * leds_addr;
  115.   volatile int * sliders_addr;
  116.   volatile int * hex_addr;
  117.   volatile int * push_button_addr;
  118. };
  119. volatile int hexs[6] = {0,0,0,0,0,0};
  120. volatile int rooms[5];
  121. volatile int flag0;
  122. volatile int flagDirection;
  123. volatile int errFlag = 0;
  124.  
  125. static void handle_pushbuttons_interrupt(struct interrupt_data * data) {
  126.   volatile int leds = IORD(data - > leds_addr, 0);
  127.   volatile int kierunek = IORD(data - > push_button_addr, 0);
  128.  
  129.   if (kierunek == KEY1) {
  130.     if (flagDirection == 0) {
  131.       leds |= LED9;
  132.       flagDirection = 1;
  133.     } else {
  134.       leds &= ~(LED9);
  135.       flagDirection = 0;
  136.     }
  137.   }
  138.   IOWR(data - > leds_addr, 0, leds);
  139. }
  140.  
  141. static void handle_sliders_interrupt(struct interrupt_data * data) {
  142.   volatile int sw_state = IORD(data - > sliders_addr, 0);
  143.   volatile int leds = IORD(data - > leds_addr, 0);
  144.  
  145.   switch (sw_state) {
  146.   case SW0:
  147.     if (flag0 == 0) {
  148.       if (flagDirection == 0) {
  149.         rooms[0]++;
  150.         errFlag = 0;
  151.       } else {
  152.         if (rooms[0] == 0)
  153.           errFlag = 1;
  154.         else {
  155.           rooms[0]--;
  156.           errFlag = 0;
  157.         }
  158.       }
  159.       flag0 = 1;
  160.       SW0on = alltickson
  161.     }
  162.     break;
  163.   case SW1:
  164.     if (flag0 == 0) {
  165.       if (flagDirection == 1) {
  166.         rooms[2]++;
  167.         errFlag = 0;
  168.       } else {
  169.         if (rooms[2] == 0)
  170.           errFlag = 1;
  171.         else {
  172.           rooms[2]--;
  173.           errFlag = 0;
  174.         }
  175.       }
  176.       flag0 = 1;
  177.     }
  178.     break;
  179.   case SW2:
  180.     if (flag0 == 0) {
  181.       if (flagDirection == 0) {
  182.         if (rooms[0] == 0)
  183.           errFlag = 1;
  184.            else {
  185.           errFlag = 0;
  186.           rooms[0]--;
  187.           rooms[2]++;
  188.         }
  189.       } else {
  190.         if (rooms[2] == 0)
  191.           errFlag = 1;
  192.             else{
  193.           rooms[0]++;
  194.           rooms[2]--;
  195.           errFlag = 0;
  196.         }
  197.       }
  198.       flag0 = 1;
  199.     }
  200.  
  201.     break;
  202.   case SW3:
  203.     if (flag0 == 0) {
  204.       if (flagDirection == 0) {
  205.         if (rooms[1] == 0)
  206.           errFlag = 1;
  207.           else {
  208.           errFlag = 0;
  209.           rooms[1]--;
  210.           rooms[3]++;
  211.         }
  212.       } else {
  213.         if (rooms[3] == 0)
  214.           errFlag = 1;
  215.         else {
  216.           rooms[3]--;
  217.           rooms[1]++;
  218.           errFlag = 0;
  219.         }
  220.       }
  221.       flag0 = 1;
  222.     }
  223.  
  224.     break;
  225.   case SW4:
  226.     if (flag0 == 0) {
  227.       if (flagDirection == 1) {
  228.         rooms[3]++;
  229.         errFlag = 0;
  230.       } else {
  231.         if (rooms[3] == 0)
  232.           errFlag = 1;
  233.         else
  234.         {
  235.           rooms[3]--;
  236.           errFlag = 0;
  237.         }
  238.       }
  239.       flag0 = 1;
  240.     }
  241.     break;
  242.   case SW5:
  243.     if (flag0 == 0) {
  244.       if (flagDirection == 0) {
  245.         rooms[1]++;
  246.         errFlag = 0;
  247.       } else {
  248.         if (rooms[1] == 0)
  249.           errFlag = 1;
  250.         else
  251.         {
  252.          rooms[1]--;
  253.          errFlag = 0;
  254.         }
  255.       }
  256.       flag0 = 1;
  257.     }
  258.  
  259.     break;
  260.   case SW6:
  261.     if (flag0 == 0) {
  262.       if (flagDirection == 1) {
  263.         rooms[4]++;
  264.         errFlag = 0;
  265.       } else {
  266.         if (rooms[4] == 0)
  267.           errFlag = 1;
  268.         else{
  269.          rooms[4]--;
  270.         errFlag = 0;
  271.         }
  272.       }
  273.       flag0 = 1;
  274.     }
  275.     break;
  276.   case SW7:
  277.     if (flag0 == 0) {
  278.       if (flagDirection == 0) {
  279.         if (rooms[1] == 0)
  280.           errFlag = 1;
  281.             else {
  282.           rooms[1]--;
  283.           rooms[4]++;
  284.           errFlag = 0;
  285.         }
  286.       } else {
  287.         if (rooms[4] == 0)
  288.           errFlag = 1;
  289.         else {
  290.           errFlag = 0;
  291.           rooms[4]--;
  292.           rooms[1]++;
  293.         }
  294.       }
  295.       flag0 = 1;
  296.     }
  297.  
  298.     break;
  299.   case SW8:
  300.     if (flag0 == 0) {
  301.       if (flagDirection == 0) {
  302.         if (rooms[0] == 0)
  303.           errFlag = 1;
  304.             else{
  305.           rooms[0]--;
  306.           rooms[1]++;
  307.           errFlag = 0;
  308.         }
  309.       } else {
  310.         if (rooms[1] == 0)
  311.           errFlag = 1;
  312.         else{
  313.           rooms[1]--;
  314.           rooms[0]++;
  315.           errFlag = 0;
  316.         }
  317.       }
  318.       flag0 = 1;
  319.     }
  320.  
  321.     break;
  322.   case SW9:
  323.     if (flag0 == 0) {
  324.       if (flagDirection == 0) {
  325.         if (rooms[2] == 0)
  326.           errFlag = 1;
  327.         else{
  328.           rooms[2]--;
  329.           rooms[3]++;
  330.           errFlag = 0;
  331.         }
  332.       } else {
  333.         if (rooms[3] == 0)
  334.           errFlag = 1;
  335.         else {
  336.           rooms[3]--;
  337.           rooms[2]++;
  338.           errFlag = 0;
  339.         }
  340.       }
  341.       flag0 = 1;
  342.     }
  343.     break;
  344.   default:
  345.     flag0 = 0;
  346.   }
  347.  
  348.   if (rooms[0] > 0) {
  349.     leds |= LED0;
  350.   } else {
  351.     leds &= ~(LED0);
  352.   }
  353.   if (rooms[1] > 0) {
  354.     leds |= LED1;
  355.   } else {
  356.     leds &= ~(LED1);
  357.   }
  358.   if (rooms[2] > 0) {
  359.     leds |= LED2;
  360.   } else {
  361.     leds &= ~(LED2);
  362.   }
  363.   if (rooms[3] > 0) {
  364.     leds |= LED3;
  365.   } else {
  366.     leds &= ~(LED3);
  367.   }
  368.   if (rooms[4] > 0) {
  369.     leds |= LED4;
  370.   } else {
  371.     leds &= ~(LED4);
  372.   }
  373.  
  374.   if (rooms[0] >= 0) {
  375.     hexs[0] = get_hex(rooms[0]);
  376.   }
  377.   if (rooms[1] >= 0) {
  378.     hexs[1] = get_hex(rooms[1]);
  379.   }
  380.   if (rooms[2] >= 0) {
  381.     hexs[2] = get_hex(rooms[2]);
  382.   }
  383.   if (rooms[3] >= 0) {
  384.     hexs[3] = get_hex(rooms[3]);
  385.   }
  386.   if (rooms[4] >= 0) {
  387.     hexs[4] = get_hex(rooms[4]);
  388.   }
  389.  
  390.   if (errFlag == 1) {
  391.     IOWR(data - > hex_addr, 3, R_HEX);
  392.     IOWR(data - > hex_addr, 4, R_HEX);
  393.     IOWR(data - > hex_addr, 5, E_HEX);
  394.     //    leds |= LED8;
  395.   } else {
  396.     IOWR(data - > hex_addr, 0, (hexs[0]));
  397.     IOWR(data - > hex_addr, 1, (hexs[1]));
  398.     IOWR(data - > hex_addr, 2, (hexs[2]));
  399.     IOWR(data - > hex_addr, 3, (hexs[3]));
  400.     IOWR(data - > hex_addr, 4, (hexs[4]));
  401.     IOWR(data - > hex_addr, 5, (hexs[5]));
  402.     // leds &= ~(LED8);
  403.   }
  404.  
  405.   IOWR(data - > leds_addr, 0, leds);
  406.  
  407. }
  408.  
  409. int main() {
  410.   volatile int * leds = (int * ) LEDS_BASE;
  411.   volatile int * sliders = (int * ) SW_SLIDERS_BASE;
  412.   volatile int * hex = (int * ) HEX_BASE;
  413.   volatile int * push = (int * ) PUSHBUTTON_BASE;
  414.  
  415.   IOWR(LEDS_BASE, 0, 0x00);
  416.   // IOWR(HEX_BASE, 0, 0x3f);
  417.  
  418.   struct interrupt_data data;
  419.  
  420.   data.leds_addr = leds;
  421.   data.sliders_addr = sliders;
  422.   data.hex_addr = hex;
  423.   data.push_button_addr = push;
  424.  
  425.   IOWR_ALTERA_AVALON_PIO_IRQ_MASK(PUSHBUTTON_BASE, 0b1111);
  426.   IOWR_ALTERA_AVALON_PIO_IRQ_MASK(SW_SLIDERS_BASE, 0b1111111111);
  427.  
  428.   alt_ic_isr_register(SW_SLIDERS_IRQ_INTERRUPT_CONTROLLER_ID, SW_SLIDERS_IRQ, handle_sliders_interrupt, & data, 0x0);
  429.   alt_ic_isr_register(PUSHBUTTON_IRQ_INTERRUPT_CONTROLLER_ID, PUSHBUTTON_IRQ, handle_pushbuttons_interrupt, & data, 0x0);
  430.  
  431.   alt_ic_irq_enable(SW_SLIDERS_IRQ_INTERRUPT_CONTROLLER_ID, SW_SLIDERS_IRQ);
  432.   alt_ic_irq_enable(SW_SLIDERS_IRQ_INTERRUPT_CONTROLLER_ID, PUSHBUTTON_IRQ);
  433.  
  434.   while (1) {}
  435.  
  436.   return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement