led.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * File : led.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2011, RT-Thread Develop 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-03-03 lgnq
  13. */
  14. #include <rtthread.h>
  15. #include <rthw.h>
  16. #include "mb9bf506r.h"
  17. #include "led.h"
  18. void rt_hw_led_on(rt_uint8_t num)
  19. {
  20. RT_ASSERT(num < LEDS_MAX_NUMBER);
  21. switch(num)
  22. {
  23. case 1:
  24. LED_PDOR &= ~LED1;
  25. break;
  26. case 2:
  27. LED_PDOR &= ~LED2;
  28. break;
  29. case 3:
  30. LED_PDOR &= ~LED3;
  31. break;
  32. default:
  33. break;
  34. }
  35. }
  36. void rt_hw_led_off(rt_uint8_t num)
  37. {
  38. RT_ASSERT(num < LEDS_MAX_NUMBER);
  39. switch(num)
  40. {
  41. case 1:
  42. LED_PDOR |= LED1;
  43. break;
  44. case 2:
  45. LED_PDOR |= LED2;
  46. break;
  47. case 3:
  48. LED_PDOR |= LED3;
  49. break;
  50. default:
  51. break;
  52. }
  53. }
  54. void rt_hw_led_toggle(rt_uint8_t num)
  55. {
  56. RT_ASSERT(num < LEDS_MAX_NUMBER);
  57. switch(num)
  58. {
  59. case 1:
  60. if(LED_PDOR&LED1)
  61. LED_PDOR &= ~LED1;
  62. else
  63. LED_PDOR |= LED1;
  64. break;
  65. case 2:
  66. if(LED_PDOR&LED2)
  67. LED_PDOR &= ~LED2;
  68. else
  69. LED_PDOR |= LED2;
  70. break;
  71. case 3:
  72. if(LED_PDOR&LED3)
  73. LED_PDOR &= ~LED3;
  74. else
  75. LED_PDOR |= LED3;
  76. break;
  77. default:
  78. break;
  79. }
  80. }
  81. static rt_err_t led_io_init(void)
  82. {
  83. /*Select CPIO function*/
  84. LED_PFR &= ~LED_MASK;
  85. /*Set Pin to turn off leds*/
  86. LED_PDOR |= LED_MASK;
  87. /*Make led pins outputs*/
  88. LED_DDR |= LED_MASK;
  89. return RT_EOK;
  90. }
  91. static void led1_thread_entry(void *parameter)
  92. {
  93. while(1)
  94. {
  95. rt_hw_led_toggle(1);
  96. rt_thread_delay(10);
  97. }
  98. }
  99. static void led2_thread_entry(void *parameter)
  100. {
  101. while(1)
  102. {
  103. rt_hw_led_toggle(2);
  104. rt_thread_delay(20);
  105. }
  106. }
  107. static rt_thread_t led1_thread;
  108. static rt_thread_t led2_thread;
  109. void rt_hw_led_init(void)
  110. {
  111. led_io_init();
  112. led1_thread = rt_thread_create("led1", led1_thread_entry, RT_NULL, 384, 31, 5);
  113. if(led1_thread != RT_NULL)
  114. rt_thread_startup(led1_thread);
  115. led2_thread = rt_thread_create("led2", led2_thread_entry, RT_NULL, 384, 30, 5);
  116. if(led2_thread != RT_NULL)
  117. rt_thread_startup(led2_thread);
  118. }