Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <system.h>
  4. #include "alt_types.h"
  5. #include "altera_avalon_pio_regs.h"
  6. #include "sys/alt_irq.h"
  7. #include "sys/alt_timestamp.h"
  8. #include <unistd.h>
  9. #include <sys/alt_alarm.h>
  10. #include <math.h>
  11. #include "includes.h"
  12. #include <altera_avalon_mutex.h>
  13. //lab10
  14. #define  SW0 0x00000001            /* CR1 */
  15. #define  SW1 0x00000002            /* CR2 */
  16. #define  SW2 0x00000004            /* CR3 */
  17. #define  SW3 0x00000008            /* CR4 */
  18. #define  SW4 0x00000010            /* CD1 */
  19. #define  SW5 0x00000020            /* CD2 */
  20. #define  SW6 0x00000040            /* CD3 */
  21. #define  SW7 0x00000080            /* WB1 */
  22.  
  23. #define  SEGA 0x00001
  24. #define  SEGB 0x00002
  25. #define  SEGC 0x00004
  26. #define  SEGD 0x00008
  27. #define  SEGE 0x00010
  28. #define  SEGF 0x00020
  29. #define  SEGG 0x00040
  30.  
  31. #define ZERO SEGA | SEGB | SEGC | SEGD |SEGE | SEGF
  32. #define ONE  SEGB | SEGC
  33. #define TWO  SEGA | SEGB | SEGG | SEGE | SEGD
  34. #define THREE SEGA | SEGB | SEGC | SEGD | SEGG
  35. #define FOUR SEGB | SEGC | SEGF | SEGG
  36. #define FIVE SEGA | SEGC | SEGD | SEGF | SEGG
  37. #define SIX SEGA | SEGC | SEGD | SEGE | SEGF | SEGG
  38. #define E SEGA | SEGD | SEGE | SEGF | SEGG
  39. #define r SEGE | SEGG
  40.  
  41. typedef enum POZIOM_TEMP {
  42.     POZIOM_0 = 0,
  43.     POZIOM_1 = 2,
  44.     POZIOM_2 = 4,
  45.     POZIOM_3 = 8,
  46.     POZIOM_4 = 16,
  47.     POZIOM_5 = 32,
  48.     POZIOM_6 = 64,
  49.     POZIOM_7 = 128,
  50.     POZIOM_8 = 256,
  51.     POZIOM_9 = 512,
  52.     POZIOM_10 = 1024,
  53.  
  54. } TEMP;
  55.  
  56. #define LED0 0x00000001
  57. #define LED1 0x00000002
  58. #define LED2 0x00000004
  59. #define LED3 0x00000008
  60.  
  61. /* Definition of Task Stacks */
  62. #define TASK_STACKSIZE 2048
  63. OS_STK task0_stk[TASK_STACKSIZE];
  64. OS_STK task1_stk[TASK_STACKSIZE];
  65. OS_STK task2_stk[TASK_STACKSIZE];
  66. OS_STK task3_stk[TASK_STACKSIZE];
  67. OS_STK task4_stk[TASK_STACKSIZE];
  68. OS_STK task5_stk[TASK_STACKSIZE];
  69. OS_STK task6_stk[TASK_STACKSIZE];
  70.  
  71. OS_EVENT *SWBox1;
  72. #define VGA VGA_CHAR_BUFFER_AVALON_CHAR_BUFFER_SLAVE_BASE
  73. /* Definition of Task Priorities */
  74. #define TASK0_PRIORITY 0
  75. #define TASK1_PRIORITY 3
  76. #define TASK2_PRIORITY 2
  77. #define TASK3_PRIORITY 1
  78. #define TASK4_PRIORITY 4
  79. #define TASK5_PRIORITY 5
  80. #define TASK6_PRIORITY 6
  81.  
  82. //alt_up_parallel_port_dev const *uchwytLEDR; = alt_up_parallel_port_open_dev("/dev/parallel_port_2");
  83.  
  84. //alt_up_parallel_port_dev *uchwytLEDR;
  85. alt_u32 SWoff[6];
  86. alt_u32 SWon[6];
  87. alt_u32 errOn = -1;
  88. alt_u32 offBlockTime = 0;
  89.  
  90. int STATE = -1;
  91. /* Prints "Hello World" and sleeps for three seconds */
  92. int flag = 1;
  93. int flag_off = 1;
  94. int flag_err = 1;
  95. int flag_err_off = 1;
  96. static alt_alarm alarm1;
  97. static alt_alarm alarmOff;
  98. static alt_alarm alarmErr;
  99. static alt_alarm alarmErrOff;
  100. int state = 0;
  101.  
  102.  
  103.  
  104. void clear() {
  105.     IOWR(HEX_BASE, 0, 0);
  106.     IOWR(HEX_BASE, 1, 0);
  107.     IOWR(HEX_BASE, 2, 0);
  108.     IOWR(HEX_BASE, 3, 0);
  109.     IOWR(HEX_BASE, 4, 0);
  110.     IOWR(HEX_BASE, 5, 0);
  111. }
  112. void VGA_text(int x, int y, char * text_ptr)
  113. {
  114.     int offset;
  115.     /* Declare volatile pointer to char buffer (volatile means that IO load
  116.        and store instructions will be used to access these pointer locations,
  117.        instead of regular memory loads and stores) */
  118.     volatile char * character_buffer = (char *) VGA;    // VGA character buffer
  119.  
  120.     /* assume that the text string fits on one line */
  121.     offset = (y << 7) + x;
  122.     while ( *(text_ptr) )
  123.     {
  124.         *(character_buffer + offset) = *(text_ptr); // write to the character buffer
  125.         ++text_ptr;
  126.         ++offset;
  127.     }
  128. }
  129.  
  130. alt_u32 alarm_proc(void *context) {
  131.     state = 0;
  132.     flag = 1;
  133.  
  134.     if (context == 0b0100) {
  135.         IOWR(LAB8_0_BASE,0, context);
  136.     }
  137. //  else if(context == 0b1000) {
  138. //  }
  139.     else {
  140.         IOWR(LEDS_BASE, 0, context);
  141.     }
  142. //  IOWR(SHARED_MEMORY_BASE, 0, context);
  143.  
  144.  
  145.     //printf(&context);
  146.  
  147.  
  148.  
  149.     return 0;
  150. }
  151.  
  152. void task0(void* pdata) {
  153.     while (1) {
  154.         if (flag_err == 0) {
  155.             if (alt_nticks() - errOn >= alt_ticks_per_second() * 3) {
  156.                 IOWR(LEDS_BASE, 0, 0b0100000000);
  157.                 flag_err = 1;
  158.             }
  159.         }
  160.         INT8U err;
  161.         int *num;
  162.         num = OSMboxPend(SWBox1, 0, &err);
  163.         if (*num != 0b1 && *num != 0b10 && *num != 0b100 && *num != 0b1000
  164.                 && *num != 0b10000 && *num != 0b100000 && *num != 0) {
  165.             if (flag_err != 0) {
  166.                 IOWR(HEX_BASE, 2, E);
  167.                 IOWR(HEX_BASE, 1, r);
  168.                 IOWR(HEX_BASE, 0, r);
  169.                 char text1_1_row[40] = "error";
  170.                 VGA_text (30, 15, text1_1_row);
  171.  
  172.                 errOn = alt_nticks();
  173.                 flag_err = 0;
  174.             }
  175.  
  176.         }
  177.         OSTimeDlyHMSM(0, 0, 0, 10);
  178.     }
  179. }
  180.  
  181. void task1(void* pdata) {
  182.     while (1) {
  183.         int sw;
  184.         int * msg;
  185.         sw = IORD(SW_SLIDERS_BASE, 0);
  186.         //printf(sw);
  187.         IOWR(SHARED_MEMORY_BASE, 0, sw);
  188.         OSMboxPostOpt(SWBox1, &sw, OS_POST_OPT_BROADCAST);
  189.         if (sw == 0b1) {
  190.             clear();
  191.             IOWR(HEX_BASE, 0, ONE);
  192.             if (flag != 0) {
  193.                 alt_alarm_stop(&alarm1);
  194. //                  alt_alarm_stop(&alarmErrOff);
  195.                 alt_alarm_start(&alarm1, alt_ticks_per_second(), alarm_proc,
  196.                         sw);
  197.                 char text1_1_row[40] = "task1";
  198.                 VGA_text (30, 15, text1_1_row);
  199.                 flag = 0;
  200.             }
  201.             STATE = 0;
  202.         }
  203.         if (sw == 0) {
  204.             for (int i = 0; i < 6; i++) {
  205.                 alt_u32 t1 = alt_timestamp();
  206.                 if (SWoff[i] != -1) {
  207.                     alt_u32 t2 = alt_timestamp();
  208.                     if ((t2 - SWoff[i]) / alt_timestamp_freq() >= 2) {
  209.                         float var = (t2 - SWoff[i]) / alt_timestamp_freq();
  210.                         //printf("var=%.6f", var);
  211.                         //printf("clr ");
  212.                         SWoff[i] = -1;
  213.                         IOWR(LEDS_BASE, 0, 0);
  214.                         char text1_1_row[40] = "                       ";
  215.                         VGA_text (30, 15, text1_1_row);
  216.                         //IOWR(SHARED_MEMORY_BASE, 0,a);
  217.                         IOWR(LAB8_0_BASE,0,0);
  218.                     }
  219.                 }
  220.             }
  221.             IOWR(LAB8_0_BASE,0,0);
  222.             if (STATE != -1) {
  223.                 clear();
  224.                 //printf("off");
  225.                 SWoff[STATE] = alt_timestamp();
  226.             }
  227.             STATE = -1;
  228.  
  229.         }
  230.         OSTimeDlyHMSM(0, 0, 0, 10);
  231.     }
  232. }
  233. /* Prints "Hello World" and sleeps for three seconds */
  234. void task2(void* pdata) {
  235.     while (1) {
  236.         //IOWR(LAB81_1_BASE,0, 0);
  237.         INT8U err;
  238.         int *num;
  239.         num = OSMboxPend(SWBox1, 0, &err);
  240.         if (*num == 0b10) {
  241.  
  242.             if (flag != 0) {
  243.                 //IOWR(LAB81_1_BASE,0, 0b1000);
  244.                 alt_alarm_stop(&alarm1);
  245. //                  alt_alarm_stop(&alarmErrOff);
  246.                 alt_alarm_start(&alarm1, alt_ticks_per_second(), alarm_proc,
  247.                         *num);
  248.                 flag = 0;
  249.                 IOWR(LAB8_0_BASE,0,0);
  250.                 char text1_1_row[40] = "task2";
  251.                 VGA_text (30, 15, text1_1_row);
  252.             }
  253.             clear();
  254.             IOWR(HEX_BASE, 0, TWO);
  255.             STATE = 1;
  256.         }
  257.         OSTimeDlyHMSM(0, 0, 0, 10);
  258.     }
  259. }
  260.  
  261. void task3(void* pdata) {
  262.  
  263.     while (1) {
  264.  
  265.         INT8U err;
  266.         int *num;
  267.         num = OSMboxPend(SWBox1, 0, &err);
  268.         if (*num == 0b0100) {
  269.             printf(num);
  270.             //printf("X");
  271.             //clear();
  272.  
  273.                                                                                                                                                                         IOWR(LEDS_BASE,0, 0b0000);
  274.             //IOWR(HEX_BASE, 0, THREE);
  275.             if (flag != 0) {
  276.                 alt_alarm_stop(&alarm1);
  277. //                  alt_alarm_stop(&alarmErrOff);
  278.                 alt_alarm_start(&alarm1, alt_ticks_per_second(), alarm_proc,
  279.                         *num);
  280.                 flag = 0;
  281.  
  282.  
  283.                 //IOWR(LAB8_0_BASE,0,0);
  284.                 char text1_1_row[40] = "task3";
  285.                 VGA_text (30, 15, text1_1_row);
  286.             }
  287.             STATE = 2;
  288.         }
  289.         OSTimeDlyHMSM(0, 0, 0, 10);
  290.     }
  291. }
  292.  
  293. void task4(void* pdata) {
  294.     while (1) {
  295.         INT8U err;
  296.         int *num;
  297.         num = OSMboxPend(SWBox1, 0, &err);
  298.         if (*num == 0b1000) {
  299.  
  300.             clear();
  301.             IOWR(HEX_BASE, 0, FOUR);
  302.             if (flag != 0) {
  303.                 alt_alarm_stop(&alarm1);
  304. //                  alt_alarm_stop(&alarmErrOff);
  305.                 alt_alarm_start(&alarm1, alt_ticks_per_second(), alarm_proc,
  306.                         *num);
  307.                                                                                                                                                                 char text1_1_row[40] = "task4";
  308.                                                                                                                                                                 VGA_text (30, 15, text1_1_row);
  309.                 flag = 0;
  310.                 IOWR(LAB8_0_BASE,0,0);
  311.  
  312.             }
  313.             STATE = 3;
  314.         }
  315.         OSTimeDlyHMSM(0, 0, 0, 10);
  316.     }
  317. }
  318.  
  319. void task5(void* pdata) {
  320.     while (1) {
  321.         INT8U err;
  322.         int *num;
  323.         num = OSMboxPend(SWBox1, 0, &err);
  324.         if (*num == 0b10000) {
  325.             if (flag != 0) {
  326.                 alt_alarm_stop(&alarm1);
  327. //                  alt_alarm_stop(&alarmErrOff);
  328.                 alt_alarm_start(&alarm1, alt_ticks_per_second(), alarm_proc,
  329.                         *num);
  330.                 flag = 0;
  331.  
  332.  
  333.                 IOWR(LAB8_0_BASE,0,0);
  334.                 char text1_1_row[40] = "task5";
  335.                 VGA_text (30, 15, text1_1_row);
  336.             }
  337.             clear();
  338.             IOWR(HEX_BASE, 0, FIVE);
  339.             STATE = 4;
  340.         }
  341.         OSTimeDlyHMSM(0, 0, 0, 10);
  342.     }
  343. }
  344.  
  345. void task6(void* pdata) {
  346.     while (1) {
  347.         INT8U err;
  348.         int *num;
  349.         num = OSMboxPend(SWBox1, 0, &err);
  350.  
  351.         if (*num == 0b100000) {
  352.             IOWR(HEX_BASE, 1, 0);
  353.             IOWR(HEX_BASE, 2, 0);
  354.             if (flag != 0) {
  355.                 alt_alarm_stop(&alarm1);
  356. //                  alt_alarm_stop(&alarmErrOff);
  357.                 alt_alarm_start(&alarm1, alt_ticks_per_second(), alarm_proc,
  358.                         *num);
  359.                 flag = 0;
  360.  
  361.  
  362.                 IOWR(LAB8_0_BASE,0,0);
  363.                 char text1_1_row[40] = "task6";
  364.                 VGA_text (30, 15, text1_1_row);
  365.             }
  366.             clear();
  367.             IOWR(HEX_BASE, 0, SIX);
  368.             STATE = 5;
  369.         }
  370.         OSTimeDlyHMSM(0, 0, 0, 10);
  371.     }
  372. }
  373.  
  374.  
  375. /* The main function creates two task and starts multi-tasking */
  376. int main(void) {
  377.     for (int i = 0; i < 6; i++) {
  378.         SWoff[i] = -1;
  379.         SWon[i] = -1;
  380.     }
  381.     if (alt_timestamp_start() < 0) {
  382.         printf("Brak timera w systemie\n");
  383.     }
  384.  
  385.     IOWR(HEX_BASE, 3, 0);
  386.     IOWR(HEX_BASE, 4, 0);
  387.     IOWR(HEX_BASE, 5, 0);
  388.  
  389.     //char text1_1_row[40] = "fn3";
  390.  
  391.     //VGA_text (30, 15, text1_1_row);
  392.  
  393.     SWBox1 = OSMboxCreate((void*) 0);
  394.     OSTaskCreateExt(task0,
  395.     NULL, (void *) &task0_stk[TASK_STACKSIZE - 1],
  396.     TASK0_PRIORITY,
  397.     TASK0_PRIORITY, task0_stk,
  398.     TASK_STACKSIZE,
  399.     NULL, 0);
  400.  
  401.     OSTaskCreateExt(task1,
  402.     NULL, (void *) &task1_stk[TASK_STACKSIZE - 1],
  403.     TASK1_PRIORITY,
  404.     TASK1_PRIORITY, task1_stk,
  405.     TASK_STACKSIZE,
  406.     NULL, 0);
  407.  
  408.     OSTaskCreateExt(task2,
  409.     NULL, (void *) &task2_stk[TASK_STACKSIZE - 1],
  410.     TASK2_PRIORITY,
  411.     TASK2_PRIORITY, task2_stk,
  412.     TASK_STACKSIZE,
  413.     NULL, 0);
  414.     OSTaskCreateExt(task3,
  415.     NULL, (void *) &task3_stk[TASK_STACKSIZE - 1],
  416.     TASK3_PRIORITY,
  417.     TASK3_PRIORITY, task3_stk,
  418.     TASK_STACKSIZE,
  419.     NULL, 0);
  420.     OSTaskCreateExt(task4,
  421.     NULL, (void *) &task4_stk[TASK_STACKSIZE - 1],
  422.     TASK4_PRIORITY,
  423.     TASK4_PRIORITY, task4_stk,
  424.     TASK_STACKSIZE,
  425.     NULL, 0);
  426.     OSTaskCreateExt(task5,
  427.     NULL, (void *) &task5_stk[TASK_STACKSIZE - 1],
  428.     TASK5_PRIORITY,
  429.     TASK5_PRIORITY, task5_stk,
  430.     TASK_STACKSIZE,
  431.     NULL, 0);
  432.     OSTaskCreateExt(task6,
  433.     NULL, (void *) &task6_stk[TASK_STACKSIZE - 1],
  434.     TASK6_PRIORITY,
  435.     TASK6_PRIORITY, task6_stk,
  436.     TASK_STACKSIZE,
  437.     NULL, 0);
  438.     OSStart();
  439.     return 0;
  440. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement