Advertisement
Guest User

Untitled

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