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. *
- **************************************************************************/
- #ifndef DEFINITION_H_
- #define DEFINITION_H_
- // sw_sliders
- #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
- // pushbuttons
- #define KEY1 0x00000002
- #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
- #define LED10 0x00000400
- #define LED11 0x00000800
- #define LED12 0x00001000
- #define LED13 0x00002000
- #define LED14 0x00004000
- #define LED15 0x00008000
- #define LED16 0x00010000
- #define LED17 0x00020000
- // 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 NONE 0
- #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 SEGF | SEGG | SEGB | SEGC
- #define FIVE SEGA | SEGF | SEGG | SEGC | SEGD
- #define SIX SEGA | SEGF | SEGG | SEGE | SEGC | SEGD
- #define SEVEN SEGA | SEGB | SEGC
- #define EIGHT SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG
- #define E SEGA | SEGF | SEGG | SEGE | SEGD
- #define R SEGG | SEGE
- #endif /* DEFINITION_H_ */
- #include <stdio.h>
- #include "includes.h"
- #include <stdio.h>
- #include <io.h>
- #include <system.h>
- #include "alt_types.h"
- #include "altera_avalon_pio_regs.h"
- #include "sys/alt_irq.h"
- #include <unistd.h>
- #include <sys/alt_timestamp.h>
- #include "alt_types.h"
- #include <sys/alt_alarm.h>
- #include <stddef.h>
- #include <stdio.h>
- #include "sys/alt_alarm.h"
- #include "alt_types.h"
- #include <stdio.h>
- int getFirstOnePos(int val)
- {
- for (int i = 0; i < sizeof(val)*8; ++i)
- {
- if (val & 1)
- return i;
- val >>= 1;
- }
- return -1;
- }
- int getNumOfOnes(int val)
- {
- int num = 0;
- for (int i = 0; i < sizeof(val) * 8; ++i)
- {
- if (val & 1)
- ++num;
- val >>= 1;
- }
- return num;
- }
- int setBit(int source, int pin, int state)
- {
- if (state == 0)
- return source &= ~(1UL << getFirstOnePos(pin));
- return source |= 1UL << getFirstOnePos(pin);
- }
- int switchState(int source, int pin)
- {
- return source ^= 1UL << getFirstOnePos(pin);
- }
- int checkState(int source, int pin)
- {
- if ((source & (1 << getFirstOnePos(pin))) == 0)
- return 0;
- return 1;
- }
- int checkDuration(int startTime, int currentTime, int duration)
- {
- if ( ((currentTime - startTime) / (double)alt_ticks_per_second()) > duration)
- return 1;
- return 0;
- }
- void clearHexes()
- {
- for(int i=0; i < 6; ++i)
- IOWR(HEX_BASE, i, 0);
- }
- /* 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];
- OS_STK task7_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
- #define TASK6_PRIORITY 6
- #define TASK7_PRIORITY 7
- // read
- void task1(void* pdata)
- {
- while (1)
- {
- int sw;
- sw = IORD(SW_SLIDERS_BASE, 0);
- sw = setBit(sw, SW6, 0);
- sw = setBit(sw, SW7, 0);
- sw = setBit(sw, SW8, 0);
- sw = setBit(sw, SW9, 0);
- //printf("%d\n", sw);
- OSMboxPostOpt(SWBox1, &sw, OS_POST_OPT_BROADCAST);
- if (getNumOfOnes(sw) > 1)
- {
- clearHexes();
- IOWR(HEX_BASE, 2, E);
- IOWR(HEX_BASE, 1, R);
- IOWR(HEX_BASE, 0, R);
- IOWR(LEDS_BASE, 0, LED9);
- }
- INT8U err;
- if(sw == LED0)
- {
- clearHexes();
- IOWR(HEX_BASE, 1, TWO);
- IOWR(HEX_BASE, 0, FIVE);
- IOWR(LEDS_BASE, 0, sw);
- }
- else if (sw == 0)
- {
- clearHexes();
- IOWR(LEDS_BASE, 0, 0);
- }
- OSTimeDlyHMSM(0, 0, 1, 0);
- }
- }
- void task3(void* pdata)
- {
- while (1)
- {
- INT8U err;
- int* num;
- num = OSMboxPend(SWBox1, 0, &err);
- //printf("%d\n", *num);
- if(*num == LED1)
- {
- IOWR(HEX_BASE, 1, THREE);
- IOWR(HEX_BASE, 0, FIVE);
- IOWR(LEDS_BASE, 0, LED1);
- }
- else if (*num == 0)
- {
- clearHexes();
- IOWR(LEDS_BASE, 0, 0);
- }
- OSTimeDlyHMSM(0, 0, 1, 0);
- }
- }
- void task4(void* pdata)
- {
- while (1)
- {
- INT8U err;
- int* num;
- num = OSMboxPend(SWBox1, 0, &err);
- //printf("%d\n", *num);
- if(*num == LED2)
- {
- clearHexes();
- IOWR(HEX_BASE, 1, FOUR);
- IOWR(HEX_BASE, 0, FIVE);
- IOWR(LEDS_BASE, 0, LED2);
- }
- else if (*num == 0)
- {
- clearHexes();
- IOWR(LEDS_BASE, 0, 0);
- }
- OSTimeDlyHMSM(0, 0, 1, 0);
- }
- }
- void task5(void* pdata)
- {
- while (1)
- {
- INT8U err;
- int* num;
- num = OSMboxPend(SWBox1, 0, &err);
- //printf("%d\n", *num);
- if(*num == LED3)
- {
- clearHexes();
- IOWR(HEX_BASE, 1, FIVE);
- IOWR(HEX_BASE, 0, FIVE);
- IOWR(LEDS_BASE, 0, LED3);
- }
- else if (*num == 0)
- {
- clearHexes();
- IOWR(LEDS_BASE, 0, 0);
- }
- OSTimeDlyHMSM(0, 0, 1, 0);
- }
- }
- void task6(void* pdata)
- {
- while (1)
- {
- INT8U err;
- int* num;
- num = OSMboxPend(SWBox1, 0, &err);
- //printf("%d\n", *num);
- if(*num == LED4)
- {
- clearHexes();
- IOWR(HEX_BASE, 1, SEVEN);
- IOWR(HEX_BASE, 0, FIVE);
- IOWR(LEDS_BASE, 0, LED4);
- }
- else if (*num == 0)
- {
- clearHexes();
- IOWR(LEDS_BASE, 0, 0);
- }
- OSTimeDlyHMSM(0, 0, 1, 0);
- }
- }
- void task7(void* pdata)
- {
- while (1)
- {
- INT8U err;
- int* num;
- num = OSMboxPend(SWBox1, 0, &err);
- //printf("%d\n", *num);
- if(*num == LED5)
- {
- clearHexes();
- IOWR(HEX_BASE, 1, EIGHT);
- IOWR(HEX_BASE, 0, FIVE);
- IOWR(LEDS_BASE, 0, LED5);
- }
- else if (*num == 0)
- {
- clearHexes();
- IOWR(LEDS_BASE, 0, 0);
- }
- OSTimeDlyHMSM(0, 0, 1, 0);
- }
- }
- int main(void)
- {
- SWBox1 =OSMboxCreate((void*)0);
- printf("start\n");
- OSTaskCreateExt(task1,
- NULL,
- (void *)&task1_stk[TASK_STACKSIZE-1],
- TASK1_PRIORITY,
- TASK1_PRIORITY,
- task1_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);
- OSTaskCreateExt(task7,
- NULL,
- (void *)&task7_stk[TASK_STACKSIZE-1],
- TASK7_PRIORITY,
- TASK7_PRIORITY,
- task7_stk,
- TASK_STACKSIZE,
- NULL,
- 0);
- OSStart();
- return 0;
- }
- /******************************************************************************
- * *
- * License Agreement *
- * *
- * Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
- * All rights reserved. *
- * *
- * Permission is hereby granted, free of charge, to any person obtaining a *
- * copy of this software and associated documentation files (the "Software"), *
- * to deal in the Software without restriction, including without limitation *
- * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
- * and/or sell copies of the Software, and to permit persons to whom the *
- * Software is furnished to do so, subject to the following conditions: *
- * *
- * The above copyright notice and this permission notice shall be included in *
- * all copies or substantial portions of the Software. *
- * *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
- * DEALINGS IN THE SOFTWARE. *
- * *
- * This agreement shall be governed in all respects by the laws of the State *
- * of California and by the laws of the United States of America. *
- * Altera does not recommend, suggest or require that this reference design *
- * file be used in conjunction or combination with any other product. *
- ******************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement