Advertisement
Guest User

Untitled

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