application.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * File : application.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, 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. * 2009-01-05 Bernard the first version
  13. * 2010-03-04 Magicoe for LPC1766 version
  14. * 2010-05-02 Aozima add led function
  15. */
  16. /**2q
  17. * @addtogroup LPC17
  18. */
  19. /*@{*/
  20. #include <rtthread.h>
  21. #include "LPC17xx.h"
  22. static void rt_hw_led_init(void)
  23. {
  24. LPC_GPIO2->FIODIR0 |= 1<<0; /* led0:P2.0 */
  25. LPC_GPIO2->FIODIR0 |= 1<<1; /* led1:P2.1 */
  26. }
  27. static void rt_hw_led_on(unsigned int led)
  28. {
  29. switch(led)
  30. {
  31. case 0: /* P2.0 = 1 */
  32. LPC_GPIO2->FIOSET0 = 1<<0;
  33. break;
  34. case 1: /* P2.1 = 1 */
  35. LPC_GPIO2->FIOSET0 = 1<<1;
  36. break;
  37. default:
  38. break;
  39. }
  40. }
  41. static void rt_hw_led_off(unsigned int led)
  42. {
  43. switch(led)
  44. {
  45. case 0: /* P2.0 = 0 */
  46. LPC_GPIO2->FIOCLR0 = 1<<0;
  47. break;
  48. case 1: /* P2.1 = 0 */
  49. LPC_GPIO2->FIOCLR0 = 1<<1;
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. static void rt_thread_entry_led1(void* parameter)
  56. {
  57. /* init led configuration */
  58. rt_hw_led_init();
  59. while (1)
  60. {
  61. /* led on */
  62. rt_kprintf("led1 on\r\n");
  63. rt_hw_led_on(0);
  64. rt_thread_delay(50); /* sleep 0.5 second and switch to other thread */
  65. /* led off */
  66. rt_kprintf("led1 off\r\n");
  67. rt_hw_led_off(0);
  68. rt_thread_delay(50);
  69. }
  70. }
  71. char thread_led2_stack[1024];
  72. struct rt_thread thread_led2;
  73. void rt_thread_entry_led2(void* parameter)
  74. {
  75. unsigned int count=0;
  76. while (1)
  77. {
  78. /* led on */
  79. rt_kprintf("led2 on,count : %d\r\n",count);
  80. count++;
  81. rt_hw_led_on(1);
  82. rt_thread_delay(RT_TICK_PER_SECOND);
  83. /* led off */
  84. rt_kprintf("led2 off\r\n");
  85. rt_hw_led_off(1);
  86. rt_thread_delay(RT_TICK_PER_SECOND);
  87. }
  88. }
  89. int rt_application_init()
  90. {
  91. rt_thread_t thread;
  92. /* create led1 thread */
  93. thread = rt_thread_create("led1",
  94. rt_thread_entry_led1, RT_NULL,
  95. 512,
  96. 20, 5);
  97. if (thread != RT_NULL)
  98. rt_thread_startup(thread);
  99. /* init led2 thread */
  100. rt_thread_init(&thread_led2,
  101. "led2",
  102. rt_thread_entry_led2,
  103. RT_NULL,
  104. &thread_led2_stack[0],
  105. sizeof(thread_led2_stack),10,10);
  106. rt_thread_startup(&thread_led2);
  107. return 0;
  108. }
  109. /*@}*/