Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "includes.h"
- #include <stdio.h>
- #include <system.h>
- #include <io.h>
- #define SW0 0x00000001
- #define SW1 0x00000002
- #define SW2 0x00000004
- #define SW3 0x00000008
- #define SW4 0x00000010
- #define SW5 0x00000020
- #define SW6 0x00000040
- #define SW7 0x00000080
- #define SW8 0x00000100
- #define SW9 0x00000200
- #define SW10 0x00000400
- #define SW11 0x00000800
- #define SW12 0x00001000
- #define SW13 0x00002000
- #define SW14 0x00004000
- #define SW15 0x00008000
- #define SW16 0x00010000
- #define SW17 0x00020000
- #define KEY1 0x00000002
- #define KEY2 0x00000004
- #define KEY3 0x00000008
- #define LED0 0x00000001
- #define LED1 0x00000002
- #define LED2 0x00000004
- #define LED3 0x00000008
- #define LED4 0x00000010
- #define LED5 0x00000020
- #define LED6 0x00000040
- #define LED7 0x00000080
- #define LED8 0x00000100
- #define LED9 0x00000200
- #define LED10 0x00000400
- #define LED11 0x00000800
- #define LED12 0x00001000
- #define LED13 0x00002000
- #define LED14 0x00004000
- #define LED15 0x00008000
- #define LED16 0x00010000
- #define LED17 0x00020000
- #define SEGA 0x01
- #define SEGB 0x02
- #define SEGC 0x04
- #define SEGD 0x08
- #define SEGE 16
- #define SEGF 32
- #define SEGG 64
- /* Definition of Task Stacks */
- #define TASK_STACKSIZE 2048
- OS_STK task1_stk[TASK_STACKSIZE];
- OS_STK task2_stk[TASK_STACKSIZE];
- OS_STK task3_stk[TASK_STACKSIZE];
- OS_STK task4_stk[TASK_STACKSIZE];
- OS_STK task5_stk[TASK_STACKSIZE];
- OS_STK task6_stk[TASK_STACKSIZE];
- /* Definition of Task Priorities */
- #define TASK1_PRIORITY 0x1
- #define TASK2_PRIORITY 0x2
- #define TASK3_PRIORITY 0x3
- #define TASK4_PRIORITY 0x4
- #define TASK5_PRIORITY 0x5
- #define TASK6_PRIORITY 0x6
- OS_EVENT *SWBox;
- typedef enum {
- IN = 1, OUT = -1
- } DIRECTION;
- typedef enum {
- ERROR_DIODE_UNDEFINED = -1, ERROR_DIODE_ERROR = 1, ERROR_DIODE_OK = 0
- } ERROR_DIODE_STATE;
- typedef struct {
- int numberOfPeople;
- } Room;
- typedef struct {
- Room* roomA;
- Room* roomB;
- } Passage;
- typedef enum {
- STATE_WAIT_FOR_SW = 1, STATE_WAIT_FOR_KEY = 2, STATE_READY = 3
- } STATE;
- volatile DIRECTION direction = IN;
- volatile STATE state = STATE_READY;
- volatile ERROR_DIODE_STATE errorDiodeState = ERROR_DIODE_OK;
- volatile ERROR_DIODE_STATE newErrorDiodeState;
- volatile Passage passages[5];
- volatile Room rooms[5];
- void init(Passage passages[], Room rooms[]) {
- for (int i = 0; i < 5; i++) {
- rooms[i].numberOfPeople = 0;
- }
- passages[3].roomA = rooms + 4;
- passages[3].roomB = NULL;
- passages[2].roomA = rooms + 1;
- passages[2].roomB = rooms + 4;
- passages[1].roomA = rooms + 0;
- passages[1].roomB = rooms + 1;
- passages[0].roomA = rooms + 2;
- passages[0].roomB = rooms + 0;
- passages[4].roomA = rooms + 3;
- passages[4].roomB = rooms + 2;
- }
- ERROR_DIODE_STATE moveThroughPassage(Passage* passage, DIRECTION direction) {
- ERROR_DIODE_STATE state;
- if (direction == IN) {
- if (passage->roomB == NULL) {
- passage->roomA->numberOfPeople += 1;
- state = ERROR_DIODE_OK;
- } else {
- if (passage->roomB->numberOfPeople > 0) {
- passage->roomA->numberOfPeople += 1;
- passage->roomB->numberOfPeople -= 1;
- state = ERROR_DIODE_OK;
- } else
- state = ERROR_DIODE_ERROR;
- }
- } else {
- if (passage->roomB == NULL) {
- if (passage->roomA->numberOfPeople > 0) {
- passage->roomA->numberOfPeople -= 1;
- state = ERROR_DIODE_OK;
- } else
- state = ERROR_DIODE_ERROR;
- } else {
- if (passage->roomA->numberOfPeople > 0) {
- passage->roomA->numberOfPeople -= 1;
- passage->roomB->numberOfPeople += 1;
- state = ERROR_DIODE_OK;
- } else
- state = ERROR_DIODE_ERROR;
- }
- }
- return state;
- }
- int getNumber(Room* room, int hex) {
- switch (room->numberOfPeople) {
- case 0:
- return (SEGA | SEGB | SEGC | SEGD | SEGE | SEGF) << (hex * 8);
- case 1:
- return (SEGB | SEGC) << (hex * 8);
- case 2:
- return (SEGA | SEGB | SEGG | SEGD | SEGE) << (hex * 8);
- case 3:
- return (SEGA | SEGD | SEGC | SEGB | SEGG) << (hex * 8);
- case 4:
- return (SEGB | SEGF | SEGG | SEGC) << (hex * 8);
- case 5:
- return (SEGA | SEGF | SEGG | SEGC | SEGD) << (hex * 8);
- case 6:
- return (SEGA | SEGC | SEGD | SEGE | SEGF | SEGG) << (hex * 8);
- case 7:
- return (SEGA | SEGB | SEGC) << (hex * 8);
- case 8:
- return (SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG) << (hex * 8);
- case 9:
- return (SEGA | SEGB | SEGC | SEGD | SEGF | SEGG) << (hex * 8);
- default:
- return (SEGA | SEGG | SEGD) << (hex * 8);
- }
- }
- /* Prints "Hello World" and sleeps for three seconds */
- void task1(void* pdata) {
- int SW, KEY;
- while (1) {
- SW = IORD(SW_SLIDERS_BASE, 0);
- KEY = IORD(PUSHBUTTONS_BASE, 0);
- if (state == STATE_READY) {
- if (KEY2 & KEY) {
- direction = direction == IN ? OUT : IN;
- printf("change dir\n");
- state = STATE_WAIT_FOR_KEY;
- }
- }
- if (state == STATE_WAIT_FOR_KEY) {
- if ((KEY2 & KEY) == 0) {
- state = STATE_READY;
- }
- }
- if (state == STATE_READY) {
- if (SW & SW3) {
- newErrorDiodeState = moveThroughPassage(passages + 3,
- direction);
- }
- if (newErrorDiodeState != ERROR_DIODE_UNDEFINED) {
- errorDiodeState = newErrorDiodeState;
- }
- }
- if (SW != 0) {
- state = STATE_WAIT_FOR_SW;
- OSMboxPostOpt(SWBox, &SW, OS_POST_OPT_BROADCAST);
- OSMboxPostOpt(SWBox, &SW, OS_POST_OPT_BROADCAST);
- OSMboxPostOpt(SWBox, &SW, OS_POST_OPT_BROADCAST);
- OSMboxPostOpt(SWBox, &SW, OS_POST_OPT_BROADCAST);
- } else {
- if (state == STATE_WAIT_FOR_SW)
- state = STATE_READY;
- }
- OSTimeDlyHMSM(0, 0, 0, 100);
- }
- }
- /* Prints "Hello World" and sleeps for three seconds */
- void task2(void* pdata) {
- INT8U err;
- printf("Hello from task2\n");
- int *SW;
- while (1) {
- if (state == STATE_READY) {
- SW = OSMboxPend(SWBox, 0, &err);
- if (*SW & SW2) {
- newErrorDiodeState = moveThroughPassage(passages + 2,
- direction);
- }
- if (newErrorDiodeState != ERROR_DIODE_UNDEFINED) {
- errorDiodeState = newErrorDiodeState;
- }
- }
- OSTimeDlyHMSM(0, 0, 1, 10);
- }
- }
- void task3(void* pdata) {
- INT8U err;
- printf("Hello from task2\n");
- int *SW;
- while (1) {
- if (state == STATE_READY) {
- SW = OSMboxPend(SWBox, 0, &err);
- if (*SW & SW1) {
- newErrorDiodeState = moveThroughPassage(passages + 1,
- direction);
- }
- if (newErrorDiodeState != ERROR_DIODE_UNDEFINED) {
- errorDiodeState = newErrorDiodeState;
- }
- }
- OSTimeDlyHMSM(0, 0, 0, 100);
- }
- }
- void task4(void* pdata) {
- INT8U err;
- printf("Hello from task2\n");
- int *SW;
- while (1) {
- if (state == STATE_READY) {
- SW = OSMboxPend(SWBox, 0, &err);
- if (*SW & SW0) {
- newErrorDiodeState = moveThroughPassage(passages + 0,
- direction);
- }
- if (newErrorDiodeState != ERROR_DIODE_UNDEFINED) {
- errorDiodeState = newErrorDiodeState;
- }
- }
- OSTimeDlyHMSM(0, 0, 0, 100);
- }
- }
- void task5(void* pdata) {
- INT8U err;
- printf("Hello from task2\n");
- int *SW;
- while (1) {
- if (state == STATE_READY) {
- SW = OSMboxPend(SWBox, 0, &err);
- if (*SW & SW4) {
- newErrorDiodeState = moveThroughPassage(passages + 4,
- direction);
- }
- if (newErrorDiodeState != ERROR_DIODE_UNDEFINED) {
- errorDiodeState = newErrorDiodeState;
- }
- }
- OSTimeDlyHMSM(0, 0, 0, 100);
- }
- }
- void task6(void* pdata) {
- int leds, numbers;
- printf("Hello from task6\n");
- while (1) {
- numbers = 0 | getNumber(rooms, 0) | getNumber(rooms + 1, 1)
- | getNumber(rooms + 2, 2) | getNumber(rooms + 3, 3);
- IOWR(HEX_3_BASE, 0, numbers);
- IOWR(HEX_7_BASE, 0, getNumber(rooms + 4, 0));
- leds = 0;
- for (int i = 0; i < 5; i++) {
- if (rooms[i].numberOfPeople > 0)
- leds |= 1 << i;
- }
- IOWR(LEDS_RED_BASE, 0, leds);
- IOWR(LEDS_GREEN_BASE, 0, errorDiodeState);
- OSTimeDlyHMSM(0, 0, 0, 100);
- }
- }
- /* The main function creates two task and starts multi-tasking */
- int main(void) {
- SWBox = OSMboxCreate((void*) 0);
- init(passages, rooms);
- OSTaskCreateExt(task1,
- NULL, (void *) &task1_stk[TASK_STACKSIZE - 1],
- TASK1_PRIORITY,
- TASK1_PRIORITY, task1_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task2,
- NULL, (void *) &task2_stk[TASK_STACKSIZE - 1],
- TASK2_PRIORITY,
- TASK2_PRIORITY, task2_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task3,
- NULL, (void *) &task3_stk[TASK_STACKSIZE - 1],
- TASK3_PRIORITY,
- TASK3_PRIORITY, task3_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task4,
- NULL, (void *) &task4_stk[TASK_STACKSIZE - 1],
- TASK4_PRIORITY,
- TASK4_PRIORITY, task4_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task5,
- NULL, (void *) &task5_stk[TASK_STACKSIZE - 1],
- TASK5_PRIORITY,
- TASK5_PRIORITY, task5_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSTaskCreateExt(task6,
- NULL, (void *) &task6_stk[TASK_STACKSIZE - 1],
- TASK6_PRIORITY,
- TASK6_PRIORITY, task6_stk,
- TASK_STACKSIZE,
- NULL, 0);
- OSStart();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement