Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "includes.h"
  3.  
  4. #include <stdio.h>
  5. #include <system.h>
  6. #include <io.h>
  7.  
  8. #define  SW0 0x00000001
  9. #define  SW1 0x00000002
  10. #define  SW2 0x00000004
  11. #define  SW3 0x00000008
  12. #define  SW4 0x00000010
  13. #define  SW5 0x00000020
  14. #define  SW6 0x00000040
  15. #define  SW7 0x00000080
  16. #define  SW8 0x00000100
  17. #define  SW9 0x00000200
  18. #define  SW10 0x00000400
  19. #define  SW11 0x00000800
  20. #define  SW12 0x00001000
  21. #define  SW13 0x00002000
  22. #define  SW14 0x00004000
  23. #define  SW15 0x00008000
  24. #define  SW16 0x00010000
  25. #define  SW17 0x00020000
  26. #define  KEY1 0x00000002
  27. #define  KEY2 0x00000004
  28. #define  KEY3 0x00000008
  29.  
  30. #define  LED0 0x00000001
  31. #define  LED1 0x00000002
  32. #define  LED2 0x00000004
  33. #define  LED3 0x00000008
  34. #define  LED4 0x00000010
  35. #define  LED5 0x00000020
  36. #define  LED6 0x00000040
  37. #define  LED7 0x00000080
  38. #define  LED8 0x00000100
  39. #define  LED9 0x00000200
  40. #define  LED10 0x00000400
  41. #define  LED11 0x00000800
  42. #define  LED12 0x00001000
  43. #define  LED13 0x00002000
  44. #define  LED14 0x00004000
  45. #define  LED15 0x00008000
  46. #define  LED16 0x00010000
  47. #define  LED17 0x00020000
  48. #define  SEGA 0x01
  49. #define  SEGB 0x02
  50. #define  SEGC 0x04
  51. #define  SEGD 0x08
  52. #define  SEGE 16
  53. #define  SEGF 32
  54. #define  SEGG 64
  55.  
  56. /* Definition of Task Stacks */
  57. #define   TASK_STACKSIZE       2048
  58. OS_STK task1_stk[TASK_STACKSIZE];
  59. OS_STK task2_stk[TASK_STACKSIZE];
  60. OS_STK task3_stk[TASK_STACKSIZE];
  61. OS_STK task4_stk[TASK_STACKSIZE];
  62. OS_STK task5_stk[TASK_STACKSIZE];
  63. OS_STK task6_stk[TASK_STACKSIZE];
  64.  
  65. /* Definition of Task Priorities */
  66.  
  67. #define TASK1_PRIORITY      0x1
  68. #define TASK2_PRIORITY      0x2
  69. #define TASK3_PRIORITY      0x3
  70. #define TASK4_PRIORITY      0x4
  71. #define TASK5_PRIORITY      0x5
  72. #define TASK6_PRIORITY      0x6
  73.  
  74. OS_EVENT *SWBox;
  75.  
  76. typedef enum {
  77.     IN = 1, OUT = -1
  78. } DIRECTION;
  79. typedef enum {
  80.     ERROR_DIODE_UNDEFINED = -1, ERROR_DIODE_ERROR = 1, ERROR_DIODE_OK = 0
  81. } ERROR_DIODE_STATE;
  82. typedef struct {
  83.     int numberOfPeople;
  84. } Room;
  85. typedef struct {
  86.     Room* roomA;
  87.     Room* roomB;
  88. } Passage;
  89.  
  90. typedef enum {
  91.     STATE_WAIT_FOR_SW = 1, STATE_WAIT_FOR_KEY = 2, STATE_READY = 3
  92. } STATE;
  93.  
  94. volatile DIRECTION direction = IN;
  95. volatile STATE state = STATE_READY;
  96. volatile ERROR_DIODE_STATE errorDiodeState = ERROR_DIODE_OK;
  97. volatile ERROR_DIODE_STATE newErrorDiodeState;
  98. volatile Passage passages[5];
  99. volatile Room rooms[5];
  100.  
  101. void init(Passage passages[], Room rooms[]) {
  102.     for (int i = 0; i < 5; i++) {
  103.         rooms[i].numberOfPeople = 0;
  104.     }
  105.  
  106.     passages[3].roomA = rooms + 4;
  107.     passages[3].roomB = NULL;
  108.  
  109.     passages[2].roomA = rooms + 1;
  110.     passages[2].roomB = rooms + 4;
  111.  
  112.     passages[1].roomA = rooms + 0;
  113.     passages[1].roomB = rooms + 1;
  114.  
  115.     passages[0].roomA = rooms + 2;
  116.     passages[0].roomB = rooms + 0;
  117.  
  118.     passages[4].roomA = rooms + 3;
  119.     passages[4].roomB = rooms + 2;
  120. }
  121.  
  122. ERROR_DIODE_STATE moveThroughPassage(Passage* passage, DIRECTION direction) {
  123.     ERROR_DIODE_STATE state;
  124.  
  125.     if (direction == IN) {
  126.         if (passage->roomB == NULL) {
  127.             passage->roomA->numberOfPeople += 1;
  128.             state = ERROR_DIODE_OK;
  129.         } else {
  130.             if (passage->roomB->numberOfPeople > 0) {
  131.                 passage->roomA->numberOfPeople += 1;
  132.                 passage->roomB->numberOfPeople -= 1;
  133.                 state = ERROR_DIODE_OK;
  134.             } else
  135.                 state = ERROR_DIODE_ERROR;
  136.         }
  137.     } else {
  138.         if (passage->roomB == NULL) {
  139.             if (passage->roomA->numberOfPeople > 0) {
  140.                 passage->roomA->numberOfPeople -= 1;
  141.                 state = ERROR_DIODE_OK;
  142.             } else
  143.                 state = ERROR_DIODE_ERROR;
  144.         } else {
  145.             if (passage->roomA->numberOfPeople > 0) {
  146.                 passage->roomA->numberOfPeople -= 1;
  147.                 passage->roomB->numberOfPeople += 1;
  148.                 state = ERROR_DIODE_OK;
  149.             } else
  150.                 state = ERROR_DIODE_ERROR;
  151.         }
  152.     }
  153.     return state;
  154. }
  155.  
  156. int getNumber(Room* room, int hex) {
  157.     switch (room->numberOfPeople) {
  158.     case 0:
  159.         return (SEGA | SEGB | SEGC | SEGD | SEGE | SEGF) << (hex * 8);
  160.     case 1:
  161.         return (SEGB | SEGC) << (hex * 8);
  162.     case 2:
  163.         return (SEGA | SEGB | SEGG | SEGD | SEGE) << (hex * 8);
  164.     case 3:
  165.         return (SEGA | SEGD | SEGC | SEGB | SEGG) << (hex * 8);
  166.     case 4:
  167.         return (SEGB | SEGF | SEGG | SEGC) << (hex * 8);
  168.     case 5:
  169.         return (SEGA | SEGF | SEGG | SEGC | SEGD) << (hex * 8);
  170.     case 6:
  171.         return (SEGA | SEGC | SEGD | SEGE | SEGF | SEGG) << (hex * 8);
  172.     case 7:
  173.         return (SEGA | SEGB | SEGC) << (hex * 8);
  174.     case 8:
  175.         return (SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG) << (hex * 8);
  176.     case 9:
  177.         return (SEGA | SEGB | SEGC | SEGD | SEGF | SEGG) << (hex * 8);
  178.     default:
  179.         return (SEGA | SEGG | SEGD) << (hex * 8);
  180.     }
  181. }
  182.  
  183. /* Prints "Hello World" and sleeps for three seconds */
  184. void task1(void* pdata) {
  185.     int SW, KEY;
  186.     while (1) {
  187.         SW = IORD(SW_SLIDERS_BASE, 0);
  188.         KEY = IORD(PUSHBUTTONS_BASE, 0);
  189.  
  190.         if (state == STATE_READY) {
  191.             if (KEY2 & KEY) {
  192.                 direction = direction == IN ? OUT : IN;
  193.                 printf("change dir\n");
  194.                 state = STATE_WAIT_FOR_KEY;
  195.             }
  196.         }
  197.  
  198.         if (state == STATE_WAIT_FOR_KEY) {
  199.             if ((KEY2 & KEY) == 0) {
  200.                 state = STATE_READY;
  201.             }
  202.         }
  203.  
  204.         if (state == STATE_READY) {
  205.             if (SW & SW3) {
  206.                 newErrorDiodeState = moveThroughPassage(passages + 3,
  207.                         direction);
  208.             }
  209.             if (newErrorDiodeState != ERROR_DIODE_UNDEFINED) {
  210.                 errorDiodeState = newErrorDiodeState;
  211.             }
  212.         }
  213.         if (SW != 0) {
  214.             state = STATE_WAIT_FOR_SW;
  215.             OSMboxPostOpt(SWBox, &SW, OS_POST_OPT_BROADCAST);
  216.             OSMboxPostOpt(SWBox, &SW, OS_POST_OPT_BROADCAST);
  217.             OSMboxPostOpt(SWBox, &SW, OS_POST_OPT_BROADCAST);
  218.             OSMboxPostOpt(SWBox, &SW, OS_POST_OPT_BROADCAST);
  219.         } else {
  220.             if (state == STATE_WAIT_FOR_SW)
  221.                 state = STATE_READY;
  222.         }
  223.  
  224.         OSTimeDlyHMSM(0, 0, 0, 100);
  225.     }
  226. }
  227. /* Prints "Hello World" and sleeps for three seconds */
  228. void task2(void* pdata) {
  229.     INT8U err;
  230.     printf("Hello from task2\n");
  231.     int *SW;
  232.  
  233.     while (1) {
  234.         if (state == STATE_READY) {
  235.             SW = OSMboxPend(SWBox, 0, &err);
  236.             if (*SW & SW2) {
  237.                 newErrorDiodeState = moveThroughPassage(passages + 2,
  238.                         direction);
  239.             }
  240.             if (newErrorDiodeState != ERROR_DIODE_UNDEFINED) {
  241.                 errorDiodeState = newErrorDiodeState;
  242.             }
  243.         }
  244.         OSTimeDlyHMSM(0, 0, 1, 10);
  245.     }
  246. }
  247. void task3(void* pdata) {
  248.     INT8U err;
  249.     printf("Hello from task2\n");
  250.     int *SW;
  251.  
  252.     while (1) {
  253.         if (state == STATE_READY) {
  254.             SW = OSMboxPend(SWBox, 0, &err);
  255.             if (*SW & SW1) {
  256.                 newErrorDiodeState = moveThroughPassage(passages + 1,
  257.                         direction);
  258.             }
  259.             if (newErrorDiodeState != ERROR_DIODE_UNDEFINED) {
  260.                 errorDiodeState = newErrorDiodeState;
  261.             }
  262.         }
  263.         OSTimeDlyHMSM(0, 0, 0, 100);
  264.     }
  265. }
  266. void task4(void* pdata) {
  267.     INT8U err;
  268.     printf("Hello from task2\n");
  269.     int *SW;
  270.  
  271.     while (1) {
  272.         if (state == STATE_READY) {
  273.             SW = OSMboxPend(SWBox, 0, &err);
  274.             if (*SW & SW0) {
  275.                 newErrorDiodeState = moveThroughPassage(passages + 0,
  276.                         direction);
  277.             }
  278.             if (newErrorDiodeState != ERROR_DIODE_UNDEFINED) {
  279.                 errorDiodeState = newErrorDiodeState;
  280.             }
  281.         }
  282.         OSTimeDlyHMSM(0, 0, 0, 100);
  283.     }
  284. }
  285. void task5(void* pdata) {
  286.     INT8U err;
  287.     printf("Hello from task2\n");
  288.     int *SW;
  289.  
  290.     while (1) {
  291.         if (state == STATE_READY) {
  292.             SW = OSMboxPend(SWBox, 0, &err);
  293.             if (*SW & SW4) {
  294.                 newErrorDiodeState = moveThroughPassage(passages + 4,
  295.                         direction);
  296.             }
  297.             if (newErrorDiodeState != ERROR_DIODE_UNDEFINED) {
  298.                 errorDiodeState = newErrorDiodeState;
  299.             }
  300.         }
  301.         OSTimeDlyHMSM(0, 0, 0, 100);
  302.     }
  303. }
  304. void task6(void* pdata) {
  305.     int leds, numbers;
  306.     printf("Hello from task6\n");
  307.     while (1) {
  308.         numbers = 0 | getNumber(rooms, 0) | getNumber(rooms + 1, 1)
  309.                 | getNumber(rooms + 2, 2) | getNumber(rooms + 3, 3);
  310.         IOWR(HEX_3_BASE, 0, numbers);
  311.         IOWR(HEX_7_BASE, 0, getNumber(rooms + 4, 0));
  312.  
  313.         leds = 0;
  314.         for (int i = 0; i < 5; i++) {
  315.             if (rooms[i].numberOfPeople > 0)
  316.                 leds |= 1 << i;
  317.         }
  318.         IOWR(LEDS_RED_BASE, 0, leds);
  319.         IOWR(LEDS_GREEN_BASE, 0, errorDiodeState);
  320.  
  321.         OSTimeDlyHMSM(0, 0, 0, 100);
  322.     }
  323. }
  324. /* The main function creates two task and starts multi-tasking */
  325. int main(void) {
  326.  
  327.     SWBox = OSMboxCreate((void*) 0);
  328.  
  329.     init(passages, rooms);
  330.  
  331.     OSTaskCreateExt(task1,
  332.     NULL, (void *) &task1_stk[TASK_STACKSIZE - 1],
  333.     TASK1_PRIORITY,
  334.     TASK1_PRIORITY, task1_stk,
  335.     TASK_STACKSIZE,
  336.     NULL, 0);
  337.     OSTaskCreateExt(task2,
  338.     NULL, (void *) &task2_stk[TASK_STACKSIZE - 1],
  339.     TASK2_PRIORITY,
  340.     TASK2_PRIORITY, task2_stk,
  341.     TASK_STACKSIZE,
  342.     NULL, 0);
  343.     OSTaskCreateExt(task3,
  344.     NULL, (void *) &task3_stk[TASK_STACKSIZE - 1],
  345.     TASK3_PRIORITY,
  346.     TASK3_PRIORITY, task3_stk,
  347.     TASK_STACKSIZE,
  348.     NULL, 0);
  349.     OSTaskCreateExt(task4,
  350.     NULL, (void *) &task4_stk[TASK_STACKSIZE - 1],
  351.     TASK4_PRIORITY,
  352.     TASK4_PRIORITY, task4_stk,
  353.     TASK_STACKSIZE,
  354.     NULL, 0);
  355.     OSTaskCreateExt(task5,
  356.     NULL, (void *) &task5_stk[TASK_STACKSIZE - 1],
  357.     TASK5_PRIORITY,
  358.     TASK5_PRIORITY, task5_stk,
  359.     TASK_STACKSIZE,
  360.     NULL, 0);
  361.     OSTaskCreateExt(task6,
  362.     NULL, (void *) &task6_stk[TASK_STACKSIZE - 1],
  363.     TASK6_PRIORITY,
  364.     TASK6_PRIORITY, task6_stk,
  365.     TASK_STACKSIZE,
  366.     NULL, 0);
  367.  
  368.     OSStart();
  369.     return 0;
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement