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 <stdio.h>
- #include "definition.h"
- #include <io.h>
- #include <ucos_ii.h>
- // 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 SEGB | SEGC | SEGF | SEGG
- #define FIVE SEGG | SEGC | SEGA | SEGD | SEGF
- #define SIX SEGA | SEGC | SEGD | SEGE | SEGF | SEGG
- #define SEVEN SEGB | SEGC | SEGA
- #define EIGHT SEGB | SEGC | SEGA | SEGD | SEGE | SEGF | SEGG
- #define NINE SEGB | SEGC | SEGA | SEGD | SEGF | SEGG
- #define E SEGA | SEGD | SEGE | SEGF | SEGG
- #define R SEGE | SEGG
- typedef enum POZIOM_TEMP{
- 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];
- OS_EVENT *SWBox1;
- /* Definition of Task Priorities */
- #define TASK1_PRIORITY 1
- #define TASK2_PRIORITY 2
- #define TASK3_PRIORITY 3
- #define TASK4_PRIORITY 4
- #define TASK5_PRIORITY 5
- static volatile int isON=0;
- static alt_alarm alarm;
- alt_u32 my_alarm_callback (void* context);
- /* Prints "Hello World" and sleeps for three seconds */
- void task1(void* pdata)
- {
- while (1)
- {
- printf("Hello from task1\n");
- int sw;
- int * msg;
- sw = IORD(SW_SLIDERS_BASE, 0);
- if(howManyLedsAreUp(sw) > 1) {
- IOWR(HEX_BASE, 0, R);
- IOWR(HEX_BASE, 1, R);
- IOWR(HEX_BASE, 2, E);
- OSTimeDlyHMSM(0, 0, 2, 0);
- IOWR(LEDS_BASE, 0, LED9);
- }
- if(howManyLedsAreUp(sw) == 0){
- IOWR(HEX_BASE, 0, ZERO);
- IOWR(HEX_BASE, 1, ZERO);
- IOWR(HEX_BASE, 2, ZERO);
- OSTimeDlyHMSM(0, 0, 1, 0);
- IOWR(LEDS_BASE, 0, 0);
- }
- if(sw & SW4 && howManyLedsAreUp(sw) == 1){
- IOWR(HEX_BASE, 0, ZERO);
- IOWR(HEX_BASE, 1, ZERO);
- IOWR(HEX_BASE, 2, NINE);
- OSTimeDlyHMSM(0, 0, 2, 0);
- IOWR(LEDS_BASE, 0, LED4);
- }
- printf("Slider = %d\n", sw);
- OSMboxPostOpt(SWBox1, &sw, OS_POST_OPT_BROADCAST);
- OSTimeDlyHMSM(0, 0, 0, 100);
- }
- }
- /* Prints "Hello World" and sleeps for three seconds */
- void task2(void* pdata)
- {
- while (1)
- {
- INT8U err;
- printf("Hello from task2\n");
- int *num;
- num = OSMboxPend(SWBox1, 0, &err);
- if(*num & SW0 && howManyLedsAreUp(*num) == 1){
- IOWR(HEX_BASE, 0, ZERO);
- IOWR(HEX_BASE, 1, FIVE);
- IOWR(HEX_BASE, 2, ONE);
- if(isON == 0){
- alt_alarm_start (&alarm, 2*alt_ticks_per_second(), my_alarm_callback, SW0);
- }
- }
- printf("T=%d\n", *num);
- OSTimeDlyHMSM(0, 0, 0, 100);
- }
- }
- void task3(void* pdata)
- {
- while (1)
- {
- INT8U err;
- printf("Hello from task2\n");
- int *num;
- num = OSMboxPend(SWBox1, 0, &err);
- if(*num & SW1 && howManyLedsAreUp(*num) == 1){
- IOWR(HEX_BASE, 0, ZERO);
- IOWR(HEX_BASE, 1, ZERO);
- IOWR(HEX_BASE, 2, TWO);
- OSTimeDlyHMSM(0, 0, 2, 0);
- IOWR(LEDS_BASE, 0, LED1);
- }
- printf("T=%d\n", *num);
- OSTimeDlyHMSM(0, 0, 0, 100);
- }
- }
- void task4(void* pdata)
- {
- while (1)
- {
- INT8U err;
- printf("Hello from task2\n");
- int *num;
- num = OSMboxPend(SWBox1, 0, &err);
- if(*num & SW2 && howManyLedsAreUp(*num) == 1){
- IOWR(HEX_BASE, 0, ZERO);
- IOWR(HEX_BASE, 1, FOUR);
- IOWR(HEX_BASE, 2, TWO);
- OSTimeDlyHMSM(0, 0, 2, 0);
- IOWR(LEDS_BASE, 0, LED2);
- }
- printf("T=%d\n", *num);
- OSTimeDlyHMSM(0, 0, 0, 100);
- }
- }
- void task5(void* pdata)
- {
- while (1)
- {
- INT8U err;
- printf("Hello from task2\n");
- int *num;
- num = OSMboxPend(SWBox1, 0, &err);
- if(*num & SW3 && howManyLedsAreUp(*num) == 1){
- IOWR(HEX_BASE, 0, ZERO);
- IOWR(HEX_BASE, 1, ZERO);
- IOWR(HEX_BASE, 2, THREE);
- OSTimeDlyHMSM(0, 0, 2, 0);
- IOWR(LEDS_BASE, 0, LED3);
- }
- printf("T=%d\n", *num);
- OSTimeDlyHMSM(0, 0, 0, 100);
- }
- }
- /* The main function creates two task and starts multi-tasking */
- int main(void)
- {
- printf("hello");
- 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);
- OSStart();
- return 0;
- }
- int howManyLedsAreUp(int swState)
- {
- int arr[6] = {SW0, SW1, SW2, SW3, SW4};
- int counter = 0;
- for (int i = 0; i < 5; ++i)
- {
- if (swState & arr[i])
- {
- counter++;
- }
- }
- return counter;
- }
- alt_u32 my_alarm_callback (void* context)
- {
- printf("alarm");
- if(isON == 0 && (int*) context == SW0){
- IOWR(LEDS_BASE,0,(int*)context);
- isON=1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement