Advertisement
Sothian

SW_6_v3

Nov 4th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.60 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 "sys/alt_irq.h"
  21. #include <unistd.h>
  22. #include "sys/alt_alarm.h"
  23. ;
  24. /* Przełączniki */
  25. #define  SW0 0x00000001
  26. #define  SW1 0x00000002
  27. #define  SW2 0x00000004
  28. #define  SW3 0x00000008
  29. #define  SW4 0x00000010
  30. #define  SW5 0x00000020
  31. #define  SW6 0x00000040
  32. #define  SW7 0x00000080
  33. #define  SW8 0x00000100
  34. #define  SW9 0x00000200
  35. #define  SW10 0x00000400
  36. #define  SW11 0x00000800
  37. #define  SW12 0x00001000
  38. #define  SW13 0x00002000
  39. #define  SW14 0x00004000
  40. #define  SW15 0x00008000
  41. #define  SW16 0x00010000
  42. #define  SW17 0x00020000
  43.  
  44. /* PushButtony */
  45. #define  KEY1 0x00000002
  46. #define  KEY2 0x00000004
  47. #define  KEY3 0x00000008
  48.  
  49. /* Ledy */
  50. #define  LED0 0x00000001
  51. #define  LED1 0x00000002
  52. #define  LED2 0x00000004
  53. #define  LED3 0x00000008
  54. #define  LED4 0x00000010
  55. #define  LED5 0x00000020
  56. #define  LED6 0x00000040
  57. #define  LED7 0x00000080
  58. #define  LED8 0x00000100
  59. #define  LED9 0x00000200
  60. #define  LED10 0x00000400
  61. #define  LED11 0x00000800
  62. #define  LED12 0x00001000
  63. #define  LED13 0x00002000
  64. #define  LED14 0x00004000
  65. #define  LED15 0x00008000
  66. #define  LED16 0x00010000
  67. #define  LED17 0x00020000
  68.  
  69. /* Segmenty HEX */
  70. #define  SEGA 1
  71. #define  SEGB 2
  72. #define  SEGC 4
  73. #define  SEGD 8
  74. #define  SEGE 16
  75. #define  SEGF 32
  76. #define  SEGG 64
  77.  
  78. /* Litery i cyfry z hex 7 segmentowych */
  79. #define HEX0 (SEGA|SEGB|SEGC|SEGD|SEGE|SEGF)
  80. #define HEX1 (SEGB|SEGC)
  81. #define HEX2 (SEGA|SEGB|SEGG|SEGE|SEGD)
  82. #define HEX3 (SEGA|SEGB|SEGC|SEGD|SEGG)
  83. #define HEX4 (SEGF|SEGG|SEGB|SEGC)
  84. #define HEX5 (SEGA|SEGF|SEGG|SEGC|SEGD)
  85. #define HEX6 (SEGA|SEGF|SEGG|SEGC|SEGD|SEGE)
  86. #define HEX7 (SEGA|SEGB|SEGC)
  87. #define HEX8 (SEGA|SEGB|SEGC|SEGD|SEGE|SEGF|SEGG)
  88. #define HEX9 (SEGA|SEGB|SEGC|SEGD|SEGF|SEGG)
  89. #define HEXE (SEGA|SEGF|SEGG|SEGE|SEGD)
  90. #define HEXr (SEGE|SEGG)
  91. #define HEXA (SEGA|SEGB|SEGC|SEGE|SEGF|SEGG)
  92. #define HEXF (SEGA|SEGE|SEGF|SEGG)
  93. #define HEXC (SEGA|SEGE|SEGF|SEGD)
  94. #define HEXD (SEGB|SEGC|SEGD|SEGE|SEGG)
  95.  
  96. static alt_alarm alarmLED;
  97. static alt_alarm alarmHEX;
  98. static alt_alarm alarmERR;
  99. int error = 0;
  100.  
  101. alt_u32 writeERR(void* context) {
  102.     if (*(int*) context == 0) {
  103.         IOWR(LEDS_RED_BASE, 0, LED17);
  104.         *(int*) context = 1;
  105.     } else if (*(int*) context == 1) {
  106.  
  107.         *(int*) context = 2;
  108.     }
  109.     if (!error) {
  110.         alt_alarm_stop(&alarmERR);
  111.         error = 1;
  112.     }
  113.     return alt_ticks_per_second(); //alt_alarm_stop (&alarmERR);
  114. }
  115.  
  116. /*
  117.  *
  118.  * alt_u32 writeERR(void* context) {
  119.  if (*(int*) context == 0) {
  120.  IOWR(LEDS_RED_BASE, 0, LED17);
  121.  *(int*) context = 1;
  122.  }
  123.  if (!error) {
  124.  alt_alarm_stop(&alarmERR);
  125.  error = 1;
  126.  }
  127.  return alt_ticks_per_second(); //alt_alarm_stop (&alarmERR);
  128.  }
  129.  
  130.  alt_u32 writeERRHex(void* context) {
  131.  if (*(int*) context == 0) {
  132.  IOWR(HEX_3_BASE, 0, ((HEXE<<16) | (HEXr<<8) | HEXr));
  133.  *(int*) context = 0;
  134.  }
  135.  if (!error) {
  136.  alt_alarm_stop(&alarmERR);
  137.  error = 1;
  138.  }
  139.  return alt_ticks_per_second(); //alt_alarm_stop (&alarmERR);
  140.  }
  141.  */
  142.  
  143. alt_u32 writeLEDs(void* context) {
  144.     int switches = IORD(SW_SLIDERS_BASE, 0);
  145.     int state = switches & (SW0 | SW1 | SW2 | SW3 | SW4 | SW5 | SW6);
  146.     printf("%d,\n", state);
  147.     switch (state) {
  148.     case 0:
  149.         IOWR(LEDS_RED_BASE, 0, 0);
  150.         break;
  151.     case 1:
  152.         IOWR(LEDS_RED_BASE, 0, LED0);
  153.         if ((switches & SW7) && (switches & SW8)) {
  154.             IOWR(LEDS_RED_BASE, 0, LED17);
  155.         } else if (switches & SW7) {
  156.             IOWR(LEDS_RED_BASE, 0, LED7);
  157.         } else if (switches & SW8) {
  158.             IOWR(LEDS_RED_BASE, 0, LED8);
  159.         }
  160.         break;
  161.     case 2:
  162.         IOWR(LEDS_RED_BASE, 0, LED1);
  163.         break;
  164.     case 4:
  165.         IOWR(LEDS_RED_BASE, 0, LED2);
  166.         break;
  167.     case 8:
  168.         IOWR(LEDS_RED_BASE, 0, LED3);
  169.  
  170.         if (((switches & SW9) && (switches & SW10))
  171.                 || ((switches & SW9) && (switches & SW11))
  172.                 || ((switches & SW9) && (switches & SW12))
  173.                 || ((switches & SW10) && (switches & SW11))
  174.                 || ((switches & SW10) && (switches & SW12))
  175.                 || ((switches & SW11) && (switches & SW12))) {
  176.             IOWR(LEDS_RED_BASE, 0, LED17);
  177.         } else if (switches & SW9) {
  178.             IOWR(LEDS_RED_BASE, 0, LED9);
  179.         } else if (switches & SW10) {
  180.             IOWR(LEDS_RED_BASE, 0, LED10);
  181.         } else if (switches & SW11) {
  182.             IOWR(LEDS_RED_BASE, 0, LED11);
  183.         } else if (switches & SW12) {
  184.             IOWR(LEDS_RED_BASE, 0, LED12);
  185.         }
  186.  
  187.         break;
  188.     case 16:
  189.         IOWR(LEDS_RED_BASE, 0, LED4);
  190.         break;
  191.     case 32:
  192.         IOWR(LEDS_RED_BASE, 0, LED5);
  193.         break;
  194.     case 64:
  195.         IOWR(LEDS_RED_BASE, 0, LED6);
  196.         break;
  197.     }
  198.     alt_alarm_stop(&alarmLED);
  199.     return 0;
  200. }
  201.  
  202. alt_u32 writeHEXes(void* context) {
  203.     int switches = IORD(SW_SLIDERS_BASE, 0);
  204.     int state = switches & (SW0 | SW1 | SW2 | SW3 | SW4 | SW5 | SW6);
  205.     switch (state) {
  206.     case 0:
  207.         IOWR(HEX_3_BASE, 0, 0);
  208.         break;
  209.     case 1:
  210.         IOWR(HEX_3_BASE, 0, HEX1);
  211.         if ((switches & SW7) && (switches & SW8)) {
  212.             IOWR(HEX_3_BASE, 0, ((HEXE<<24) | (HEXr<<16) | (HEXr<<8)));
  213.         } else if (switches & SW7) {
  214.             IOWR(HEX_3_BASE, 0, (HEXA<<8));
  215.         } else if (switches & SW8) {
  216.             IOWR(HEX_3_BASE, 0, (HEXF<<8));
  217.         }
  218.  
  219.         break;
  220.     case 2:
  221.         IOWR(HEX_3_BASE, 0, HEX2);
  222.         break;
  223.     case 4:
  224.         IOWR(HEX_3_BASE, 0, HEX3);
  225.         break;
  226.     case 8:
  227.         IOWR(HEX_3_BASE, 0, HEX4);
  228.  
  229.         if (((switches & SW9) && (switches & SW10))
  230.                 || ((switches & SW9) && (switches & SW11))
  231.                 || ((switches & SW9) && (switches & SW12))
  232.                 || ((switches & SW10) && (switches & SW11))
  233.                 || ((switches & SW10) && (switches & SW12))
  234.                 || ((switches & SW11) && (switches & SW12))) {
  235.             IOWR(HEX_3_BASE, 0, ((HEXE<<24) | (HEXr<<16) | (HEXr<<8)));
  236.         } else if (switches & SW9) {
  237.             IOWR(HEX_3_BASE, 0, ((HEXC<<16)| (HEX1 <<8)));
  238.         } else if (switches & SW10) {
  239.             IOWR(HEX_3_BASE, 0, ((HEXC<<16)| (HEX2 <<8)));
  240.         } else if (switches & SW11) {
  241.             IOWR(HEX_3_BASE, 0, ((HEXD<<16)| (HEX1 <<8)));
  242.         } else if (switches & SW12) {
  243.             IOWR(HEX_3_BASE, 0, ((HEXD<<16)| (HEX2 <<8)));
  244.         }
  245.  
  246.         break;
  247.     case 16:
  248.         IOWR(HEX_3_BASE, 0, HEX5);
  249.         break;
  250.     case 32:
  251.         IOWR(HEX_3_BASE, 0, HEX6);
  252.         break;
  253.     case 64:
  254.         IOWR(HEX_3_BASE, 0, HEX7);
  255.         break;
  256.     }
  257.     alt_alarm_stop(&alarmHEX);
  258.     return 0;
  259. }
  260.  
  261. int main() {
  262.     printf("Hello from Nios II!\n");
  263.     int state = 0;
  264.     int lastState = 0;
  265.     int timeToEventLeds = 1;
  266.     int timeToEventHex = 0;
  267.  
  268.     int which = 0;
  269.  
  270.     do {
  271.         int switches = IORD(SW_SLIDERS_BASE, 0);
  272.         state = switches & (SW0 | SW1 | SW2 | SW3 | SW4 | SW5 | SW6);
  273.         if (lastState != state) {
  274.             switch (state) {
  275.             case 0:
  276.                 if (error) {
  277.                     timeToEventLeds = 3;
  278.                     timeToEventHex = 0;
  279.                 } else {
  280.                     timeToEventLeds = 2;
  281.                     timeToEventHex = 0;
  282.                 }
  283.                 error = 0;
  284.                 break;
  285.             case 1:
  286.                 if (error) {
  287.                     timeToEventLeds = 3;
  288.                     timeToEventHex = 0;
  289.                 } else {
  290.                     timeToEventLeds = 1;
  291.                     timeToEventHex = 0;
  292.                 }
  293.                 error = 0;
  294.                 break;
  295.             case 2:
  296.                 if (error) {
  297.                     timeToEventLeds = 3;
  298.                     timeToEventHex = 0;
  299.                 } else {
  300.                     timeToEventLeds = 1;
  301.                     timeToEventHex = 0;
  302.                 }
  303.                 error = 0;
  304.                 break;
  305.             case 4:
  306.                 if (error) {
  307.                     timeToEventLeds = 3;
  308.                     timeToEventHex = 0;
  309.                 } else {
  310.                     timeToEventLeds = 1;
  311.                     timeToEventHex = 0;
  312.                 }
  313.                 error = 0;
  314.                 break;
  315.             case 8:
  316.                 if (error) {
  317.                     timeToEventLeds = 3;
  318.                     timeToEventHex = 0;
  319.                 } else {
  320.                     timeToEventLeds = 1;
  321.                     timeToEventHex = 0;riteERR(void* context) {
  322.                         if (*(int*) context == 0) {
  323.                             IOWR(LEDS_RED_BASE, 0, LED17);
  324.                             *(int*) context = 1;
  325.                         } else if (*(int*) context == 1) {
  326.  
  327.                             *(int*) context = 2;
  328.                         }
  329.                         if (!error) {
  330.                             alt_alarm_stop(&alarmERR);
  331.                             error = 1;
  332.                         }
  333.                         return alt_ticks_per_second(); //alt_alarm_stop (&alarmERR);
  334.                     }
  335.  
  336.                 }
  337.                 error = 0;
  338.                 break;
  339.             case 16:
  340.                 if (error) {
  341.                     timeToEventLeds = 3;
  342.                     timeToEventHex = 0;
  343.                 } else {
  344.                     timeToEventLeds = 1;
  345.                     timeToEventHex = 0;
  346.                 }
  347.                 error = 0;
  348.                 break;
  349.             case 32:
  350.                 if (error) {
  351.                     timeToEventLeds = 3;
  352.                     timeToEventHex = 0;
  353.                 } else {
  354.                     timeToEventLeds = 1;
  355.                     timeToEventHex = 0;
  356.                 }
  357.                 error = 0;
  358.                 break;
  359.             case 64:
  360.                 if (error) {
  361.                     timeToEventLeds = 3;
  362.                     timeToEventHex = 0;
  363.                 } else {
  364.                     timeToEventLeds = 1;
  365.                     timeToEventHex = 0;
  366.                 }
  367.                 error = 0;
  368.                 break;
  369.             default:
  370.                 timeToEventLeds = 1;
  371.                 timeToEventHex = 0;
  372.                 error = 1;
  373.  
  374.                 break;
  375.             }
  376.  
  377.             if (error == 1) {
  378.                 //alt_alarm_start(&alarmERR,timeToEventHex * alt_ticks_per_second(), writeERR, NULL);//(void*)&which) ;
  379.                 printf("Hello from Nios II!\n");
  380.                 IOWR(HEX_3_BASE, 0, ((HEXE<<16) | (HEXr<<8) | HEXr));
  381.                 alt_alarm_start(&alarmERR,
  382.                         timeToEventLeds * alt_ticks_per_second(), writeERR,
  383.                         &which);
  384.                 if (which == 2) {
  385.                     alt_alarm_stop(&alarmERR);
  386.                     which = 0;
  387.                 }
  388.  
  389.                 //which = 1;
  390.                 //alt_alarm_start(&alarmERR,(timeToEventHex-timeToEventLeds) * alt_ticks_per_second(), writeERR, &which) ;
  391.             } else {
  392.                 alt_alarm_start(&alarmLED,
  393.                         timeToEventLeds * alt_ticks_per_second(), writeLEDs,
  394.                         NULL);
  395.                 alt_alarm_start(&alarmHEX,
  396.                         timeToEventHex * alt_ticks_per_second(), writeHEXes,
  397.                         NULL);
  398.             }
  399.         }
  400.         lastState = state;
  401.     } while (1);
  402.     return 0;
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement