Advertisement
Sothian

SW_Lab7_na4.5

Nov 25th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.06 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. // alt_up_parallel_port_dev *uchwytLEDR;
  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. int lastState = 0;
  143. int startNicks = 0;
  144. int startTimeHex = 0;
  145.  
  146. static alt_alarm alarmerror;
  147.  
  148. static int shouldError = 0;
  149.  
  150. alt_u32 writeERROR(void* context) {
  151.     printf("OK_ERR\n");
  152.     if (shouldError == 0) {
  153.         IOWR(LEDS_RED_BASE, 0, LED17);
  154.         shouldError = 1;
  155.         return alt_ticks_per_second() * 2;
  156.     } else {
  157.         shouldError = 0;
  158.         return 0;
  159.     }
  160. }
  161.  
  162. /* Prints "Hello World" and sleeps for three seconds */
  163. void task1(void* pdata) {
  164.     while (1) {
  165.         //printf("Hello from task 1\n");
  166.         int sw;
  167.         sw = IORD(SW_SLIDERS_BASE, 0);
  168.         sw = sw & (SW0 | SW1 | SW2 | SW3 | SW4 | SW5 | SW6);
  169.  
  170.         // Odliczanie czasu
  171.         if (sw != lastState) {
  172.             startNicks = alt_nticks();
  173.             //startTimeHex = alt_timestamp();
  174.             //printf("%d \n", startTimeHex);
  175.             //printf("%d \n", startNicks);
  176.         }
  177.  
  178.         //printf("Slider = %d\n", sw);
  179.         OSMboxPostOpt(SWBox1, &sw, OS_POST_OPT_BROADCAST);
  180.  
  181.         if (sw == 0) {
  182.             // Odliczanie czasu
  183.             if (sw != lastState) {
  184.                 startNicks = alt_nticks();
  185.             }
  186.             // Hex bez opoznienia
  187.             IOWR(HEX_3_BASE, 0, 0);
  188.  
  189.             // Diody z opoznieneim
  190.             if (((alt_nticks() - startNicks) >= (2 * alt_ticks_per_second()))) {
  191.                 IOWR(LEDS_RED_BASE, 0, 0);
  192.             }
  193.  
  194.         } else if (sw != SW0 && sw != SW1 && sw != SW2 && sw != SW3 && sw != SW4
  195.                 && sw != SW5 && sw != SW6 && sw < 128) {
  196.             if (sw != lastState) {
  197.                 // Hex bez opoznienia
  198.                 IOWR(HEX_3_BASE, 0, ((HEXE<<16) | (HEXr<<8) | HEXr));
  199.                 shouldError = 0;
  200.                 // Ledy funkcja z opoznieniem
  201.                 alt_alarm_start(&alarmerror, alt_ticks_per_second(), writeERROR,
  202.                         sw);
  203.             }
  204.         }
  205.         if (sw == SW0 && shouldError == 0) {
  206.             int subSw;
  207.             subSw = IORD(SW_SLIDERS_BASE, 0);
  208.             subSw = subSw & (SW7 | SW8);
  209.  
  210.             IOWR(HEX_3_BASE, 0, HEX1);
  211.             IOWR(LEDS_RED_BASE, 0, LED0);
  212.  
  213.             if ((subSw & SW7) && (subSw & SW8)) {
  214.                 IOWR(HEX_3_BASE, 0, ((HEXE<<16) | (HEXr<<8) | HEXr));
  215.                 IOWR(LEDS_RED_BASE, 0, LED17);
  216.                 OSTimeDlyHMSM(0, 0, 1, 0);
  217.             } else if (subSw == SW7) {
  218.                 IOWR(LEDS_RED_BASE, 0, LED0 | LED7);
  219.                 IOWR(HEX_3_BASE, 0, (HEXA<<8) | HEX1);
  220.                 OSTimeDlyHMSM(0, 0, 1, 0);
  221.             } else if (subSw == SW8) {
  222.                 IOWR(LEDS_RED_BASE, 0, LED0 | LED8);
  223.                 IOWR(HEX_3_BASE, 0, (HEXF<<8) | HEX1);
  224.                 OSTimeDlyHMSM(0, 0, 1, 0);
  225.             }
  226.         }
  227.         OSTimeDlyHMSM(0, 0, 1, 0);
  228.         lastState = sw;
  229.     }
  230. }
  231.  
  232. /* Prints "Hello World" and sleeps for three seconds */
  233. void task2(void* pdata) {
  234.     while (1) {
  235.         INT8U err;
  236.         //printf("Hello from task 2\n");
  237.         int *sw;
  238.         sw = OSMboxPend(SWBox1, 0, &err);
  239.  
  240.         if (err == OS_NO_ERR) {
  241.             //printf("T=%d\n", *sw);
  242.             if (*sw == SW1) {
  243.                 IOWR(HEX_3_BASE, 0, HEX2);
  244.                 IOWR(LEDS_RED_BASE, 0, LED1);
  245.                 //OSTimeDlyHMSM(0, 0, 1, 0);
  246.             }
  247.             OSTimeDlyHMSM(0, 0, 1, 0);
  248.         } else if (err == OS_TIMEOUT) {
  249.             printf("Timeout\n");
  250.             OSTimeDlyHMSM(0, 0, 1, 0);
  251.         }
  252.  
  253.     }
  254. }
  255.  
  256. void task3(void* pdata) {
  257.     while (1) {
  258.         INT8U err;
  259.         //printf("Hello from task 3\n");
  260.         int *sw;
  261.         sw = OSMboxPend(SWBox1, 0, &err);
  262.  
  263.         if (err == OS_NO_ERR) {
  264.             //printf("T=%d\n", *sw);
  265.             if (*sw == SW2) {
  266.                 IOWR(HEX_3_BASE, 0, HEX3);
  267.                 IOWR(LEDS_RED_BASE, 0, LED2);
  268.                 //OSTimeDlyHMSM(0, 0, 1, 0);
  269.             }
  270.             OSTimeDlyHMSM(0, 0, 1, 0);
  271.         } else if (err == OS_TIMEOUT) {
  272.             printf("Timeout\n");
  273.             OSTimeDlyHMSM(0, 0, 1, 0);
  274.         }
  275.     }
  276. }
  277.  
  278. void task4(void* pdata) {
  279.     while (1) {
  280.         INT8U err;
  281.         //printf("Hello from task 4\n");
  282.         int *sw;
  283.         sw = OSMboxPend(SWBox1, 0, &err);
  284.  
  285.         if (err == OS_NO_ERR) {
  286.             //printf("T=%d\n", *sw);
  287.             if (*sw == SW3) {
  288.                 int subSw;
  289.                 subSw = IORD(SW_SLIDERS_BASE, 0);
  290.                 subSw = subSw & (SW9 | SW10 | SW11 | SW12);
  291.  
  292.                 IOWR(HEX_3_BASE, 0, HEX4);
  293.                 IOWR(LEDS_RED_BASE, 0, LED3);
  294.  
  295.                 if (((subSw & SW9) && (subSw & SW10))
  296.                         || ((subSw & SW9) && (subSw & SW11))
  297.                         || ((subSw & SW9) && (subSw & SW12))
  298.                         || ((subSw & SW10) && (subSw & SW11))
  299.                         || ((subSw & SW10) && (subSw & SW12))
  300.                         || ((subSw & SW11) && (subSw & SW12))) {
  301.                     IOWR(LEDS_RED_BASE, 0, LED17);
  302.                     IOWR(HEX_3_BASE, 0, ((HEXE<<24) | (HEXr<<16) | (HEXr<<8)));
  303.                     OSTimeDlyHMSM(0, 0, 1, 0);
  304.                 } else if (subSw == SW9) {
  305.                     IOWR(LEDS_RED_BASE, 0, LED9 | LED3);
  306.                     IOWR(HEX_3_BASE, 0, ((HEXC<<16)| (HEX1 <<8) | HEX4));
  307.                     OSTimeDlyHMSM(0, 0, 1, 0);
  308.                 } else if (subSw == SW10) {
  309.                     IOWR(LEDS_RED_BASE, 0, LED10 | LED3);
  310.                     IOWR(HEX_3_BASE, 0, ((HEXC<<16)| (HEX2 <<8) | HEX4));
  311.                     OSTimeDlyHMSM(0, 0, 1, 0);
  312.                 } else if (subSw == SW11) {
  313.                     IOWR(LEDS_RED_BASE, 0, LED11 | LED3);
  314.                     IOWR(HEX_3_BASE, 0, ((HEXD<<16)| (HEX1 <<8) | HEX4));
  315.                     OSTimeDlyHMSM(0, 0, 1, 0);
  316.                 } else if (subSw == SW12) {
  317.                     IOWR(LEDS_RED_BASE, 0, LED12 | LED3);
  318.                     IOWR(HEX_3_BASE, 0, ((HEXD<<16)| (HEX2 <<8) | HEX4));
  319.                     OSTimeDlyHMSM(0, 0, 1, 0);
  320.                 }
  321.  
  322.                 //OSTimeDlyHMSM(0, 0, 1, 0);
  323.             }
  324.             OSTimeDlyHMSM(0, 0, 1, 0);
  325.         } else if (err == OS_TIMEOUT) {
  326.             printf("Timeout\n");
  327.             OSTimeDlyHMSM(0, 0, 1, 0);
  328.         }
  329.     }
  330. }
  331. void task5(void* pdata) {
  332.     while (1) {
  333.         INT8U err;
  334.         //printf("Hello from task 5\n");
  335.         int *sw;
  336.         sw = OSMboxPend(SWBox1, 0, &err);
  337.  
  338.         if (err == OS_NO_ERR) {
  339.             //printf("T=%d\n", *sw);
  340.             if (*sw == SW4) {
  341.                 IOWR(HEX_3_BASE, 0, HEX5);
  342.                 IOWR(LEDS_RED_BASE, 0, LED4);
  343.                 //OSTimeDlyHMSM(0, 0, 1, 0);
  344.             }
  345.             OSTimeDlyHMSM(0, 0, 1, 0);
  346.         } else if (err == OS_TIMEOUT) {
  347.             printf("Timeout\n");
  348.             OSTimeDlyHMSM(0, 0, 1, 0);
  349.         }
  350.     }
  351. }
  352. void task6(void* pdata) {
  353.     while (1) {
  354.         INT8U err;
  355.         //printf("Hello from task 6\n");
  356.         int *sw;
  357.         sw = OSMboxPend(SWBox1, 0, &err);
  358.  
  359.         if (err == OS_NO_ERR) {
  360.             //printf("T=%d\n", *sw);
  361.             if (*sw == SW5) {
  362.                 IOWR(HEX_3_BASE, 0, HEX6);
  363.                 IOWR(LEDS_RED_BASE, 0, LED5);
  364.                 //OSTimeDlyHMSM(0, 0, 1, 0);
  365.             }
  366.             OSTimeDlyHMSM(0, 0, 1, 0);
  367.         } else if (err == OS_TIMEOUT) {
  368.             printf("Timeout\n");
  369.             OSTimeDlyHMSM(0, 0, 1, 0);
  370.         }
  371.     }
  372. }
  373. void task7(void* pdata) {
  374.     while (1) {
  375.         INT8U err;
  376.         //printf("Hello from task 7\n");
  377.         int *sw;
  378.         sw = OSMboxPend(SWBox1, 0, &err);
  379.  
  380.         if (err == OS_NO_ERR) {
  381.             //printf("T=%d\n", *sw);
  382.             if (*sw == SW6) {
  383.                 IOWR(HEX_3_BASE, 0, HEX7);
  384.                 IOWR(LEDS_RED_BASE, 0, LED6);
  385.                 //OSTimeDlyHMSM(0, 0, 1, 0);
  386.             }
  387.             OSTimeDlyHMSM(0, 0, 1, 0);
  388.         } else if (err == OS_TIMEOUT) {
  389.             printf("Timeout\n");
  390.             OSTimeDlyHMSM(0, 0, 1, 0);
  391.         }
  392.     }
  393. }
  394.  
  395. // IOWR(HEX_3_BASE, 0, ((HEX9<<8)| HEX0));
  396.  
  397. /* The main function creates two task and starts multi-tasking */
  398. int main(void) {
  399.  
  400.     SWBox1 = OSMboxCreate((void*) 0);
  401.     //alt_timestamp_start();
  402.  
  403.     OSTaskCreateExt(task1,
  404.     NULL, (void *) &task1_stk[TASK_STACKSIZE - 1],
  405.     TASK1_PRIORITY,
  406.     TASK1_PRIORITY, task1_stk,
  407.     TASK_STACKSIZE,
  408.     NULL, 0);
  409.  
  410.     OSTaskCreateExt(task2,
  411.     NULL, (void *) &task2_stk[TASK_STACKSIZE - 1],
  412.     TASK2_PRIORITY,
  413.     TASK2_PRIORITY, task2_stk,
  414.     TASK_STACKSIZE,
  415.     NULL, 0);
  416.  
  417.     OSTaskCreateExt(task3,
  418.     NULL, (void *) &task3_stk[TASK_STACKSIZE - 1],
  419.     TASK3_PRIORITY,
  420.     TASK3_PRIORITY, task3_stk,
  421.     TASK_STACKSIZE,
  422.     NULL, 0);
  423.  
  424.     OSTaskCreateExt(task4,
  425.     NULL, (void *) &task4_stk[TASK_STACKSIZE - 1],
  426.     TASK4_PRIORITY,
  427.     TASK4_PRIORITY, task4_stk,
  428.     TASK_STACKSIZE,
  429.     NULL, 0);
  430.  
  431.     OSTaskCreateExt(task5,
  432.     NULL, (void *) &task5_stk[TASK_STACKSIZE - 1],
  433.     TASK5_PRIORITY,
  434.     TASK5_PRIORITY, task5_stk,
  435.     TASK_STACKSIZE,
  436.     NULL, 0);
  437.  
  438.     OSTaskCreateExt(task6,
  439.     NULL, (void *) &task6_stk[TASK_STACKSIZE - 1],
  440.     TASK6_PRIORITY,
  441.     TASK6_PRIORITY, task6_stk,
  442.     TASK_STACKSIZE,
  443.     NULL, 0);
  444.  
  445.     OSTaskCreateExt(task7,
  446.     NULL, (void *) &task7_stk[TASK_STACKSIZE - 1],
  447.     TASK7_PRIORITY,
  448.     TASK7_PRIORITY, task7_stk,
  449.     TASK_STACKSIZE,
  450.     NULL, 0);
  451.  
  452.     OSStart();
  453.  
  454. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement