Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.68 KB | None | 0 0
  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. #ifndef DEFINITION_H_
  31. #define DEFINITION_H_
  32.  
  33. //        sw_sliders
  34. #define  SW0 0x00000001
  35. #define  SW1 0x00000002
  36. #define  SW2 0x00000004
  37. #define  SW3 0x00000008
  38. #define  SW4 0x00000010
  39. #define  SW5 0x00000020
  40. #define  SW6 0x00000040
  41. #define  SW7 0x00000080
  42. #define  SW8 0x00000100
  43. #define  SW9 0x00000200
  44. #define  SW10 0x00000400
  45. #define  SW11 0x00000800
  46. #define  SW12 0x00001000
  47. #define  SW13 0x00002000
  48. #define  SW14 0x00004000
  49. #define  SW15 0x00008000
  50. #define  SW16 0x00010000
  51. #define  SW17 0x00020000
  52.  
  53. //      pushbuttons
  54. #define  KEY1 0x00000002
  55. #define  KEY2 0x00000004
  56. #define  KEY3 0x00000008
  57. #define  KEY4 0x00000010
  58.  
  59. //      leds
  60. #define  LED0 0x00000001
  61. #define  LED1 0x00000002
  62. #define  LED2 0x00000004
  63. #define  LED3 0x00000008
  64. #define  LED4 0x00000010
  65. #define  LED5 0x00000020
  66. #define  LED6 0x00000040
  67. #define  LED7 0x00000080
  68. #define  LED8 0x00000100
  69. #define  LED9 0x00000200
  70. #define  LED10 0x00000400
  71. #define  LED11 0x00000800
  72. #define  LED12 0x00001000
  73. #define  LED13 0x00002000
  74. #define  LED14 0x00004000
  75. #define  LED15 0x00008000
  76. #define  LED16 0x00010000
  77. #define  LED17 0x00020000
  78.  
  79. //      hex
  80. #define  SEGA 0x00001
  81. #define  SEGB 0x00002
  82. #define  SEGC 0x00004
  83. #define  SEGD 0x00008
  84. #define  SEGE 0x00010
  85. #define  SEGF 0x00020
  86. #define  SEGG 0x00040
  87.  
  88. //     hex - numbers
  89. #define NONE 0
  90. #define ZERO SEGA | SEGB | SEGC | SEGD |SEGE | SEGF
  91. #define ONE  SEGB | SEGC
  92. #define TWO  SEGA | SEGB | SEGG | SEGE | SEGD
  93. #define THREE SEGA | SEGB | SEGC | SEGD | SEGG
  94. #define FOUR SEGF | SEGG | SEGB | SEGC
  95. #define FIVE SEGA | SEGF | SEGG | SEGC | SEGD
  96. #define SIX SEGA | SEGF | SEGG | SEGE | SEGC | SEGD
  97. #define SEVEN SEGA | SEGB | SEGC
  98. #define EIGHT SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG
  99. #define E SEGA | SEGF | SEGG | SEGE | SEGD
  100. #define R SEGG | SEGE
  101.  
  102. #endif /* DEFINITION_H_ */
  103.  
  104.  
  105. #include <stdio.h>
  106. #include "includes.h"
  107. #include <stdio.h>
  108. #include <io.h>
  109. #include <system.h>
  110. #include "alt_types.h"
  111. #include "altera_avalon_pio_regs.h"
  112. #include "sys/alt_irq.h"
  113. #include <unistd.h>
  114. #include <sys/alt_timestamp.h>
  115. #include "alt_types.h"
  116. #include <sys/alt_alarm.h>
  117. #include <stddef.h>
  118. #include <stdio.h>
  119. #include "sys/alt_alarm.h"
  120. #include "alt_types.h"
  121. #include <stdio.h>
  122.  
  123.  
  124. int getFirstOnePos(int val)
  125. {
  126.     for (int i = 0; i < sizeof(val)*8; ++i)
  127.     {
  128.         if (val & 1)
  129.             return i;
  130.         val >>= 1;
  131.     }
  132.     return -1;
  133. }
  134.  
  135. int getNumOfOnes(int val)
  136. {
  137.     int num = 0;
  138.     for (int i = 0; i < sizeof(val) * 8; ++i)
  139.     {
  140.         if (val & 1)
  141.             ++num;
  142.         val >>= 1;
  143.     }
  144.     return num;
  145. }
  146.  
  147. int setBit(int source, int pin, int state)
  148. {
  149.     if (state == 0)
  150.         return source &= ~(1UL << getFirstOnePos(pin));
  151.     return source |= 1UL << getFirstOnePos(pin);
  152. }
  153.  
  154. int switchState(int source, int pin)
  155. {
  156.     return source ^= 1UL << getFirstOnePos(pin);
  157. }
  158.  
  159. int checkState(int source, int pin)
  160. {
  161.     if ((source & (1 << getFirstOnePos(pin))) == 0)
  162.         return 0;
  163.     return 1;
  164. }
  165.  
  166. int checkDuration(int startTime, int currentTime, int duration)
  167. {
  168.     if ( ((currentTime - startTime) / (double)alt_ticks_per_second()) > duration)
  169.         return 1;
  170.     return 0;
  171. }
  172.  
  173.  
  174. void clearHexes()
  175. {
  176.     for(int i=0; i < 6; ++i)
  177.             IOWR(HEX_BASE, i, 0);
  178. }
  179.  
  180.  
  181.  
  182.  
  183. /* Definition of Task Stacks */
  184. #define   TASK_STACKSIZE       2048
  185. OS_STK    task1_stk[TASK_STACKSIZE];
  186. OS_STK    task2_stk[TASK_STACKSIZE];
  187. OS_STK    task3_stk[TASK_STACKSIZE];
  188. OS_STK    task4_stk[TASK_STACKSIZE];
  189. OS_STK    task5_stk[TASK_STACKSIZE];
  190. OS_STK    task6_stk[TASK_STACKSIZE];
  191. OS_STK    task7_stk[TASK_STACKSIZE];
  192.  
  193. OS_EVENT *SWBox1;
  194.  
  195. /* Definition of Task Priorities */
  196.  
  197. #define TASK1_PRIORITY      1
  198. #define TASK2_PRIORITY      2
  199. #define TASK3_PRIORITY      3
  200. #define TASK4_PRIORITY      4
  201. #define TASK5_PRIORITY      5
  202. #define TASK6_PRIORITY      6
  203. #define TASK7_PRIORITY      7
  204.  
  205. // read
  206. void task1(void* pdata)
  207. {
  208.     while (1)
  209.     {
  210.         int sw;
  211.         sw = IORD(SW_SLIDERS_BASE, 0);
  212.         sw = setBit(sw, SW6, 0);
  213.         sw = setBit(sw, SW7, 0);
  214.         sw = setBit(sw, SW8, 0);
  215.         sw = setBit(sw, SW9, 0);
  216.         //printf("%d\n", sw);
  217.         OSMboxPostOpt(SWBox1, &sw, OS_POST_OPT_BROADCAST);
  218.  
  219.         if (getNumOfOnes(sw) > 1)
  220.         {
  221.             clearHexes();
  222.             IOWR(HEX_BASE, 2, E);
  223.             IOWR(HEX_BASE, 1, R);
  224.             IOWR(HEX_BASE, 0, R);
  225.             IOWR(LEDS_BASE, 0, LED9);
  226.         }
  227.         INT8U err;
  228.  
  229.  
  230.         if(sw == LED0)
  231.         {
  232.             clearHexes();
  233.             IOWR(HEX_BASE, 1, TWO);
  234.             IOWR(HEX_BASE, 0, FIVE);
  235.             IOWR(LEDS_BASE, 0, sw);
  236.         }
  237.         else if (sw == 0)
  238.         {
  239.             clearHexes();
  240.             IOWR(LEDS_BASE, 0, 0);
  241.         }
  242.         OSTimeDlyHMSM(0, 0, 1, 0);
  243.       }
  244. }
  245.  
  246.  
  247. void task3(void* pdata)
  248. {
  249.   while (1)
  250.   {
  251.     INT8U err;
  252.     int* num;
  253.     num = OSMboxPend(SWBox1, 0, &err);
  254.     //printf("%d\n", *num);
  255.     if(*num == LED1)
  256.     {
  257.         IOWR(HEX_BASE, 1, THREE);
  258.         IOWR(HEX_BASE, 0, FIVE);
  259.         IOWR(LEDS_BASE, 0, LED1);
  260.     }
  261.     else if (*num == 0)
  262.     {
  263.         clearHexes();
  264.         IOWR(LEDS_BASE, 0, 0);
  265.     }
  266.     OSTimeDlyHMSM(0, 0, 1, 0);
  267.   }
  268. }
  269.  
  270.  
  271. void task4(void* pdata)
  272. {
  273.   while (1)
  274.   {
  275.     INT8U err;
  276.     int* num;
  277.     num = OSMboxPend(SWBox1, 0, &err);
  278.     //printf("%d\n", *num);
  279.     if(*num == LED2)
  280.     {
  281.         clearHexes();
  282.         IOWR(HEX_BASE, 1, FOUR);
  283.         IOWR(HEX_BASE, 0, FIVE);
  284.         IOWR(LEDS_BASE, 0, LED2);
  285.     }
  286.     else if (*num == 0)
  287.     {
  288.         clearHexes();
  289.         IOWR(LEDS_BASE, 0, 0);
  290.     }
  291.     OSTimeDlyHMSM(0, 0, 1, 0);
  292.   }
  293. }
  294.  
  295.  
  296. void task5(void* pdata)
  297. {
  298.   while (1)
  299.   {
  300.     INT8U err;
  301.     int* num;
  302.     num = OSMboxPend(SWBox1, 0, &err);
  303.     //printf("%d\n", *num);
  304.     if(*num == LED3)
  305.     {
  306.         clearHexes();
  307.         IOWR(HEX_BASE, 1, FIVE);
  308.         IOWR(HEX_BASE, 0, FIVE);
  309.         IOWR(LEDS_BASE, 0, LED3);
  310.     }
  311.     else if (*num == 0)
  312.     {
  313.         clearHexes();
  314.         IOWR(LEDS_BASE, 0, 0);
  315.     }
  316.     OSTimeDlyHMSM(0, 0, 1, 0);
  317.   }
  318. }
  319.  
  320.  
  321. void task6(void* pdata)
  322. {
  323.       while (1)
  324.       {
  325.         INT8U err;
  326.         int* num;
  327.         num = OSMboxPend(SWBox1, 0, &err);
  328.         //printf("%d\n", *num);
  329.         if(*num == LED4)
  330.         {
  331.             clearHexes();
  332.             IOWR(HEX_BASE, 1, SEVEN);
  333.             IOWR(HEX_BASE, 0, FIVE);
  334.             IOWR(LEDS_BASE, 0, LED4);
  335.         }
  336.         else if (*num == 0)
  337.         {
  338.             clearHexes();
  339.             IOWR(LEDS_BASE, 0, 0);
  340.         }
  341.         OSTimeDlyHMSM(0, 0, 1, 0);
  342.       }
  343. }
  344.  
  345. void task7(void* pdata)
  346. {
  347.       while (1)
  348.       {
  349.         INT8U err;
  350.         int* num;
  351.         num = OSMboxPend(SWBox1, 0, &err);
  352.         //printf("%d\n", *num);
  353.         if(*num == LED5)
  354.         {
  355.             clearHexes();
  356.             IOWR(HEX_BASE, 1, EIGHT);
  357.             IOWR(HEX_BASE, 0, FIVE);
  358.             IOWR(LEDS_BASE, 0, LED5);
  359.         }
  360.         else if (*num == 0)
  361.         {
  362.             clearHexes();
  363.             IOWR(LEDS_BASE, 0, 0);
  364.         }
  365.         OSTimeDlyHMSM(0, 0, 1, 0);
  366.       }
  367. }
  368.  
  369.  
  370.  
  371. int main(void)
  372. {
  373.     SWBox1 =OSMboxCreate((void*)0);
  374.   printf("start\n");
  375.   OSTaskCreateExt(task1,
  376.                   NULL,
  377.                   (void *)&task1_stk[TASK_STACKSIZE-1],
  378.                   TASK1_PRIORITY,
  379.                   TASK1_PRIORITY,
  380.                   task1_stk,
  381.                   TASK_STACKSIZE,
  382.                   NULL,
  383.                   0);
  384.  
  385.  
  386.  
  387.   OSTaskCreateExt(task3,
  388.                   NULL,
  389.                   (void *)&task3_stk[TASK_STACKSIZE-1],
  390.                   TASK3_PRIORITY,
  391.                   TASK3_PRIORITY,
  392.                   task3_stk,
  393.                   TASK_STACKSIZE,
  394.                   NULL,
  395.                   0);
  396.  
  397.   OSTaskCreateExt(task4,
  398.                   NULL,
  399.                   (void *)&task4_stk[TASK_STACKSIZE-1],
  400.                   TASK4_PRIORITY,
  401.                   TASK4_PRIORITY,
  402.                   task4_stk,
  403.                   TASK_STACKSIZE,
  404.                   NULL,
  405.                   0);
  406.  
  407.   OSTaskCreateExt(task5,
  408.                   NULL,
  409.                   (void *)&task5_stk[TASK_STACKSIZE-1],
  410.                   TASK5_PRIORITY,
  411.                   TASK5_PRIORITY,
  412.                   task5_stk,
  413.                   TASK_STACKSIZE,
  414.                   NULL,
  415.                   0);
  416.  
  417.   OSTaskCreateExt(task6,
  418.                   NULL,
  419.                   (void *)&task6_stk[TASK_STACKSIZE-1],
  420.                   TASK6_PRIORITY,
  421.                   TASK6_PRIORITY,
  422.                   task6_stk,
  423.                   TASK_STACKSIZE,
  424.                   NULL,
  425.                   0);
  426.  
  427.   OSTaskCreateExt(task7,
  428.                   NULL,
  429.                   (void *)&task7_stk[TASK_STACKSIZE-1],
  430.                   TASK7_PRIORITY,
  431.                   TASK7_PRIORITY,
  432.                   task7_stk,
  433.                   TASK_STACKSIZE,
  434.                   NULL,
  435.                   0);
  436.   OSStart();
  437.   return 0;
  438. }
  439.  
  440. /******************************************************************************
  441. *                                                                             *
  442. * License Agreement                                                           *
  443. *                                                                             *
  444. * Copyright (c) 2004 Altera Corporation, San Jose, California, USA.           *
  445. * All rights reserved.                                                        *
  446. *                                                                             *
  447. * Permission is hereby granted, free of charge, to any person obtaining a     *
  448. * copy of this software and associated documentation files (the "Software"),  *
  449. * to deal in the Software without restriction, including without limitation   *
  450. * the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
  451. * and/or sell copies of the Software, and to permit persons to whom the       *
  452. * Software is furnished to do so, subject to the following conditions:        *
  453. *                                                                             *
  454. * The above copyright notice and this permission notice shall be included in  *
  455. * all copies or substantial portions of the Software.                         *
  456. *                                                                             *
  457. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
  458. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
  459. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  460. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
  461. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
  462. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
  463. * DEALINGS IN THE SOFTWARE.                                                   *
  464. *                                                                             *
  465. * This agreement shall be governed in all respects by the laws of the State   *
  466. * of California and by the laws of the United States of America.              *
  467. * Altera does not recommend, suggest or require that this reference design    *
  468. * file be used in conjunction or combination with any other product.          *
  469. ******************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement