led.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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-03-03 lgnq
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include "mb9bf506r.h"
  13. #include "led.h"
  14. void rt_hw_led_on(rt_uint8_t num)
  15. {
  16. RT_ASSERT(num < LEDS_MAX_NUMBER);
  17. switch (num)
  18. {
  19. case 1:
  20. LED_PDOR &= ~LED1;
  21. break;
  22. case 2:
  23. LED_PDOR &= ~LED2;
  24. break;
  25. case 3:
  26. LED_PDOR &= ~LED3;
  27. break;
  28. default:
  29. break;
  30. }
  31. }
  32. void rt_hw_led_off(rt_uint8_t num)
  33. {
  34. RT_ASSERT(num < LEDS_MAX_NUMBER);
  35. switch (num)
  36. {
  37. case 1:
  38. LED_PDOR |= LED1;
  39. break;
  40. case 2:
  41. LED_PDOR |= LED2;
  42. break;
  43. case 3:
  44. LED_PDOR |= LED3;
  45. break;
  46. default:
  47. break;
  48. }
  49. }
  50. void rt_hw_led_toggle(rt_uint8_t num)
  51. {
  52. RT_ASSERT(num < LEDS_MAX_NUMBER);
  53. switch (num)
  54. {
  55. case 1:
  56. if (LED_PDOR&LED1)
  57. LED_PDOR &= ~LED1;
  58. else
  59. LED_PDOR |= LED1;
  60. break;
  61. case 2:
  62. if (LED_PDOR&LED2)
  63. LED_PDOR &= ~LED2;
  64. else
  65. LED_PDOR |= LED2;
  66. break;
  67. case 3:
  68. if (LED_PDOR&LED3)
  69. LED_PDOR &= ~LED3;
  70. else
  71. LED_PDOR |= LED3;
  72. break;
  73. default:
  74. break;
  75. }
  76. }
  77. static rt_err_t led_io_init(void)
  78. {
  79. /*Select CPIO function*/
  80. LED_PFR &= ~LED_MASK;
  81. /*Set Pin to turn off leds*/
  82. LED_PDOR |= LED_MASK;
  83. /*Make led pins outputs*/
  84. LED_DDR |= LED_MASK;
  85. //LED3 is controled by PWM
  86. FM3_GPIO->PFR3 = 0x1000;
  87. FM3_GPIO->EPFR04 = 0x00080000;
  88. FM3_BT2_PWM->TMCR = 0x0018;
  89. FM3_BT2_PWM->TMCR2 = 0x01; /* cks=0b1000 count clk 1/512 */
  90. FM3_BT2_PWM->STC = 0x00;
  91. FM3_BT2_PWM->PCSR = 0x61A; /* Down count = 1562 */
  92. FM3_BT2_PWM->PDUT = 0x0; /* Duty count = 16/1562=10% */
  93. FM3_BT2_PWM->TMCR |= 0x03; /* start base timer(softwere TRG) */
  94. return RT_EOK;
  95. }
  96. void pwm_update(rt_uint16_t value)
  97. {
  98. FM3_BT2_PWM->PDUT = value;
  99. }
  100. static void led1_thread_entry(void *parameter)
  101. {
  102. while (1)
  103. {
  104. rt_hw_led_toggle(1);
  105. rt_thread_delay(RT_TICK_PER_SECOND);
  106. }
  107. }
  108. static void led2_thread_entry(void *parameter)
  109. {
  110. while (1)
  111. {
  112. rt_hw_led_toggle(2);
  113. rt_thread_delay(RT_TICK_PER_SECOND/2);
  114. }
  115. }
  116. static rt_thread_t led1_thread;
  117. static rt_thread_t led2_thread;
  118. void rt_hw_led_init(void)
  119. {
  120. led_io_init();
  121. led1_thread = rt_thread_create("led1", led1_thread_entry, RT_NULL, 384, 29, 5);
  122. if (led1_thread != RT_NULL)
  123. rt_thread_startup(led1_thread);
  124. led2_thread = rt_thread_create("led2", led2_thread_entry, RT_NULL, 384, 30, 5);
  125. if (led2_thread != RT_NULL)
  126. rt_thread_startup(led2_thread);
  127. }