Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************************************
- * Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
- * All rights reserved. All use of this software and documentation is *
- * subject to the License Agreement located at the end of this file below.*
- **************************************************************************
- * Description: *
- * The following is a simple hello world program running MicroC/OS-II.The *
- * purpose of the design is to be a very simple application that just *
- * demonstrates MicroC/OS-II running on NIOS II.The design doesn't account*
- * for issues such as checking system call return codes. etc. *
- * *
- * Requirements: *
- * -Supported Example Hardware Platforms *
- * Standard *
- * Full Featured *
- * Low Cost *
- * -Supported Development Boards *
- * Nios II Development Board, Stratix II Edition *
- * Nios Development Board, Stratix Professional Edition *
- * Nios Development Board, Stratix Edition *
- * Nios Development Board, Cyclone Edition *
- * -System Library Settings *
- * RTOS Type - MicroC/OS-II *
- * Periodic System Timer *
- * -Know Issues *
- * If this design is run on the ISS, terminal output will take several*
- * minutes per iteration. *
- **************************************************************************/
- #include <io.h>
- #include <system.h>
- #include <stdio.h>
- #include "includes.h"
- #include <ucos_ii.h>
- #include "altera_up_avalon_ps2.h"
- #include "altera_up_ps2_keyboard.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
- // pushbuttons
- #define KEY1 0x0000000E
- #define KEY2 0x00000004
- #define KEY3 0x00000008
- #define KEY4 0x00000010
- // leds
- #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
- // hex
- #define SEGA 0x00001
- #define SEGB 0x00002
- #define SEGC 0x00004
- #define SEGD 0x00008
- #define SEGE 0x00010
- #define SEGF 0x00020
- #define SEGG 0x00040
- // hex - numbers
- #define ZERO SEGA | SEGB | SEGC | SEGD |SEGE | SEGF
- #define ONE SEGB | SEGC
- #define TWO SEGA | SEGB | SEGG | SEGE | SEGD
- #define THREE SEGA | SEGB | SEGC | SEGD | SEGG
- #define FOUR SEGA | SEGC | SEGG | SEGF
- #define FIVE SEGA | SEGC | SEGD | SEGF | SEGG
- #define SIX SEGA | SEGC | SEGD | SEGE | SEGF | SEGG
- #define SEVEN SEGA | SEGB | SEGC
- #define EIGHT SEGA | SEGB | SEGC | SEGD |SEGE | SEGF | SEGG
- #define NINE SEGA | SEGB | SEGC | SEGD | SEGF
- #define E_HEX SEGA | SEGD |SEGE | SEGF | SEGG
- #define R_HEX SEGE | SEGG
- typedef enum TEMP_LEVEL {
- POZIOM_0 = 0,
- POZIOM_1 = 2,
- POZIOM_2 = 4,
- POZIOM_3 = 8,
- POZIOM_4 = 16,
- POZIOM_5 = 32,
- POZIOM_6 = 64,
- POZIOM_7 = 128,
- POZIOM_8 = 256,
- POZIOM_9 = 512,
- POZIOM_10 = 1024,
- } TEMP;
- /* 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];
- /* Definition of Task Priorities */
- #define TASK1_PRIORITY 1
- #define TASK2_PRIORITY 2
- #define TASK3_PRIORITY 3
- #define TASK4_PRIORITY 4
- #define TASK5_PRIORITY 5
- OS_EVENT *SWBox1;
- char read_key(alt_up_ps2_dev *ps2_kb)
- {
- KB_CODE_TYPE decode_mode;
- alt_u8 buffer;
- char ascii;
- char *inputStr;
- if (decode_scancode(ps2_kb, &decode_mode, &buffer, &ascii)==0) //If get a make code from keyboard
- {
- if (decode_mode == KB_ASCII_MAKE_CODE );
- {
- translate_make_code(decode_mode, buffer, inputStr); //translate the code like "SPCAE", "BACKSPACE"
- }
- }
- return ascii;
- }
- int key_sw(char ascii) {
- int result = 0;
- switch(ascii) {
- // 1
- case '1': {
- result |= SW0;
- break;
- }
- // 2
- case '2': {
- result |= SW1;
- break;
- }
- // 3
- case '3': {
- result |= SW2;
- break;
- }
- // 4
- case '4': {
- result |= SW3;
- break;
- }
- // 5
- case '5': {
- result |= SW4;
- break;
- }
- // 6
- case '6': {
- result |= SW5;
- break;
- }
- // 7
- case '7': {
- result |= SW6;
- break;
- }
- // 8
- case '8': {
- result |= SW7;
- break;
- }
- // 9
- case '9': {
- result |= SW8;
- break;
- }
- // 0
- case '0': {
- result |= SW9;
- break;
- }
- case '/': {
- result |= KEY1;
- break;
- }
- case 'd': {
- result = ~(0);
- break;
- }
- }
- return result;
- }
- int get_hex(int value) {
- switch (value) {
- case 0:
- return ZERO;
- break;
- case 1:
- return ONE;
- break;
- case 2:
- return TWO;
- break;
- case 3:
- return THREE;
- break;
- case 4:
- return FOUR;
- break;
- case 5:
- return FIVE;
- break;
- case 6:
- return SIX;
- break;
- case 7:
- return SEVEN;
- break;
- case 8:
- return EIGHT;
- break;
- case 9:
- return NINE;
- break;
- default:
- return SEGA;
- }
- }
- struct data {
- int room0;
- int room2;
- int room3;
- int room4;
- int led;
- int err;
- };
- void VGA_text(int x, int y, char * text_ptr) {
- int offset;
- /* Declare volatile pointer to char buffer (volatile means that IO load
- and store instructions will be used to access these pointer locations,
- instead of regular memory loads and stores) */
- volatile char * character_buffer = (char *) 0x00080000; // VGA character buffer
- /* assume that the text string fits on one line */
- offset = (y << 7) + x;
- while (*(text_ptr)) {
- *(character_buffer + offset) = *(text_ptr); // write to the character buffer
- ++text_ptr;
- ++offset;
- }
- }
- alt_up_ps2_dev *ps2;
- /* Prints "Hello World" and sleeps for three seconds */
- void task1(void* pdata) {
- int sw_state;
- int kierunek = 0;
- int flagDirection = 0;
- int hexs[6] = { 0, 0, 0, 0, 0, 0 };
- int rooms[5] = { 0, 0, 0, 0, 0 };
- int flag0;
- int errFlag = 0;
- int leds;
- struct data d;
- while (1) {
- //printf("%d\n",rooms[1]);
- d.room0 = rooms[0];
- d.room2 = rooms[2];
- d.room3 = rooms[3];
- d.room4 = rooms[4];
- d.led = leds;
- d.err = errFlag;
- OSMboxPostOpt(SWBox1, &d, OS_POST_OPT_BROADCAST); // wyslij wiadomosc do innych taskow
- /*
- sw_state = IORD(SW_SLIDERS_BASE, 0);
- leds = IORD(LEDS_BASE, 0);
- kierunek = IORD(PUSHBUTTON_BASE, 0);
- if (kierunek == KEY1) {
- if (flagDirection == 0) {
- leds |= LED9;
- flagDirection = 1;
- } else {
- leds &= ~(LED9);
- flagDirection = 0;
- }
- }
- */
- /*
- if(ascii != '/')//zmiana kierunku
- sw_state = key_sw(ascii);
- else {
- if(flagDirection == 1)
- flagDirection = 0;
- else
- flagDirection = 1;
- }
- if(flagDirection == 1)
- leds |= LED9;
- else
- leds &= ~(LED9);
- */
- char ascii = read_key(ps2);
- leds = IORD(LEDS_BASE, 0);
- sw_state = key_sw(ascii);
- if (sw_state == KEY1) {
- if (flagDirection == 0) {
- leds |= LED9;
- flagDirection = 1;
- } else {
- leds &= ~(LED9);
- flagDirection = 0;
- }
- }
- switch (sw_state) {
- case SW0:
- if (flag0 == 0) {
- if (flagDirection == 0) {
- rooms[0]++;
- errFlag = 0;
- } else {
- if (rooms[0] == 0) {
- errFlag = 1;
- }
- if (rooms[0] != 0) {
- rooms[0]--;
- errFlag = 0;
- }
- }
- flag0 = 1;
- }
- break;
- case SW1:
- if (flag0 == 0) {
- if (flagDirection == 1) {
- rooms[2]++;
- errFlag = 0;
- } else {
- if (rooms[2] == 0) {
- errFlag = 1;
- }
- if (rooms[2] != 0) {
- rooms[2]--;
- errFlag = 0;
- }
- }
- flag0 = 1;
- }
- break;
- case SW2:
- if (flag0 == 0) {
- if (flagDirection == 0) {
- if (rooms[0] == 0) {
- errFlag = 1;
- }
- if (rooms[0] != 0) {
- errFlag = 0;
- rooms[0]--;
- rooms[2]++;
- }
- } else {
- if (rooms[2] == 0) {
- errFlag = 1;
- }
- if (rooms[2] != 0) {
- rooms[0]++;
- rooms[2]--;
- errFlag = 0;
- }
- }
- flag0 = 1;
- }
- break;
- case SW3:
- if (flag0 == 0) {
- if (flagDirection == 0) {
- if (rooms[1] == 0) {
- errFlag = 1;
- }
- if (rooms[1] != 0) {
- errFlag = 0;
- rooms[1]--;
- rooms[3]++;
- }
- } else {
- if (rooms[3] == 0)
- {
- errFlag = 1;
- }
- if (rooms[3] != 0) {
- rooms[3]--;
- rooms[1]++;
- errFlag = 0;
- }
- }
- flag0 = 1;
- }
- break;
- case SW4:
- if (flag0 == 0) {
- if (flagDirection == 1) {
- rooms[3]++;
- errFlag = 0;
- } else {
- if (rooms[3] == 0)
- errFlag = 1;
- if (rooms[3] != 0) {
- rooms[3]--;
- errFlag = 0;
- }
- }
- flag0 = 1;
- }
- break;
- case SW5:
- if (flag0 == 0) {
- if (flagDirection == 0) {
- rooms[1]++;
- errFlag = 0;
- } else {
- if (rooms[1] == 0)
- errFlag = 1;
- if (rooms[1] != 0) {
- rooms[1]--;
- errFlag = 0;
- }
- }
- flag0 = 1;
- }
- break;
- case SW6:
- if (flag0 == 0) {
- if (flagDirection == 1) {
- rooms[4]++;
- errFlag = 0;
- } else {
- if (rooms[4] == 0)
- errFlag = 1;
- if (rooms[4] != 0) {
- rooms[4]--;
- errFlag = 0;
- }
- }
- flag0 = 1;
- }
- break;
- case SW7:
- if (flag0 == 0) {
- if (flagDirection == 0) {
- if (rooms[1] == 0)
- errFlag = 1;
- if (rooms[1] != 0) {
- rooms[1]--;
- rooms[4]++;
- errFlag = 0;
- }
- } else {
- if (rooms[4] == 0)
- errFlag = 1;
- if (rooms[4] != 0) {
- errFlag = 0;
- rooms[4]--;
- rooms[1]++;
- }
- }
- flag0 = 1;
- }
- break;
- case SW8:
- if (flag0 == 0) {
- if (flagDirection == 0) {
- if (rooms[0] == 0)
- errFlag = 1;
- if (rooms[0] != 0) {
- rooms[0]--;
- rooms[1]++;
- errFlag = 0;
- }
- } else {
- if (rooms[1] == 0)
- errFlag = 1;
- if (rooms[1] != 0) {
- rooms[1]--;
- rooms[0]++;
- errFlag = 0;
- }
- }
- flag0 = 1;
- }
- break;
- case SW9:
- if (flag0 == 0) {
- if (flagDirection == 0) {
- if (rooms[2] == 0)
- errFlag = 1;
- if (rooms[2] != 0) {
- rooms[2]--;
- rooms[3]++;
- errFlag = 0;
- }
- } else {
- if (rooms[3] == 0)
- errFlag = 1;
- if (rooms[3] != 0) {
- rooms[3]--;
- rooms[2]++;
- errFlag = 0;
- }
- }
- flag0 = 1;
- }
- break;
- default:
- flag0 = 0;
- break;
- }
- if (rooms[0] > 0) {
- leds |= LED0;
- } else {
- leds &= ~(LED0);
- }
- if (rooms[1] > 0) {
- leds |= LED1;
- } else {
- leds &= ~(LED1);
- }
- if (rooms[2] > 0) {
- leds |= LED2;
- } else {
- leds &= ~(LED2);
- }
- if (rooms[3] > 0) {
- leds |= LED3;
- } else {
- leds &= ~(LED3);
- }
- if (rooms[4] > 0) {
- leds |= LED4;
- } else {
- leds &= ~(LED4);
- }
- if (rooms[1] >= 0) {
- hexs[1] = get_hex(rooms[1]);
- }
- if (errFlag == 1) {
- IOWR(HEX_BASE, 3, R_HEX);
- IOWR(HEX_BASE, 4, R_HEX);
- IOWR(HEX_BASE, 5, E_HEX);
- char textError[33] = " error";
- VGA_text(33, 15, textError);
- leds |= LED8;
- } else {
- leds &= ~(LED8);
- IOWR(HEX_BASE, 1, hexs[1]);
- char text1[33];
- itoa(rooms[1], text1, 10);
- VGA_text(30, 16, text1);
- IOWR(HEX_BASE, 5, ZERO);
- char textError[33] = " ";
- VGA_text(33, 15, textError);
- }
- IOWR(LEDS_BASE, 0, leds);
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- /* Prints "Hello World" and sleeps for three seconds */
- void task2(void* pdata) {
- while (1) {
- INT8U err;
- struct data *temp;
- temp = OSMboxPend(SWBox1, 0, &err);
- int rooms = temp->room0;
- int errFlag = temp->err;
- //8
- char text1[33];
- itoa(rooms, text1, 10);
- VGA_text(30, 15, text1);
- if (errFlag == 1) {
- rooms = rooms + 16; //10000
- }
- IOWR(ROOMS_0_BASE, 0, rooms);
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- void task3(void* pdata) {
- while (1) {
- INT8U err;
- struct data *temp;
- temp = OSMboxPend(SWBox1, 0, &err);
- int rooms = temp->room2;
- int errFlag = temp->err;
- // printf("T=%d\n", leds);
- int hexs;
- if (rooms >= 0) {
- hexs = get_hex(rooms);
- }
- if (errFlag == 0) {
- IOWR(HEX_BASE, 2, hexs);
- char text1[33];
- itoa(rooms, text1, 10);
- VGA_text(30, 17, text1);
- }
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- void task4(void* pdata) {
- while (1) {
- INT8U err;
- struct data *temp;
- temp = OSMboxPend(SWBox1, 0, &err);
- int rooms = temp->room3;
- int errFlag = temp->err;
- // printf("T=%d\n", leds);
- int hexs;
- if (rooms >= 0) {
- hexs = get_hex(rooms);
- }
- if (errFlag == 0) {
- IOWR(HEX_BASE, 3, hexs);
- char text1[33];
- itoa(rooms, text1, 10);
- VGA_text(30, 18, text1);
- }
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- void task5(void* pdata) {
- while (1) {
- INT8U err;
- struct data *temp;
- temp = OSMboxPend(SWBox1, 0, &err);
- int rooms = temp->room4;
- int errFlag = temp->err;
- // printf("T=%d\n", leds);
- int hexs;
- if (rooms >= 0) {
- hexs = get_hex(rooms);
- }
- if (errFlag == 0) {
- IOWR(HEX_BASE, 4, hexs);
- char text1[33];
- itoa(rooms, text1, 10);
- VGA_text(30, 19, text1);
- }
- OSTimeDlyHMSM(0, 0, 0, 10);
- }
- }
- /* The main function creates two task and starts multi-tasking */
- int main(void) {
- SWBox1 = OSMboxCreate((void*) 0);
- 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);
- ps2 = alt_up_ps2_open_dev("/dev/ps2_0"); //open PS/2 device
- alt_printf("Detected device: %d\n",ps2);
- ps2->device_type = 1;
- alt_printf("Detected device: %d\n",ps2->device_type);
- alt_up_ps2_init(ps2);
- alt_up_ps2_clear_fifo(ps2);
- if (ps2->device_type != PS2_KEYBOARD) //Check if the device connected to PS/2 is keyboard // doesn't satisfy this condition.
- {
- printf("Error...\n");
- return -1;
- }
- OSStart();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement