application.c 2.5 KB

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