Advertisement
Sothian

SW_Lab6_na4

Nov 18th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.78 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 "sys/alt_irq.h"
  20. #include <unistd.h>
  21. #include "sys/alt_alarm.h"
  22. #include "includes.h"
  23. #include "altera_up_avalon_parallel_port.h"
  24. #include "sys/alt_timestamp.h"
  25. #include "alt_types.h"
  26.  
  27. struct interrupt_data {
  28.     volatile int* leds_addr;
  29.     volatile int* sliders_addr;
  30.     volatile int* hex_addr;
  31. };
  32. /* Przełączniki */
  33. #define  SW0 0x00000001
  34. #define  SW1 0x00000002
  35. #define  SW2 0x00000004
  36. #define  SW3 0x00000008
  37. #define  SW4 0x00000010
  38. #define  SW5 0x00000020
  39. #define  SW6 0x00000040
  40. #define  SW7 0x00000080
  41. #define  SW8 0x00000100
  42. #define  SW9 0x00000200
  43. #define  SW10 0x00000400
  44. #define  SW11 0x00000800
  45. #define  SW12 0x00001000
  46. #define  SW13 0x00002000
  47. #define  SW14 0x00004000
  48. #define  SW15 0x00008000
  49. #define  SW16 0x00010000
  50. #define  SW17 0x00020000
  51.  
  52. /* PushButtony */
  53. #define  KEY1 0x00000002
  54. #define  KEY2 0x00000004
  55. #define  KEY3 0x00000008
  56.  
  57. /* Ledy */
  58. #define  LED0 0x00000001
  59. #define  LED1 0x00000002
  60. #define  LED2 0x00000004
  61. #define  LED3 0x00000008
  62. #define  LED4 0x00000010
  63. #define  LED5 0x00000020
  64. #define  LED6 0x00000040
  65. #define  LED7 0x00000080
  66. #define  LED8 0x00000100
  67. #define  LED9 0x00000200
  68. #define  LED10 0x00000400
  69. #define  LED11 0x00000800
  70. #define  LED12 0x00001000
  71. #define  LED13 0x00002000
  72. #define  LED14 0x00004000
  73. #define  LED15 0x00008000
  74. #define  LED16 0x00010000
  75. #define  LED17 0x00020000
  76.  
  77. /* Segmenty HEX */
  78. #define  SEGA 1
  79. #define  SEGB 2
  80. #define  SEGC 4
  81. #define  SEGD 8
  82. #define  SEGE 16
  83. #define  SEGF 32
  84. #define  SEGG 64
  85.  
  86. /* Litery i cyfry z hex 7 segmentowych */
  87. #define HEX0 (SEGA|SEGB|SEGC|SEGD|SEGE|SEGF)
  88. #define HEX1 (SEGB|SEGC)
  89. #define HEX2 (SEGA|SEGB|SEGG|SEGE|SEGD)
  90. #define HEX3 (SEGA|SEGB|SEGC|SEGD|SEGG)
  91. #define HEX4 (SEGF|SEGG|SEGB|SEGC)
  92. #define HEX5 (SEGA|SEGF|SEGG|SEGC|SEGD)
  93. #define HEX6 (SEGA|SEGF|SEGG|SEGC|SEGD|SEGE)
  94. #define HEX7 (SEGA|SEGB|SEGC)
  95. #define HEX8 (SEGA|SEGB|SEGC|SEGD|SEGE|SEGF|SEGG)
  96. #define HEX9 (SEGA|SEGB|SEGC|SEGD|SEGF|SEGG)
  97. #define HEXE (SEGA|SEGF|SEGG|SEGE|SEGD)
  98. #define HEXr (SEGE|SEGG)
  99. #define HEXA (SEGA|SEGB|SEGC|SEGE|SEGF|SEGG)
  100. #define HEXF (SEGA|SEGE|SEGF|SEGG)
  101. #define HEXC (SEGA|SEGE|SEGF|SEGD)
  102. #define HEXD (SEGB|SEGC|SEGD|SEGE|SEGG)
  103.  
  104. typedef enum POZIOM_TEMP {
  105.     POZIOM_0 = 0,
  106.     POZIOM_1 = 2,
  107.     POZIOM_2 = 4,
  108.     POZIOM_3 = 8,
  109.     POZIOM_4 = 16,
  110.     POZIOM_5 = 32,
  111.     POZIOM_6 = 64,
  112.     POZIOM_7 = 128,
  113.     POZIOM_8 = 256,
  114.     POZIOM_9 = 512,
  115.     POZIOM_10 = 1024,
  116.  
  117. } TEMP;
  118.  
  119. #define TASK_STACKSIZE 2048
  120. OS_STK task1_stk[TASK_STACKSIZE];
  121. OS_STK task2_stk[TASK_STACKSIZE];
  122. OS_STK task3_stk[TASK_STACKSIZE];
  123. OS_STK task4_stk[TASK_STACKSIZE];
  124. OS_STK task5_stk[TASK_STACKSIZE];
  125. OS_STK task6_stk[TASK_STACKSIZE];
  126. OS_STK task7_stk[TASK_STACKSIZE];
  127.  
  128. OS_EVENT *SWBox1;
  129.  
  130. /* Definition of Task Priorities */
  131.  
  132. #define TASK1_PRIORITY 1
  133. #define TASK2_PRIORITY 2
  134. #define TASK3_PRIORITY 3
  135. #define TASK4_PRIORITY 4
  136. #define TASK5_PRIORITY 5
  137. #define TASK6_PRIORITY 6
  138. #define TASK7_PRIORITY 7
  139.  
  140. void VGA_text(int, int, char *);
  141.  
  142. /* Prints "Hello World" and sleeps for three seconds */
  143. void task1(void* pdata) {
  144.     while (1) {
  145.         printf("Hello from task 1\n");
  146.         int sw;
  147.         sw = IORD(SW_SLIDERS_BASE, 0);
  148.         sw = sw & (SW0 | SW1 | SW2 | SW3 | SW4 | SW5 | SW6);
  149.  
  150.         printf("Slider = %d\n", sw);
  151.         OSMboxPostOpt(SWBox1, &sw, OS_POST_OPT_BROADCAST);
  152.  
  153.         if (sw == 0) {
  154.             IOWR(HEX_3_BASE, 0, 0);
  155.             IOWR(LEDS_RED_BASE, 0, 0);
  156.         } else if (sw != SW0 && sw != SW1 && sw != SW2 && sw != SW3 && sw != SW4
  157.                 && sw != SW5 && sw != SW6 && sw < 128) {
  158.             IOWR(HEX_3_BASE, 0, ((HEXE<<16) | (HEXr<<8) | HEXr));
  159.             IOWR(LEDS_RED_BASE, 0, LED17);
  160.         }
  161.         if (sw == SW0) {
  162.             int subSw;
  163.             subSw = IORD(SW_SLIDERS_BASE, 0);
  164.             subSw = subSw & (SW7 | SW8);
  165.  
  166.             IOWR(HEX_3_BASE, 0, HEX1);
  167.             IOWR(LEDS_RED_BASE, 0, LED0);
  168.  
  169.             if ((subSw & SW7) && (subSw & SW8)) {
  170.                 IOWR(HEX_3_BASE, 0, ((HEXE<<16) | (HEXr<<8) | HEXr));
  171.                 IOWR(LEDS_RED_BASE, 0, LED17);
  172.             } else if (subSw == SW7) {
  173.                 IOWR(LEDS_RED_BASE, 0, LED0 | LED7);
  174.                 IOWR(HEX_3_BASE, 0, (HEXA<<8) | HEX1);
  175.             } else if (subSw == SW8) {
  176.                 IOWR(LEDS_RED_BASE, 0, LED0 | LED8);
  177.                 IOWR(HEX_3_BASE, 0, (HEXF<<8) | HEX1);
  178.             }
  179.         }
  180.  
  181.         OSTimeDlyHMSM(0, 0, 1, 0);
  182.     }
  183. }
  184.  
  185. /* Prints "Hello World" and sleeps for three seconds */
  186. void task2(void* pdata) {
  187.     while (1) {
  188.         INT8U err;
  189.         printf("Hello from task 2\n");
  190.         int *sw;
  191.  
  192.         sw = OSMboxPend(SWBox1, 0, &err);
  193.  
  194.         printf("T=%d\n", *sw);
  195.         if (*sw == SW1) {
  196.             IOWR(HEX_3_BASE, 0, HEX2);
  197.             IOWR(LEDS_RED_BASE, 0, LED1);
  198.         }
  199.         OSTimeDlyHMSM(0, 0, 1, 0);
  200.     }
  201. }
  202.  
  203. void task3(void* pdata) {
  204.     while (1) {
  205.         INT8U err;
  206.         printf("Hello from task 3\n");
  207.         int *sw;
  208.  
  209.         sw = OSMboxPend(SWBox1, 0, &err);
  210.  
  211.         printf("T=%d\n", *sw);
  212.         if (*sw == SW2) {
  213.             IOWR(HEX_3_BASE, 0, HEX3);
  214.             IOWR(LEDS_RED_BASE, 0, LED2);
  215.         }
  216.         OSTimeDlyHMSM(0, 0, 1, 0);
  217.     }
  218. }
  219. void task4(void* pdata) {
  220.     while (1) {
  221.         INT8U err;
  222.         printf("Hello from task 4\n");
  223.         int *sw;
  224.  
  225.         sw = OSMboxPend(SWBox1, 0, &err);
  226.  
  227.         printf("T=%d\n", *sw);
  228.         if (*sw == SW3) {
  229.             int subSw;
  230.             subSw = IORD(SW_SLIDERS_BASE, 0);
  231.             subSw = subSw & (SW9 | SW10 | SW11 | SW12);
  232.  
  233.             IOWR(HEX_3_BASE, 0, HEX4);
  234.             IOWR(LEDS_RED_BASE, 0, LED3);
  235.  
  236.             if (((subSw & SW9) && (subSw & SW10))
  237.                     || ((subSw & SW9) && (subSw & SW11))
  238.                     || ((subSw & SW9) && (subSw & SW12))
  239.                     || ((subSw & SW10) && (subSw & SW11))
  240.                     || ((subSw & SW10) && (subSw & SW12))
  241.                     || ((subSw & SW11) && (subSw & SW12))) {
  242.                 IOWR(LEDS_RED_BASE, 0, LED17);
  243.                 IOWR(HEX_3_BASE, 0, ((HEXE<<24) | (HEXr<<16) | (HEXr<<8)));
  244.             } else if (subSw == SW9) {
  245.                 IOWR(LEDS_RED_BASE, 0, LED9 | LED3);
  246.                 IOWR(HEX_3_BASE, 0, ((HEXC<<16)| (HEX1 <<8) | HEX4));
  247.             } else if (subSw == SW10) {
  248.                 IOWR(LEDS_RED_BASE, 0, LED10 | LED3);
  249.                 IOWR(HEX_3_BASE, 0, ((HEXC<<16)| (HEX2 <<8) | HEX4));
  250.             } else if (subSw == SW11) {
  251.                 IOWR(LEDS_RED_BASE, 0, LED11 | LED3);
  252.                 IOWR(HEX_3_BASE, 0, ((HEXD<<16)| (HEX1 <<8) | HEX4));
  253.             } else if (subSw == SW12) {
  254.                 IOWR(LEDS_RED_BASE, 0, LED12 | LED3);
  255.                 IOWR(HEX_3_BASE, 0, ((HEXD<<16)| (HEX2 <<8) | HEX4));
  256.             }
  257.         }
  258.  
  259.         OSTimeDlyHMSM(0, 0, 1, 0);
  260.     }
  261. }
  262. void task5(void* pdata) {
  263.     while (1) {
  264.         INT8U err;
  265.         printf("Hello from task 5\n");
  266.         int *sw;
  267.  
  268.         sw = OSMboxPend(SWBox1, 0, &err);
  269.  
  270.         printf("T=%d\n", *sw);
  271.         if (*sw == SW4) {
  272.             IOWR(HEX_3_BASE, 0, HEX5);
  273.             IOWR(LEDS_RED_BASE, 0, LED4);
  274.         }
  275.         OSTimeDlyHMSM(0, 0, 1, 0);
  276.     }
  277. }
  278. void task6(void* pdata) {
  279.     while (1) {
  280.         INT8U err;
  281.         printf("Hello from task 6\n");
  282.         int *sw;
  283.  
  284.         sw = OSMboxPend(SWBox1, 0, &err);
  285.  
  286.         printf("T=%d\n", *sw);
  287.         if (*sw == SW5) {
  288.             IOWR(HEX_3_BASE, 0, HEX6);
  289.             IOWR(LEDS_RED_BASE, 0, LED5);
  290.         }
  291.         OSTimeDlyHMSM(0, 0, 1, 0);
  292.     }
  293. }
  294. void task7(void* pdata) {
  295.     while (1) {
  296.         INT8U err;
  297.         printf("Hello from task 7\n");
  298.         int *sw;
  299.  
  300.         sw = OSMboxPend(SWBox1, 0, &err);
  301.  
  302.         printf("T=%d\n", *sw);
  303.         if (*sw == SW6) {
  304.             IOWR(HEX_3_BASE, 0, HEX7);
  305.             IOWR(LEDS_RED_BASE, 0, LED6);
  306.         }
  307.         OSTimeDlyHMSM(0, 0, 1, 0);
  308.     }
  309. }
  310.  
  311. // IOWR(HEX_3_BASE, 0, ((HEX9<<8)| HEX0));
  312.  
  313. /* The main function creates two task and starts multi-tasking */
  314. int main(void) {
  315.  
  316.     SWBox1 = OSMboxCreate((void*) 0);
  317.  
  318.     OSTaskCreateExt(task1,
  319.     NULL, (void *) &task1_stk[TASK_STACKSIZE - 1],
  320.     TASK1_PRIORITY,
  321.     TASK1_PRIORITY, task1_stk,
  322.     TASK_STACKSIZE,
  323.     NULL, 0);
  324.  
  325.     OSTaskCreateExt(task2,
  326.     NULL, (void *) &task2_stk[TASK_STACKSIZE - 1],
  327.     TASK2_PRIORITY,
  328.     TASK2_PRIORITY, task2_stk,
  329.     TASK_STACKSIZE,
  330.     NULL, 0);
  331.  
  332.     OSTaskCreateExt(task3,
  333.     NULL, (void *) &task3_stk[TASK_STACKSIZE - 1],
  334.     TASK3_PRIORITY,
  335.     TASK3_PRIORITY, task3_stk,
  336.     TASK_STACKSIZE,
  337.     NULL, 0);
  338.  
  339.     OSTaskCreateExt(task4,
  340.     NULL, (void *) &task4_stk[TASK_STACKSIZE - 1],
  341.     TASK4_PRIORITY,
  342.     TASK4_PRIORITY, task4_stk,
  343.     TASK_STACKSIZE,
  344.     NULL, 0);
  345.  
  346.     OSTaskCreateExt(task5,
  347.     NULL, (void *) &task5_stk[TASK_STACKSIZE - 1],
  348.     TASK5_PRIORITY,
  349.     TASK5_PRIORITY, task5_stk,
  350.     TASK_STACKSIZE,
  351.     NULL, 0);
  352.  
  353.     OSTaskCreateExt(task6,
  354.     NULL, (void *) &task6_stk[TASK_STACKSIZE - 1],
  355.     TASK6_PRIORITY,
  356.     TASK6_PRIORITY, task6_stk,
  357.     TASK_STACKSIZE,
  358.     NULL, 0);
  359.  
  360.     OSTaskCreateExt(task7,
  361.     NULL, (void *) &task7_stk[TASK_STACKSIZE - 1],
  362.     TASK7_PRIORITY,
  363.     TASK7_PRIORITY, task7_stk,
  364.     TASK_STACKSIZE,
  365.     NULL, 0);
  366.  
  367.     OSStart();
  368.  
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement