led.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. //LED3 is controled by PWM
  90. FM3_GPIO->PFR3 = 0x1000;
  91. FM3_GPIO->EPFR04 = 0x00080000;
  92. FM3_BT2_PWM->TMCR = 0x0018;
  93. FM3_BT2_PWM->TMCR2 = 0x01; /* cks=0b1000 count clk 1/512 */
  94. FM3_BT2_PWM->STC = 0x00;
  95. FM3_BT2_PWM->PCSR = 0x61A; /* Down count = 1562 */
  96. FM3_BT2_PWM->PDUT = 0x0; /* Duty count = 16/1562=10% */
  97. FM3_BT2_PWM->TMCR |= 0x03; /* start base timer(softwere TRG) */
  98. return RT_EOK;
  99. }
  100. void pwm_update(rt_uint16_t value)
  101. {
  102. FM3_BT2_PWM->PDUT = value;
  103. }
  104. static void led1_thread_entry(void *parameter)
  105. {
  106. while (1)
  107. {
  108. rt_hw_led_toggle(1);
  109. rt_thread_delay(RT_TICK_PER_SECOND);
  110. }
  111. }
  112. static void led2_thread_entry(void *parameter)
  113. {
  114. while (1)
  115. {
  116. rt_hw_led_toggle(2);
  117. rt_thread_delay(RT_TICK_PER_SECOND/2);
  118. }
  119. }
  120. static rt_thread_t led1_thread;
  121. static rt_thread_t led2_thread;
  122. void rt_hw_led_init(void)
  123. {
  124. led_io_init();
  125. led1_thread = rt_thread_create("led1", led1_thread_entry, RT_NULL, 384, 29, 5);
  126. if (led1_thread != RT_NULL)
  127. rt_thread_startup(led1_thread);
  128. led2_thread = rt_thread_create("led2", led2_thread_entry, RT_NULL, 384, 30, 5);
  129. if (led2_thread != RT_NULL)
  130. rt_thread_startup(led2_thread);
  131. }