Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.13 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.  
  31. #include <io.h>
  32. #include <system.h>
  33. #include <stdio.h>
  34. #include "includes.h"
  35.  
  36. #define SW0 0x00000001
  37. #define SW1 0x00000002
  38. #define SW2 0x00000004
  39. #define SW3 0x00000008
  40. #define SW4 0x00000010
  41. #define SW5 0x00000020
  42. #define SW6 0x00000040
  43. #define SW7 0x00000080
  44. #define SW8 0x00000100
  45. #define SW9 0x00000200
  46.  
  47. // pushbuttons
  48. #define KEY1 0x0000000E
  49. #define KEY2 0x00000004
  50. #define KEY3 0x00000008
  51. #define KEY4 0x00000010
  52.  
  53. // leds
  54. #define LED0 0x00000001
  55. #define LED1 0x00000002
  56. #define LED2 0x00000004
  57. #define LED3 0x00000008
  58. #define LED4 0x00000010
  59. #define LED5 0x00000020
  60. #define LED6 0x00000040
  61. #define LED7 0x00000080
  62. #define LED8 0x00000100
  63. #define LED9 0x00000200
  64.  
  65. // hex
  66. #define SEGA 0x00001
  67. #define SEGB 0x00002
  68. #define SEGC 0x00004
  69. #define SEGD 0x00008
  70. #define SEGE 0x00010
  71. #define SEGF 0x00020
  72. #define SEGG 0x00040
  73.  
  74.  
  75. // hex - numbers
  76. #define ZERO SEGA | SEGB | SEGC | SEGD |SEGE | SEGF
  77. #define ONE SEGB | SEGC
  78. #define TWO SEGA | SEGB | SEGG | SEGE | SEGD
  79. #define THREE SEGA | SEGB | SEGC | SEGD | SEGG
  80. #define FOUR SEGA | SEGC | SEGG | SEGF
  81. #define FIVE SEGA | SEGC | SEGD | SEGF | SEGG
  82. #define SIX SEGA | SEGC | SEGD | SEGE | SEGF | SEGG
  83. #define SEVEN SEGA | SEGB | SEGC
  84. #define EIGHT SEGA | SEGB | SEGC | SEGD |SEGE | SEGF | SEGG
  85. #define NINE SEGA | SEGB | SEGC | SEGD | SEGF
  86. #define E_HEX SEGA | SEGD |SEGE | SEGF | SEGG
  87. #define R_HEX SEGE | SEGG
  88.  
  89.  
  90.  
  91. typedef enum TEMP_LEVEL {
  92. POZIOM_0 = 0,
  93. POZIOM_1 = 2,
  94. POZIOM_2 = 4,
  95. POZIOM_3 = 8,
  96. POZIOM_4 = 16,
  97. POZIOM_5 = 32,
  98. POZIOM_6 = 64,
  99. POZIOM_7 = 128,
  100. POZIOM_8 = 256,
  101. POZIOM_9 = 512,
  102. POZIOM_10 = 1024,
  103.  
  104. } TEMP;
  105.  
  106. /* Definition of Task Stacks */
  107. #define TASK_STACKSIZE 2048
  108. OS_STK task1_stk[TASK_STACKSIZE];
  109. OS_STK task2_stk[TASK_STACKSIZE];
  110. OS_STK task3_stk[TASK_STACKSIZE];
  111. OS_STK task4_stk[TASK_STACKSIZE];
  112. OS_STK task5_stk[TASK_STACKSIZE];
  113.  
  114. /* Definition of Task Priorities */
  115.  
  116. #define TASK1_PRIORITY 1
  117. #define TASK2_PRIORITY 2
  118. #define TASK3_PRIORITY 3
  119. #define TASK4_PRIORITY 4
  120. #define TASK5_PRIORITY 5
  121.  
  122.  
  123. OS_EVENT *SWBox1;
  124.  
  125. int get_hex(int value){
  126. switch(value) {
  127. case 0:
  128. return ZERO;
  129. break;
  130. case 1:
  131. return ONE;
  132. break;
  133. case 2:
  134. return TWO;
  135. break;
  136. case 3:
  137. return THREE;
  138. break;
  139. case 4:
  140. return FOUR;
  141. break;
  142. case 5:
  143. return FIVE;
  144. break;
  145. case 6:
  146. return SIX;
  147. break;
  148. case 7:
  149. return SEVEN;
  150. break;
  151. case 8:
  152. return EIGHT;
  153. break;
  154. case 9:
  155. return NINE;
  156. break;
  157. default:
  158. return SEGA;
  159. }
  160. }
  161.  
  162. struct data{
  163. int room1;
  164. int room2;
  165. int room3;
  166. int room4;
  167. int led;
  168. int err;
  169. };
  170.  
  171.  
  172. /* Prints "Hello World" and sleeps for three seconds */
  173. void task1(void* pdata) {
  174. int sw_state;
  175. int kierunek = 0;
  176. int flagDirection = 0;
  177. int hexs[6]={0,0,0,0,0,0};
  178. int rooms[5]={0,0,0,0,0};
  179. int flag0;
  180. int errFlag = 0;
  181. int leds;
  182. struct data d;
  183. while(1)
  184. {
  185.  
  186. printf("%d\n",rooms[1]);
  187. d.room1=rooms[1];
  188. d.room2=rooms[2];
  189. d.room3=rooms[3];
  190. d.room4=rooms[4];
  191. d.led = leds;
  192. d.err = errFlag;
  193. OSMboxPostOpt(SWBox1, &d, OS_POST_OPT_BROADCAST); // wyslij wiadomosc do innych taskow
  194.  
  195. sw_state = IORD(SW_SLIDERS_BASE,0);
  196. leds = IORD(LEDS_BASE,0);
  197. kierunek = IORD(PUSHBUTTONS_BASE,0);
  198. if(kierunek == KEY1){
  199. if(flagDirection==0){
  200. leds |= LED9;
  201. flagDirection = 1;
  202. } else {
  203. leds &= ~(LED9);
  204. flagDirection = 0;
  205. }
  206. }
  207.  
  208.  
  209. switch(sw_state){
  210. case SW0:
  211. if (flag0 == 0) {
  212. if (flagDirection == 0) {
  213. if (rooms[0] == 0)
  214. errFlag = 1;
  215. if (rooms[0] > 0) {
  216. errFlag = 0;
  217. rooms[0]--;
  218. rooms[4]++;
  219. }
  220. } else {
  221. if (rooms[4] == 0)
  222. errFlag = 1;
  223. if (rooms[4] > 0) {
  224. rooms[0]++;
  225. rooms[4]--;
  226. errFlag = 0;
  227. }
  228. }
  229. flag0 = 1;
  230. }
  231.  
  232. break;
  233. case SW1:
  234. if (flag0 == 0) {
  235. if (flagDirection == 0) {
  236. if (rooms[4] == 0)
  237. errFlag = 1;
  238. if (rooms[4] > 0) {
  239. errFlag = 0;
  240. rooms[4]--;
  241. rooms[3]++;
  242. }
  243. } else {
  244. if (rooms[3] == 0)
  245. errFlag = 1;
  246. if (rooms[3] > 0) {
  247. rooms[4]++;
  248. rooms[3]--;
  249. errFlag = 0;
  250. }
  251. }
  252. flag0 = 1;
  253. }
  254.  
  255. break;
  256. case SW2:
  257. if (flag0 == 0) {
  258. if (flagDirection == 0) {
  259. if (rooms[3] == 0)
  260. errFlag = 1;
  261. if (rooms[3] > 0) {
  262. errFlag = 0;
  263. rooms[3]--;
  264. }
  265. } else {
  266. rooms[3]++;
  267. errFlag = 0;
  268. }
  269. flag0 = 1;
  270. }
  271.  
  272. break;
  273. case SW3:
  274. if (flag0 == 0) {
  275. if (flagDirection == 0) {
  276. errFlag = 0;
  277. rooms[0]++;
  278. }
  279.  
  280. else {
  281. if (rooms[0] == 0)
  282. errFlag = 1;
  283. if (rooms[0] > 0) {
  284. rooms[0]--;
  285. errFlag = 0;
  286. }
  287. }
  288. flag0 = 1;
  289. }
  290.  
  291. break;
  292. case SW4:
  293. if (flag0 == 0) {
  294. if (flagDirection == 0) {
  295. if (rooms[4] == 0)
  296. errFlag = 1;
  297. if (rooms[4] > 0) {
  298. errFlag = 0;
  299. rooms[4]--;
  300. }
  301. } else {
  302. rooms[4]++;
  303. errFlag = 0;
  304.  
  305. }
  306. flag0 = 1;
  307. }
  308.  
  309. break;
  310. case SW5:
  311. if (flag0 == 0) {
  312. if (flagDirection == 1) {
  313. errFlag = 0;
  314. rooms[3]++;
  315.  
  316. } else {
  317. if (rooms[3] == 0)
  318. errFlag = 1;
  319. if (rooms[3] > 0) {
  320. rooms[3]--;
  321. errFlag = 0;
  322. }
  323. }
  324. flag0 = 1;
  325. }
  326.  
  327. break;
  328. case SW6:
  329. if (flag0 == 0) {
  330. if (flagDirection == 0) {
  331. if (rooms[1] == 0)
  332. errFlag = 1;
  333. if (rooms[1] > 0) {
  334.  
  335. errFlag = 0;
  336. rooms[1]--;
  337. rooms[3]++;
  338. }
  339. } else {
  340. if (rooms[3] == 0)
  341. errFlag = 1;
  342. if (rooms[3] > 0) {
  343. rooms[1]++;
  344. rooms[3]--;
  345. errFlag = 0;
  346. }
  347. }
  348. flag0 = 1;
  349. }
  350.  
  351. break;
  352. case SW7:
  353. if (flag0 == 0) {
  354. if (flagDirection == 0) {
  355. if (rooms[1] == 0)
  356. errFlag = 1;
  357. if (rooms[1] != 0) {
  358.  
  359. errFlag = 0;
  360. rooms[1]--;
  361. rooms[2]++;
  362. }
  363. } else {
  364. if (rooms[2] == 0)
  365. errFlag = 1;
  366. if (rooms[2] != 0) {
  367. rooms[1]++;
  368. rooms[2]--;
  369. errFlag = 0;
  370. }
  371. }
  372. flag0 = 1;
  373. }
  374.  
  375. break;
  376.  
  377. default:
  378. flag0 = 0;
  379. break;
  380. }
  381.  
  382. if (rooms[0] > 0) {
  383. leds |= LED0;
  384. } else {
  385. leds &= ~(LED0);
  386. }
  387.  
  388. if (rooms[1] > 0) {
  389. leds |= LED1;
  390. } else {
  391. leds &= ~(LED1);
  392. }
  393. if (rooms[2] > 0) {
  394. leds |= LED2;
  395. } else {
  396. leds &= ~(LED2);
  397. }
  398. if (rooms[3] > 0) {
  399. leds |= LED3;
  400. } else {
  401. leds &= ~(LED3);
  402. }
  403. if (rooms[4] > 0) {
  404. leds |= LED4;
  405. } else {
  406. leds &= ~(LED4);
  407. }
  408.  
  409.  
  410. if(rooms[0] >= 0)
  411. {
  412. hexs[0] = get_hex(rooms[0]);
  413. }
  414. if(errFlag == 1)
  415. {
  416. IOWR(HEX_BASE,3,R_HEX);
  417. IOWR(HEX_BASE,4,R_HEX);
  418. IOWR(HEX_BASE,5,E_HEX);
  419. leds |= LED8;
  420. }
  421. else
  422. {
  423. leds &= ~(LED8);
  424. IOWR(HEX_BASE,0,hexs[0]);
  425. IOWR(HEX_BASE,5,ZERO);
  426. }
  427.  
  428.  
  429.  
  430. IOWR(LEDS_BASE,0,leds);
  431.  
  432. OSTimeDlyHMSM(0, 0, 0, 10);
  433. }
  434.  
  435. }
  436.  
  437. /* Prints "Hello World" and sleeps for three seconds */
  438. void task2(void* pdata) {
  439. while (1) {
  440. INT8U err;
  441. struct data *temp;
  442. temp = OSMboxPend(SWBox1, 0, &err);
  443.  
  444. int rooms = temp->room1;
  445.  
  446.  
  447. int errFlag = temp->err;
  448. int hexs;
  449. if(rooms >= 0)
  450. {
  451. hexs = get_hex(rooms);
  452. }
  453. if(errFlag == 0)
  454. {
  455. IOWR(HEX_BASE,1,hexs);
  456. }
  457. OSTimeDlyHMSM(0, 0, 0, 10);
  458.  
  459. }
  460. }
  461. void task3(void* pdata) {
  462. while (1) {
  463. INT8U err;
  464. struct data *temp;
  465. temp = OSMboxPend(SWBox1, 0, &err);
  466. int rooms = temp->room2;
  467. int errFlag = temp->err;
  468. // printf("T=%d\n", leds);
  469. int hexs;
  470. if(rooms >= 0)
  471. {
  472. hexs = get_hex(rooms);
  473. }
  474. if(errFlag == 0)
  475. {
  476. IOWR(HEX_BASE,2,hexs);
  477. }
  478. OSTimeDlyHMSM(0, 0, 0, 10);
  479.  
  480. }
  481. }
  482. void task4(void* pdata) {
  483. while (1) {
  484. INT8U err;
  485. struct data *temp;
  486. temp = OSMboxPend(SWBox1, 0, &err);
  487.  
  488. int rooms = temp->room3;
  489. int errFlag = temp->err;
  490.  
  491. // printf("T=%d\n", leds);
  492. int hexs;
  493. if(rooms >= 0)
  494. {
  495. hexs = get_hex(rooms);
  496. }
  497. if(errFlag == 0)
  498. {
  499. IOWR(HEX_BASE,3,hexs);
  500. }
  501. OSTimeDlyHMSM(0, 0, 0, 10);
  502.  
  503. }
  504. }
  505. void task5(void* pdata) {
  506. while (1) {
  507. INT8U err;
  508. struct data *temp;
  509. temp = OSMboxPend(SWBox1, 0, &err);
  510.  
  511. int rooms = temp->room4;
  512. int errFlag = temp->err;
  513. // printf("T=%d\n", leds);
  514. int hexs;
  515. if(rooms >= 0)
  516. {
  517. hexs = get_hex(rooms);
  518. }
  519. if(errFlag == 0)
  520. {
  521. IOWR(HEX_BASE,4,hexs);
  522. }
  523. OSTimeDlyHMSM(0, 0, 0, 10);
  524.  
  525. }
  526. }
  527.  
  528.  
  529.  
  530. /* The main function creates two task and starts multi-tasking */
  531. int main(void) {
  532.  
  533. SWBox1 = OSMboxCreate((void*) 0);
  534.  
  535. OSTaskCreateExt(task1,
  536. NULL, (void *) &task1_stk[TASK_STACKSIZE - 1],
  537. TASK1_PRIORITY,
  538. TASK1_PRIORITY, task1_stk,
  539. TASK_STACKSIZE,
  540. NULL, 0);
  541.  
  542. OSTaskCreateExt(task2,
  543. NULL, (void *) &task2_stk[TASK_STACKSIZE - 1],
  544. TASK2_PRIORITY,
  545. TASK2_PRIORITY, task2_stk,
  546. TASK_STACKSIZE,
  547. NULL, 0);
  548.  
  549. OSTaskCreateExt(task3,
  550. NULL, (void *) &task3_stk[TASK_STACKSIZE - 1],
  551. TASK3_PRIORITY,
  552. TASK3_PRIORITY, task3_stk,
  553. TASK_STACKSIZE,
  554. NULL, 0);
  555.  
  556. OSTaskCreateExt(task4,
  557. NULL, (void *) &task4_stk[TASK_STACKSIZE - 1],
  558. TASK4_PRIORITY,
  559. TASK4_PRIORITY, task4_stk,
  560. TASK_STACKSIZE,
  561. NULL, 0);
  562.  
  563. OSTaskCreateExt(task5,
  564. NULL, (void *) &task5_stk[TASK_STACKSIZE - 1],
  565. TASK5_PRIORITY,
  566. TASK5_PRIORITY, task5_stk,
  567. TASK_STACKSIZE,
  568. NULL, 0);
  569.  
  570. OSStart();
  571. return 0;
  572. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement