Advertisement
Sothian

SW_6_v1

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