application.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * File : application.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2011-02-14 aozima first implementation for Nios II.
  13. * 2011-03-04 aozima add led.
  14. */
  15. #include <rtthread.h>
  16. #include "board.h"
  17. /**
  18. * @addtogroup NIOS_II
  19. */
  20. /*@{*/
  21. #include "system.h"
  22. #include "altera_avalon_pio_regs.h"
  23. // trun on led n
  24. #define rt_hw_led_on(n) IOWR_ALTERA_AVALON_PIO_DATA(\
  25. LED_BASE,\
  26. IORD_ALTERA_AVALON_PIO_DATA(LED_BASE) | 1<<n)
  27. // trun off led n
  28. #define rt_hw_led_off(n) IOWR_ALTERA_AVALON_PIO_DATA(\
  29. LED_BASE,\
  30. IORD_ALTERA_AVALON_PIO_DATA(LED_BASE) & ~(1<<n) )
  31. ALIGN(RT_ALIGN_SIZE)
  32. static char thread_led1_stack[1024];
  33. struct rt_thread thread_led1;
  34. static void rt_thread_entry_led1(void* parameter)
  35. {
  36. unsigned int count=0;
  37. while (1)
  38. {
  39. /* led1 on */
  40. #ifndef RT_USING_FINSH
  41. rt_kprintf("led1 on,count : %d\r\n",count);
  42. #endif
  43. count++;
  44. rt_hw_led_on(1);
  45. /* sleep 0.5 second and switch to other thread */
  46. rt_thread_delay(RT_TICK_PER_SECOND/2);
  47. /* led1 off */
  48. #ifndef RT_USING_FINSH
  49. rt_kprintf("led1 off\r\n");
  50. #endif
  51. rt_hw_led_off(1);
  52. rt_thread_delay(RT_TICK_PER_SECOND/2);
  53. }
  54. }
  55. ALIGN(RT_ALIGN_SIZE)
  56. static char thread_led2_stack[1024];
  57. struct rt_thread thread_led2;
  58. void rt_thread_entry_led2(void* parameter)
  59. {
  60. unsigned int count=0;
  61. while (1)
  62. {
  63. /* led2 on */
  64. #ifndef RT_USING_FINSH
  65. rt_kprintf("led2 on,count : %d\r\n",count);
  66. #endif
  67. count++;
  68. rt_hw_led_on(2);
  69. rt_thread_delay(RT_TICK_PER_SECOND);
  70. /* led2 off */
  71. #ifndef RT_USING_FINSH
  72. rt_kprintf("led2 off\r\n");
  73. #endif
  74. rt_hw_led_off(2);
  75. rt_thread_delay(RT_TICK_PER_SECOND);
  76. }
  77. }
  78. int rt_application_init()
  79. {
  80. // led_init();
  81. //------- init led1 thread
  82. rt_thread_init(&thread_led1,
  83. "led1",
  84. rt_thread_entry_led1,
  85. RT_NULL,
  86. &thread_led1_stack[0],
  87. sizeof(thread_led1_stack),11,5);
  88. rt_thread_startup(&thread_led1);
  89. //------- init led2 thread
  90. rt_thread_init(&thread_led2,
  91. "led2",
  92. rt_thread_entry_led2,
  93. RT_NULL,
  94. &thread_led2_stack[0],
  95. sizeof(thread_led2_stack),12,5);
  96. rt_thread_startup(&thread_led2);
  97. return 0;
  98. }
  99. /*@}*/