SHARE
TWEET

Untitled

a guest Nov 12th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*************************************************************************
  2. * Copyright (c) 2004 Altera Corporation, San Jose, California, USA.      *
  3. * All rights reserved. All use of this software and documentation is     *
  4. * subject to the License Agreement located at the end of this file below.*
  5. **************************************************************************
  6. * Description:                                                           *
  7. * The following is a simple hello world program running MicroC/OS-II.The *
  8. * purpose of the design is to be a very simple application that just     *
  9. * demonstrates MicroC/OS-II running on NIOS II.The design doesn't account*
  10. * for issues such as checking system call return codes. etc.             *
  11. *                                                                        *
  12. * Requirements:                                                          *
  13. *   -Supported Example Hardware Platforms                                *
  14. *     Standard                                                           *
  15. *     Full Featured                                                      *
  16. *     Low Cost                                                           *
  17. *   -Supported Development Boards                                        *
  18. *     Nios II Development Board, Stratix II Edition                      *
  19. *     Nios Development Board, Stratix Professional Edition               *
  20. *     Nios Development Board, Stratix Edition                            *
  21. *     Nios Development Board, Cyclone Edition                            *
  22. *   -System Library Settings                                             *
  23. *     RTOS Type - MicroC/OS-II                                           *
  24. *     Periodic System Timer                                              *
  25. *   -Know Issues                                                         *
  26. *     If this design is run on the ISS, terminal output will take several*
  27. *     minutes per iteration.                                             *
  28. **************************************************************************/
  29.  
  30.  
  31. #include <stdio.h>
  32. #include "definition.h"
  33. #include <io.h>
  34. #include <ucos_ii.h>
  35.  
  36. //     hex - numbers
  37. #define ZERO SEGA | SEGB | SEGC | SEGD |SEGE | SEGF
  38. #define ONE  SEGB | SEGC
  39. #define TWO  SEGA | SEGB | SEGG | SEGE | SEGD
  40. #define THREE SEGA | SEGB | SEGC | SEGD | SEGG
  41. #define FOUR  SEGB | SEGC | SEGF | SEGG
  42. #define FIVE  SEGG | SEGC | SEGA | SEGD | SEGF
  43. #define SIX  SEGA | SEGC | SEGD | SEGE | SEGF | SEGG
  44. #define SEVEN  SEGB | SEGC | SEGA
  45. #define EIGHT  SEGB | SEGC | SEGA | SEGD | SEGE | SEGF | SEGG
  46. #define NINE SEGB | SEGC | SEGA | SEGD | SEGF | SEGG
  47. #define E SEGA | SEGD | SEGE | SEGF | SEGG
  48. #define R SEGE | SEGG
  49.  
  50. typedef enum POZIOM_TEMP{
  51. POZIOM_0 = 0,
  52. POZIOM_1 = 2,
  53. POZIOM_2 = 4,
  54. POZIOM_3 = 8,
  55. POZIOM_4 = 16,
  56. POZIOM_5 = 32,
  57. POZIOM_6 = 64,
  58. POZIOM_7 = 128,
  59. POZIOM_8 = 256,
  60. POZIOM_9 = 512,
  61. POZIOM_10 = 1024,
  62.  
  63. }TEMP;
  64.  
  65. /* Definition of Task Stacks */\
  66. #define TASK_STACKSIZE 2048
  67. OS_STK task1_stk[TASK_STACKSIZE];
  68. OS_STK task2_stk[TASK_STACKSIZE];
  69. OS_STK task3_stk[TASK_STACKSIZE];
  70. OS_STK task4_stk[TASK_STACKSIZE];
  71. OS_STK task5_stk[TASK_STACKSIZE];
  72.  
  73.  
  74. OS_EVENT *SWBox1;
  75.  
  76.  
  77. /* Definition of Task Priorities */
  78.  
  79. #define TASK1_PRIORITY 1
  80. #define TASK2_PRIORITY 2
  81. #define TASK3_PRIORITY 3
  82. #define TASK4_PRIORITY 4
  83. #define TASK5_PRIORITY 5
  84.  
  85. static volatile int isON=0;
  86.  
  87.  
  88. static alt_alarm alarm;
  89.  
  90. alt_u32 my_alarm_callback (void* context);
  91.  
  92.  
  93. /* Prints "Hello World" and sleeps for three seconds */
  94. void task1(void* pdata)
  95. {
  96.   while (1)
  97.   {
  98.       printf("Hello from task1\n");
  99.       int sw;
  100.       int * msg;
  101.       sw = IORD(SW_SLIDERS_BASE, 0);
  102.  
  103.       if(howManyLedsAreUp(sw) > 1) {
  104.           IOWR(HEX_BASE, 0, R);
  105.           IOWR(HEX_BASE, 1, R);
  106.           IOWR(HEX_BASE, 2, E);
  107.           OSTimeDlyHMSM(0, 0, 2, 0);
  108.           IOWR(LEDS_BASE, 0, LED9);
  109.       }
  110.  
  111.       if(howManyLedsAreUp(sw) == 0){
  112.           IOWR(HEX_BASE, 0, ZERO);
  113.           IOWR(HEX_BASE, 1, ZERO);
  114.           IOWR(HEX_BASE, 2, ZERO);
  115.           OSTimeDlyHMSM(0, 0, 1, 0);
  116.           IOWR(LEDS_BASE, 0, 0);
  117.       }
  118.  
  119.       if(sw & SW4 && howManyLedsAreUp(sw) == 1){
  120.           IOWR(HEX_BASE, 0, ZERO);
  121.           IOWR(HEX_BASE, 1, ZERO);
  122.           IOWR(HEX_BASE, 2, NINE);
  123.           OSTimeDlyHMSM(0, 0, 2, 0);
  124.           IOWR(LEDS_BASE, 0, LED4);
  125.       }
  126.  
  127.       printf("Slider = %d\n", sw);
  128.       OSMboxPostOpt(SWBox1, &sw, OS_POST_OPT_BROADCAST);
  129.  
  130.       OSTimeDlyHMSM(0, 0, 0, 100);
  131.   }
  132. }
  133. /* Prints "Hello World" and sleeps for three seconds */
  134. void task2(void* pdata)
  135. {
  136.   while (1)
  137.   {
  138.       INT8U err;
  139.       printf("Hello from task2\n");
  140.       int *num;
  141.       num = OSMboxPend(SWBox1, 0, &err);
  142.       if(*num & SW0 && howManyLedsAreUp(*num) == 1){
  143.           IOWR(HEX_BASE, 0, ZERO);
  144.           IOWR(HEX_BASE, 1, FIVE);
  145.           IOWR(HEX_BASE, 2, ONE);
  146.           if(isON == 0){
  147.               alt_alarm_start (&alarm, 2*alt_ticks_per_second(), my_alarm_callback, SW0);
  148.           }
  149.       }
  150.  
  151.  
  152.       printf("T=%d\n", *num);
  153.       OSTimeDlyHMSM(0, 0, 0, 100);
  154.   }
  155. }
  156.  
  157. void task3(void* pdata)
  158. {
  159.   while (1)
  160.   {
  161.       INT8U err;
  162.       printf("Hello from task2\n");
  163.       int *num;
  164.       num = OSMboxPend(SWBox1, 0, &err);
  165.       if(*num & SW1 && howManyLedsAreUp(*num) == 1){
  166.           IOWR(HEX_BASE, 0, ZERO);
  167.           IOWR(HEX_BASE, 1, ZERO);
  168.           IOWR(HEX_BASE, 2, TWO);
  169.           OSTimeDlyHMSM(0, 0, 2, 0);
  170.           IOWR(LEDS_BASE, 0, LED1);
  171.       }
  172.  
  173.  
  174.       printf("T=%d\n", *num);
  175.  
  176.       OSTimeDlyHMSM(0, 0, 0, 100);
  177.   }
  178. }
  179.  
  180. void task4(void* pdata)
  181. {
  182.   while (1)
  183.   {
  184.       INT8U err;
  185.       printf("Hello from task2\n");
  186.       int *num;
  187.       num = OSMboxPend(SWBox1, 0, &err);
  188.       if(*num & SW2 && howManyLedsAreUp(*num) == 1){
  189.           IOWR(HEX_BASE, 0, ZERO);
  190.           IOWR(HEX_BASE, 1, FOUR);
  191.           IOWR(HEX_BASE, 2, TWO);
  192.           OSTimeDlyHMSM(0, 0, 2, 0);
  193.           IOWR(LEDS_BASE, 0, LED2);
  194.       }
  195.  
  196.  
  197.       printf("T=%d\n", *num);
  198.  
  199.       OSTimeDlyHMSM(0, 0, 0, 100);
  200.   }
  201. }
  202.  
  203. void task5(void* pdata)
  204. {
  205.   while (1)
  206.   {
  207.       INT8U err;
  208.       printf("Hello from task2\n");
  209.       int *num;
  210.       num = OSMboxPend(SWBox1, 0, &err);
  211.       if(*num & SW3 && howManyLedsAreUp(*num) == 1){
  212.           IOWR(HEX_BASE, 0, ZERO);
  213.           IOWR(HEX_BASE, 1, ZERO);
  214.           IOWR(HEX_BASE, 2, THREE);
  215.           OSTimeDlyHMSM(0, 0, 2, 0);
  216.           IOWR(LEDS_BASE, 0, LED3);
  217.       }
  218.  
  219.  
  220.       printf("T=%d\n", *num);
  221.  
  222.       OSTimeDlyHMSM(0, 0, 0, 100);
  223.   }
  224. }
  225.  
  226.  
  227. /* The main function creates two task and starts multi-tasking */
  228. int main(void)
  229. {
  230.     printf("hello");
  231.     SWBox1 =OSMboxCreate((void*)0);
  232.  
  233.  
  234.     OSTaskCreateExt(task1,
  235.                   NULL,
  236.                   (void *)&task1_stk[TASK_STACKSIZE-1],
  237.                   TASK1_PRIORITY,
  238.                   TASK1_PRIORITY,
  239.                   task1_stk,
  240.                   TASK_STACKSIZE,
  241.                   NULL,
  242.                   0);
  243.  
  244.  
  245.     OSTaskCreateExt(task2,
  246.                   NULL,
  247.                   (void *)&task2_stk[TASK_STACKSIZE-1],
  248.                   TASK2_PRIORITY,
  249.                   TASK2_PRIORITY,
  250.                   task2_stk,
  251.                   TASK_STACKSIZE,
  252.                   NULL,
  253.                   0);
  254.     OSTaskCreateExt(task3,
  255.                       NULL,
  256.                       (void *)&task3_stk[TASK_STACKSIZE-1],
  257.                       TASK3_PRIORITY,
  258.                       TASK3_PRIORITY,
  259.                       task3_stk,
  260.                       TASK_STACKSIZE,
  261.                       NULL,
  262.                       0);
  263.  
  264.     OSTaskCreateExt(task4,
  265.                       NULL,
  266.                       (void *)&task4_stk[TASK_STACKSIZE-1],
  267.                       TASK4_PRIORITY,
  268.                       TASK4_PRIORITY,
  269.                       task4_stk,
  270.                       TASK_STACKSIZE,
  271.                       NULL,
  272.                       0);
  273.  
  274.     OSTaskCreateExt(task5,
  275.                       NULL,
  276.                       (void *)&task5_stk[TASK_STACKSIZE-1],
  277.                       TASK5_PRIORITY,
  278.                       TASK5_PRIORITY,
  279.                       task5_stk,
  280.                       TASK_STACKSIZE,
  281.                       NULL,
  282.                       0);
  283.  
  284.  
  285.  
  286.  
  287.     OSStart();
  288.     return 0;
  289. }
  290.  
  291. int howManyLedsAreUp(int swState)
  292. {
  293.     int arr[6] = {SW0, SW1, SW2, SW3, SW4};
  294.     int counter = 0;
  295.     for (int i = 0; i < 5; ++i)
  296.     {
  297.         if (swState & arr[i])
  298.         {
  299.             counter++;
  300.         }
  301.     }
  302.     return counter;
  303. }
  304.  
  305.  
  306. alt_u32 my_alarm_callback (void* context)
  307. {
  308.     printf("alarm");
  309.     if(isON == 0 && (int*) context == SW0){
  310.         IOWR(LEDS_BASE,0,(int*)context);
  311.         isON=1;
  312.     }
  313.     return 0;
  314. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top