Advertisement
Sothian

SW_6_v2

Nov 4th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.94 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.     switch (state) {
  147.     case 0:
  148.         IOWR(LEDS_RED_BASE, 0, 0);
  149.         break;
  150.     case 1:
  151.         IOWR(LEDS_RED_BASE, 0, LED0);
  152.         if ((switches & SW7) && (switches & SW8)) {
  153.             IOWR(LEDS_RED_BASE, 0, LED17);
  154.         } else if (switches & SW7) {
  155.             IOWR(LEDS_RED_BASE, 0, LED7);
  156.         } else if (switches & SW8) {
  157.             IOWR(LEDS_RED_BASE, 0, LED8);
  158.         }
  159.         break;
  160.     case 2:
  161.         IOWR(LEDS_RED_BASE, 0, LED1);
  162.         break;
  163.     case 4:
  164.         IOWR(LEDS_RED_BASE, 0, LED2);
  165.         break;
  166.     case 8:
  167.         IOWR(LEDS_RED_BASE, 0, LED3);
  168.         break;
  169.     case 16:
  170.         IOWR(LEDS_RED_BASE, 0, LED4);
  171.         break;
  172.     case 32:
  173.         IOWR(LEDS_RED_BASE, 0, LED5);
  174.         break;
  175.     case 64:
  176.         IOWR(LEDS_RED_BASE, 0, LED6);
  177.         break;
  178.     }
  179.     alt_alarm_stop(&alarmLED);
  180.     return 0;
  181. }
  182.  
  183. alt_u32 writeHEXes(void* context) {
  184.     int switches = IORD(SW_SLIDERS_BASE, 0);
  185.     int state = switches & (SW0 | SW1 | SW2 | SW3 | SW4 | SW5 | SW6);
  186.     switch (state) {
  187.     case 0:
  188.         IOWR(HEX_3_BASE, 0, 0);
  189.         break;
  190.     case 1:
  191.         IOWR(HEX_3_BASE, 0, HEX1);
  192.         if ((switches & SW7) && (switches & SW8)) {
  193.             IOWR(HEX_3_BASE, 0, ((HEXE<<24) | (HEXr<<16) | (HEXr<<8)));
  194.         } else if (switches & SW7) {
  195.             IOWR(HEX_3_BASE, 0, (HEXA<<8));
  196.         } else if (switches & SW8) {
  197.             IOWR(HEX_3_BASE, 0, (HEXF<<8));
  198.         }
  199.  
  200.         break;
  201.     case 2:
  202.         IOWR(HEX_3_BASE, 0, HEX2);
  203.         break;
  204.     case 4:
  205.         IOWR(HEX_3_BASE, 0, HEX3);
  206.         break;
  207.     case 8:
  208.         IOWR(HEX_3_BASE, 0, HEX4);
  209.         break;
  210.     case 16:
  211.         IOWR(HEX_3_BASE, 0, HEX5);
  212.         break;
  213.     case 32:
  214.         IOWR(HEX_3_BASE, 0, HEX6);
  215.         break;
  216.     case 64:
  217.         IOWR(HEX_3_BASE, 0, HEX7);
  218.         break;
  219.     }
  220.     alt_alarm_stop(&alarmHEX);
  221.     return 0;
  222. }
  223.  
  224. int main() {
  225.     printf("Hello from Nios II!\n");
  226.     int state = 0;
  227.     int lastState = 0;
  228.     int timeToEventLeds = 1;
  229.     int timeToEventHex = 0;
  230.  
  231.     int which = 0;
  232.  
  233.     do {
  234.         int switches = IORD(SW_SLIDERS_BASE, 0);
  235.         state = switches & (SW0 | SW1 | SW2 | SW3 | SW4 | SW5);
  236.         if (lastState != state) {
  237.             switch (state) {
  238.             case 0:
  239.                 if (error) {
  240.                     timeToEventLeds = 3;
  241.                     timeToEventHex = 0;
  242.                 } else {
  243.                     timeToEventLeds = 2;
  244.                     timeToEventHex = 0;
  245.                 }
  246.                 error = 0;
  247.                 break;
  248.             case 1:
  249.                 if (error) {
  250.                     timeToEventLeds = 3;
  251.                     timeToEventHex = 0;
  252.                 } else {
  253.                     timeToEventLeds = 1;
  254.                     timeToEventHex = 0;
  255.                 }
  256.                 error = 0;
  257.                 break;
  258.             case 2:
  259.                 if (error) {
  260.                     timeToEventLeds = 3;
  261.                     timeToEventHex = 0;
  262.                 } else {
  263.                     timeToEventLeds = 1;
  264.                     timeToEventHex = 0;
  265.                 }
  266.                 error = 0;
  267.                 break;
  268.             case 4:
  269.                 if (error) {
  270.                     timeToEventLeds = 3;
  271.                     timeToEventHex = 0;
  272.                 } else {
  273.                     timeToEventLeds = 1;
  274.                     timeToEventHex = 0;
  275.                 }
  276.                 error = 0;
  277.                 break;
  278.             case 8:
  279.                 if (error) {
  280.                     timeToEventLeds = 3;
  281.                     timeToEventHex = 0;
  282.                 } else {
  283.                     timeToEventLeds = 1;
  284.                     timeToEventHex = 0;
  285.                 }
  286.                 error = 0;
  287.                 break;
  288.             case 16:
  289.                 if (error) {
  290.                     timeToEventLeds = 3;
  291.                     timeToEventHex = 0;
  292.                 } else {
  293.                     timeToEventLeds = 1;
  294.                     timeToEventHex = 0;
  295.                 }
  296.                 error = 0;
  297.                 break;
  298.             case 32:
  299.                 if (error) {
  300.                     timeToEventLeds = 3;
  301.                     timeToEventHex = 0;
  302.                 } else {
  303.                     timeToEventLeds = 1;
  304.                     timeToEventHex = 0;
  305.                 }
  306.                 error = 0;
  307.                 break;
  308.             case 64:
  309.                 if (error) {
  310.                     timeToEventLeds = 3;
  311.                     timeToEventHex = 0;
  312.                 } else {
  313.                     timeToEventLeds = 1;
  314.                     timeToEventHex = 0;
  315.                 }
  316.                 error = 0;
  317.                 break;
  318.             default:
  319.                 timeToEventLeds = 1;
  320.                 timeToEventHex = 0;
  321.                 error = 1;
  322.  
  323.                 break;
  324.             }
  325.  
  326.             if (error == 1) {
  327.                 //alt_alarm_start(&alarmERR,timeToEventHex * alt_ticks_per_second(), writeERR, NULL);//(void*)&which) ;
  328.                 printf("Hello from Nios II!\n");
  329.                 IOWR(HEX_3_BASE, 0, ((HEXE<<16) | (HEXr<<8) | HEXr));
  330.                 alt_alarm_start(&alarmERR,
  331.                         timeToEventLeds * alt_ticks_per_second(), writeERR,
  332.                         &which);
  333.                 if (which == 2) {
  334.                     alt_alarm_stop(&alarmERR);
  335.                     which = 0;
  336.                 }
  337.  
  338.                 //which = 1;
  339.                 //alt_alarm_start(&alarmERR,(timeToEventHex-timeToEventLeds) * alt_ticks_per_second(), writeERR, &which) ;
  340.             } else {
  341.                 alt_alarm_start(&alarmLED,
  342.                         timeToEventLeds * alt_ticks_per_second(), writeLEDs,
  343.                         NULL);
  344.                 alt_alarm_start(&alarmHEX,
  345.                         timeToEventHex * alt_ticks_per_second(), writeHEXes,
  346.                         NULL);
  347.             }
  348.         }
  349.         lastState = state;
  350.     } while (1);
  351.     return 0;
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement