led.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. USER_LED_PDOR &= ~USER_LED1;
  25. break;
  26. case 2:
  27. USER_LED_PDOR &= ~USER_LED2;
  28. break;
  29. case 3:
  30. USER_LED_PDOR &= ~USER_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. USER_LED_PDOR |= USER_LED1;
  43. break;
  44. case 2:
  45. USER_LED_PDOR |= USER_LED2;
  46. break;
  47. case 3:
  48. USER_LED_PDOR |= USER_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 (USER_LED_PDOR&USER_LED1)
  61. USER_LED_PDOR &= ~USER_LED1;
  62. else
  63. USER_LED_PDOR |= USER_LED1;
  64. break;
  65. case 2:
  66. if (USER_LED_PDOR&USER_LED2)
  67. USER_LED_PDOR &= ~USER_LED2;
  68. else
  69. USER_LED_PDOR |= USER_LED2;
  70. break;
  71. case 3:
  72. if (USER_LED_PDOR&USER_LED3)
  73. USER_LED_PDOR &= ~USER_LED3;
  74. else
  75. USER_LED_PDOR |= USER_LED3;
  76. break;
  77. default:
  78. break;
  79. }
  80. }
  81. void led_init(void)
  82. {
  83. /*Select CPIO function*/
  84. USER_LED_PFR &= ~USER_LED_MASK;
  85. /* disable analog input */
  86. FM3_GPIO->ADE &= ~USER_LED_MASK;
  87. /*Set CPIO Pull-Up function*/
  88. USER_LED_PCR |= USER_LED_MASK;
  89. /*Make led pins outputs*/
  90. USER_LED_DDR |= USER_LED_MASK;
  91. USER_LED_PDOR |= USER_LED_MASK;
  92. }
  93. void pwm_update(rt_uint16_t value)
  94. {
  95. FM3_BT2_PWM->PDUT = value;
  96. }
  97. static void led1_thread_entry(void *parameter)
  98. {
  99. while (1)
  100. {
  101. rt_hw_led_toggle(1);
  102. rt_thread_delay(RT_TICK_PER_SECOND);
  103. }
  104. }
  105. static void led2_thread_entry(void *parameter)
  106. {
  107. while (1)
  108. {
  109. rt_hw_led_toggle(2);
  110. rt_thread_delay(RT_TICK_PER_SECOND/2);
  111. }
  112. }
  113. static rt_thread_t led1_thread;
  114. static rt_thread_t led2_thread;
  115. void rt_hw_led_init(void)
  116. {
  117. led_init();
  118. led1_thread = rt_thread_create("led1", led1_thread_entry, RT_NULL, 384, 29, 5);
  119. if (led1_thread != RT_NULL)
  120. rt_thread_startup(led1_thread);
  121. led2_thread = rt_thread_create("led2", led2_thread_entry, RT_NULL, 384, 30, 5);
  122. if (led2_thread != RT_NULL)
  123. rt_thread_startup(led2_thread);
  124. }