Advertisement
Guest User

Untitled

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